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