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

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

size_t as a return type of zone_conf_size() is more natural than uintptr_t

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