[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2005 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2005 Sergey Bondari
|
---|
[5f0f29ce] | 4 | * Copyright (c) 2009 Martin Decky
|
---|
[f761f1eb] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[cc73a8a1] | 31 | /** @addtogroup genericmm
|
---|
[b45c443] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[9179d0a] | 35 | /**
|
---|
[b45c443] | 36 | * @file
|
---|
[5f0f29ce] | 37 | * @brief Physical frame allocator.
|
---|
[9179d0a] | 38 | *
|
---|
| 39 | * This file contains the physical frame allocator and memory zone management.
|
---|
| 40 | * The frame allocator is built on top of the buddy allocator.
|
---|
| 41 | *
|
---|
| 42 | * @see buddy.c
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[d99c1d2] | 45 | #include <typedefs.h>
|
---|
[f761f1eb] | 46 | #include <mm/frame.h>
|
---|
[89bcb520] | 47 | #include <mm/reserve.h>
|
---|
[20d50a1] | 48 | #include <mm/as.h>
|
---|
[f761f1eb] | 49 | #include <panic.h>
|
---|
[fcacfb7] | 50 | #include <debug.h>
|
---|
[5c9a08b] | 51 | #include <adt/list.h>
|
---|
[1a1744e] | 52 | #include <synch/mutex.h>
|
---|
| 53 | #include <synch/condvar.h>
|
---|
[18e0a6c] | 54 | #include <arch/asm.h>
|
---|
[9c0a9b3] | 55 | #include <arch.h>
|
---|
[328f2934] | 56 | #include <print.h>
|
---|
[9ebc238] | 57 | #include <align.h>
|
---|
[085d973] | 58 | #include <mm/slab.h>
|
---|
[bb68433] | 59 | #include <bitops.h>
|
---|
[93165be] | 60 | #include <macros.h>
|
---|
[d630139] | 61 | #include <config.h>
|
---|
[933cadf] | 62 | #include <str.h>
|
---|
[1066041] | 63 | #include <proc/thread.h> /* THREAD */
|
---|
[18e0a6c] | 64 |
|
---|
[e49e234] | 65 | zones_t zones;
|
---|
[f761f1eb] | 66 |
|
---|
[1a1744e] | 67 | /*
|
---|
| 68 | * Synchronization primitives used to sleep when there is no memory
|
---|
| 69 | * available.
|
---|
| 70 | */
|
---|
[da1bafb] | 71 | static mutex_t mem_avail_mtx;
|
---|
| 72 | static condvar_t mem_avail_cv;
|
---|
| 73 | static size_t mem_avail_req = 0; /**< Number of frames requested. */
|
---|
| 74 | static size_t mem_avail_gen = 0; /**< Generation counter. */
|
---|
[a294ad0] | 75 |
|
---|
[71eef11] | 76 | /********************/
|
---|
[085d973] | 77 | /* Helper functions */
|
---|
[71eef11] | 78 | /********************/
|
---|
| 79 |
|
---|
[7a0359b] | 80 | NO_TRACE static inline size_t frame_index(zone_t *zone, frame_t *frame)
|
---|
[085d973] | 81 | {
|
---|
[98000fb] | 82 | return (size_t) (frame - zone->frames);
|
---|
[a294ad0] | 83 | }
|
---|
[71eef11] | 84 |
|
---|
[7a0359b] | 85 | NO_TRACE static inline size_t frame_index_abs(zone_t *zone, frame_t *frame)
|
---|
[f761f1eb] | 86 | {
|
---|
[98000fb] | 87 | return (size_t) (frame - zone->frames) + zone->base;
|
---|
[f761f1eb] | 88 | }
|
---|
[71eef11] | 89 |
|
---|
[7a0359b] | 90 | NO_TRACE static inline bool frame_index_valid(zone_t *zone, size_t index)
|
---|
[a294ad0] | 91 | {
|
---|
[6c441cf8] | 92 | return (index < zone->count);
|
---|
[a294ad0] | 93 | }
|
---|
| 94 |
|
---|
[7a0359b] | 95 | NO_TRACE static inline size_t make_frame_index(zone_t *zone, frame_t *frame)
|
---|
[a294ad0] | 96 | {
|
---|
[71eef11] | 97 | return (frame - zone->frames);
|
---|
[a294ad0] | 98 | }
|
---|
| 99 |
|
---|
[deaf8d5] | 100 | /** Initialize frame structure.
|
---|
[84dd253] | 101 | *
|
---|
[5f0f29ce] | 102 | * @param frame Frame structure to be initialized.
|
---|
| 103 | *
|
---|
[f761f1eb] | 104 | */
|
---|
[7a0359b] | 105 | NO_TRACE static void frame_initialize(frame_t *frame)
|
---|
[f761f1eb] | 106 | {
|
---|
[085d973] | 107 | frame->refcount = 1;
|
---|
| 108 | frame->buddy_order = 0;
|
---|
[f761f1eb] | 109 | }
|
---|
| 110 |
|
---|
[5f0f29ce] | 111 | /*******************/
|
---|
| 112 | /* Zones functions */
|
---|
| 113 | /*******************/
|
---|
[085d973] | 114 |
|
---|
[deaf8d5] | 115 | /** Insert-sort zone into zones list.
|
---|
[bb68433] | 116 | *
|
---|
[5f0f29ce] | 117 | * Assume interrupts are disabled and zones lock is
|
---|
| 118 | * locked.
|
---|
| 119 | *
|
---|
| 120 | * @param base Base frame of the newly inserted zone.
|
---|
| 121 | * @param count Number of frames of the newly inserted zone.
|
---|
| 122 | *
|
---|
| 123 | * @return Zone number on success, -1 on error.
|
---|
| 124 | *
|
---|
[84dd253] | 125 | */
|
---|
[e2650d3] | 126 | NO_TRACE static size_t zones_insert_zone(pfn_t base, size_t count,
|
---|
| 127 | zone_flags_t flags)
|
---|
[f761f1eb] | 128 | {
|
---|
[71eef11] | 129 | if (zones.count + 1 == ZONES_MAX) {
|
---|
| 130 | printf("Maximum zone count %u exceeded!\n", ZONES_MAX);
|
---|
[98000fb] | 131 | return (size_t) -1;
|
---|
[71eef11] | 132 | }
|
---|
| 133 |
|
---|
[98000fb] | 134 | size_t i;
|
---|
[b6b576c] | 135 | for (i = 0; i < zones.count; i++) {
|
---|
[5f0f29ce] | 136 | /* Check for overlap */
|
---|
[e2650d3] | 137 | if (overlaps(zones.info[i].base, zones.info[i].count,
|
---|
| 138 | base, count)) {
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | * If the overlaping zones are of the same type
|
---|
| 142 | * and the new zone is completely within the previous
|
---|
| 143 | * one, then quietly ignore the new zone.
|
---|
| 144 | *
|
---|
| 145 | */
|
---|
| 146 |
|
---|
| 147 | if ((zones.info[i].flags != flags) ||
|
---|
| 148 | (!iswithin(zones.info[i].base, zones.info[i].count,
|
---|
| 149 | base, count))) {
|
---|
[7e752b2] | 150 | printf("Zone (%p, %p) overlaps "
|
---|
| 151 | "with previous zone (%p %p)!\n",
|
---|
| 152 | (void *) PFN2ADDR(base), (void *) PFN2ADDR(count),
|
---|
| 153 | (void *) PFN2ADDR(zones.info[i].base),
|
---|
| 154 | (void *) PFN2ADDR(zones.info[i].count));
|
---|
[e2650d3] | 155 | }
|
---|
| 156 |
|
---|
[98000fb] | 157 | return (size_t) -1;
|
---|
[085d973] | 158 | }
|
---|
[5f0f29ce] | 159 | if (base < zones.info[i].base)
|
---|
[bb68433] | 160 | break;
|
---|
[085d973] | 161 | }
|
---|
[71eef11] | 162 |
|
---|
[bb68433] | 163 | /* Move other zones up */
|
---|
[98000fb] | 164 | size_t j;
|
---|
[e49e234] | 165 | for (j = zones.count; j > i; j--) {
|
---|
| 166 | zones.info[j] = zones.info[j - 1];
|
---|
[af96dd57] | 167 | if (zones.info[j].buddy_system != NULL)
|
---|
| 168 | zones.info[j].buddy_system->data =
|
---|
| 169 | (void *) &zones.info[j];
|
---|
[e49e234] | 170 | }
|
---|
[71eef11] | 171 |
|
---|
[bb68433] | 172 | zones.count++;
|
---|
[71eef11] | 173 |
|
---|
[bb68433] | 174 | return i;
|
---|
[f761f1eb] | 175 | }
|
---|
[fcacfb7] | 176 |
|
---|
[5f0f29ce] | 177 | /** Get total available frames.
|
---|
| 178 | *
|
---|
| 179 | * Assume interrupts are disabled and zones lock is
|
---|
| 180 | * locked.
|
---|
[71eef11] | 181 | *
|
---|
[5f0f29ce] | 182 | * @return Total number of available frames.
|
---|
[71eef11] | 183 | *
|
---|
[eef75f6] | 184 | */
|
---|
[8d308b9] | 185 | NO_TRACE static size_t frame_total_free_get_internal(void)
|
---|
[085d973] | 186 | {
|
---|
[98000fb] | 187 | size_t total = 0;
|
---|
| 188 | size_t i;
|
---|
[8d308b9] | 189 |
|
---|
[5f0f29ce] | 190 | for (i = 0; i < zones.count; i++)
|
---|
| 191 | total += zones.info[i].free_count;
|
---|
[328f2934] | 192 |
|
---|
[5f0f29ce] | 193 | return total;
|
---|
| 194 | }
|
---|
[8d308b9] | 195 |
|
---|
| 196 | NO_TRACE size_t frame_total_free_get(void)
|
---|
| 197 | {
|
---|
| 198 | size_t total;
|
---|
| 199 |
|
---|
| 200 | irq_spinlock_lock(&zones.lock, true);
|
---|
| 201 | total = frame_total_free_get_internal();
|
---|
| 202 | irq_spinlock_unlock(&zones.lock, true);
|
---|
| 203 |
|
---|
| 204 | return total;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[4457455] | 207 |
|
---|
[e49e234] | 208 | /** Find a zone with a given frames.
|
---|
[5f0f29ce] | 209 | *
|
---|
| 210 | * Assume interrupts are disabled and zones lock is
|
---|
| 211 | * locked.
|
---|
| 212 | *
|
---|
| 213 | * @param frame Frame number contained in zone.
|
---|
[e49e234] | 214 | * @param count Number of frames to look for.
|
---|
[5f0f29ce] | 215 | * @param hint Used as zone hint.
|
---|
| 216 | *
|
---|
| 217 | * @return Zone index or -1 if not found.
|
---|
| 218 | *
|
---|
| 219 | */
|
---|
[97bdb4a] | 220 | NO_TRACE size_t find_zone(pfn_t frame, size_t count, size_t hint)
|
---|
[5f0f29ce] | 221 | {
|
---|
[6c441cf8] | 222 | if (hint >= zones.count)
|
---|
[085d973] | 223 | hint = 0;
|
---|
[328f2934] | 224 |
|
---|
[98000fb] | 225 | size_t i = hint;
|
---|
[085d973] | 226 | do {
|
---|
[5f0f29ce] | 227 | if ((zones.info[i].base <= frame)
|
---|
[e49e234] | 228 | && (zones.info[i].base + zones.info[i].count >= frame + count))
|
---|
[5f0f29ce] | 229 | return i;
|
---|
| 230 |
|
---|
[085d973] | 231 | i++;
|
---|
| 232 | if (i >= zones.count)
|
---|
| 233 | i = 0;
|
---|
[da1bafb] | 234 |
|
---|
[5f7a0ef] | 235 | } while (i != hint);
|
---|
[5f0f29ce] | 236 |
|
---|
[98000fb] | 237 | return (size_t) -1;
|
---|
[085d973] | 238 | }
|
---|
[328f2934] | 239 |
|
---|
[bb68433] | 240 | /** @return True if zone can allocate specified order */
|
---|
[7a0359b] | 241 | NO_TRACE static bool zone_can_alloc(zone_t *zone, uint8_t order)
|
---|
[bb68433] | 242 | {
|
---|
[e6c4b94] | 243 | return ((zone->flags & ZONE_AVAILABLE) &&
|
---|
| 244 | buddy_system_can_alloc(zone->buddy_system, order));
|
---|
[bb68433] | 245 | }
|
---|
| 246 |
|
---|
[5f0f29ce] | 247 | /** Find a zone that can allocate order frames.
|
---|
| 248 | *
|
---|
| 249 | * Assume interrupts are disabled and zones lock is
|
---|
| 250 | * locked.
|
---|
[fcacfb7] | 251 | *
|
---|
[5f0f29ce] | 252 | * @param order Size (2^order) of free space we are trying to find.
|
---|
| 253 | * @param flags Required flags of the target zone.
|
---|
| 254 | * @param hind Preferred zone.
|
---|
[fcacfb7] | 255 | *
|
---|
| 256 | */
|
---|
[7a0359b] | 257 | NO_TRACE static size_t find_free_zone(uint8_t order, zone_flags_t flags,
|
---|
| 258 | size_t hint)
|
---|
[fcacfb7] | 259 | {
|
---|
[085d973] | 260 | if (hint >= zones.count)
|
---|
| 261 | hint = 0;
|
---|
[5f0f29ce] | 262 |
|
---|
[98000fb] | 263 | size_t i = hint;
|
---|
[085d973] | 264 | do {
|
---|
[5f7a0ef] | 265 | /*
|
---|
| 266 | * Check whether the zone meets the search criteria.
|
---|
| 267 | */
|
---|
[e6c4b94] | 268 | if (ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) {
|
---|
[5f7a0ef] | 269 | /*
|
---|
| 270 | * Check if the zone has 2^order frames area available.
|
---|
| 271 | */
|
---|
[5f0f29ce] | 272 | if (zone_can_alloc(&zones.info[i], order))
|
---|
| 273 | return i;
|
---|
[328f2934] | 274 | }
|
---|
[5f0f29ce] | 275 |
|
---|
| 276 | i++;
|
---|
| 277 | if (i >= zones.count)
|
---|
[085d973] | 278 | i = 0;
|
---|
[da1bafb] | 279 |
|
---|
[5f7a0ef] | 280 | } while (i != hint);
|
---|
[5f0f29ce] | 281 |
|
---|
[98000fb] | 282 | return (size_t) -1;
|
---|
[fcacfb7] | 283 | }
|
---|
| 284 |
|
---|
[b43eaba0] | 285 | /**************************/
|
---|
[085d973] | 286 | /* Buddy system functions */
|
---|
[b43eaba0] | 287 | /**************************/
|
---|
[085d973] | 288 |
|
---|
[deaf8d5] | 289 | /** Buddy system find_block implementation.
|
---|
[fcacfb7] | 290 | *
|
---|
[085d973] | 291 | * Find block that is parent of current list.
|
---|
| 292 | * That means go to lower addresses, until such block is found
|
---|
[fcacfb7] | 293 | *
|
---|
[5f0f29ce] | 294 | * @param order Order of parent must be different then this
|
---|
| 295 | * parameter!!
|
---|
| 296 | *
|
---|
[fcacfb7] | 297 | */
|
---|
[7a0359b] | 298 | NO_TRACE static link_t *zone_buddy_find_block(buddy_system_t *buddy,
|
---|
| 299 | link_t *child, uint8_t order)
|
---|
[fcacfb7] | 300 | {
|
---|
[5f0f29ce] | 301 | frame_t *frame = list_get_instance(child, frame_t, buddy_link);
|
---|
| 302 | zone_t *zone = (zone_t *) buddy->data;
|
---|
[fcacfb7] | 303 |
|
---|
[98000fb] | 304 | size_t index = frame_index(zone, frame);
|
---|
[085d973] | 305 | do {
|
---|
[5f0f29ce] | 306 | if (zone->frames[index].buddy_order != order)
|
---|
[085d973] | 307 | return &zone->frames[index].buddy_link;
|
---|
[5f0f29ce] | 308 | } while (index-- > 0);
|
---|
| 309 |
|
---|
[085d973] | 310 | return NULL;
|
---|
[fcacfb7] | 311 | }
|
---|
[6e8b3c8] | 312 |
|
---|
[deaf8d5] | 313 | /** Buddy system find_buddy implementation.
|
---|
[594a468] | 314 | *
|
---|
[5f0f29ce] | 315 | * @param buddy Buddy system.
|
---|
| 316 | * @param block Block for which buddy should be found.
|
---|
| 317 | *
|
---|
| 318 | * @return Buddy for given block if found.
|
---|
[6e8b3c8] | 319 | *
|
---|
| 320 | */
|
---|
[7a0359b] | 321 | NO_TRACE static link_t *zone_buddy_find_buddy(buddy_system_t *buddy,
|
---|
| 322 | link_t *block)
|
---|
[085d973] | 323 | {
|
---|
[5f0f29ce] | 324 | frame_t *frame = list_get_instance(block, frame_t, buddy_link);
|
---|
| 325 | zone_t *zone = (zone_t *) buddy->data;
|
---|
[4638401] | 326 | ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame),
|
---|
| 327 | frame->buddy_order));
|
---|
[b87f418] | 328 |
|
---|
[5f0f29ce] | 329 | bool is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
|
---|
| 330 |
|
---|
[98000fb] | 331 | size_t index;
|
---|
[328f2934] | 332 | if (is_left) {
|
---|
[deaf8d5] | 333 | index = (frame_index(zone, frame)) +
|
---|
| 334 | (1 << frame->buddy_order);
|
---|
[da1bafb] | 335 | } else { /* is_right */
|
---|
[deaf8d5] | 336 | index = (frame_index(zone, frame)) -
|
---|
| 337 | (1 << frame->buddy_order);
|
---|
[328f2934] | 338 | }
|
---|
| 339 |
|
---|
[085d973] | 340 | if (frame_index_valid(zone, index)) {
|
---|
[5f0f29ce] | 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 | }
|
---|
[5f0f29ce] | 346 |
|
---|
| 347 | return NULL;
|
---|
[6e8b3c8] | 348 | }
|
---|
| 349 |
|
---|
[deaf8d5] | 350 | /** Buddy system bisect implementation.
|
---|
[6e8b3c8] | 351 | *
|
---|
[5f0f29ce] | 352 | * @param buddy Buddy system.
|
---|
| 353 | * @param block Block to bisect.
|
---|
| 354 | *
|
---|
| 355 | * @return Right block.
|
---|
[30187eb] | 356 | *
|
---|
[6e8b3c8] | 357 | */
|
---|
[7a0359b] | 358 | NO_TRACE static link_t *zone_buddy_bisect(buddy_system_t *buddy, link_t *block)
|
---|
[deaf8d5] | 359 | {
|
---|
[5f0f29ce] | 360 | frame_t *frame_l = list_get_instance(block, frame_t, buddy_link);
|
---|
| 361 | frame_t *frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
|
---|
[b87f418] | 362 |
|
---|
[30187eb] | 363 | return &frame_r->buddy_link;
|
---|
[6e8b3c8] | 364 | }
|
---|
| 365 |
|
---|
[deaf8d5] | 366 | /** Buddy system coalesce implementation.
|
---|
[6e8b3c8] | 367 | *
|
---|
[5f0f29ce] | 368 | * @param buddy Buddy system.
|
---|
| 369 | * @param block_1 First block.
|
---|
| 370 | * @param block_2 First block's buddy.
|
---|
| 371 | *
|
---|
| 372 | * @return Coalesced block (actually block that represents lower
|
---|
| 373 | * address).
|
---|
[30187eb] | 374 | *
|
---|
[6e8b3c8] | 375 | */
|
---|
[7a0359b] | 376 | NO_TRACE static link_t *zone_buddy_coalesce(buddy_system_t *buddy,
|
---|
| 377 | link_t *block_1, link_t *block_2)
|
---|
[bb68433] | 378 | {
|
---|
[5f0f29ce] | 379 | frame_t *frame1 = list_get_instance(block_1, frame_t, buddy_link);
|
---|
| 380 | frame_t *frame2 = list_get_instance(block_2, frame_t, buddy_link);
|
---|
[b87f418] | 381 |
|
---|
[5f0f29ce] | 382 | return ((frame1 < frame2) ? block_1 : block_2);
|
---|
[6e8b3c8] | 383 | }
|
---|
| 384 |
|
---|
[deaf8d5] | 385 | /** Buddy system set_order implementation.
|
---|
[594a468] | 386 | *
|
---|
[5f0f29ce] | 387 | * @param buddy Buddy system.
|
---|
| 388 | * @param block Buddy system block.
|
---|
| 389 | * @param order Order to set.
|
---|
| 390 | *
|
---|
[6e8b3c8] | 391 | */
|
---|
[7a0359b] | 392 | NO_TRACE static void zone_buddy_set_order(buddy_system_t *buddy, link_t *block,
|
---|
[deaf8d5] | 393 | uint8_t order)
|
---|
| 394 | {
|
---|
[5f0f29ce] | 395 | list_get_instance(block, frame_t, buddy_link)->buddy_order = order;
|
---|
[6e8b3c8] | 396 | }
|
---|
| 397 |
|
---|
[deaf8d5] | 398 | /** Buddy system get_order implementation.
|
---|
[594a468] | 399 | *
|
---|
[5f0f29ce] | 400 | * @param buddy Buddy system.
|
---|
| 401 | * @param block Buddy system block.
|
---|
| 402 | *
|
---|
| 403 | * @return Order of block.
|
---|
[6e8b3c8] | 404 | *
|
---|
| 405 | */
|
---|
[7a0359b] | 406 | NO_TRACE static uint8_t zone_buddy_get_order(buddy_system_t *buddy,
|
---|
| 407 | link_t *block)
|
---|
[deaf8d5] | 408 | {
|
---|
[5f0f29ce] | 409 | return list_get_instance(block, frame_t, buddy_link)->buddy_order;
|
---|
[6e8b3c8] | 410 | }
|
---|
[328f2934] | 411 |
|
---|
[deaf8d5] | 412 | /** Buddy system mark_busy implementation.
|
---|
[328f2934] | 413 | *
|
---|
[5f0f29ce] | 414 | * @param buddy Buddy system.
|
---|
| 415 | * @param block Buddy system block.
|
---|
| 416 | *
|
---|
[328f2934] | 417 | */
|
---|
[7a0359b] | 418 | NO_TRACE static void zone_buddy_mark_busy(buddy_system_t *buddy, link_t *block)
|
---|
[deaf8d5] | 419 | {
|
---|
[5f0f29ce] | 420 | list_get_instance(block, frame_t, buddy_link)->refcount = 1;
|
---|
[328f2934] | 421 | }
|
---|
[dfd9186] | 422 |
|
---|
[deaf8d5] | 423 | /** Buddy system mark_available implementation.
|
---|
[085d973] | 424 | *
|
---|
[5f0f29ce] | 425 | * @param buddy Buddy system.
|
---|
| 426 | * @param block Buddy system block.
|
---|
[7a0359b] | 427 | *
|
---|
[085d973] | 428 | */
|
---|
[7a0359b] | 429 | NO_TRACE static void zone_buddy_mark_available(buddy_system_t *buddy,
|
---|
| 430 | link_t *block)
|
---|
[deaf8d5] | 431 | {
|
---|
[5f0f29ce] | 432 | list_get_instance(block, frame_t, buddy_link)->refcount = 0;
|
---|
[085d973] | 433 | }
|
---|
| 434 |
|
---|
[0f3fc9b] | 435 | static buddy_system_operations_t zone_buddy_system_operations = {
|
---|
[085d973] | 436 | .find_buddy = zone_buddy_find_buddy,
|
---|
| 437 | .bisect = zone_buddy_bisect,
|
---|
| 438 | .coalesce = zone_buddy_coalesce,
|
---|
| 439 | .set_order = zone_buddy_set_order,
|
---|
| 440 | .get_order = zone_buddy_get_order,
|
---|
| 441 | .mark_busy = zone_buddy_mark_busy,
|
---|
| 442 | .mark_available = zone_buddy_mark_available,
|
---|
[2ec725f] | 443 | .find_block = zone_buddy_find_block
|
---|
[085d973] | 444 | };
|
---|
| 445 |
|
---|
[b43eaba0] | 446 | /******************/
|
---|
[085d973] | 447 | /* Zone functions */
|
---|
[b43eaba0] | 448 | /******************/
|
---|
[085d973] | 449 |
|
---|
[deaf8d5] | 450 | /** Allocate frame in particular zone.
|
---|
[085d973] | 451 | *
|
---|
[5f0f29ce] | 452 | * Assume zone is locked and is available for allocation.
|
---|
[9a68b34d] | 453 | * Panics if allocation is impossible.
|
---|
| 454 | *
|
---|
[5f0f29ce] | 455 | * @param zone Zone to allocate from.
|
---|
| 456 | * @param order Allocate exactly 2^order frames.
|
---|
[085d973] | 457 | *
|
---|
[5f0f29ce] | 458 | * @return Frame index in zone.
|
---|
[9a68b34d] | 459 | *
|
---|
[085d973] | 460 | */
|
---|
[7a0359b] | 461 | NO_TRACE static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
|
---|
[085d973] | 462 | {
|
---|
[e6c4b94] | 463 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 464 |
|
---|
[085d973] | 465 | /* Allocate frames from zone buddy system */
|
---|
[5f0f29ce] | 466 | link_t *link = buddy_system_alloc(zone->buddy_system, order);
|
---|
[085d973] | 467 |
|
---|
[5f0f29ce] | 468 | ASSERT(link);
|
---|
[085d973] | 469 |
|
---|
| 470 | /* Update zone information. */
|
---|
| 471 | zone->free_count -= (1 << order);
|
---|
| 472 | zone->busy_count += (1 << order);
|
---|
[5f0f29ce] | 473 |
|
---|
[085d973] | 474 | /* Frame will be actually a first frame of the block. */
|
---|
[5f0f29ce] | 475 | frame_t *frame = list_get_instance(link, frame_t, buddy_link);
|
---|
[085d973] | 476 |
|
---|
[5f0f29ce] | 477 | /* Get frame address */
|
---|
| 478 | return make_frame_index(zone, frame);
|
---|
[085d973] | 479 | }
|
---|
| 480 |
|
---|
[deaf8d5] | 481 | /** Free frame from zone.
|
---|
[085d973] | 482 | *
|
---|
[5f0f29ce] | 483 | * Assume zone is locked and is available for deallocation.
|
---|
| 484 | *
|
---|
| 485 | * @param zone Pointer to zone from which the frame is to be freed.
|
---|
| 486 | * @param frame_idx Frame index relative to zone.
|
---|
[eb3d379] | 487 | *
|
---|
[89bcb520] | 488 | * @return Number of freed frames.
|
---|
| 489 | *
|
---|
[085d973] | 490 | */
|
---|
[89bcb520] | 491 | NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t frame_idx)
|
---|
[085d973] | 492 | {
|
---|
[e6c4b94] | 493 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 494 |
|
---|
| 495 | frame_t *frame = &zone->frames[frame_idx];
|
---|
[164c653] | 496 | size_t size = 0;
|
---|
[085d973] | 497 |
|
---|
| 498 | ASSERT(frame->refcount);
|
---|
[5f0f29ce] | 499 |
|
---|
[085d973] | 500 | if (!--frame->refcount) {
|
---|
[164c653] | 501 | size = 1 << frame->buddy_order;
|
---|
| 502 | buddy_system_free(zone->buddy_system, &frame->buddy_link);
|
---|
[d3dfa42] | 503 | /* Update zone information. */
|
---|
[89bcb520] | 504 | zone->free_count += size;
|
---|
| 505 | zone->busy_count -= size;
|
---|
[085d973] | 506 | }
|
---|
[89bcb520] | 507 |
|
---|
| 508 | return size;
|
---|
[085d973] | 509 | }
|
---|
| 510 |
|
---|
[deaf8d5] | 511 | /** Return frame from zone. */
|
---|
[7a0359b] | 512 | NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t frame_idx)
|
---|
[085d973] | 513 | {
|
---|
| 514 | ASSERT(frame_idx < zone->count);
|
---|
| 515 | return &zone->frames[frame_idx];
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[deaf8d5] | 518 | /** Mark frame in zone unavailable to allocation. */
|
---|
[7a0359b] | 519 | NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t frame_idx)
|
---|
[085d973] | 520 | {
|
---|
[e6c4b94] | 521 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 522 |
|
---|
| 523 | frame_t *frame = zone_get_frame(zone, frame_idx);
|
---|
[bb68433] | 524 | if (frame->refcount)
|
---|
| 525 | return;
|
---|
[5f0f29ce] | 526 |
|
---|
[9482bf0b] | 527 | link_t *link __attribute__ ((unused));
|
---|
| 528 |
|
---|
| 529 | link = buddy_system_alloc_block(zone->buddy_system,
|
---|
[4638401] | 530 | &frame->buddy_link);
|
---|
[5f0f29ce] | 531 |
|
---|
[085d973] | 532 | ASSERT(link);
|
---|
| 533 | zone->free_count--;
|
---|
[d060900] | 534 | reserve_force_alloc(1);
|
---|
[085d973] | 535 | }
|
---|
| 536 |
|
---|
[5f0f29ce] | 537 | /** Merge two zones.
|
---|
[bb68433] | 538 | *
|
---|
[5f0f29ce] | 539 | * Expect buddy to point to space at least zone_conf_size large.
|
---|
| 540 | * Assume z1 & z2 are locked and compatible and zones lock is
|
---|
| 541 | * locked.
|
---|
[bb68433] | 542 | *
|
---|
[5f0f29ce] | 543 | * @param z1 First zone to merge.
|
---|
| 544 | * @param z2 Second zone to merge.
|
---|
| 545 | * @param old_z1 Original date of the first zone.
|
---|
| 546 | * @param buddy Merged zone buddy.
|
---|
[eb3d379] | 547 | *
|
---|
[bb68433] | 548 | */
|
---|
[7a0359b] | 549 | NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
|
---|
| 550 | buddy_system_t *buddy)
|
---|
[bb68433] | 551 | {
|
---|
[e6c4b94] | 552 | ASSERT(zones.info[z1].flags & ZONE_AVAILABLE);
|
---|
| 553 | ASSERT(zones.info[z2].flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 554 | ASSERT(zones.info[z1].flags == zones.info[z2].flags);
|
---|
| 555 | ASSERT(zones.info[z1].base < zones.info[z2].base);
|
---|
| 556 | ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
|
---|
| 557 | zones.info[z2].base, zones.info[z2].count));
|
---|
| 558 |
|
---|
| 559 | /* Difference between zone bases */
|
---|
| 560 | pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
|
---|
| 561 |
|
---|
| 562 | zones.info[z1].count = base_diff + zones.info[z2].count;
|
---|
| 563 | zones.info[z1].free_count += zones.info[z2].free_count;
|
---|
| 564 | zones.info[z1].busy_count += zones.info[z2].busy_count;
|
---|
| 565 | zones.info[z1].buddy_system = buddy;
|
---|
| 566 |
|
---|
| 567 | uint8_t order = fnzb(zones.info[z1].count);
|
---|
| 568 | buddy_system_create(zones.info[z1].buddy_system, order,
|
---|
| 569 | &zone_buddy_system_operations, (void *) &zones.info[z1]);
|
---|
| 570 |
|
---|
| 571 | zones.info[z1].frames =
|
---|
| 572 | (frame_t *) ((uint8_t *) zones.info[z1].buddy_system
|
---|
| 573 | + buddy_conf_size(order));
|
---|
| 574 |
|
---|
| 575 | /* This marks all frames busy */
|
---|
[98000fb] | 576 | size_t i;
|
---|
[5f0f29ce] | 577 | for (i = 0; i < zones.info[z1].count; i++)
|
---|
| 578 | frame_initialize(&zones.info[z1].frames[i]);
|
---|
[bb68433] | 579 |
|
---|
| 580 | /* Copy frames from both zones to preserve full frame orders,
|
---|
[5f0f29ce] | 581 | * parents etc. Set all free frames with refcount = 0 to 1, because
|
---|
| 582 | * we add all free frames to buddy allocator later again, clearing
|
---|
| 583 | * order to 0. Don't set busy frames with refcount = 0, as they
|
---|
[ad64a2d] | 584 | * will not be reallocated during merge and it would make later
|
---|
| 585 | * problems with allocation/free.
|
---|
[bb68433] | 586 | */
|
---|
[5f0f29ce] | 587 | for (i = 0; i < old_z1->count; i++)
|
---|
| 588 | zones.info[z1].frames[i] = old_z1->frames[i];
|
---|
| 589 |
|
---|
| 590 | for (i = 0; i < zones.info[z2].count; i++)
|
---|
| 591 | zones.info[z1].frames[base_diff + i]
|
---|
| 592 | = zones.info[z2].frames[i];
|
---|
| 593 |
|
---|
[ad64a2d] | 594 | i = 0;
|
---|
[5f0f29ce] | 595 | while (i < zones.info[z1].count) {
|
---|
| 596 | if (zones.info[z1].frames[i].refcount) {
|
---|
| 597 | /* Skip busy frames */
|
---|
| 598 | i += 1 << zones.info[z1].frames[i].buddy_order;
|
---|
| 599 | } else {
|
---|
| 600 | /* Free frames, set refcount = 1
|
---|
| 601 | * (all free frames have refcount == 0, we need not
|
---|
| 602 | * to check the order)
|
---|
| 603 | */
|
---|
| 604 | zones.info[z1].frames[i].refcount = 1;
|
---|
| 605 | zones.info[z1].frames[i].buddy_order = 0;
|
---|
[ad64a2d] | 606 | i++;
|
---|
[bb68433] | 607 | }
|
---|
| 608 | }
|
---|
[5f0f29ce] | 609 |
|
---|
| 610 | /* Add free blocks from the original zone z1 */
|
---|
| 611 | while (zone_can_alloc(old_z1, 0)) {
|
---|
| 612 | /* Allocate from the original zone */
|
---|
| 613 | pfn_t frame_idx = zone_frame_alloc(old_z1, 0);
|
---|
| 614 |
|
---|
| 615 | /* Free the frame from the merged zone */
|
---|
| 616 | frame_t *frame = &zones.info[z1].frames[frame_idx];
|
---|
[bb68433] | 617 | frame->refcount = 0;
|
---|
[5f0f29ce] | 618 | buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
|
---|
[bb68433] | 619 | }
|
---|
[5f0f29ce] | 620 |
|
---|
| 621 | /* Add free blocks from the original zone z2 */
|
---|
| 622 | while (zone_can_alloc(&zones.info[z2], 0)) {
|
---|
| 623 | /* Allocate from the original zone */
|
---|
| 624 | pfn_t frame_idx = zone_frame_alloc(&zones.info[z2], 0);
|
---|
| 625 |
|
---|
| 626 | /* Free the frame from the merged zone */
|
---|
| 627 | frame_t *frame = &zones.info[z1].frames[base_diff + frame_idx];
|
---|
[bb68433] | 628 | frame->refcount = 0;
|
---|
[5f0f29ce] | 629 | buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
|
---|
[bb68433] | 630 | }
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[deaf8d5] | 633 | /** Return old configuration frames into the zone.
|
---|
[bb68433] | 634 | *
|
---|
[5f0f29ce] | 635 | * We have two cases:
|
---|
| 636 | * - The configuration data is outside the zone
|
---|
| 637 | * -> do nothing (perhaps call frame_free?)
|
---|
| 638 | * - The configuration data was created by zone_create
|
---|
| 639 | * or updated by reduce_region -> free every frame
|
---|
| 640 | *
|
---|
| 641 | * @param znum The actual zone where freeing should occur.
|
---|
| 642 | * @param pfn Old zone configuration frame.
|
---|
| 643 | * @param count Old zone frame count.
|
---|
[874878a] | 644 | *
|
---|
[bb68433] | 645 | */
|
---|
[7a0359b] | 646 | NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
|
---|
[bb68433] | 647 | {
|
---|
[e6c4b94] | 648 | ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 649 |
|
---|
[98000fb] | 650 | size_t cframes = SIZE2FRAMES(zone_conf_size(count));
|
---|
[bb68433] | 651 |
|
---|
[5f0f29ce] | 652 | if ((pfn < zones.info[znum].base)
|
---|
| 653 | || (pfn >= zones.info[znum].base + zones.info[znum].count))
|
---|
[bb68433] | 654 | return;
|
---|
[5f0f29ce] | 655 |
|
---|
[9482bf0b] | 656 | frame_t *frame __attribute__ ((unused));
|
---|
| 657 |
|
---|
| 658 | frame = &zones.info[znum].frames[pfn - zones.info[znum].base];
|
---|
[874878a] | 659 | ASSERT(!frame->buddy_order);
|
---|
[5f0f29ce] | 660 |
|
---|
[98000fb] | 661 | size_t i;
|
---|
[2936eef] | 662 | for (i = 0; i < cframes; i++) {
|
---|
[5f0f29ce] | 663 | zones.info[znum].busy_count++;
|
---|
[89bcb520] | 664 | (void) zone_frame_free(&zones.info[znum],
|
---|
[5f0f29ce] | 665 | pfn - zones.info[znum].base + i);
|
---|
[bb68433] | 666 | }
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[deaf8d5] | 669 | /** Reduce allocated block to count of order 0 frames.
|
---|
[874878a] | 670 | *
|
---|
[5f0f29ce] | 671 | * The allocated block needs 2^order frames. Reduce all frames
|
---|
| 672 | * in the block to order 0 and free the unneeded frames. This means that
|
---|
| 673 | * when freeing the previously allocated block starting with frame_idx,
|
---|
[eb3d379] | 674 | * you have to free every frame.
|
---|
[874878a] | 675 | *
|
---|
[5f0f29ce] | 676 | * @param znum Zone.
|
---|
| 677 | * @param frame_idx Index the first frame of the block.
|
---|
| 678 | * @param count Allocated frames in block.
|
---|
| 679 | *
|
---|
[874878a] | 680 | */
|
---|
[7a0359b] | 681 | NO_TRACE static void zone_reduce_region(size_t znum, pfn_t frame_idx,
|
---|
| 682 | size_t count)
|
---|
[874878a] | 683 | {
|
---|
[e6c4b94] | 684 | ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 685 | ASSERT(frame_idx + count < zones.info[znum].count);
|
---|
[874878a] | 686 |
|
---|
[5f0f29ce] | 687 | uint8_t order = zones.info[znum].frames[frame_idx].buddy_order;
|
---|
[98000fb] | 688 | ASSERT((size_t) (1 << order) >= count);
|
---|
[5f0f29ce] | 689 |
|
---|
[874878a] | 690 | /* Reduce all blocks to order 0 */
|
---|
[98000fb] | 691 | size_t i;
|
---|
| 692 | for (i = 0; i < (size_t) (1 << order); i++) {
|
---|
[5f0f29ce] | 693 | frame_t *frame = &zones.info[znum].frames[i + frame_idx];
|
---|
[874878a] | 694 | frame->buddy_order = 0;
|
---|
[4638401] | 695 | if (!frame->refcount)
|
---|
[874878a] | 696 | frame->refcount = 1;
|
---|
| 697 | ASSERT(frame->refcount == 1);
|
---|
| 698 | }
|
---|
[5f0f29ce] | 699 |
|
---|
[874878a] | 700 | /* Free unneeded frames */
|
---|
[98000fb] | 701 | for (i = count; i < (size_t) (1 << order); i++)
|
---|
[89bcb520] | 702 | (void) zone_frame_free(&zones.info[znum], i + frame_idx);
|
---|
[874878a] | 703 | }
|
---|
| 704 |
|
---|
[deaf8d5] | 705 | /** Merge zones z1 and z2.
|
---|
[bb68433] | 706 | *
|
---|
[5f0f29ce] | 707 | * The merged zones must be 2 zones with no zone existing in between
|
---|
| 708 | * (which means that z2 = z1 + 1). Both zones must be available zones
|
---|
| 709 | * with the same flags.
|
---|
| 710 | *
|
---|
| 711 | * When you create a new zone, the frame allocator configuration does
|
---|
| 712 | * not to be 2^order size. Once the allocator is running it is no longer
|
---|
| 713 | * possible, merged configuration data occupies more space :-/
|
---|
| 714 | *
|
---|
[bb68433] | 715 | */
|
---|
[98000fb] | 716 | bool zone_merge(size_t z1, size_t z2)
|
---|
[bb68433] | 717 | {
|
---|
[da1bafb] | 718 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 719 |
|
---|
| 720 | bool ret = true;
|
---|
| 721 |
|
---|
| 722 | /* We can join only 2 zones with none existing inbetween,
|
---|
| 723 | * the zones have to be available and with the same
|
---|
| 724 | * set of flags
|
---|
| 725 | */
|
---|
[e6c4b94] | 726 | if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
|
---|
| 727 | (zones.info[z1].flags != zones.info[z2].flags)) {
|
---|
[5f0f29ce] | 728 | ret = false;
|
---|
[bb68433] | 729 | goto errout;
|
---|
[5f0f29ce] | 730 | }
|
---|
| 731 |
|
---|
| 732 | pfn_t cframes = SIZE2FRAMES(zone_conf_size(
|
---|
| 733 | zones.info[z2].base - zones.info[z1].base
|
---|
| 734 | + zones.info[z2].count));
|
---|
| 735 |
|
---|
| 736 | uint8_t order;
|
---|
[279952c] | 737 | if (cframes == 1)
|
---|
| 738 | order = 0;
|
---|
[bb68433] | 739 | else
|
---|
[5f0f29ce] | 740 | order = fnzb(cframes - 1) + 1;
|
---|
| 741 |
|
---|
| 742 | /* Allocate merged zone data inside one of the zones */
|
---|
| 743 | pfn_t pfn;
|
---|
| 744 | if (zone_can_alloc(&zones.info[z1], order)) {
|
---|
| 745 | pfn = zones.info[z1].base + zone_frame_alloc(&zones.info[z1], order);
|
---|
| 746 | } else if (zone_can_alloc(&zones.info[z2], order)) {
|
---|
| 747 | pfn = zones.info[z2].base + zone_frame_alloc(&zones.info[z2], order);
|
---|
| 748 | } else {
|
---|
| 749 | ret = false;
|
---|
| 750 | goto errout;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | /* Preserve original data from z1 */
|
---|
| 754 | zone_t old_z1 = zones.info[z1];
|
---|
| 755 | old_z1.buddy_system->data = (void *) &old_z1;
|
---|
| 756 |
|
---|
| 757 | /* Do zone merging */
|
---|
| 758 | buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(pfn));
|
---|
| 759 | zone_merge_internal(z1, z2, &old_z1, buddy);
|
---|
| 760 |
|
---|
[874878a] | 761 | /* Free unneeded config frames */
|
---|
[5f0f29ce] | 762 | zone_reduce_region(z1, pfn - zones.info[z1].base, cframes);
|
---|
| 763 |
|
---|
[bb68433] | 764 | /* Subtract zone information from busy frames */
|
---|
[5f0f29ce] | 765 | zones.info[z1].busy_count -= cframes;
|
---|
| 766 |
|
---|
| 767 | /* Free old zone information */
|
---|
| 768 | return_config_frames(z1,
|
---|
| 769 | ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
|
---|
| 770 | return_config_frames(z1,
|
---|
| 771 | ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
|
---|
| 772 | zones.info[z2].count);
|
---|
| 773 |
|
---|
[e49e234] | 774 | /* Move zones down */
|
---|
[98000fb] | 775 | size_t i;
|
---|
[e49e234] | 776 | for (i = z2 + 1; i < zones.count; i++) {
|
---|
[b6b576c] | 777 | zones.info[i - 1] = zones.info[i];
|
---|
[af96dd57] | 778 | if (zones.info[i - 1].buddy_system != NULL)
|
---|
| 779 | zones.info[i - 1].buddy_system->data =
|
---|
| 780 | (void *) &zones.info[i - 1];
|
---|
[e49e234] | 781 | }
|
---|
| 782 |
|
---|
[bb68433] | 783 | zones.count--;
|
---|
[5f0f29ce] | 784 |
|
---|
[bb68433] | 785 | errout:
|
---|
[da1bafb] | 786 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 787 |
|
---|
| 788 | return ret;
|
---|
[bb68433] | 789 | }
|
---|
| 790 |
|
---|
[5f0f29ce] | 791 | /** Merge all mergeable zones into one big zone.
|
---|
| 792 | *
|
---|
| 793 | * It is reasonable to do this on systems where
|
---|
| 794 | * BIOS reports parts in chunks, so that we could
|
---|
| 795 | * have 1 zone (it's faster).
|
---|
[bb68433] | 796 | *
|
---|
| 797 | */
|
---|
| 798 | void zone_merge_all(void)
|
---|
| 799 | {
|
---|
[98000fb] | 800 | size_t i = 0;
|
---|
[5f0f29ce] | 801 | while (i < zones.count) {
|
---|
| 802 | if (!zone_merge(i, i + 1))
|
---|
| 803 | i++;
|
---|
[bb68433] | 804 | }
|
---|
| 805 | }
|
---|
| 806 |
|
---|
[deaf8d5] | 807 | /** Create new frame zone.
|
---|
[085d973] | 808 | *
|
---|
[5f0f29ce] | 809 | * @param zone Zone to construct.
|
---|
| 810 | * @param buddy Address of buddy system configuration information.
|
---|
| 811 | * @param start Physical address of the first frame within the zone.
|
---|
| 812 | * @param count Count of frames in zone.
|
---|
| 813 | * @param flags Zone flags.
|
---|
| 814 | *
|
---|
| 815 | * @return Initialized zone.
|
---|
[085d973] | 816 | *
|
---|
| 817 | */
|
---|
[7a0359b] | 818 | NO_TRACE static void zone_construct(zone_t *zone, buddy_system_t *buddy,
|
---|
| 819 | pfn_t start, size_t count, zone_flags_t flags)
|
---|
[085d973] | 820 | {
|
---|
[5f0f29ce] | 821 | zone->base = start;
|
---|
| 822 | zone->count = count;
|
---|
| 823 | zone->flags = flags;
|
---|
| 824 | zone->free_count = count;
|
---|
| 825 | zone->busy_count = 0;
|
---|
| 826 | zone->buddy_system = buddy;
|
---|
[085d973] | 827 |
|
---|
[e6c4b94] | 828 | if (flags & ZONE_AVAILABLE) {
|
---|
[5f0f29ce] | 829 | /*
|
---|
| 830 | * Compute order for buddy system and initialize
|
---|
| 831 | */
|
---|
| 832 | uint8_t order = fnzb(count);
|
---|
| 833 | buddy_system_create(zone->buddy_system, order,
|
---|
| 834 | &zone_buddy_system_operations, (void *) zone);
|
---|
| 835 |
|
---|
| 836 | /* Allocate frames _after_ the confframe */
|
---|
| 837 |
|
---|
| 838 | /* Check sizes */
|
---|
| 839 | zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
|
---|
| 840 | buddy_conf_size(order));
|
---|
| 841 |
|
---|
[98000fb] | 842 | size_t i;
|
---|
[5f0f29ce] | 843 | for (i = 0; i < count; i++)
|
---|
| 844 | frame_initialize(&zone->frames[i]);
|
---|
| 845 |
|
---|
| 846 | /* Stuffing frames */
|
---|
| 847 | for (i = 0; i < count; i++) {
|
---|
| 848 | zone->frames[i].refcount = 0;
|
---|
| 849 | buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
|
---|
| 850 | }
|
---|
| 851 | } else
|
---|
| 852 | zone->frames = NULL;
|
---|
[085d973] | 853 | }
|
---|
| 854 |
|
---|
[deaf8d5] | 855 | /** Compute configuration data size for zone.
|
---|
[eb3d379] | 856 | *
|
---|
[5f0f29ce] | 857 | * @param count Size of zone in frames.
|
---|
| 858 | *
|
---|
| 859 | * @return Size of zone configuration info (in bytes).
|
---|
| 860 | *
|
---|
[eb3d379] | 861 | */
|
---|
[bbfdf62] | 862 | size_t zone_conf_size(size_t count)
|
---|
[085d973] | 863 | {
|
---|
[5f0f29ce] | 864 | return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
|
---|
[085d973] | 865 | }
|
---|
| 866 |
|
---|
[8bdcffa] | 867 | /** Allocate external configuration frames from low memory. */
|
---|
| 868 | pfn_t zone_external_conf_alloc(size_t count)
|
---|
| 869 | {
|
---|
| 870 | size_t size = zone_conf_size(count);
|
---|
| 871 | size_t order = ispwr2(size) ? fnzb(size) : (fnzb(size) + 1);
|
---|
| 872 |
|
---|
[7852625] | 873 | return ADDR2PFN((uintptr_t) frame_alloc(order - FRAME_WIDTH,
|
---|
| 874 | FRAME_LOWMEM | FRAME_ATOMIC));
|
---|
[8bdcffa] | 875 | }
|
---|
| 876 |
|
---|
[deaf8d5] | 877 | /** Create and add zone to system.
|
---|
[085d973] | 878 | *
|
---|
[5f0f29ce] | 879 | * @param start First frame number (absolute).
|
---|
| 880 | * @param count Size of zone in frames.
|
---|
| 881 | * @param confframe Where configuration frames are supposed to be.
|
---|
| 882 | * Automatically checks, that we will not disturb the
|
---|
| 883 | * kernel and possibly init. If confframe is given
|
---|
| 884 | * _outside_ this zone, it is expected, that the area is
|
---|
| 885 | * already marked BUSY and big enough to contain
|
---|
| 886 | * zone_conf_size() amount of data. If the confframe is
|
---|
| 887 | * inside the area, the zone free frame information is
|
---|
| 888 | * modified not to include it.
|
---|
| 889 | *
|
---|
| 890 | * @return Zone number or -1 on error.
|
---|
| 891 | *
|
---|
[085d973] | 892 | */
|
---|
[da1bafb] | 893 | size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
|
---|
| 894 | zone_flags_t flags)
|
---|
[085d973] | 895 | {
|
---|
[da1bafb] | 896 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 897 |
|
---|
[e6c4b94] | 898 | if (flags & ZONE_AVAILABLE) { /* Create available zone */
|
---|
[5f0f29ce] | 899 | /* Theoretically we could have NULL here, practically make sure
|
---|
| 900 | * nobody tries to do that. If some platform requires, remove
|
---|
| 901 | * the assert
|
---|
| 902 | */
|
---|
[0b4a67a] | 903 | ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
|
---|
[40c8c17] | 904 |
|
---|
| 905 | /* Update the known end of physical memory. */
|
---|
| 906 | config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
|
---|
[5f0f29ce] | 907 |
|
---|
| 908 | /* If confframe is supposed to be inside our zone, then make sure
|
---|
| 909 | * it does not span kernel & init
|
---|
| 910 | */
|
---|
[98000fb] | 911 | size_t confcount = SIZE2FRAMES(zone_conf_size(count));
|
---|
[5f0f29ce] | 912 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
| 913 | for (; confframe < start + count; confframe++) {
|
---|
| 914 | uintptr_t addr = PFN2ADDR(confframe);
|
---|
| 915 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
| 916 | KA2PA(config.base), config.kernel_size))
|
---|
| 917 | continue;
|
---|
| 918 |
|
---|
[4638401] | 919 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
[5f0f29ce] | 920 | KA2PA(config.stack_base), config.stack_size))
|
---|
| 921 | continue;
|
---|
| 922 |
|
---|
| 923 | bool overlap = false;
|
---|
[98000fb] | 924 | size_t i;
|
---|
[5f0f29ce] | 925 | for (i = 0; i < init.cnt; i++)
|
---|
| 926 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
[32817cc] | 927 | init.tasks[i].paddr,
|
---|
[5f0f29ce] | 928 | init.tasks[i].size)) {
|
---|
| 929 | overlap = true;
|
---|
| 930 | break;
|
---|
| 931 | }
|
---|
| 932 | if (overlap)
|
---|
| 933 | continue;
|
---|
| 934 |
|
---|
| 935 | break;
|
---|
| 936 | }
|
---|
[b6b576c] | 937 |
|
---|
[5f0f29ce] | 938 | if (confframe >= start + count)
|
---|
| 939 | panic("Cannot find configuration data for zone.");
|
---|
[085d973] | 940 | }
|
---|
[5f0f29ce] | 941 |
|
---|
[e2650d3] | 942 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 943 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 944 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 945 | return (size_t) -1;
|
---|
[5f0f29ce] | 946 | }
|
---|
| 947 |
|
---|
| 948 | buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
|
---|
| 949 | zone_construct(&zones.info[znum], buddy, start, count, flags);
|
---|
| 950 |
|
---|
| 951 | /* If confdata in zone, mark as unavailable */
|
---|
| 952 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
[98000fb] | 953 | size_t i;
|
---|
[5f0f29ce] | 954 | for (i = confframe; i < confframe + confcount; i++)
|
---|
| 955 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 956 | i - zones.info[znum].base);
|
---|
[085d973] | 957 | }
|
---|
[5f0f29ce] | 958 |
|
---|
[da1bafb] | 959 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 960 |
|
---|
| 961 | return znum;
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | /* Non-available zone */
|
---|
[e2650d3] | 965 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 966 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 967 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 968 | return (size_t) -1;
|
---|
[5f0f29ce] | 969 | }
|
---|
| 970 | zone_construct(&zones.info[znum], NULL, start, count, flags);
|
---|
| 971 |
|
---|
[da1bafb] | 972 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[1bb3766] | 973 |
|
---|
[bb68433] | 974 | return znum;
|
---|
[085d973] | 975 | }
|
---|
| 976 |
|
---|
[5f0f29ce] | 977 | /*******************/
|
---|
[085d973] | 978 | /* Frame functions */
|
---|
[5f0f29ce] | 979 | /*******************/
|
---|
[085d973] | 980 |
|
---|
[deaf8d5] | 981 | /** Set parent of frame. */
|
---|
[98000fb] | 982 | void frame_set_parent(pfn_t pfn, void *data, size_t hint)
|
---|
[085d973] | 983 | {
|
---|
[da1bafb] | 984 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 985 |
|
---|
[98000fb] | 986 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[5f0f29ce] | 987 |
|
---|
[98000fb] | 988 | ASSERT(znum != (size_t) -1);
|
---|
[5f0f29ce] | 989 |
|
---|
| 990 | zone_get_frame(&zones.info[znum],
|
---|
| 991 | pfn - zones.info[znum].base)->parent = data;
|
---|
| 992 |
|
---|
[da1bafb] | 993 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 994 | }
|
---|
| 995 |
|
---|
[98000fb] | 996 | void *frame_get_parent(pfn_t pfn, size_t hint)
|
---|
[085d973] | 997 | {
|
---|
[da1bafb] | 998 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 999 |
|
---|
[98000fb] | 1000 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[5f0f29ce] | 1001 |
|
---|
[98000fb] | 1002 | ASSERT(znum != (size_t) -1);
|
---|
[5f0f29ce] | 1003 |
|
---|
| 1004 | void *res = zone_get_frame(&zones.info[znum],
|
---|
| 1005 | pfn - zones.info[znum].base)->parent;
|
---|
| 1006 |
|
---|
[da1bafb] | 1007 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 1008 |
|
---|
| 1009 | return res;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | /** Allocate power-of-two frames of physical memory.
|
---|
| 1013 | *
|
---|
[5f0f29ce] | 1014 | * @param order Allocate exactly 2^order frames.
|
---|
| 1015 | * @param flags Flags for host zone selection and address processing.
|
---|
| 1016 | * @param pzone Preferred zone.
|
---|
[085d973] | 1017 | *
|
---|
[5f0f29ce] | 1018 | * @return Physical address of the allocated frame.
|
---|
[9a68b34d] | 1019 | *
|
---|
[085d973] | 1020 | */
|
---|
[98000fb] | 1021 | void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
|
---|
[085d973] | 1022 | {
|
---|
[98000fb] | 1023 | size_t size = ((size_t) 1) << order;
|
---|
| 1024 | size_t hint = pzone ? (*pzone) : 0;
|
---|
[085d973] | 1025 |
|
---|
[89bcb520] | 1026 | /*
|
---|
| 1027 | * If not told otherwise, we must first reserve the memory.
|
---|
| 1028 | */
|
---|
[864b0dd] | 1029 | if (!(flags & FRAME_NO_RESERVE))
|
---|
| 1030 | reserve_force_alloc(size);
|
---|
| 1031 |
|
---|
[085d973] | 1032 | loop:
|
---|
[da1bafb] | 1033 | irq_spinlock_lock(&zones.lock, true);
|
---|
[9a68b34d] | 1034 |
|
---|
[085d973] | 1035 | /*
|
---|
| 1036 | * First, find suitable frame zone.
|
---|
| 1037 | */
|
---|
[98000fb] | 1038 | size_t znum = find_free_zone(order,
|
---|
[5f0f29ce] | 1039 | FRAME_TO_ZONE_FLAGS(flags), hint);
|
---|
[9a68b34d] | 1040 |
|
---|
[085d973] | 1041 | /* If no memory, reclaim some slab memory,
|
---|
| 1042 | if it does not help, reclaim all */
|
---|
[98000fb] | 1043 | if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
|
---|
[da1bafb] | 1044 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 1045 | size_t freed = slab_reclaim(0);
|
---|
[da1bafb] | 1046 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f049eec] | 1047 |
|
---|
[5f0f29ce] | 1048 | if (freed > 0)
|
---|
| 1049 | znum = find_free_zone(order,
|
---|
| 1050 | FRAME_TO_ZONE_FLAGS(flags), hint);
|
---|
| 1051 |
|
---|
[98000fb] | 1052 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 1053 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 1054 | freed = slab_reclaim(SLAB_RECLAIM_ALL);
|
---|
[da1bafb] | 1055 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f049eec] | 1056 |
|
---|
[5f0f29ce] | 1057 | if (freed > 0)
|
---|
| 1058 | znum = find_free_zone(order,
|
---|
| 1059 | FRAME_TO_ZONE_FLAGS(flags), hint);
|
---|
[085d973] | 1060 | }
|
---|
| 1061 | }
|
---|
[5f0f29ce] | 1062 |
|
---|
[98000fb] | 1063 | if (znum == (size_t) -1) {
|
---|
[1a1744e] | 1064 | if (flags & FRAME_ATOMIC) {
|
---|
[da1bafb] | 1065 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[89bcb520] | 1066 | if (!(flags & FRAME_NO_RESERVE))
|
---|
| 1067 | reserve_free(size);
|
---|
[5f0f29ce] | 1068 | return NULL;
|
---|
[1a1744e] | 1069 | }
|
---|
[085d973] | 1070 |
|
---|
[1a1744e] | 1071 | #ifdef CONFIG_DEBUG
|
---|
[8d308b9] | 1072 | size_t avail = frame_total_free_get_internal();
|
---|
[1a1744e] | 1073 | #endif
|
---|
[5f0f29ce] | 1074 |
|
---|
[da1bafb] | 1075 | irq_spinlock_unlock(&zones.lock, true);
|
---|
| 1076 |
|
---|
[05411e8] | 1077 | if (!THREAD)
|
---|
| 1078 | panic("Cannot wait for memory to become available.");
|
---|
[5f0f29ce] | 1079 |
|
---|
| 1080 | /*
|
---|
| 1081 | * Sleep until some frames are available again.
|
---|
| 1082 | */
|
---|
| 1083 |
|
---|
| 1084 | #ifdef CONFIG_DEBUG
|
---|
[7e752b2] | 1085 | printf("Thread %" PRIu64 " waiting for %zu frames, "
|
---|
| 1086 | "%zu available.\n", THREAD->tid, size, avail);
|
---|
[5f0f29ce] | 1087 | #endif
|
---|
| 1088 |
|
---|
[905721b] | 1089 | /*
|
---|
| 1090 | * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
|
---|
| 1091 | * to prevent deadlock with TLB shootdown.
|
---|
| 1092 | */
|
---|
| 1093 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 1094 | mutex_lock(&mem_avail_mtx);
|
---|
[5f0f29ce] | 1095 |
|
---|
| 1096 | if (mem_avail_req > 0)
|
---|
| 1097 | mem_avail_req = min(mem_avail_req, size);
|
---|
| 1098 | else
|
---|
| 1099 | mem_avail_req = size;
|
---|
[98000fb] | 1100 | size_t gen = mem_avail_gen;
|
---|
[5f0f29ce] | 1101 |
|
---|
| 1102 | while (gen == mem_avail_gen)
|
---|
[1bb3766] | 1103 | condvar_wait(&mem_avail_cv, &mem_avail_mtx);
|
---|
[5f0f29ce] | 1104 |
|
---|
[1bb3766] | 1105 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 1106 | interrupts_restore(ipl);
|
---|
[5f0f29ce] | 1107 |
|
---|
[1a1744e] | 1108 | #ifdef CONFIG_DEBUG
|
---|
[5f0f29ce] | 1109 | printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
|
---|
[1a1744e] | 1110 | #endif
|
---|
[5f0f29ce] | 1111 |
|
---|
[085d973] | 1112 | goto loop;
|
---|
| 1113 | }
|
---|
[9a68b34d] | 1114 |
|
---|
[5f0f29ce] | 1115 | pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
|
---|
| 1116 | + zones.info[znum].base;
|
---|
[1bb3766] | 1117 |
|
---|
[da1bafb] | 1118 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 1119 |
|
---|
| 1120 | if (pzone)
|
---|
| 1121 | *pzone = znum;
|
---|
| 1122 |
|
---|
[2e9eae2] | 1123 | if (flags & FRAME_KA)
|
---|
[5f0f29ce] | 1124 | return (void *) PA2KA(PFN2ADDR(pfn));
|
---|
| 1125 |
|
---|
| 1126 | return (void *) PFN2ADDR(pfn);
|
---|
[085d973] | 1127 | }
|
---|
| 1128 |
|
---|
[0139747] | 1129 | void *frame_alloc(uint8_t order, frame_flags_t flags)
|
---|
| 1130 | {
|
---|
| 1131 | return frame_alloc_generic(order, flags, NULL);
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
[e608cbe] | 1134 | void *frame_alloc_noreserve(uint8_t order, frame_flags_t flags)
|
---|
| 1135 | {
|
---|
| 1136 | return frame_alloc_generic(order, flags | FRAME_NO_RESERVE, NULL);
|
---|
| 1137 | }
|
---|
| 1138 |
|
---|
[085d973] | 1139 | /** Free a frame.
|
---|
| 1140 | *
|
---|
[32fffef0] | 1141 | * Find respective frame structure for supplied physical frame address.
|
---|
[5f0f29ce] | 1142 | * Decrement frame reference count. If it drops to zero, move the frame
|
---|
| 1143 | * structure to free list.
|
---|
| 1144 | *
|
---|
| 1145 | * @param frame Physical Address of of the frame to be freed.
|
---|
[c32e6bc] | 1146 | * @param flags Flags to control memory reservation.
|
---|
[085d973] | 1147 | *
|
---|
| 1148 | */
|
---|
[c32e6bc] | 1149 | void frame_free_generic(uintptr_t frame, frame_flags_t flags)
|
---|
[085d973] | 1150 | {
|
---|
[89bcb520] | 1151 | size_t size;
|
---|
| 1152 |
|
---|
[da1bafb] | 1153 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 1154 |
|
---|
[085d973] | 1155 | /*
|
---|
| 1156 | * First, find host frame zone for addr.
|
---|
| 1157 | */
|
---|
[5f0f29ce] | 1158 | pfn_t pfn = ADDR2PFN(frame);
|
---|
[0b4a67a] | 1159 | size_t znum = find_zone(pfn, 1, 0);
|
---|
[89bcb520] | 1160 |
|
---|
[98000fb] | 1161 | ASSERT(znum != (size_t) -1);
|
---|
[085d973] | 1162 |
|
---|
[89bcb520] | 1163 | size = zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
|
---|
[085d973] | 1164 |
|
---|
[da1bafb] | 1165 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[1a1744e] | 1166 |
|
---|
| 1167 | /*
|
---|
| 1168 | * Signal that some memory has been freed.
|
---|
| 1169 | */
|
---|
[905721b] | 1170 |
|
---|
| 1171 |
|
---|
| 1172 | /*
|
---|
| 1173 | * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
|
---|
| 1174 | * to prevent deadlock with TLB shootdown.
|
---|
| 1175 | */
|
---|
| 1176 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 1177 | mutex_lock(&mem_avail_mtx);
|
---|
[5f0f29ce] | 1178 | if (mem_avail_req > 0)
|
---|
[28295955] | 1179 | mem_avail_req -= min(mem_avail_req, size);
|
---|
[5f0f29ce] | 1180 |
|
---|
| 1181 | if (mem_avail_req == 0) {
|
---|
| 1182 | mem_avail_gen++;
|
---|
| 1183 | condvar_broadcast(&mem_avail_cv);
|
---|
| 1184 | }
|
---|
[1bb3766] | 1185 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 1186 | interrupts_restore(ipl);
|
---|
[89bcb520] | 1187 |
|
---|
| 1188 | if (!(flags & FRAME_NO_RESERVE))
|
---|
| 1189 | reserve_free(size);
|
---|
[085d973] | 1190 | }
|
---|
| 1191 |
|
---|
[c32e6bc] | 1192 | void frame_free(uintptr_t frame)
|
---|
| 1193 | {
|
---|
| 1194 | frame_free_generic(frame, 0);
|
---|
| 1195 | }
|
---|
| 1196 |
|
---|
[e608cbe] | 1197 | void frame_free_noreserve(uintptr_t frame)
|
---|
| 1198 | {
|
---|
| 1199 | frame_free_generic(frame, FRAME_NO_RESERVE);
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
[f3ac636] | 1202 | /** Add reference to frame.
|
---|
| 1203 | *
|
---|
| 1204 | * Find respective frame structure for supplied PFN and
|
---|
| 1205 | * increment frame reference count.
|
---|
| 1206 | *
|
---|
[5f0f29ce] | 1207 | * @param pfn Frame number of the frame to be freed.
|
---|
| 1208 | *
|
---|
[f3ac636] | 1209 | */
|
---|
[97bdb4a] | 1210 | NO_TRACE void frame_reference_add(pfn_t pfn)
|
---|
[f3ac636] | 1211 | {
|
---|
[da1bafb] | 1212 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f3ac636] | 1213 |
|
---|
| 1214 | /*
|
---|
| 1215 | * First, find host frame zone for addr.
|
---|
| 1216 | */
|
---|
[0b4a67a] | 1217 | size_t znum = find_zone(pfn, 1, 0);
|
---|
[5f0f29ce] | 1218 |
|
---|
[98000fb] | 1219 | ASSERT(znum != (size_t) -1);
|
---|
[f3ac636] | 1220 |
|
---|
[5f0f29ce] | 1221 | zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
|
---|
[f3ac636] | 1222 |
|
---|
[da1bafb] | 1223 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[f3ac636] | 1224 | }
|
---|
[085d973] | 1225 |
|
---|
[da1bafb] | 1226 | /** Mark given range unavailable in frame zones.
|
---|
| 1227 | *
|
---|
| 1228 | */
|
---|
[97bdb4a] | 1229 | NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
|
---|
[085d973] | 1230 | {
|
---|
[da1bafb] | 1231 | irq_spinlock_lock(&zones.lock, true);
|
---|
[052da81] | 1232 |
|
---|
[98000fb] | 1233 | size_t i;
|
---|
[2936eef] | 1234 | for (i = 0; i < count; i++) {
|
---|
[98000fb] | 1235 | size_t znum = find_zone(start + i, 1, 0);
|
---|
| 1236 | if (znum == (size_t) -1) /* PFN not found */
|
---|
[085d973] | 1237 | continue;
|
---|
[5f0f29ce] | 1238 |
|
---|
| 1239 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 1240 | start + i - zones.info[znum].base);
|
---|
[085d973] | 1241 | }
|
---|
[5f0f29ce] | 1242 |
|
---|
[da1bafb] | 1243 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 1244 | }
|
---|
| 1245 |
|
---|
[da1bafb] | 1246 | /** Initialize physical memory management.
|
---|
| 1247 | *
|
---|
| 1248 | */
|
---|
[085d973] | 1249 | void frame_init(void)
|
---|
| 1250 | {
|
---|
| 1251 | if (config.cpu_active == 1) {
|
---|
| 1252 | zones.count = 0;
|
---|
[da1bafb] | 1253 | irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
|
---|
[1bb3766] | 1254 | mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
|
---|
| 1255 | condvar_initialize(&mem_avail_cv);
|
---|
[085d973] | 1256 | }
|
---|
[5f0f29ce] | 1257 |
|
---|
[085d973] | 1258 | /* Tell the architecture to create some memory */
|
---|
[ddcc8a0] | 1259 | frame_low_arch_init();
|
---|
[085d973] | 1260 | if (config.cpu_active == 1) {
|
---|
[4638401] | 1261 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
|
---|
| 1262 | SIZE2FRAMES(config.kernel_size));
|
---|
| 1263 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
|
---|
| 1264 | SIZE2FRAMES(config.stack_size));
|
---|
[b6b576c] | 1265 |
|
---|
[98000fb] | 1266 | size_t i;
|
---|
[4638401] | 1267 | for (i = 0; i < init.cnt; i++) {
|
---|
[32817cc] | 1268 | pfn_t pfn = ADDR2PFN(init.tasks[i].paddr);
|
---|
[4638401] | 1269 | frame_mark_unavailable(pfn,
|
---|
| 1270 | SIZE2FRAMES(init.tasks[i].size));
|
---|
| 1271 | }
|
---|
[5f0f29ce] | 1272 |
|
---|
[61e90dd] | 1273 | if (ballocs.size)
|
---|
[4638401] | 1274 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
|
---|
| 1275 | SIZE2FRAMES(ballocs.size));
|
---|
[5f0f29ce] | 1276 |
|
---|
[bffa0b06] | 1277 | /* Black list first frame, as allocating NULL would
|
---|
[5f0f29ce] | 1278 | * fail in some places
|
---|
| 1279 | */
|
---|
[bffa0b06] | 1280 | frame_mark_unavailable(0, 1);
|
---|
[085d973] | 1281 | }
|
---|
[ddcc8a0] | 1282 | frame_high_arch_init();
|
---|
[085d973] | 1283 | }
|
---|
| 1284 |
|
---|
[ad12b5ea] | 1285 | /** Adjust bounds of physical memory region according to low/high memory split.
|
---|
| 1286 | *
|
---|
[2cc7f16] | 1287 | * @param low[in] If true, the adjustment is performed to make the region
|
---|
[ad12b5ea] | 1288 | * fit in the low memory. Otherwise the adjustment is
|
---|
| 1289 | * performed to make the region fit in the high memory.
|
---|
| 1290 | * @param basep[inout] Pointer to a variable which contains the region's base
|
---|
| 1291 | * address and which may receive the adjusted base address.
|
---|
| 1292 | * @param sizep[inout] Pointer to a variable which contains the region's size
|
---|
| 1293 | * and which may receive the adjusted size.
|
---|
| 1294 | * @retun True if the region still exists even after the
|
---|
| 1295 | * adjustment, false otherwise.
|
---|
| 1296 | */
|
---|
| 1297 | bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
|
---|
| 1298 | {
|
---|
[2cc7f16] | 1299 | uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
|
---|
[ad12b5ea] | 1300 |
|
---|
| 1301 | if (low) {
|
---|
| 1302 | if (*basep > limit)
|
---|
| 1303 | return false;
|
---|
| 1304 | if (*basep + *sizep > limit)
|
---|
| 1305 | *sizep = limit - *basep;
|
---|
| 1306 | } else {
|
---|
| 1307 | if (*basep + *sizep <= limit)
|
---|
| 1308 | return false;
|
---|
| 1309 | if (*basep <= limit) {
|
---|
| 1310 | *sizep -= limit - *basep;
|
---|
| 1311 | *basep = limit;
|
---|
| 1312 | }
|
---|
| 1313 | }
|
---|
| 1314 | return true;
|
---|
| 1315 | }
|
---|
| 1316 |
|
---|
[da1bafb] | 1317 | /** Return total size of all zones.
|
---|
| 1318 | *
|
---|
| 1319 | */
|
---|
[9dae191e] | 1320 | uint64_t zones_total_size(void)
|
---|
[deaf8d5] | 1321 | {
|
---|
[da1bafb] | 1322 | irq_spinlock_lock(&zones.lock, true);
|
---|
[71eef11] | 1323 |
|
---|
[5f0f29ce] | 1324 | uint64_t total = 0;
|
---|
[98000fb] | 1325 | size_t i;
|
---|
[5f0f29ce] | 1326 | for (i = 0; i < zones.count; i++)
|
---|
| 1327 | total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[71eef11] | 1328 |
|
---|
[da1bafb] | 1329 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[71eef11] | 1330 |
|
---|
| 1331 | return total;
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
[9dae191e] | 1334 | void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
|
---|
| 1335 | uint64_t *free)
|
---|
[516adce] | 1336 | {
|
---|
[9dae191e] | 1337 | ASSERT(total != NULL);
|
---|
| 1338 | ASSERT(unavail != NULL);
|
---|
| 1339 | ASSERT(busy != NULL);
|
---|
| 1340 | ASSERT(free != NULL);
|
---|
| 1341 |
|
---|
[da1bafb] | 1342 | irq_spinlock_lock(&zones.lock, true);
|
---|
[9dae191e] | 1343 |
|
---|
| 1344 | *total = 0;
|
---|
| 1345 | *unavail = 0;
|
---|
| 1346 | *busy = 0;
|
---|
| 1347 | *free = 0;
|
---|
| 1348 |
|
---|
[516adce] | 1349 | size_t i;
|
---|
| 1350 | for (i = 0; i < zones.count; i++) {
|
---|
[9dae191e] | 1351 | *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
| 1352 |
|
---|
[e6c4b94] | 1353 | if (zones.info[i].flags & ZONE_AVAILABLE) {
|
---|
[9dae191e] | 1354 | *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
|
---|
| 1355 | *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
|
---|
| 1356 | } else
|
---|
| 1357 | *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[516adce] | 1358 | }
|
---|
[9dae191e] | 1359 |
|
---|
[da1bafb] | 1360 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[516adce] | 1361 | }
|
---|
| 1362 |
|
---|
[da1bafb] | 1363 | /** Prints list of zones.
|
---|
| 1364 | *
|
---|
| 1365 | */
|
---|
[9dae191e] | 1366 | void zones_print_list(void)
|
---|
[deaf8d5] | 1367 | {
|
---|
[5f0f29ce] | 1368 | #ifdef __32_BITS__
|
---|
[74c5a1ca] | 1369 | printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1370 | #endif
|
---|
| 1371 |
|
---|
| 1372 | #ifdef __64_BITS__
|
---|
[74c5a1ca] | 1373 | printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1374 | #endif
|
---|
[43b1e86] | 1375 |
|
---|
[000350f8] | 1376 | /*
|
---|
| 1377 | * Because printing may require allocation of memory, we may not hold
|
---|
| 1378 | * the frame allocator locks when printing zone statistics. Therefore,
|
---|
| 1379 | * we simply gather the statistics under the protection of the locks and
|
---|
| 1380 | * print the statistics when the locks have been released.
|
---|
| 1381 | *
|
---|
| 1382 | * When someone adds/removes zones while we are printing the statistics,
|
---|
| 1383 | * we may end up with inaccurate output (e.g. a zone being skipped from
|
---|
| 1384 | * the listing).
|
---|
| 1385 | */
|
---|
[5f0f29ce] | 1386 |
|
---|
[98000fb] | 1387 | size_t i;
|
---|
[5f0f29ce] | 1388 | for (i = 0;; i++) {
|
---|
[da1bafb] | 1389 | irq_spinlock_lock(&zones.lock, true);
|
---|
[000350f8] | 1390 |
|
---|
| 1391 | if (i >= zones.count) {
|
---|
[da1bafb] | 1392 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[000350f8] | 1393 | break;
|
---|
| 1394 | }
|
---|
[5f0f29ce] | 1395 |
|
---|
| 1396 | uintptr_t base = PFN2ADDR(zones.info[i].base);
|
---|
[98000fb] | 1397 | size_t count = zones.info[i].count;
|
---|
[5f0f29ce] | 1398 | zone_flags_t flags = zones.info[i].flags;
|
---|
[98000fb] | 1399 | size_t free_count = zones.info[i].free_count;
|
---|
| 1400 | size_t busy_count = zones.info[i].busy_count;
|
---|
[000350f8] | 1401 |
|
---|
[da1bafb] | 1402 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 1403 |
|
---|
[e6c4b94] | 1404 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[5f0f29ce] | 1405 |
|
---|
[7e752b2] | 1406 | printf("%-4zu", i);
|
---|
[e49e234] | 1407 |
|
---|
[2b8b0ca] | 1408 | #ifdef __32_BITS__
|
---|
[7e752b2] | 1409 | printf(" %p", (void *) base);
|
---|
[2b8b0ca] | 1410 | #endif
|
---|
[5f0f29ce] | 1411 |
|
---|
[2b8b0ca] | 1412 | #ifdef __64_BITS__
|
---|
[7e752b2] | 1413 | printf(" %p", (void *) base);
|
---|
[e49e234] | 1414 | #endif
|
---|
| 1415 |
|
---|
[e6c4b94] | 1416 | printf(" %12zu %c%c%c%c%c ", count,
|
---|
| 1417 | available ? 'A' : '-',
|
---|
| 1418 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1419 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1420 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1421 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[43b1e86] | 1422 |
|
---|
[5f0f29ce] | 1423 | if (available)
|
---|
[7e752b2] | 1424 | printf("%14zu %14zu",
|
---|
[5f0f29ce] | 1425 | free_count, busy_count);
|
---|
[e49e234] | 1426 |
|
---|
[5f0f29ce] | 1427 | printf("\n");
|
---|
[dfd9186] | 1428 | }
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
[abbc16e] | 1431 | /** Prints zone details.
|
---|
[96cacc1] | 1432 | *
|
---|
[5f0f29ce] | 1433 | * @param num Zone base address or zone number.
|
---|
| 1434 | *
|
---|
[96cacc1] | 1435 | */
|
---|
[98000fb] | 1436 | void zone_print_one(size_t num)
|
---|
[deaf8d5] | 1437 | {
|
---|
[da1bafb] | 1438 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 1439 | size_t znum = (size_t) -1;
|
---|
[5f0f29ce] | 1440 |
|
---|
[98000fb] | 1441 | size_t i;
|
---|
[b6b576c] | 1442 | for (i = 0; i < zones.count; i++) {
|
---|
[5f0f29ce] | 1443 | if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
|
---|
| 1444 | znum = i;
|
---|
[bb68433] | 1445 | break;
|
---|
| 1446 | }
|
---|
| 1447 | }
|
---|
[5f0f29ce] | 1448 |
|
---|
[98000fb] | 1449 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 1450 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[bb68433] | 1451 | printf("Zone not found.\n");
|
---|
[2ec725f] | 1452 | return;
|
---|
[dfd9186] | 1453 | }
|
---|
[085d973] | 1454 |
|
---|
[5f0f29ce] | 1455 | uintptr_t base = PFN2ADDR(zones.info[i].base);
|
---|
| 1456 | zone_flags_t flags = zones.info[i].flags;
|
---|
[98000fb] | 1457 | size_t count = zones.info[i].count;
|
---|
| 1458 | size_t free_count = zones.info[i].free_count;
|
---|
| 1459 | size_t busy_count = zones.info[i].busy_count;
|
---|
[5f0f29ce] | 1460 |
|
---|
[da1bafb] | 1461 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 1462 |
|
---|
[e6c4b94] | 1463 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[5f0f29ce] | 1464 |
|
---|
[933cadf] | 1465 | uint64_t size;
|
---|
| 1466 | const char *size_suffix;
|
---|
| 1467 | bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
|
---|
| 1468 |
|
---|
[7e752b2] | 1469 | printf("Zone number: %zu\n", znum);
|
---|
| 1470 | printf("Zone base address: %p\n", (void *) base);
|
---|
[933cadf] | 1471 | printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
|
---|
| 1472 | size, size_suffix);
|
---|
[e6c4b94] | 1473 | printf("Zone flags: %c%c%c%c%c\n",
|
---|
| 1474 | available ? 'A' : '-',
|
---|
| 1475 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1476 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1477 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1478 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[5f0f29ce] | 1479 |
|
---|
| 1480 | if (available) {
|
---|
[933cadf] | 1481 | bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
|
---|
| 1482 | false);
|
---|
| 1483 | printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1484 | busy_count, size, size_suffix);
|
---|
| 1485 | bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
|
---|
| 1486 | false);
|
---|
| 1487 | printf("Available space: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1488 | free_count, size, size_suffix);
|
---|
[5f0f29ce] | 1489 | }
|
---|
[dfd9186] | 1490 | }
|
---|
| 1491 |
|
---|
[cc73a8a1] | 1492 | /** @}
|
---|
[b45c443] | 1493 | */
|
---|