| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2005 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2005 Sergey Bondari
|
|---|
| 4 | * Copyright (c) 2009 Martin Decky
|
|---|
| 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 |
|
|---|
| 31 | /** @addtogroup kernel_generic_mm
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * @file
|
|---|
| 37 | * @brief Physical frame allocator.
|
|---|
| 38 | *
|
|---|
| 39 | * This file contains the physical frame allocator and memory zone management.
|
|---|
| 40 | * The frame allocator is built on top of the two-level bitmap structure.
|
|---|
| 41 | *
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | #include <typedefs.h>
|
|---|
| 45 | #include <mm/frame.h>
|
|---|
| 46 | #include <mm/reserve.h>
|
|---|
| 47 | #include <mm/as.h>
|
|---|
| 48 | #include <panic.h>
|
|---|
| 49 | #include <assert.h>
|
|---|
| 50 | #include <adt/list.h>
|
|---|
| 51 | #include <synch/mutex.h>
|
|---|
| 52 | #include <synch/condvar.h>
|
|---|
| 53 | #include <arch/asm.h>
|
|---|
| 54 | #include <arch.h>
|
|---|
| 55 | #include <stdio.h>
|
|---|
| 56 | #include <log.h>
|
|---|
| 57 | #include <align.h>
|
|---|
| 58 | #include <mm/slab.h>
|
|---|
| 59 | #include <bitops.h>
|
|---|
| 60 | #include <macros.h>
|
|---|
| 61 | #include <config.h>
|
|---|
| 62 | #include <str.h>
|
|---|
| 63 | #include <proc/thread.h> /* THREAD */
|
|---|
| 64 |
|
|---|
| 65 | zones_t zones = {
|
|---|
| 66 | .count = 0,
|
|---|
| 67 | .lock = IRQ_SPINLOCK_INITIALIZER("frame.zones.lock"),
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | /*
|
|---|
| 71 | * Synchronization primitives used to sleep when there is no memory
|
|---|
| 72 | * available.
|
|---|
| 73 | */
|
|---|
| 74 | static IRQ_SPINLOCK_INITIALIZE(mem_avail_lock);
|
|---|
| 75 | static CONDVAR_INITIALIZE(mem_avail_cv);
|
|---|
| 76 | static size_t mem_avail_req = 0; /**< Number of frames requested. */
|
|---|
| 77 | static size_t mem_avail_gen = 0; /**< Generation counter. */
|
|---|
| 78 |
|
|---|
| 79 | /** Initialize frame structure.
|
|---|
| 80 | *
|
|---|
| 81 | * @param frame Frame structure to be initialized.
|
|---|
| 82 | *
|
|---|
| 83 | */
|
|---|
| 84 | _NO_TRACE static void frame_initialize(frame_t *frame)
|
|---|
| 85 | {
|
|---|
| 86 | frame->refcount = 0;
|
|---|
| 87 | frame->parent = NULL;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /*
|
|---|
| 91 | * Zones functions
|
|---|
| 92 | */
|
|---|
| 93 |
|
|---|
| 94 | /** Insert-sort zone into zones list.
|
|---|
| 95 | *
|
|---|
| 96 | * Assume interrupts are disabled and zones lock is
|
|---|
| 97 | * locked.
|
|---|
| 98 | *
|
|---|
| 99 | * @param base Base frame of the newly inserted zone.
|
|---|
| 100 | * @param count Number of frames of the newly inserted zone.
|
|---|
| 101 | *
|
|---|
| 102 | * @return Zone number on success, -1 on error.
|
|---|
| 103 | *
|
|---|
| 104 | */
|
|---|
| 105 | _NO_TRACE static size_t zones_insert_zone(pfn_t base, size_t count,
|
|---|
| 106 | zone_flags_t flags)
|
|---|
| 107 | {
|
|---|
| 108 | if (zones.count + 1 == ZONES_MAX) {
|
|---|
| 109 | log(LF_OTHER, LVL_ERROR, "Maximum zone count %u exceeded!",
|
|---|
| 110 | ZONES_MAX);
|
|---|
| 111 | return (size_t) -1;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | size_t i;
|
|---|
| 115 | for (i = 0; i < zones.count; i++) {
|
|---|
| 116 | /* Check for overlap */
|
|---|
| 117 | if (overlaps(zones.info[i].base, zones.info[i].count,
|
|---|
| 118 | base, count)) {
|
|---|
| 119 |
|
|---|
| 120 | /*
|
|---|
| 121 | * If the overlaping zones are of the same type
|
|---|
| 122 | * and the new zone is completely within the previous
|
|---|
| 123 | * one, then quietly ignore the new zone.
|
|---|
| 124 | *
|
|---|
| 125 | */
|
|---|
| 126 |
|
|---|
| 127 | if ((zones.info[i].flags != flags) ||
|
|---|
| 128 | (!iswithin(zones.info[i].base, zones.info[i].count,
|
|---|
| 129 | base, count))) {
|
|---|
| 130 | log(LF_OTHER, LVL_WARN,
|
|---|
| 131 | "Zone (%p, %p) overlaps "
|
|---|
| 132 | "with previous zone (%p %p)!",
|
|---|
| 133 | (void *) PFN2ADDR(base), (void *) PFN2ADDR(count),
|
|---|
| 134 | (void *) PFN2ADDR(zones.info[i].base),
|
|---|
| 135 | (void *) PFN2ADDR(zones.info[i].count));
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | return (size_t) -1;
|
|---|
| 139 | }
|
|---|
| 140 | if (base < zones.info[i].base)
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* Move other zones up */
|
|---|
| 145 | for (size_t j = zones.count; j > i; j--)
|
|---|
| 146 | zones.info[j] = zones.info[j - 1];
|
|---|
| 147 |
|
|---|
| 148 | zones.count++;
|
|---|
| 149 |
|
|---|
| 150 | return i;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /** Get total available frames.
|
|---|
| 154 | *
|
|---|
| 155 | * Assume interrupts are disabled and zones lock is
|
|---|
| 156 | * locked.
|
|---|
| 157 | *
|
|---|
| 158 | * @return Total number of available frames.
|
|---|
| 159 | *
|
|---|
| 160 | */
|
|---|
| 161 | _NO_TRACE static size_t frame_total_free_get_internal(void)
|
|---|
| 162 | {
|
|---|
| 163 | size_t total = 0;
|
|---|
| 164 | size_t i;
|
|---|
| 165 |
|
|---|
| 166 | for (i = 0; i < zones.count; i++)
|
|---|
| 167 | total += zones.info[i].free_count;
|
|---|
| 168 |
|
|---|
| 169 | return total;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | _NO_TRACE size_t frame_total_free_get(void)
|
|---|
| 173 | {
|
|---|
| 174 | size_t total;
|
|---|
| 175 |
|
|---|
| 176 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 177 | total = frame_total_free_get_internal();
|
|---|
| 178 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 179 |
|
|---|
| 180 | return total;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /** Find a zone with a given frames.
|
|---|
| 184 | *
|
|---|
| 185 | * Assume interrupts are disabled and zones lock is
|
|---|
| 186 | * locked.
|
|---|
| 187 | *
|
|---|
| 188 | * @param frame Frame number contained in zone.
|
|---|
| 189 | * @param count Number of frames to look for.
|
|---|
| 190 | * @param hint Used as zone hint.
|
|---|
| 191 | *
|
|---|
| 192 | * @return Zone index or -1 if not found.
|
|---|
| 193 | *
|
|---|
| 194 | */
|
|---|
| 195 | _NO_TRACE size_t find_zone(pfn_t frame, size_t count, size_t hint)
|
|---|
| 196 | {
|
|---|
| 197 | if (hint >= zones.count)
|
|---|
| 198 | hint = 0;
|
|---|
| 199 |
|
|---|
| 200 | size_t i = hint;
|
|---|
| 201 | do {
|
|---|
| 202 | if ((zones.info[i].base <= frame) &&
|
|---|
| 203 | (zones.info[i].base + zones.info[i].count >= frame + count))
|
|---|
| 204 | return i;
|
|---|
| 205 |
|
|---|
| 206 | i++;
|
|---|
| 207 | if (i >= zones.count)
|
|---|
| 208 | i = 0;
|
|---|
| 209 |
|
|---|
| 210 | } while (i != hint);
|
|---|
| 211 |
|
|---|
| 212 | return (size_t) -1;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /** @return True if zone can allocate specified number of frames */
|
|---|
| 216 | _NO_TRACE static bool zone_can_alloc(zone_t *zone, size_t count,
|
|---|
| 217 | pfn_t constraint)
|
|---|
| 218 | {
|
|---|
| 219 | /*
|
|---|
| 220 | * The function bitmap_allocate_range() does not modify
|
|---|
| 221 | * the bitmap if the last argument is NULL.
|
|---|
| 222 | */
|
|---|
| 223 |
|
|---|
| 224 | return ((zone->flags & ZONE_AVAILABLE) &&
|
|---|
| 225 | bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
|---|
| 226 | FRAME_LOWPRIO, constraint, NULL));
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /** Find a zone that can allocate specified number of frames
|
|---|
| 230 | *
|
|---|
| 231 | * This function searches among all zones. Assume interrupts are
|
|---|
| 232 | * disabled and zones lock is locked.
|
|---|
| 233 | *
|
|---|
| 234 | * @param count Number of free frames we are trying to find.
|
|---|
| 235 | * @param flags Required flags of the zone.
|
|---|
| 236 | * @param constraint Indication of bits that cannot be set in the
|
|---|
| 237 | * physical frame number of the first allocated frame.
|
|---|
| 238 | * @param hint Preferred zone.
|
|---|
| 239 | *
|
|---|
| 240 | * @return Zone that can allocate specified number of frames.
|
|---|
| 241 | * @return -1 if no zone can satisfy the request.
|
|---|
| 242 | *
|
|---|
| 243 | */
|
|---|
| 244 | _NO_TRACE static size_t find_free_zone_all(size_t count, zone_flags_t flags,
|
|---|
| 245 | pfn_t constraint, size_t hint)
|
|---|
| 246 | {
|
|---|
| 247 | for (size_t pos = 0; pos < zones.count; pos++) {
|
|---|
| 248 | size_t i = (pos + hint) % zones.count;
|
|---|
| 249 |
|
|---|
| 250 | /* Check whether the zone meets the search criteria. */
|
|---|
| 251 | if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
|
|---|
| 252 | continue;
|
|---|
| 253 |
|
|---|
| 254 | /* Check if the zone can satisfy the allocation request. */
|
|---|
| 255 | if (zone_can_alloc(&zones.info[i], count, constraint))
|
|---|
| 256 | return i;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | return (size_t) -1;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** Check if frame range priority memory
|
|---|
| 263 | *
|
|---|
| 264 | * @param pfn Starting frame.
|
|---|
| 265 | * @param count Number of frames.
|
|---|
| 266 | *
|
|---|
| 267 | * @return True if the range contains only priority memory.
|
|---|
| 268 | *
|
|---|
| 269 | */
|
|---|
| 270 | _NO_TRACE static bool is_high_priority(pfn_t base, size_t count)
|
|---|
| 271 | {
|
|---|
| 272 | return (base + count <= FRAME_LOWPRIO);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Find a zone that can allocate specified number of frames
|
|---|
| 276 | *
|
|---|
| 277 | * This function ignores zones that contain only high-priority
|
|---|
| 278 | * memory. Assume interrupts are disabled and zones lock is locked.
|
|---|
| 279 | *
|
|---|
| 280 | * @param count Number of free frames we are trying to find.
|
|---|
| 281 | * @param flags Required flags of the zone.
|
|---|
| 282 | * @param constraint Indication of bits that cannot be set in the
|
|---|
| 283 | * physical frame number of the first allocated frame.
|
|---|
| 284 | * @param hint Preferred zone.
|
|---|
| 285 | *
|
|---|
| 286 | * @return Zone that can allocate specified number of frames.
|
|---|
| 287 | * @return -1 if no low-priority zone can satisfy the request.
|
|---|
| 288 | *
|
|---|
| 289 | */
|
|---|
| 290 | _NO_TRACE static size_t find_free_zone_lowprio(size_t count, zone_flags_t flags,
|
|---|
| 291 | pfn_t constraint, size_t hint)
|
|---|
| 292 | {
|
|---|
| 293 | for (size_t pos = 0; pos < zones.count; pos++) {
|
|---|
| 294 | size_t i = (pos + hint) % zones.count;
|
|---|
| 295 |
|
|---|
| 296 | /* Skip zones containing only high-priority memory. */
|
|---|
| 297 | if (is_high_priority(zones.info[i].base, zones.info[i].count))
|
|---|
| 298 | continue;
|
|---|
| 299 |
|
|---|
| 300 | /* Check whether the zone meets the search criteria. */
|
|---|
| 301 | if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
|
|---|
| 302 | continue;
|
|---|
| 303 |
|
|---|
| 304 | /* Check if the zone can satisfy the allocation request. */
|
|---|
| 305 | if (zone_can_alloc(&zones.info[i], count, constraint))
|
|---|
| 306 | return i;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | return (size_t) -1;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | /** Find a zone that can allocate specified number of frames
|
|---|
| 313 | *
|
|---|
| 314 | * Assume interrupts are disabled and zones lock is
|
|---|
| 315 | * locked.
|
|---|
| 316 | *
|
|---|
| 317 | * @param count Number of free frames we are trying to find.
|
|---|
| 318 | * @param flags Required flags of the target zone.
|
|---|
| 319 | * @param constraint Indication of bits that cannot be set in the
|
|---|
| 320 | * physical frame number of the first allocated frame.
|
|---|
| 321 | * @param hint Preferred zone.
|
|---|
| 322 | *
|
|---|
| 323 | * @return Zone that can allocate specified number of frames.
|
|---|
| 324 | * @return -1 if no zone can satisfy the request.
|
|---|
| 325 | *
|
|---|
| 326 | */
|
|---|
| 327 | _NO_TRACE static size_t find_free_zone(size_t count, zone_flags_t flags,
|
|---|
| 328 | pfn_t constraint, size_t hint)
|
|---|
| 329 | {
|
|---|
| 330 | if (hint >= zones.count)
|
|---|
| 331 | hint = 0;
|
|---|
| 332 |
|
|---|
| 333 | /*
|
|---|
| 334 | * Prefer zones with low-priority memory over
|
|---|
| 335 | * zones with high-priority memory.
|
|---|
| 336 | */
|
|---|
| 337 |
|
|---|
| 338 | size_t znum = find_free_zone_lowprio(count, flags, constraint, hint);
|
|---|
| 339 | if (znum != (size_t) -1)
|
|---|
| 340 | return znum;
|
|---|
| 341 |
|
|---|
| 342 | /* Take all zones into account */
|
|---|
| 343 | return find_free_zone_all(count, flags, constraint, hint);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /*
|
|---|
| 347 | * Zone functions
|
|---|
| 348 | */
|
|---|
| 349 |
|
|---|
| 350 | /** Return frame from zone. */
|
|---|
| 351 | _NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t index)
|
|---|
| 352 | {
|
|---|
| 353 | assert(index < zone->count);
|
|---|
| 354 |
|
|---|
| 355 | return &zone->frames[index];
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | /** Allocate frame in particular zone.
|
|---|
| 359 | *
|
|---|
| 360 | * Assume zone is locked and is available for allocation.
|
|---|
| 361 | * Panics if allocation is impossible.
|
|---|
| 362 | *
|
|---|
| 363 | * @param zone Zone to allocate from.
|
|---|
| 364 | * @param count Number of frames to allocate
|
|---|
| 365 | * @param constraint Indication of bits that cannot be set in the
|
|---|
| 366 | * physical frame number of the first allocated frame.
|
|---|
| 367 | *
|
|---|
| 368 | * @return Frame index in zone.
|
|---|
| 369 | *
|
|---|
| 370 | */
|
|---|
| 371 | _NO_TRACE static size_t zone_frame_alloc(zone_t *zone, size_t count,
|
|---|
| 372 | pfn_t constraint)
|
|---|
| 373 | {
|
|---|
| 374 | assert(zone->flags & ZONE_AVAILABLE);
|
|---|
| 375 | assert(zone->free_count >= count);
|
|---|
| 376 |
|
|---|
| 377 | /* Allocate frames from zone */
|
|---|
| 378 | size_t index = (size_t) -1;
|
|---|
| 379 | int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
|---|
| 380 | FRAME_LOWPRIO, constraint, &index);
|
|---|
| 381 |
|
|---|
| 382 | (void) avail;
|
|---|
| 383 | assert(avail);
|
|---|
| 384 | assert(index != (size_t) -1);
|
|---|
| 385 |
|
|---|
| 386 | /* Update frame reference count */
|
|---|
| 387 | for (size_t i = 0; i < count; i++) {
|
|---|
| 388 | frame_t *frame = zone_get_frame(zone, index + i);
|
|---|
| 389 |
|
|---|
| 390 | assert(frame->refcount == 0);
|
|---|
| 391 | frame->refcount = 1;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | /* Update zone information. */
|
|---|
| 395 | zone->free_count -= count;
|
|---|
| 396 | zone->busy_count += count;
|
|---|
| 397 |
|
|---|
| 398 | return index;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | /** Free frame from zone.
|
|---|
| 402 | *
|
|---|
| 403 | * Assume zone is locked and is available for deallocation.
|
|---|
| 404 | *
|
|---|
| 405 | * @param zone Pointer to zone from which the frame is to be freed.
|
|---|
| 406 | * @param index Frame index relative to zone.
|
|---|
| 407 | *
|
|---|
| 408 | * @return Number of freed frames.
|
|---|
| 409 | *
|
|---|
| 410 | */
|
|---|
| 411 | _NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t index)
|
|---|
| 412 | {
|
|---|
| 413 | assert(zone->flags & ZONE_AVAILABLE);
|
|---|
| 414 |
|
|---|
| 415 | frame_t *frame = zone_get_frame(zone, index);
|
|---|
| 416 | assert(frame->refcount > 0);
|
|---|
| 417 |
|
|---|
| 418 | if (!--frame->refcount) {
|
|---|
| 419 | assert(zone->busy_count > 0);
|
|---|
| 420 |
|
|---|
| 421 | bitmap_set(&zone->bitmap, index, 0);
|
|---|
| 422 |
|
|---|
| 423 | /* Update zone information. */
|
|---|
| 424 | zone->free_count++;
|
|---|
| 425 | zone->busy_count--;
|
|---|
| 426 |
|
|---|
| 427 | return 1;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | return 0;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /** Mark frame in zone unavailable to allocation. */
|
|---|
| 434 | _NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index)
|
|---|
| 435 | {
|
|---|
| 436 | assert(zone->flags & ZONE_AVAILABLE);
|
|---|
| 437 |
|
|---|
| 438 | frame_t *frame = zone_get_frame(zone, index);
|
|---|
| 439 | assert(frame->refcount <= 1);
|
|---|
| 440 |
|
|---|
| 441 | if (frame->refcount > 0)
|
|---|
| 442 | return;
|
|---|
| 443 |
|
|---|
| 444 | assert(zone->free_count > 0);
|
|---|
| 445 |
|
|---|
| 446 | frame->refcount = 1;
|
|---|
| 447 | bitmap_set_range(&zone->bitmap, index, 1);
|
|---|
| 448 |
|
|---|
| 449 | zone->free_count--;
|
|---|
| 450 | reserve_force_alloc(1);
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | /** Mark frame in zone available to allocation. */
|
|---|
| 454 | _NO_TRACE static void zone_mark_available(zone_t *zone, size_t index)
|
|---|
| 455 | {
|
|---|
| 456 | assert(zone->flags & ZONE_AVAILABLE);
|
|---|
| 457 |
|
|---|
| 458 | frame_t *frame = zone_get_frame(zone, index);
|
|---|
| 459 | assert(frame->refcount == 1);
|
|---|
| 460 |
|
|---|
| 461 | frame->refcount = 0;
|
|---|
| 462 | bitmap_set_range(&zone->bitmap, index, 0);
|
|---|
| 463 |
|
|---|
| 464 | zone->free_count++;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | /** Merge two zones.
|
|---|
| 468 | *
|
|---|
| 469 | * Assume z1 & z2 are locked and compatible and zones lock is
|
|---|
| 470 | * locked.
|
|---|
| 471 | *
|
|---|
| 472 | * @param z1 First zone to merge.
|
|---|
| 473 | * @param z2 Second zone to merge.
|
|---|
| 474 | * @param old_z1 Original data of the first zone.
|
|---|
| 475 | * @param confdata Merged zone configuration data.
|
|---|
| 476 | *
|
|---|
| 477 | */
|
|---|
| 478 | _NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
|
|---|
| 479 | void *confdata)
|
|---|
| 480 | {
|
|---|
| 481 | assert(zones.info[z1].flags & ZONE_AVAILABLE);
|
|---|
| 482 | assert(zones.info[z2].flags & ZONE_AVAILABLE);
|
|---|
| 483 | assert(zones.info[z1].flags == zones.info[z2].flags);
|
|---|
| 484 | assert(zones.info[z1].base < zones.info[z2].base);
|
|---|
| 485 | assert(!overlaps(zones.info[z1].base, zones.info[z1].count,
|
|---|
| 486 | zones.info[z2].base, zones.info[z2].count));
|
|---|
| 487 |
|
|---|
| 488 | /* Difference between zone bases */
|
|---|
| 489 | pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
|
|---|
| 490 | pfn_t gap = base_diff - zones.info[z1].count;
|
|---|
| 491 |
|
|---|
| 492 | zones.info[z1].count = base_diff + zones.info[z2].count;
|
|---|
| 493 | zones.info[z1].free_count += zones.info[z2].free_count;
|
|---|
| 494 | zones.info[z1].busy_count += zones.info[z2].busy_count;
|
|---|
| 495 |
|
|---|
| 496 | bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
|
|---|
| 497 | confdata + (sizeof(frame_t) * zones.info[z1].count));
|
|---|
| 498 | bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count);
|
|---|
| 499 |
|
|---|
| 500 | zones.info[z1].frames = (frame_t *) confdata;
|
|---|
| 501 |
|
|---|
| 502 | /*
|
|---|
| 503 | * Copy frames and bits from both zones to preserve parents, etc.
|
|---|
| 504 | */
|
|---|
| 505 |
|
|---|
| 506 | for (size_t i = 0; i < old_z1->count; i++) {
|
|---|
| 507 | bitmap_set(&zones.info[z1].bitmap, i,
|
|---|
| 508 | bitmap_get(&old_z1->bitmap, i));
|
|---|
| 509 | zones.info[z1].frames[i] = old_z1->frames[i];
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | for (size_t i = 0; i < zones.info[z2].count; i++) {
|
|---|
| 513 | bitmap_set(&zones.info[z1].bitmap, base_diff + i,
|
|---|
| 514 | bitmap_get(&zones.info[z2].bitmap, i));
|
|---|
| 515 | zones.info[z1].frames[base_diff + i] =
|
|---|
| 516 | zones.info[z2].frames[i];
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | /*
|
|---|
| 520 | * Mark the gap between the original zones as unavailable.
|
|---|
| 521 | */
|
|---|
| 522 |
|
|---|
| 523 | for (size_t i = 0; i < gap; i++) {
|
|---|
| 524 | frame_initialize(&zones.info[z1].frames[old_z1->count + i]);
|
|---|
| 525 | zone_mark_unavailable(&zones.info[z1], old_z1->count + i);
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | /** Return old configuration frames into the zone.
|
|---|
| 530 | *
|
|---|
| 531 | * We have two cases:
|
|---|
| 532 | * - The configuration data is outside the zone
|
|---|
| 533 | * -> do nothing (perhaps call frame_free?)
|
|---|
| 534 | * - The configuration data was created by zone_create
|
|---|
| 535 | * or updated by reduce_region -> free every frame
|
|---|
| 536 | *
|
|---|
| 537 | * @param znum The actual zone where freeing should occur.
|
|---|
| 538 | * @param pfn Old zone configuration frame.
|
|---|
| 539 | * @param count Old zone frame count.
|
|---|
| 540 | *
|
|---|
| 541 | */
|
|---|
| 542 | _NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
|
|---|
| 543 | {
|
|---|
| 544 | assert(zones.info[znum].flags & ZONE_AVAILABLE);
|
|---|
| 545 |
|
|---|
| 546 | size_t cframes = SIZE2FRAMES(zone_conf_size(count));
|
|---|
| 547 |
|
|---|
| 548 | if ((pfn < zones.info[znum].base) ||
|
|---|
| 549 | (pfn >= zones.info[znum].base + zones.info[znum].count))
|
|---|
| 550 | return;
|
|---|
| 551 |
|
|---|
| 552 | for (size_t i = 0; i < cframes; i++)
|
|---|
| 553 | zone_mark_available(&zones.info[znum],
|
|---|
| 554 | pfn - zones.info[znum].base + i);
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | /** Merge zones z1 and z2.
|
|---|
| 558 | *
|
|---|
| 559 | * The merged zones must be 2 zones with no zone existing in between
|
|---|
| 560 | * (which means that z2 = z1 + 1). Both zones must be available zones
|
|---|
| 561 | * with the same flags.
|
|---|
| 562 | *
|
|---|
| 563 | * When you create a new zone, the frame allocator configuration does
|
|---|
| 564 | * not to be 2^order size. Once the allocator is running it is no longer
|
|---|
| 565 | * possible, merged configuration data occupies more space :-/
|
|---|
| 566 | *
|
|---|
| 567 | */
|
|---|
| 568 | bool zone_merge(size_t z1, size_t z2)
|
|---|
| 569 | {
|
|---|
| 570 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 571 |
|
|---|
| 572 | bool ret = true;
|
|---|
| 573 |
|
|---|
| 574 | /*
|
|---|
| 575 | * We can join only 2 zones with none existing inbetween,
|
|---|
| 576 | * the zones have to be available and with the same
|
|---|
| 577 | * set of flags
|
|---|
| 578 | */
|
|---|
| 579 | if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
|
|---|
| 580 | (zones.info[z1].flags != zones.info[z2].flags)) {
|
|---|
| 581 | ret = false;
|
|---|
| 582 | goto errout;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | pfn_t cframes = SIZE2FRAMES(zone_conf_size(
|
|---|
| 586 | zones.info[z2].base - zones.info[z1].base +
|
|---|
| 587 | zones.info[z2].count));
|
|---|
| 588 |
|
|---|
| 589 | /* Allocate merged zone data inside one of the zones */
|
|---|
| 590 | pfn_t pfn;
|
|---|
| 591 | if (zone_can_alloc(&zones.info[z1], cframes, 0)) {
|
|---|
| 592 | pfn = zones.info[z1].base +
|
|---|
| 593 | zone_frame_alloc(&zones.info[z1], cframes, 0);
|
|---|
| 594 | } else if (zone_can_alloc(&zones.info[z2], cframes, 0)) {
|
|---|
| 595 | pfn = zones.info[z2].base +
|
|---|
| 596 | zone_frame_alloc(&zones.info[z2], cframes, 0);
|
|---|
| 597 | } else {
|
|---|
| 598 | ret = false;
|
|---|
| 599 | goto errout;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | /* Preserve original data from z1 */
|
|---|
| 603 | zone_t old_z1 = zones.info[z1];
|
|---|
| 604 |
|
|---|
| 605 | /* Do zone merging */
|
|---|
| 606 | zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
|
|---|
| 607 |
|
|---|
| 608 | /* Subtract zone information from busy frames */
|
|---|
| 609 | zones.info[z1].busy_count -= cframes;
|
|---|
| 610 |
|
|---|
| 611 | /* Free old zone information */
|
|---|
| 612 | return_config_frames(z1,
|
|---|
| 613 | ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
|
|---|
| 614 | return_config_frames(z1,
|
|---|
| 615 | ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
|
|---|
| 616 | zones.info[z2].count);
|
|---|
| 617 |
|
|---|
| 618 | /* Move zones down */
|
|---|
| 619 | for (size_t i = z2 + 1; i < zones.count; i++)
|
|---|
| 620 | zones.info[i - 1] = zones.info[i];
|
|---|
| 621 |
|
|---|
| 622 | zones.count--;
|
|---|
| 623 |
|
|---|
| 624 | errout:
|
|---|
| 625 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 626 |
|
|---|
| 627 | return ret;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /** Merge all mergeable zones into one big zone.
|
|---|
| 631 | *
|
|---|
| 632 | * It is reasonable to do this on systems where
|
|---|
| 633 | * BIOS reports parts in chunks, so that we could
|
|---|
| 634 | * have 1 zone (it's faster).
|
|---|
| 635 | *
|
|---|
| 636 | */
|
|---|
| 637 | void zone_merge_all(void)
|
|---|
| 638 | {
|
|---|
| 639 | size_t i = 1;
|
|---|
| 640 |
|
|---|
| 641 | while (i < zones.count) {
|
|---|
| 642 | if (!zone_merge(i - 1, i))
|
|---|
| 643 | i++;
|
|---|
| 644 | }
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | /** Create new frame zone.
|
|---|
| 648 | *
|
|---|
| 649 | * @param zone Zone to construct.
|
|---|
| 650 | * @param start Physical address of the first frame within the zone.
|
|---|
| 651 | * @param count Count of frames in zone.
|
|---|
| 652 | * @param flags Zone flags.
|
|---|
| 653 | * @param confdata Configuration data of the zone.
|
|---|
| 654 | *
|
|---|
| 655 | * @return Initialized zone.
|
|---|
| 656 | *
|
|---|
| 657 | */
|
|---|
| 658 | _NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
|
|---|
| 659 | zone_flags_t flags, void *confdata)
|
|---|
| 660 | {
|
|---|
| 661 | zone->base = start;
|
|---|
| 662 | zone->count = count;
|
|---|
| 663 | zone->flags = flags;
|
|---|
| 664 | zone->free_count = count;
|
|---|
| 665 | zone->busy_count = 0;
|
|---|
| 666 |
|
|---|
| 667 | if (flags & ZONE_AVAILABLE) {
|
|---|
| 668 | /*
|
|---|
| 669 | * Initialize frame bitmap (located after the array of
|
|---|
| 670 | * frame_t structures in the configuration space).
|
|---|
| 671 | */
|
|---|
| 672 |
|
|---|
| 673 | bitmap_initialize(&zone->bitmap, count, confdata +
|
|---|
| 674 | (sizeof(frame_t) * count));
|
|---|
| 675 | bitmap_clear_range(&zone->bitmap, 0, count);
|
|---|
| 676 |
|
|---|
| 677 | /*
|
|---|
| 678 | * Initialize the array of frame_t structures.
|
|---|
| 679 | */
|
|---|
| 680 |
|
|---|
| 681 | zone->frames = (frame_t *) confdata;
|
|---|
| 682 |
|
|---|
| 683 | for (size_t i = 0; i < count; i++)
|
|---|
| 684 | frame_initialize(&zone->frames[i]);
|
|---|
| 685 | } else {
|
|---|
| 686 | bitmap_initialize(&zone->bitmap, 0, NULL);
|
|---|
| 687 | zone->frames = NULL;
|
|---|
| 688 | }
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | /** Compute configuration data size for zone.
|
|---|
| 692 | *
|
|---|
| 693 | * @param count Size of zone in frames.
|
|---|
| 694 | *
|
|---|
| 695 | * @return Size of zone configuration info (in bytes).
|
|---|
| 696 | *
|
|---|
| 697 | */
|
|---|
| 698 | size_t zone_conf_size(size_t count)
|
|---|
| 699 | {
|
|---|
| 700 | return (count * sizeof(frame_t) + bitmap_size(count));
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | /** Allocate external configuration frames from low memory. */
|
|---|
| 704 | pfn_t zone_external_conf_alloc(size_t count)
|
|---|
| 705 | {
|
|---|
| 706 | size_t frames = SIZE2FRAMES(zone_conf_size(count));
|
|---|
| 707 |
|
|---|
| 708 | return ADDR2PFN((uintptr_t)
|
|---|
| 709 | frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | /** Create and add zone to system.
|
|---|
| 713 | *
|
|---|
| 714 | * @param start First frame number (absolute).
|
|---|
| 715 | * @param count Size of zone in frames.
|
|---|
| 716 | * @param confframe Where configuration frames are supposed to be.
|
|---|
| 717 | * Automatically checks that we will not disturb the
|
|---|
| 718 | * kernel and possibly init. If confframe is given
|
|---|
| 719 | * _outside_ this zone, it is expected, that the area is
|
|---|
| 720 | * already marked BUSY and big enough to contain
|
|---|
| 721 | * zone_conf_size() amount of data. If the confframe is
|
|---|
| 722 | * inside the area, the zone free frame information is
|
|---|
| 723 | * modified not to include it.
|
|---|
| 724 | *
|
|---|
| 725 | * @return Zone number or -1 on error.
|
|---|
| 726 | *
|
|---|
| 727 | */
|
|---|
| 728 | size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
|
|---|
| 729 | zone_flags_t flags)
|
|---|
| 730 | {
|
|---|
| 731 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 732 |
|
|---|
| 733 | if (flags & ZONE_AVAILABLE) { /* Create available zone */
|
|---|
| 734 | /*
|
|---|
| 735 | * Theoretically we could have NULL here, practically make sure
|
|---|
| 736 | * nobody tries to do that. If some platform requires, remove
|
|---|
| 737 | * the assert
|
|---|
| 738 | */
|
|---|
| 739 | assert(confframe != ADDR2PFN((uintptr_t) NULL));
|
|---|
| 740 |
|
|---|
| 741 | /* Update the known end of physical memory. */
|
|---|
| 742 | config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
|
|---|
| 743 |
|
|---|
| 744 | /*
|
|---|
| 745 | * If confframe is supposed to be inside our zone, then make sure
|
|---|
| 746 | * it does not span kernel & init
|
|---|
| 747 | */
|
|---|
| 748 | size_t confcount = SIZE2FRAMES(zone_conf_size(count));
|
|---|
| 749 |
|
|---|
| 750 | if ((confframe >= start) && (confframe < start + count)) {
|
|---|
| 751 | for (; confframe < start + count; confframe++) {
|
|---|
| 752 | uintptr_t addr = PFN2ADDR(confframe);
|
|---|
| 753 | if (overlaps(addr, PFN2ADDR(confcount),
|
|---|
| 754 | KA2PA(config.base), config.kernel_size))
|
|---|
| 755 | continue;
|
|---|
| 756 |
|
|---|
| 757 | if (overlaps(addr, PFN2ADDR(confcount),
|
|---|
| 758 | KA2PA(ballocs.base), ballocs.size))
|
|---|
| 759 | continue;
|
|---|
| 760 |
|
|---|
| 761 | bool overlap = false;
|
|---|
| 762 | for (size_t i = 0; i < init.cnt; i++) {
|
|---|
| 763 | if (overlaps(addr, PFN2ADDR(confcount),
|
|---|
| 764 | init.tasks[i].paddr,
|
|---|
| 765 | init.tasks[i].size)) {
|
|---|
| 766 | overlap = true;
|
|---|
| 767 | break;
|
|---|
| 768 | }
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | if (overlap)
|
|---|
| 772 | continue;
|
|---|
| 773 |
|
|---|
| 774 | break;
|
|---|
| 775 | }
|
|---|
| 776 |
|
|---|
| 777 | if (confframe >= start + count)
|
|---|
| 778 | panic("Cannot find configuration data for zone.");
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | size_t znum = zones_insert_zone(start, count, flags);
|
|---|
| 782 | if (znum == (size_t) -1) {
|
|---|
| 783 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 784 | return (size_t) -1;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
|
|---|
| 788 | zone_construct(&zones.info[znum], start, count, flags, confdata);
|
|---|
| 789 |
|
|---|
| 790 | /* If confdata in zone, mark as unavailable */
|
|---|
| 791 | if ((confframe >= start) && (confframe < start + count)) {
|
|---|
| 792 | for (size_t i = confframe; i < confframe + confcount; i++)
|
|---|
| 793 | zone_mark_unavailable(&zones.info[znum],
|
|---|
| 794 | i - zones.info[znum].base);
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 798 |
|
|---|
| 799 | return znum;
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | /* Non-available zone */
|
|---|
| 803 | size_t znum = zones_insert_zone(start, count, flags);
|
|---|
| 804 | if (znum == (size_t) -1) {
|
|---|
| 805 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 806 | return (size_t) -1;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | zone_construct(&zones.info[znum], start, count, flags, NULL);
|
|---|
| 810 |
|
|---|
| 811 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 812 |
|
|---|
| 813 | return znum;
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| 816 | /*
|
|---|
| 817 | * Frame functions
|
|---|
| 818 | */
|
|---|
| 819 |
|
|---|
| 820 | /** Set parent of frame. */
|
|---|
| 821 | void frame_set_parent(pfn_t pfn, void *data, size_t hint)
|
|---|
| 822 | {
|
|---|
| 823 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 824 |
|
|---|
| 825 | size_t znum = find_zone(pfn, 1, hint);
|
|---|
| 826 |
|
|---|
| 827 | assert(znum != (size_t) -1);
|
|---|
| 828 |
|
|---|
| 829 | zone_get_frame(&zones.info[znum],
|
|---|
| 830 | pfn - zones.info[znum].base)->parent = data;
|
|---|
| 831 |
|
|---|
| 832 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 833 | }
|
|---|
| 834 |
|
|---|
| 835 | void *frame_get_parent(pfn_t pfn, size_t hint)
|
|---|
| 836 | {
|
|---|
| 837 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 838 |
|
|---|
| 839 | size_t znum = find_zone(pfn, 1, hint);
|
|---|
| 840 |
|
|---|
| 841 | assert(znum != (size_t) -1);
|
|---|
| 842 |
|
|---|
| 843 | void *res = zone_get_frame(&zones.info[znum],
|
|---|
| 844 | pfn - zones.info[znum].base)->parent;
|
|---|
| 845 |
|
|---|
| 846 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 847 |
|
|---|
| 848 | return res;
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | static size_t try_find_zone(size_t count, bool lowmem,
|
|---|
| 852 | pfn_t frame_constraint, size_t hint)
|
|---|
| 853 | {
|
|---|
| 854 | if (!lowmem) {
|
|---|
| 855 | size_t znum = find_free_zone(count,
|
|---|
| 856 | ZONE_HIGHMEM | ZONE_AVAILABLE, frame_constraint, hint);
|
|---|
| 857 | if (znum != (size_t) -1)
|
|---|
| 858 | return znum;
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | return find_free_zone(count, ZONE_LOWMEM | ZONE_AVAILABLE,
|
|---|
| 862 | frame_constraint, hint);
|
|---|
| 863 | }
|
|---|
| 864 |
|
|---|
| 865 | /** Allocate frames of physical memory.
|
|---|
| 866 | *
|
|---|
| 867 | * @param count Number of continuous frames to allocate.
|
|---|
| 868 | * @param flags Flags for host zone selection and address processing.
|
|---|
| 869 | * @param constraint Indication of physical address bits that cannot be
|
|---|
| 870 | * set in the address of the first allocated frame.
|
|---|
| 871 | * @param pzone Preferred zone.
|
|---|
| 872 | *
|
|---|
| 873 | * @return Physical address of the allocated frame.
|
|---|
| 874 | *
|
|---|
| 875 | */
|
|---|
| 876 | uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
|
|---|
| 877 | uintptr_t constraint, size_t *pzone)
|
|---|
| 878 | {
|
|---|
| 879 | assert(count > 0);
|
|---|
| 880 |
|
|---|
| 881 | size_t hint = pzone ? (*pzone) : 0;
|
|---|
| 882 | pfn_t frame_constraint = ADDR2PFN(constraint);
|
|---|
| 883 |
|
|---|
| 884 | /*
|
|---|
| 885 | * If not told otherwise, we must first reserve the memory.
|
|---|
| 886 | */
|
|---|
| 887 | if (!(flags & FRAME_NO_RESERVE))
|
|---|
| 888 | reserve_force_alloc(count);
|
|---|
| 889 |
|
|---|
| 890 | loop:
|
|---|
| 891 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 892 |
|
|---|
| 893 | // TODO: Print diagnostic if neither is explicitly specified.
|
|---|
| 894 | bool lowmem = (flags & FRAME_LOWMEM) || !(flags & FRAME_HIGHMEM);
|
|---|
| 895 |
|
|---|
| 896 | /*
|
|---|
| 897 | * First, find suitable frame zone.
|
|---|
| 898 | */
|
|---|
| 899 | size_t znum = try_find_zone(count, lowmem, frame_constraint, hint);
|
|---|
| 900 |
|
|---|
| 901 | /*
|
|---|
| 902 | * If no memory, reclaim some slab memory,
|
|---|
| 903 | * if it does not help, reclaim all.
|
|---|
| 904 | */
|
|---|
| 905 | if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
|
|---|
| 906 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 907 | size_t freed = slab_reclaim(0);
|
|---|
| 908 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 909 |
|
|---|
| 910 | if (freed > 0)
|
|---|
| 911 | znum = try_find_zone(count, lowmem,
|
|---|
| 912 | frame_constraint, hint);
|
|---|
| 913 |
|
|---|
| 914 | if (znum == (size_t) -1) {
|
|---|
| 915 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 916 | freed = slab_reclaim(SLAB_RECLAIM_ALL);
|
|---|
| 917 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 918 |
|
|---|
| 919 | if (freed > 0)
|
|---|
| 920 | znum = try_find_zone(count, lowmem,
|
|---|
| 921 | frame_constraint, hint);
|
|---|
| 922 | }
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | if (znum == (size_t) -1) {
|
|---|
| 926 | if (flags & FRAME_ATOMIC) {
|
|---|
| 927 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 928 |
|
|---|
| 929 | if (!(flags & FRAME_NO_RESERVE))
|
|---|
| 930 | reserve_free(count);
|
|---|
| 931 |
|
|---|
| 932 | return 0;
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | size_t avail = frame_total_free_get_internal();
|
|---|
| 936 |
|
|---|
| 937 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 938 |
|
|---|
| 939 | if (!THREAD)
|
|---|
| 940 | panic("Cannot wait for %zu frames to become available "
|
|---|
| 941 | "(%zu available).", count, avail);
|
|---|
| 942 |
|
|---|
| 943 | /*
|
|---|
| 944 | * Sleep until some frames are available again.
|
|---|
| 945 | */
|
|---|
| 946 |
|
|---|
| 947 | #ifdef CONFIG_DEBUG
|
|---|
| 948 | log(LF_OTHER, LVL_DEBUG,
|
|---|
| 949 | "Thread %" PRIu64 " waiting for %zu frames "
|
|---|
| 950 | "%zu available.", THREAD->tid, count, avail);
|
|---|
| 951 | #endif
|
|---|
| 952 |
|
|---|
| 953 | /* Disabled interrupts needed to prevent deadlock with TLB shootdown. */
|
|---|
| 954 | irq_spinlock_lock(&mem_avail_lock, true);
|
|---|
| 955 |
|
|---|
| 956 | if (mem_avail_req > 0)
|
|---|
| 957 | mem_avail_req = min(mem_avail_req, count);
|
|---|
| 958 | else
|
|---|
| 959 | mem_avail_req = count;
|
|---|
| 960 |
|
|---|
| 961 | size_t gen = mem_avail_gen;
|
|---|
| 962 |
|
|---|
| 963 | while (gen == mem_avail_gen)
|
|---|
| 964 | condvar_wait(&mem_avail_cv, &mem_avail_lock);
|
|---|
| 965 |
|
|---|
| 966 | irq_spinlock_unlock(&mem_avail_lock, true);
|
|---|
| 967 |
|
|---|
| 968 | #ifdef CONFIG_DEBUG
|
|---|
| 969 | log(LF_OTHER, LVL_DEBUG, "Thread %" PRIu64 " woken up.",
|
|---|
| 970 | THREAD->tid);
|
|---|
| 971 | #endif
|
|---|
| 972 |
|
|---|
| 973 | goto loop;
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 | pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
|
|---|
| 977 | frame_constraint) + zones.info[znum].base;
|
|---|
| 978 |
|
|---|
| 979 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 980 |
|
|---|
| 981 | if (pzone)
|
|---|
| 982 | *pzone = znum;
|
|---|
| 983 |
|
|---|
| 984 | return PFN2ADDR(pfn);
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
|
|---|
| 988 | {
|
|---|
| 989 | return frame_alloc_generic(count, flags, constraint, NULL);
|
|---|
| 990 | }
|
|---|
| 991 |
|
|---|
| 992 | /** Free frames of physical memory.
|
|---|
| 993 | *
|
|---|
| 994 | * Find respective frame structures for supplied physical frames.
|
|---|
| 995 | * Decrement each frame reference count. If it drops to zero, mark
|
|---|
| 996 | * the frames as available.
|
|---|
| 997 | *
|
|---|
| 998 | * @param start Physical Address of the first frame to be freed.
|
|---|
| 999 | * @param count Number of frames to free.
|
|---|
| 1000 | * @param flags Flags to control memory reservation.
|
|---|
| 1001 | *
|
|---|
| 1002 | */
|
|---|
| 1003 | void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
|
|---|
| 1004 | {
|
|---|
| 1005 | size_t freed = 0;
|
|---|
| 1006 |
|
|---|
| 1007 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1008 |
|
|---|
| 1009 | for (size_t i = 0; i < count; i++) {
|
|---|
| 1010 | /*
|
|---|
| 1011 | * First, find host frame zone for addr.
|
|---|
| 1012 | */
|
|---|
| 1013 | pfn_t pfn = ADDR2PFN(start) + i;
|
|---|
| 1014 | size_t znum = find_zone(pfn, 1, 0);
|
|---|
| 1015 |
|
|---|
| 1016 | assert(znum != (size_t) -1);
|
|---|
| 1017 |
|
|---|
| 1018 | freed += zone_frame_free(&zones.info[znum],
|
|---|
| 1019 | pfn - zones.info[znum].base);
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1023 |
|
|---|
| 1024 | /* Signal that some memory has been freed. */
|
|---|
| 1025 |
|
|---|
| 1026 | /* Disabled interrupts needed to prevent deadlock with TLB shootdown. */
|
|---|
| 1027 | irq_spinlock_lock(&mem_avail_lock, true);
|
|---|
| 1028 |
|
|---|
| 1029 | if (mem_avail_req > 0)
|
|---|
| 1030 | mem_avail_req -= min(mem_avail_req, freed);
|
|---|
| 1031 |
|
|---|
| 1032 | if (mem_avail_req == 0) {
|
|---|
| 1033 | mem_avail_gen++;
|
|---|
| 1034 | condvar_broadcast(&mem_avail_cv);
|
|---|
| 1035 | }
|
|---|
| 1036 |
|
|---|
| 1037 | irq_spinlock_unlock(&mem_avail_lock, true);
|
|---|
| 1038 |
|
|---|
| 1039 | if (!(flags & FRAME_NO_RESERVE))
|
|---|
| 1040 | reserve_free(freed);
|
|---|
| 1041 | }
|
|---|
| 1042 |
|
|---|
| 1043 | void frame_free(uintptr_t frame, size_t count)
|
|---|
| 1044 | {
|
|---|
| 1045 | frame_free_generic(frame, count, 0);
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | void frame_free_noreserve(uintptr_t frame, size_t count)
|
|---|
| 1049 | {
|
|---|
| 1050 | frame_free_generic(frame, count, FRAME_NO_RESERVE);
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | /** Add reference to frame.
|
|---|
| 1054 | *
|
|---|
| 1055 | * Find respective frame structure for supplied PFN and
|
|---|
| 1056 | * increment frame reference count.
|
|---|
| 1057 | *
|
|---|
| 1058 | * @param pfn Frame number of the frame to be freed.
|
|---|
| 1059 | *
|
|---|
| 1060 | */
|
|---|
| 1061 | _NO_TRACE void frame_reference_add(pfn_t pfn)
|
|---|
| 1062 | {
|
|---|
| 1063 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1064 |
|
|---|
| 1065 | /*
|
|---|
| 1066 | * First, find host frame zone for addr.
|
|---|
| 1067 | */
|
|---|
| 1068 | size_t znum = find_zone(pfn, 1, 0);
|
|---|
| 1069 |
|
|---|
| 1070 | assert(znum != (size_t) -1);
|
|---|
| 1071 |
|
|---|
| 1072 | zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
|
|---|
| 1073 |
|
|---|
| 1074 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1075 | }
|
|---|
| 1076 |
|
|---|
| 1077 | /** Mark given range unavailable in frame zones.
|
|---|
| 1078 | *
|
|---|
| 1079 | */
|
|---|
| 1080 | _NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
|
|---|
| 1081 | {
|
|---|
| 1082 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1083 |
|
|---|
| 1084 | for (size_t i = 0; i < count; i++) {
|
|---|
| 1085 | size_t znum = find_zone(start + i, 1, 0);
|
|---|
| 1086 |
|
|---|
| 1087 | if (znum == (size_t) -1) /* PFN not found */
|
|---|
| 1088 | continue;
|
|---|
| 1089 |
|
|---|
| 1090 | zone_mark_unavailable(&zones.info[znum],
|
|---|
| 1091 | start + i - zones.info[znum].base);
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1095 | }
|
|---|
| 1096 |
|
|---|
| 1097 | /** Initialize physical memory management.
|
|---|
| 1098 | *
|
|---|
| 1099 | */
|
|---|
| 1100 | void frame_init(void)
|
|---|
| 1101 | {
|
|---|
| 1102 | /* Tell the architecture to create some memory */
|
|---|
| 1103 | frame_low_arch_init();
|
|---|
| 1104 |
|
|---|
| 1105 | if (config.cpu_active == 1) {
|
|---|
| 1106 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
|
|---|
| 1107 | SIZE2FRAMES(config.kernel_size));
|
|---|
| 1108 |
|
|---|
| 1109 | for (size_t i = 0; i < init.cnt; i++)
|
|---|
| 1110 | frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
|
|---|
| 1111 | SIZE2FRAMES(init.tasks[i].size));
|
|---|
| 1112 |
|
|---|
| 1113 | if (ballocs.size)
|
|---|
| 1114 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
|
|---|
| 1115 | SIZE2FRAMES(ballocs.size));
|
|---|
| 1116 |
|
|---|
| 1117 | /*
|
|---|
| 1118 | * Blacklist first frame, as allocating NULL would
|
|---|
| 1119 | * fail in some places
|
|---|
| 1120 | */
|
|---|
| 1121 | frame_mark_unavailable(0, 1);
|
|---|
| 1122 | }
|
|---|
| 1123 |
|
|---|
| 1124 | frame_high_arch_init();
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | /** Adjust bounds of physical memory region according to low/high memory split.
|
|---|
| 1128 | *
|
|---|
| 1129 | * @param low[in] If true, the adjustment is performed to make the region
|
|---|
| 1130 | * fit in the low memory. Otherwise the adjustment is
|
|---|
| 1131 | * performed to make the region fit in the high memory.
|
|---|
| 1132 | * @param basep[inout] Pointer to a variable which contains the region's base
|
|---|
| 1133 | * address and which may receive the adjusted base address.
|
|---|
| 1134 | * @param sizep[inout] Pointer to a variable which contains the region's size
|
|---|
| 1135 | * and which may receive the adjusted size.
|
|---|
| 1136 | *
|
|---|
| 1137 | * @return True if the region still exists even after the adjustment.
|
|---|
| 1138 | * @return False otherwise.
|
|---|
| 1139 | *
|
|---|
| 1140 | */
|
|---|
| 1141 | bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
|
|---|
| 1142 | {
|
|---|
| 1143 | uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
|
|---|
| 1144 |
|
|---|
| 1145 | if (low) {
|
|---|
| 1146 | if (*basep > limit)
|
|---|
| 1147 | return false;
|
|---|
| 1148 |
|
|---|
| 1149 | if (*basep + *sizep > limit)
|
|---|
| 1150 | *sizep = limit - *basep;
|
|---|
| 1151 | } else {
|
|---|
| 1152 | if (*basep + *sizep <= limit)
|
|---|
| 1153 | return false;
|
|---|
| 1154 |
|
|---|
| 1155 | if (*basep <= limit) {
|
|---|
| 1156 | *sizep -= limit - *basep;
|
|---|
| 1157 | *basep = limit;
|
|---|
| 1158 | }
|
|---|
| 1159 | }
|
|---|
| 1160 |
|
|---|
| 1161 | return true;
|
|---|
| 1162 | }
|
|---|
| 1163 |
|
|---|
| 1164 | /** Return total size of all zones.
|
|---|
| 1165 | *
|
|---|
| 1166 | */
|
|---|
| 1167 | uint64_t zones_total_size(void)
|
|---|
| 1168 | {
|
|---|
| 1169 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1170 |
|
|---|
| 1171 | uint64_t total = 0;
|
|---|
| 1172 |
|
|---|
| 1173 | for (size_t i = 0; i < zones.count; i++)
|
|---|
| 1174 | total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
|---|
| 1175 |
|
|---|
| 1176 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1177 |
|
|---|
| 1178 | return total;
|
|---|
| 1179 | }
|
|---|
| 1180 |
|
|---|
| 1181 | void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
|
|---|
| 1182 | uint64_t *free)
|
|---|
| 1183 | {
|
|---|
| 1184 | assert(total != NULL);
|
|---|
| 1185 | assert(unavail != NULL);
|
|---|
| 1186 | assert(busy != NULL);
|
|---|
| 1187 | assert(free != NULL);
|
|---|
| 1188 |
|
|---|
| 1189 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1190 |
|
|---|
| 1191 | *total = 0;
|
|---|
| 1192 | *unavail = 0;
|
|---|
| 1193 | *busy = 0;
|
|---|
| 1194 | *free = 0;
|
|---|
| 1195 |
|
|---|
| 1196 | for (size_t i = 0; i < zones.count; i++) {
|
|---|
| 1197 | *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
|---|
| 1198 |
|
|---|
| 1199 | if (zones.info[i].flags & ZONE_AVAILABLE) {
|
|---|
| 1200 | *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
|
|---|
| 1201 | *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
|
|---|
| 1202 | } else
|
|---|
| 1203 | *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
|---|
| 1204 | }
|
|---|
| 1205 |
|
|---|
| 1206 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | /** Prints list of zones.
|
|---|
| 1210 | *
|
|---|
| 1211 | */
|
|---|
| 1212 | void zones_print_list(void)
|
|---|
| 1213 | {
|
|---|
| 1214 | #ifdef __32_BITS__
|
|---|
| 1215 | printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
|---|
| 1216 | #endif
|
|---|
| 1217 |
|
|---|
| 1218 | #ifdef __64_BITS__
|
|---|
| 1219 | printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
|---|
| 1220 | #endif
|
|---|
| 1221 |
|
|---|
| 1222 | /*
|
|---|
| 1223 | * Because printing may require allocation of memory, we may not hold
|
|---|
| 1224 | * the frame allocator locks when printing zone statistics. Therefore,
|
|---|
| 1225 | * we simply gather the statistics under the protection of the locks and
|
|---|
| 1226 | * print the statistics when the locks have been released.
|
|---|
| 1227 | *
|
|---|
| 1228 | * When someone adds/removes zones while we are printing the statistics,
|
|---|
| 1229 | * we may end up with inaccurate output (e.g. a zone being skipped from
|
|---|
| 1230 | * the listing).
|
|---|
| 1231 | */
|
|---|
| 1232 |
|
|---|
| 1233 | size_t free_lowmem = 0;
|
|---|
| 1234 | size_t free_highmem = 0;
|
|---|
| 1235 | size_t free_highprio = 0;
|
|---|
| 1236 |
|
|---|
| 1237 | for (size_t i = 0; ; i++) {
|
|---|
| 1238 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1239 |
|
|---|
| 1240 | if (i >= zones.count) {
|
|---|
| 1241 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1242 | break;
|
|---|
| 1243 | }
|
|---|
| 1244 |
|
|---|
| 1245 | pfn_t fbase = zones.info[i].base;
|
|---|
| 1246 | uintptr_t base = PFN2ADDR(fbase);
|
|---|
| 1247 | size_t count = zones.info[i].count;
|
|---|
| 1248 | zone_flags_t flags = zones.info[i].flags;
|
|---|
| 1249 | size_t free_count = zones.info[i].free_count;
|
|---|
| 1250 | size_t busy_count = zones.info[i].busy_count;
|
|---|
| 1251 |
|
|---|
| 1252 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
|---|
| 1253 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
|---|
| 1254 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
|---|
| 1255 | bool highprio = is_high_priority(fbase, count);
|
|---|
| 1256 |
|
|---|
| 1257 | if (available) {
|
|---|
| 1258 | if (lowmem)
|
|---|
| 1259 | free_lowmem += free_count;
|
|---|
| 1260 |
|
|---|
| 1261 | if (highmem)
|
|---|
| 1262 | free_highmem += free_count;
|
|---|
| 1263 |
|
|---|
| 1264 | if (highprio) {
|
|---|
| 1265 | free_highprio += free_count;
|
|---|
| 1266 | } else {
|
|---|
| 1267 | /*
|
|---|
| 1268 | * Walk all frames of the zone and examine
|
|---|
| 1269 | * all high priority memory to get accurate
|
|---|
| 1270 | * statistics.
|
|---|
| 1271 | */
|
|---|
| 1272 |
|
|---|
| 1273 | for (size_t index = 0; index < count; index++) {
|
|---|
| 1274 | if (is_high_priority(fbase + index, 0)) {
|
|---|
| 1275 | if (!bitmap_get(&zones.info[i].bitmap, index))
|
|---|
| 1276 | free_highprio++;
|
|---|
| 1277 | } else
|
|---|
| 1278 | break;
|
|---|
| 1279 | }
|
|---|
| 1280 | }
|
|---|
| 1281 | }
|
|---|
| 1282 |
|
|---|
| 1283 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1284 |
|
|---|
| 1285 | printf("%-4zu", i);
|
|---|
| 1286 |
|
|---|
| 1287 | #ifdef __32_BITS__
|
|---|
| 1288 | printf(" %p", (void *) base);
|
|---|
| 1289 | #endif
|
|---|
| 1290 |
|
|---|
| 1291 | #ifdef __64_BITS__
|
|---|
| 1292 | printf(" %p", (void *) base);
|
|---|
| 1293 | #endif
|
|---|
| 1294 |
|
|---|
| 1295 | printf(" %12zu %c%c%c%c%c ", count,
|
|---|
| 1296 | available ? 'A' : '-',
|
|---|
| 1297 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
|---|
| 1298 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
|---|
| 1299 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
|---|
| 1300 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
|---|
| 1301 |
|
|---|
| 1302 | if (available)
|
|---|
| 1303 | printf("%14zu %14zu",
|
|---|
| 1304 | free_count, busy_count);
|
|---|
| 1305 |
|
|---|
| 1306 | printf("\n");
|
|---|
| 1307 | }
|
|---|
| 1308 |
|
|---|
| 1309 | printf("\n");
|
|---|
| 1310 |
|
|---|
| 1311 | uint64_t size;
|
|---|
| 1312 | const char *size_suffix;
|
|---|
| 1313 |
|
|---|
| 1314 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
|---|
| 1315 | false);
|
|---|
| 1316 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1317 | free_lowmem, size, size_suffix);
|
|---|
| 1318 |
|
|---|
| 1319 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
|---|
| 1320 | false);
|
|---|
| 1321 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1322 | free_highmem, size, size_suffix);
|
|---|
| 1323 |
|
|---|
| 1324 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
|---|
| 1325 | false);
|
|---|
| 1326 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1327 | free_highprio, size, size_suffix);
|
|---|
| 1328 | }
|
|---|
| 1329 |
|
|---|
| 1330 | /** Prints zone details.
|
|---|
| 1331 | *
|
|---|
| 1332 | * @param num Zone base address or zone number.
|
|---|
| 1333 | *
|
|---|
| 1334 | */
|
|---|
| 1335 | void zone_print_one(size_t num)
|
|---|
| 1336 | {
|
|---|
| 1337 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 1338 | size_t znum = (size_t) -1;
|
|---|
| 1339 |
|
|---|
| 1340 | for (size_t i = 0; i < zones.count; i++) {
|
|---|
| 1341 | if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
|
|---|
| 1342 | znum = i;
|
|---|
| 1343 | break;
|
|---|
| 1344 | }
|
|---|
| 1345 | }
|
|---|
| 1346 |
|
|---|
| 1347 | if (znum == (size_t) -1) {
|
|---|
| 1348 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1349 | printf("Zone not found.\n");
|
|---|
| 1350 | return;
|
|---|
| 1351 | }
|
|---|
| 1352 |
|
|---|
| 1353 | size_t free_lowmem = 0;
|
|---|
| 1354 | size_t free_highmem = 0;
|
|---|
| 1355 | size_t free_highprio = 0;
|
|---|
| 1356 |
|
|---|
| 1357 | pfn_t fbase = zones.info[znum].base;
|
|---|
| 1358 | uintptr_t base = PFN2ADDR(fbase);
|
|---|
| 1359 | zone_flags_t flags = zones.info[znum].flags;
|
|---|
| 1360 | size_t count = zones.info[znum].count;
|
|---|
| 1361 | size_t free_count = zones.info[znum].free_count;
|
|---|
| 1362 | size_t busy_count = zones.info[znum].busy_count;
|
|---|
| 1363 |
|
|---|
| 1364 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
|---|
| 1365 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
|---|
| 1366 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
|---|
| 1367 | bool highprio = is_high_priority(fbase, count);
|
|---|
| 1368 |
|
|---|
| 1369 | if (available) {
|
|---|
| 1370 | if (lowmem)
|
|---|
| 1371 | free_lowmem = free_count;
|
|---|
| 1372 |
|
|---|
| 1373 | if (highmem)
|
|---|
| 1374 | free_highmem = free_count;
|
|---|
| 1375 |
|
|---|
| 1376 | if (highprio) {
|
|---|
| 1377 | free_highprio = free_count;
|
|---|
| 1378 | } else {
|
|---|
| 1379 | /*
|
|---|
| 1380 | * Walk all frames of the zone and examine
|
|---|
| 1381 | * all high priority memory to get accurate
|
|---|
| 1382 | * statistics.
|
|---|
| 1383 | */
|
|---|
| 1384 |
|
|---|
| 1385 | for (size_t index = 0; index < count; index++) {
|
|---|
| 1386 | if (is_high_priority(fbase + index, 0)) {
|
|---|
| 1387 | if (!bitmap_get(&zones.info[znum].bitmap, index))
|
|---|
| 1388 | free_highprio++;
|
|---|
| 1389 | } else
|
|---|
| 1390 | break;
|
|---|
| 1391 | }
|
|---|
| 1392 | }
|
|---|
| 1393 | }
|
|---|
| 1394 |
|
|---|
| 1395 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 1396 |
|
|---|
| 1397 | uint64_t size;
|
|---|
| 1398 | const char *size_suffix;
|
|---|
| 1399 |
|
|---|
| 1400 | bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
|
|---|
| 1401 |
|
|---|
| 1402 | printf("Zone number: %zu\n", znum);
|
|---|
| 1403 | printf("Zone base address: %p\n", (void *) base);
|
|---|
| 1404 | printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
|
|---|
| 1405 | size, size_suffix);
|
|---|
| 1406 | printf("Zone flags: %c%c%c%c%c\n",
|
|---|
| 1407 | available ? 'A' : '-',
|
|---|
| 1408 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
|---|
| 1409 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
|---|
| 1410 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
|---|
| 1411 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
|---|
| 1412 |
|
|---|
| 1413 | if (available) {
|
|---|
| 1414 | bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
|
|---|
| 1415 | false);
|
|---|
| 1416 | printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1417 | busy_count, size, size_suffix);
|
|---|
| 1418 |
|
|---|
| 1419 | bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
|
|---|
| 1420 | false);
|
|---|
| 1421 | printf("Available space: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1422 | free_count, size, size_suffix);
|
|---|
| 1423 |
|
|---|
| 1424 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
|---|
| 1425 | false);
|
|---|
| 1426 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1427 | free_lowmem, size, size_suffix);
|
|---|
| 1428 |
|
|---|
| 1429 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
|---|
| 1430 | false);
|
|---|
| 1431 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1432 | free_highmem, size, size_suffix);
|
|---|
| 1433 |
|
|---|
| 1434 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
|---|
| 1435 | false);
|
|---|
| 1436 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
|---|
| 1437 | free_highprio, size, size_suffix);
|
|---|
| 1438 | }
|
|---|
| 1439 | }
|
|---|
| 1440 |
|
|---|
| 1441 | /** @}
|
|---|
| 1442 | */
|
|---|