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