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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2936eef was 2936eef, checked in by Martin Decky <martin@…>, 18 years ago

explicit typecast, fix signed/unsigned comparison

  • Property mode set to 100644
File size: 29.0 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2005 Jakub Jermar
3 * Copyright (c) 2005 Sergey Bondari
[f761f1eb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[cc73a8a1]30/** @addtogroup genericmm
[b45c443]31 * @{
32 */
33
[9179d0a]34/**
[b45c443]35 * @file
[9179d0a]36 * @brief Physical frame allocator.
37 *
38 * This file contains the physical frame allocator and memory zone management.
39 * The frame allocator is built on top of the buddy allocator.
40 *
41 * @see buddy.c
42 */
43
[085d973]44/*
45 * Locking order
46 *
47 * In order to access particular zone, the process must first lock
48 * the zones.lock, then lock the zone and then unlock the zones.lock.
49 * This insures, that we can fiddle with the zones in runtime without
50 * affecting the processes.
51 *
52 */
53
[f761f1eb]54#include <arch/types.h>
55#include <mm/frame.h>
[20d50a1]56#include <mm/as.h>
[f761f1eb]57#include <panic.h>
[fcacfb7]58#include <debug.h>
[5c9a08b]59#include <adt/list.h>
[f761f1eb]60#include <synch/spinlock.h>
[18e0a6c]61#include <arch/asm.h>
[9c0a9b3]62#include <arch.h>
[328f2934]63#include <print.h>
[9ebc238]64#include <align.h>
[085d973]65#include <mm/slab.h>
[bb68433]66#include <bitops.h>
[93165be]67#include <macros.h>
[18e0a6c]68
[085d973]69typedef struct {
70 count_t refcount; /**< tracking of shared frames */
[7f1c620]71 uint8_t buddy_order; /**< buddy system block order */
[085d973]72 link_t buddy_link; /**< link to the next free block inside one order */
73 void *parent; /**< If allocated by slab, this points there */
[f3ac636]74} frame_t;
[fcacfb7]75
[085d973]76typedef struct {
77 SPINLOCK_DECLARE(lock); /**< this lock protects everything below */
[f3ac636]78 pfn_t base; /**< frame_no of the first frame in the frames array */
[42744880]79 count_t count; /**< Size of zone */
[328f2934]80
[085d973]81 frame_t *frames; /**< array of frame_t structures in this zone */
82 count_t free_count; /**< number of free frame_t structures */
83 count_t busy_count; /**< number of busy frame_t structures */
84
[b43eaba0]85 buddy_system_t *buddy_system; /**< buddy system for the zone */
[085d973]86 int flags;
[f3ac636]87} zone_t;
[6e8b3c8]88
[085d973]89/*
90 * The zoneinfo.lock must be locked when accessing zoneinfo structure.
91 * Some of the attributes in zone_t structures are 'read-only'
[84dd253]92 */
[f761f1eb]93
[55b4437]94typedef struct {
[085d973]95 SPINLOCK_DECLARE(lock);
[2936eef]96 unsigned int count;
[085d973]97 zone_t *info[ZONES_MAX];
[55b4437]98} zones_t;
99
100static zones_t zones;
[f761f1eb]101
[a294ad0]102
[085d973]103/*********************************/
104/* Helper functions */
105static inline index_t frame_index(zone_t *zone, frame_t *frame)
106{
107 return (index_t)(frame - zone->frames);
[a294ad0]108}
[085d973]109static inline index_t frame_index_abs(zone_t *zone, frame_t *frame)
[f761f1eb]110{
[085d973]111 return (index_t)(frame - zone->frames) + zone->base;
[f761f1eb]112}
[085d973]113static inline int frame_index_valid(zone_t *zone, index_t index)
[a294ad0]114{
[085d973]115 return index >= 0 && index < zone->count;
[a294ad0]116}
117
[085d973]118/** Compute pfn_t from frame_t pointer & zone pointer */
[42744880]119static index_t make_frame_index(zone_t *zone, frame_t *frame)
[a294ad0]120{
[085d973]121 return frame - zone->frames;
[a294ad0]122}
123
[085d973]124/** Initialize frame structure
[84dd253]125 *
[085d973]126 * Initialize frame structure.
[84dd253]127 *
[085d973]128 * @param frame Frame structure to be initialized.
[f761f1eb]129 */
[085d973]130static void frame_initialize(frame_t *frame)
[f761f1eb]131{
[085d973]132 frame->refcount = 1;
133 frame->buddy_order = 0;
[f761f1eb]134}
135
[085d973]136/*************************************/
137/* Zoneinfo functions */
138
139/**
140 * Insert-sort zone into zones list
[bb68433]141 *
[eb3d379]142 * @param newzone New zone to be inserted into zone list
[bb68433]143 * @return zone number on success, -1 on error
[84dd253]144 */
[bb68433]145static int zones_add_zone(zone_t *newzone)
[f761f1eb]146{
[2936eef]147 unsigned int i, j;
[bb68433]148 ipl_t ipl;
149 zone_t *z;
[4457455]150
[bb68433]151 ipl = interrupts_disable();
[085d973]152 spinlock_lock(&zones.lock);
153 /* Try to merge */
[b6b576c]154 if (zones.count + 1 == ZONES_MAX)
[bb68433]155 panic("Maximum zone(%d) count exceeded.", ZONES_MAX);
[b6b576c]156 for (i = 0; i < zones.count; i++) {
[bb68433]157 /* Check for overflow */
[052da81]158 z = zones.info[i];
[bb68433]159 if (overlaps(newzone->base,newzone->count,
160 z->base, z->count)) {
161 printf("Zones overlap!\n");
162 return -1;
[085d973]163 }
[052da81]164 if (newzone->base < z->base)
[bb68433]165 break;
[085d973]166 }
[bb68433]167 /* Move other zones up */
[b6b576c]168 for (j = i;j < zones.count; j++)
169 zones.info[j + 1] = zones.info[j];
[bb68433]170 zones.info[i] = newzone;
171 zones.count++;
[085d973]172 spinlock_unlock(&zones.lock);
[bb68433]173 interrupts_restore(ipl);
174
175 return i;
[f761f1eb]176}
[fcacfb7]177
[085d973]178/**
179 * Try to find a zone where can we find the frame
[b43eaba0]180
181 * Assume interrupts are disabled.
182
[eb3d379]183 * @param frame Frame number contained in zone
184 * @param pzone If not null, it is used as zone hint. Zone index
185 * is filled into the variable on success.
[b43eaba0]186 * @return Pointer to locked zone containing frame
[eef75f6]187 */
[2936eef]188static zone_t * find_zone_and_lock(pfn_t frame, unsigned int *pzone)
[085d973]189{
[2936eef]190 unsigned int i;
191 unsigned int hint = pzone ? *pzone : 0;
[085d973]192 zone_t *z;
[328f2934]193
[085d973]194 spinlock_lock(&zones.lock);
[4457455]195
[085d973]196 if (hint >= zones.count || hint < 0)
197 hint = 0;
[328f2934]198
[085d973]199 i = hint;
200 do {
201 z = zones.info[i];
202 spinlock_lock(&z->lock);
203 if (z->base <= frame && z->base + z->count > frame) {
204 spinlock_unlock(&zones.lock); /* Unlock the global lock */
205 if (pzone)
206 *pzone = i;
207 return z;
208 }
209 spinlock_unlock(&z->lock);
[328f2934]210
[085d973]211 i++;
212 if (i >= zones.count)
213 i = 0;
214 } while(i != hint);
[328f2934]215
[085d973]216 spinlock_unlock(&zones.lock);
217 return NULL;
218}
[328f2934]219
[bb68433]220/** @return True if zone can allocate specified order */
[7f1c620]221static int zone_can_alloc(zone_t *z, uint8_t order)
[bb68433]222{
223 return buddy_system_can_alloc(z->buddy_system, order);
224}
225
[b43eaba0]226/** Find and lock zone that can allocate order frames.
[fcacfb7]227 *
[b43eaba0]228 * Assume interrupts are disabled.
[fcacfb7]229 *
[eb3d379]230 * @param order Size (2^order) of free space we are trying to find
[085d973]231 * @param pzone Pointer to preferred zone or NULL, on return contains zone number
[fcacfb7]232 */
[e5a2ee8]233static zone_t * find_free_zone_and_lock(uint8_t order, int *pzone)
[fcacfb7]234{
[2936eef]235 unsigned int i;
[085d973]236 zone_t *z;
[2936eef]237 unsigned int hint = pzone ? *pzone : 0;
[085d973]238
239 spinlock_lock(&zones.lock);
240 if (hint >= zones.count)
241 hint = 0;
242 i = hint;
243 do {
244 z = zones.info[i];
[5d2ab23]245
[085d973]246 spinlock_lock(&z->lock);
[fcacfb7]247
[085d973]248 /* Check if the zone has 2^order frames area available */
[bb68433]249 if (zone_can_alloc(z, order)) {
[085d973]250 spinlock_unlock(&zones.lock);
251 if (pzone)
252 *pzone = i;
253 return z;
[328f2934]254 }
[085d973]255 spinlock_unlock(&z->lock);
256 if (++i >= zones.count)
257 i = 0;
258 } while(i != hint);
259 spinlock_unlock(&zones.lock);
260 return NULL;
[fcacfb7]261}
262
[b43eaba0]263/**************************/
[085d973]264/* Buddy system functions */
[b43eaba0]265/**************************/
[085d973]266
267/** Buddy system find_block implementation
[fcacfb7]268 *
[085d973]269 * Find block that is parent of current list.
270 * That means go to lower addresses, until such block is found
[fcacfb7]271 *
[085d973]272 * @param order - Order of parent must be different then this parameter!!
[fcacfb7]273 */
[085d973]274static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child,
[7f1c620]275 uint8_t order)
[fcacfb7]276{
[085d973]277 frame_t * frame;
278 zone_t * zone;
279 index_t index;
[fcacfb7]280
[085d973]281 frame = list_get_instance(child, frame_t, buddy_link);
282 zone = (zone_t *) b->data;
[fcacfb7]283
[085d973]284 index = frame_index(zone, frame);
285 do {
286 if (zone->frames[index].buddy_order != order) {
287 return &zone->frames[index].buddy_link;
288 }
289 } while(index-- > 0);
290 return NULL;
[fcacfb7]291}
[6e8b3c8]292
[bb68433]293static void zone_buddy_print_id(buddy_system_t *b, link_t *block)
294{
295 frame_t * frame;
296 zone_t * zone;
297 index_t index;
298
299 frame = list_get_instance(block, frame_t, buddy_link);
300 zone = (zone_t *) b->data;
301 index = frame_index(zone, frame);
[280a27e]302 printf("%zd", index);
[bb68433]303}
[6e8b3c8]304
305/** Buddy system find_buddy implementation
[594a468]306 *
307 * @param b Buddy system.
[30187eb]308 * @param block Block for which buddy should be found
[6e8b3c8]309 *
[30187eb]310 * @return Buddy for given block if found
[6e8b3c8]311 */
[085d973]312static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block)
313{
[eef75f6]314 frame_t * frame;
[30187eb]315 zone_t * zone;
[b87f418]316 index_t index;
[30187eb]317 bool is_left, is_right;
[6e8b3c8]318
[30187eb]319 frame = list_get_instance(block, frame_t, buddy_link);
[328f2934]320 zone = (zone_t *) b->data;
[085d973]321 ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order));
[b87f418]322
[5d2ab23]323 is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
324 is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
[085d973]325
[b87f418]326 ASSERT(is_left ^ is_right);
[328f2934]327 if (is_left) {
[085d973]328 index = (frame_index(zone, frame)) + (1 << frame->buddy_order);
[e5a2ee8]329 } else { /* if (is_right) */
[085d973]330 index = (frame_index(zone, frame)) - (1 << frame->buddy_order);
[328f2934]331 }
332
[085d973]333 if (frame_index_valid(zone, index)) {
334 if (zone->frames[index].buddy_order == frame->buddy_order &&
335 zone->frames[index].refcount == 0) {
[328f2934]336 return &zone->frames[index].buddy_link;
[30187eb]337 }
338 }
[085d973]339
[eef75f6]340 return NULL;
[6e8b3c8]341}
342
343/** Buddy system bisect implementation
344 *
[594a468]345 * @param b Buddy system.
[30187eb]346 * @param block Block to bisect
347 *
348 * @return right block
[6e8b3c8]349 */
[085d973]350static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
[30187eb]351 frame_t * frame_l, * frame_r;
[b87f418]352
[30187eb]353 frame_l = list_get_instance(block, frame_t, buddy_link);
[328f2934]354 frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
[b87f418]355
[30187eb]356 return &frame_r->buddy_link;
[6e8b3c8]357}
358
359/** Buddy system coalesce implementation
360 *
[594a468]361 * @param b Buddy system.
[30187eb]362 * @param block_1 First block
363 * @param block_2 First block's buddy
364 *
365 * @return Coalesced block (actually block that represents lower address)
[6e8b3c8]366 */
[085d973]367static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1,
[bb68433]368 link_t * block_2)
369{
[085d973]370 frame_t *frame1, *frame2;
[b87f418]371
[30187eb]372 frame1 = list_get_instance(block_1, frame_t, buddy_link);
373 frame2 = list_get_instance(block_2, frame_t, buddy_link);
[b87f418]374
[328f2934]375 return frame1 < frame2 ? block_1 : block_2;
[6e8b3c8]376}
377
378/** Buddy system set_order implementation
[594a468]379 *
380 * @param b Buddy system.
[30187eb]381 * @param block Buddy system block
382 * @param order Order to set
[6e8b3c8]383 */
[7f1c620]384static void zone_buddy_set_order(buddy_system_t *b, link_t * block, uint8_t order) {
[30187eb]385 frame_t * frame;
386 frame = list_get_instance(block, frame_t, buddy_link);
387 frame->buddy_order = order;
[6e8b3c8]388}
389
390/** Buddy system get_order implementation
[594a468]391 *
392 * @param b Buddy system.
[30187eb]393 * @param block Buddy system block
[6e8b3c8]394 *
[30187eb]395 * @return Order of block
[6e8b3c8]396 */
[7f1c620]397static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t * block) {
[30187eb]398 frame_t * frame;
399 frame = list_get_instance(block, frame_t, buddy_link);
400 return frame->buddy_order;
[6e8b3c8]401}
[328f2934]402
403/** Buddy system mark_busy implementation
404 *
405 * @param b Buddy system
406 * @param block Buddy system block
407 *
408 */
[085d973]409static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
[328f2934]410 frame_t * frame;
[bb68433]411
[328f2934]412 frame = list_get_instance(block, frame_t, buddy_link);
413 frame->refcount = 1;
414}
[dfd9186]415
[085d973]416/** Buddy system mark_available implementation
417 *
418 * @param b Buddy system
419 * @param block Buddy system block
420 *
421 */
422static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) {
423 frame_t * frame;
424 frame = list_get_instance(block, frame_t, buddy_link);
425 frame->refcount = 0;
426}
427
[0f3fc9b]428static buddy_system_operations_t zone_buddy_system_operations = {
[085d973]429 .find_buddy = zone_buddy_find_buddy,
430 .bisect = zone_buddy_bisect,
431 .coalesce = zone_buddy_coalesce,
432 .set_order = zone_buddy_set_order,
433 .get_order = zone_buddy_get_order,
434 .mark_busy = zone_buddy_mark_busy,
435 .mark_available = zone_buddy_mark_available,
[bb68433]436 .find_block = zone_buddy_find_block,
437 .print_id = zone_buddy_print_id
[085d973]438};
439
[b43eaba0]440/******************/
[085d973]441/* Zone functions */
[b43eaba0]442/******************/
[085d973]443
444/** Allocate frame in particular zone
445 *
446 * Assume zone is locked
[9a68b34d]447 * Panics if allocation is impossible.
448 *
449 * @param zone Zone to allocate from.
450 * @param order Allocate exactly 2^order frames.
[085d973]451 *
452 * @return Frame index in zone
[9a68b34d]453 *
[085d973]454 */
[7f1c620]455static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
[085d973]456{
457 pfn_t v;
458 link_t *tmp;
459 frame_t *frame;
460
461 /* Allocate frames from zone buddy system */
462 tmp = buddy_system_alloc(zone->buddy_system, order);
463
464 ASSERT(tmp);
465
466 /* Update zone information. */
467 zone->free_count -= (1 << order);
468 zone->busy_count += (1 << order);
469
470 /* Frame will be actually a first frame of the block. */
471 frame = list_get_instance(tmp, frame_t, buddy_link);
472
473 /* get frame address */
474 v = make_frame_index(zone, frame);
475 return v;
476}
477
478/** Free frame from zone
479 *
480 * Assume zone is locked
[eb3d379]481 *
482 * @param zone Pointer to zone from which the frame is to be freed
483 * @param frame_idx Frame index relative to zone
[085d973]484 */
[42744880]485static void zone_frame_free(zone_t *zone, index_t frame_idx)
[085d973]486{
487 frame_t *frame;
[7f1c620]488 uint8_t order;
[085d973]489
490 frame = &zone->frames[frame_idx];
491
492 /* remember frame order */
493 order = frame->buddy_order;
494
495 ASSERT(frame->refcount);
496
497 if (!--frame->refcount) {
498 buddy_system_free(zone->buddy_system, &frame->buddy_link);
[d3dfa42]499
500 /* Update zone information. */
501 zone->free_count += (1 << order);
502 zone->busy_count -= (1 << order);
[085d973]503 }
504}
505
506/** Return frame from zone */
[42744880]507static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx)
[085d973]508{
509 ASSERT(frame_idx < zone->count);
510 return &zone->frames[frame_idx];
511}
512
513/** Mark frame in zone unavailable to allocation */
[42744880]514static void zone_mark_unavailable(zone_t *zone, index_t frame_idx)
[085d973]515{
516 frame_t *frame;
517 link_t *link;
518
519 frame = zone_get_frame(zone, frame_idx);
[bb68433]520 if (frame->refcount)
521 return;
[085d973]522 link = buddy_system_alloc_block(zone->buddy_system,
523 &frame->buddy_link);
524 ASSERT(link);
525 zone->free_count--;
526}
527
[bb68433]528/**
529 * Join 2 zones
530 *
531 * Expect zone_t *z to point to space at least zone_conf_size large
532 *
533 * Assume z1 & z2 are locked
[eb3d379]534 *
535 * @param z Target zone structure pointer
536 * @param z1 Zone to merge
537 * @param z2 Zone to merge
[bb68433]538 */
539static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2)
540{
[7f1c620]541 uint8_t max_order;
[2936eef]542 unsigned int i;
543 int z2idx;
[bb68433]544 pfn_t frame_idx;
545 frame_t *frame;
546
547 ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count));
548 ASSERT(z1->base < z2->base);
549
550 spinlock_initialize(&z->lock, "zone_lock");
551 z->base = z1->base;
552 z->count = z2->base+z2->count - z1->base;
553 z->flags = z1->flags & z2->flags;
554
555 z->free_count = z1->free_count + z2->free_count;
556 z->busy_count = z1->busy_count + z2->busy_count;
557
558 max_order = fnzb(z->count);
559
560 z->buddy_system = (buddy_system_t *)&z[1];
561 buddy_system_create(z->buddy_system, max_order,
562 &zone_buddy_system_operations,
563 (void *) z);
564
[2936eef]565 z->frames = (frame_t *)((uint8_t *) z->buddy_system + buddy_conf_size(max_order));
[bb68433]566 for (i = 0; i < z->count; i++) {
567 /* This marks all frames busy */
568 frame_initialize(&z->frames[i]);
569 }
570 /* Copy frames from both zones to preserve full frame orders,
[ad64a2d]571 * parents etc. Set all free frames with refcount=0 to 1, because
[bb68433]572 * we add all free frames to buddy allocator later again, clear
[ad64a2d]573 * order to 0. Don't set busy frames with refcount=0, as they
574 * will not be reallocated during merge and it would make later
575 * problems with allocation/free.
[bb68433]576 */
[2936eef]577 for (i = 0; i < z1->count; i++)
[bb68433]578 z->frames[i] = z1->frames[i];
[2936eef]579 for (i = 0; i < z2->count; i++) {
[bb68433]580 z2idx = i + (z2->base - z1->base);
581 z->frames[z2idx] = z2->frames[i];
582 }
[ad64a2d]583 i = 0;
584 while (i < z->count) {
585 if (z->frames[i].refcount) {
586 /* skip busy frames */
587 i += 1 << z->frames[i].buddy_order;
588 } else { /* Free frames, set refcount=1 */
589 /* All free frames have refcount=0, we need not
590 * to check the order */
[bb68433]591 z->frames[i].refcount = 1;
592 z->frames[i].buddy_order = 0;
[ad64a2d]593 i++;
[bb68433]594 }
595 }
596 /* Add free blocks from the 2 original zones */
597 while (zone_can_alloc(z1, 0)) {
598 frame_idx = zone_frame_alloc(z1, 0);
599 frame = &z->frames[frame_idx];
600 frame->refcount = 0;
601 buddy_system_free(z->buddy_system, &frame->buddy_link);
602 }
603 while (zone_can_alloc(z2, 0)) {
604 frame_idx = zone_frame_alloc(z2, 0);
605 frame = &z->frames[frame_idx + (z2->base-z1->base)];
606 frame->refcount = 0;
607 buddy_system_free(z->buddy_system, &frame->buddy_link);
608 }
609}
610
611/** Return old configuration frames into the zone
612 *
613 * We have several cases
614 * - the conf. data is outside of zone -> exit, shall we call frame_free??
[874878a]615 * - the conf. data was created by zone_create or
616 * updated with reduce_region -> free every frame
617 *
618 * @param newzone The actual zone where freeing should occur
619 * @param oldzone Pointer to old zone configuration data that should
620 * be freed from new zone
[bb68433]621 */
622static void return_config_frames(zone_t *newzone, zone_t *oldzone)
623{
624 pfn_t pfn;
625 frame_t *frame;
626 count_t cframes;
[2936eef]627 unsigned int i;
[bb68433]628
[7f1c620]629 pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone));
[bb68433]630 cframes = SIZE2FRAMES(zone_conf_size(oldzone->count));
631
632 if (pfn < newzone->base || pfn >= newzone->base + newzone->count)
633 return;
634
635 frame = &newzone->frames[pfn - newzone->base];
[874878a]636 ASSERT(!frame->buddy_order);
[bb68433]637
[2936eef]638 for (i = 0; i < cframes; i++) {
[bb68433]639 newzone->busy_count++;
640 zone_frame_free(newzone, pfn+i-newzone->base);
641 }
642}
643
[874878a]644/** Reduce allocated block to count of order 0 frames
645 *
646 * The allocated block need 2^order frames of space. Reduce all frames
[eb3d379]647 * in block to order 0 and free the unneeded frames. This means, that
648 * when freeing the previously allocated block starting with frame_idx,
649 * you have to free every frame.
[874878a]650 *
651 * @param zone
652 * @param frame_idx Index to block
653 * @param count Allocated space in block
654 */
655static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count)
656{
657 count_t i;
[7f1c620]658 uint8_t order;
[874878a]659 frame_t *frame;
660
[2936eef]661 ASSERT(frame_idx + count < zone->count);
[874878a]662
663 order = zone->frames[frame_idx].buddy_order;
[2936eef]664 ASSERT((count_t) (1 << order) >= count);
[874878a]665
666 /* Reduce all blocks to order 0 */
[2936eef]667 for (i = 0; i < (count_t) (1 << order); i++) {
[874878a]668 frame = &zone->frames[i + frame_idx];
669 frame->buddy_order = 0;
670 if (! frame->refcount)
671 frame->refcount = 1;
672 ASSERT(frame->refcount == 1);
673 }
674 /* Free unneeded frames */
[2936eef]675 for (i = count; i < (count_t) (1 << order); i++) {
[874878a]676 zone_frame_free(zone, i + frame_idx);
677 }
678}
679
[bb68433]680/** Merge zones z1 and z2
681 *
682 * - the zones must be 2 zones with no zone existing in between,
683 * which means that z2 = z1+1
684 *
685 * - When you create a new zone, the frame allocator configuration does
686 * not to be 2^order size. Once the allocator is running it is no longer
687 * possible, merged configuration data occupies more space :-/
688 */
[2936eef]689void zone_merge(unsigned int z1, unsigned int z2)
[bb68433]690{
691 ipl_t ipl;
692 zone_t *zone1, *zone2, *newzone;
[2936eef]693 unsigned int cframes;
[7f1c620]694 uint8_t order;
[2936eef]695 unsigned int i;
[bb68433]696 pfn_t pfn;
697
698 ipl = interrupts_disable();
699 spinlock_lock(&zones.lock);
700
701 if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count)
702 goto errout;
703 /* We can join only 2 zones with none existing inbetween */
704 if (z2-z1 != 1)
705 goto errout;
706
707 zone1 = zones.info[z1];
708 zone2 = zones.info[z2];
709 spinlock_lock(&zone1->lock);
710 spinlock_lock(&zone2->lock);
711
712 cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base));
[279952c]713 if (cframes == 1)
714 order = 0;
715 else
716 order = fnzb(cframes - 1) + 1;
[bb68433]717
718 /* Allocate zonedata inside one of the zones */
719 if (zone_can_alloc(zone1, order))
720 pfn = zone1->base + zone_frame_alloc(zone1, order);
721 else if (zone_can_alloc(zone2, order))
722 pfn = zone2->base + zone_frame_alloc(zone2, order);
723 else
724 goto errout2;
725
[2936eef]726 newzone = (zone_t *) PA2KA(PFN2ADDR(pfn));
[bb68433]727
728 _zone_merge(newzone, zone1, zone2);
729
[874878a]730 /* Free unneeded config frames */
731 zone_reduce_region(newzone, pfn - newzone->base, cframes);
[bb68433]732 /* Subtract zone information from busy frames */
[874878a]733 newzone->busy_count -= cframes;
[bb68433]734
[874878a]735 /* Replace existing zones in zoneinfo list */
[bb68433]736 zones.info[z1] = newzone;
[b6b576c]737 for (i = z2 + 1; i < zones.count; i++)
738 zones.info[i - 1] = zones.info[i];
[bb68433]739 zones.count--;
740
741 /* Free old zone information */
742 return_config_frames(newzone, zone1);
743 return_config_frames(newzone, zone2);
744errout2:
745 /* Nobody is allowed to enter to zone, so we are safe
746 * to touch the spinlocks last time */
747 spinlock_unlock(&zone1->lock);
748 spinlock_unlock(&zone2->lock);
749errout:
750 spinlock_unlock(&zones.lock);
751 interrupts_restore(ipl);
752}
753
754/**
755 * Merge all zones into one big zone
756 *
757 * It is reasonable to do this on systems whose bios reports parts in chunks,
758 * so that we could have 1 zone (it's faster).
759 */
760void zone_merge_all(void)
761{
762 int count = zones.count;
763
764 while (zones.count > 1 && --count) {
765 zone_merge(0,1);
766 break;
767 }
768}
769
[085d973]770/** Create frame zone
771 *
772 * Create new frame zone.
773 *
774 * @param start Physical address of the first frame within the zone.
[eb3d379]775 * @param count Count of frames in zone
776 * @param z Address of configuration information of zone
[085d973]777 * @param flags Zone flags.
778 *
779 * @return Initialized zone.
780 */
[bb68433]781static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags)
[085d973]782{
[2936eef]783 unsigned int i;
[7f1c620]784 uint8_t max_order;
[085d973]785
786 spinlock_initialize(&z->lock, "zone_lock");
787 z->base = start;
788 z->count = count;
789 z->flags = flags;
790 z->free_count = count;
791 z->busy_count = 0;
792
793 /*
794 * Compute order for buddy system, initialize
795 */
[bb68433]796 max_order = fnzb(count);
[085d973]797 z->buddy_system = (buddy_system_t *)&z[1];
798
799 buddy_system_create(z->buddy_system, max_order,
800 &zone_buddy_system_operations,
801 (void *) z);
802
803 /* Allocate frames _after_ the conframe */
804 /* Check sizes */
[2936eef]805 z->frames = (frame_t *)((uint8_t *) z->buddy_system + buddy_conf_size(max_order));
806 for (i = 0; i < count; i++) {
[085d973]807 frame_initialize(&z->frames[i]);
808 }
[052da81]809
[085d973]810 /* Stuffing frames */
811 for (i = 0; i < count; i++) {
812 z->frames[i].refcount = 0;
813 buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);
814 }
815}
816
[eb3d379]817/** Compute configuration data size for zone
818 *
819 * @param count Size of zone in frames
820 * @return Size of zone configuration info (in bytes)
821 */
[7f1c620]822uintptr_t zone_conf_size(count_t count)
[085d973]823{
824 int size = sizeof(zone_t) + count*sizeof(frame_t);
825 int max_order;
826
[bb68433]827 max_order = fnzb(count);
[085d973]828 size += buddy_conf_size(max_order);
829 return size;
830}
831
832/** Create and add zone to system
833 *
[eb3d379]834 * @param start First frame number (absolute)
835 * @param count Size of zone in frames
836 * @param confframe Where configuration frames are supposed to be.
837 * Automatically checks, that we will not disturb the
838 * kernel and possibly init.
[085d973]839 * If confframe is given _outside_ this zone, it is expected,
840 * that the area is already marked BUSY and big enough
[eb3d379]841 * to contain zone_conf_size() amount of data.
842 * If the confframe is inside the area, the zone free frame
843 * information is modified not to include it.
[bb68433]844 *
845 * @return Zone number or -1 on error
[085d973]846 */
[bb68433]847int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags)
[085d973]848{
849 zone_t *z;
[7f1c620]850 uintptr_t addr;
[42744880]851 count_t confcount;
[2936eef]852 unsigned int i;
[bb68433]853 int znum;
[085d973]854
855 /* Theoretically we could have here 0, practically make sure
856 * nobody tries to do that. If some platform requires, remove
857 * the assert
858 */
859 ASSERT(confframe);
860 /* If conframe is supposed to be inside our zone, then make sure
861 * it does not span kernel & init
862 */
[bb68433]863 confcount = SIZE2FRAMES(zone_conf_size(count));
[085d973]864 if (confframe >= start && confframe < start+count) {
[b6b576c]865 for (;confframe < start + count; confframe++) {
[085d973]866 addr = PFN2ADDR(confframe);
[b6b576c]867 if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size))
[085d973]868 continue;
[b6b576c]869
[deaa22f]870 if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.stack_base), config.stack_size))
871 continue;
872
[b6b576c]873 bool overlap = false;
874 count_t i;
875 for (i = 0; i < init.cnt; i++)
876 if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) {
877 overlap = true;
878 break;
879 }
880 if (overlap)
881 continue;
882
[085d973]883 break;
884 }
[b6b576c]885 if (confframe >= start + count)
[085d973]886 panic("Cannot find configuration data for zone.");
887 }
888
[bb68433]889 z = (zone_t *)PA2KA(PFN2ADDR(confframe));
890 zone_construct(start, count, z, flags);
891 znum = zones_add_zone(z);
892 if (znum == -1)
893 return -1;
894
[085d973]895 /* If confdata in zone, mark as unavailable */
[2936eef]896 if (confframe >= start && confframe < start + count)
897 for (i = confframe; i < confframe + confcount; i++) {
[085d973]898 zone_mark_unavailable(z, i - z->base);
899 }
[bb68433]900 return znum;
[085d973]901}
902
903/***************************************/
904/* Frame functions */
905
906/** Set parent of frame */
[2936eef]907void frame_set_parent(pfn_t pfn, void *data, unsigned int hint)
[085d973]908{
909 zone_t *zone = find_zone_and_lock(pfn, &hint);
910
911 ASSERT(zone);
912
913 zone_get_frame(zone, pfn-zone->base)->parent = data;
914 spinlock_unlock(&zone->lock);
915}
916
[2936eef]917void * frame_get_parent(pfn_t pfn, unsigned int hint)
[085d973]918{
919 zone_t *zone = find_zone_and_lock(pfn, &hint);
920 void *res;
921
922 ASSERT(zone);
923 res = zone_get_frame(zone, pfn - zone->base)->parent;
924
925 spinlock_unlock(&zone->lock);
926 return res;
927}
928
929/** Allocate power-of-two frames of physical memory.
930 *
[9a68b34d]931 * @param order Allocate exactly 2^order frames.
932 * @param flags Flags for host zone selection and address processing.
933 * @param pzone Preferred zone
[085d973]934 *
[2e9eae2]935 * @return Physical address of the allocated frame.
[9a68b34d]936 *
[085d973]937 */
[7f1c620]938void * frame_alloc_generic(uint8_t order, int flags, int *pzone)
[085d973]939{
940 ipl_t ipl;
941 int freed;
942 pfn_t v;
943 zone_t *zone;
944
945loop:
946 ipl = interrupts_disable();
[9a68b34d]947
[085d973]948 /*
949 * First, find suitable frame zone.
950 */
[e5a2ee8]951 zone = find_free_zone_and_lock(order, pzone);
[9a68b34d]952
[085d973]953 /* If no memory, reclaim some slab memory,
954 if it does not help, reclaim all */
955 if (!zone && !(flags & FRAME_NO_RECLAIM)) {
956 freed = slab_reclaim(0);
957 if (freed)
[e5a2ee8]958 zone = find_free_zone_and_lock(order, pzone);
[085d973]959 if (!zone) {
960 freed = slab_reclaim(SLAB_RECLAIM_ALL);
961 if (freed)
[e5a2ee8]962 zone = find_free_zone_and_lock(order, pzone);
[085d973]963 }
964 }
965 if (!zone) {
966 /*
967 * TODO: Sleep until frames are available again.
968 */
969 interrupts_restore(ipl);
970
[e45f81a]971 if (flags & FRAME_ATOMIC)
[2e9eae2]972 return 0;
[085d973]973
974 panic("Sleep not implemented.\n");
975 goto loop;
976 }
[9a68b34d]977
978 v = zone_frame_alloc(zone, order);
[085d973]979 v += zone->base;
980
981 spinlock_unlock(&zone->lock);
982 interrupts_restore(ipl);
983
[2e9eae2]984 if (flags & FRAME_KA)
985 return (void *)PA2KA(PFN2ADDR(v));
986 return (void *)PFN2ADDR(v);
[085d973]987}
988
989/** Free a frame.
990 *
[32fffef0]991 * Find respective frame structure for supplied physical frame address.
[085d973]992 * Decrement frame reference count.
993 * If it drops to zero, move the frame structure to free list.
994 *
[2e9eae2]995 * @param Frame Physical Address of of the frame to be freed.
[085d973]996 */
[7f1c620]997void frame_free(uintptr_t frame)
[085d973]998{
999 ipl_t ipl;
1000 zone_t *zone;
[2e9eae2]1001 pfn_t pfn = ADDR2PFN(frame);
[085d973]1002
1003 ipl = interrupts_disable();
1004
1005 /*
1006 * First, find host frame zone for addr.
1007 */
1008 zone = find_zone_and_lock(pfn,NULL);
1009 ASSERT(zone);
1010
1011 zone_frame_free(zone, pfn-zone->base);
1012
1013 spinlock_unlock(&zone->lock);
1014 interrupts_restore(ipl);
1015}
1016
[f3ac636]1017/** Add reference to frame.
1018 *
1019 * Find respective frame structure for supplied PFN and
1020 * increment frame reference count.
1021 *
[abbc16e]1022 * @param pfn Frame number of the frame to be freed.
[f3ac636]1023 */
1024void frame_reference_add(pfn_t pfn)
1025{
1026 ipl_t ipl;
1027 zone_t *zone;
1028 frame_t *frame;
[085d973]1029
[f3ac636]1030 ipl = interrupts_disable();
1031
1032 /*
1033 * First, find host frame zone for addr.
1034 */
1035 zone = find_zone_and_lock(pfn,NULL);
1036 ASSERT(zone);
1037
1038 frame = &zone->frames[pfn-zone->base];
1039 frame->refcount++;
1040
1041 spinlock_unlock(&zone->lock);
1042 interrupts_restore(ipl);
1043}
[085d973]1044
1045/** Mark given range unavailable in frame zones */
[42744880]1046void frame_mark_unavailable(pfn_t start, count_t count)
[085d973]1047{
[2936eef]1048 unsigned int i;
[085d973]1049 zone_t *zone;
[2936eef]1050 unsigned int prefzone = 0;
[052da81]1051
[2936eef]1052 for (i = 0; i < count; i++) {
[e5a2ee8]1053 zone = find_zone_and_lock(start + i, &prefzone);
[085d973]1054 if (!zone) /* PFN not found */
1055 continue;
[e5a2ee8]1056 zone_mark_unavailable(zone, start + i - zone->base);
[085d973]1057
1058 spinlock_unlock(&zone->lock);
1059 }
1060}
1061
1062/** Initialize physical memory management
1063 *
1064 * Initialize physical memory managemnt.
1065 */
1066void frame_init(void)
1067{
1068 if (config.cpu_active == 1) {
1069 zones.count = 0;
[e5a2ee8]1070 spinlock_initialize(&zones.lock, "zones.lock");
[085d973]1071 }
1072 /* Tell the architecture to create some memory */
1073 frame_arch_init();
1074 if (config.cpu_active == 1) {
[deaa22f]1075 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), SIZE2FRAMES(config.kernel_size));
1076 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), SIZE2FRAMES(config.stack_size));
[b6b576c]1077
1078 count_t i;
1079 for (i = 0; i < init.cnt; i++)
1080 frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size));
[bffa0b06]1081
[61e90dd]1082 if (ballocs.size)
1083 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)), SIZE2FRAMES(ballocs.size));
1084
[bffa0b06]1085 /* Black list first frame, as allocating NULL would
[94d614e]1086 * fail in some places */
[bffa0b06]1087 frame_mark_unavailable(0, 1);
[085d973]1088 }
1089}
1090
1091
1092
[96cacc1]1093/** Prints list of zones
1094 *
1095 */
[dfd9186]1096void zone_print_list(void) {
1097 zone_t *zone = NULL;
[2936eef]1098 unsigned int i;
[263104b]1099 ipl_t ipl;
1100
1101 ipl = interrupts_disable();
[085d973]1102 spinlock_lock(&zones.lock);
[f88fcbe]1103 printf("# base address free frames busy frames\n");
1104 printf("-- ------------ ------------ ------------\n");
[b6b576c]1105 for (i = 0; i < zones.count; i++) {
[085d973]1106 zone = zones.info[i];
[566ba81]1107 spinlock_lock(&zone->lock);
[f88fcbe]1108 printf("%-2d %12p %12zd %12zd\n", i, PFN2ADDR(zone->base), zone->free_count, zone->busy_count);
[263104b]1109 spinlock_unlock(&zone->lock);
[dfd9186]1110 }
[085d973]1111 spinlock_unlock(&zones.lock);
[263104b]1112 interrupts_restore(ipl);
[dfd9186]1113}
1114
[abbc16e]1115/** Prints zone details.
[96cacc1]1116 *
[abbc16e]1117 * @param num Zone base address or zone number.
[96cacc1]1118 */
[2936eef]1119void zone_print_one(unsigned int num) {
[085d973]1120 zone_t *zone = NULL;
[263104b]1121 ipl_t ipl;
[2936eef]1122 unsigned int i;
[263104b]1123
1124 ipl = interrupts_disable();
[085d973]1125 spinlock_lock(&zones.lock);
[bb68433]1126
[b6b576c]1127 for (i = 0; i < zones.count; i++) {
[874878a]1128 if (i == num || PFN2ADDR(zones.info[i]->base) == num) {
[bb68433]1129 zone = zones.info[i];
1130 break;
1131 }
1132 }
1133 if (!zone) {
1134 printf("Zone not found.\n");
1135 goto out;
[dfd9186]1136 }
[085d973]1137
[566ba81]1138 spinlock_lock(&zone->lock);
[bb68433]1139 printf("Memory zone information\n");
[7f1c620]1140 printf("Zone base address: %#.*p\n", sizeof(uintptr_t) * 2, PFN2ADDR(zone->base));
[280a27e]1141 printf("Zone size: %zd frames (%zdK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10);
1142 printf("Allocated space: %zd frames (%zdK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
[771cd22]1143 printf("Available space: %zd frames (%zdK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
[59adc2b]1144 buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
[566ba81]1145
1146 spinlock_unlock(&zone->lock);
[bb68433]1147out:
[085d973]1148 spinlock_unlock(&zones.lock);
[263104b]1149 interrupts_restore(ipl);
[dfd9186]1150}
1151
[cc73a8a1]1152/** @}
[b45c443]1153 */
Note: See TracBrowser for help on using the repository browser.