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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef4218f was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

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