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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1c57a8 was b1c57a8, checked in by Jakub Jermar <jakub@…>, 11 years ago

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

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