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

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

avoid tracing of several memory-related functions which are called from tight loops
make the reporting of overlaping zones more informative

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