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