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

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

new physical memory allocator supporting physical address constrains
the buddy allocator framework is retired and replaced by a two-level bitmap
the allocator can allocate an arbitrary number of frames, not only a power-of-two count

Caution: Change of semantics
The physical memory allocator no longer allocates naturally aligned blocks. If you require an aligned block, specify it as the constraint.

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