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

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

Small cleanup in frame.[ch].

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