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

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

mem_avail_mtx is an active mutex and needs to be IRQ-safe.

  • Property mode set to 100644
File size: 38.1 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
[7852625]872 return ADDR2PFN((uintptr_t) frame_alloc(order - FRAME_WIDTH,
873 FRAME_LOWMEM | FRAME_ATOMIC));
[8bdcffa]874}
875
[deaf8d5]876/** Create and add zone to system.
[085d973]877 *
[5f0f29ce]878 * @param start First frame number (absolute).
879 * @param count Size of zone in frames.
880 * @param confframe Where configuration frames are supposed to be.
881 * Automatically checks, that we will not disturb the
882 * kernel and possibly init. If confframe is given
883 * _outside_ this zone, it is expected, that the area is
884 * already marked BUSY and big enough to contain
885 * zone_conf_size() amount of data. If the confframe is
886 * inside the area, the zone free frame information is
887 * modified not to include it.
888 *
889 * @return Zone number or -1 on error.
890 *
[085d973]891 */
[da1bafb]892size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
893 zone_flags_t flags)
[085d973]894{
[da1bafb]895 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]896
[e6c4b94]897 if (flags & ZONE_AVAILABLE) { /* Create available zone */
[5f0f29ce]898 /* Theoretically we could have NULL here, practically make sure
899 * nobody tries to do that. If some platform requires, remove
900 * the assert
901 */
[0b4a67a]902 ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
[40c8c17]903
904 /* Update the known end of physical memory. */
905 config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
[5f0f29ce]906
907 /* If confframe is supposed to be inside our zone, then make sure
908 * it does not span kernel & init
909 */
[98000fb]910 size_t confcount = SIZE2FRAMES(zone_conf_size(count));
[5f0f29ce]911 if ((confframe >= start) && (confframe < start + count)) {
912 for (; confframe < start + count; confframe++) {
913 uintptr_t addr = PFN2ADDR(confframe);
914 if (overlaps(addr, PFN2ADDR(confcount),
915 KA2PA(config.base), config.kernel_size))
916 continue;
917
[4638401]918 if (overlaps(addr, PFN2ADDR(confcount),
[5f0f29ce]919 KA2PA(config.stack_base), config.stack_size))
920 continue;
921
922 bool overlap = false;
[98000fb]923 size_t i;
[5f0f29ce]924 for (i = 0; i < init.cnt; i++)
925 if (overlaps(addr, PFN2ADDR(confcount),
[32817cc]926 init.tasks[i].paddr,
[5f0f29ce]927 init.tasks[i].size)) {
928 overlap = true;
929 break;
930 }
931 if (overlap)
932 continue;
933
934 break;
935 }
[b6b576c]936
[5f0f29ce]937 if (confframe >= start + count)
938 panic("Cannot find configuration data for zone.");
[085d973]939 }
[5f0f29ce]940
[e2650d3]941 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]942 if (znum == (size_t) -1) {
[da1bafb]943 irq_spinlock_unlock(&zones.lock, true);
[98000fb]944 return (size_t) -1;
[5f0f29ce]945 }
946
947 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
948 zone_construct(&zones.info[znum], buddy, start, count, flags);
949
950 /* If confdata in zone, mark as unavailable */
951 if ((confframe >= start) && (confframe < start + count)) {
[98000fb]952 size_t i;
[5f0f29ce]953 for (i = confframe; i < confframe + confcount; i++)
954 zone_mark_unavailable(&zones.info[znum],
955 i - zones.info[znum].base);
[085d973]956 }
[5f0f29ce]957
[da1bafb]958 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]959
960 return znum;
961 }
962
963 /* Non-available zone */
[e2650d3]964 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]965 if (znum == (size_t) -1) {
[da1bafb]966 irq_spinlock_unlock(&zones.lock, true);
[98000fb]967 return (size_t) -1;
[5f0f29ce]968 }
969 zone_construct(&zones.info[znum], NULL, start, count, flags);
970
[da1bafb]971 irq_spinlock_unlock(&zones.lock, true);
[1bb3766]972
[bb68433]973 return znum;
[085d973]974}
975
[5f0f29ce]976/*******************/
[085d973]977/* Frame functions */
[5f0f29ce]978/*******************/
[085d973]979
[deaf8d5]980/** Set parent of frame. */
[98000fb]981void frame_set_parent(pfn_t pfn, void *data, size_t hint)
[085d973]982{
[da1bafb]983 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]984
[98000fb]985 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]986
[98000fb]987 ASSERT(znum != (size_t) -1);
[5f0f29ce]988
989 zone_get_frame(&zones.info[znum],
990 pfn - zones.info[znum].base)->parent = data;
991
[da1bafb]992 irq_spinlock_unlock(&zones.lock, true);
[085d973]993}
994
[98000fb]995void *frame_get_parent(pfn_t pfn, size_t hint)
[085d973]996{
[da1bafb]997 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]998
[98000fb]999 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]1000
[98000fb]1001 ASSERT(znum != (size_t) -1);
[5f0f29ce]1002
1003 void *res = zone_get_frame(&zones.info[znum],
1004 pfn - zones.info[znum].base)->parent;
1005
[da1bafb]1006 irq_spinlock_unlock(&zones.lock, true);
[085d973]1007
1008 return res;
1009}
1010
1011/** Allocate power-of-two frames of physical memory.
1012 *
[5f0f29ce]1013 * @param order Allocate exactly 2^order frames.
1014 * @param flags Flags for host zone selection and address processing.
1015 * @param pzone Preferred zone.
[085d973]1016 *
[5f0f29ce]1017 * @return Physical address of the allocated frame.
[9a68b34d]1018 *
[085d973]1019 */
[98000fb]1020void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
[085d973]1021{
[98000fb]1022 size_t size = ((size_t) 1) << order;
1023 size_t hint = pzone ? (*pzone) : 0;
[085d973]1024
[89bcb520]1025 /*
1026 * If not told otherwise, we must first reserve the memory.
1027 */
[864b0dd]1028 if (!(flags & FRAME_NO_RESERVE))
1029 reserve_force_alloc(size);
1030
[085d973]1031loop:
[da1bafb]1032 irq_spinlock_lock(&zones.lock, true);
[9a68b34d]1033
[085d973]1034 /*
1035 * First, find suitable frame zone.
1036 */
[98000fb]1037 size_t znum = find_free_zone(order,
[5f0f29ce]1038 FRAME_TO_ZONE_FLAGS(flags), hint);
[9a68b34d]1039
[085d973]1040 /* If no memory, reclaim some slab memory,
1041 if it does not help, reclaim all */
[98000fb]1042 if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
[da1bafb]1043 irq_spinlock_unlock(&zones.lock, true);
[98000fb]1044 size_t freed = slab_reclaim(0);
[da1bafb]1045 irq_spinlock_lock(&zones.lock, true);
[f049eec]1046
[5f0f29ce]1047 if (freed > 0)
1048 znum = find_free_zone(order,
1049 FRAME_TO_ZONE_FLAGS(flags), hint);
1050
[98000fb]1051 if (znum == (size_t) -1) {
[da1bafb]1052 irq_spinlock_unlock(&zones.lock, true);
[085d973]1053 freed = slab_reclaim(SLAB_RECLAIM_ALL);
[da1bafb]1054 irq_spinlock_lock(&zones.lock, true);
[f049eec]1055
[5f0f29ce]1056 if (freed > 0)
1057 znum = find_free_zone(order,
1058 FRAME_TO_ZONE_FLAGS(flags), hint);
[085d973]1059 }
1060 }
[5f0f29ce]1061
[98000fb]1062 if (znum == (size_t) -1) {
[1a1744e]1063 if (flags & FRAME_ATOMIC) {
[da1bafb]1064 irq_spinlock_unlock(&zones.lock, true);
[89bcb520]1065 if (!(flags & FRAME_NO_RESERVE))
1066 reserve_free(size);
[5f0f29ce]1067 return NULL;
[1a1744e]1068 }
[085d973]1069
[1a1744e]1070#ifdef CONFIG_DEBUG
[8d308b9]1071 size_t avail = frame_total_free_get_internal();
[1a1744e]1072#endif
[5f0f29ce]1073
[da1bafb]1074 irq_spinlock_unlock(&zones.lock, true);
1075
[05411e8]1076 if (!THREAD)
1077 panic("Cannot wait for memory to become available.");
[5f0f29ce]1078
1079 /*
1080 * Sleep until some frames are available again.
1081 */
1082
1083#ifdef CONFIG_DEBUG
[7e752b2]1084 printf("Thread %" PRIu64 " waiting for %zu frames, "
1085 "%zu available.\n", THREAD->tid, size, avail);
[5f0f29ce]1086#endif
1087
[905721b]1088 /*
1089 * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
1090 * to prevent deadlock with TLB shootdown.
1091 */
1092 ipl_t ipl = interrupts_disable();
[1bb3766]1093 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1094
1095 if (mem_avail_req > 0)
1096 mem_avail_req = min(mem_avail_req, size);
1097 else
1098 mem_avail_req = size;
[98000fb]1099 size_t gen = mem_avail_gen;
[5f0f29ce]1100
1101 while (gen == mem_avail_gen)
[1bb3766]1102 condvar_wait(&mem_avail_cv, &mem_avail_mtx);
[5f0f29ce]1103
[1bb3766]1104 mutex_unlock(&mem_avail_mtx);
[905721b]1105 interrupts_restore(ipl);
[5f0f29ce]1106
[1a1744e]1107#ifdef CONFIG_DEBUG
[5f0f29ce]1108 printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
[1a1744e]1109#endif
[5f0f29ce]1110
[085d973]1111 goto loop;
1112 }
[9a68b34d]1113
[5f0f29ce]1114 pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
1115 + zones.info[znum].base;
[1bb3766]1116
[da1bafb]1117 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1118
1119 if (pzone)
1120 *pzone = znum;
1121
[2e9eae2]1122 if (flags & FRAME_KA)
[5f0f29ce]1123 return (void *) PA2KA(PFN2ADDR(pfn));
1124
1125 return (void *) PFN2ADDR(pfn);
[085d973]1126}
1127
[0139747]1128void *frame_alloc(uint8_t order, frame_flags_t flags)
1129{
1130 return frame_alloc_generic(order, flags, NULL);
1131}
1132
[e608cbe]1133void *frame_alloc_noreserve(uint8_t order, frame_flags_t flags)
1134{
1135 return frame_alloc_generic(order, flags | FRAME_NO_RESERVE, NULL);
1136}
1137
[085d973]1138/** Free a frame.
1139 *
[32fffef0]1140 * Find respective frame structure for supplied physical frame address.
[5f0f29ce]1141 * Decrement frame reference count. If it drops to zero, move the frame
1142 * structure to free list.
1143 *
1144 * @param frame Physical Address of of the frame to be freed.
[c32e6bc]1145 * @param flags Flags to control memory reservation.
[085d973]1146 *
1147 */
[c32e6bc]1148void frame_free_generic(uintptr_t frame, frame_flags_t flags)
[085d973]1149{
[89bcb520]1150 size_t size;
1151
[da1bafb]1152 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]1153
[085d973]1154 /*
1155 * First, find host frame zone for addr.
1156 */
[5f0f29ce]1157 pfn_t pfn = ADDR2PFN(frame);
[0b4a67a]1158 size_t znum = find_zone(pfn, 1, 0);
[89bcb520]1159
[98000fb]1160 ASSERT(znum != (size_t) -1);
[085d973]1161
[89bcb520]1162 size = zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
[085d973]1163
[da1bafb]1164 irq_spinlock_unlock(&zones.lock, true);
[1a1744e]1165
1166 /*
1167 * Signal that some memory has been freed.
1168 */
[905721b]1169
1170
1171 /*
1172 * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
1173 * to prevent deadlock with TLB shootdown.
1174 */
1175 ipl_t ipl = interrupts_disable();
[1bb3766]1176 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1177 if (mem_avail_req > 0)
[28295955]1178 mem_avail_req -= min(mem_avail_req, size);
[5f0f29ce]1179
1180 if (mem_avail_req == 0) {
1181 mem_avail_gen++;
1182 condvar_broadcast(&mem_avail_cv);
1183 }
[1bb3766]1184 mutex_unlock(&mem_avail_mtx);
[905721b]1185 interrupts_restore(ipl);
[89bcb520]1186
1187 if (!(flags & FRAME_NO_RESERVE))
1188 reserve_free(size);
[085d973]1189}
1190
[c32e6bc]1191void frame_free(uintptr_t frame)
1192{
1193 frame_free_generic(frame, 0);
1194}
1195
[e608cbe]1196void frame_free_noreserve(uintptr_t frame)
1197{
1198 frame_free_generic(frame, FRAME_NO_RESERVE);
1199}
1200
[f3ac636]1201/** Add reference to frame.
1202 *
1203 * Find respective frame structure for supplied PFN and
1204 * increment frame reference count.
1205 *
[5f0f29ce]1206 * @param pfn Frame number of the frame to be freed.
1207 *
[f3ac636]1208 */
[97bdb4a]1209NO_TRACE void frame_reference_add(pfn_t pfn)
[f3ac636]1210{
[da1bafb]1211 irq_spinlock_lock(&zones.lock, true);
[f3ac636]1212
1213 /*
1214 * First, find host frame zone for addr.
1215 */
[0b4a67a]1216 size_t znum = find_zone(pfn, 1, 0);
[5f0f29ce]1217
[98000fb]1218 ASSERT(znum != (size_t) -1);
[f3ac636]1219
[5f0f29ce]1220 zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
[f3ac636]1221
[da1bafb]1222 irq_spinlock_unlock(&zones.lock, true);
[f3ac636]1223}
[085d973]1224
[da1bafb]1225/** Mark given range unavailable in frame zones.
1226 *
1227 */
[97bdb4a]1228NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
[085d973]1229{
[da1bafb]1230 irq_spinlock_lock(&zones.lock, true);
[052da81]1231
[98000fb]1232 size_t i;
[2936eef]1233 for (i = 0; i < count; i++) {
[98000fb]1234 size_t znum = find_zone(start + i, 1, 0);
1235 if (znum == (size_t) -1) /* PFN not found */
[085d973]1236 continue;
[5f0f29ce]1237
1238 zone_mark_unavailable(&zones.info[znum],
1239 start + i - zones.info[znum].base);
[085d973]1240 }
[5f0f29ce]1241
[da1bafb]1242 irq_spinlock_unlock(&zones.lock, true);
[085d973]1243}
1244
[da1bafb]1245/** Initialize physical memory management.
1246 *
1247 */
[085d973]1248void frame_init(void)
1249{
1250 if (config.cpu_active == 1) {
1251 zones.count = 0;
[da1bafb]1252 irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
[1bb3766]1253 mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1254 condvar_initialize(&mem_avail_cv);
[085d973]1255 }
[5f0f29ce]1256
[085d973]1257 /* Tell the architecture to create some memory */
[ddcc8a0]1258 frame_low_arch_init();
[085d973]1259 if (config.cpu_active == 1) {
[4638401]1260 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1261 SIZE2FRAMES(config.kernel_size));
1262 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1263 SIZE2FRAMES(config.stack_size));
[b6b576c]1264
[98000fb]1265 size_t i;
[4638401]1266 for (i = 0; i < init.cnt; i++) {
[32817cc]1267 pfn_t pfn = ADDR2PFN(init.tasks[i].paddr);
[4638401]1268 frame_mark_unavailable(pfn,
1269 SIZE2FRAMES(init.tasks[i].size));
1270 }
[5f0f29ce]1271
[61e90dd]1272 if (ballocs.size)
[4638401]1273 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1274 SIZE2FRAMES(ballocs.size));
[5f0f29ce]1275
[bffa0b06]1276 /* Black list first frame, as allocating NULL would
[5f0f29ce]1277 * fail in some places
1278 */
[bffa0b06]1279 frame_mark_unavailable(0, 1);
[085d973]1280 }
[ddcc8a0]1281 frame_high_arch_init();
[085d973]1282}
1283
[ad12b5ea]1284/** Adjust bounds of physical memory region according to low/high memory split.
1285 *
[2cc7f16]1286 * @param low[in] If true, the adjustment is performed to make the region
[ad12b5ea]1287 * fit in the low memory. Otherwise the adjustment is
1288 * performed to make the region fit in the high memory.
1289 * @param basep[inout] Pointer to a variable which contains the region's base
1290 * address and which may receive the adjusted base address.
1291 * @param sizep[inout] Pointer to a variable which contains the region's size
1292 * and which may receive the adjusted size.
1293 * @retun True if the region still exists even after the
1294 * adjustment, false otherwise.
1295 */
1296bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
1297{
[2cc7f16]1298 uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
[ad12b5ea]1299
1300 if (low) {
1301 if (*basep > limit)
1302 return false;
1303 if (*basep + *sizep > limit)
1304 *sizep = limit - *basep;
1305 } else {
1306 if (*basep + *sizep <= limit)
1307 return false;
1308 if (*basep <= limit) {
1309 *sizep -= limit - *basep;
1310 *basep = limit;
1311 }
1312 }
1313 return true;
1314}
1315
[da1bafb]1316/** Return total size of all zones.
1317 *
1318 */
[9dae191e]1319uint64_t zones_total_size(void)
[deaf8d5]1320{
[da1bafb]1321 irq_spinlock_lock(&zones.lock, true);
[71eef11]1322
[5f0f29ce]1323 uint64_t total = 0;
[98000fb]1324 size_t i;
[5f0f29ce]1325 for (i = 0; i < zones.count; i++)
1326 total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[71eef11]1327
[da1bafb]1328 irq_spinlock_unlock(&zones.lock, true);
[71eef11]1329
1330 return total;
1331}
1332
[9dae191e]1333void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
1334 uint64_t *free)
[516adce]1335{
[9dae191e]1336 ASSERT(total != NULL);
1337 ASSERT(unavail != NULL);
1338 ASSERT(busy != NULL);
1339 ASSERT(free != NULL);
1340
[da1bafb]1341 irq_spinlock_lock(&zones.lock, true);
[9dae191e]1342
1343 *total = 0;
1344 *unavail = 0;
1345 *busy = 0;
1346 *free = 0;
1347
[516adce]1348 size_t i;
1349 for (i = 0; i < zones.count; i++) {
[9dae191e]1350 *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1351
[e6c4b94]1352 if (zones.info[i].flags & ZONE_AVAILABLE) {
[9dae191e]1353 *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
1354 *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
1355 } else
1356 *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[516adce]1357 }
[9dae191e]1358
[da1bafb]1359 irq_spinlock_unlock(&zones.lock, true);
[516adce]1360}
1361
[da1bafb]1362/** Prints list of zones.
1363 *
1364 */
[9dae191e]1365void zones_print_list(void)
[deaf8d5]1366{
[5f0f29ce]1367#ifdef __32_BITS__
[74c5a1ca]1368 printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1369#endif
1370
1371#ifdef __64_BITS__
[74c5a1ca]1372 printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1373#endif
[43b1e86]1374
[000350f8]1375 /*
1376 * Because printing may require allocation of memory, we may not hold
1377 * the frame allocator locks when printing zone statistics. Therefore,
1378 * we simply gather the statistics under the protection of the locks and
1379 * print the statistics when the locks have been released.
1380 *
1381 * When someone adds/removes zones while we are printing the statistics,
1382 * we may end up with inaccurate output (e.g. a zone being skipped from
1383 * the listing).
1384 */
[5f0f29ce]1385
[98000fb]1386 size_t i;
[5f0f29ce]1387 for (i = 0;; i++) {
[da1bafb]1388 irq_spinlock_lock(&zones.lock, true);
[000350f8]1389
1390 if (i >= zones.count) {
[da1bafb]1391 irq_spinlock_unlock(&zones.lock, true);
[000350f8]1392 break;
1393 }
[5f0f29ce]1394
1395 uintptr_t base = PFN2ADDR(zones.info[i].base);
[98000fb]1396 size_t count = zones.info[i].count;
[5f0f29ce]1397 zone_flags_t flags = zones.info[i].flags;
[98000fb]1398 size_t free_count = zones.info[i].free_count;
1399 size_t busy_count = zones.info[i].busy_count;
[000350f8]1400
[da1bafb]1401 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1402
[e6c4b94]1403 bool available = ((flags & ZONE_AVAILABLE) != 0);
[5f0f29ce]1404
[7e752b2]1405 printf("%-4zu", i);
[e49e234]1406
[2b8b0ca]1407#ifdef __32_BITS__
[7e752b2]1408 printf(" %p", (void *) base);
[2b8b0ca]1409#endif
[5f0f29ce]1410
[2b8b0ca]1411#ifdef __64_BITS__
[7e752b2]1412 printf(" %p", (void *) base);
[e49e234]1413#endif
1414
[e6c4b94]1415 printf(" %12zu %c%c%c%c%c ", count,
1416 available ? 'A' : '-',
1417 (flags & ZONE_RESERVED) ? 'R' : '-',
1418 (flags & ZONE_FIRMWARE) ? 'F' : '-',
1419 (flags & ZONE_LOWMEM) ? 'L' : '-',
1420 (flags & ZONE_HIGHMEM) ? 'H' : '-');
[43b1e86]1421
[5f0f29ce]1422 if (available)
[7e752b2]1423 printf("%14zu %14zu",
[5f0f29ce]1424 free_count, busy_count);
[e49e234]1425
[5f0f29ce]1426 printf("\n");
[dfd9186]1427 }
1428}
1429
[abbc16e]1430/** Prints zone details.
[96cacc1]1431 *
[5f0f29ce]1432 * @param num Zone base address or zone number.
1433 *
[96cacc1]1434 */
[98000fb]1435void zone_print_one(size_t num)
[deaf8d5]1436{
[da1bafb]1437 irq_spinlock_lock(&zones.lock, true);
[98000fb]1438 size_t znum = (size_t) -1;
[5f0f29ce]1439
[98000fb]1440 size_t i;
[b6b576c]1441 for (i = 0; i < zones.count; i++) {
[5f0f29ce]1442 if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1443 znum = i;
[bb68433]1444 break;
1445 }
1446 }
[5f0f29ce]1447
[98000fb]1448 if (znum == (size_t) -1) {
[da1bafb]1449 irq_spinlock_unlock(&zones.lock, true);
[bb68433]1450 printf("Zone not found.\n");
[2ec725f]1451 return;
[dfd9186]1452 }
[085d973]1453
[5f0f29ce]1454 uintptr_t base = PFN2ADDR(zones.info[i].base);
1455 zone_flags_t flags = zones.info[i].flags;
[98000fb]1456 size_t count = zones.info[i].count;
1457 size_t free_count = zones.info[i].free_count;
1458 size_t busy_count = zones.info[i].busy_count;
[5f0f29ce]1459
[da1bafb]1460 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1461
[e6c4b94]1462 bool available = ((flags & ZONE_AVAILABLE) != 0);
[5f0f29ce]1463
[933cadf]1464 uint64_t size;
1465 const char *size_suffix;
1466 bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
1467
[7e752b2]1468 printf("Zone number: %zu\n", znum);
1469 printf("Zone base address: %p\n", (void *) base);
[933cadf]1470 printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
1471 size, size_suffix);
[e6c4b94]1472 printf("Zone flags: %c%c%c%c%c\n",
1473 available ? 'A' : '-',
1474 (flags & ZONE_RESERVED) ? 'R' : '-',
1475 (flags & ZONE_FIRMWARE) ? 'F' : '-',
1476 (flags & ZONE_LOWMEM) ? 'L' : '-',
1477 (flags & ZONE_HIGHMEM) ? 'H' : '-');
[5f0f29ce]1478
1479 if (available) {
[933cadf]1480 bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
1481 false);
1482 printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
1483 busy_count, size, size_suffix);
1484 bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
1485 false);
1486 printf("Available space: %zu frames (%" PRIu64 " %s)\n",
1487 free_count, size, size_suffix);
[5f0f29ce]1488 }
[dfd9186]1489}
1490
[cc73a8a1]1491/** @}
[b45c443]1492 */
Note: See TracBrowser for help on using the repository browser.