[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2005 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2005 Sergey Bondari
|
---|
[5f0f29ce] | 4 | * Copyright (c) 2009 Martin Decky
|
---|
[f761f1eb] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[174156fd] | 31 | /** @addtogroup kernel_generic_mm
|
---|
[b45c443] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[9179d0a] | 35 | /**
|
---|
[b45c443] | 36 | * @file
|
---|
[5f0f29ce] | 37 | * @brief Physical frame allocator.
|
---|
[9179d0a] | 38 | *
|
---|
| 39 | * This file contains the physical frame allocator and memory zone management.
|
---|
[b0c2075] | 40 | * The frame allocator is built on top of the two-level bitmap structure.
|
---|
[9179d0a] | 41 | *
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[d99c1d2] | 44 | #include <typedefs.h>
|
---|
[f761f1eb] | 45 | #include <mm/frame.h>
|
---|
[89bcb520] | 46 | #include <mm/reserve.h>
|
---|
[20d50a1] | 47 | #include <mm/as.h>
|
---|
[f761f1eb] | 48 | #include <panic.h>
|
---|
[63e27ef] | 49 | #include <assert.h>
|
---|
[5c9a08b] | 50 | #include <adt/list.h>
|
---|
[1a1744e] | 51 | #include <synch/mutex.h>
|
---|
| 52 | #include <synch/condvar.h>
|
---|
[18e0a6c] | 53 | #include <arch/asm.h>
|
---|
[9c0a9b3] | 54 | #include <arch.h>
|
---|
[bab75df6] | 55 | #include <stdio.h>
|
---|
[b2fa1204] | 56 | #include <log.h>
|
---|
[9ebc238] | 57 | #include <align.h>
|
---|
[085d973] | 58 | #include <mm/slab.h>
|
---|
[bb68433] | 59 | #include <bitops.h>
|
---|
[93165be] | 60 | #include <macros.h>
|
---|
[d630139] | 61 | #include <config.h>
|
---|
[933cadf] | 62 | #include <str.h>
|
---|
[1066041] | 63 | #include <proc/thread.h> /* THREAD */
|
---|
[18e0a6c] | 64 |
|
---|
[597fa24] | 65 | zones_t zones = {
|
---|
| 66 | .count = 0,
|
---|
| 67 | .lock = IRQ_SPINLOCK_INITIALIZER("frame.zones.lock"),
|
---|
| 68 | };
|
---|
[f761f1eb] | 69 |
|
---|
[1a1744e] | 70 | /*
|
---|
| 71 | * Synchronization primitives used to sleep when there is no memory
|
---|
| 72 | * available.
|
---|
| 73 | */
|
---|
[597fa24] | 74 | static MUTEX_INITIALIZE(mem_avail_mtx, MUTEX_ACTIVE);
|
---|
| 75 | static CONDVAR_INITIALIZE(mem_avail_cv);
|
---|
[da1bafb] | 76 | static size_t mem_avail_req = 0; /**< Number of frames requested. */
|
---|
| 77 | static size_t mem_avail_gen = 0; /**< Generation counter. */
|
---|
[a294ad0] | 78 |
|
---|
[deaf8d5] | 79 | /** Initialize frame structure.
|
---|
[84dd253] | 80 | *
|
---|
[5f0f29ce] | 81 | * @param frame Frame structure to be initialized.
|
---|
| 82 | *
|
---|
[f761f1eb] | 83 | */
|
---|
[8df5f20] | 84 | _NO_TRACE static void frame_initialize(frame_t *frame)
|
---|
[f761f1eb] | 85 | {
|
---|
[b0c2075] | 86 | frame->refcount = 0;
|
---|
| 87 | frame->parent = NULL;
|
---|
[f761f1eb] | 88 | }
|
---|
| 89 |
|
---|
[d1582b50] | 90 | /*
|
---|
| 91 | * Zones functions
|
---|
| 92 | */
|
---|
[085d973] | 93 |
|
---|
[deaf8d5] | 94 | /** Insert-sort zone into zones list.
|
---|
[bb68433] | 95 | *
|
---|
[5f0f29ce] | 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 | *
|
---|
[84dd253] | 104 | */
|
---|
[8df5f20] | 105 | _NO_TRACE static size_t zones_insert_zone(pfn_t base, size_t count,
|
---|
[e2650d3] | 106 | zone_flags_t flags)
|
---|
[f761f1eb] | 107 | {
|
---|
[71eef11] | 108 | if (zones.count + 1 == ZONES_MAX) {
|
---|
[b2fa1204] | 109 | log(LF_OTHER, LVL_ERROR, "Maximum zone count %u exceeded!",
|
---|
| 110 | ZONES_MAX);
|
---|
[98000fb] | 111 | return (size_t) -1;
|
---|
[71eef11] | 112 | }
|
---|
[a35b458] | 113 |
|
---|
[98000fb] | 114 | size_t i;
|
---|
[b6b576c] | 115 | for (i = 0; i < zones.count; i++) {
|
---|
[5f0f29ce] | 116 | /* Check for overlap */
|
---|
[e2650d3] | 117 | if (overlaps(zones.info[i].base, zones.info[i].count,
|
---|
| 118 | base, count)) {
|
---|
[a35b458] | 119 |
|
---|
[e2650d3] | 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 | */
|
---|
[a35b458] | 126 |
|
---|
[e2650d3] | 127 | if ((zones.info[i].flags != flags) ||
|
---|
| 128 | (!iswithin(zones.info[i].base, zones.info[i].count,
|
---|
| 129 | base, count))) {
|
---|
[b2fa1204] | 130 | log(LF_OTHER, LVL_WARN,
|
---|
| 131 | "Zone (%p, %p) overlaps "
|
---|
| 132 | "with previous zone (%p %p)!",
|
---|
[7e752b2] | 133 | (void *) PFN2ADDR(base), (void *) PFN2ADDR(count),
|
---|
| 134 | (void *) PFN2ADDR(zones.info[i].base),
|
---|
| 135 | (void *) PFN2ADDR(zones.info[i].count));
|
---|
[e2650d3] | 136 | }
|
---|
[a35b458] | 137 |
|
---|
[98000fb] | 138 | return (size_t) -1;
|
---|
[085d973] | 139 | }
|
---|
[5f0f29ce] | 140 | if (base < zones.info[i].base)
|
---|
[bb68433] | 141 | break;
|
---|
[085d973] | 142 | }
|
---|
[a35b458] | 143 |
|
---|
[bb68433] | 144 | /* Move other zones up */
|
---|
[b0c2075] | 145 | for (size_t j = zones.count; j > i; j--)
|
---|
[e49e234] | 146 | zones.info[j] = zones.info[j - 1];
|
---|
[a35b458] | 147 |
|
---|
[bb68433] | 148 | zones.count++;
|
---|
[a35b458] | 149 |
|
---|
[bb68433] | 150 | return i;
|
---|
[f761f1eb] | 151 | }
|
---|
[fcacfb7] | 152 |
|
---|
[5f0f29ce] | 153 | /** Get total available frames.
|
---|
| 154 | *
|
---|
| 155 | * Assume interrupts are disabled and zones lock is
|
---|
| 156 | * locked.
|
---|
[71eef11] | 157 | *
|
---|
[5f0f29ce] | 158 | * @return Total number of available frames.
|
---|
[71eef11] | 159 | *
|
---|
[eef75f6] | 160 | */
|
---|
[8df5f20] | 161 | _NO_TRACE static size_t frame_total_free_get_internal(void)
|
---|
[085d973] | 162 | {
|
---|
[98000fb] | 163 | size_t total = 0;
|
---|
| 164 | size_t i;
|
---|
[8d308b9] | 165 |
|
---|
[5f0f29ce] | 166 | for (i = 0; i < zones.count; i++)
|
---|
| 167 | total += zones.info[i].free_count;
|
---|
[a35b458] | 168 |
|
---|
[5f0f29ce] | 169 | return total;
|
---|
| 170 | }
|
---|
[8d308b9] | 171 |
|
---|
[8df5f20] | 172 | _NO_TRACE size_t frame_total_free_get(void)
|
---|
[8d308b9] | 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 |
|
---|
[e49e234] | 183 | /** Find a zone with a given frames.
|
---|
[5f0f29ce] | 184 | *
|
---|
| 185 | * Assume interrupts are disabled and zones lock is
|
---|
| 186 | * locked.
|
---|
| 187 | *
|
---|
| 188 | * @param frame Frame number contained in zone.
|
---|
[e49e234] | 189 | * @param count Number of frames to look for.
|
---|
[5f0f29ce] | 190 | * @param hint Used as zone hint.
|
---|
| 191 | *
|
---|
| 192 | * @return Zone index or -1 if not found.
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
[8df5f20] | 195 | _NO_TRACE size_t find_zone(pfn_t frame, size_t count, size_t hint)
|
---|
[5f0f29ce] | 196 | {
|
---|
[6c441cf8] | 197 | if (hint >= zones.count)
|
---|
[085d973] | 198 | hint = 0;
|
---|
[a35b458] | 199 |
|
---|
[98000fb] | 200 | size_t i = hint;
|
---|
[085d973] | 201 | do {
|
---|
[3bacee1] | 202 | if ((zones.info[i].base <= frame) &&
|
---|
| 203 | (zones.info[i].base + zones.info[i].count >= frame + count))
|
---|
[5f0f29ce] | 204 | return i;
|
---|
[a35b458] | 205 |
|
---|
[085d973] | 206 | i++;
|
---|
| 207 | if (i >= zones.count)
|
---|
| 208 | i = 0;
|
---|
[a35b458] | 209 |
|
---|
[5f7a0ef] | 210 | } while (i != hint);
|
---|
[a35b458] | 211 |
|
---|
[98000fb] | 212 | return (size_t) -1;
|
---|
[085d973] | 213 | }
|
---|
[328f2934] | 214 |
|
---|
[b0c2075] | 215 | /** @return True if zone can allocate specified number of frames */
|
---|
[8df5f20] | 216 | _NO_TRACE static bool zone_can_alloc(zone_t *zone, size_t count,
|
---|
[b0c2075] | 217 | pfn_t constraint)
|
---|
[bb68433] | 218 | {
|
---|
[b0c2075] | 219 | /*
|
---|
| 220 | * The function bitmap_allocate_range() does not modify
|
---|
| 221 | * the bitmap if the last argument is NULL.
|
---|
| 222 | */
|
---|
[a35b458] | 223 |
|
---|
[e6c4b94] | 224 | return ((zone->flags & ZONE_AVAILABLE) &&
|
---|
[b0c2075] | 225 | bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
---|
[f72906c] | 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 | */
|
---|
[8df5f20] | 244 | _NO_TRACE static size_t find_free_zone_all(size_t count, zone_flags_t flags,
|
---|
[f72906c] | 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;
|
---|
[a35b458] | 249 |
|
---|
[f72906c] | 250 | /* Check whether the zone meets the search criteria. */
|
---|
| 251 | if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
|
---|
| 252 | continue;
|
---|
[a35b458] | 253 |
|
---|
[f72906c] | 254 | /* Check if the zone can satisfy the allocation request. */
|
---|
| 255 | if (zone_can_alloc(&zones.info[i], count, constraint))
|
---|
| 256 | return i;
|
---|
| 257 | }
|
---|
[a35b458] | 258 |
|
---|
[f72906c] | 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 | */
|
---|
[8df5f20] | 270 | _NO_TRACE static bool is_high_priority(pfn_t base, size_t count)
|
---|
[f72906c] | 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 | */
|
---|
[8df5f20] | 290 | _NO_TRACE static size_t find_free_zone_lowprio(size_t count, zone_flags_t flags,
|
---|
[f72906c] | 291 | pfn_t constraint, size_t hint)
|
---|
[1b20da0] | 292 | {
|
---|
[f72906c] | 293 | for (size_t pos = 0; pos < zones.count; pos++) {
|
---|
| 294 | size_t i = (pos + hint) % zones.count;
|
---|
[a35b458] | 295 |
|
---|
[f72906c] | 296 | /* Skip zones containing only high-priority memory. */
|
---|
| 297 | if (is_high_priority(zones.info[i].base, zones.info[i].count))
|
---|
| 298 | continue;
|
---|
[a35b458] | 299 |
|
---|
[f72906c] | 300 | /* Check whether the zone meets the search criteria. */
|
---|
| 301 | if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
|
---|
| 302 | continue;
|
---|
[a35b458] | 303 |
|
---|
[f72906c] | 304 | /* Check if the zone can satisfy the allocation request. */
|
---|
| 305 | if (zone_can_alloc(&zones.info[i], count, constraint))
|
---|
| 306 | return i;
|
---|
| 307 | }
|
---|
[a35b458] | 308 |
|
---|
[f72906c] | 309 | return (size_t) -1;
|
---|
[bb68433] | 310 | }
|
---|
| 311 |
|
---|
[b0c2075] | 312 | /** Find a zone that can allocate specified number of frames
|
---|
[5f0f29ce] | 313 | *
|
---|
| 314 | * Assume interrupts are disabled and zones lock is
|
---|
| 315 | * locked.
|
---|
[fcacfb7] | 316 | *
|
---|
[b0c2075] | 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.
|
---|
[f72906c] | 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.
|
---|
[fcacfb7] | 325 | *
|
---|
| 326 | */
|
---|
[8df5f20] | 327 | _NO_TRACE static size_t find_free_zone(size_t count, zone_flags_t flags,
|
---|
[b0c2075] | 328 | pfn_t constraint, size_t hint)
|
---|
[fcacfb7] | 329 | {
|
---|
[085d973] | 330 | if (hint >= zones.count)
|
---|
| 331 | hint = 0;
|
---|
[a35b458] | 332 |
|
---|
[f72906c] | 333 | /*
|
---|
| 334 | * Prefer zones with low-priority memory over
|
---|
| 335 | * zones with high-priority memory.
|
---|
| 336 | */
|
---|
[a35b458] | 337 |
|
---|
[f72906c] | 338 | size_t znum = find_free_zone_lowprio(count, flags, constraint, hint);
|
---|
| 339 | if (znum != (size_t) -1)
|
---|
| 340 | return znum;
|
---|
[a35b458] | 341 |
|
---|
[f72906c] | 342 | /* Take all zones into account */
|
---|
| 343 | return find_free_zone_all(count, flags, constraint, hint);
|
---|
[fcacfb7] | 344 | }
|
---|
| 345 |
|
---|
[d1582b50] | 346 | /*
|
---|
| 347 | * Zone functions
|
---|
| 348 | */
|
---|
[6e8b3c8] | 349 |
|
---|
[b0c2075] | 350 | /** Return frame from zone. */
|
---|
[8df5f20] | 351 | _NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t index)
|
---|
[bb68433] | 352 | {
|
---|
[63e27ef] | 353 | assert(index < zone->count);
|
---|
[a35b458] | 354 |
|
---|
[b0c2075] | 355 | return &zone->frames[index];
|
---|
[6e8b3c8] | 356 | }
|
---|
| 357 |
|
---|
[deaf8d5] | 358 | /** Allocate frame in particular zone.
|
---|
[085d973] | 359 | *
|
---|
[5f0f29ce] | 360 | * Assume zone is locked and is available for allocation.
|
---|
[9a68b34d] | 361 | * Panics if allocation is impossible.
|
---|
| 362 | *
|
---|
[b0c2075] | 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.
|
---|
[085d973] | 367 | *
|
---|
[5f0f29ce] | 368 | * @return Frame index in zone.
|
---|
[9a68b34d] | 369 | *
|
---|
[085d973] | 370 | */
|
---|
[8df5f20] | 371 | _NO_TRACE static size_t zone_frame_alloc(zone_t *zone, size_t count,
|
---|
[b0c2075] | 372 | pfn_t constraint)
|
---|
[085d973] | 373 | {
|
---|
[63e27ef] | 374 | assert(zone->flags & ZONE_AVAILABLE);
|
---|
[81d3612] | 375 | assert(zone->free_count >= count);
|
---|
[a35b458] | 376 |
|
---|
[b0c2075] | 377 | /* Allocate frames from zone */
|
---|
[566da7f8] | 378 | size_t index = (size_t) -1;
|
---|
[b0c2075] | 379 | int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base,
|
---|
[f72906c] | 380 | FRAME_LOWPRIO, constraint, &index);
|
---|
[a35b458] | 381 |
|
---|
[0705fc5] | 382 | (void) avail;
|
---|
[63e27ef] | 383 | assert(avail);
|
---|
| 384 | assert(index != (size_t) -1);
|
---|
[a35b458] | 385 |
|
---|
[b0c2075] | 386 | /* Update frame reference count */
|
---|
| 387 | for (size_t i = 0; i < count; i++) {
|
---|
| 388 | frame_t *frame = zone_get_frame(zone, index + i);
|
---|
[a35b458] | 389 |
|
---|
[63e27ef] | 390 | assert(frame->refcount == 0);
|
---|
[b0c2075] | 391 | frame->refcount = 1;
|
---|
| 392 | }
|
---|
[a35b458] | 393 |
|
---|
[085d973] | 394 | /* Update zone information. */
|
---|
[b0c2075] | 395 | zone->free_count -= count;
|
---|
| 396 | zone->busy_count += count;
|
---|
[a35b458] | 397 |
|
---|
[b0c2075] | 398 | return index;
|
---|
[085d973] | 399 | }
|
---|
| 400 |
|
---|
[deaf8d5] | 401 | /** Free frame from zone.
|
---|
[085d973] | 402 | *
|
---|
[5f0f29ce] | 403 | * Assume zone is locked and is available for deallocation.
|
---|
| 404 | *
|
---|
[b0c2075] | 405 | * @param zone Pointer to zone from which the frame is to be freed.
|
---|
| 406 | * @param index Frame index relative to zone.
|
---|
[eb3d379] | 407 | *
|
---|
[b0c2075] | 408 | * @return Number of freed frames.
|
---|
[89bcb520] | 409 | *
|
---|
[085d973] | 410 | */
|
---|
[8df5f20] | 411 | _NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t index)
|
---|
[085d973] | 412 | {
|
---|
[63e27ef] | 413 | assert(zone->flags & ZONE_AVAILABLE);
|
---|
[a35b458] | 414 |
|
---|
[b0c2075] | 415 | frame_t *frame = zone_get_frame(zone, index);
|
---|
[63e27ef] | 416 | assert(frame->refcount > 0);
|
---|
[a35b458] | 417 |
|
---|
[085d973] | 418 | if (!--frame->refcount) {
|
---|
[81d3612] | 419 | assert(zone->busy_count > 0);
|
---|
| 420 |
|
---|
[64f3d3b] | 421 | bitmap_set(&zone->bitmap, index, 0);
|
---|
[a35b458] | 422 |
|
---|
[d3dfa42] | 423 | /* Update zone information. */
|
---|
[b0c2075] | 424 | zone->free_count++;
|
---|
| 425 | zone->busy_count--;
|
---|
[a35b458] | 426 |
|
---|
[5df1963] | 427 | return 1;
|
---|
[085d973] | 428 | }
|
---|
[a35b458] | 429 |
|
---|
[5df1963] | 430 | return 0;
|
---|
[085d973] | 431 | }
|
---|
| 432 |
|
---|
[deaf8d5] | 433 | /** Mark frame in zone unavailable to allocation. */
|
---|
[8df5f20] | 434 | _NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index)
|
---|
[085d973] | 435 | {
|
---|
[63e27ef] | 436 | assert(zone->flags & ZONE_AVAILABLE);
|
---|
[a35b458] | 437 |
|
---|
[b0c2075] | 438 | frame_t *frame = zone_get_frame(zone, index);
|
---|
[81d3612] | 439 | assert(frame->refcount <= 1);
|
---|
| 440 |
|
---|
[b0c2075] | 441 | if (frame->refcount > 0)
|
---|
[bb68433] | 442 | return;
|
---|
[a35b458] | 443 |
|
---|
[81d3612] | 444 | assert(zone->free_count > 0);
|
---|
| 445 |
|
---|
[cd3b380] | 446 | frame->refcount = 1;
|
---|
[b0c2075] | 447 | bitmap_set_range(&zone->bitmap, index, 1);
|
---|
[a35b458] | 448 |
|
---|
[085d973] | 449 | zone->free_count--;
|
---|
[d060900] | 450 | reserve_force_alloc(1);
|
---|
[085d973] | 451 | }
|
---|
| 452 |
|
---|
[81d3612] | 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 |
|
---|
[5f0f29ce] | 467 | /** Merge two zones.
|
---|
[bb68433] | 468 | *
|
---|
[5f0f29ce] | 469 | * Assume z1 & z2 are locked and compatible and zones lock is
|
---|
| 470 | * locked.
|
---|
[bb68433] | 471 | *
|
---|
[b0c2075] | 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.
|
---|
[eb3d379] | 476 | *
|
---|
[bb68433] | 477 | */
|
---|
[8df5f20] | 478 | _NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
|
---|
[b0c2075] | 479 | void *confdata)
|
---|
[bb68433] | 480 | {
|
---|
[63e27ef] | 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,
|
---|
[5f0f29ce] | 486 | zones.info[z2].base, zones.info[z2].count));
|
---|
[a35b458] | 487 |
|
---|
[5f0f29ce] | 488 | /* Difference between zone bases */
|
---|
| 489 | pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
|
---|
[81d3612] | 490 | pfn_t gap = base_diff - zones.info[z1].count;
|
---|
[a35b458] | 491 |
|
---|
[5f0f29ce] | 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;
|
---|
[a35b458] | 495 |
|
---|
[b0c2075] | 496 | bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
|
---|
[c5396c1] | 497 | confdata + (sizeof(frame_t) * zones.info[z1].count));
|
---|
[adb252c0] | 498 | bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count);
|
---|
[a35b458] | 499 |
|
---|
[b0c2075] | 500 | zones.info[z1].frames = (frame_t *) confdata;
|
---|
[a35b458] | 501 |
|
---|
[b0c2075] | 502 | /*
|
---|
| 503 | * Copy frames and bits from both zones to preserve parents, etc.
|
---|
[bb68433] | 504 | */
|
---|
[a35b458] | 505 |
|
---|
[b0c2075] | 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];
|
---|
[bb68433] | 510 | }
|
---|
[a35b458] | 511 |
|
---|
[b0c2075] | 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];
|
---|
[bb68433] | 517 | }
|
---|
[81d3612] | 518 |
|
---|
| 519 | /*
|
---|
| 520 | * Mark the gap between the original zones as unavailable.
|
---|
| 521 | */
|
---|
| 522 |
|
---|
| 523 | for (size_t i = 0; i < gap; i++) {
|
---|
[5ecac7e] | 524 | frame_initialize(&zones.info[z1].frames[old_z1->count + i]);
|
---|
[81d3612] | 525 | zone_mark_unavailable(&zones.info[z1], old_z1->count + i);
|
---|
| 526 | }
|
---|
[bb68433] | 527 | }
|
---|
| 528 |
|
---|
[deaf8d5] | 529 | /** Return old configuration frames into the zone.
|
---|
[bb68433] | 530 | *
|
---|
[5f0f29ce] | 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.
|
---|
[874878a] | 540 | *
|
---|
[bb68433] | 541 | */
|
---|
[8df5f20] | 542 | _NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
|
---|
[bb68433] | 543 | {
|
---|
[63e27ef] | 544 | assert(zones.info[znum].flags & ZONE_AVAILABLE);
|
---|
[a35b458] | 545 |
|
---|
[98000fb] | 546 | size_t cframes = SIZE2FRAMES(zone_conf_size(count));
|
---|
[a35b458] | 547 |
|
---|
[b0c2075] | 548 | if ((pfn < zones.info[znum].base) ||
|
---|
| 549 | (pfn >= zones.info[znum].base + zones.info[znum].count))
|
---|
[bb68433] | 550 | return;
|
---|
[a35b458] | 551 |
|
---|
[b0c2075] | 552 | for (size_t i = 0; i < cframes; i++)
|
---|
[81d3612] | 553 | zone_mark_available(&zones.info[znum],
|
---|
[5f0f29ce] | 554 | pfn - zones.info[znum].base + i);
|
---|
[874878a] | 555 | }
|
---|
| 556 |
|
---|
[deaf8d5] | 557 | /** Merge zones z1 and z2.
|
---|
[bb68433] | 558 | *
|
---|
[5f0f29ce] | 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 | *
|
---|
[bb68433] | 567 | */
|
---|
[98000fb] | 568 | bool zone_merge(size_t z1, size_t z2)
|
---|
[bb68433] | 569 | {
|
---|
[da1bafb] | 570 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 571 |
|
---|
[5f0f29ce] | 572 | bool ret = true;
|
---|
[a35b458] | 573 |
|
---|
[b0c2075] | 574 | /*
|
---|
| 575 | * We can join only 2 zones with none existing inbetween,
|
---|
[5f0f29ce] | 576 | * the zones have to be available and with the same
|
---|
| 577 | * set of flags
|
---|
| 578 | */
|
---|
[e6c4b94] | 579 | if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
|
---|
| 580 | (zones.info[z1].flags != zones.info[z2].flags)) {
|
---|
[5f0f29ce] | 581 | ret = false;
|
---|
[bb68433] | 582 | goto errout;
|
---|
[5f0f29ce] | 583 | }
|
---|
[a35b458] | 584 |
|
---|
[5f0f29ce] | 585 | pfn_t cframes = SIZE2FRAMES(zone_conf_size(
|
---|
[3bacee1] | 586 | zones.info[z2].base - zones.info[z1].base +
|
---|
| 587 | zones.info[z2].count));
|
---|
[a35b458] | 588 |
|
---|
[5f0f29ce] | 589 | /* Allocate merged zone data inside one of the zones */
|
---|
| 590 | pfn_t pfn;
|
---|
[b0c2075] | 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);
|
---|
[5f0f29ce] | 597 | } else {
|
---|
| 598 | ret = false;
|
---|
| 599 | goto errout;
|
---|
| 600 | }
|
---|
[a35b458] | 601 |
|
---|
[5f0f29ce] | 602 | /* Preserve original data from z1 */
|
---|
| 603 | zone_t old_z1 = zones.info[z1];
|
---|
[a35b458] | 604 |
|
---|
[5f0f29ce] | 605 | /* Do zone merging */
|
---|
[b0c2075] | 606 | zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
|
---|
[a35b458] | 607 |
|
---|
[bb68433] | 608 | /* Subtract zone information from busy frames */
|
---|
[5f0f29ce] | 609 | zones.info[z1].busy_count -= cframes;
|
---|
[a35b458] | 610 |
|
---|
[5f0f29ce] | 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);
|
---|
[a35b458] | 617 |
|
---|
[e49e234] | 618 | /* Move zones down */
|
---|
[b0c2075] | 619 | for (size_t i = z2 + 1; i < zones.count; i++)
|
---|
[b6b576c] | 620 | zones.info[i - 1] = zones.info[i];
|
---|
[a35b458] | 621 |
|
---|
[bb68433] | 622 | zones.count--;
|
---|
[a35b458] | 623 |
|
---|
[bb68433] | 624 | errout:
|
---|
[da1bafb] | 625 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 626 |
|
---|
[5f0f29ce] | 627 | return ret;
|
---|
[bb68433] | 628 | }
|
---|
| 629 |
|
---|
[5f0f29ce] | 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).
|
---|
[bb68433] | 635 | *
|
---|
| 636 | */
|
---|
| 637 | void zone_merge_all(void)
|
---|
| 638 | {
|
---|
[b0c2075] | 639 | size_t i = 1;
|
---|
[a35b458] | 640 |
|
---|
[5f0f29ce] | 641 | while (i < zones.count) {
|
---|
[b0c2075] | 642 | if (!zone_merge(i - 1, i))
|
---|
[5f0f29ce] | 643 | i++;
|
---|
[bb68433] | 644 | }
|
---|
| 645 | }
|
---|
| 646 |
|
---|
[deaf8d5] | 647 | /** Create new frame zone.
|
---|
[085d973] | 648 | *
|
---|
[b0c2075] | 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.
|
---|
[5f0f29ce] | 654 | *
|
---|
| 655 | * @return Initialized zone.
|
---|
[085d973] | 656 | *
|
---|
| 657 | */
|
---|
[8df5f20] | 658 | _NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
|
---|
[b0c2075] | 659 | zone_flags_t flags, void *confdata)
|
---|
[085d973] | 660 | {
|
---|
[5f0f29ce] | 661 | zone->base = start;
|
---|
| 662 | zone->count = count;
|
---|
| 663 | zone->flags = flags;
|
---|
| 664 | zone->free_count = count;
|
---|
| 665 | zone->busy_count = 0;
|
---|
[a35b458] | 666 |
|
---|
[e6c4b94] | 667 | if (flags & ZONE_AVAILABLE) {
|
---|
[5f0f29ce] | 668 | /*
|
---|
[b0c2075] | 669 | * Initialize frame bitmap (located after the array of
|
---|
| 670 | * frame_t structures in the configuration space).
|
---|
[5f0f29ce] | 671 | */
|
---|
[a35b458] | 672 |
|
---|
[c5396c1] | 673 | bitmap_initialize(&zone->bitmap, count, confdata +
|
---|
| 674 | (sizeof(frame_t) * count));
|
---|
[adb252c0] | 675 | bitmap_clear_range(&zone->bitmap, 0, count);
|
---|
[a35b458] | 676 |
|
---|
[b0c2075] | 677 | /*
|
---|
| 678 | * Initialize the array of frame_t structures.
|
---|
| 679 | */
|
---|
[a35b458] | 680 |
|
---|
[b0c2075] | 681 | zone->frames = (frame_t *) confdata;
|
---|
[a35b458] | 682 |
|
---|
[b0c2075] | 683 | for (size_t i = 0; i < count; i++)
|
---|
| 684 | frame_initialize(&zone->frames[i]);
|
---|
| 685 | } else {
|
---|
[c5396c1] | 686 | bitmap_initialize(&zone->bitmap, 0, NULL);
|
---|
[5f0f29ce] | 687 | zone->frames = NULL;
|
---|
[b0c2075] | 688 | }
|
---|
[085d973] | 689 | }
|
---|
| 690 |
|
---|
[deaf8d5] | 691 | /** Compute configuration data size for zone.
|
---|
[eb3d379] | 692 | *
|
---|
[5f0f29ce] | 693 | * @param count Size of zone in frames.
|
---|
| 694 | *
|
---|
| 695 | * @return Size of zone configuration info (in bytes).
|
---|
| 696 | *
|
---|
[eb3d379] | 697 | */
|
---|
[bbfdf62] | 698 | size_t zone_conf_size(size_t count)
|
---|
[085d973] | 699 | {
|
---|
[c5396c1] | 700 | return (count * sizeof(frame_t) + bitmap_size(count));
|
---|
[085d973] | 701 | }
|
---|
| 702 |
|
---|
[8bdcffa] | 703 | /** Allocate external configuration frames from low memory. */
|
---|
| 704 | pfn_t zone_external_conf_alloc(size_t count)
|
---|
| 705 | {
|
---|
[b0c2075] | 706 | size_t frames = SIZE2FRAMES(zone_conf_size(count));
|
---|
[a35b458] | 707 |
|
---|
[b0c2075] | 708 | return ADDR2PFN((uintptr_t)
|
---|
| 709 | frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
|
---|
[8bdcffa] | 710 | }
|
---|
| 711 |
|
---|
[deaf8d5] | 712 | /** Create and add zone to system.
|
---|
[085d973] | 713 | *
|
---|
[5f0f29ce] | 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.
|
---|
[b0c2075] | 717 | * Automatically checks that we will not disturb the
|
---|
[5f0f29ce] | 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 | *
|
---|
[085d973] | 727 | */
|
---|
[da1bafb] | 728 | size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
|
---|
| 729 | zone_flags_t flags)
|
---|
[085d973] | 730 | {
|
---|
[da1bafb] | 731 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 732 |
|
---|
[e6c4b94] | 733 | if (flags & ZONE_AVAILABLE) { /* Create available zone */
|
---|
[b0c2075] | 734 | /*
|
---|
| 735 | * Theoretically we could have NULL here, practically make sure
|
---|
[5f0f29ce] | 736 | * nobody tries to do that. If some platform requires, remove
|
---|
| 737 | * the assert
|
---|
| 738 | */
|
---|
[3bacee1] | 739 | assert(confframe != ADDR2PFN((uintptr_t) NULL));
|
---|
[a35b458] | 740 |
|
---|
[40c8c17] | 741 | /* Update the known end of physical memory. */
|
---|
| 742 | config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
|
---|
[a35b458] | 743 |
|
---|
[b0c2075] | 744 | /*
|
---|
| 745 | * If confframe is supposed to be inside our zone, then make sure
|
---|
[5f0f29ce] | 746 | * it does not span kernel & init
|
---|
| 747 | */
|
---|
[98000fb] | 748 | size_t confcount = SIZE2FRAMES(zone_conf_size(count));
|
---|
[a35b458] | 749 |
|
---|
[5f0f29ce] | 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;
|
---|
[a35b458] | 756 |
|
---|
[6188fee] | 757 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
| 758 | KA2PA(ballocs.base), ballocs.size))
|
---|
| 759 | continue;
|
---|
| 760 |
|
---|
[5f0f29ce] | 761 | bool overlap = false;
|
---|
[b0c2075] | 762 | for (size_t i = 0; i < init.cnt; i++) {
|
---|
[5f0f29ce] | 763 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
[32817cc] | 764 | init.tasks[i].paddr,
|
---|
[5f0f29ce] | 765 | init.tasks[i].size)) {
|
---|
| 766 | overlap = true;
|
---|
| 767 | break;
|
---|
| 768 | }
|
---|
[b0c2075] | 769 | }
|
---|
[a35b458] | 770 |
|
---|
[5f0f29ce] | 771 | if (overlap)
|
---|
| 772 | continue;
|
---|
[a35b458] | 773 |
|
---|
[5f0f29ce] | 774 | break;
|
---|
| 775 | }
|
---|
[a35b458] | 776 |
|
---|
[b0c2075] | 777 | if (confframe >= start + count)
|
---|
| 778 | panic("Cannot find configuration data for zone.");
|
---|
[085d973] | 779 | }
|
---|
[a35b458] | 780 |
|
---|
[e2650d3] | 781 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 782 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 783 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 784 | return (size_t) -1;
|
---|
[5f0f29ce] | 785 | }
|
---|
[a35b458] | 786 |
|
---|
[b0c2075] | 787 | void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
|
---|
| 788 | zone_construct(&zones.info[znum], start, count, flags, confdata);
|
---|
[a35b458] | 789 |
|
---|
[5f0f29ce] | 790 | /* If confdata in zone, mark as unavailable */
|
---|
| 791 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
[b0c2075] | 792 | for (size_t i = confframe; i < confframe + confcount; i++)
|
---|
[5f0f29ce] | 793 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 794 | i - zones.info[znum].base);
|
---|
[085d973] | 795 | }
|
---|
[a35b458] | 796 |
|
---|
[da1bafb] | 797 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 798 |
|
---|
[5f0f29ce] | 799 | return znum;
|
---|
| 800 | }
|
---|
[a35b458] | 801 |
|
---|
[5f0f29ce] | 802 | /* Non-available zone */
|
---|
[e2650d3] | 803 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 804 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 805 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 806 | return (size_t) -1;
|
---|
[5f0f29ce] | 807 | }
|
---|
[a35b458] | 808 |
|
---|
[b0c2075] | 809 | zone_construct(&zones.info[znum], start, count, flags, NULL);
|
---|
[a35b458] | 810 |
|
---|
[da1bafb] | 811 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 812 |
|
---|
[bb68433] | 813 | return znum;
|
---|
[085d973] | 814 | }
|
---|
| 815 |
|
---|
[d1582b50] | 816 | /*
|
---|
| 817 | * Frame functions
|
---|
| 818 | */
|
---|
[085d973] | 819 |
|
---|
[deaf8d5] | 820 | /** Set parent of frame. */
|
---|
[98000fb] | 821 | void frame_set_parent(pfn_t pfn, void *data, size_t hint)
|
---|
[085d973] | 822 | {
|
---|
[da1bafb] | 823 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 824 |
|
---|
[98000fb] | 825 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[a35b458] | 826 |
|
---|
[63e27ef] | 827 | assert(znum != (size_t) -1);
|
---|
[a35b458] | 828 |
|
---|
[5f0f29ce] | 829 | zone_get_frame(&zones.info[znum],
|
---|
| 830 | pfn - zones.info[znum].base)->parent = data;
|
---|
[a35b458] | 831 |
|
---|
[da1bafb] | 832 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 833 | }
|
---|
| 834 |
|
---|
[98000fb] | 835 | void *frame_get_parent(pfn_t pfn, size_t hint)
|
---|
[085d973] | 836 | {
|
---|
[da1bafb] | 837 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 838 |
|
---|
[98000fb] | 839 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[a35b458] | 840 |
|
---|
[63e27ef] | 841 | assert(znum != (size_t) -1);
|
---|
[a35b458] | 842 |
|
---|
[5f0f29ce] | 843 | void *res = zone_get_frame(&zones.info[znum],
|
---|
| 844 | pfn - zones.info[znum].base)->parent;
|
---|
[a35b458] | 845 |
|
---|
[da1bafb] | 846 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 847 |
|
---|
[085d973] | 848 | return res;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
[482f968] | 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 |
|
---|
[b0c2075] | 865 | /** Allocate frames of physical memory.
|
---|
[085d973] | 866 | *
|
---|
[b0c2075] | 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.
|
---|
[085d973] | 872 | *
|
---|
[5f0f29ce] | 873 | * @return Physical address of the allocated frame.
|
---|
[9a68b34d] | 874 | *
|
---|
[085d973] | 875 | */
|
---|
[b0c2075] | 876 | uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
|
---|
[8cbf1c3] | 877 | uintptr_t constraint, size_t *pzone)
|
---|
[085d973] | 878 | {
|
---|
[63e27ef] | 879 | assert(count > 0);
|
---|
[a35b458] | 880 |
|
---|
[98000fb] | 881 | size_t hint = pzone ? (*pzone) : 0;
|
---|
[b0c2075] | 882 | pfn_t frame_constraint = ADDR2PFN(constraint);
|
---|
[a35b458] | 883 |
|
---|
[89bcb520] | 884 | /*
|
---|
| 885 | * If not told otherwise, we must first reserve the memory.
|
---|
| 886 | */
|
---|
[b0c2075] | 887 | if (!(flags & FRAME_NO_RESERVE))
|
---|
| 888 | reserve_force_alloc(count);
|
---|
[a35b458] | 889 |
|
---|
[085d973] | 890 | loop:
|
---|
[da1bafb] | 891 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 892 |
|
---|
[482f968] | 893 | // TODO: Print diagnostic if neither is explicitly specified.
|
---|
| 894 | bool lowmem = (flags & FRAME_LOWMEM) || !(flags & FRAME_HIGHMEM);
|
---|
| 895 |
|
---|
[085d973] | 896 | /*
|
---|
| 897 | * First, find suitable frame zone.
|
---|
| 898 | */
|
---|
[482f968] | 899 | size_t znum = try_find_zone(count, lowmem, frame_constraint, hint);
|
---|
[a35b458] | 900 |
|
---|
[b0c2075] | 901 | /*
|
---|
| 902 | * If no memory, reclaim some slab memory,
|
---|
| 903 | * if it does not help, reclaim all.
|
---|
| 904 | */
|
---|
[98000fb] | 905 | if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
|
---|
[da1bafb] | 906 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 907 | size_t freed = slab_reclaim(0);
|
---|
[da1bafb] | 908 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 909 |
|
---|
[5f0f29ce] | 910 | if (freed > 0)
|
---|
[482f968] | 911 | znum = try_find_zone(count, lowmem,
|
---|
[b0c2075] | 912 | frame_constraint, hint);
|
---|
[a35b458] | 913 |
|
---|
[98000fb] | 914 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 915 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 916 | freed = slab_reclaim(SLAB_RECLAIM_ALL);
|
---|
[da1bafb] | 917 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 918 |
|
---|
[5f0f29ce] | 919 | if (freed > 0)
|
---|
[482f968] | 920 | znum = try_find_zone(count, lowmem,
|
---|
[b0c2075] | 921 | frame_constraint, hint);
|
---|
[085d973] | 922 | }
|
---|
| 923 | }
|
---|
[a35b458] | 924 |
|
---|
[98000fb] | 925 | if (znum == (size_t) -1) {
|
---|
[1a1744e] | 926 | if (flags & FRAME_ATOMIC) {
|
---|
[da1bafb] | 927 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 928 |
|
---|
[89bcb520] | 929 | if (!(flags & FRAME_NO_RESERVE))
|
---|
[b0c2075] | 930 | reserve_free(count);
|
---|
[a35b458] | 931 |
|
---|
[8cbf1c3] | 932 | return 0;
|
---|
[1a1744e] | 933 | }
|
---|
[a35b458] | 934 |
|
---|
[8d308b9] | 935 | size_t avail = frame_total_free_get_internal();
|
---|
[a35b458] | 936 |
|
---|
[da1bafb] | 937 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 938 |
|
---|
[05411e8] | 939 | if (!THREAD)
|
---|
[adb252c0] | 940 | panic("Cannot wait for %zu frames to become available "
|
---|
| 941 | "(%zu available).", count, avail);
|
---|
[a35b458] | 942 |
|
---|
[5f0f29ce] | 943 | /*
|
---|
| 944 | * Sleep until some frames are available again.
|
---|
| 945 | */
|
---|
[a35b458] | 946 |
|
---|
[5f0f29ce] | 947 | #ifdef CONFIG_DEBUG
|
---|
[b2fa1204] | 948 | log(LF_OTHER, LVL_DEBUG,
|
---|
| 949 | "Thread %" PRIu64 " waiting for %zu frames "
|
---|
| 950 | "%zu available.", THREAD->tid, count, avail);
|
---|
[5f0f29ce] | 951 | #endif
|
---|
[a35b458] | 952 |
|
---|
[905721b] | 953 | /*
|
---|
[b0c2075] | 954 | * Since the mem_avail_mtx is an active mutex, we need to
|
---|
| 955 | * disable interrupts to prevent deadlock with TLB shootdown.
|
---|
[905721b] | 956 | */
|
---|
| 957 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 958 | mutex_lock(&mem_avail_mtx);
|
---|
[a35b458] | 959 |
|
---|
[5f0f29ce] | 960 | if (mem_avail_req > 0)
|
---|
[b0c2075] | 961 | mem_avail_req = min(mem_avail_req, count);
|
---|
[5f0f29ce] | 962 | else
|
---|
[b0c2075] | 963 | mem_avail_req = count;
|
---|
[a35b458] | 964 |
|
---|
[98000fb] | 965 | size_t gen = mem_avail_gen;
|
---|
[a35b458] | 966 |
|
---|
[5f0f29ce] | 967 | while (gen == mem_avail_gen)
|
---|
[1bb3766] | 968 | condvar_wait(&mem_avail_cv, &mem_avail_mtx);
|
---|
[a35b458] | 969 |
|
---|
[1bb3766] | 970 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 971 | interrupts_restore(ipl);
|
---|
[a35b458] | 972 |
|
---|
[1a1744e] | 973 | #ifdef CONFIG_DEBUG
|
---|
[b2fa1204] | 974 | log(LF_OTHER, LVL_DEBUG, "Thread %" PRIu64 " woken up.",
|
---|
| 975 | THREAD->tid);
|
---|
[1a1744e] | 976 | #endif
|
---|
[a35b458] | 977 |
|
---|
[085d973] | 978 | goto loop;
|
---|
| 979 | }
|
---|
[a35b458] | 980 |
|
---|
[b0c2075] | 981 | pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
|
---|
| 982 | frame_constraint) + zones.info[znum].base;
|
---|
[a35b458] | 983 |
|
---|
[da1bafb] | 984 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 985 |
|
---|
[5f0f29ce] | 986 | if (pzone)
|
---|
| 987 | *pzone = znum;
|
---|
[a35b458] | 988 |
|
---|
[8cbf1c3] | 989 | return PFN2ADDR(pfn);
|
---|
[085d973] | 990 | }
|
---|
| 991 |
|
---|
[b0c2075] | 992 | uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
|
---|
[0139747] | 993 | {
|
---|
[b0c2075] | 994 | return frame_alloc_generic(count, flags, constraint, NULL);
|
---|
[0139747] | 995 | }
|
---|
| 996 |
|
---|
[5df1963] | 997 | /** Free frames of physical memory.
|
---|
[085d973] | 998 | *
|
---|
[5df1963] | 999 | * Find respective frame structures for supplied physical frames.
|
---|
| 1000 | * Decrement each frame reference count. If it drops to zero, mark
|
---|
| 1001 | * the frames as available.
|
---|
[5f0f29ce] | 1002 | *
|
---|
[5df1963] | 1003 | * @param start Physical Address of the first frame to be freed.
|
---|
| 1004 | * @param count Number of frames to free.
|
---|
[c32e6bc] | 1005 | * @param flags Flags to control memory reservation.
|
---|
[085d973] | 1006 | *
|
---|
| 1007 | */
|
---|
[5df1963] | 1008 | void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
|
---|
[085d973] | 1009 | {
|
---|
[5df1963] | 1010 | size_t freed = 0;
|
---|
[a35b458] | 1011 |
|
---|
[5df1963] | 1012 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1013 |
|
---|
[5df1963] | 1014 | for (size_t i = 0; i < count; i++) {
|
---|
| 1015 | /*
|
---|
| 1016 | * First, find host frame zone for addr.
|
---|
| 1017 | */
|
---|
| 1018 | pfn_t pfn = ADDR2PFN(start) + i;
|
---|
| 1019 | size_t znum = find_zone(pfn, 1, 0);
|
---|
[a35b458] | 1020 |
|
---|
[63e27ef] | 1021 | assert(znum != (size_t) -1);
|
---|
[a35b458] | 1022 |
|
---|
[5df1963] | 1023 | freed += zone_frame_free(&zones.info[znum],
|
---|
| 1024 | pfn - zones.info[znum].base);
|
---|
| 1025 | }
|
---|
[a35b458] | 1026 |
|
---|
[da1bafb] | 1027 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 1028 |
|
---|
[1a1744e] | 1029 | /*
|
---|
| 1030 | * Signal that some memory has been freed.
|
---|
[5df1963] | 1031 | * Since the mem_avail_mtx is an active mutex,
|
---|
| 1032 | * we need to disable interruptsto prevent deadlock
|
---|
| 1033 | * with TLB shootdown.
|
---|
[905721b] | 1034 | */
|
---|
[a35b458] | 1035 |
|
---|
[905721b] | 1036 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 1037 | mutex_lock(&mem_avail_mtx);
|
---|
[a35b458] | 1038 |
|
---|
[5f0f29ce] | 1039 | if (mem_avail_req > 0)
|
---|
[5df1963] | 1040 | mem_avail_req -= min(mem_avail_req, freed);
|
---|
[a35b458] | 1041 |
|
---|
[5f0f29ce] | 1042 | if (mem_avail_req == 0) {
|
---|
| 1043 | mem_avail_gen++;
|
---|
| 1044 | condvar_broadcast(&mem_avail_cv);
|
---|
| 1045 | }
|
---|
[a35b458] | 1046 |
|
---|
[1bb3766] | 1047 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 1048 | interrupts_restore(ipl);
|
---|
[a35b458] | 1049 |
|
---|
[89bcb520] | 1050 | if (!(flags & FRAME_NO_RESERVE))
|
---|
[5df1963] | 1051 | reserve_free(freed);
|
---|
[085d973] | 1052 | }
|
---|
| 1053 |
|
---|
[5df1963] | 1054 | void frame_free(uintptr_t frame, size_t count)
|
---|
[c32e6bc] | 1055 | {
|
---|
[5df1963] | 1056 | frame_free_generic(frame, count, 0);
|
---|
[c32e6bc] | 1057 | }
|
---|
| 1058 |
|
---|
[5df1963] | 1059 | void frame_free_noreserve(uintptr_t frame, size_t count)
|
---|
[e608cbe] | 1060 | {
|
---|
[5df1963] | 1061 | frame_free_generic(frame, count, FRAME_NO_RESERVE);
|
---|
[e608cbe] | 1062 | }
|
---|
| 1063 |
|
---|
[f3ac636] | 1064 | /** Add reference to frame.
|
---|
| 1065 | *
|
---|
| 1066 | * Find respective frame structure for supplied PFN and
|
---|
| 1067 | * increment frame reference count.
|
---|
| 1068 | *
|
---|
[5f0f29ce] | 1069 | * @param pfn Frame number of the frame to be freed.
|
---|
| 1070 | *
|
---|
[f3ac636] | 1071 | */
|
---|
[8df5f20] | 1072 | _NO_TRACE void frame_reference_add(pfn_t pfn)
|
---|
[f3ac636] | 1073 | {
|
---|
[da1bafb] | 1074 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1075 |
|
---|
[f3ac636] | 1076 | /*
|
---|
| 1077 | * First, find host frame zone for addr.
|
---|
| 1078 | */
|
---|
[0b4a67a] | 1079 | size_t znum = find_zone(pfn, 1, 0);
|
---|
[a35b458] | 1080 |
|
---|
[63e27ef] | 1081 | assert(znum != (size_t) -1);
|
---|
[a35b458] | 1082 |
|
---|
[5f0f29ce] | 1083 | zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
|
---|
[a35b458] | 1084 |
|
---|
[da1bafb] | 1085 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[f3ac636] | 1086 | }
|
---|
[085d973] | 1087 |
|
---|
[da1bafb] | 1088 | /** Mark given range unavailable in frame zones.
|
---|
| 1089 | *
|
---|
| 1090 | */
|
---|
[8df5f20] | 1091 | _NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
|
---|
[085d973] | 1092 | {
|
---|
[da1bafb] | 1093 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1094 |
|
---|
[b0c2075] | 1095 | for (size_t i = 0; i < count; i++) {
|
---|
[98000fb] | 1096 | size_t znum = find_zone(start + i, 1, 0);
|
---|
[a35b458] | 1097 |
|
---|
[98000fb] | 1098 | if (znum == (size_t) -1) /* PFN not found */
|
---|
[085d973] | 1099 | continue;
|
---|
[a35b458] | 1100 |
|
---|
[5f0f29ce] | 1101 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 1102 | start + i - zones.info[znum].base);
|
---|
[085d973] | 1103 | }
|
---|
[a35b458] | 1104 |
|
---|
[da1bafb] | 1105 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 1106 | }
|
---|
| 1107 |
|
---|
[da1bafb] | 1108 | /** Initialize physical memory management.
|
---|
| 1109 | *
|
---|
| 1110 | */
|
---|
[085d973] | 1111 | void frame_init(void)
|
---|
| 1112 | {
|
---|
| 1113 | /* Tell the architecture to create some memory */
|
---|
[ddcc8a0] | 1114 | frame_low_arch_init();
|
---|
[a35b458] | 1115 |
|
---|
[085d973] | 1116 | if (config.cpu_active == 1) {
|
---|
[4638401] | 1117 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
|
---|
| 1118 | SIZE2FRAMES(config.kernel_size));
|
---|
[a35b458] | 1119 |
|
---|
[b0c2075] | 1120 | for (size_t i = 0; i < init.cnt; i++)
|
---|
| 1121 | frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
|
---|
[4638401] | 1122 | SIZE2FRAMES(init.tasks[i].size));
|
---|
[a35b458] | 1123 |
|
---|
[61e90dd] | 1124 | if (ballocs.size)
|
---|
[4638401] | 1125 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
|
---|
| 1126 | SIZE2FRAMES(ballocs.size));
|
---|
[a35b458] | 1127 |
|
---|
[b0c2075] | 1128 | /*
|
---|
| 1129 | * Blacklist first frame, as allocating NULL would
|
---|
[5f0f29ce] | 1130 | * fail in some places
|
---|
| 1131 | */
|
---|
[bffa0b06] | 1132 | frame_mark_unavailable(0, 1);
|
---|
[085d973] | 1133 | }
|
---|
[a35b458] | 1134 |
|
---|
[ddcc8a0] | 1135 | frame_high_arch_init();
|
---|
[085d973] | 1136 | }
|
---|
| 1137 |
|
---|
[ad12b5ea] | 1138 | /** Adjust bounds of physical memory region according to low/high memory split.
|
---|
| 1139 | *
|
---|
[b0c2075] | 1140 | * @param low[in] If true, the adjustment is performed to make the region
|
---|
| 1141 | * fit in the low memory. Otherwise the adjustment is
|
---|
| 1142 | * performed to make the region fit in the high memory.
|
---|
| 1143 | * @param basep[inout] Pointer to a variable which contains the region's base
|
---|
| 1144 | * address and which may receive the adjusted base address.
|
---|
| 1145 | * @param sizep[inout] Pointer to a variable which contains the region's size
|
---|
| 1146 | * and which may receive the adjusted size.
|
---|
| 1147 | *
|
---|
| 1148 | * @return True if the region still exists even after the adjustment.
|
---|
| 1149 | * @return False otherwise.
|
---|
| 1150 | *
|
---|
[ad12b5ea] | 1151 | */
|
---|
| 1152 | bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
|
---|
| 1153 | {
|
---|
[2cc7f16] | 1154 | uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
|
---|
[a35b458] | 1155 |
|
---|
[ad12b5ea] | 1156 | if (low) {
|
---|
| 1157 | if (*basep > limit)
|
---|
| 1158 | return false;
|
---|
[a35b458] | 1159 |
|
---|
[ad12b5ea] | 1160 | if (*basep + *sizep > limit)
|
---|
| 1161 | *sizep = limit - *basep;
|
---|
| 1162 | } else {
|
---|
| 1163 | if (*basep + *sizep <= limit)
|
---|
| 1164 | return false;
|
---|
[a35b458] | 1165 |
|
---|
[ad12b5ea] | 1166 | if (*basep <= limit) {
|
---|
| 1167 | *sizep -= limit - *basep;
|
---|
| 1168 | *basep = limit;
|
---|
| 1169 | }
|
---|
| 1170 | }
|
---|
[a35b458] | 1171 |
|
---|
[ad12b5ea] | 1172 | return true;
|
---|
| 1173 | }
|
---|
| 1174 |
|
---|
[da1bafb] | 1175 | /** Return total size of all zones.
|
---|
| 1176 | *
|
---|
| 1177 | */
|
---|
[9dae191e] | 1178 | uint64_t zones_total_size(void)
|
---|
[deaf8d5] | 1179 | {
|
---|
[da1bafb] | 1180 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1181 |
|
---|
[5f0f29ce] | 1182 | uint64_t total = 0;
|
---|
[a35b458] | 1183 |
|
---|
[b0c2075] | 1184 | for (size_t i = 0; i < zones.count; i++)
|
---|
[5f0f29ce] | 1185 | total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[a35b458] | 1186 |
|
---|
[da1bafb] | 1187 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 1188 |
|
---|
[71eef11] | 1189 | return total;
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
[9dae191e] | 1192 | void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
|
---|
| 1193 | uint64_t *free)
|
---|
[516adce] | 1194 | {
|
---|
[63e27ef] | 1195 | assert(total != NULL);
|
---|
| 1196 | assert(unavail != NULL);
|
---|
| 1197 | assert(busy != NULL);
|
---|
| 1198 | assert(free != NULL);
|
---|
[a35b458] | 1199 |
|
---|
[da1bafb] | 1200 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1201 |
|
---|
[9dae191e] | 1202 | *total = 0;
|
---|
| 1203 | *unavail = 0;
|
---|
| 1204 | *busy = 0;
|
---|
| 1205 | *free = 0;
|
---|
[a35b458] | 1206 |
|
---|
[b0c2075] | 1207 | for (size_t i = 0; i < zones.count; i++) {
|
---|
[9dae191e] | 1208 | *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[a35b458] | 1209 |
|
---|
[e6c4b94] | 1210 | if (zones.info[i].flags & ZONE_AVAILABLE) {
|
---|
[9dae191e] | 1211 | *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
|
---|
| 1212 | *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
|
---|
| 1213 | } else
|
---|
| 1214 | *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[516adce] | 1215 | }
|
---|
[a35b458] | 1216 |
|
---|
[da1bafb] | 1217 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[516adce] | 1218 | }
|
---|
| 1219 |
|
---|
[da1bafb] | 1220 | /** Prints list of zones.
|
---|
| 1221 | *
|
---|
| 1222 | */
|
---|
[9dae191e] | 1223 | void zones_print_list(void)
|
---|
[deaf8d5] | 1224 | {
|
---|
[5f0f29ce] | 1225 | #ifdef __32_BITS__
|
---|
[74c5a1ca] | 1226 | printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1227 | #endif
|
---|
| 1228 |
|
---|
| 1229 | #ifdef __64_BITS__
|
---|
[74c5a1ca] | 1230 | printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1231 | #endif
|
---|
[a35b458] | 1232 |
|
---|
[000350f8] | 1233 | /*
|
---|
| 1234 | * Because printing may require allocation of memory, we may not hold
|
---|
[f72906c] | 1235 | * the frame allocator locks when printing zone statistics. Therefore,
|
---|
[000350f8] | 1236 | * we simply gather the statistics under the protection of the locks and
|
---|
| 1237 | * print the statistics when the locks have been released.
|
---|
| 1238 | *
|
---|
| 1239 | * When someone adds/removes zones while we are printing the statistics,
|
---|
| 1240 | * we may end up with inaccurate output (e.g. a zone being skipped from
|
---|
| 1241 | * the listing).
|
---|
| 1242 | */
|
---|
[a35b458] | 1243 |
|
---|
[f72906c] | 1244 | size_t free_lowmem = 0;
|
---|
| 1245 | size_t free_highmem = 0;
|
---|
| 1246 | size_t free_highprio = 0;
|
---|
[a35b458] | 1247 |
|
---|
[3bacee1] | 1248 | for (size_t i = 0; ; i++) {
|
---|
[da1bafb] | 1249 | irq_spinlock_lock(&zones.lock, true);
|
---|
[a35b458] | 1250 |
|
---|
[000350f8] | 1251 | if (i >= zones.count) {
|
---|
[da1bafb] | 1252 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[000350f8] | 1253 | break;
|
---|
| 1254 | }
|
---|
[a35b458] | 1255 |
|
---|
[f72906c] | 1256 | pfn_t fbase = zones.info[i].base;
|
---|
| 1257 | uintptr_t base = PFN2ADDR(fbase);
|
---|
[98000fb] | 1258 | size_t count = zones.info[i].count;
|
---|
[5f0f29ce] | 1259 | zone_flags_t flags = zones.info[i].flags;
|
---|
[98000fb] | 1260 | size_t free_count = zones.info[i].free_count;
|
---|
| 1261 | size_t busy_count = zones.info[i].busy_count;
|
---|
[a35b458] | 1262 |
|
---|
[e6c4b94] | 1263 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[f72906c] | 1264 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
---|
| 1265 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
---|
| 1266 | bool highprio = is_high_priority(fbase, count);
|
---|
[a35b458] | 1267 |
|
---|
[f72906c] | 1268 | if (available) {
|
---|
| 1269 | if (lowmem)
|
---|
| 1270 | free_lowmem += free_count;
|
---|
[a35b458] | 1271 |
|
---|
[f72906c] | 1272 | if (highmem)
|
---|
| 1273 | free_highmem += free_count;
|
---|
[a35b458] | 1274 |
|
---|
[f72906c] | 1275 | if (highprio) {
|
---|
| 1276 | free_highprio += free_count;
|
---|
| 1277 | } else {
|
---|
| 1278 | /*
|
---|
| 1279 | * Walk all frames of the zone and examine
|
---|
| 1280 | * all high priority memory to get accurate
|
---|
| 1281 | * statistics.
|
---|
| 1282 | */
|
---|
[a35b458] | 1283 |
|
---|
[f72906c] | 1284 | for (size_t index = 0; index < count; index++) {
|
---|
| 1285 | if (is_high_priority(fbase + index, 0)) {
|
---|
| 1286 | if (!bitmap_get(&zones.info[i].bitmap, index))
|
---|
| 1287 | free_highprio++;
|
---|
| 1288 | } else
|
---|
| 1289 | break;
|
---|
| 1290 | }
|
---|
| 1291 | }
|
---|
| 1292 | }
|
---|
[a35b458] | 1293 |
|
---|
[f72906c] | 1294 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 1295 |
|
---|
[7e752b2] | 1296 | printf("%-4zu", i);
|
---|
[a35b458] | 1297 |
|
---|
[2b8b0ca] | 1298 | #ifdef __32_BITS__
|
---|
[7e752b2] | 1299 | printf(" %p", (void *) base);
|
---|
[2b8b0ca] | 1300 | #endif
|
---|
[a35b458] | 1301 |
|
---|
[2b8b0ca] | 1302 | #ifdef __64_BITS__
|
---|
[7e752b2] | 1303 | printf(" %p", (void *) base);
|
---|
[e49e234] | 1304 | #endif
|
---|
[a35b458] | 1305 |
|
---|
[e6c4b94] | 1306 | printf(" %12zu %c%c%c%c%c ", count,
|
---|
| 1307 | available ? 'A' : '-',
|
---|
| 1308 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1309 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1310 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1311 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[a35b458] | 1312 |
|
---|
[5f0f29ce] | 1313 | if (available)
|
---|
[7e752b2] | 1314 | printf("%14zu %14zu",
|
---|
[5f0f29ce] | 1315 | free_count, busy_count);
|
---|
[a35b458] | 1316 |
|
---|
[5f0f29ce] | 1317 | printf("\n");
|
---|
[dfd9186] | 1318 | }
|
---|
[a35b458] | 1319 |
|
---|
[f72906c] | 1320 | printf("\n");
|
---|
[a35b458] | 1321 |
|
---|
[f72906c] | 1322 | uint64_t size;
|
---|
| 1323 | const char *size_suffix;
|
---|
[a35b458] | 1324 |
|
---|
[f72906c] | 1325 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
---|
| 1326 | false);
|
---|
| 1327 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1328 | free_lowmem, size, size_suffix);
|
---|
[a35b458] | 1329 |
|
---|
[f72906c] | 1330 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
---|
| 1331 | false);
|
---|
| 1332 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1333 | free_highmem, size, size_suffix);
|
---|
[a35b458] | 1334 |
|
---|
[f72906c] | 1335 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
---|
| 1336 | false);
|
---|
| 1337 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1338 | free_highprio, size, size_suffix);
|
---|
[dfd9186] | 1339 | }
|
---|
| 1340 |
|
---|
[abbc16e] | 1341 | /** Prints zone details.
|
---|
[96cacc1] | 1342 | *
|
---|
[5f0f29ce] | 1343 | * @param num Zone base address or zone number.
|
---|
| 1344 | *
|
---|
[96cacc1] | 1345 | */
|
---|
[98000fb] | 1346 | void zone_print_one(size_t num)
|
---|
[deaf8d5] | 1347 | {
|
---|
[da1bafb] | 1348 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 1349 | size_t znum = (size_t) -1;
|
---|
[a35b458] | 1350 |
|
---|
[b0c2075] | 1351 | for (size_t i = 0; i < zones.count; i++) {
|
---|
[5f0f29ce] | 1352 | if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
|
---|
| 1353 | znum = i;
|
---|
[bb68433] | 1354 | break;
|
---|
| 1355 | }
|
---|
| 1356 | }
|
---|
[a35b458] | 1357 |
|
---|
[98000fb] | 1358 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 1359 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[bb68433] | 1360 | printf("Zone not found.\n");
|
---|
[2ec725f] | 1361 | return;
|
---|
[dfd9186] | 1362 | }
|
---|
[a35b458] | 1363 |
|
---|
[f72906c] | 1364 | size_t free_lowmem = 0;
|
---|
| 1365 | size_t free_highmem = 0;
|
---|
| 1366 | size_t free_highprio = 0;
|
---|
[a35b458] | 1367 |
|
---|
[f72906c] | 1368 | pfn_t fbase = zones.info[znum].base;
|
---|
| 1369 | uintptr_t base = PFN2ADDR(fbase);
|
---|
[b0c2075] | 1370 | zone_flags_t flags = zones.info[znum].flags;
|
---|
| 1371 | size_t count = zones.info[znum].count;
|
---|
| 1372 | size_t free_count = zones.info[znum].free_count;
|
---|
| 1373 | size_t busy_count = zones.info[znum].busy_count;
|
---|
[a35b458] | 1374 |
|
---|
[e6c4b94] | 1375 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[f72906c] | 1376 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
---|
| 1377 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
---|
| 1378 | bool highprio = is_high_priority(fbase, count);
|
---|
[a35b458] | 1379 |
|
---|
[f72906c] | 1380 | if (available) {
|
---|
| 1381 | if (lowmem)
|
---|
| 1382 | free_lowmem = free_count;
|
---|
[a35b458] | 1383 |
|
---|
[f72906c] | 1384 | if (highmem)
|
---|
| 1385 | free_highmem = free_count;
|
---|
[a35b458] | 1386 |
|
---|
[f72906c] | 1387 | if (highprio) {
|
---|
| 1388 | free_highprio = free_count;
|
---|
| 1389 | } else {
|
---|
| 1390 | /*
|
---|
| 1391 | * Walk all frames of the zone and examine
|
---|
| 1392 | * all high priority memory to get accurate
|
---|
| 1393 | * statistics.
|
---|
| 1394 | */
|
---|
[a35b458] | 1395 |
|
---|
[f72906c] | 1396 | for (size_t index = 0; index < count; index++) {
|
---|
| 1397 | if (is_high_priority(fbase + index, 0)) {
|
---|
| 1398 | if (!bitmap_get(&zones.info[znum].bitmap, index))
|
---|
| 1399 | free_highprio++;
|
---|
| 1400 | } else
|
---|
| 1401 | break;
|
---|
| 1402 | }
|
---|
| 1403 | }
|
---|
| 1404 | }
|
---|
[a35b458] | 1405 |
|
---|
[f72906c] | 1406 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[a35b458] | 1407 |
|
---|
[933cadf] | 1408 | uint64_t size;
|
---|
| 1409 | const char *size_suffix;
|
---|
[a35b458] | 1410 |
|
---|
[933cadf] | 1411 | bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
|
---|
[a35b458] | 1412 |
|
---|
[f72906c] | 1413 | printf("Zone number: %zu\n", znum);
|
---|
| 1414 | printf("Zone base address: %p\n", (void *) base);
|
---|
| 1415 | printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
|
---|
[933cadf] | 1416 | size, size_suffix);
|
---|
[f72906c] | 1417 | printf("Zone flags: %c%c%c%c%c\n",
|
---|
[e6c4b94] | 1418 | available ? 'A' : '-',
|
---|
| 1419 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1420 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1421 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1422 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[a35b458] | 1423 |
|
---|
[5f0f29ce] | 1424 | if (available) {
|
---|
[933cadf] | 1425 | bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
|
---|
| 1426 | false);
|
---|
[f72906c] | 1427 | printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
|
---|
[933cadf] | 1428 | busy_count, size, size_suffix);
|
---|
[a35b458] | 1429 |
|
---|
[933cadf] | 1430 | bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
|
---|
| 1431 | false);
|
---|
[f72906c] | 1432 | printf("Available space: %zu frames (%" PRIu64 " %s)\n",
|
---|
[933cadf] | 1433 | free_count, size, size_suffix);
|
---|
[a35b458] | 1434 |
|
---|
[f72906c] | 1435 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
---|
| 1436 | false);
|
---|
| 1437 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1438 | free_lowmem, size, size_suffix);
|
---|
[a35b458] | 1439 |
|
---|
[f72906c] | 1440 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
---|
| 1441 | false);
|
---|
| 1442 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1443 | free_highmem, size, size_suffix);
|
---|
[a35b458] | 1444 |
|
---|
[f72906c] | 1445 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
---|
| 1446 | false);
|
---|
| 1447 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1448 | free_highprio, size, size_suffix);
|
---|
[5f0f29ce] | 1449 | }
|
---|
[dfd9186] | 1450 | }
|
---|
| 1451 |
|
---|
[cc73a8a1] | 1452 | /** @}
|
---|
[b45c443] | 1453 | */
|
---|