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

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

frame_free_generic() can actually free multiple frames.

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