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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7329e6a was 7e752b2, checked in by Martin Decky <martin@…>, 15 years ago
  • correct printf() formatting strings and corresponding arguments
  • minor cstyle changes and other small fixes
  • 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 *
698 * The function uses
[bb68433]699 *
700 */
[98000fb]701bool zone_merge(size_t z1, size_t z2)
[bb68433]702{
[da1bafb]703 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]704
705 bool ret = true;
706
707 /* We can join only 2 zones with none existing inbetween,
708 * the zones have to be available and with the same
709 * set of flags
710 */
711 if ((z1 >= zones.count) || (z2 >= zones.count)
712 || (z2 - z1 != 1)
713 || (!zone_flags_available(zones.info[z1].flags))
714 || (!zone_flags_available(zones.info[z2].flags))
715 || (zones.info[z1].flags != zones.info[z2].flags)) {
716 ret = false;
[bb68433]717 goto errout;
[5f0f29ce]718 }
719
720 pfn_t cframes = SIZE2FRAMES(zone_conf_size(
721 zones.info[z2].base - zones.info[z1].base
722 + zones.info[z2].count));
723
724 uint8_t order;
[279952c]725 if (cframes == 1)
726 order = 0;
[bb68433]727 else
[5f0f29ce]728 order = fnzb(cframes - 1) + 1;
729
730 /* Allocate merged zone data inside one of the zones */
731 pfn_t pfn;
732 if (zone_can_alloc(&zones.info[z1], order)) {
733 pfn = zones.info[z1].base + zone_frame_alloc(&zones.info[z1], order);
734 } else if (zone_can_alloc(&zones.info[z2], order)) {
735 pfn = zones.info[z2].base + zone_frame_alloc(&zones.info[z2], order);
736 } else {
737 ret = false;
738 goto errout;
739 }
740
741 /* Preserve original data from z1 */
742 zone_t old_z1 = zones.info[z1];
743 old_z1.buddy_system->data = (void *) &old_z1;
744
745 /* Do zone merging */
746 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(pfn));
747 zone_merge_internal(z1, z2, &old_z1, buddy);
748
[874878a]749 /* Free unneeded config frames */
[5f0f29ce]750 zone_reduce_region(z1, pfn - zones.info[z1].base, cframes);
751
[bb68433]752 /* Subtract zone information from busy frames */
[5f0f29ce]753 zones.info[z1].busy_count -= cframes;
754
755 /* Free old zone information */
756 return_config_frames(z1,
757 ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
758 return_config_frames(z1,
759 ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
760 zones.info[z2].count);
761
[e49e234]762 /* Move zones down */
[98000fb]763 size_t i;
[e49e234]764 for (i = z2 + 1; i < zones.count; i++) {
[b6b576c]765 zones.info[i - 1] = zones.info[i];
[af96dd57]766 if (zones.info[i - 1].buddy_system != NULL)
767 zones.info[i - 1].buddy_system->data =
768 (void *) &zones.info[i - 1];
[e49e234]769 }
770
[bb68433]771 zones.count--;
[5f0f29ce]772
[bb68433]773errout:
[da1bafb]774 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]775
776 return ret;
[bb68433]777}
778
[5f0f29ce]779/** Merge all mergeable zones into one big zone.
780 *
781 * It is reasonable to do this on systems where
782 * BIOS reports parts in chunks, so that we could
783 * have 1 zone (it's faster).
[bb68433]784 *
785 */
786void zone_merge_all(void)
787{
[98000fb]788 size_t i = 0;
[5f0f29ce]789 while (i < zones.count) {
790 if (!zone_merge(i, i + 1))
791 i++;
[bb68433]792 }
793}
794
[deaf8d5]795/** Create new frame zone.
[085d973]796 *
[5f0f29ce]797 * @param zone Zone to construct.
798 * @param buddy Address of buddy system configuration information.
799 * @param start Physical address of the first frame within the zone.
800 * @param count Count of frames in zone.
801 * @param flags Zone flags.
802 *
803 * @return Initialized zone.
[085d973]804 *
805 */
[7a0359b]806NO_TRACE static void zone_construct(zone_t *zone, buddy_system_t *buddy,
807 pfn_t start, size_t count, zone_flags_t flags)
[085d973]808{
[5f0f29ce]809 zone->base = start;
810 zone->count = count;
811 zone->flags = flags;
812 zone->free_count = count;
813 zone->busy_count = 0;
814 zone->buddy_system = buddy;
[085d973]815
[5f0f29ce]816 if (zone_flags_available(flags)) {
817 /*
818 * Compute order for buddy system and initialize
819 */
820 uint8_t order = fnzb(count);
821 buddy_system_create(zone->buddy_system, order,
822 &zone_buddy_system_operations, (void *) zone);
823
824 /* Allocate frames _after_ the confframe */
825
826 /* Check sizes */
827 zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
828 buddy_conf_size(order));
829
[98000fb]830 size_t i;
[5f0f29ce]831 for (i = 0; i < count; i++)
832 frame_initialize(&zone->frames[i]);
833
834 /* Stuffing frames */
835 for (i = 0; i < count; i++) {
836 zone->frames[i].refcount = 0;
837 buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
838 }
839 } else
840 zone->frames = NULL;
[085d973]841}
842
[deaf8d5]843/** Compute configuration data size for zone.
[eb3d379]844 *
[5f0f29ce]845 * @param count Size of zone in frames.
846 *
847 * @return Size of zone configuration info (in bytes).
848 *
[eb3d379]849 */
[bbfdf62]850size_t zone_conf_size(size_t count)
[085d973]851{
[5f0f29ce]852 return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
[085d973]853}
854
[deaf8d5]855/** Create and add zone to system.
[085d973]856 *
[5f0f29ce]857 * @param start First frame number (absolute).
858 * @param count Size of zone in frames.
859 * @param confframe Where configuration frames are supposed to be.
860 * Automatically checks, that we will not disturb the
861 * kernel and possibly init. If confframe is given
862 * _outside_ this zone, it is expected, that the area is
863 * already marked BUSY and big enough to contain
864 * zone_conf_size() amount of data. If the confframe is
865 * inside the area, the zone free frame information is
866 * modified not to include it.
867 *
868 * @return Zone number or -1 on error.
869 *
[085d973]870 */
[da1bafb]871size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
872 zone_flags_t flags)
[085d973]873{
[da1bafb]874 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]875
876 if (zone_flags_available(flags)) { /* Create available zone */
877 /* Theoretically we could have NULL here, practically make sure
878 * nobody tries to do that. If some platform requires, remove
879 * the assert
880 */
[0b4a67a]881 ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
[5f0f29ce]882
883 /* If confframe is supposed to be inside our zone, then make sure
884 * it does not span kernel & init
885 */
[98000fb]886 size_t confcount = SIZE2FRAMES(zone_conf_size(count));
[5f0f29ce]887 if ((confframe >= start) && (confframe < start + count)) {
888 for (; confframe < start + count; confframe++) {
889 uintptr_t addr = PFN2ADDR(confframe);
890 if (overlaps(addr, PFN2ADDR(confcount),
891 KA2PA(config.base), config.kernel_size))
892 continue;
893
[4638401]894 if (overlaps(addr, PFN2ADDR(confcount),
[5f0f29ce]895 KA2PA(config.stack_base), config.stack_size))
896 continue;
897
898 bool overlap = false;
[98000fb]899 size_t i;
[5f0f29ce]900 for (i = 0; i < init.cnt; i++)
901 if (overlaps(addr, PFN2ADDR(confcount),
902 KA2PA(init.tasks[i].addr),
903 init.tasks[i].size)) {
904 overlap = true;
905 break;
906 }
907 if (overlap)
908 continue;
909
910 break;
911 }
[b6b576c]912
[5f0f29ce]913 if (confframe >= start + count)
914 panic("Cannot find configuration data for zone.");
[085d973]915 }
[5f0f29ce]916
[e2650d3]917 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]918 if (znum == (size_t) -1) {
[da1bafb]919 irq_spinlock_unlock(&zones.lock, true);
[98000fb]920 return (size_t) -1;
[5f0f29ce]921 }
922
923 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
924 zone_construct(&zones.info[znum], buddy, start, count, flags);
925
926 /* If confdata in zone, mark as unavailable */
927 if ((confframe >= start) && (confframe < start + count)) {
[98000fb]928 size_t i;
[5f0f29ce]929 for (i = confframe; i < confframe + confcount; i++)
930 zone_mark_unavailable(&zones.info[znum],
931 i - zones.info[znum].base);
[085d973]932 }
[5f0f29ce]933
[da1bafb]934 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]935
936 return znum;
937 }
938
939 /* Non-available zone */
[e2650d3]940 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]941 if (znum == (size_t) -1) {
[da1bafb]942 irq_spinlock_unlock(&zones.lock, true);
[98000fb]943 return (size_t) -1;
[5f0f29ce]944 }
945 zone_construct(&zones.info[znum], NULL, start, count, flags);
946
[da1bafb]947 irq_spinlock_unlock(&zones.lock, true);
[1bb3766]948
[bb68433]949 return znum;
[085d973]950}
951
[5f0f29ce]952/*******************/
[085d973]953/* Frame functions */
[5f0f29ce]954/*******************/
[085d973]955
[deaf8d5]956/** Set parent of frame. */
[98000fb]957void frame_set_parent(pfn_t pfn, void *data, size_t hint)
[085d973]958{
[da1bafb]959 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]960
[98000fb]961 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]962
[98000fb]963 ASSERT(znum != (size_t) -1);
[5f0f29ce]964
965 zone_get_frame(&zones.info[znum],
966 pfn - zones.info[znum].base)->parent = data;
967
[da1bafb]968 irq_spinlock_unlock(&zones.lock, true);
[085d973]969}
970
[98000fb]971void *frame_get_parent(pfn_t pfn, size_t hint)
[085d973]972{
[da1bafb]973 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]974
[98000fb]975 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]976
[98000fb]977 ASSERT(znum != (size_t) -1);
[5f0f29ce]978
979 void *res = zone_get_frame(&zones.info[znum],
980 pfn - zones.info[znum].base)->parent;
981
[da1bafb]982 irq_spinlock_unlock(&zones.lock, true);
[085d973]983
984 return res;
985}
986
987/** Allocate power-of-two frames of physical memory.
988 *
[5f0f29ce]989 * @param order Allocate exactly 2^order frames.
990 * @param flags Flags for host zone selection and address processing.
991 * @param pzone Preferred zone.
[085d973]992 *
[5f0f29ce]993 * @return Physical address of the allocated frame.
[9a68b34d]994 *
[085d973]995 */
[98000fb]996void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
[085d973]997{
[98000fb]998 size_t size = ((size_t) 1) << order;
999 size_t hint = pzone ? (*pzone) : 0;
[085d973]1000
1001loop:
[da1bafb]1002 irq_spinlock_lock(&zones.lock, true);
[9a68b34d]1003
[085d973]1004 /*
1005 * First, find suitable frame zone.
1006 */
[98000fb]1007 size_t znum = find_free_zone(order,
[5f0f29ce]1008 FRAME_TO_ZONE_FLAGS(flags), hint);
[9a68b34d]1009
[085d973]1010 /* If no memory, reclaim some slab memory,
1011 if it does not help, reclaim all */
[98000fb]1012 if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
[da1bafb]1013 irq_spinlock_unlock(&zones.lock, true);
[98000fb]1014 size_t freed = slab_reclaim(0);
[da1bafb]1015 irq_spinlock_lock(&zones.lock, true);
[f049eec]1016
[5f0f29ce]1017 if (freed > 0)
1018 znum = find_free_zone(order,
1019 FRAME_TO_ZONE_FLAGS(flags), hint);
1020
[98000fb]1021 if (znum == (size_t) -1) {
[da1bafb]1022 irq_spinlock_unlock(&zones.lock, true);
[085d973]1023 freed = slab_reclaim(SLAB_RECLAIM_ALL);
[da1bafb]1024 irq_spinlock_lock(&zones.lock, true);
[f049eec]1025
[5f0f29ce]1026 if (freed > 0)
1027 znum = find_free_zone(order,
1028 FRAME_TO_ZONE_FLAGS(flags), hint);
[085d973]1029 }
1030 }
[5f0f29ce]1031
[98000fb]1032 if (znum == (size_t) -1) {
[1a1744e]1033 if (flags & FRAME_ATOMIC) {
[da1bafb]1034 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1035 return NULL;
[1a1744e]1036 }
[085d973]1037
[1a1744e]1038#ifdef CONFIG_DEBUG
[98000fb]1039 size_t avail = total_frames_free();
[1a1744e]1040#endif
[5f0f29ce]1041
[da1bafb]1042 irq_spinlock_unlock(&zones.lock, true);
1043
[05411e8]1044 if (!THREAD)
1045 panic("Cannot wait for memory to become available.");
[5f0f29ce]1046
1047 /*
1048 * Sleep until some frames are available again.
1049 */
1050
1051#ifdef CONFIG_DEBUG
[7e752b2]1052 printf("Thread %" PRIu64 " waiting for %zu frames, "
1053 "%zu available.\n", THREAD->tid, size, avail);
[5f0f29ce]1054#endif
1055
[1bb3766]1056 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1057
1058 if (mem_avail_req > 0)
1059 mem_avail_req = min(mem_avail_req, size);
1060 else
1061 mem_avail_req = size;
[98000fb]1062 size_t gen = mem_avail_gen;
[5f0f29ce]1063
1064 while (gen == mem_avail_gen)
[1bb3766]1065 condvar_wait(&mem_avail_cv, &mem_avail_mtx);
[5f0f29ce]1066
[1bb3766]1067 mutex_unlock(&mem_avail_mtx);
[5f0f29ce]1068
[1a1744e]1069#ifdef CONFIG_DEBUG
[5f0f29ce]1070 printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
[1a1744e]1071#endif
[5f0f29ce]1072
[085d973]1073 goto loop;
1074 }
[9a68b34d]1075
[5f0f29ce]1076 pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
1077 + zones.info[znum].base;
[1bb3766]1078
[da1bafb]1079 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1080
1081 if (pzone)
1082 *pzone = znum;
1083
[2e9eae2]1084 if (flags & FRAME_KA)
[5f0f29ce]1085 return (void *) PA2KA(PFN2ADDR(pfn));
1086
1087 return (void *) PFN2ADDR(pfn);
[085d973]1088}
1089
1090/** Free a frame.
1091 *
[32fffef0]1092 * Find respective frame structure for supplied physical frame address.
[5f0f29ce]1093 * Decrement frame reference count. If it drops to zero, move the frame
1094 * structure to free list.
1095 *
1096 * @param frame Physical Address of of the frame to be freed.
[085d973]1097 *
1098 */
[7f1c620]1099void frame_free(uintptr_t frame)
[085d973]1100{
[da1bafb]1101 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]1102
[085d973]1103 /*
1104 * First, find host frame zone for addr.
1105 */
[5f0f29ce]1106 pfn_t pfn = ADDR2PFN(frame);
[0b4a67a]1107 size_t znum = find_zone(pfn, 1, 0);
[5f0f29ce]1108
[98000fb]1109 ASSERT(znum != (size_t) -1);
[085d973]1110
[5f0f29ce]1111 zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
[085d973]1112
[da1bafb]1113 irq_spinlock_unlock(&zones.lock, true);
[1a1744e]1114
1115 /*
1116 * Signal that some memory has been freed.
1117 */
[1bb3766]1118 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1119 if (mem_avail_req > 0)
1120 mem_avail_req--;
1121
1122 if (mem_avail_req == 0) {
1123 mem_avail_gen++;
1124 condvar_broadcast(&mem_avail_cv);
1125 }
[1bb3766]1126 mutex_unlock(&mem_avail_mtx);
[085d973]1127}
1128
[f3ac636]1129/** Add reference to frame.
1130 *
1131 * Find respective frame structure for supplied PFN and
1132 * increment frame reference count.
1133 *
[5f0f29ce]1134 * @param pfn Frame number of the frame to be freed.
1135 *
[f3ac636]1136 */
[97bdb4a]1137NO_TRACE void frame_reference_add(pfn_t pfn)
[f3ac636]1138{
[da1bafb]1139 irq_spinlock_lock(&zones.lock, true);
[f3ac636]1140
1141 /*
1142 * First, find host frame zone for addr.
1143 */
[0b4a67a]1144 size_t znum = find_zone(pfn, 1, 0);
[5f0f29ce]1145
[98000fb]1146 ASSERT(znum != (size_t) -1);
[f3ac636]1147
[5f0f29ce]1148 zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
[f3ac636]1149
[da1bafb]1150 irq_spinlock_unlock(&zones.lock, true);
[f3ac636]1151}
[085d973]1152
[da1bafb]1153/** Mark given range unavailable in frame zones.
1154 *
1155 */
[97bdb4a]1156NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
[085d973]1157{
[da1bafb]1158 irq_spinlock_lock(&zones.lock, true);
[052da81]1159
[98000fb]1160 size_t i;
[2936eef]1161 for (i = 0; i < count; i++) {
[98000fb]1162 size_t znum = find_zone(start + i, 1, 0);
1163 if (znum == (size_t) -1) /* PFN not found */
[085d973]1164 continue;
[5f0f29ce]1165
1166 zone_mark_unavailable(&zones.info[znum],
1167 start + i - zones.info[znum].base);
[085d973]1168 }
[5f0f29ce]1169
[da1bafb]1170 irq_spinlock_unlock(&zones.lock, true);
[085d973]1171}
1172
[da1bafb]1173/** Initialize physical memory management.
1174 *
1175 */
[085d973]1176void frame_init(void)
1177{
1178 if (config.cpu_active == 1) {
1179 zones.count = 0;
[da1bafb]1180 irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
[1bb3766]1181 mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1182 condvar_initialize(&mem_avail_cv);
[085d973]1183 }
[5f0f29ce]1184
[085d973]1185 /* Tell the architecture to create some memory */
1186 frame_arch_init();
1187 if (config.cpu_active == 1) {
[4638401]1188 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1189 SIZE2FRAMES(config.kernel_size));
1190 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1191 SIZE2FRAMES(config.stack_size));
[b6b576c]1192
[98000fb]1193 size_t i;
[4638401]1194 for (i = 0; i < init.cnt; i++) {
1195 pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr));
1196 frame_mark_unavailable(pfn,
1197 SIZE2FRAMES(init.tasks[i].size));
1198 }
[5f0f29ce]1199
[61e90dd]1200 if (ballocs.size)
[4638401]1201 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1202 SIZE2FRAMES(ballocs.size));
[5f0f29ce]1203
[bffa0b06]1204 /* Black list first frame, as allocating NULL would
[5f0f29ce]1205 * fail in some places
1206 */
[bffa0b06]1207 frame_mark_unavailable(0, 1);
[085d973]1208 }
1209}
1210
[da1bafb]1211/** Return total size of all zones.
1212 *
1213 */
[9dae191e]1214uint64_t zones_total_size(void)
[deaf8d5]1215{
[da1bafb]1216 irq_spinlock_lock(&zones.lock, true);
[71eef11]1217
[5f0f29ce]1218 uint64_t total = 0;
[98000fb]1219 size_t i;
[5f0f29ce]1220 for (i = 0; i < zones.count; i++)
1221 total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[71eef11]1222
[da1bafb]1223 irq_spinlock_unlock(&zones.lock, true);
[71eef11]1224
1225 return total;
1226}
1227
[9dae191e]1228void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
1229 uint64_t *free)
[516adce]1230{
[9dae191e]1231 ASSERT(total != NULL);
1232 ASSERT(unavail != NULL);
1233 ASSERT(busy != NULL);
1234 ASSERT(free != NULL);
1235
[da1bafb]1236 irq_spinlock_lock(&zones.lock, true);
[9dae191e]1237
1238 *total = 0;
1239 *unavail = 0;
1240 *busy = 0;
1241 *free = 0;
1242
[516adce]1243 size_t i;
1244 for (i = 0; i < zones.count; i++) {
[9dae191e]1245 *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1246
1247 if (zone_flags_available(zones.info[i].flags)) {
1248 *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
1249 *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
1250 } else
1251 *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[516adce]1252 }
[9dae191e]1253
[da1bafb]1254 irq_spinlock_unlock(&zones.lock, true);
[516adce]1255}
1256
[da1bafb]1257/** Prints list of zones.
1258 *
1259 */
[9dae191e]1260void zones_print_list(void)
[deaf8d5]1261{
[5f0f29ce]1262#ifdef __32_BITS__
[74c5a1ca]1263 printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1264#endif
1265
1266#ifdef __64_BITS__
[74c5a1ca]1267 printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1268#endif
[43b1e86]1269
[000350f8]1270 /*
1271 * Because printing may require allocation of memory, we may not hold
1272 * the frame allocator locks when printing zone statistics. Therefore,
1273 * we simply gather the statistics under the protection of the locks and
1274 * print the statistics when the locks have been released.
1275 *
1276 * When someone adds/removes zones while we are printing the statistics,
1277 * we may end up with inaccurate output (e.g. a zone being skipped from
1278 * the listing).
1279 */
[5f0f29ce]1280
[98000fb]1281 size_t i;
[5f0f29ce]1282 for (i = 0;; i++) {
[da1bafb]1283 irq_spinlock_lock(&zones.lock, true);
[000350f8]1284
1285 if (i >= zones.count) {
[da1bafb]1286 irq_spinlock_unlock(&zones.lock, true);
[000350f8]1287 break;
1288 }
[5f0f29ce]1289
1290 uintptr_t base = PFN2ADDR(zones.info[i].base);
[98000fb]1291 size_t count = zones.info[i].count;
[5f0f29ce]1292 zone_flags_t flags = zones.info[i].flags;
[98000fb]1293 size_t free_count = zones.info[i].free_count;
1294 size_t busy_count = zones.info[i].busy_count;
[000350f8]1295
[da1bafb]1296 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1297
1298 bool available = zone_flags_available(flags);
1299
[7e752b2]1300 printf("%-4zu", i);
[e49e234]1301
[2b8b0ca]1302#ifdef __32_BITS__
[7e752b2]1303 printf(" %p", (void *) base);
[2b8b0ca]1304#endif
[5f0f29ce]1305
[2b8b0ca]1306#ifdef __64_BITS__
[7e752b2]1307 printf(" %p", (void *) base);
[e49e234]1308#endif
1309
[7e752b2]1310 printf(" %12zu %c%c%c ", count,
[5f0f29ce]1311 available ? 'A' : ' ',
1312 (flags & ZONE_RESERVED) ? 'R' : ' ',
1313 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
[43b1e86]1314
[5f0f29ce]1315 if (available)
[7e752b2]1316 printf("%14zu %14zu",
[5f0f29ce]1317 free_count, busy_count);
[e49e234]1318
[5f0f29ce]1319 printf("\n");
[dfd9186]1320 }
1321}
1322
[abbc16e]1323/** Prints zone details.
[96cacc1]1324 *
[5f0f29ce]1325 * @param num Zone base address or zone number.
1326 *
[96cacc1]1327 */
[98000fb]1328void zone_print_one(size_t num)
[deaf8d5]1329{
[da1bafb]1330 irq_spinlock_lock(&zones.lock, true);
[98000fb]1331 size_t znum = (size_t) -1;
[5f0f29ce]1332
[98000fb]1333 size_t i;
[b6b576c]1334 for (i = 0; i < zones.count; i++) {
[5f0f29ce]1335 if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1336 znum = i;
[bb68433]1337 break;
1338 }
1339 }
[5f0f29ce]1340
[98000fb]1341 if (znum == (size_t) -1) {
[da1bafb]1342 irq_spinlock_unlock(&zones.lock, true);
[bb68433]1343 printf("Zone not found.\n");
[2ec725f]1344 return;
[dfd9186]1345 }
[085d973]1346
[5f0f29ce]1347 uintptr_t base = PFN2ADDR(zones.info[i].base);
1348 zone_flags_t flags = zones.info[i].flags;
[98000fb]1349 size_t count = zones.info[i].count;
1350 size_t free_count = zones.info[i].free_count;
1351 size_t busy_count = zones.info[i].busy_count;
[5f0f29ce]1352
[da1bafb]1353 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1354
1355 bool available = zone_flags_available(flags);
1356
[7e752b2]1357 printf("Zone number: %zu\n", znum);
1358 printf("Zone base address: %p\n", (void *) base);
1359 printf("Zone size: %zu frames (%zu KiB)\n", count,
[2ec725f]1360 SIZE2KB(FRAMES2SIZE(count)));
[5f0f29ce]1361 printf("Zone flags: %c%c%c\n",
1362 available ? 'A' : ' ',
1363 (flags & ZONE_RESERVED) ? 'R' : ' ',
1364 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1365
1366 if (available) {
[7e752b2]1367 printf("Allocated space: %zu frames (%zu KiB)\n",
[5f0f29ce]1368 busy_count, SIZE2KB(FRAMES2SIZE(busy_count)));
[7e752b2]1369 printf("Available space: %zu frames (%zu KiB)\n",
[5f0f29ce]1370 free_count, SIZE2KB(FRAMES2SIZE(free_count)));
1371 }
[dfd9186]1372}
1373
[cc73a8a1]1374/** @}
[b45c443]1375 */
Note: See TracBrowser for help on using the repository browser.