source: mainline/generic/src/mm/frame.c@ f275cb3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f275cb3 was f275cb3, checked in by Sergey Bondari <bondari@…>, 19 years ago

Frame alloc test #1

  • Property mode set to 100644
File size: 13.5 KB
RevLine 
[f761f1eb]1/*
[b87f418]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
[84dd253]30#include <typedefs.h>
[f761f1eb]31#include <arch/types.h>
32#include <mm/heap.h>
33#include <mm/frame.h>
34#include <mm/vm.h>
35#include <panic.h>
[fcacfb7]36#include <debug.h>
[84dd253]37#include <list.h>
[f761f1eb]38#include <synch/spinlock.h>
[18e0a6c]39#include <arch/asm.h>
[9c0a9b3]40#include <arch.h>
[328f2934]41#include <print.h>
[9ebc238]42#include <align.h>
[18e0a6c]43
[dc747e3]44SPINLOCK_INITIALIZE(zone_head_lock); /**< this lock protects zone_head list */
[7dd2561]45LIST_INITIALIZE(zone_head); /**< list of all zones in the system */
[fcacfb7]46
[eef75f6]47/** Blacklist containing non-available areas of memory.
48 *
49 * This blacklist is used to exclude frames that cannot be allocated
50 * (e.g. kernel memory) from available memory map.
51 */
[328f2934]52region_t zone_blacklist[ZONE_BLACKLIST_SIZE];
53count_t zone_blacklist_count = 0;
54
[6e8b3c8]55static struct buddy_system_operations zone_buddy_system_operations = {
56 .find_buddy = zone_buddy_find_buddy,
57 .bisect = zone_buddy_bisect,
58 .coalesce = zone_buddy_coalesce,
59 .set_order = zone_buddy_set_order,
60 .get_order = zone_buddy_get_order,
[328f2934]61 .mark_busy = zone_buddy_mark_busy,
[6e8b3c8]62};
63
[84dd253]64/** Initialize physical memory management
65 *
66 * Initialize physical memory managemnt.
67 */
[f761f1eb]68void frame_init(void)
69{
[76cec1e]70 if (config.cpu_active == 1) {
[4457455]71 frame_region_not_free(KA2PA(config.base), config.kernel_size);
[961b5f0]72 if (config.init_size > 0)
73 frame_region_not_free(KA2PA(config.init_addr), config.init_size);
[f761f1eb]74 }
75
76 frame_arch_init();
77}
78
[4457455]79/** Allocate power-of-two frames of physical memory.
[84dd253]80 *
81 * @param flags Flags for host zone selection and address processing.
[4457455]82 * @param order Allocate exactly 2^order frames.
[84dd253]83 *
84 * @return Allocated frame.
[f761f1eb]85 */
[f275cb3]86__address frame_alloc(int flags, __u8 order, int * status)
[f761f1eb]87{
[22f7769]88 ipl_t ipl;
[84dd253]89 link_t *cur, *tmp;
90 zone_t *z;
91 zone_t *zone = NULL;
92 frame_t *frame = NULL;
93 __address v;
[f761f1eb]94
95loop:
[22f7769]96 ipl = interrupts_disable();
[84dd253]97 spinlock_lock(&zone_head_lock);
[328f2934]98
[84dd253]99 /*
100 * First, find suitable frame zone.
101 */
102 for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
103 z = list_get_instance(cur, zone_t, link);
[f761f1eb]104
[84dd253]105 spinlock_lock(&z->lock);
[328f2934]106
107 /* Check if the zone has 2^order frames area available */
108 if (buddy_system_can_alloc(z->buddy_system, order)) {
[84dd253]109 zone = z;
110 break;
[f761f1eb]111 }
[328f2934]112
[84dd253]113 spinlock_unlock(&z->lock);
[f761f1eb]114 }
[84dd253]115
116 if (!zone) {
117 if (flags & FRAME_PANIC)
118 panic("Can't allocate frame.\n");
119
[f275cb3]120
121
[84dd253]122 /*
123 * TODO: Sleep until frames are available again.
124 */
125 spinlock_unlock(&zone_head_lock);
[22f7769]126 interrupts_restore(ipl);
[f761f1eb]127
[f275cb3]128 if (flags & FRAME_NON_BLOCKING) {
129 ASSERT(status != NULL);
130 *status = FRAME_NO_MEMORY;
131 return NULL;
132 }
133
[84dd253]134 panic("Sleep not implemented.\n");
135 goto loop;
136 }
[f761f1eb]137
[84dd253]138
[328f2934]139 /* Allocate frames from zone buddy system */
[eef75f6]140 tmp = buddy_system_alloc(zone->buddy_system, order);
141
142 ASSERT(tmp);
[84dd253]143
[eef75f6]144 /* Update zone information. */
145 zone->free_count -= (1 << order);
146 zone->busy_count += (1 << order);
147
148 /* Frame will be actually a first frame of the block. */
149 frame = list_get_instance(tmp, frame_t, buddy_link);
[84dd253]150
[328f2934]151 /* get frame address */
152 v = FRAME2ADDR(zone, frame);
153
[84dd253]154 spinlock_unlock(&zone->lock);
155 spinlock_unlock(&zone_head_lock);
[22f7769]156 interrupts_restore(ipl);
[328f2934]157
[eef75f6]158
159 if (flags & FRAME_KA)
160 v = PA2KA(v);
161
[f275cb3]162 if (flags & FRAME_NON_BLOCKING) {
163 ASSERT(status != NULL);
164 *status = FRAME_OK;
165 }
166
[eef75f6]167 return v;
[f761f1eb]168}
169
[84dd253]170/** Free a frame.
171 *
172 * Find respective frame structrue for supplied addr.
173 * Decrement frame reference count.
174 * If it drops to zero, move the frame structure to free list.
175 *
176 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
[f761f1eb]177 */
178void frame_free(__address addr)
179{
[22f7769]180 ipl_t ipl;
[84dd253]181 link_t *cur;
182 zone_t *z;
183 zone_t *zone = NULL;
184 frame_t *frame;
185 ASSERT(addr % FRAME_SIZE == 0);
[f761f1eb]186
[22f7769]187 ipl = interrupts_disable();
[84dd253]188 spinlock_lock(&zone_head_lock);
[f761f1eb]189
[84dd253]190 /*
191 * First, find host frame zone for addr.
192 */
193 for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
194 z = list_get_instance(cur, zone_t, link);
195
196 spinlock_lock(&z->lock);
197
198 if (IS_KA(addr))
199 addr = KA2PA(addr);
200
201 /*
202 * Check if addr belongs to z.
203 */
204 if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
205 zone = z;
206 break;
[f761f1eb]207 }
[84dd253]208 spinlock_unlock(&z->lock);
209 }
210
211 ASSERT(zone != NULL);
212
[6e8b3c8]213 frame = ADDR2FRAME(zone, addr);
[328f2934]214
[84dd253]215 ASSERT(frame->refcount);
216
217 if (!--frame->refcount) {
[328f2934]218 buddy_system_free(zone->buddy_system, &frame->buddy_link);
[f761f1eb]219 }
[eef75f6]220
221 /* Update zone information. */
222 zone->free_count += (1 << frame->buddy_order);
223 zone->busy_count -= (1 << frame->buddy_order);
[84dd253]224
[eef75f6]225 spinlock_unlock(&zone->lock);
[84dd253]226 spinlock_unlock(&zone_head_lock);
[22f7769]227 interrupts_restore(ipl);
[f761f1eb]228}
229
[84dd253]230/** Mark frame region not free.
231 *
232 * Mark frame region not free.
233 *
[4457455]234 * @param base Base address of non-available region.
235 * @param size Size of non-available region.
[84dd253]236 */
[328f2934]237void frame_region_not_free(__address base, size_t size)
[f761f1eb]238{
[4457455]239 index_t index;
[328f2934]240 index = zone_blacklist_count++;
[4457455]241
242 /* Force base to the nearest lower address frame boundary. */
[b87f418]243 base = ALIGN_DOWN(base, FRAME_SIZE);
[4457455]244 /* Align size to frame boundary. */
[b87f418]245 size = ALIGN_UP(size, FRAME_SIZE);
[4457455]246
[b87f418]247 ASSERT(index < ZONE_BLACKLIST_SIZE);
[328f2934]248 zone_blacklist[index].base = base;
249 zone_blacklist[index].size = size;
[f761f1eb]250}
[fcacfb7]251
[eef75f6]252/** Create frame zones in region of available memory.
253 *
254 * Avoid any black listed areas of non-available memory.
255 * Assume that the black listed areas cannot overlap
256 * one another or cross available memory region boundaries.
257 *
258 * @param base Base address of available memory region.
259 * @param size Size of the region.
260 */
[328f2934]261void zone_create_in_region(__address base, size_t size) {
262 int i;
263 zone_t * z;
[4457455]264 __address s;
265 size_t sz;
[328f2934]266
267 ASSERT(base % FRAME_SIZE == 0);
268 ASSERT(size % FRAME_SIZE == 0);
269
[4457455]270 if (!size)
271 return;
272
[328f2934]273 for (i = 0; i < zone_blacklist_count; i++) {
274 if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
275 s = base; sz = zone_blacklist[i].base - base;
276 ASSERT(base != s || sz != size);
277 zone_create_in_region(s, sz);
278
279 s = zone_blacklist[i].base + zone_blacklist[i].size;
280 sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
281 ASSERT(base != s || sz != size);
282 zone_create_in_region(s, sz);
283 return;
284
285 }
286 }
287
288 z = zone_create(base, size, 0);
289
290 if (!z) {
[b87f418]291 panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
[328f2934]292 }
293
294 zone_attach(z);
295}
296
297
[fcacfb7]298/** Create frame zone
299 *
300 * Create new frame zone.
301 *
302 * @param start Physical address of the first frame within the zone.
303 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
304 * @param flags Zone flags.
305 *
306 * @return Initialized zone.
307 */
[328f2934]308zone_t * zone_create(__address start, size_t size, int flags)
[fcacfb7]309{
310 zone_t *z;
311 count_t cnt;
312 int i;
[6e8b3c8]313 __u8 max_order;
[328f2934]314
[fcacfb7]315 ASSERT(start % FRAME_SIZE == 0);
316 ASSERT(size % FRAME_SIZE == 0);
317
318 cnt = size / FRAME_SIZE;
319
[adecf496]320 z = (zone_t *) early_malloc(sizeof(zone_t));
[fcacfb7]321 if (z) {
322 link_initialize(&z->link);
[2d93f1f9]323 spinlock_initialize(&z->lock, "zone_lock");
[fcacfb7]324
325 z->base = start;
326 z->flags = flags;
327
328 z->free_count = cnt;
329 z->busy_count = 0;
330
[adecf496]331 z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
[fcacfb7]332 if (!z->frames) {
[48a02ef]333 early_free(z);
[fcacfb7]334 return NULL;
335 }
336
337 for (i = 0; i<cnt; i++) {
338 frame_initialize(&z->frames[i], z);
339 }
340
[6e8b3c8]341 /*
342 * Create buddy system for the zone
343 */
[4457455]344 for (max_order = 0; cnt >> max_order; max_order++)
345 ;
[594a468]346 z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
[328f2934]347
348 /* Stuffing frames */
349 for (i = 0; i<cnt; i++) {
350 z->frames[i].refcount = 0;
351 buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);
352 }
[fcacfb7]353 }
354 return z;
355}
356
357/** Attach frame zone
358 *
359 * Attach frame zone to zone list.
360 *
361 * @param zone Zone to be attached.
362 */
363void zone_attach(zone_t *zone)
364{
[22f7769]365 ipl_t ipl;
[fcacfb7]366
[22f7769]367 ipl = interrupts_disable();
[fcacfb7]368 spinlock_lock(&zone_head_lock);
369
370 list_append(&zone->link, &zone_head);
371
372 spinlock_unlock(&zone_head_lock);
[22f7769]373 interrupts_restore(ipl);
[fcacfb7]374}
375
376/** Initialize frame structure
377 *
378 * Initialize frame structure.
379 *
380 * @param frame Frame structure to be initialized.
381 * @param zone Host frame zone.
382 */
383void frame_initialize(frame_t *frame, zone_t *zone)
384{
[328f2934]385 frame->refcount = 1;
386 frame->buddy_order = 0;
[fcacfb7]387}
[6e8b3c8]388
389
390/** Buddy system find_buddy implementation
[594a468]391 *
392 * @param b Buddy system.
[30187eb]393 * @param block Block for which buddy should be found
[6e8b3c8]394 *
[30187eb]395 * @return Buddy for given block if found
[6e8b3c8]396 */
[594a468]397link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
[eef75f6]398 frame_t * frame;
[30187eb]399 zone_t * zone;
[b87f418]400 index_t index;
[30187eb]401 bool is_left, is_right;
[6e8b3c8]402
[30187eb]403 frame = list_get_instance(block, frame_t, buddy_link);
[328f2934]404 zone = (zone_t *) b->data;
[30187eb]405
[b87f418]406 ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX(zone, frame), frame->buddy_order));
407
[30187eb]408 is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
[b87f418]409 is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
410
411 ASSERT(is_left ^ is_right);
[30187eb]412
[328f2934]413 if (is_left) {
414 index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
[5a95b25]415 } else { // if (is_right)
[328f2934]416 index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
417 }
418
419 if (FRAME_INDEX_VALID(zone, index)) {
420 if ( zone->frames[index].buddy_order == frame->buddy_order &&
421 zone->frames[index].refcount == 0) {
422 return &zone->frames[index].buddy_link;
[30187eb]423 }
424 }
425
[eef75f6]426 return NULL;
[6e8b3c8]427}
428
429/** Buddy system bisect implementation
430 *
[594a468]431 * @param b Buddy system.
[30187eb]432 * @param block Block to bisect
433 *
434 * @return right block
[6e8b3c8]435 */
[594a468]436link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
[30187eb]437 frame_t * frame_l, * frame_r;
[b87f418]438
[30187eb]439 frame_l = list_get_instance(block, frame_t, buddy_link);
[328f2934]440 frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
[b87f418]441
[30187eb]442 return &frame_r->buddy_link;
[6e8b3c8]443}
444
445/** Buddy system coalesce implementation
446 *
[594a468]447 * @param b Buddy system.
[30187eb]448 * @param block_1 First block
449 * @param block_2 First block's buddy
450 *
451 * @return Coalesced block (actually block that represents lower address)
[6e8b3c8]452 */
[594a468]453link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
[30187eb]454 frame_t * frame1, * frame2;
[b87f418]455
[30187eb]456 frame1 = list_get_instance(block_1, frame_t, buddy_link);
457 frame2 = list_get_instance(block_2, frame_t, buddy_link);
[b87f418]458
[328f2934]459 return frame1 < frame2 ? block_1 : block_2;
[6e8b3c8]460}
461
462/** Buddy system set_order implementation
[594a468]463 *
464 * @param b Buddy system.
[30187eb]465 * @param block Buddy system block
466 * @param order Order to set
[6e8b3c8]467 */
[594a468]468void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
[30187eb]469 frame_t * frame;
470 frame = list_get_instance(block, frame_t, buddy_link);
471 frame->buddy_order = order;
[6e8b3c8]472}
473
474/** Buddy system get_order implementation
[594a468]475 *
476 * @param b Buddy system.
[30187eb]477 * @param block Buddy system block
[6e8b3c8]478 *
[30187eb]479 * @return Order of block
[6e8b3c8]480 */
[594a468]481__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
[30187eb]482 frame_t * frame;
483 frame = list_get_instance(block, frame_t, buddy_link);
484 return frame->buddy_order;
[6e8b3c8]485}
[328f2934]486
487/** Buddy system mark_busy implementation
488 *
489 * @param b Buddy system
490 * @param block Buddy system block
491 *
492 */
493void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
494 frame_t * frame;
495 frame = list_get_instance(block, frame_t, buddy_link);
496 frame->refcount = 1;
497}
[dfd9186]498
[96cacc1]499/** Prints list of zones
500 *
501 */
[dfd9186]502void zone_print_list(void) {
503 zone_t *zone = NULL;
504 link_t *cur;
[566ba81]505 spinlock_lock(&zone_head_lock);
[59adc2b]506 printf("Base address\tFree Frames\tBusy Frames\n");
507 printf("------------\t-----------\t-----------\n");
[dfd9186]508 for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
509 zone = list_get_instance(cur, zone_t, link);
[566ba81]510 spinlock_lock(&zone->lock);
[59adc2b]511 printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
[dfd9186]512 }
[566ba81]513 spinlock_unlock(&zone_head_lock);
[dfd9186]514
515}
516
[96cacc1]517/** Prints zone details
518 *
[566ba81]519 * @param base Zone base address
[96cacc1]520 */
[566ba81]521void zone_print_one(__address base) {
522 zone_t *zone = NULL, *z ;
[dfd9186]523 link_t *cur;
[566ba81]524
525
526 spinlock_lock(&zone_head_lock);
527
[dfd9186]528
529 for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
[566ba81]530 z = list_get_instance(cur, zone_t, link);
531 if (base == z->base) {
532 zone = z;
[dfd9186]533 break;
534 }
535 }
536
[566ba81]537
[dfd9186]538 if (!zone) {
[566ba81]539 printf("No zone with address %X\n", base);
[dfd9186]540 return;
541 }
542
[566ba81]543 spinlock_lock(&zone->lock);
544 printf("Memory zone information\n\n");
[dfd9186]545 printf("Zone base address: %P\n", zone->base);
546 printf("Zone size: %d frames (%d kbytes)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
547 printf("Allocated space: %d frames (%d kbytes)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
548 printf("Available space: %d (%d kbytes)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
[566ba81]549
550 printf("\nBuddy allocator structures:\n\n");
[59adc2b]551 buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
[566ba81]552
553 spinlock_unlock(&zone->lock);
554 spinlock_unlock(&zone_head_lock);
555
[dfd9186]556}
557
Note: See TracBrowser for help on using the repository browser.