source: mainline/kernel/generic/src/mm/frame.c@ c5396c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c5396c1 was c5396c1, checked in by Martin Decky <martin@…>, 12 years ago

abandon the 2nd level bitmap
according to observations it is very rare to have allocation requests for more than 8 frames, therefore the conservative update of the 2nd level bitmap can hardly provide any benefits
the natural blocking of the bitmap (by bytes) should provide a reasonable performance

  • Property mode set to 100644
File size: 31.4 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2005 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
[5f0f29ce]4 * Copyright (c) 2009 Martin Decky
[f761f1eb]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[cc73a8a1]31/** @addtogroup genericmm
[b45c443]32 * @{
33 */
34
[9179d0a]35/**
[b45c443]36 * @file
[5f0f29ce]37 * @brief Physical frame allocator.
[9179d0a]38 *
39 * This file contains the physical frame allocator and memory zone management.
[b0c2075]40 * The frame allocator is built on top of the two-level bitmap structure.
[9179d0a]41 *
42 */
43
[d99c1d2]44#include <typedefs.h>
[f761f1eb]45#include <mm/frame.h>
[89bcb520]46#include <mm/reserve.h>
[20d50a1]47#include <mm/as.h>
[f761f1eb]48#include <panic.h>
[fcacfb7]49#include <debug.h>
[5c9a08b]50#include <adt/list.h>
[1a1744e]51#include <synch/mutex.h>
52#include <synch/condvar.h>
[18e0a6c]53#include <arch/asm.h>
[9c0a9b3]54#include <arch.h>
[328f2934]55#include <print.h>
[9ebc238]56#include <align.h>
[085d973]57#include <mm/slab.h>
[bb68433]58#include <bitops.h>
[93165be]59#include <macros.h>
[d630139]60#include <config.h>
[933cadf]61#include <str.h>
[18e0a6c]62
[e49e234]63zones_t zones;
[f761f1eb]64
[1a1744e]65/*
66 * Synchronization primitives used to sleep when there is no memory
67 * available.
68 */
[da1bafb]69static mutex_t mem_avail_mtx;
70static condvar_t mem_avail_cv;
71static size_t mem_avail_req = 0; /**< Number of frames requested. */
72static size_t mem_avail_gen = 0; /**< Generation counter. */
[a294ad0]73
[71eef11]74/********************/
[085d973]75/* Helper functions */
[71eef11]76/********************/
77
[7a0359b]78NO_TRACE static inline size_t frame_index(zone_t *zone, frame_t *frame)
[085d973]79{
[98000fb]80 return (size_t) (frame - zone->frames);
[a294ad0]81}
[71eef11]82
[7a0359b]83NO_TRACE static inline size_t frame_index_abs(zone_t *zone, frame_t *frame)
[f761f1eb]84{
[98000fb]85 return (size_t) (frame - zone->frames) + zone->base;
[f761f1eb]86}
[71eef11]87
[7a0359b]88NO_TRACE static inline bool frame_index_valid(zone_t *zone, size_t index)
[a294ad0]89{
[6c441cf8]90 return (index < zone->count);
[a294ad0]91}
92
[deaf8d5]93/** Initialize frame structure.
[84dd253]94 *
[5f0f29ce]95 * @param frame Frame structure to be initialized.
96 *
[f761f1eb]97 */
[7a0359b]98NO_TRACE static void frame_initialize(frame_t *frame)
[f761f1eb]99{
[b0c2075]100 frame->refcount = 0;
101 frame->parent = NULL;
[f761f1eb]102}
103
[5f0f29ce]104/*******************/
105/* Zones functions */
106/*******************/
[085d973]107
[deaf8d5]108/** Insert-sort zone into zones list.
[bb68433]109 *
[5f0f29ce]110 * Assume interrupts are disabled and zones lock is
111 * locked.
112 *
113 * @param base Base frame of the newly inserted zone.
114 * @param count Number of frames of the newly inserted zone.
115 *
116 * @return Zone number on success, -1 on error.
117 *
[84dd253]118 */
[e2650d3]119NO_TRACE static size_t zones_insert_zone(pfn_t base, size_t count,
120 zone_flags_t flags)
[f761f1eb]121{
[71eef11]122 if (zones.count + 1 == ZONES_MAX) {
123 printf("Maximum zone count %u exceeded!\n", ZONES_MAX);
[98000fb]124 return (size_t) -1;
[71eef11]125 }
126
[98000fb]127 size_t i;
[b6b576c]128 for (i = 0; i < zones.count; i++) {
[5f0f29ce]129 /* Check for overlap */
[e2650d3]130 if (overlaps(zones.info[i].base, zones.info[i].count,
131 base, count)) {
132
133 /*
134 * If the overlaping zones are of the same type
135 * and the new zone is completely within the previous
136 * one, then quietly ignore the new zone.
137 *
138 */
139
140 if ((zones.info[i].flags != flags) ||
141 (!iswithin(zones.info[i].base, zones.info[i].count,
142 base, count))) {
[7e752b2]143 printf("Zone (%p, %p) overlaps "
144 "with previous zone (%p %p)!\n",
145 (void *) PFN2ADDR(base), (void *) PFN2ADDR(count),
146 (void *) PFN2ADDR(zones.info[i].base),
147 (void *) PFN2ADDR(zones.info[i].count));
[e2650d3]148 }
149
[98000fb]150 return (size_t) -1;
[085d973]151 }
[5f0f29ce]152 if (base < zones.info[i].base)
[bb68433]153 break;
[085d973]154 }
[71eef11]155
[bb68433]156 /* Move other zones up */
[b0c2075]157 for (size_t j = zones.count; j > i; j--)
[e49e234]158 zones.info[j] = zones.info[j - 1];
[71eef11]159
[bb68433]160 zones.count++;
[71eef11]161
[bb68433]162 return i;
[f761f1eb]163}
[fcacfb7]164
[5f0f29ce]165/** Get total available frames.
166 *
167 * Assume interrupts are disabled and zones lock is
168 * locked.
[71eef11]169 *
[5f0f29ce]170 * @return Total number of available frames.
[71eef11]171 *
[eef75f6]172 */
[8d308b9]173NO_TRACE static size_t frame_total_free_get_internal(void)
[085d973]174{
[98000fb]175 size_t total = 0;
176 size_t i;
[8d308b9]177
[5f0f29ce]178 for (i = 0; i < zones.count; i++)
179 total += zones.info[i].free_count;
[328f2934]180
[5f0f29ce]181 return total;
182}
[8d308b9]183
184NO_TRACE size_t frame_total_free_get(void)
185{
186 size_t total;
187
188 irq_spinlock_lock(&zones.lock, true);
189 total = frame_total_free_get_internal();
190 irq_spinlock_unlock(&zones.lock, true);
191
192 return total;
193}
194
[4457455]195
[e49e234]196/** Find a zone with a given frames.
[5f0f29ce]197 *
198 * Assume interrupts are disabled and zones lock is
199 * locked.
200 *
201 * @param frame Frame number contained in zone.
[e49e234]202 * @param count Number of frames to look for.
[5f0f29ce]203 * @param hint Used as zone hint.
204 *
205 * @return Zone index or -1 if not found.
206 *
207 */
[97bdb4a]208NO_TRACE size_t find_zone(pfn_t frame, size_t count, size_t hint)
[5f0f29ce]209{
[6c441cf8]210 if (hint >= zones.count)
[085d973]211 hint = 0;
[328f2934]212
[98000fb]213 size_t i = hint;
[085d973]214 do {
[5f0f29ce]215 if ((zones.info[i].base <= frame)
[e49e234]216 && (zones.info[i].base + zones.info[i].count >= frame + count))
[5f0f29ce]217 return i;
218
[085d973]219 i++;
220 if (i >= zones.count)
221 i = 0;
[da1bafb]222
[5f7a0ef]223 } while (i != hint);
[5f0f29ce]224
[98000fb]225 return (size_t) -1;
[085d973]226}
[328f2934]227
[b0c2075]228/** @return True if zone can allocate specified number of frames */
229NO_TRACE static bool zone_can_alloc(zone_t *zone, size_t count,
230 pfn_t constraint)
[bb68433]231{
[b0c2075]232 /*
233 * The function bitmap_allocate_range() does not modify
234 * the bitmap if the last argument is NULL.
235 */
[e6c4b94]236 return ((zone->flags & ZONE_AVAILABLE) &&
[b0c2075]237 bitmap_allocate_range(&zone->bitmap, count, zone->base,
238 constraint, NULL));
[bb68433]239}
240
[b0c2075]241/** Find a zone that can allocate specified number of frames
[5f0f29ce]242 *
243 * Assume interrupts are disabled and zones lock is
244 * locked.
[fcacfb7]245 *
[b0c2075]246 * @param count Number of free frames we are trying to find.
247 * @param flags Required flags of the target zone.
248 * @param constraint Indication of bits that cannot be set in the
249 * physical frame number of the first allocated frame.
250 * @param hind Preferred zone.
[fcacfb7]251 *
252 */
[b0c2075]253NO_TRACE static size_t find_free_zone(size_t count, zone_flags_t flags,
254 pfn_t constraint, size_t hint)
[fcacfb7]255{
[085d973]256 if (hint >= zones.count)
257 hint = 0;
[5f0f29ce]258
[98000fb]259 size_t i = hint;
[085d973]260 do {
[5f7a0ef]261 /*
262 * Check whether the zone meets the search criteria.
263 */
[e6c4b94]264 if (ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) {
[5f7a0ef]265 /*
[b0c2075]266 * Check if the zone can satisfy the allocation request.
[5f7a0ef]267 */
[b0c2075]268 if (zone_can_alloc(&zones.info[i], count, constraint))
[5f0f29ce]269 return i;
[328f2934]270 }
[5f0f29ce]271
272 i++;
273 if (i >= zones.count)
[085d973]274 i = 0;
[da1bafb]275
[5f7a0ef]276 } while (i != hint);
[5f0f29ce]277
[98000fb]278 return (size_t) -1;
[fcacfb7]279}
280
[b0c2075]281/******************/
282/* Zone functions */
283/******************/
[6e8b3c8]284
[b0c2075]285/** Return frame from zone. */
286NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t index)
[bb68433]287{
[b0c2075]288 ASSERT(index < zone->count);
[b87f418]289
[b0c2075]290 return &zone->frames[index];
[6e8b3c8]291}
292
[deaf8d5]293/** Allocate frame in particular zone.
[085d973]294 *
[5f0f29ce]295 * Assume zone is locked and is available for allocation.
[9a68b34d]296 * Panics if allocation is impossible.
297 *
[b0c2075]298 * @param zone Zone to allocate from.
299 * @param count Number of frames to allocate
300 * @param constraint Indication of bits that cannot be set in the
301 * physical frame number of the first allocated frame.
[085d973]302 *
[5f0f29ce]303 * @return Frame index in zone.
[9a68b34d]304 *
[085d973]305 */
[b0c2075]306NO_TRACE static size_t zone_frame_alloc(zone_t *zone, size_t count,
307 pfn_t constraint)
[085d973]308{
[e6c4b94]309 ASSERT(zone->flags & ZONE_AVAILABLE);
[5f0f29ce]310
[b0c2075]311 /* Allocate frames from zone */
312 size_t index;
313 int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base,
314 constraint, &index);
315
316 ASSERT(avail);
[085d973]317
[b0c2075]318 /* Update frame reference count */
319 for (size_t i = 0; i < count; i++) {
320 frame_t *frame = zone_get_frame(zone, index + i);
321
322 ASSERT(frame->refcount == 0);
323 frame->refcount = 1;
324 }
[085d973]325
326 /* Update zone information. */
[b0c2075]327 zone->free_count -= count;
328 zone->busy_count += count;
[5f0f29ce]329
[b0c2075]330 return index;
[085d973]331}
332
[deaf8d5]333/** Free frame from zone.
[085d973]334 *
[5f0f29ce]335 * Assume zone is locked and is available for deallocation.
336 *
[b0c2075]337 * @param zone Pointer to zone from which the frame is to be freed.
338 * @param index Frame index relative to zone.
[eb3d379]339 *
[b0c2075]340 * @return Number of freed frames.
[89bcb520]341 *
[085d973]342 */
[b0c2075]343NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t index)
[085d973]344{
[e6c4b94]345 ASSERT(zone->flags & ZONE_AVAILABLE);
[5f0f29ce]346
[b0c2075]347 frame_t *frame = zone_get_frame(zone, index);
[085d973]348
[b0c2075]349 ASSERT(frame->refcount > 0);
[5f0f29ce]350
[085d973]351 if (!--frame->refcount) {
[64f3d3b]352 bitmap_set(&zone->bitmap, index, 0);
[b0c2075]353
[d3dfa42]354 /* Update zone information. */
[b0c2075]355 zone->free_count++;
356 zone->busy_count--;
357
[5df1963]358 return 1;
[085d973]359 }
[89bcb520]360
[5df1963]361 return 0;
[085d973]362}
363
[deaf8d5]364/** Mark frame in zone unavailable to allocation. */
[b0c2075]365NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index)
[085d973]366{
[aaceebc4]367 if (!(zone->flags & ZONE_AVAILABLE))
368 return;
[5f0f29ce]369
[b0c2075]370 frame_t *frame = zone_get_frame(zone, index);
371 if (frame->refcount > 0)
[bb68433]372 return;
[5f0f29ce]373
[cd3b380]374 frame->refcount = 1;
[b0c2075]375 bitmap_set_range(&zone->bitmap, index, 1);
[5f0f29ce]376
[085d973]377 zone->free_count--;
[d060900]378 reserve_force_alloc(1);
[085d973]379}
380
[5f0f29ce]381/** Merge two zones.
[bb68433]382 *
[5f0f29ce]383 * Assume z1 & z2 are locked and compatible and zones lock is
384 * locked.
[bb68433]385 *
[b0c2075]386 * @param z1 First zone to merge.
387 * @param z2 Second zone to merge.
388 * @param old_z1 Original data of the first zone.
389 * @param confdata Merged zone configuration data.
[eb3d379]390 *
[bb68433]391 */
[7a0359b]392NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
[b0c2075]393 void *confdata)
[bb68433]394{
[e6c4b94]395 ASSERT(zones.info[z1].flags & ZONE_AVAILABLE);
396 ASSERT(zones.info[z2].flags & ZONE_AVAILABLE);
[5f0f29ce]397 ASSERT(zones.info[z1].flags == zones.info[z2].flags);
398 ASSERT(zones.info[z1].base < zones.info[z2].base);
399 ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
400 zones.info[z2].base, zones.info[z2].count));
401
402 /* Difference between zone bases */
403 pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
404
405 zones.info[z1].count = base_diff + zones.info[z2].count;
406 zones.info[z1].free_count += zones.info[z2].free_count;
407 zones.info[z1].busy_count += zones.info[z2].busy_count;
408
[b0c2075]409 bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
[c5396c1]410 confdata + (sizeof(frame_t) * zones.info[z1].count));
[adb252c0]411 bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count);
[5f0f29ce]412
[b0c2075]413 zones.info[z1].frames = (frame_t *) confdata;
[5f0f29ce]414
[b0c2075]415 /*
416 * Copy frames and bits from both zones to preserve parents, etc.
[bb68433]417 */
[5f0f29ce]418
[b0c2075]419 for (size_t i = 0; i < old_z1->count; i++) {
420 bitmap_set(&zones.info[z1].bitmap, i,
421 bitmap_get(&old_z1->bitmap, i));
422 zones.info[z1].frames[i] = old_z1->frames[i];
[bb68433]423 }
[5f0f29ce]424
[b0c2075]425 for (size_t i = 0; i < zones.info[z2].count; i++) {
426 bitmap_set(&zones.info[z1].bitmap, base_diff + i,
427 bitmap_get(&zones.info[z2].bitmap, i));
428 zones.info[z1].frames[base_diff + i] =
429 zones.info[z2].frames[i];
[bb68433]430 }
431}
432
[deaf8d5]433/** Return old configuration frames into the zone.
[bb68433]434 *
[5f0f29ce]435 * We have two cases:
436 * - The configuration data is outside the zone
437 * -> do nothing (perhaps call frame_free?)
438 * - The configuration data was created by zone_create
439 * or updated by reduce_region -> free every frame
440 *
441 * @param znum The actual zone where freeing should occur.
442 * @param pfn Old zone configuration frame.
443 * @param count Old zone frame count.
[874878a]444 *
[bb68433]445 */
[7a0359b]446NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
[bb68433]447{
[e6c4b94]448 ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
[5f0f29ce]449
[98000fb]450 size_t cframes = SIZE2FRAMES(zone_conf_size(count));
[bb68433]451
[b0c2075]452 if ((pfn < zones.info[znum].base) ||
453 (pfn >= zones.info[znum].base + zones.info[znum].count))
[bb68433]454 return;
[5f0f29ce]455
[b0c2075]456 for (size_t i = 0; i < cframes; i++)
[89bcb520]457 (void) zone_frame_free(&zones.info[znum],
[5f0f29ce]458 pfn - zones.info[znum].base + i);
[874878a]459}
460
[deaf8d5]461/** Merge zones z1 and z2.
[bb68433]462 *
[5f0f29ce]463 * The merged zones must be 2 zones with no zone existing in between
464 * (which means that z2 = z1 + 1). Both zones must be available zones
465 * with the same flags.
466 *
467 * When you create a new zone, the frame allocator configuration does
468 * not to be 2^order size. Once the allocator is running it is no longer
469 * possible, merged configuration data occupies more space :-/
470 *
[bb68433]471 */
[98000fb]472bool zone_merge(size_t z1, size_t z2)
[bb68433]473{
[da1bafb]474 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]475
476 bool ret = true;
477
[b0c2075]478 /*
479 * We can join only 2 zones with none existing inbetween,
[5f0f29ce]480 * the zones have to be available and with the same
481 * set of flags
482 */
[e6c4b94]483 if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
484 (zones.info[z1].flags != zones.info[z2].flags)) {
[5f0f29ce]485 ret = false;
[bb68433]486 goto errout;
[5f0f29ce]487 }
488
489 pfn_t cframes = SIZE2FRAMES(zone_conf_size(
490 zones.info[z2].base - zones.info[z1].base
491 + zones.info[z2].count));
492
493 /* Allocate merged zone data inside one of the zones */
494 pfn_t pfn;
[b0c2075]495 if (zone_can_alloc(&zones.info[z1], cframes, 0)) {
496 pfn = zones.info[z1].base +
497 zone_frame_alloc(&zones.info[z1], cframes, 0);
498 } else if (zone_can_alloc(&zones.info[z2], cframes, 0)) {
499 pfn = zones.info[z2].base +
500 zone_frame_alloc(&zones.info[z2], cframes, 0);
[5f0f29ce]501 } else {
502 ret = false;
503 goto errout;
504 }
505
506 /* Preserve original data from z1 */
507 zone_t old_z1 = zones.info[z1];
508
509 /* Do zone merging */
[b0c2075]510 zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
[5f0f29ce]511
[bb68433]512 /* Subtract zone information from busy frames */
[5f0f29ce]513 zones.info[z1].busy_count -= cframes;
514
515 /* Free old zone information */
516 return_config_frames(z1,
517 ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
518 return_config_frames(z1,
519 ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
520 zones.info[z2].count);
521
[e49e234]522 /* Move zones down */
[b0c2075]523 for (size_t i = z2 + 1; i < zones.count; i++)
[b6b576c]524 zones.info[i - 1] = zones.info[i];
[e49e234]525
[bb68433]526 zones.count--;
[5f0f29ce]527
[bb68433]528errout:
[da1bafb]529 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]530
531 return ret;
[bb68433]532}
533
[5f0f29ce]534/** Merge all mergeable zones into one big zone.
535 *
536 * It is reasonable to do this on systems where
537 * BIOS reports parts in chunks, so that we could
538 * have 1 zone (it's faster).
[bb68433]539 *
540 */
541void zone_merge_all(void)
542{
[b0c2075]543 size_t i = 1;
544
[5f0f29ce]545 while (i < zones.count) {
[b0c2075]546 if (!zone_merge(i - 1, i))
[5f0f29ce]547 i++;
[bb68433]548 }
549}
550
[deaf8d5]551/** Create new frame zone.
[085d973]552 *
[b0c2075]553 * @param zone Zone to construct.
554 * @param start Physical address of the first frame within the zone.
555 * @param count Count of frames in zone.
556 * @param flags Zone flags.
557 * @param confdata Configuration data of the zone.
[5f0f29ce]558 *
559 * @return Initialized zone.
[085d973]560 *
561 */
[b0c2075]562NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
563 zone_flags_t flags, void *confdata)
[085d973]564{
[5f0f29ce]565 zone->base = start;
566 zone->count = count;
567 zone->flags = flags;
568 zone->free_count = count;
569 zone->busy_count = 0;
[085d973]570
[e6c4b94]571 if (flags & ZONE_AVAILABLE) {
[5f0f29ce]572 /*
[b0c2075]573 * Initialize frame bitmap (located after the array of
574 * frame_t structures in the configuration space).
[5f0f29ce]575 */
576
[c5396c1]577 bitmap_initialize(&zone->bitmap, count, confdata +
578 (sizeof(frame_t) * count));
[adb252c0]579 bitmap_clear_range(&zone->bitmap, 0, count);
[5f0f29ce]580
[b0c2075]581 /*
582 * Initialize the array of frame_t structures.
583 */
[5f0f29ce]584
[b0c2075]585 zone->frames = (frame_t *) confdata;
[5f0f29ce]586
[b0c2075]587 for (size_t i = 0; i < count; i++)
588 frame_initialize(&zone->frames[i]);
589 } else {
[c5396c1]590 bitmap_initialize(&zone->bitmap, 0, NULL);
[5f0f29ce]591 zone->frames = NULL;
[b0c2075]592 }
[085d973]593}
594
[deaf8d5]595/** Compute configuration data size for zone.
[eb3d379]596 *
[5f0f29ce]597 * @param count Size of zone in frames.
598 *
599 * @return Size of zone configuration info (in bytes).
600 *
[eb3d379]601 */
[bbfdf62]602size_t zone_conf_size(size_t count)
[085d973]603{
[c5396c1]604 return (count * sizeof(frame_t) + bitmap_size(count));
[085d973]605}
606
[8bdcffa]607/** Allocate external configuration frames from low memory. */
608pfn_t zone_external_conf_alloc(size_t count)
609{
[b0c2075]610 size_t frames = SIZE2FRAMES(zone_conf_size(count));
611
612 return ADDR2PFN((uintptr_t)
613 frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
[8bdcffa]614}
615
[deaf8d5]616/** Create and add zone to system.
[085d973]617 *
[5f0f29ce]618 * @param start First frame number (absolute).
619 * @param count Size of zone in frames.
620 * @param confframe Where configuration frames are supposed to be.
[b0c2075]621 * Automatically checks that we will not disturb the
[5f0f29ce]622 * kernel and possibly init. If confframe is given
623 * _outside_ this zone, it is expected, that the area is
624 * already marked BUSY and big enough to contain
625 * zone_conf_size() amount of data. If the confframe is
626 * inside the area, the zone free frame information is
627 * modified not to include it.
628 *
629 * @return Zone number or -1 on error.
630 *
[085d973]631 */
[da1bafb]632size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
633 zone_flags_t flags)
[085d973]634{
[da1bafb]635 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]636
[e6c4b94]637 if (flags & ZONE_AVAILABLE) { /* Create available zone */
[b0c2075]638 /*
639 * Theoretically we could have NULL here, practically make sure
[5f0f29ce]640 * nobody tries to do that. If some platform requires, remove
641 * the assert
642 */
[0b4a67a]643 ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
[b0c2075]644
[40c8c17]645 /* Update the known end of physical memory. */
646 config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
[5f0f29ce]647
[b0c2075]648 /*
649 * If confframe is supposed to be inside our zone, then make sure
[5f0f29ce]650 * it does not span kernel & init
651 */
[98000fb]652 size_t confcount = SIZE2FRAMES(zone_conf_size(count));
[b0c2075]653
[5f0f29ce]654 if ((confframe >= start) && (confframe < start + count)) {
655 for (; confframe < start + count; confframe++) {
656 uintptr_t addr = PFN2ADDR(confframe);
657 if (overlaps(addr, PFN2ADDR(confcount),
658 KA2PA(config.base), config.kernel_size))
659 continue;
660
[4638401]661 if (overlaps(addr, PFN2ADDR(confcount),
[5f0f29ce]662 KA2PA(config.stack_base), config.stack_size))
663 continue;
664
665 bool overlap = false;
[b0c2075]666 for (size_t i = 0; i < init.cnt; i++) {
[5f0f29ce]667 if (overlaps(addr, PFN2ADDR(confcount),
[32817cc]668 init.tasks[i].paddr,
[5f0f29ce]669 init.tasks[i].size)) {
670 overlap = true;
671 break;
672 }
[b0c2075]673 }
674
[5f0f29ce]675 if (overlap)
676 continue;
677
678 break;
679 }
[b6b576c]680
[b0c2075]681 if (confframe >= start + count)
682 panic("Cannot find configuration data for zone.");
[085d973]683 }
[5f0f29ce]684
[e2650d3]685 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]686 if (znum == (size_t) -1) {
[da1bafb]687 irq_spinlock_unlock(&zones.lock, true);
[98000fb]688 return (size_t) -1;
[5f0f29ce]689 }
690
[b0c2075]691 void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
692 zone_construct(&zones.info[znum], start, count, flags, confdata);
[5f0f29ce]693
694 /* If confdata in zone, mark as unavailable */
695 if ((confframe >= start) && (confframe < start + count)) {
[b0c2075]696 for (size_t i = confframe; i < confframe + confcount; i++)
[5f0f29ce]697 zone_mark_unavailable(&zones.info[znum],
698 i - zones.info[znum].base);
[085d973]699 }
[5f0f29ce]700
[da1bafb]701 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]702
703 return znum;
704 }
[b0c2075]705
[5f0f29ce]706 /* Non-available zone */
[e2650d3]707 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]708 if (znum == (size_t) -1) {
[da1bafb]709 irq_spinlock_unlock(&zones.lock, true);
[98000fb]710 return (size_t) -1;
[5f0f29ce]711 }
[b0c2075]712
713 zone_construct(&zones.info[znum], start, count, flags, NULL);
[5f0f29ce]714
[da1bafb]715 irq_spinlock_unlock(&zones.lock, true);
[1bb3766]716
[bb68433]717 return znum;
[085d973]718}
719
[5f0f29ce]720/*******************/
[085d973]721/* Frame functions */
[5f0f29ce]722/*******************/
[085d973]723
[deaf8d5]724/** Set parent of frame. */
[98000fb]725void frame_set_parent(pfn_t pfn, void *data, size_t hint)
[085d973]726{
[da1bafb]727 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]728
[98000fb]729 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]730
[98000fb]731 ASSERT(znum != (size_t) -1);
[5f0f29ce]732
733 zone_get_frame(&zones.info[znum],
734 pfn - zones.info[znum].base)->parent = data;
735
[da1bafb]736 irq_spinlock_unlock(&zones.lock, true);
[085d973]737}
738
[98000fb]739void *frame_get_parent(pfn_t pfn, size_t hint)
[085d973]740{
[da1bafb]741 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]742
[98000fb]743 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]744
[98000fb]745 ASSERT(znum != (size_t) -1);
[5f0f29ce]746
747 void *res = zone_get_frame(&zones.info[znum],
748 pfn - zones.info[znum].base)->parent;
749
[da1bafb]750 irq_spinlock_unlock(&zones.lock, true);
[085d973]751
752 return res;
753}
754
[b0c2075]755/** Allocate frames of physical memory.
[085d973]756 *
[b0c2075]757 * @param count Number of continuous frames to allocate.
758 * @param flags Flags for host zone selection and address processing.
759 * @param constraint Indication of physical address bits that cannot be
760 * set in the address of the first allocated frame.
761 * @param pzone Preferred zone.
[085d973]762 *
[5f0f29ce]763 * @return Physical address of the allocated frame.
[9a68b34d]764 *
[085d973]765 */
[b0c2075]766uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
[8cbf1c3]767 uintptr_t constraint, size_t *pzone)
[085d973]768{
[b0c2075]769 ASSERT(count > 0);
770
[98000fb]771 size_t hint = pzone ? (*pzone) : 0;
[b0c2075]772 pfn_t frame_constraint = ADDR2PFN(constraint);
[085d973]773
[89bcb520]774 /*
775 * If not told otherwise, we must first reserve the memory.
776 */
[b0c2075]777 if (!(flags & FRAME_NO_RESERVE))
778 reserve_force_alloc(count);
779
[085d973]780loop:
[da1bafb]781 irq_spinlock_lock(&zones.lock, true);
[9a68b34d]782
[085d973]783 /*
784 * First, find suitable frame zone.
785 */
[b0c2075]786 size_t znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
787 frame_constraint, hint);
[9a68b34d]788
[b0c2075]789 /*
790 * If no memory, reclaim some slab memory,
791 * if it does not help, reclaim all.
792 */
[98000fb]793 if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
[da1bafb]794 irq_spinlock_unlock(&zones.lock, true);
[98000fb]795 size_t freed = slab_reclaim(0);
[da1bafb]796 irq_spinlock_lock(&zones.lock, true);
[f049eec]797
[5f0f29ce]798 if (freed > 0)
[b0c2075]799 znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
800 frame_constraint, hint);
[5f0f29ce]801
[98000fb]802 if (znum == (size_t) -1) {
[da1bafb]803 irq_spinlock_unlock(&zones.lock, true);
[085d973]804 freed = slab_reclaim(SLAB_RECLAIM_ALL);
[da1bafb]805 irq_spinlock_lock(&zones.lock, true);
[f049eec]806
[5f0f29ce]807 if (freed > 0)
[b0c2075]808 znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
809 frame_constraint, hint);
[085d973]810 }
811 }
[5f0f29ce]812
[98000fb]813 if (znum == (size_t) -1) {
[1a1744e]814 if (flags & FRAME_ATOMIC) {
[da1bafb]815 irq_spinlock_unlock(&zones.lock, true);
[b0c2075]816
[89bcb520]817 if (!(flags & FRAME_NO_RESERVE))
[b0c2075]818 reserve_free(count);
819
[8cbf1c3]820 return 0;
[1a1744e]821 }
[085d973]822
[1a1744e]823#ifdef CONFIG_DEBUG
[8d308b9]824 size_t avail = frame_total_free_get_internal();
[1a1744e]825#endif
[5f0f29ce]826
[da1bafb]827 irq_spinlock_unlock(&zones.lock, true);
828
[05411e8]829 if (!THREAD)
[adb252c0]830 panic("Cannot wait for %zu frames to become available "
831 "(%zu available).", count, avail);
[5f0f29ce]832
833 /*
834 * Sleep until some frames are available again.
835 */
836
837#ifdef CONFIG_DEBUG
[adb252c0]838 printf("Thread %" PRIu64 " waiting for %zu frames "
839 "(%zu available).\n", THREAD->tid, count, avail);
[5f0f29ce]840#endif
841
[905721b]842 /*
[b0c2075]843 * Since the mem_avail_mtx is an active mutex, we need to
844 * disable interrupts to prevent deadlock with TLB shootdown.
[905721b]845 */
846 ipl_t ipl = interrupts_disable();
[1bb3766]847 mutex_lock(&mem_avail_mtx);
[5f0f29ce]848
849 if (mem_avail_req > 0)
[b0c2075]850 mem_avail_req = min(mem_avail_req, count);
[5f0f29ce]851 else
[b0c2075]852 mem_avail_req = count;
853
[98000fb]854 size_t gen = mem_avail_gen;
[5f0f29ce]855
856 while (gen == mem_avail_gen)
[1bb3766]857 condvar_wait(&mem_avail_cv, &mem_avail_mtx);
[5f0f29ce]858
[1bb3766]859 mutex_unlock(&mem_avail_mtx);
[905721b]860 interrupts_restore(ipl);
[5f0f29ce]861
[1a1744e]862#ifdef CONFIG_DEBUG
[5f0f29ce]863 printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
[1a1744e]864#endif
[5f0f29ce]865
[085d973]866 goto loop;
867 }
[9a68b34d]868
[b0c2075]869 pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
870 frame_constraint) + zones.info[znum].base;
[1bb3766]871
[da1bafb]872 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]873
874 if (pzone)
875 *pzone = znum;
876
[8cbf1c3]877 return PFN2ADDR(pfn);
[085d973]878}
879
[b0c2075]880uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
[0139747]881{
[b0c2075]882 return frame_alloc_generic(count, flags, constraint, NULL);
[0139747]883}
884
[b0c2075]885uintptr_t frame_alloc_noreserve(size_t count, frame_flags_t flags,
[8cbf1c3]886 uintptr_t constraint)
[e608cbe]887{
[b0c2075]888 return frame_alloc_generic(count, flags | FRAME_NO_RESERVE, constraint,
[8cbf1c3]889 NULL);
[e608cbe]890}
891
[5df1963]892/** Free frames of physical memory.
[085d973]893 *
[5df1963]894 * Find respective frame structures for supplied physical frames.
895 * Decrement each frame reference count. If it drops to zero, mark
896 * the frames as available.
[5f0f29ce]897 *
[5df1963]898 * @param start Physical Address of the first frame to be freed.
899 * @param count Number of frames to free.
[c32e6bc]900 * @param flags Flags to control memory reservation.
[085d973]901 *
902 */
[5df1963]903void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
[085d973]904{
[5df1963]905 size_t freed = 0;
[5f0f29ce]906
[5df1963]907 irq_spinlock_lock(&zones.lock, true);
[085d973]908
[5df1963]909 for (size_t i = 0; i < count; i++) {
910 /*
911 * First, find host frame zone for addr.
912 */
913 pfn_t pfn = ADDR2PFN(start) + i;
914 size_t znum = find_zone(pfn, 1, 0);
915
916 ASSERT(znum != (size_t) -1);
917
918 freed += zone_frame_free(&zones.info[znum],
919 pfn - zones.info[znum].base);
920 }
[085d973]921
[da1bafb]922 irq_spinlock_unlock(&zones.lock, true);
[1a1744e]923
924 /*
925 * Signal that some memory has been freed.
[5df1963]926 * Since the mem_avail_mtx is an active mutex,
927 * we need to disable interruptsto prevent deadlock
928 * with TLB shootdown.
[905721b]929 */
[b0c2075]930
[905721b]931 ipl_t ipl = interrupts_disable();
[1bb3766]932 mutex_lock(&mem_avail_mtx);
[b0c2075]933
[5f0f29ce]934 if (mem_avail_req > 0)
[5df1963]935 mem_avail_req -= min(mem_avail_req, freed);
[5f0f29ce]936
937 if (mem_avail_req == 0) {
938 mem_avail_gen++;
939 condvar_broadcast(&mem_avail_cv);
940 }
[b0c2075]941
[1bb3766]942 mutex_unlock(&mem_avail_mtx);
[905721b]943 interrupts_restore(ipl);
[89bcb520]944
945 if (!(flags & FRAME_NO_RESERVE))
[5df1963]946 reserve_free(freed);
[085d973]947}
948
[5df1963]949void frame_free(uintptr_t frame, size_t count)
[c32e6bc]950{
[5df1963]951 frame_free_generic(frame, count, 0);
[c32e6bc]952}
953
[5df1963]954void frame_free_noreserve(uintptr_t frame, size_t count)
[e608cbe]955{
[5df1963]956 frame_free_generic(frame, count, FRAME_NO_RESERVE);
[e608cbe]957}
958
[f3ac636]959/** Add reference to frame.
960 *
961 * Find respective frame structure for supplied PFN and
962 * increment frame reference count.
963 *
[5f0f29ce]964 * @param pfn Frame number of the frame to be freed.
965 *
[f3ac636]966 */
[97bdb4a]967NO_TRACE void frame_reference_add(pfn_t pfn)
[f3ac636]968{
[da1bafb]969 irq_spinlock_lock(&zones.lock, true);
[f3ac636]970
971 /*
972 * First, find host frame zone for addr.
973 */
[0b4a67a]974 size_t znum = find_zone(pfn, 1, 0);
[5f0f29ce]975
[98000fb]976 ASSERT(znum != (size_t) -1);
[f3ac636]977
[5f0f29ce]978 zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
[f3ac636]979
[da1bafb]980 irq_spinlock_unlock(&zones.lock, true);
[f3ac636]981}
[085d973]982
[da1bafb]983/** Mark given range unavailable in frame zones.
984 *
985 */
[97bdb4a]986NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
[085d973]987{
[da1bafb]988 irq_spinlock_lock(&zones.lock, true);
[052da81]989
[b0c2075]990 for (size_t i = 0; i < count; i++) {
[98000fb]991 size_t znum = find_zone(start + i, 1, 0);
[b0c2075]992
[98000fb]993 if (znum == (size_t) -1) /* PFN not found */
[085d973]994 continue;
[5f0f29ce]995
996 zone_mark_unavailable(&zones.info[znum],
997 start + i - zones.info[znum].base);
[085d973]998 }
[5f0f29ce]999
[da1bafb]1000 irq_spinlock_unlock(&zones.lock, true);
[085d973]1001}
1002
[da1bafb]1003/** Initialize physical memory management.
1004 *
1005 */
[085d973]1006void frame_init(void)
1007{
1008 if (config.cpu_active == 1) {
1009 zones.count = 0;
[da1bafb]1010 irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
[1bb3766]1011 mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1012 condvar_initialize(&mem_avail_cv);
[085d973]1013 }
[5f0f29ce]1014
[085d973]1015 /* Tell the architecture to create some memory */
[ddcc8a0]1016 frame_low_arch_init();
[b0c2075]1017
[085d973]1018 if (config.cpu_active == 1) {
[4638401]1019 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1020 SIZE2FRAMES(config.kernel_size));
1021 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1022 SIZE2FRAMES(config.stack_size));
[b6b576c]1023
[b0c2075]1024 for (size_t i = 0; i < init.cnt; i++)
1025 frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
[4638401]1026 SIZE2FRAMES(init.tasks[i].size));
[5f0f29ce]1027
[61e90dd]1028 if (ballocs.size)
[4638401]1029 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1030 SIZE2FRAMES(ballocs.size));
[5f0f29ce]1031
[b0c2075]1032 /*
1033 * Blacklist first frame, as allocating NULL would
[5f0f29ce]1034 * fail in some places
1035 */
[bffa0b06]1036 frame_mark_unavailable(0, 1);
[085d973]1037 }
[b0c2075]1038
[ddcc8a0]1039 frame_high_arch_init();
[085d973]1040}
1041
[ad12b5ea]1042/** Adjust bounds of physical memory region according to low/high memory split.
1043 *
[b0c2075]1044 * @param low[in] If true, the adjustment is performed to make the region
1045 * fit in the low memory. Otherwise the adjustment is
1046 * performed to make the region fit in the high memory.
1047 * @param basep[inout] Pointer to a variable which contains the region's base
1048 * address and which may receive the adjusted base address.
1049 * @param sizep[inout] Pointer to a variable which contains the region's size
1050 * and which may receive the adjusted size.
1051 *
1052 * @return True if the region still exists even after the adjustment.
1053 * @return False otherwise.
1054 *
[ad12b5ea]1055 */
1056bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
1057{
[2cc7f16]1058 uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
[b0c2075]1059
[ad12b5ea]1060 if (low) {
1061 if (*basep > limit)
1062 return false;
[b0c2075]1063
[ad12b5ea]1064 if (*basep + *sizep > limit)
1065 *sizep = limit - *basep;
1066 } else {
1067 if (*basep + *sizep <= limit)
1068 return false;
[b0c2075]1069
[ad12b5ea]1070 if (*basep <= limit) {
1071 *sizep -= limit - *basep;
1072 *basep = limit;
1073 }
1074 }
[b0c2075]1075
[ad12b5ea]1076 return true;
1077}
1078
[da1bafb]1079/** Return total size of all zones.
1080 *
1081 */
[9dae191e]1082uint64_t zones_total_size(void)
[deaf8d5]1083{
[da1bafb]1084 irq_spinlock_lock(&zones.lock, true);
[71eef11]1085
[5f0f29ce]1086 uint64_t total = 0;
[b0c2075]1087
1088 for (size_t i = 0; i < zones.count; i++)
[5f0f29ce]1089 total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[71eef11]1090
[da1bafb]1091 irq_spinlock_unlock(&zones.lock, true);
[71eef11]1092
1093 return total;
1094}
1095
[9dae191e]1096void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
1097 uint64_t *free)
[516adce]1098{
[9dae191e]1099 ASSERT(total != NULL);
1100 ASSERT(unavail != NULL);
1101 ASSERT(busy != NULL);
1102 ASSERT(free != NULL);
1103
[da1bafb]1104 irq_spinlock_lock(&zones.lock, true);
[9dae191e]1105
1106 *total = 0;
1107 *unavail = 0;
1108 *busy = 0;
1109 *free = 0;
1110
[b0c2075]1111 for (size_t i = 0; i < zones.count; i++) {
[9dae191e]1112 *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1113
[e6c4b94]1114 if (zones.info[i].flags & ZONE_AVAILABLE) {
[9dae191e]1115 *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
1116 *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
1117 } else
1118 *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[516adce]1119 }
[9dae191e]1120
[da1bafb]1121 irq_spinlock_unlock(&zones.lock, true);
[516adce]1122}
1123
[da1bafb]1124/** Prints list of zones.
1125 *
1126 */
[9dae191e]1127void zones_print_list(void)
[deaf8d5]1128{
[5f0f29ce]1129#ifdef __32_BITS__
[74c5a1ca]1130 printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1131#endif
1132
1133#ifdef __64_BITS__
[74c5a1ca]1134 printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1135#endif
[43b1e86]1136
[000350f8]1137 /*
1138 * Because printing may require allocation of memory, we may not hold
1139 * the frame allocator locks when printing zone statistics. Therefore,
1140 * we simply gather the statistics under the protection of the locks and
1141 * print the statistics when the locks have been released.
1142 *
1143 * When someone adds/removes zones while we are printing the statistics,
1144 * we may end up with inaccurate output (e.g. a zone being skipped from
1145 * the listing).
1146 */
[5f0f29ce]1147
[b0c2075]1148 for (size_t i = 0;; i++) {
[da1bafb]1149 irq_spinlock_lock(&zones.lock, true);
[000350f8]1150
1151 if (i >= zones.count) {
[da1bafb]1152 irq_spinlock_unlock(&zones.lock, true);
[000350f8]1153 break;
1154 }
[5f0f29ce]1155
1156 uintptr_t base = PFN2ADDR(zones.info[i].base);
[98000fb]1157 size_t count = zones.info[i].count;
[5f0f29ce]1158 zone_flags_t flags = zones.info[i].flags;
[98000fb]1159 size_t free_count = zones.info[i].free_count;
1160 size_t busy_count = zones.info[i].busy_count;
[000350f8]1161
[da1bafb]1162 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1163
[e6c4b94]1164 bool available = ((flags & ZONE_AVAILABLE) != 0);
[5f0f29ce]1165
[7e752b2]1166 printf("%-4zu", i);
[e49e234]1167
[2b8b0ca]1168#ifdef __32_BITS__
[7e752b2]1169 printf(" %p", (void *) base);
[2b8b0ca]1170#endif
[5f0f29ce]1171
[2b8b0ca]1172#ifdef __64_BITS__
[7e752b2]1173 printf(" %p", (void *) base);
[e49e234]1174#endif
1175
[e6c4b94]1176 printf(" %12zu %c%c%c%c%c ", count,
1177 available ? 'A' : '-',
1178 (flags & ZONE_RESERVED) ? 'R' : '-',
1179 (flags & ZONE_FIRMWARE) ? 'F' : '-',
1180 (flags & ZONE_LOWMEM) ? 'L' : '-',
1181 (flags & ZONE_HIGHMEM) ? 'H' : '-');
[43b1e86]1182
[5f0f29ce]1183 if (available)
[7e752b2]1184 printf("%14zu %14zu",
[5f0f29ce]1185 free_count, busy_count);
[e49e234]1186
[5f0f29ce]1187 printf("\n");
[dfd9186]1188 }
1189}
1190
[abbc16e]1191/** Prints zone details.
[96cacc1]1192 *
[5f0f29ce]1193 * @param num Zone base address or zone number.
1194 *
[96cacc1]1195 */
[98000fb]1196void zone_print_one(size_t num)
[deaf8d5]1197{
[da1bafb]1198 irq_spinlock_lock(&zones.lock, true);
[98000fb]1199 size_t znum = (size_t) -1;
[5f0f29ce]1200
[b0c2075]1201 for (size_t i = 0; i < zones.count; i++) {
[5f0f29ce]1202 if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1203 znum = i;
[bb68433]1204 break;
1205 }
1206 }
[5f0f29ce]1207
[98000fb]1208 if (znum == (size_t) -1) {
[da1bafb]1209 irq_spinlock_unlock(&zones.lock, true);
[bb68433]1210 printf("Zone not found.\n");
[2ec725f]1211 return;
[dfd9186]1212 }
[085d973]1213
[b0c2075]1214 uintptr_t base = PFN2ADDR(zones.info[znum].base);
1215 zone_flags_t flags = zones.info[znum].flags;
1216 size_t count = zones.info[znum].count;
1217 size_t free_count = zones.info[znum].free_count;
1218 size_t busy_count = zones.info[znum].busy_count;
[5f0f29ce]1219
[da1bafb]1220 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1221
[e6c4b94]1222 bool available = ((flags & ZONE_AVAILABLE) != 0);
[5f0f29ce]1223
[933cadf]1224 uint64_t size;
1225 const char *size_suffix;
1226 bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
1227
[7e752b2]1228 printf("Zone number: %zu\n", znum);
1229 printf("Zone base address: %p\n", (void *) base);
[933cadf]1230 printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
1231 size, size_suffix);
[e6c4b94]1232 printf("Zone flags: %c%c%c%c%c\n",
1233 available ? 'A' : '-',
1234 (flags & ZONE_RESERVED) ? 'R' : '-',
1235 (flags & ZONE_FIRMWARE) ? 'F' : '-',
1236 (flags & ZONE_LOWMEM) ? 'L' : '-',
1237 (flags & ZONE_HIGHMEM) ? 'H' : '-');
[5f0f29ce]1238
1239 if (available) {
[933cadf]1240 bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
1241 false);
1242 printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
1243 busy_count, size, size_suffix);
1244 bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
1245 false);
1246 printf("Available space: %zu frames (%" PRIu64 " %s)\n",
1247 free_count, size, size_suffix);
[5f0f29ce]1248 }
[dfd9186]1249}
1250
[cc73a8a1]1251/** @}
[b45c443]1252 */
Note: See TracBrowser for help on using the repository browser.