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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fe19611 was eb3d379, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Slightly remodelled ipc hangups to facilitate correct cleanup.
Doc updates for frame allocator.

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