[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 | {
|
---|
[11d41be5] | 445 | ASSERT(zone->flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 446 |
|
---|
[b0c2075] | 447 | frame_t *frame = zone_get_frame(zone, index);
|
---|
| 448 | if (frame->refcount > 0)
|
---|
[bb68433] | 449 | return;
|
---|
[5f0f29ce] | 450 |
|
---|
[cd3b380] | 451 | frame->refcount = 1;
|
---|
[b0c2075] | 452 | bitmap_set_range(&zone->bitmap, index, 1);
|
---|
[5f0f29ce] | 453 |
|
---|
[085d973] | 454 | zone->free_count--;
|
---|
[d060900] | 455 | reserve_force_alloc(1);
|
---|
[085d973] | 456 | }
|
---|
| 457 |
|
---|
[5f0f29ce] | 458 | /** Merge two zones.
|
---|
[bb68433] | 459 | *
|
---|
[5f0f29ce] | 460 | * Assume z1 & z2 are locked and compatible and zones lock is
|
---|
| 461 | * locked.
|
---|
[bb68433] | 462 | *
|
---|
[b0c2075] | 463 | * @param z1 First zone to merge.
|
---|
| 464 | * @param z2 Second zone to merge.
|
---|
| 465 | * @param old_z1 Original data of the first zone.
|
---|
| 466 | * @param confdata Merged zone configuration data.
|
---|
[eb3d379] | 467 | *
|
---|
[bb68433] | 468 | */
|
---|
[7a0359b] | 469 | NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
|
---|
[b0c2075] | 470 | void *confdata)
|
---|
[bb68433] | 471 | {
|
---|
[e6c4b94] | 472 | ASSERT(zones.info[z1].flags & ZONE_AVAILABLE);
|
---|
| 473 | ASSERT(zones.info[z2].flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 474 | ASSERT(zones.info[z1].flags == zones.info[z2].flags);
|
---|
| 475 | ASSERT(zones.info[z1].base < zones.info[z2].base);
|
---|
| 476 | ASSERT(!overlaps(zones.info[z1].base, zones.info[z1].count,
|
---|
| 477 | zones.info[z2].base, zones.info[z2].count));
|
---|
| 478 |
|
---|
| 479 | /* Difference between zone bases */
|
---|
| 480 | pfn_t base_diff = zones.info[z2].base - zones.info[z1].base;
|
---|
| 481 |
|
---|
| 482 | zones.info[z1].count = base_diff + zones.info[z2].count;
|
---|
| 483 | zones.info[z1].free_count += zones.info[z2].free_count;
|
---|
| 484 | zones.info[z1].busy_count += zones.info[z2].busy_count;
|
---|
| 485 |
|
---|
[b0c2075] | 486 | bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
|
---|
[c5396c1] | 487 | confdata + (sizeof(frame_t) * zones.info[z1].count));
|
---|
[adb252c0] | 488 | bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count);
|
---|
[5f0f29ce] | 489 |
|
---|
[b0c2075] | 490 | zones.info[z1].frames = (frame_t *) confdata;
|
---|
[5f0f29ce] | 491 |
|
---|
[b0c2075] | 492 | /*
|
---|
| 493 | * Copy frames and bits from both zones to preserve parents, etc.
|
---|
[bb68433] | 494 | */
|
---|
[5f0f29ce] | 495 |
|
---|
[b0c2075] | 496 | for (size_t i = 0; i < old_z1->count; i++) {
|
---|
| 497 | bitmap_set(&zones.info[z1].bitmap, i,
|
---|
| 498 | bitmap_get(&old_z1->bitmap, i));
|
---|
| 499 | zones.info[z1].frames[i] = old_z1->frames[i];
|
---|
[bb68433] | 500 | }
|
---|
[5f0f29ce] | 501 |
|
---|
[b0c2075] | 502 | for (size_t i = 0; i < zones.info[z2].count; i++) {
|
---|
| 503 | bitmap_set(&zones.info[z1].bitmap, base_diff + i,
|
---|
| 504 | bitmap_get(&zones.info[z2].bitmap, i));
|
---|
| 505 | zones.info[z1].frames[base_diff + i] =
|
---|
| 506 | zones.info[z2].frames[i];
|
---|
[bb68433] | 507 | }
|
---|
| 508 | }
|
---|
| 509 |
|
---|
[deaf8d5] | 510 | /** Return old configuration frames into the zone.
|
---|
[bb68433] | 511 | *
|
---|
[5f0f29ce] | 512 | * We have two cases:
|
---|
| 513 | * - The configuration data is outside the zone
|
---|
| 514 | * -> do nothing (perhaps call frame_free?)
|
---|
| 515 | * - The configuration data was created by zone_create
|
---|
| 516 | * or updated by reduce_region -> free every frame
|
---|
| 517 | *
|
---|
| 518 | * @param znum The actual zone where freeing should occur.
|
---|
| 519 | * @param pfn Old zone configuration frame.
|
---|
| 520 | * @param count Old zone frame count.
|
---|
[874878a] | 521 | *
|
---|
[bb68433] | 522 | */
|
---|
[7a0359b] | 523 | NO_TRACE static void return_config_frames(size_t znum, pfn_t pfn, size_t count)
|
---|
[bb68433] | 524 | {
|
---|
[e6c4b94] | 525 | ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
|
---|
[5f0f29ce] | 526 |
|
---|
[98000fb] | 527 | size_t cframes = SIZE2FRAMES(zone_conf_size(count));
|
---|
[bb68433] | 528 |
|
---|
[b0c2075] | 529 | if ((pfn < zones.info[znum].base) ||
|
---|
| 530 | (pfn >= zones.info[znum].base + zones.info[znum].count))
|
---|
[bb68433] | 531 | return;
|
---|
[5f0f29ce] | 532 |
|
---|
[b0c2075] | 533 | for (size_t i = 0; i < cframes; i++)
|
---|
[89bcb520] | 534 | (void) zone_frame_free(&zones.info[znum],
|
---|
[5f0f29ce] | 535 | pfn - zones.info[znum].base + i);
|
---|
[874878a] | 536 | }
|
---|
| 537 |
|
---|
[deaf8d5] | 538 | /** Merge zones z1 and z2.
|
---|
[bb68433] | 539 | *
|
---|
[5f0f29ce] | 540 | * The merged zones must be 2 zones with no zone existing in between
|
---|
| 541 | * (which means that z2 = z1 + 1). Both zones must be available zones
|
---|
| 542 | * with the same flags.
|
---|
| 543 | *
|
---|
| 544 | * When you create a new zone, the frame allocator configuration does
|
---|
| 545 | * not to be 2^order size. Once the allocator is running it is no longer
|
---|
| 546 | * possible, merged configuration data occupies more space :-/
|
---|
| 547 | *
|
---|
[bb68433] | 548 | */
|
---|
[98000fb] | 549 | bool zone_merge(size_t z1, size_t z2)
|
---|
[bb68433] | 550 | {
|
---|
[da1bafb] | 551 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 552 |
|
---|
| 553 | bool ret = true;
|
---|
| 554 |
|
---|
[b0c2075] | 555 | /*
|
---|
| 556 | * We can join only 2 zones with none existing inbetween,
|
---|
[5f0f29ce] | 557 | * the zones have to be available and with the same
|
---|
| 558 | * set of flags
|
---|
| 559 | */
|
---|
[e6c4b94] | 560 | if ((z1 >= zones.count) || (z2 >= zones.count) || (z2 - z1 != 1) ||
|
---|
| 561 | (zones.info[z1].flags != zones.info[z2].flags)) {
|
---|
[5f0f29ce] | 562 | ret = false;
|
---|
[bb68433] | 563 | goto errout;
|
---|
[5f0f29ce] | 564 | }
|
---|
| 565 |
|
---|
| 566 | pfn_t cframes = SIZE2FRAMES(zone_conf_size(
|
---|
| 567 | zones.info[z2].base - zones.info[z1].base
|
---|
| 568 | + zones.info[z2].count));
|
---|
| 569 |
|
---|
| 570 | /* Allocate merged zone data inside one of the zones */
|
---|
| 571 | pfn_t pfn;
|
---|
[b0c2075] | 572 | if (zone_can_alloc(&zones.info[z1], cframes, 0)) {
|
---|
| 573 | pfn = zones.info[z1].base +
|
---|
| 574 | zone_frame_alloc(&zones.info[z1], cframes, 0);
|
---|
| 575 | } else if (zone_can_alloc(&zones.info[z2], cframes, 0)) {
|
---|
| 576 | pfn = zones.info[z2].base +
|
---|
| 577 | zone_frame_alloc(&zones.info[z2], cframes, 0);
|
---|
[5f0f29ce] | 578 | } else {
|
---|
| 579 | ret = false;
|
---|
| 580 | goto errout;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | /* Preserve original data from z1 */
|
---|
| 584 | zone_t old_z1 = zones.info[z1];
|
---|
| 585 |
|
---|
| 586 | /* Do zone merging */
|
---|
[b0c2075] | 587 | zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
|
---|
[5f0f29ce] | 588 |
|
---|
[bb68433] | 589 | /* Subtract zone information from busy frames */
|
---|
[5f0f29ce] | 590 | zones.info[z1].busy_count -= cframes;
|
---|
| 591 |
|
---|
| 592 | /* Free old zone information */
|
---|
| 593 | return_config_frames(z1,
|
---|
| 594 | ADDR2PFN(KA2PA((uintptr_t) old_z1.frames)), old_z1.count);
|
---|
| 595 | return_config_frames(z1,
|
---|
| 596 | ADDR2PFN(KA2PA((uintptr_t) zones.info[z2].frames)),
|
---|
| 597 | zones.info[z2].count);
|
---|
| 598 |
|
---|
[e49e234] | 599 | /* Move zones down */
|
---|
[b0c2075] | 600 | for (size_t i = z2 + 1; i < zones.count; i++)
|
---|
[b6b576c] | 601 | zones.info[i - 1] = zones.info[i];
|
---|
[e49e234] | 602 |
|
---|
[bb68433] | 603 | zones.count--;
|
---|
[5f0f29ce] | 604 |
|
---|
[bb68433] | 605 | errout:
|
---|
[da1bafb] | 606 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 607 |
|
---|
| 608 | return ret;
|
---|
[bb68433] | 609 | }
|
---|
| 610 |
|
---|
[5f0f29ce] | 611 | /** Merge all mergeable zones into one big zone.
|
---|
| 612 | *
|
---|
| 613 | * It is reasonable to do this on systems where
|
---|
| 614 | * BIOS reports parts in chunks, so that we could
|
---|
| 615 | * have 1 zone (it's faster).
|
---|
[bb68433] | 616 | *
|
---|
| 617 | */
|
---|
| 618 | void zone_merge_all(void)
|
---|
| 619 | {
|
---|
[b0c2075] | 620 | size_t i = 1;
|
---|
| 621 |
|
---|
[5f0f29ce] | 622 | while (i < zones.count) {
|
---|
[b0c2075] | 623 | if (!zone_merge(i - 1, i))
|
---|
[5f0f29ce] | 624 | i++;
|
---|
[bb68433] | 625 | }
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[deaf8d5] | 628 | /** Create new frame zone.
|
---|
[085d973] | 629 | *
|
---|
[b0c2075] | 630 | * @param zone Zone to construct.
|
---|
| 631 | * @param start Physical address of the first frame within the zone.
|
---|
| 632 | * @param count Count of frames in zone.
|
---|
| 633 | * @param flags Zone flags.
|
---|
| 634 | * @param confdata Configuration data of the zone.
|
---|
[5f0f29ce] | 635 | *
|
---|
| 636 | * @return Initialized zone.
|
---|
[085d973] | 637 | *
|
---|
| 638 | */
|
---|
[b0c2075] | 639 | NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
|
---|
| 640 | zone_flags_t flags, void *confdata)
|
---|
[085d973] | 641 | {
|
---|
[5f0f29ce] | 642 | zone->base = start;
|
---|
| 643 | zone->count = count;
|
---|
| 644 | zone->flags = flags;
|
---|
| 645 | zone->free_count = count;
|
---|
| 646 | zone->busy_count = 0;
|
---|
[085d973] | 647 |
|
---|
[e6c4b94] | 648 | if (flags & ZONE_AVAILABLE) {
|
---|
[5f0f29ce] | 649 | /*
|
---|
[b0c2075] | 650 | * Initialize frame bitmap (located after the array of
|
---|
| 651 | * frame_t structures in the configuration space).
|
---|
[5f0f29ce] | 652 | */
|
---|
| 653 |
|
---|
[c5396c1] | 654 | bitmap_initialize(&zone->bitmap, count, confdata +
|
---|
| 655 | (sizeof(frame_t) * count));
|
---|
[adb252c0] | 656 | bitmap_clear_range(&zone->bitmap, 0, count);
|
---|
[5f0f29ce] | 657 |
|
---|
[b0c2075] | 658 | /*
|
---|
| 659 | * Initialize the array of frame_t structures.
|
---|
| 660 | */
|
---|
[5f0f29ce] | 661 |
|
---|
[b0c2075] | 662 | zone->frames = (frame_t *) confdata;
|
---|
[5f0f29ce] | 663 |
|
---|
[b0c2075] | 664 | for (size_t i = 0; i < count; i++)
|
---|
| 665 | frame_initialize(&zone->frames[i]);
|
---|
| 666 | } else {
|
---|
[c5396c1] | 667 | bitmap_initialize(&zone->bitmap, 0, NULL);
|
---|
[5f0f29ce] | 668 | zone->frames = NULL;
|
---|
[b0c2075] | 669 | }
|
---|
[085d973] | 670 | }
|
---|
| 671 |
|
---|
[deaf8d5] | 672 | /** Compute configuration data size for zone.
|
---|
[eb3d379] | 673 | *
|
---|
[5f0f29ce] | 674 | * @param count Size of zone in frames.
|
---|
| 675 | *
|
---|
| 676 | * @return Size of zone configuration info (in bytes).
|
---|
| 677 | *
|
---|
[eb3d379] | 678 | */
|
---|
[bbfdf62] | 679 | size_t zone_conf_size(size_t count)
|
---|
[085d973] | 680 | {
|
---|
[c5396c1] | 681 | return (count * sizeof(frame_t) + bitmap_size(count));
|
---|
[085d973] | 682 | }
|
---|
| 683 |
|
---|
[8bdcffa] | 684 | /** Allocate external configuration frames from low memory. */
|
---|
| 685 | pfn_t zone_external_conf_alloc(size_t count)
|
---|
| 686 | {
|
---|
[b0c2075] | 687 | size_t frames = SIZE2FRAMES(zone_conf_size(count));
|
---|
| 688 |
|
---|
| 689 | return ADDR2PFN((uintptr_t)
|
---|
| 690 | frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
|
---|
[8bdcffa] | 691 | }
|
---|
| 692 |
|
---|
[deaf8d5] | 693 | /** Create and add zone to system.
|
---|
[085d973] | 694 | *
|
---|
[5f0f29ce] | 695 | * @param start First frame number (absolute).
|
---|
| 696 | * @param count Size of zone in frames.
|
---|
| 697 | * @param confframe Where configuration frames are supposed to be.
|
---|
[b0c2075] | 698 | * Automatically checks that we will not disturb the
|
---|
[5f0f29ce] | 699 | * kernel and possibly init. If confframe is given
|
---|
| 700 | * _outside_ this zone, it is expected, that the area is
|
---|
| 701 | * already marked BUSY and big enough to contain
|
---|
| 702 | * zone_conf_size() amount of data. If the confframe is
|
---|
| 703 | * inside the area, the zone free frame information is
|
---|
| 704 | * modified not to include it.
|
---|
| 705 | *
|
---|
| 706 | * @return Zone number or -1 on error.
|
---|
| 707 | *
|
---|
[085d973] | 708 | */
|
---|
[da1bafb] | 709 | size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
|
---|
| 710 | zone_flags_t flags)
|
---|
[085d973] | 711 | {
|
---|
[da1bafb] | 712 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 713 |
|
---|
[e6c4b94] | 714 | if (flags & ZONE_AVAILABLE) { /* Create available zone */
|
---|
[b0c2075] | 715 | /*
|
---|
| 716 | * Theoretically we could have NULL here, practically make sure
|
---|
[5f0f29ce] | 717 | * nobody tries to do that. If some platform requires, remove
|
---|
| 718 | * the assert
|
---|
| 719 | */
|
---|
[0b4a67a] | 720 | ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
|
---|
[b0c2075] | 721 |
|
---|
[40c8c17] | 722 | /* Update the known end of physical memory. */
|
---|
| 723 | config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
|
---|
[5f0f29ce] | 724 |
|
---|
[b0c2075] | 725 | /*
|
---|
| 726 | * If confframe is supposed to be inside our zone, then make sure
|
---|
[5f0f29ce] | 727 | * it does not span kernel & init
|
---|
| 728 | */
|
---|
[98000fb] | 729 | size_t confcount = SIZE2FRAMES(zone_conf_size(count));
|
---|
[b0c2075] | 730 |
|
---|
[5f0f29ce] | 731 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
| 732 | for (; confframe < start + count; confframe++) {
|
---|
| 733 | uintptr_t addr = PFN2ADDR(confframe);
|
---|
| 734 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
| 735 | KA2PA(config.base), config.kernel_size))
|
---|
| 736 | continue;
|
---|
| 737 |
|
---|
[4638401] | 738 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
[5f0f29ce] | 739 | KA2PA(config.stack_base), config.stack_size))
|
---|
| 740 | continue;
|
---|
| 741 |
|
---|
| 742 | bool overlap = false;
|
---|
[b0c2075] | 743 | for (size_t i = 0; i < init.cnt; i++) {
|
---|
[5f0f29ce] | 744 | if (overlaps(addr, PFN2ADDR(confcount),
|
---|
[32817cc] | 745 | init.tasks[i].paddr,
|
---|
[5f0f29ce] | 746 | init.tasks[i].size)) {
|
---|
| 747 | overlap = true;
|
---|
| 748 | break;
|
---|
| 749 | }
|
---|
[b0c2075] | 750 | }
|
---|
| 751 |
|
---|
[5f0f29ce] | 752 | if (overlap)
|
---|
| 753 | continue;
|
---|
| 754 |
|
---|
| 755 | break;
|
---|
| 756 | }
|
---|
[b6b576c] | 757 |
|
---|
[b0c2075] | 758 | if (confframe >= start + count)
|
---|
| 759 | panic("Cannot find configuration data for zone.");
|
---|
[085d973] | 760 | }
|
---|
[5f0f29ce] | 761 |
|
---|
[e2650d3] | 762 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 763 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 764 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 765 | return (size_t) -1;
|
---|
[5f0f29ce] | 766 | }
|
---|
| 767 |
|
---|
[b0c2075] | 768 | void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
|
---|
| 769 | zone_construct(&zones.info[znum], start, count, flags, confdata);
|
---|
[5f0f29ce] | 770 |
|
---|
| 771 | /* If confdata in zone, mark as unavailable */
|
---|
| 772 | if ((confframe >= start) && (confframe < start + count)) {
|
---|
[b0c2075] | 773 | for (size_t i = confframe; i < confframe + confcount; i++)
|
---|
[5f0f29ce] | 774 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 775 | i - zones.info[znum].base);
|
---|
[085d973] | 776 | }
|
---|
[5f0f29ce] | 777 |
|
---|
[da1bafb] | 778 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 779 |
|
---|
| 780 | return znum;
|
---|
| 781 | }
|
---|
[b0c2075] | 782 |
|
---|
[5f0f29ce] | 783 | /* Non-available zone */
|
---|
[e2650d3] | 784 | size_t znum = zones_insert_zone(start, count, flags);
|
---|
[98000fb] | 785 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 786 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 787 | return (size_t) -1;
|
---|
[5f0f29ce] | 788 | }
|
---|
[b0c2075] | 789 |
|
---|
| 790 | zone_construct(&zones.info[znum], start, count, flags, NULL);
|
---|
[5f0f29ce] | 791 |
|
---|
[da1bafb] | 792 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[1bb3766] | 793 |
|
---|
[bb68433] | 794 | return znum;
|
---|
[085d973] | 795 | }
|
---|
| 796 |
|
---|
[5f0f29ce] | 797 | /*******************/
|
---|
[085d973] | 798 | /* Frame functions */
|
---|
[5f0f29ce] | 799 | /*******************/
|
---|
[085d973] | 800 |
|
---|
[deaf8d5] | 801 | /** Set parent of frame. */
|
---|
[98000fb] | 802 | void frame_set_parent(pfn_t pfn, void *data, size_t hint)
|
---|
[085d973] | 803 | {
|
---|
[da1bafb] | 804 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 805 |
|
---|
[98000fb] | 806 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[5f0f29ce] | 807 |
|
---|
[98000fb] | 808 | ASSERT(znum != (size_t) -1);
|
---|
[5f0f29ce] | 809 |
|
---|
| 810 | zone_get_frame(&zones.info[znum],
|
---|
| 811 | pfn - zones.info[znum].base)->parent = data;
|
---|
| 812 |
|
---|
[da1bafb] | 813 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 814 | }
|
---|
| 815 |
|
---|
[98000fb] | 816 | void *frame_get_parent(pfn_t pfn, size_t hint)
|
---|
[085d973] | 817 | {
|
---|
[da1bafb] | 818 | irq_spinlock_lock(&zones.lock, true);
|
---|
[5f0f29ce] | 819 |
|
---|
[98000fb] | 820 | size_t znum = find_zone(pfn, 1, hint);
|
---|
[5f0f29ce] | 821 |
|
---|
[98000fb] | 822 | ASSERT(znum != (size_t) -1);
|
---|
[5f0f29ce] | 823 |
|
---|
| 824 | void *res = zone_get_frame(&zones.info[znum],
|
---|
| 825 | pfn - zones.info[znum].base)->parent;
|
---|
| 826 |
|
---|
[da1bafb] | 827 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 828 |
|
---|
| 829 | return res;
|
---|
| 830 | }
|
---|
| 831 |
|
---|
[b0c2075] | 832 | /** Allocate frames of physical memory.
|
---|
[085d973] | 833 | *
|
---|
[b0c2075] | 834 | * @param count Number of continuous frames to allocate.
|
---|
| 835 | * @param flags Flags for host zone selection and address processing.
|
---|
| 836 | * @param constraint Indication of physical address bits that cannot be
|
---|
| 837 | * set in the address of the first allocated frame.
|
---|
| 838 | * @param pzone Preferred zone.
|
---|
[085d973] | 839 | *
|
---|
[5f0f29ce] | 840 | * @return Physical address of the allocated frame.
|
---|
[9a68b34d] | 841 | *
|
---|
[085d973] | 842 | */
|
---|
[b0c2075] | 843 | uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
|
---|
[8cbf1c3] | 844 | uintptr_t constraint, size_t *pzone)
|
---|
[085d973] | 845 | {
|
---|
[b0c2075] | 846 | ASSERT(count > 0);
|
---|
| 847 |
|
---|
[98000fb] | 848 | size_t hint = pzone ? (*pzone) : 0;
|
---|
[b0c2075] | 849 | pfn_t frame_constraint = ADDR2PFN(constraint);
|
---|
[085d973] | 850 |
|
---|
[89bcb520] | 851 | /*
|
---|
| 852 | * If not told otherwise, we must first reserve the memory.
|
---|
| 853 | */
|
---|
[b0c2075] | 854 | if (!(flags & FRAME_NO_RESERVE))
|
---|
| 855 | reserve_force_alloc(count);
|
---|
| 856 |
|
---|
[085d973] | 857 | loop:
|
---|
[da1bafb] | 858 | irq_spinlock_lock(&zones.lock, true);
|
---|
[9a68b34d] | 859 |
|
---|
[085d973] | 860 | /*
|
---|
| 861 | * First, find suitable frame zone.
|
---|
| 862 | */
|
---|
[b0c2075] | 863 | size_t znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
| 864 | frame_constraint, hint);
|
---|
[9a68b34d] | 865 |
|
---|
[b0c2075] | 866 | /*
|
---|
| 867 | * If no memory, reclaim some slab memory,
|
---|
| 868 | * if it does not help, reclaim all.
|
---|
| 869 | */
|
---|
[98000fb] | 870 | if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
|
---|
[da1bafb] | 871 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[98000fb] | 872 | size_t freed = slab_reclaim(0);
|
---|
[da1bafb] | 873 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f049eec] | 874 |
|
---|
[5f0f29ce] | 875 | if (freed > 0)
|
---|
[b0c2075] | 876 | znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
| 877 | frame_constraint, hint);
|
---|
[5f0f29ce] | 878 |
|
---|
[98000fb] | 879 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 880 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 881 | freed = slab_reclaim(SLAB_RECLAIM_ALL);
|
---|
[da1bafb] | 882 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f049eec] | 883 |
|
---|
[5f0f29ce] | 884 | if (freed > 0)
|
---|
[b0c2075] | 885 | znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
|
---|
| 886 | frame_constraint, hint);
|
---|
[085d973] | 887 | }
|
---|
| 888 | }
|
---|
[5f0f29ce] | 889 |
|
---|
[98000fb] | 890 | if (znum == (size_t) -1) {
|
---|
[1a1744e] | 891 | if (flags & FRAME_ATOMIC) {
|
---|
[da1bafb] | 892 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[b0c2075] | 893 |
|
---|
[89bcb520] | 894 | if (!(flags & FRAME_NO_RESERVE))
|
---|
[b0c2075] | 895 | reserve_free(count);
|
---|
| 896 |
|
---|
[8cbf1c3] | 897 | return 0;
|
---|
[1a1744e] | 898 | }
|
---|
[085d973] | 899 |
|
---|
[1a1744e] | 900 | #ifdef CONFIG_DEBUG
|
---|
[8d308b9] | 901 | size_t avail = frame_total_free_get_internal();
|
---|
[1a1744e] | 902 | #endif
|
---|
[5f0f29ce] | 903 |
|
---|
[da1bafb] | 904 | irq_spinlock_unlock(&zones.lock, true);
|
---|
| 905 |
|
---|
[05411e8] | 906 | if (!THREAD)
|
---|
[adb252c0] | 907 | panic("Cannot wait for %zu frames to become available "
|
---|
| 908 | "(%zu available).", count, avail);
|
---|
[5f0f29ce] | 909 |
|
---|
| 910 | /*
|
---|
| 911 | * Sleep until some frames are available again.
|
---|
| 912 | */
|
---|
| 913 |
|
---|
| 914 | #ifdef CONFIG_DEBUG
|
---|
[adb252c0] | 915 | printf("Thread %" PRIu64 " waiting for %zu frames "
|
---|
| 916 | "(%zu available).\n", THREAD->tid, count, avail);
|
---|
[5f0f29ce] | 917 | #endif
|
---|
| 918 |
|
---|
[905721b] | 919 | /*
|
---|
[b0c2075] | 920 | * Since the mem_avail_mtx is an active mutex, we need to
|
---|
| 921 | * disable interrupts to prevent deadlock with TLB shootdown.
|
---|
[905721b] | 922 | */
|
---|
| 923 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 924 | mutex_lock(&mem_avail_mtx);
|
---|
[5f0f29ce] | 925 |
|
---|
| 926 | if (mem_avail_req > 0)
|
---|
[b0c2075] | 927 | mem_avail_req = min(mem_avail_req, count);
|
---|
[5f0f29ce] | 928 | else
|
---|
[b0c2075] | 929 | mem_avail_req = count;
|
---|
| 930 |
|
---|
[98000fb] | 931 | size_t gen = mem_avail_gen;
|
---|
[5f0f29ce] | 932 |
|
---|
| 933 | while (gen == mem_avail_gen)
|
---|
[1bb3766] | 934 | condvar_wait(&mem_avail_cv, &mem_avail_mtx);
|
---|
[5f0f29ce] | 935 |
|
---|
[1bb3766] | 936 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 937 | interrupts_restore(ipl);
|
---|
[5f0f29ce] | 938 |
|
---|
[1a1744e] | 939 | #ifdef CONFIG_DEBUG
|
---|
[5f0f29ce] | 940 | printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
|
---|
[1a1744e] | 941 | #endif
|
---|
[5f0f29ce] | 942 |
|
---|
[085d973] | 943 | goto loop;
|
---|
| 944 | }
|
---|
[9a68b34d] | 945 |
|
---|
[b0c2075] | 946 | pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
|
---|
| 947 | frame_constraint) + zones.info[znum].base;
|
---|
[1bb3766] | 948 |
|
---|
[da1bafb] | 949 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 950 |
|
---|
| 951 | if (pzone)
|
---|
| 952 | *pzone = znum;
|
---|
| 953 |
|
---|
[8cbf1c3] | 954 | return PFN2ADDR(pfn);
|
---|
[085d973] | 955 | }
|
---|
| 956 |
|
---|
[b0c2075] | 957 | uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
|
---|
[0139747] | 958 | {
|
---|
[b0c2075] | 959 | return frame_alloc_generic(count, flags, constraint, NULL);
|
---|
[0139747] | 960 | }
|
---|
| 961 |
|
---|
[5df1963] | 962 | /** Free frames of physical memory.
|
---|
[085d973] | 963 | *
|
---|
[5df1963] | 964 | * Find respective frame structures for supplied physical frames.
|
---|
| 965 | * Decrement each frame reference count. If it drops to zero, mark
|
---|
| 966 | * the frames as available.
|
---|
[5f0f29ce] | 967 | *
|
---|
[5df1963] | 968 | * @param start Physical Address of the first frame to be freed.
|
---|
| 969 | * @param count Number of frames to free.
|
---|
[c32e6bc] | 970 | * @param flags Flags to control memory reservation.
|
---|
[085d973] | 971 | *
|
---|
| 972 | */
|
---|
[5df1963] | 973 | void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
|
---|
[085d973] | 974 | {
|
---|
[5df1963] | 975 | size_t freed = 0;
|
---|
[5f0f29ce] | 976 |
|
---|
[5df1963] | 977 | irq_spinlock_lock(&zones.lock, true);
|
---|
[085d973] | 978 |
|
---|
[5df1963] | 979 | for (size_t i = 0; i < count; i++) {
|
---|
| 980 | /*
|
---|
| 981 | * First, find host frame zone for addr.
|
---|
| 982 | */
|
---|
| 983 | pfn_t pfn = ADDR2PFN(start) + i;
|
---|
| 984 | size_t znum = find_zone(pfn, 1, 0);
|
---|
| 985 |
|
---|
| 986 | ASSERT(znum != (size_t) -1);
|
---|
| 987 |
|
---|
| 988 | freed += zone_frame_free(&zones.info[znum],
|
---|
| 989 | pfn - zones.info[znum].base);
|
---|
| 990 | }
|
---|
[085d973] | 991 |
|
---|
[da1bafb] | 992 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[1a1744e] | 993 |
|
---|
| 994 | /*
|
---|
| 995 | * Signal that some memory has been freed.
|
---|
[5df1963] | 996 | * Since the mem_avail_mtx is an active mutex,
|
---|
| 997 | * we need to disable interruptsto prevent deadlock
|
---|
| 998 | * with TLB shootdown.
|
---|
[905721b] | 999 | */
|
---|
[b0c2075] | 1000 |
|
---|
[905721b] | 1001 | ipl_t ipl = interrupts_disable();
|
---|
[1bb3766] | 1002 | mutex_lock(&mem_avail_mtx);
|
---|
[b0c2075] | 1003 |
|
---|
[5f0f29ce] | 1004 | if (mem_avail_req > 0)
|
---|
[5df1963] | 1005 | mem_avail_req -= min(mem_avail_req, freed);
|
---|
[5f0f29ce] | 1006 |
|
---|
| 1007 | if (mem_avail_req == 0) {
|
---|
| 1008 | mem_avail_gen++;
|
---|
| 1009 | condvar_broadcast(&mem_avail_cv);
|
---|
| 1010 | }
|
---|
[b0c2075] | 1011 |
|
---|
[1bb3766] | 1012 | mutex_unlock(&mem_avail_mtx);
|
---|
[905721b] | 1013 | interrupts_restore(ipl);
|
---|
[89bcb520] | 1014 |
|
---|
| 1015 | if (!(flags & FRAME_NO_RESERVE))
|
---|
[5df1963] | 1016 | reserve_free(freed);
|
---|
[085d973] | 1017 | }
|
---|
| 1018 |
|
---|
[5df1963] | 1019 | void frame_free(uintptr_t frame, size_t count)
|
---|
[c32e6bc] | 1020 | {
|
---|
[5df1963] | 1021 | frame_free_generic(frame, count, 0);
|
---|
[c32e6bc] | 1022 | }
|
---|
| 1023 |
|
---|
[5df1963] | 1024 | void frame_free_noreserve(uintptr_t frame, size_t count)
|
---|
[e608cbe] | 1025 | {
|
---|
[5df1963] | 1026 | frame_free_generic(frame, count, FRAME_NO_RESERVE);
|
---|
[e608cbe] | 1027 | }
|
---|
| 1028 |
|
---|
[f3ac636] | 1029 | /** Add reference to frame.
|
---|
| 1030 | *
|
---|
| 1031 | * Find respective frame structure for supplied PFN and
|
---|
| 1032 | * increment frame reference count.
|
---|
| 1033 | *
|
---|
[5f0f29ce] | 1034 | * @param pfn Frame number of the frame to be freed.
|
---|
| 1035 | *
|
---|
[f3ac636] | 1036 | */
|
---|
[97bdb4a] | 1037 | NO_TRACE void frame_reference_add(pfn_t pfn)
|
---|
[f3ac636] | 1038 | {
|
---|
[da1bafb] | 1039 | irq_spinlock_lock(&zones.lock, true);
|
---|
[f3ac636] | 1040 |
|
---|
| 1041 | /*
|
---|
| 1042 | * First, find host frame zone for addr.
|
---|
| 1043 | */
|
---|
[0b4a67a] | 1044 | size_t znum = find_zone(pfn, 1, 0);
|
---|
[5f0f29ce] | 1045 |
|
---|
[98000fb] | 1046 | ASSERT(znum != (size_t) -1);
|
---|
[f3ac636] | 1047 |
|
---|
[5f0f29ce] | 1048 | zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
|
---|
[f3ac636] | 1049 |
|
---|
[da1bafb] | 1050 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[f3ac636] | 1051 | }
|
---|
[085d973] | 1052 |
|
---|
[da1bafb] | 1053 | /** Mark given range unavailable in frame zones.
|
---|
| 1054 | *
|
---|
| 1055 | */
|
---|
[97bdb4a] | 1056 | NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
|
---|
[085d973] | 1057 | {
|
---|
[da1bafb] | 1058 | irq_spinlock_lock(&zones.lock, true);
|
---|
[052da81] | 1059 |
|
---|
[b0c2075] | 1060 | for (size_t i = 0; i < count; i++) {
|
---|
[98000fb] | 1061 | size_t znum = find_zone(start + i, 1, 0);
|
---|
[b0c2075] | 1062 |
|
---|
[98000fb] | 1063 | if (znum == (size_t) -1) /* PFN not found */
|
---|
[085d973] | 1064 | continue;
|
---|
[5f0f29ce] | 1065 |
|
---|
| 1066 | zone_mark_unavailable(&zones.info[znum],
|
---|
| 1067 | start + i - zones.info[znum].base);
|
---|
[085d973] | 1068 | }
|
---|
[5f0f29ce] | 1069 |
|
---|
[da1bafb] | 1070 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[085d973] | 1071 | }
|
---|
| 1072 |
|
---|
[da1bafb] | 1073 | /** Initialize physical memory management.
|
---|
| 1074 | *
|
---|
| 1075 | */
|
---|
[085d973] | 1076 | void frame_init(void)
|
---|
| 1077 | {
|
---|
| 1078 | if (config.cpu_active == 1) {
|
---|
| 1079 | zones.count = 0;
|
---|
[da1bafb] | 1080 | irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
|
---|
[1bb3766] | 1081 | mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
|
---|
| 1082 | condvar_initialize(&mem_avail_cv);
|
---|
[085d973] | 1083 | }
|
---|
[5f0f29ce] | 1084 |
|
---|
[085d973] | 1085 | /* Tell the architecture to create some memory */
|
---|
[ddcc8a0] | 1086 | frame_low_arch_init();
|
---|
[b0c2075] | 1087 |
|
---|
[085d973] | 1088 | if (config.cpu_active == 1) {
|
---|
[4638401] | 1089 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
|
---|
| 1090 | SIZE2FRAMES(config.kernel_size));
|
---|
| 1091 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
|
---|
| 1092 | SIZE2FRAMES(config.stack_size));
|
---|
[b6b576c] | 1093 |
|
---|
[b0c2075] | 1094 | for (size_t i = 0; i < init.cnt; i++)
|
---|
| 1095 | frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
|
---|
[4638401] | 1096 | SIZE2FRAMES(init.tasks[i].size));
|
---|
[5f0f29ce] | 1097 |
|
---|
[61e90dd] | 1098 | if (ballocs.size)
|
---|
[4638401] | 1099 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
|
---|
| 1100 | SIZE2FRAMES(ballocs.size));
|
---|
[5f0f29ce] | 1101 |
|
---|
[b0c2075] | 1102 | /*
|
---|
| 1103 | * Blacklist first frame, as allocating NULL would
|
---|
[5f0f29ce] | 1104 | * fail in some places
|
---|
| 1105 | */
|
---|
[bffa0b06] | 1106 | frame_mark_unavailable(0, 1);
|
---|
[085d973] | 1107 | }
|
---|
[b0c2075] | 1108 |
|
---|
[ddcc8a0] | 1109 | frame_high_arch_init();
|
---|
[085d973] | 1110 | }
|
---|
| 1111 |
|
---|
[ad12b5ea] | 1112 | /** Adjust bounds of physical memory region according to low/high memory split.
|
---|
| 1113 | *
|
---|
[b0c2075] | 1114 | * @param low[in] If true, the adjustment is performed to make the region
|
---|
| 1115 | * fit in the low memory. Otherwise the adjustment is
|
---|
| 1116 | * performed to make the region fit in the high memory.
|
---|
| 1117 | * @param basep[inout] Pointer to a variable which contains the region's base
|
---|
| 1118 | * address and which may receive the adjusted base address.
|
---|
| 1119 | * @param sizep[inout] Pointer to a variable which contains the region's size
|
---|
| 1120 | * and which may receive the adjusted size.
|
---|
| 1121 | *
|
---|
| 1122 | * @return True if the region still exists even after the adjustment.
|
---|
| 1123 | * @return False otherwise.
|
---|
| 1124 | *
|
---|
[ad12b5ea] | 1125 | */
|
---|
| 1126 | bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
|
---|
| 1127 | {
|
---|
[2cc7f16] | 1128 | uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
|
---|
[b0c2075] | 1129 |
|
---|
[ad12b5ea] | 1130 | if (low) {
|
---|
| 1131 | if (*basep > limit)
|
---|
| 1132 | return false;
|
---|
[b0c2075] | 1133 |
|
---|
[ad12b5ea] | 1134 | if (*basep + *sizep > limit)
|
---|
| 1135 | *sizep = limit - *basep;
|
---|
| 1136 | } else {
|
---|
| 1137 | if (*basep + *sizep <= limit)
|
---|
| 1138 | return false;
|
---|
[b0c2075] | 1139 |
|
---|
[ad12b5ea] | 1140 | if (*basep <= limit) {
|
---|
| 1141 | *sizep -= limit - *basep;
|
---|
| 1142 | *basep = limit;
|
---|
| 1143 | }
|
---|
| 1144 | }
|
---|
[b0c2075] | 1145 |
|
---|
[ad12b5ea] | 1146 | return true;
|
---|
| 1147 | }
|
---|
| 1148 |
|
---|
[da1bafb] | 1149 | /** Return total size of all zones.
|
---|
| 1150 | *
|
---|
| 1151 | */
|
---|
[9dae191e] | 1152 | uint64_t zones_total_size(void)
|
---|
[deaf8d5] | 1153 | {
|
---|
[da1bafb] | 1154 | irq_spinlock_lock(&zones.lock, true);
|
---|
[71eef11] | 1155 |
|
---|
[5f0f29ce] | 1156 | uint64_t total = 0;
|
---|
[b0c2075] | 1157 |
|
---|
| 1158 | for (size_t i = 0; i < zones.count; i++)
|
---|
[5f0f29ce] | 1159 | total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[71eef11] | 1160 |
|
---|
[da1bafb] | 1161 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[71eef11] | 1162 |
|
---|
| 1163 | return total;
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
[9dae191e] | 1166 | void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
|
---|
| 1167 | uint64_t *free)
|
---|
[516adce] | 1168 | {
|
---|
[9dae191e] | 1169 | ASSERT(total != NULL);
|
---|
| 1170 | ASSERT(unavail != NULL);
|
---|
| 1171 | ASSERT(busy != NULL);
|
---|
| 1172 | ASSERT(free != NULL);
|
---|
| 1173 |
|
---|
[da1bafb] | 1174 | irq_spinlock_lock(&zones.lock, true);
|
---|
[9dae191e] | 1175 |
|
---|
| 1176 | *total = 0;
|
---|
| 1177 | *unavail = 0;
|
---|
| 1178 | *busy = 0;
|
---|
| 1179 | *free = 0;
|
---|
| 1180 |
|
---|
[b0c2075] | 1181 | for (size_t i = 0; i < zones.count; i++) {
|
---|
[9dae191e] | 1182 | *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
| 1183 |
|
---|
[e6c4b94] | 1184 | if (zones.info[i].flags & ZONE_AVAILABLE) {
|
---|
[9dae191e] | 1185 | *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
|
---|
| 1186 | *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
|
---|
| 1187 | } else
|
---|
| 1188 | *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
|
---|
[516adce] | 1189 | }
|
---|
[9dae191e] | 1190 |
|
---|
[da1bafb] | 1191 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[516adce] | 1192 | }
|
---|
| 1193 |
|
---|
[da1bafb] | 1194 | /** Prints list of zones.
|
---|
| 1195 | *
|
---|
| 1196 | */
|
---|
[9dae191e] | 1197 | void zones_print_list(void)
|
---|
[deaf8d5] | 1198 | {
|
---|
[5f0f29ce] | 1199 | #ifdef __32_BITS__
|
---|
[74c5a1ca] | 1200 | printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1201 | #endif
|
---|
| 1202 |
|
---|
| 1203 | #ifdef __64_BITS__
|
---|
[74c5a1ca] | 1204 | printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
|
---|
[2b8b0ca] | 1205 | #endif
|
---|
[43b1e86] | 1206 |
|
---|
[000350f8] | 1207 | /*
|
---|
| 1208 | * Because printing may require allocation of memory, we may not hold
|
---|
[f72906c] | 1209 | * the frame allocator locks when printing zone statistics. Therefore,
|
---|
[000350f8] | 1210 | * we simply gather the statistics under the protection of the locks and
|
---|
| 1211 | * print the statistics when the locks have been released.
|
---|
| 1212 | *
|
---|
| 1213 | * When someone adds/removes zones while we are printing the statistics,
|
---|
| 1214 | * we may end up with inaccurate output (e.g. a zone being skipped from
|
---|
| 1215 | * the listing).
|
---|
| 1216 | */
|
---|
[5f0f29ce] | 1217 |
|
---|
[f72906c] | 1218 | size_t free_lowmem = 0;
|
---|
| 1219 | size_t free_highmem = 0;
|
---|
| 1220 | size_t free_highprio = 0;
|
---|
| 1221 |
|
---|
[b0c2075] | 1222 | for (size_t i = 0;; i++) {
|
---|
[da1bafb] | 1223 | irq_spinlock_lock(&zones.lock, true);
|
---|
[000350f8] | 1224 |
|
---|
| 1225 | if (i >= zones.count) {
|
---|
[da1bafb] | 1226 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[000350f8] | 1227 | break;
|
---|
| 1228 | }
|
---|
[5f0f29ce] | 1229 |
|
---|
[f72906c] | 1230 | pfn_t fbase = zones.info[i].base;
|
---|
| 1231 | uintptr_t base = PFN2ADDR(fbase);
|
---|
[98000fb] | 1232 | size_t count = zones.info[i].count;
|
---|
[5f0f29ce] | 1233 | zone_flags_t flags = zones.info[i].flags;
|
---|
[98000fb] | 1234 | size_t free_count = zones.info[i].free_count;
|
---|
| 1235 | size_t busy_count = zones.info[i].busy_count;
|
---|
[000350f8] | 1236 |
|
---|
[e6c4b94] | 1237 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[f72906c] | 1238 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
---|
| 1239 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
---|
| 1240 | bool highprio = is_high_priority(fbase, count);
|
---|
| 1241 |
|
---|
| 1242 | if (available) {
|
---|
| 1243 | if (lowmem)
|
---|
| 1244 | free_lowmem += free_count;
|
---|
| 1245 |
|
---|
| 1246 | if (highmem)
|
---|
| 1247 | free_highmem += free_count;
|
---|
| 1248 |
|
---|
| 1249 | if (highprio) {
|
---|
| 1250 | free_highprio += free_count;
|
---|
| 1251 | } else {
|
---|
| 1252 | /*
|
---|
| 1253 | * Walk all frames of the zone and examine
|
---|
| 1254 | * all high priority memory to get accurate
|
---|
| 1255 | * statistics.
|
---|
| 1256 | */
|
---|
| 1257 |
|
---|
| 1258 | for (size_t index = 0; index < count; index++) {
|
---|
| 1259 | if (is_high_priority(fbase + index, 0)) {
|
---|
| 1260 | if (!bitmap_get(&zones.info[i].bitmap, index))
|
---|
| 1261 | free_highprio++;
|
---|
| 1262 | } else
|
---|
| 1263 | break;
|
---|
| 1264 | }
|
---|
| 1265 | }
|
---|
| 1266 | }
|
---|
| 1267 |
|
---|
| 1268 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 1269 |
|
---|
[7e752b2] | 1270 | printf("%-4zu", i);
|
---|
[e49e234] | 1271 |
|
---|
[2b8b0ca] | 1272 | #ifdef __32_BITS__
|
---|
[7e752b2] | 1273 | printf(" %p", (void *) base);
|
---|
[2b8b0ca] | 1274 | #endif
|
---|
[5f0f29ce] | 1275 |
|
---|
[2b8b0ca] | 1276 | #ifdef __64_BITS__
|
---|
[7e752b2] | 1277 | printf(" %p", (void *) base);
|
---|
[e49e234] | 1278 | #endif
|
---|
| 1279 |
|
---|
[e6c4b94] | 1280 | printf(" %12zu %c%c%c%c%c ", count,
|
---|
| 1281 | available ? 'A' : '-',
|
---|
| 1282 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1283 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1284 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1285 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[43b1e86] | 1286 |
|
---|
[5f0f29ce] | 1287 | if (available)
|
---|
[7e752b2] | 1288 | printf("%14zu %14zu",
|
---|
[5f0f29ce] | 1289 | free_count, busy_count);
|
---|
[e49e234] | 1290 |
|
---|
[5f0f29ce] | 1291 | printf("\n");
|
---|
[dfd9186] | 1292 | }
|
---|
[f72906c] | 1293 |
|
---|
| 1294 | printf("\n");
|
---|
| 1295 |
|
---|
| 1296 | uint64_t size;
|
---|
| 1297 | const char *size_suffix;
|
---|
| 1298 |
|
---|
| 1299 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
---|
| 1300 | false);
|
---|
| 1301 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1302 | free_lowmem, size, size_suffix);
|
---|
| 1303 |
|
---|
| 1304 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
---|
| 1305 | false);
|
---|
| 1306 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1307 | free_highmem, size, size_suffix);
|
---|
| 1308 |
|
---|
| 1309 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
---|
| 1310 | false);
|
---|
| 1311 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1312 | free_highprio, size, size_suffix);
|
---|
[dfd9186] | 1313 | }
|
---|
| 1314 |
|
---|
[abbc16e] | 1315 | /** Prints zone details.
|
---|
[96cacc1] | 1316 | *
|
---|
[5f0f29ce] | 1317 | * @param num Zone base address or zone number.
|
---|
| 1318 | *
|
---|
[96cacc1] | 1319 | */
|
---|
[98000fb] | 1320 | void zone_print_one(size_t num)
|
---|
[deaf8d5] | 1321 | {
|
---|
[da1bafb] | 1322 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 1323 | size_t znum = (size_t) -1;
|
---|
[5f0f29ce] | 1324 |
|
---|
[b0c2075] | 1325 | for (size_t i = 0; i < zones.count; i++) {
|
---|
[5f0f29ce] | 1326 | if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
|
---|
| 1327 | znum = i;
|
---|
[bb68433] | 1328 | break;
|
---|
| 1329 | }
|
---|
| 1330 | }
|
---|
[5f0f29ce] | 1331 |
|
---|
[98000fb] | 1332 | if (znum == (size_t) -1) {
|
---|
[da1bafb] | 1333 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[bb68433] | 1334 | printf("Zone not found.\n");
|
---|
[2ec725f] | 1335 | return;
|
---|
[dfd9186] | 1336 | }
|
---|
[085d973] | 1337 |
|
---|
[f72906c] | 1338 | size_t free_lowmem = 0;
|
---|
| 1339 | size_t free_highmem = 0;
|
---|
| 1340 | size_t free_highprio = 0;
|
---|
| 1341 |
|
---|
| 1342 | pfn_t fbase = zones.info[znum].base;
|
---|
| 1343 | uintptr_t base = PFN2ADDR(fbase);
|
---|
[b0c2075] | 1344 | zone_flags_t flags = zones.info[znum].flags;
|
---|
| 1345 | size_t count = zones.info[znum].count;
|
---|
| 1346 | size_t free_count = zones.info[znum].free_count;
|
---|
| 1347 | size_t busy_count = zones.info[znum].busy_count;
|
---|
[5f0f29ce] | 1348 |
|
---|
[e6c4b94] | 1349 | bool available = ((flags & ZONE_AVAILABLE) != 0);
|
---|
[f72906c] | 1350 | bool lowmem = ((flags & ZONE_LOWMEM) != 0);
|
---|
| 1351 | bool highmem = ((flags & ZONE_HIGHMEM) != 0);
|
---|
| 1352 | bool highprio = is_high_priority(fbase, count);
|
---|
| 1353 |
|
---|
| 1354 | if (available) {
|
---|
| 1355 | if (lowmem)
|
---|
| 1356 | free_lowmem = free_count;
|
---|
| 1357 |
|
---|
| 1358 | if (highmem)
|
---|
| 1359 | free_highmem = free_count;
|
---|
| 1360 |
|
---|
| 1361 | if (highprio) {
|
---|
| 1362 | free_highprio = free_count;
|
---|
| 1363 | } else {
|
---|
| 1364 | /*
|
---|
| 1365 | * Walk all frames of the zone and examine
|
---|
| 1366 | * all high priority memory to get accurate
|
---|
| 1367 | * statistics.
|
---|
| 1368 | */
|
---|
| 1369 |
|
---|
| 1370 | for (size_t index = 0; index < count; index++) {
|
---|
| 1371 | if (is_high_priority(fbase + index, 0)) {
|
---|
| 1372 | if (!bitmap_get(&zones.info[znum].bitmap, index))
|
---|
| 1373 | free_highprio++;
|
---|
| 1374 | } else
|
---|
| 1375 | break;
|
---|
| 1376 | }
|
---|
| 1377 | }
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[5f0f29ce] | 1381 |
|
---|
[933cadf] | 1382 | uint64_t size;
|
---|
| 1383 | const char *size_suffix;
|
---|
[f72906c] | 1384 |
|
---|
[933cadf] | 1385 | bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
|
---|
| 1386 |
|
---|
[f72906c] | 1387 | printf("Zone number: %zu\n", znum);
|
---|
| 1388 | printf("Zone base address: %p\n", (void *) base);
|
---|
| 1389 | printf("Zone size: %zu frames (%" PRIu64 " %s)\n", count,
|
---|
[933cadf] | 1390 | size, size_suffix);
|
---|
[f72906c] | 1391 | printf("Zone flags: %c%c%c%c%c\n",
|
---|
[e6c4b94] | 1392 | available ? 'A' : '-',
|
---|
| 1393 | (flags & ZONE_RESERVED) ? 'R' : '-',
|
---|
| 1394 | (flags & ZONE_FIRMWARE) ? 'F' : '-',
|
---|
| 1395 | (flags & ZONE_LOWMEM) ? 'L' : '-',
|
---|
| 1396 | (flags & ZONE_HIGHMEM) ? 'H' : '-');
|
---|
[5f0f29ce] | 1397 |
|
---|
| 1398 | if (available) {
|
---|
[933cadf] | 1399 | bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
|
---|
| 1400 | false);
|
---|
[f72906c] | 1401 | printf("Allocated space: %zu frames (%" PRIu64 " %s)\n",
|
---|
[933cadf] | 1402 | busy_count, size, size_suffix);
|
---|
[f72906c] | 1403 |
|
---|
[933cadf] | 1404 | bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
|
---|
| 1405 | false);
|
---|
[f72906c] | 1406 | printf("Available space: %zu frames (%" PRIu64 " %s)\n",
|
---|
[933cadf] | 1407 | free_count, size, size_suffix);
|
---|
[f72906c] | 1408 |
|
---|
| 1409 | bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
|
---|
| 1410 | false);
|
---|
| 1411 | printf("Available low memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1412 | free_lowmem, size, size_suffix);
|
---|
| 1413 |
|
---|
| 1414 | bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
|
---|
| 1415 | false);
|
---|
| 1416 | printf("Available high memory: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1417 | free_highmem, size, size_suffix);
|
---|
| 1418 |
|
---|
| 1419 | bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
|
---|
| 1420 | false);
|
---|
| 1421 | printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
|
---|
| 1422 | free_highprio, size, size_suffix);
|
---|
[5f0f29ce] | 1423 | }
|
---|
[dfd9186] | 1424 | }
|
---|
| 1425 |
|
---|
[cc73a8a1] | 1426 | /** @}
|
---|
[b45c443] | 1427 | */
|
---|