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