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