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

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

Factor out the amd64/ia32 code which calculates the bounds of
a zone after considering the low/hich memory split to a new
generic function frame_adjust_zone_bounds().

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