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

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

Feed memory from the newly created zone into the reservation system.

  • Property mode set to 100644
File size: 35.9 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2005 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
[5f0f29ce]4 * Copyright (c) 2009 Martin Decky
[f761f1eb]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[cc73a8a1]31/** @addtogroup genericmm
[b45c443]32 * @{
33 */
34
[9179d0a]35/**
[b45c443]36 * @file
[5f0f29ce]37 * @brief Physical frame allocator.
[9179d0a]38 *
39 * This file contains the physical frame allocator and memory zone management.
40 * The frame allocator is built on top of the buddy allocator.
41 *
42 * @see buddy.c
43 */
44
[d99c1d2]45#include <typedefs.h>
[f761f1eb]46#include <mm/frame.h>
[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--;
[d060900]521 reserve_force_alloc(1);
[085d973]522}
523
[5f0f29ce]524/** Merge two zones.
[bb68433]525 *
[5f0f29ce]526 * Expect buddy to point to space at least zone_conf_size large.
527 * Assume z1 & z2 are locked and compatible and zones lock is
528 * locked.
[bb68433]529 *
[5f0f29ce]530 * @param z1 First zone to merge.
531 * @param z2 Second zone to merge.
532 * @param old_z1 Original date of the first zone.
533 * @param buddy Merged zone buddy.
[eb3d379]534 *
[bb68433]535 */
[7a0359b]536NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
537 buddy_system_t *buddy)
[bb68433]538{
[5f0f29ce]539 ASSERT(zone_flags_available(zones.info[z1].flags));
540 ASSERT(zone_flags_available(zones.info[z2].flags));
541 ASSERT(zones.info[z1].flags == zones.info[z2].flags);
542 ASSERT(zones.info[z1].base < zones.info[z2].base);
543 ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
544 zones.info[z2].base, zones.info[z2].count));
545
546 /* Difference between zone bases */
547 pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
548
549 zones.info[z1].count = base_diff + zones.info[z2].count;
550 zones.info[z1].free_count += zones.info[z2].free_count;
551 zones.info[z1].busy_count += zones.info[z2].busy_count;
552 zones.info[z1].buddy_system = buddy;
553
554 uint8_t order = fnzb(zones.info[z1].count);
555 buddy_system_create(zones.info[z1].buddy_system, order,
556 &zone_buddy_system_operations, (void *) &zones.info[z1]);
557
558 zones.info[z1].frames =
559 (frame_t *) ((uint8_t *) zones.info[z1].buddy_system
560 + buddy_conf_size(order));
561
562 /* This marks all frames busy */
[98000fb]563 size_t i;
[5f0f29ce]564 for (i = 0; i < zones.info[z1].count; i++)
565 frame_initialize(&zones.info[z1].frames[i]);
[bb68433]566
567 /* Copy frames from both zones to preserve full frame orders,
[5f0f29ce]568 * parents etc. Set all free frames with refcount = 0 to 1, because
569 * we add all free frames to buddy allocator later again, clearing
570 * order to 0. Don't set busy frames with refcount = 0, as they
[ad64a2d]571 * will not be reallocated during merge and it would make later
572 * problems with allocation/free.
[bb68433]573 */
[5f0f29ce]574 for (i = 0; i < old_z1->count; i++)
575 zones.info[z1].frames[i] = old_z1->frames[i];
576
577 for (i = 0; i < zones.info[z2].count; i++)
578 zones.info[z1].frames[base_diff + i]
579 = zones.info[z2].frames[i];
580
[ad64a2d]581 i = 0;
[5f0f29ce]582 while (i < zones.info[z1].count) {
583 if (zones.info[z1].frames[i].refcount) {
584 /* Skip busy frames */
585 i += 1 << zones.info[z1].frames[i].buddy_order;
586 } else {
587 /* Free frames, set refcount = 1
588 * (all free frames have refcount == 0, we need not
589 * to check the order)
590 */
591 zones.info[z1].frames[i].refcount = 1;
592 zones.info[z1].frames[i].buddy_order = 0;
[ad64a2d]593 i++;
[bb68433]594 }
595 }
[5f0f29ce]596
597 /* Add free blocks from the original zone z1 */
598 while (zone_can_alloc(old_z1, 0)) {
599 /* Allocate from the original zone */
600 pfn_t frame_idx = zone_frame_alloc(old_z1, 0);
601
602 /* Free the frame from the merged zone */
603 frame_t *frame = &zones.info[z1].frames[frame_idx];
[bb68433]604 frame->refcount = 0;
[5f0f29ce]605 buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
[bb68433]606 }
[5f0f29ce]607
608 /* Add free blocks from the original zone z2 */
609 while (zone_can_alloc(&zones.info[z2], 0)) {
610 /* Allocate from the original zone */
611 pfn_t frame_idx = zone_frame_alloc(&zones.info[z2], 0);
612
613 /* Free the frame from the merged zone */
614 frame_t *frame = &zones.info[z1].frames[base_diff + frame_idx];
[bb68433]615 frame->refcount = 0;
[5f0f29ce]616 buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
[bb68433]617 }
618}
619
[deaf8d5]620/** Return old configuration frames into the zone.
[bb68433]621 *
[5f0f29ce]622 * We have two cases:
623 * - The configuration data is outside the zone
624 * -> do nothing (perhaps call frame_free?)
625 * - The configuration data was created by zone_create
626 * or updated by reduce_region -> free every frame
627 *
628 * @param znum The actual zone where freeing should occur.
629 * @param pfn Old zone configuration frame.
630 * @param count Old zone frame count.
[874878a]631 *
[bb68433]632 */
[7a0359b]633NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
[bb68433]634{
[5f0f29ce]635 ASSERT(zone_flags_available(zones.info[znum].flags));
636
[98000fb]637 size_t cframes = SIZE2FRAMES(zone_conf_size(count));
[bb68433]638
[5f0f29ce]639 if ((pfn < zones.info[znum].base)
640 || (pfn >= zones.info[znum].base + zones.info[znum].count))
[bb68433]641 return;
[5f0f29ce]642
[9482bf0b]643 frame_t *frame __attribute__ ((unused));
644
645 frame = &zones.info[znum].frames[pfn - zones.info[znum].base];
[874878a]646 ASSERT(!frame->buddy_order);
[5f0f29ce]647
[98000fb]648 size_t i;
[2936eef]649 for (i = 0; i < cframes; i++) {
[5f0f29ce]650 zones.info[znum].busy_count++;
[89bcb520]651 (void) zone_frame_free(&zones.info[znum],
[5f0f29ce]652 pfn - zones.info[znum].base + i);
[bb68433]653 }
654}
655
[deaf8d5]656/** Reduce allocated block to count of order 0 frames.
[874878a]657 *
[5f0f29ce]658 * The allocated block needs 2^order frames. Reduce all frames
659 * in the block to order 0 and free the unneeded frames. This means that
660 * when freeing the previously allocated block starting with frame_idx,
[eb3d379]661 * you have to free every frame.
[874878a]662 *
[5f0f29ce]663 * @param znum Zone.
664 * @param frame_idx Index the first frame of the block.
665 * @param count Allocated frames in block.
666 *
[874878a]667 */
[7a0359b]668NO_TRACE static void zone_reduce_region(size_t znum, pfn_t frame_idx,
669 size_t count)
[874878a]670{
[5f0f29ce]671 ASSERT(zone_flags_available(zones.info[znum].flags));
672 ASSERT(frame_idx + count < zones.info[znum].count);
[874878a]673
[5f0f29ce]674 uint8_t order = zones.info[znum].frames[frame_idx].buddy_order;
[98000fb]675 ASSERT((size_t) (1 << order) >= count);
[5f0f29ce]676
[874878a]677 /* Reduce all blocks to order 0 */
[98000fb]678 size_t i;
679 for (i = 0; i < (size_t) (1 << order); i++) {
[5f0f29ce]680 frame_t *frame = &zones.info[znum].frames[i + frame_idx];
[874878a]681 frame->buddy_order = 0;
[4638401]682 if (!frame->refcount)
[874878a]683 frame->refcount = 1;
684 ASSERT(frame->refcount == 1);
685 }
[5f0f29ce]686
[874878a]687 /* Free unneeded frames */
[98000fb]688 for (i = count; i < (size_t) (1 << order); i++)
[89bcb520]689 (void) zone_frame_free(&zones.info[znum], i + frame_idx);
[874878a]690}
691
[deaf8d5]692/** Merge zones z1 and z2.
[bb68433]693 *
[5f0f29ce]694 * The merged zones must be 2 zones with no zone existing in between
695 * (which means that z2 = z1 + 1). Both zones must be available zones
696 * with the same flags.
697 *
698 * When you create a new zone, the frame allocator configuration does
699 * not to be 2^order size. Once the allocator is running it is no longer
700 * possible, merged configuration data occupies more space :-/
701 *
[bb68433]702 */
[98000fb]703bool zone_merge(size_t z1, size_t z2)
[bb68433]704{
[da1bafb]705 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]706
707 bool ret = true;
708
709 /* We can join only 2 zones with none existing inbetween,
710 * the zones have to be available and with the same
711 * set of flags
712 */
713 if ((z1 >= zones.count) || (z2 >= zones.count)
714 || (z2 - z1 != 1)
715 || (!zone_flags_available(zones.info[z1].flags))
716 || (!zone_flags_available(zones.info[z2].flags))
717 || (zones.info[z1].flags != zones.info[z2].flags)) {
718 ret = false;
[bb68433]719 goto errout;
[5f0f29ce]720 }
721
722 pfn_t cframes = SIZE2FRAMES(zone_conf_size(
723 zones.info[z2].base - zones.info[z1].base
724 + zones.info[z2].count));
725
726 uint8_t order;
[279952c]727 if (cframes == 1)
728 order = 0;
[bb68433]729 else
[5f0f29ce]730 order = fnzb(cframes - 1) + 1;
731
732 /* Allocate merged zone data inside one of the zones */
733 pfn_t pfn;
734 if (zone_can_alloc(&zones.info[z1], order)) {
735 pfn = zones.info[z1].base + zone_frame_alloc(&zones.info[z1], order);
736 } else if (zone_can_alloc(&zones.info[z2], order)) {
737 pfn = zones.info[z2].base + zone_frame_alloc(&zones.info[z2], order);
738 } else {
739 ret = false;
740 goto errout;
741 }
742
743 /* Preserve original data from z1 */
744 zone_t old_z1 = zones.info[z1];
745 old_z1.buddy_system->data = (void *) &old_z1;
746
747 /* Do zone merging */
748 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(pfn));
749 zone_merge_internal(z1, z2, &old_z1, buddy);
750
[874878a]751 /* Free unneeded config frames */
[5f0f29ce]752 zone_reduce_region(z1, pfn - zones.info[z1].base, cframes);
753
[bb68433]754 /* Subtract zone information from busy frames */
[5f0f29ce]755 zones.info[z1].busy_count -= cframes;
756
757 /* Free old zone information */
758 return_config_frames(z1,
759 ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
760 return_config_frames(z1,
761 ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
762 zones.info[z2].count);
763
[e49e234]764 /* Move zones down */
[98000fb]765 size_t i;
[e49e234]766 for (i = z2 + 1; i < zones.count; i++) {
[b6b576c]767 zones.info[i - 1] = zones.info[i];
[af96dd57]768 if (zones.info[i - 1].buddy_system != NULL)
769 zones.info[i - 1].buddy_system->data =
770 (void *) &zones.info[i - 1];
[e49e234]771 }
772
[bb68433]773 zones.count--;
[5f0f29ce]774
[bb68433]775errout:
[da1bafb]776 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]777
778 return ret;
[bb68433]779}
780
[5f0f29ce]781/** Merge all mergeable zones into one big zone.
782 *
783 * It is reasonable to do this on systems where
784 * BIOS reports parts in chunks, so that we could
785 * have 1 zone (it's faster).
[bb68433]786 *
787 */
788void zone_merge_all(void)
789{
[98000fb]790 size_t i = 0;
[5f0f29ce]791 while (i < zones.count) {
792 if (!zone_merge(i, i + 1))
793 i++;
[bb68433]794 }
795}
796
[deaf8d5]797/** Create new frame zone.
[085d973]798 *
[5f0f29ce]799 * @param zone Zone to construct.
800 * @param buddy Address of buddy system configuration information.
801 * @param start Physical address of the first frame within the zone.
802 * @param count Count of frames in zone.
803 * @param flags Zone flags.
804 *
805 * @return Initialized zone.
[085d973]806 *
807 */
[7a0359b]808NO_TRACE static void zone_construct(zone_t *zone, buddy_system_t *buddy,
809 pfn_t start, size_t count, zone_flags_t flags)
[085d973]810{
[5f0f29ce]811 zone->base = start;
812 zone->count = count;
813 zone->flags = flags;
814 zone->free_count = count;
815 zone->busy_count = 0;
816 zone->buddy_system = buddy;
[085d973]817
[5f0f29ce]818 if (zone_flags_available(flags)) {
819 /*
820 * Compute order for buddy system and initialize
821 */
822 uint8_t order = fnzb(count);
823 buddy_system_create(zone->buddy_system, order,
824 &zone_buddy_system_operations, (void *) zone);
825
826 /* Allocate frames _after_ the confframe */
827
828 /* Check sizes */
829 zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
830 buddy_conf_size(order));
831
[98000fb]832 size_t i;
[5f0f29ce]833 for (i = 0; i < count; i++)
834 frame_initialize(&zone->frames[i]);
835
836 /* Stuffing frames */
837 for (i = 0; i < count; i++) {
838 zone->frames[i].refcount = 0;
839 buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
840 }
[d060900]841
842 /* "Unreserve" new frames. */
843 reserve_free(count);
[5f0f29ce]844 } else
845 zone->frames = NULL;
[085d973]846}
847
[deaf8d5]848/** Compute configuration data size for zone.
[eb3d379]849 *
[5f0f29ce]850 * @param count Size of zone in frames.
851 *
852 * @return Size of zone configuration info (in bytes).
853 *
[eb3d379]854 */
[bbfdf62]855size_t zone_conf_size(size_t count)
[085d973]856{
[5f0f29ce]857 return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
[085d973]858}
859
[deaf8d5]860/** Create and add zone to system.
[085d973]861 *
[5f0f29ce]862 * @param start First frame number (absolute).
863 * @param count Size of zone in frames.
864 * @param confframe Where configuration frames are supposed to be.
865 * Automatically checks, that we will not disturb the
866 * kernel and possibly init. If confframe is given
867 * _outside_ this zone, it is expected, that the area is
868 * already marked BUSY and big enough to contain
869 * zone_conf_size() amount of data. If the confframe is
870 * inside the area, the zone free frame information is
871 * modified not to include it.
872 *
873 * @return Zone number or -1 on error.
874 *
[085d973]875 */
[da1bafb]876size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
877 zone_flags_t flags)
[085d973]878{
[da1bafb]879 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]880
881 if (zone_flags_available(flags)) { /* Create available zone */
882 /* Theoretically we could have NULL here, practically make sure
883 * nobody tries to do that. If some platform requires, remove
884 * the assert
885 */
[0b4a67a]886 ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
[5f0f29ce]887
888 /* If confframe is supposed to be inside our zone, then make sure
889 * it does not span kernel & init
890 */
[98000fb]891 size_t confcount = SIZE2FRAMES(zone_conf_size(count));
[5f0f29ce]892 if ((confframe >= start) && (confframe < start + count)) {
893 for (; confframe < start + count; confframe++) {
894 uintptr_t addr = PFN2ADDR(confframe);
895 if (overlaps(addr, PFN2ADDR(confcount),
896 KA2PA(config.base), config.kernel_size))
897 continue;
898
[4638401]899 if (overlaps(addr, PFN2ADDR(confcount),
[5f0f29ce]900 KA2PA(config.stack_base), config.stack_size))
901 continue;
902
903 bool overlap = false;
[98000fb]904 size_t i;
[5f0f29ce]905 for (i = 0; i < init.cnt; i++)
906 if (overlaps(addr, PFN2ADDR(confcount),
907 KA2PA(init.tasks[i].addr),
908 init.tasks[i].size)) {
909 overlap = true;
910 break;
911 }
912 if (overlap)
913 continue;
914
915 break;
916 }
[b6b576c]917
[5f0f29ce]918 if (confframe >= start + count)
919 panic("Cannot find configuration data for zone.");
[085d973]920 }
[5f0f29ce]921
[e2650d3]922 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]923 if (znum == (size_t) -1) {
[da1bafb]924 irq_spinlock_unlock(&zones.lock, true);
[98000fb]925 return (size_t) -1;
[5f0f29ce]926 }
927
928 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
929 zone_construct(&zones.info[znum], buddy, start, count, flags);
930
931 /* If confdata in zone, mark as unavailable */
932 if ((confframe >= start) && (confframe < start + count)) {
[98000fb]933 size_t i;
[5f0f29ce]934 for (i = confframe; i < confframe + confcount; i++)
935 zone_mark_unavailable(&zones.info[znum],
936 i - zones.info[znum].base);
[085d973]937 }
[5f0f29ce]938
[da1bafb]939 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]940
941 return znum;
942 }
943
944 /* Non-available zone */
[e2650d3]945 size_t znum = zones_insert_zone(start, count, flags);
[98000fb]946 if (znum == (size_t) -1) {
[da1bafb]947 irq_spinlock_unlock(&zones.lock, true);
[98000fb]948 return (size_t) -1;
[5f0f29ce]949 }
950 zone_construct(&zones.info[znum], NULL, start, count, flags);
951
[da1bafb]952 irq_spinlock_unlock(&zones.lock, true);
[1bb3766]953
[bb68433]954 return znum;
[085d973]955}
956
[5f0f29ce]957/*******************/
[085d973]958/* Frame functions */
[5f0f29ce]959/*******************/
[085d973]960
[deaf8d5]961/** Set parent of frame. */
[98000fb]962void frame_set_parent(pfn_t pfn, void *data, size_t hint)
[085d973]963{
[da1bafb]964 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]965
[98000fb]966 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]967
[98000fb]968 ASSERT(znum != (size_t) -1);
[5f0f29ce]969
970 zone_get_frame(&zones.info[znum],
971 pfn - zones.info[znum].base)->parent = data;
972
[da1bafb]973 irq_spinlock_unlock(&zones.lock, true);
[085d973]974}
975
[98000fb]976void *frame_get_parent(pfn_t pfn, size_t hint)
[085d973]977{
[da1bafb]978 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]979
[98000fb]980 size_t znum = find_zone(pfn, 1, hint);
[5f0f29ce]981
[98000fb]982 ASSERT(znum != (size_t) -1);
[5f0f29ce]983
984 void *res = zone_get_frame(&zones.info[znum],
985 pfn - zones.info[znum].base)->parent;
986
[da1bafb]987 irq_spinlock_unlock(&zones.lock, true);
[085d973]988
989 return res;
990}
991
992/** Allocate power-of-two frames of physical memory.
993 *
[5f0f29ce]994 * @param order Allocate exactly 2^order frames.
995 * @param flags Flags for host zone selection and address processing.
996 * @param pzone Preferred zone.
[085d973]997 *
[5f0f29ce]998 * @return Physical address of the allocated frame.
[9a68b34d]999 *
[085d973]1000 */
[98000fb]1001void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
[085d973]1002{
[98000fb]1003 size_t size = ((size_t) 1) << order;
1004 size_t hint = pzone ? (*pzone) : 0;
[085d973]1005
[89bcb520]1006 /*
1007 * If not told otherwise, we must first reserve the memory.
1008 */
1009 if (!(flags & FRAME_NO_RESERVE)) {
1010 if (flags & FRAME_ATOMIC) {
1011 if (!reserve_try_alloc(size))
1012 return NULL;
1013 } else {
1014 reserve_force_alloc(size);
1015 }
1016 }
1017
[085d973]1018loop:
[da1bafb]1019 irq_spinlock_lock(&zones.lock, true);
[9a68b34d]1020
[085d973]1021 /*
1022 * First, find suitable frame zone.
1023 */
[98000fb]1024 size_t znum = find_free_zone(order,
[5f0f29ce]1025 FRAME_TO_ZONE_FLAGS(flags), hint);
[9a68b34d]1026
[085d973]1027 /* If no memory, reclaim some slab memory,
1028 if it does not help, reclaim all */
[98000fb]1029 if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
[da1bafb]1030 irq_spinlock_unlock(&zones.lock, true);
[98000fb]1031 size_t freed = slab_reclaim(0);
[da1bafb]1032 irq_spinlock_lock(&zones.lock, true);
[f049eec]1033
[5f0f29ce]1034 if (freed > 0)
1035 znum = find_free_zone(order,
1036 FRAME_TO_ZONE_FLAGS(flags), hint);
1037
[98000fb]1038 if (znum == (size_t) -1) {
[da1bafb]1039 irq_spinlock_unlock(&zones.lock, true);
[085d973]1040 freed = slab_reclaim(SLAB_RECLAIM_ALL);
[da1bafb]1041 irq_spinlock_lock(&zones.lock, true);
[f049eec]1042
[5f0f29ce]1043 if (freed > 0)
1044 znum = find_free_zone(order,
1045 FRAME_TO_ZONE_FLAGS(flags), hint);
[085d973]1046 }
1047 }
[5f0f29ce]1048
[98000fb]1049 if (znum == (size_t) -1) {
[1a1744e]1050 if (flags & FRAME_ATOMIC) {
[da1bafb]1051 irq_spinlock_unlock(&zones.lock, true);
[89bcb520]1052 if (!(flags & FRAME_NO_RESERVE))
1053 reserve_free(size);
[5f0f29ce]1054 return NULL;
[1a1744e]1055 }
[085d973]1056
[1a1744e]1057#ifdef CONFIG_DEBUG
[98000fb]1058 size_t avail = total_frames_free();
[1a1744e]1059#endif
[5f0f29ce]1060
[da1bafb]1061 irq_spinlock_unlock(&zones.lock, true);
1062
[05411e8]1063 if (!THREAD)
1064 panic("Cannot wait for memory to become available.");
[5f0f29ce]1065
1066 /*
1067 * Sleep until some frames are available again.
1068 */
1069
1070#ifdef CONFIG_DEBUG
[7e752b2]1071 printf("Thread %" PRIu64 " waiting for %zu frames, "
1072 "%zu available.\n", THREAD->tid, size, avail);
[5f0f29ce]1073#endif
1074
[1bb3766]1075 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1076
1077 if (mem_avail_req > 0)
1078 mem_avail_req = min(mem_avail_req, size);
1079 else
1080 mem_avail_req = size;
[98000fb]1081 size_t gen = mem_avail_gen;
[5f0f29ce]1082
1083 while (gen == mem_avail_gen)
[1bb3766]1084 condvar_wait(&mem_avail_cv, &mem_avail_mtx);
[5f0f29ce]1085
[1bb3766]1086 mutex_unlock(&mem_avail_mtx);
[5f0f29ce]1087
[1a1744e]1088#ifdef CONFIG_DEBUG
[5f0f29ce]1089 printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
[1a1744e]1090#endif
[5f0f29ce]1091
[085d973]1092 goto loop;
1093 }
[9a68b34d]1094
[5f0f29ce]1095 pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
1096 + zones.info[znum].base;
[1bb3766]1097
[da1bafb]1098 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1099
1100 if (pzone)
1101 *pzone = znum;
1102
[2e9eae2]1103 if (flags & FRAME_KA)
[5f0f29ce]1104 return (void *) PA2KA(PFN2ADDR(pfn));
1105
1106 return (void *) PFN2ADDR(pfn);
[085d973]1107}
1108
[0139747]1109void *frame_alloc(uint8_t order, frame_flags_t flags)
1110{
1111 return frame_alloc_generic(order, flags, NULL);
1112}
1113
[e608cbe]1114void *frame_alloc_noreserve(uint8_t order, frame_flags_t flags)
1115{
1116 return frame_alloc_generic(order, flags | FRAME_NO_RESERVE, NULL);
1117}
1118
[085d973]1119/** Free a frame.
1120 *
[32fffef0]1121 * Find respective frame structure for supplied physical frame address.
[5f0f29ce]1122 * Decrement frame reference count. If it drops to zero, move the frame
1123 * structure to free list.
1124 *
1125 * @param frame Physical Address of of the frame to be freed.
[c32e6bc]1126 * @param flags Flags to control memory reservation.
[085d973]1127 *
1128 */
[c32e6bc]1129void frame_free_generic(uintptr_t frame, frame_flags_t flags)
[085d973]1130{
[89bcb520]1131 size_t size;
1132
[da1bafb]1133 irq_spinlock_lock(&zones.lock, true);
[5f0f29ce]1134
[085d973]1135 /*
1136 * First, find host frame zone for addr.
1137 */
[5f0f29ce]1138 pfn_t pfn = ADDR2PFN(frame);
[0b4a67a]1139 size_t znum = find_zone(pfn, 1, 0);
[89bcb520]1140
[5f0f29ce]1141
[98000fb]1142 ASSERT(znum != (size_t) -1);
[085d973]1143
[89bcb520]1144 size = zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
[085d973]1145
[da1bafb]1146 irq_spinlock_unlock(&zones.lock, true);
[1a1744e]1147
1148 /*
1149 * Signal that some memory has been freed.
1150 */
[1bb3766]1151 mutex_lock(&mem_avail_mtx);
[5f0f29ce]1152 if (mem_avail_req > 0)
[28295955]1153 mem_avail_req -= min(mem_avail_req, size);
[5f0f29ce]1154
1155 if (mem_avail_req == 0) {
1156 mem_avail_gen++;
1157 condvar_broadcast(&mem_avail_cv);
1158 }
[1bb3766]1159 mutex_unlock(&mem_avail_mtx);
[89bcb520]1160
1161 if (!(flags & FRAME_NO_RESERVE))
1162 reserve_free(size);
[085d973]1163}
1164
[c32e6bc]1165void frame_free(uintptr_t frame)
1166{
1167 frame_free_generic(frame, 0);
1168}
1169
[e608cbe]1170void frame_free_noreserve(uintptr_t frame)
1171{
1172 frame_free_generic(frame, FRAME_NO_RESERVE);
1173}
1174
[f3ac636]1175/** Add reference to frame.
1176 *
1177 * Find respective frame structure for supplied PFN and
1178 * increment frame reference count.
1179 *
[5f0f29ce]1180 * @param pfn Frame number of the frame to be freed.
1181 *
[f3ac636]1182 */
[97bdb4a]1183NO_TRACE void frame_reference_add(pfn_t pfn)
[f3ac636]1184{
[da1bafb]1185 irq_spinlock_lock(&zones.lock, true);
[f3ac636]1186
1187 /*
1188 * First, find host frame zone for addr.
1189 */
[0b4a67a]1190 size_t znum = find_zone(pfn, 1, 0);
[5f0f29ce]1191
[98000fb]1192 ASSERT(znum != (size_t) -1);
[f3ac636]1193
[5f0f29ce]1194 zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
[f3ac636]1195
[da1bafb]1196 irq_spinlock_unlock(&zones.lock, true);
[f3ac636]1197}
[085d973]1198
[da1bafb]1199/** Mark given range unavailable in frame zones.
1200 *
1201 */
[97bdb4a]1202NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
[085d973]1203{
[da1bafb]1204 irq_spinlock_lock(&zones.lock, true);
[052da81]1205
[98000fb]1206 size_t i;
[2936eef]1207 for (i = 0; i < count; i++) {
[98000fb]1208 size_t znum = find_zone(start + i, 1, 0);
1209 if (znum == (size_t) -1) /* PFN not found */
[085d973]1210 continue;
[5f0f29ce]1211
1212 zone_mark_unavailable(&zones.info[znum],
1213 start + i - zones.info[znum].base);
[085d973]1214 }
[5f0f29ce]1215
[da1bafb]1216 irq_spinlock_unlock(&zones.lock, true);
[085d973]1217}
1218
[da1bafb]1219/** Initialize physical memory management.
1220 *
1221 */
[085d973]1222void frame_init(void)
1223{
1224 if (config.cpu_active == 1) {
1225 zones.count = 0;
[da1bafb]1226 irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
[1bb3766]1227 mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1228 condvar_initialize(&mem_avail_cv);
[085d973]1229 }
[5f0f29ce]1230
[085d973]1231 /* Tell the architecture to create some memory */
1232 frame_arch_init();
1233 if (config.cpu_active == 1) {
[4638401]1234 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1235 SIZE2FRAMES(config.kernel_size));
1236 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1237 SIZE2FRAMES(config.stack_size));
[b6b576c]1238
[98000fb]1239 size_t i;
[4638401]1240 for (i = 0; i < init.cnt; i++) {
1241 pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr));
1242 frame_mark_unavailable(pfn,
1243 SIZE2FRAMES(init.tasks[i].size));
1244 }
[5f0f29ce]1245
[61e90dd]1246 if (ballocs.size)
[4638401]1247 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1248 SIZE2FRAMES(ballocs.size));
[5f0f29ce]1249
[bffa0b06]1250 /* Black list first frame, as allocating NULL would
[5f0f29ce]1251 * fail in some places
1252 */
[bffa0b06]1253 frame_mark_unavailable(0, 1);
[085d973]1254 }
1255}
1256
[da1bafb]1257/** Return total size of all zones.
1258 *
1259 */
[9dae191e]1260uint64_t zones_total_size(void)
[deaf8d5]1261{
[da1bafb]1262 irq_spinlock_lock(&zones.lock, true);
[71eef11]1263
[5f0f29ce]1264 uint64_t total = 0;
[98000fb]1265 size_t i;
[5f0f29ce]1266 for (i = 0; i < zones.count; i++)
1267 total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[71eef11]1268
[da1bafb]1269 irq_spinlock_unlock(&zones.lock, true);
[71eef11]1270
1271 return total;
1272}
1273
[9dae191e]1274void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
1275 uint64_t *free)
[516adce]1276{
[9dae191e]1277 ASSERT(total != NULL);
1278 ASSERT(unavail != NULL);
1279 ASSERT(busy != NULL);
1280 ASSERT(free != NULL);
1281
[da1bafb]1282 irq_spinlock_lock(&zones.lock, true);
[9dae191e]1283
1284 *total = 0;
1285 *unavail = 0;
1286 *busy = 0;
1287 *free = 0;
1288
[516adce]1289 size_t i;
1290 for (i = 0; i < zones.count; i++) {
[9dae191e]1291 *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1292
1293 if (zone_flags_available(zones.info[i].flags)) {
1294 *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
1295 *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
1296 } else
1297 *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
[516adce]1298 }
[9dae191e]1299
[da1bafb]1300 irq_spinlock_unlock(&zones.lock, true);
[516adce]1301}
1302
[da1bafb]1303/** Prints list of zones.
1304 *
1305 */
[9dae191e]1306void zones_print_list(void)
[deaf8d5]1307{
[5f0f29ce]1308#ifdef __32_BITS__
[74c5a1ca]1309 printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1310#endif
1311
1312#ifdef __64_BITS__
[74c5a1ca]1313 printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
[2b8b0ca]1314#endif
[43b1e86]1315
[000350f8]1316 /*
1317 * Because printing may require allocation of memory, we may not hold
1318 * the frame allocator locks when printing zone statistics. Therefore,
1319 * we simply gather the statistics under the protection of the locks and
1320 * print the statistics when the locks have been released.
1321 *
1322 * When someone adds/removes zones while we are printing the statistics,
1323 * we may end up with inaccurate output (e.g. a zone being skipped from
1324 * the listing).
1325 */
[5f0f29ce]1326
[98000fb]1327 size_t i;
[5f0f29ce]1328 for (i = 0;; i++) {
[da1bafb]1329 irq_spinlock_lock(&zones.lock, true);
[000350f8]1330
1331 if (i >= zones.count) {
[da1bafb]1332 irq_spinlock_unlock(&zones.lock, true);
[000350f8]1333 break;
1334 }
[5f0f29ce]1335
1336 uintptr_t base = PFN2ADDR(zones.info[i].base);
[98000fb]1337 size_t count = zones.info[i].count;
[5f0f29ce]1338 zone_flags_t flags = zones.info[i].flags;
[98000fb]1339 size_t free_count = zones.info[i].free_count;
1340 size_t busy_count = zones.info[i].busy_count;
[000350f8]1341
[da1bafb]1342 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1343
1344 bool available = zone_flags_available(flags);
1345
[7e752b2]1346 printf("%-4zu", i);
[e49e234]1347
[2b8b0ca]1348#ifdef __32_BITS__
[7e752b2]1349 printf(" %p", (void *) base);
[2b8b0ca]1350#endif
[5f0f29ce]1351
[2b8b0ca]1352#ifdef __64_BITS__
[7e752b2]1353 printf(" %p", (void *) base);
[e49e234]1354#endif
1355
[7e752b2]1356 printf(" %12zu %c%c%c ", count,
[5f0f29ce]1357 available ? 'A' : ' ',
1358 (flags & ZONE_RESERVED) ? 'R' : ' ',
1359 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
[43b1e86]1360
[5f0f29ce]1361 if (available)
[7e752b2]1362 printf("%14zu %14zu",
[5f0f29ce]1363 free_count, busy_count);
[e49e234]1364
[5f0f29ce]1365 printf("\n");
[dfd9186]1366 }
1367}
1368
[abbc16e]1369/** Prints zone details.
[96cacc1]1370 *
[5f0f29ce]1371 * @param num Zone base address or zone number.
1372 *
[96cacc1]1373 */
[98000fb]1374void zone_print_one(size_t num)
[deaf8d5]1375{
[da1bafb]1376 irq_spinlock_lock(&zones.lock, true);
[98000fb]1377 size_t znum = (size_t) -1;
[5f0f29ce]1378
[98000fb]1379 size_t i;
[b6b576c]1380 for (i = 0; i < zones.count; i++) {
[5f0f29ce]1381 if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1382 znum = i;
[bb68433]1383 break;
1384 }
1385 }
[5f0f29ce]1386
[98000fb]1387 if (znum == (size_t) -1) {
[da1bafb]1388 irq_spinlock_unlock(&zones.lock, true);
[bb68433]1389 printf("Zone not found.\n");
[2ec725f]1390 return;
[dfd9186]1391 }
[085d973]1392
[5f0f29ce]1393 uintptr_t base = PFN2ADDR(zones.info[i].base);
1394 zone_flags_t flags = zones.info[i].flags;
[98000fb]1395 size_t count = zones.info[i].count;
1396 size_t free_count = zones.info[i].free_count;
1397 size_t busy_count = zones.info[i].busy_count;
[5f0f29ce]1398
[da1bafb]1399 irq_spinlock_unlock(&zones.lock, true);
[5f0f29ce]1400
1401 bool available = zone_flags_available(flags);
1402
[7e752b2]1403 printf("Zone number: %zu\n", znum);
1404 printf("Zone base address: %p\n", (void *) base);
1405 printf("Zone size: %zu frames (%zu KiB)\n", count,
[2ec725f]1406 SIZE2KB(FRAMES2SIZE(count)));
[5f0f29ce]1407 printf("Zone flags: %c%c%c\n",
1408 available ? 'A' : ' ',
1409 (flags & ZONE_RESERVED) ? 'R' : ' ',
1410 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1411
1412 if (available) {
[7e752b2]1413 printf("Allocated space: %zu frames (%zu KiB)\n",
[5f0f29ce]1414 busy_count, SIZE2KB(FRAMES2SIZE(busy_count)));
[7e752b2]1415 printf("Available space: %zu frames (%zu KiB)\n",
[5f0f29ce]1416 free_count, SIZE2KB(FRAMES2SIZE(free_count)));
1417 }
[dfd9186]1418}
1419
[cc73a8a1]1420/** @}
[b45c443]1421 */
Note: See TracBrowser for help on using the repository browser.