Rev 3205 | Rev 3207 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2001-2005 Jakub Jermar |
3 | * Copyright (c) 2005 Sergey Bondari |
||
1 | jermar | 4 | * All rights reserved. |
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
1757 | jermar | 30 | /** @addtogroup genericmm |
1702 | cejka | 31 | * @{ |
32 | */ |
||
33 | |||
1248 | jermar | 34 | /** |
1702 | cejka | 35 | * @file |
1248 | jermar | 36 | * @brief Physical frame allocator. |
37 | * |
||
38 | * This file contains the physical frame allocator and memory zone management. |
||
39 | * The frame allocator is built on top of the buddy allocator. |
||
40 | * |
||
41 | * @see buddy.c |
||
42 | */ |
||
43 | |||
814 | palkovsky | 44 | /* |
45 | * Locking order |
||
46 | * |
||
47 | * In order to access particular zone, the process must first lock |
||
48 | * the zones.lock, then lock the zone and then unlock the zones.lock. |
||
49 | * This insures, that we can fiddle with the zones in runtime without |
||
50 | * affecting the processes. |
||
51 | * |
||
52 | */ |
||
53 | |||
1 | jermar | 54 | #include <arch/types.h> |
55 | #include <mm/frame.h> |
||
703 | jermar | 56 | #include <mm/as.h> |
1 | jermar | 57 | #include <panic.h> |
367 | jermar | 58 | #include <debug.h> |
788 | jermar | 59 | #include <adt/list.h> |
1 | jermar | 60 | #include <synch/spinlock.h> |
3187 | jermar | 61 | #include <synch/mutex.h> |
62 | #include <synch/condvar.h> |
||
115 | jermar | 63 | #include <arch/asm.h> |
195 | vana | 64 | #include <arch.h> |
533 | bondari | 65 | #include <print.h> |
536 | bondari | 66 | #include <align.h> |
814 | palkovsky | 67 | #include <mm/slab.h> |
822 | palkovsky | 68 | #include <bitops.h> |
1063 | palkovsky | 69 | #include <macros.h> |
2128 | jermar | 70 | #include <config.h> |
115 | jermar | 71 | |
814 | palkovsky | 72 | typedef struct { |
73 | count_t refcount; /**< tracking of shared frames */ |
||
1780 | jermar | 74 | uint8_t buddy_order; /**< buddy system block order */ |
2133 | jermar | 75 | link_t buddy_link; /**< link to the next free block inside one |
76 | order */ |
||
814 | palkovsky | 77 | void *parent; /**< If allocated by slab, this points there */ |
1236 | jermar | 78 | } frame_t; |
367 | jermar | 79 | |
814 | palkovsky | 80 | typedef struct { |
81 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
||
2133 | jermar | 82 | pfn_t base; /**< frame_no of the first frame in the frames |
83 | array */ |
||
820 | jermar | 84 | count_t count; /**< Size of zone */ |
533 | bondari | 85 | |
2133 | jermar | 86 | frame_t *frames; /**< array of frame_t structures in this |
87 | zone */ |
||
814 | palkovsky | 88 | count_t free_count; /**< number of free frame_t structures */ |
89 | count_t busy_count; /**< number of busy frame_t structures */ |
||
90 | |||
2059 | jermar | 91 | buddy_system_t *buddy_system; /**< buddy system for the zone */ |
814 | palkovsky | 92 | int flags; |
1236 | jermar | 93 | } zone_t; |
479 | bondari | 94 | |
814 | palkovsky | 95 | /* |
96 | * The zoneinfo.lock must be locked when accessing zoneinfo structure. |
||
97 | * Some of the attributes in zone_t structures are 'read-only' |
||
368 | jermar | 98 | */ |
1 | jermar | 99 | |
2121 | decky | 100 | typedef struct { |
814 | palkovsky | 101 | SPINLOCK_DECLARE(lock); |
2122 | decky | 102 | unsigned int count; |
814 | palkovsky | 103 | zone_t *info[ZONES_MAX]; |
2121 | decky | 104 | } zones_t; |
1 | jermar | 105 | |
2121 | decky | 106 | static zones_t zones; |
814 | palkovsky | 107 | |
3187 | jermar | 108 | /* |
109 | * Synchronization primitives used to sleep when there is no memory |
||
110 | * available. |
||
111 | */ |
||
3188 | jermar | 112 | mutex_t mem_avail_mtx; |
113 | condvar_t mem_avail_cv; |
||
114 | unsigned long mem_avail_frames = 0; /**< Number of available frames. */ |
||
115 | unsigned long mem_avail_gen = 0; /**< Generation counter. */ |
||
2121 | decky | 116 | |
2725 | decky | 117 | /********************/ |
814 | palkovsky | 118 | /* Helper functions */ |
2725 | decky | 119 | /********************/ |
120 | |||
814 | palkovsky | 121 | static inline index_t frame_index(zone_t *zone, frame_t *frame) |
762 | palkovsky | 122 | { |
2725 | decky | 123 | return (index_t) (frame - zone->frames); |
762 | palkovsky | 124 | } |
2725 | decky | 125 | |
814 | palkovsky | 126 | static inline index_t frame_index_abs(zone_t *zone, frame_t *frame) |
1 | jermar | 127 | { |
2725 | decky | 128 | return (index_t) (frame - zone->frames) + zone->base; |
1 | jermar | 129 | } |
2725 | decky | 130 | |
814 | palkovsky | 131 | static inline int frame_index_valid(zone_t *zone, index_t index) |
132 | { |
||
2745 | decky | 133 | return (index < zone->count); |
814 | palkovsky | 134 | } |
1 | jermar | 135 | |
814 | palkovsky | 136 | /** Compute pfn_t from frame_t pointer & zone pointer */ |
820 | jermar | 137 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
762 | palkovsky | 138 | { |
2725 | decky | 139 | return (frame - zone->frames); |
762 | palkovsky | 140 | } |
141 | |||
3185 | jermar | 142 | /** Initialize frame structure. |
762 | palkovsky | 143 | * |
3188 | jermar | 144 | * @param frame Frame structure to be initialized. |
762 | palkovsky | 145 | */ |
814 | palkovsky | 146 | static void frame_initialize(frame_t *frame) |
762 | palkovsky | 147 | { |
814 | palkovsky | 148 | frame->refcount = 1; |
149 | frame->buddy_order = 0; |
||
762 | palkovsky | 150 | } |
151 | |||
2725 | decky | 152 | /**********************/ |
814 | palkovsky | 153 | /* Zoneinfo functions */ |
2725 | decky | 154 | /**********************/ |
762 | palkovsky | 155 | |
3185 | jermar | 156 | /** Insert-sort zone into zones list. |
822 | palkovsky | 157 | * |
3185 | jermar | 158 | * @param newzone New zone to be inserted into zone list. |
159 | * @return Zone number on success, -1 on error. |
||
1 | jermar | 160 | */ |
822 | palkovsky | 161 | static int zones_add_zone(zone_t *newzone) |
1 | jermar | 162 | { |
2122 | decky | 163 | unsigned int i, j; |
822 | palkovsky | 164 | ipl_t ipl; |
165 | zone_t *z; |
||
762 | palkovsky | 166 | |
822 | palkovsky | 167 | ipl = interrupts_disable(); |
814 | palkovsky | 168 | spinlock_lock(&zones.lock); |
2725 | decky | 169 | |
814 | palkovsky | 170 | /* Try to merge */ |
2725 | decky | 171 | if (zones.count + 1 == ZONES_MAX) { |
172 | printf("Maximum zone count %u exceeded!\n", ZONES_MAX); |
||
173 | spinlock_unlock(&zones.lock); |
||
174 | interrupts_restore(ipl); |
||
175 | return -1; |
||
176 | } |
||
177 | |||
1037 | decky | 178 | for (i = 0; i < zones.count; i++) { |
822 | palkovsky | 179 | /* Check for overflow */ |
852 | palkovsky | 180 | z = zones.info[i]; |
3185 | jermar | 181 | if (overlaps(newzone->base, newzone->count, z->base, |
182 | z->count)) { |
||
822 | palkovsky | 183 | printf("Zones overlap!\n"); |
184 | return -1; |
||
814 | palkovsky | 185 | } |
852 | palkovsky | 186 | if (newzone->base < z->base) |
822 | palkovsky | 187 | break; |
368 | jermar | 188 | } |
2725 | decky | 189 | |
822 | palkovsky | 190 | /* Move other zones up */ |
2133 | jermar | 191 | for (j = i; j < zones.count; j++) |
1037 | decky | 192 | zones.info[j + 1] = zones.info[j]; |
2725 | decky | 193 | |
822 | palkovsky | 194 | zones.info[i] = newzone; |
195 | zones.count++; |
||
2725 | decky | 196 | |
814 | palkovsky | 197 | spinlock_unlock(&zones.lock); |
822 | palkovsky | 198 | interrupts_restore(ipl); |
199 | |||
200 | return i; |
||
1 | jermar | 201 | } |
202 | |||
814 | palkovsky | 203 | /** |
3185 | jermar | 204 | * Try to find a zone where can we find the frame. |
2725 | decky | 205 | * |
2059 | jermar | 206 | * Assume interrupts are disabled. |
2725 | decky | 207 | * |
3185 | jermar | 208 | * @param frame Frame number contained in zone. |
209 | * @param pzone If not null, it is used as zone hint. Zone index is |
||
210 | * filled into the variable on success. |
||
211 | * @return Pointer to locked zone containing frame. |
||
368 | jermar | 212 | */ |
3185 | jermar | 213 | static zone_t *find_zone_and_lock(pfn_t frame, unsigned int *pzone) |
1 | jermar | 214 | { |
2122 | decky | 215 | unsigned int i; |
216 | unsigned int hint = pzone ? *pzone : 0; |
||
814 | palkovsky | 217 | zone_t *z; |
533 | bondari | 218 | |
814 | palkovsky | 219 | spinlock_lock(&zones.lock); |
220 | |||
2745 | decky | 221 | if (hint >= zones.count) |
814 | palkovsky | 222 | hint = 0; |
533 | bondari | 223 | |
814 | palkovsky | 224 | i = hint; |
225 | do { |
||
226 | z = zones.info[i]; |
||
227 | spinlock_lock(&z->lock); |
||
228 | if (z->base <= frame && z->base + z->count > frame) { |
||
2133 | jermar | 229 | /* Unlock the global lock */ |
230 | spinlock_unlock(&zones.lock); |
||
814 | palkovsky | 231 | if (pzone) |
232 | *pzone = i; |
||
233 | return z; |
||
533 | bondari | 234 | } |
814 | palkovsky | 235 | spinlock_unlock(&z->lock); |
533 | bondari | 236 | |
814 | palkovsky | 237 | i++; |
238 | if (i >= zones.count) |
||
239 | i = 0; |
||
240 | } while(i != hint); |
||
241 | |||
242 | spinlock_unlock(&zones.lock); |
||
243 | return NULL; |
||
533 | bondari | 244 | } |
245 | |||
822 | palkovsky | 246 | /** @return True if zone can allocate specified order */ |
1780 | jermar | 247 | static int zone_can_alloc(zone_t *z, uint8_t order) |
822 | palkovsky | 248 | { |
249 | return buddy_system_can_alloc(z->buddy_system, order); |
||
250 | } |
||
251 | |||
2059 | jermar | 252 | /** Find and lock zone that can allocate order frames. |
367 | jermar | 253 | * |
2059 | jermar | 254 | * Assume interrupts are disabled. |
367 | jermar | 255 | * |
3185 | jermar | 256 | * @param order Size (2^order) of free space we are trying to find. |
257 | * @param pzone Pointer to preferred zone or NULL, on return contains |
||
258 | * zone number. |
||
367 | jermar | 259 | */ |
3185 | jermar | 260 | static zone_t *find_free_zone_and_lock(uint8_t order, unsigned int *pzone) |
367 | jermar | 261 | { |
2122 | decky | 262 | unsigned int i; |
367 | jermar | 263 | zone_t *z; |
2122 | decky | 264 | unsigned int hint = pzone ? *pzone : 0; |
367 | jermar | 265 | |
814 | palkovsky | 266 | spinlock_lock(&zones.lock); |
267 | if (hint >= zones.count) |
||
268 | hint = 0; |
||
269 | i = hint; |
||
270 | do { |
||
271 | z = zones.info[i]; |
||
724 | palkovsky | 272 | |
814 | palkovsky | 273 | spinlock_lock(&z->lock); |
367 | jermar | 274 | |
814 | palkovsky | 275 | /* Check if the zone has 2^order frames area available */ |
822 | palkovsky | 276 | if (zone_can_alloc(z, order)) { |
814 | palkovsky | 277 | spinlock_unlock(&zones.lock); |
278 | if (pzone) |
||
279 | *pzone = i; |
||
280 | return z; |
||
367 | jermar | 281 | } |
814 | palkovsky | 282 | spinlock_unlock(&z->lock); |
283 | if (++i >= zones.count) |
||
284 | i = 0; |
||
285 | } while(i != hint); |
||
286 | spinlock_unlock(&zones.lock); |
||
287 | return NULL; |
||
367 | jermar | 288 | } |
289 | |||
2059 | jermar | 290 | /**************************/ |
814 | palkovsky | 291 | /* Buddy system functions */ |
2059 | jermar | 292 | /**************************/ |
814 | palkovsky | 293 | |
3185 | jermar | 294 | /** Buddy system find_block implementation. |
367 | jermar | 295 | * |
814 | palkovsky | 296 | * Find block that is parent of current list. |
297 | * That means go to lower addresses, until such block is found |
||
367 | jermar | 298 | * |
3185 | jermar | 299 | * @param order Order of parent must be different then this |
300 | * parameter!! |
||
367 | jermar | 301 | */ |
814 | palkovsky | 302 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
2133 | jermar | 303 | uint8_t order) |
367 | jermar | 304 | { |
2133 | jermar | 305 | frame_t *frame; |
306 | zone_t *zone; |
||
814 | palkovsky | 307 | index_t index; |
367 | jermar | 308 | |
814 | palkovsky | 309 | frame = list_get_instance(child, frame_t, buddy_link); |
310 | zone = (zone_t *) b->data; |
||
367 | jermar | 311 | |
814 | palkovsky | 312 | index = frame_index(zone, frame); |
313 | do { |
||
314 | if (zone->frames[index].buddy_order != order) { |
||
315 | return &zone->frames[index].buddy_link; |
||
316 | } |
||
317 | } while(index-- > 0); |
||
318 | return NULL; |
||
367 | jermar | 319 | } |
479 | bondari | 320 | |
3185 | jermar | 321 | /** Buddy system find_buddy implementation. |
489 | jermar | 322 | * |
3185 | jermar | 323 | * @param b Buddy system. |
324 | * @param block Block for which buddy should be found. |
||
479 | bondari | 325 | * |
3185 | jermar | 326 | * @return Buddy for given block if found. |
479 | bondari | 327 | */ |
2133 | jermar | 328 | static link_t *zone_buddy_find_buddy(buddy_system_t *b, link_t *block) |
814 | palkovsky | 329 | { |
2133 | jermar | 330 | frame_t *frame; |
331 | zone_t *zone; |
||
564 | jermar | 332 | index_t index; |
480 | bondari | 333 | bool is_left, is_right; |
479 | bondari | 334 | |
480 | bondari | 335 | frame = list_get_instance(block, frame_t, buddy_link); |
533 | bondari | 336 | zone = (zone_t *) b->data; |
2133 | jermar | 337 | ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), |
338 | frame->buddy_order)); |
||
480 | bondari | 339 | |
724 | palkovsky | 340 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
341 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
||
814 | palkovsky | 342 | |
564 | jermar | 343 | ASSERT(is_left ^ is_right); |
533 | bondari | 344 | if (is_left) { |
3185 | jermar | 345 | index = (frame_index(zone, frame)) + |
346 | (1 << frame->buddy_order); |
||
1981 | jermar | 347 | } else { /* if (is_right) */ |
3185 | jermar | 348 | index = (frame_index(zone, frame)) - |
349 | (1 << frame->buddy_order); |
||
533 | bondari | 350 | } |
351 | |||
814 | palkovsky | 352 | if (frame_index_valid(zone, index)) { |
353 | if (zone->frames[index].buddy_order == frame->buddy_order && |
||
354 | zone->frames[index].refcount == 0) { |
||
533 | bondari | 355 | return &zone->frames[index].buddy_link; |
480 | bondari | 356 | } |
357 | } |
||
814 | palkovsky | 358 | |
539 | jermar | 359 | return NULL; |
479 | bondari | 360 | } |
361 | |||
3185 | jermar | 362 | /** Buddy system bisect implementation. |
479 | bondari | 363 | * |
3185 | jermar | 364 | * @param b Buddy system. |
365 | * @param block Block to bisect. |
||
480 | bondari | 366 | * |
3185 | jermar | 367 | * @return Right block. |
479 | bondari | 368 | */ |
3185 | jermar | 369 | static link_t *zone_buddy_bisect(buddy_system_t *b, link_t *block) |
370 | { |
||
2133 | jermar | 371 | frame_t *frame_l, *frame_r; |
564 | jermar | 372 | |
480 | bondari | 373 | frame_l = list_get_instance(block, frame_t, buddy_link); |
533 | bondari | 374 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
564 | jermar | 375 | |
480 | bondari | 376 | return &frame_r->buddy_link; |
479 | bondari | 377 | } |
378 | |||
3185 | jermar | 379 | /** Buddy system coalesce implementation. |
479 | bondari | 380 | * |
3185 | jermar | 381 | * @param b Buddy system. |
382 | * @param block_1 First block. |
||
383 | * @param block_2 First block's buddy. |
||
480 | bondari | 384 | * |
3185 | jermar | 385 | * @return Coalesced block (actually block that represents lower |
386 | * address). |
||
479 | bondari | 387 | */ |
2133 | jermar | 388 | static link_t *zone_buddy_coalesce(buddy_system_t *b, link_t *block_1, |
389 | link_t *block_2) |
||
822 | palkovsky | 390 | { |
814 | palkovsky | 391 | frame_t *frame1, *frame2; |
564 | jermar | 392 | |
480 | bondari | 393 | frame1 = list_get_instance(block_1, frame_t, buddy_link); |
394 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
||
564 | jermar | 395 | |
533 | bondari | 396 | return frame1 < frame2 ? block_1 : block_2; |
479 | bondari | 397 | } |
398 | |||
3185 | jermar | 399 | /** Buddy system set_order implementation. |
489 | jermar | 400 | * |
3185 | jermar | 401 | * @param b Buddy system. |
402 | * @param block Buddy system block. |
||
403 | * @param order Order to set. |
||
479 | bondari | 404 | */ |
2133 | jermar | 405 | static void zone_buddy_set_order(buddy_system_t *b, link_t *block, |
3185 | jermar | 406 | uint8_t order) |
407 | { |
||
2133 | jermar | 408 | frame_t *frame; |
480 | bondari | 409 | frame = list_get_instance(block, frame_t, buddy_link); |
410 | frame->buddy_order = order; |
||
479 | bondari | 411 | } |
412 | |||
3185 | jermar | 413 | /** Buddy system get_order implementation. |
489 | jermar | 414 | * |
3185 | jermar | 415 | * @param b Buddy system. |
416 | * @param block Buddy system block. |
||
479 | bondari | 417 | * |
3185 | jermar | 418 | * @return Order of block. |
479 | bondari | 419 | */ |
3185 | jermar | 420 | static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t *block) |
421 | { |
||
2133 | jermar | 422 | frame_t *frame; |
480 | bondari | 423 | frame = list_get_instance(block, frame_t, buddy_link); |
424 | return frame->buddy_order; |
||
479 | bondari | 425 | } |
533 | bondari | 426 | |
3185 | jermar | 427 | /** Buddy system mark_busy implementation. |
533 | bondari | 428 | * |
3185 | jermar | 429 | * @param b Buddy system. |
430 | * @param block Buddy system block. |
||
533 | bondari | 431 | */ |
3185 | jermar | 432 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) |
433 | { |
||
533 | bondari | 434 | frame_t * frame; |
822 | palkovsky | 435 | |
533 | bondari | 436 | frame = list_get_instance(block, frame_t, buddy_link); |
437 | frame->refcount = 1; |
||
438 | } |
||
676 | bondari | 439 | |
3185 | jermar | 440 | /** Buddy system mark_available implementation. |
814 | palkovsky | 441 | * |
3185 | jermar | 442 | * @param b Buddy system. |
443 | * @param block Buddy system block. |
||
814 | palkovsky | 444 | */ |
3185 | jermar | 445 | static void zone_buddy_mark_available(buddy_system_t *b, link_t *block) |
446 | { |
||
2133 | jermar | 447 | frame_t *frame; |
814 | palkovsky | 448 | frame = list_get_instance(block, frame_t, buddy_link); |
449 | frame->refcount = 0; |
||
450 | } |
||
451 | |||
2083 | decky | 452 | static buddy_system_operations_t zone_buddy_system_operations = { |
814 | palkovsky | 453 | .find_buddy = zone_buddy_find_buddy, |
454 | .bisect = zone_buddy_bisect, |
||
455 | .coalesce = zone_buddy_coalesce, |
||
456 | .set_order = zone_buddy_set_order, |
||
457 | .get_order = zone_buddy_get_order, |
||
458 | .mark_busy = zone_buddy_mark_busy, |
||
459 | .mark_available = zone_buddy_mark_available, |
||
3206 | jermar | 460 | .find_block = zone_buddy_find_block |
814 | palkovsky | 461 | }; |
462 | |||
2059 | jermar | 463 | /******************/ |
814 | palkovsky | 464 | /* Zone functions */ |
2059 | jermar | 465 | /******************/ |
814 | palkovsky | 466 | |
3185 | jermar | 467 | /** Allocate frame in particular zone. |
814 | palkovsky | 468 | * |
3185 | jermar | 469 | * Assume zone is locked. |
1269 | decky | 470 | * Panics if allocation is impossible. |
814 | palkovsky | 471 | * |
3185 | jermar | 472 | * @param zone Zone to allocate from. |
473 | * @param order Allocate exactly 2^order frames. |
||
1269 | decky | 474 | * |
3185 | jermar | 475 | * @return Frame index in zone. |
1269 | decky | 476 | * |
814 | palkovsky | 477 | */ |
1780 | jermar | 478 | static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order) |
814 | palkovsky | 479 | { |
480 | pfn_t v; |
||
481 | link_t *tmp; |
||
482 | frame_t *frame; |
||
483 | |||
484 | /* Allocate frames from zone buddy system */ |
||
485 | tmp = buddy_system_alloc(zone->buddy_system, order); |
||
486 | |||
487 | ASSERT(tmp); |
||
488 | |||
489 | /* Update zone information. */ |
||
490 | zone->free_count -= (1 << order); |
||
491 | zone->busy_count += (1 << order); |
||
492 | |||
493 | /* Frame will be actually a first frame of the block. */ |
||
494 | frame = list_get_instance(tmp, frame_t, buddy_link); |
||
495 | |||
496 | /* get frame address */ |
||
497 | v = make_frame_index(zone, frame); |
||
498 | return v; |
||
499 | } |
||
500 | |||
3185 | jermar | 501 | /** Free frame from zone. |
814 | palkovsky | 502 | * |
3185 | jermar | 503 | * Assume zone is locked. |
1568 | palkovsky | 504 | * |
3185 | jermar | 505 | * @param zone Pointer to zone from which the frame is to be freed. |
506 | * @param frame_idx Frame index relative to zone. |
||
814 | palkovsky | 507 | */ |
820 | jermar | 508 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 509 | { |
510 | frame_t *frame; |
||
1780 | jermar | 511 | uint8_t order; |
814 | palkovsky | 512 | |
513 | frame = &zone->frames[frame_idx]; |
||
514 | |||
515 | /* remember frame order */ |
||
516 | order = frame->buddy_order; |
||
517 | |||
518 | ASSERT(frame->refcount); |
||
519 | |||
520 | if (!--frame->refcount) { |
||
521 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
||
946 | jermar | 522 | |
523 | /* Update zone information. */ |
||
524 | zone->free_count += (1 << order); |
||
525 | zone->busy_count -= (1 << order); |
||
814 | palkovsky | 526 | } |
527 | } |
||
528 | |||
3185 | jermar | 529 | /** Return frame from zone. */ |
3188 | jermar | 530 | static frame_t *zone_get_frame(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 531 | { |
532 | ASSERT(frame_idx < zone->count); |
||
533 | return &zone->frames[frame_idx]; |
||
534 | } |
||
535 | |||
3185 | jermar | 536 | /** Mark frame in zone unavailable to allocation. */ |
820 | jermar | 537 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 538 | { |
539 | frame_t *frame; |
||
540 | link_t *link; |
||
541 | |||
542 | frame = zone_get_frame(zone, frame_idx); |
||
822 | palkovsky | 543 | if (frame->refcount) |
544 | return; |
||
2133 | jermar | 545 | link = buddy_system_alloc_block(zone->buddy_system, |
546 | &frame->buddy_link); |
||
814 | palkovsky | 547 | ASSERT(link); |
548 | zone->free_count--; |
||
3188 | jermar | 549 | |
550 | mutex_lock(&mem_avail_mtx); |
||
551 | mem_avail_frames--; |
||
552 | mutex_unlock(&mem_avail_mtx); |
||
814 | palkovsky | 553 | } |
554 | |||
3185 | jermar | 555 | /** Join two zones. |
822 | palkovsky | 556 | * |
3185 | jermar | 557 | * Expect zone_t *z to point to space at least zone_conf_size large. |
822 | palkovsky | 558 | * |
3185 | jermar | 559 | * Assume z1 & z2 are locked. |
1568 | palkovsky | 560 | * |
3185 | jermar | 561 | * @param z Target zone structure pointer. |
562 | * @param z1 Zone to merge. |
||
563 | * @param z2 Zone to merge. |
||
822 | palkovsky | 564 | */ |
565 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
||
566 | { |
||
1780 | jermar | 567 | uint8_t max_order; |
2122 | decky | 568 | unsigned int i; |
569 | int z2idx; |
||
822 | palkovsky | 570 | pfn_t frame_idx; |
571 | frame_t *frame; |
||
572 | |||
2133 | jermar | 573 | ASSERT(!overlaps(z1->base, z1->count, z2->base, z2->count)); |
822 | palkovsky | 574 | ASSERT(z1->base < z2->base); |
575 | |||
576 | spinlock_initialize(&z->lock, "zone_lock"); |
||
577 | z->base = z1->base; |
||
2133 | jermar | 578 | z->count = z2->base + z2->count - z1->base; |
822 | palkovsky | 579 | z->flags = z1->flags & z2->flags; |
580 | |||
581 | z->free_count = z1->free_count + z2->free_count; |
||
582 | z->busy_count = z1->busy_count + z2->busy_count; |
||
583 | |||
584 | max_order = fnzb(z->count); |
||
585 | |||
2133 | jermar | 586 | z->buddy_system = (buddy_system_t *) &z[1]; |
587 | buddy_system_create(z->buddy_system, max_order, |
||
588 | &zone_buddy_system_operations, (void *) z); |
||
822 | palkovsky | 589 | |
2133 | jermar | 590 | z->frames = (frame_t *)((uint8_t *) z->buddy_system + |
591 | buddy_conf_size(max_order)); |
||
822 | palkovsky | 592 | for (i = 0; i < z->count; i++) { |
593 | /* This marks all frames busy */ |
||
594 | frame_initialize(&z->frames[i]); |
||
595 | } |
||
596 | /* Copy frames from both zones to preserve full frame orders, |
||
1093 | palkovsky | 597 | * parents etc. Set all free frames with refcount=0 to 1, because |
822 | palkovsky | 598 | * we add all free frames to buddy allocator later again, clear |
1093 | palkovsky | 599 | * order to 0. Don't set busy frames with refcount=0, as they |
600 | * will not be reallocated during merge and it would make later |
||
601 | * problems with allocation/free. |
||
822 | palkovsky | 602 | */ |
2122 | decky | 603 | for (i = 0; i < z1->count; i++) |
822 | palkovsky | 604 | z->frames[i] = z1->frames[i]; |
2122 | decky | 605 | for (i = 0; i < z2->count; i++) { |
822 | palkovsky | 606 | z2idx = i + (z2->base - z1->base); |
607 | z->frames[z2idx] = z2->frames[i]; |
||
608 | } |
||
1093 | palkovsky | 609 | i = 0; |
610 | while (i < z->count) { |
||
611 | if (z->frames[i].refcount) { |
||
612 | /* skip busy frames */ |
||
613 | i += 1 << z->frames[i].buddy_order; |
||
614 | } else { /* Free frames, set refcount=1 */ |
||
615 | /* All free frames have refcount=0, we need not |
||
616 | * to check the order */ |
||
822 | palkovsky | 617 | z->frames[i].refcount = 1; |
618 | z->frames[i].buddy_order = 0; |
||
1093 | palkovsky | 619 | i++; |
822 | palkovsky | 620 | } |
621 | } |
||
622 | /* Add free blocks from the 2 original zones */ |
||
623 | while (zone_can_alloc(z1, 0)) { |
||
624 | frame_idx = zone_frame_alloc(z1, 0); |
||
625 | frame = &z->frames[frame_idx]; |
||
626 | frame->refcount = 0; |
||
627 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
||
628 | } |
||
629 | while (zone_can_alloc(z2, 0)) { |
||
630 | frame_idx = zone_frame_alloc(z2, 0); |
||
2133 | jermar | 631 | frame = &z->frames[frame_idx + (z2->base - z1->base)]; |
822 | palkovsky | 632 | frame->refcount = 0; |
633 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
||
634 | } |
||
635 | } |
||
636 | |||
3185 | jermar | 637 | /** Return old configuration frames into the zone. |
822 | palkovsky | 638 | * |
639 | * We have several cases |
||
640 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
||
824 | palkovsky | 641 | * - the conf. data was created by zone_create or |
642 | * updated with reduce_region -> free every frame |
||
643 | * |
||
3185 | jermar | 644 | * @param newzone The actual zone where freeing should occur. |
645 | * @param oldzone Pointer to old zone configuration data that should |
||
646 | * be freed from new zone. |
||
822 | palkovsky | 647 | */ |
648 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
||
649 | { |
||
650 | pfn_t pfn; |
||
651 | frame_t *frame; |
||
652 | count_t cframes; |
||
2122 | decky | 653 | unsigned int i; |
822 | palkovsky | 654 | |
1780 | jermar | 655 | pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone)); |
822 | palkovsky | 656 | cframes = SIZE2FRAMES(zone_conf_size(oldzone->count)); |
657 | |||
658 | if (pfn < newzone->base || pfn >= newzone->base + newzone->count) |
||
659 | return; |
||
660 | |||
661 | frame = &newzone->frames[pfn - newzone->base]; |
||
824 | palkovsky | 662 | ASSERT(!frame->buddy_order); |
822 | palkovsky | 663 | |
2122 | decky | 664 | for (i = 0; i < cframes; i++) { |
822 | palkovsky | 665 | newzone->busy_count++; |
666 | zone_frame_free(newzone, pfn+i-newzone->base); |
||
667 | } |
||
668 | } |
||
669 | |||
3185 | jermar | 670 | /** Reduce allocated block to count of order 0 frames. |
824 | palkovsky | 671 | * |
672 | * The allocated block need 2^order frames of space. Reduce all frames |
||
1568 | palkovsky | 673 | * in block to order 0 and free the unneeded frames. This means, that |
674 | * when freeing the previously allocated block starting with frame_idx, |
||
675 | * you have to free every frame. |
||
824 | palkovsky | 676 | * |
677 | * @param zone |
||
3185 | jermar | 678 | * @param frame_idx Index to block. |
679 | * @param count Allocated space in block. |
||
824 | palkovsky | 680 | */ |
681 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
||
682 | { |
||
683 | count_t i; |
||
1780 | jermar | 684 | uint8_t order; |
824 | palkovsky | 685 | frame_t *frame; |
686 | |||
2122 | decky | 687 | ASSERT(frame_idx + count < zone->count); |
824 | palkovsky | 688 | |
689 | order = zone->frames[frame_idx].buddy_order; |
||
2122 | decky | 690 | ASSERT((count_t) (1 << order) >= count); |
824 | palkovsky | 691 | |
692 | /* Reduce all blocks to order 0 */ |
||
2122 | decky | 693 | for (i = 0; i < (count_t) (1 << order); i++) { |
824 | palkovsky | 694 | frame = &zone->frames[i + frame_idx]; |
695 | frame->buddy_order = 0; |
||
2133 | jermar | 696 | if (!frame->refcount) |
824 | palkovsky | 697 | frame->refcount = 1; |
698 | ASSERT(frame->refcount == 1); |
||
699 | } |
||
700 | /* Free unneeded frames */ |
||
2122 | decky | 701 | for (i = count; i < (count_t) (1 << order); i++) { |
824 | palkovsky | 702 | zone_frame_free(zone, i + frame_idx); |
703 | } |
||
704 | } |
||
705 | |||
3185 | jermar | 706 | /** Merge zones z1 and z2. |
822 | palkovsky | 707 | * |
708 | * - the zones must be 2 zones with no zone existing in between, |
||
709 | * which means that z2 = z1+1 |
||
710 | * |
||
711 | * - When you create a new zone, the frame allocator configuration does |
||
712 | * not to be 2^order size. Once the allocator is running it is no longer |
||
713 | * possible, merged configuration data occupies more space :-/ |
||
714 | */ |
||
2122 | decky | 715 | void zone_merge(unsigned int z1, unsigned int z2) |
822 | palkovsky | 716 | { |
717 | ipl_t ipl; |
||
718 | zone_t *zone1, *zone2, *newzone; |
||
2122 | decky | 719 | unsigned int cframes; |
1780 | jermar | 720 | uint8_t order; |
2122 | decky | 721 | unsigned int i; |
822 | palkovsky | 722 | pfn_t pfn; |
723 | |||
724 | ipl = interrupts_disable(); |
||
725 | spinlock_lock(&zones.lock); |
||
726 | |||
2745 | decky | 727 | if ((z1 >= zones.count) || (z2 >= zones.count)) |
822 | palkovsky | 728 | goto errout; |
729 | /* We can join only 2 zones with none existing inbetween */ |
||
3188 | jermar | 730 | if (z2 - z1 != 1) |
822 | palkovsky | 731 | goto errout; |
732 | |||
733 | zone1 = zones.info[z1]; |
||
734 | zone2 = zones.info[z2]; |
||
735 | spinlock_lock(&zone1->lock); |
||
736 | spinlock_lock(&zone2->lock); |
||
737 | |||
2133 | jermar | 738 | cframes = SIZE2FRAMES(zone_conf_size(zone2->base + zone2->count - |
739 | zone1->base)); |
||
1700 | palkovsky | 740 | if (cframes == 1) |
741 | order = 0; |
||
742 | else |
||
743 | order = fnzb(cframes - 1) + 1; |
||
822 | palkovsky | 744 | |
745 | /* Allocate zonedata inside one of the zones */ |
||
746 | if (zone_can_alloc(zone1, order)) |
||
747 | pfn = zone1->base + zone_frame_alloc(zone1, order); |
||
748 | else if (zone_can_alloc(zone2, order)) |
||
749 | pfn = zone2->base + zone_frame_alloc(zone2, order); |
||
750 | else |
||
751 | goto errout2; |
||
752 | |||
2122 | decky | 753 | newzone = (zone_t *) PA2KA(PFN2ADDR(pfn)); |
822 | palkovsky | 754 | |
755 | _zone_merge(newzone, zone1, zone2); |
||
756 | |||
824 | palkovsky | 757 | /* Free unneeded config frames */ |
758 | zone_reduce_region(newzone, pfn - newzone->base, cframes); |
||
822 | palkovsky | 759 | /* Subtract zone information from busy frames */ |
824 | palkovsky | 760 | newzone->busy_count -= cframes; |
822 | palkovsky | 761 | |
824 | palkovsky | 762 | /* Replace existing zones in zoneinfo list */ |
822 | palkovsky | 763 | zones.info[z1] = newzone; |
1037 | decky | 764 | for (i = z2 + 1; i < zones.count; i++) |
765 | zones.info[i - 1] = zones.info[i]; |
||
822 | palkovsky | 766 | zones.count--; |
767 | |||
768 | /* Free old zone information */ |
||
769 | return_config_frames(newzone, zone1); |
||
770 | return_config_frames(newzone, zone2); |
||
771 | errout2: |
||
772 | /* Nobody is allowed to enter to zone, so we are safe |
||
773 | * to touch the spinlocks last time */ |
||
774 | spinlock_unlock(&zone1->lock); |
||
775 | spinlock_unlock(&zone2->lock); |
||
776 | errout: |
||
777 | spinlock_unlock(&zones.lock); |
||
778 | interrupts_restore(ipl); |
||
779 | } |
||
780 | |||
3185 | jermar | 781 | /** Merge all zones into one big zone. |
822 | palkovsky | 782 | * |
783 | * It is reasonable to do this on systems whose bios reports parts in chunks, |
||
784 | * so that we could have 1 zone (it's faster). |
||
785 | */ |
||
786 | void zone_merge_all(void) |
||
787 | { |
||
788 | int count = zones.count; |
||
789 | |||
790 | while (zones.count > 1 && --count) { |
||
3188 | jermar | 791 | zone_merge(0, 1); |
822 | palkovsky | 792 | break; |
793 | } |
||
794 | } |
||
795 | |||
3185 | jermar | 796 | /** Create new frame zone. |
814 | palkovsky | 797 | * |
3185 | jermar | 798 | * @param start Physical address of the first frame within the zone. |
799 | * @param count Count of frames in zone. |
||
800 | * @param z Address of configuration information of zone. |
||
801 | * @param flags Zone flags. |
||
814 | palkovsky | 802 | * |
3185 | jermar | 803 | * @return Initialized zone. |
814 | palkovsky | 804 | */ |
822 | palkovsky | 805 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
814 | palkovsky | 806 | { |
2122 | decky | 807 | unsigned int i; |
1780 | jermar | 808 | uint8_t max_order; |
814 | palkovsky | 809 | |
810 | spinlock_initialize(&z->lock, "zone_lock"); |
||
811 | z->base = start; |
||
812 | z->count = count; |
||
813 | z->flags = flags; |
||
814 | z->free_count = count; |
||
815 | z->busy_count = 0; |
||
816 | |||
817 | /* |
||
818 | * Compute order for buddy system, initialize |
||
819 | */ |
||
822 | palkovsky | 820 | max_order = fnzb(count); |
814 | palkovsky | 821 | z->buddy_system = (buddy_system_t *)&z[1]; |
822 | |||
823 | buddy_system_create(z->buddy_system, max_order, |
||
3185 | jermar | 824 | &zone_buddy_system_operations, (void *) z); |
814 | palkovsky | 825 | |
826 | /* Allocate frames _after_ the conframe */ |
||
827 | /* Check sizes */ |
||
2133 | jermar | 828 | z->frames = (frame_t *)((uint8_t *) z->buddy_system + |
829 | buddy_conf_size(max_order)); |
||
2122 | decky | 830 | for (i = 0; i < count; i++) { |
814 | palkovsky | 831 | frame_initialize(&z->frames[i]); |
832 | } |
||
852 | palkovsky | 833 | |
814 | palkovsky | 834 | /* Stuffing frames */ |
835 | for (i = 0; i < count; i++) { |
||
836 | z->frames[i].refcount = 0; |
||
837 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
||
838 | } |
||
839 | } |
||
840 | |||
3185 | jermar | 841 | /** Compute configuration data size for zone. |
1568 | palkovsky | 842 | * |
3185 | jermar | 843 | * @param count Size of zone in frames. |
844 | * @return Size of zone configuration info (in bytes). |
||
1568 | palkovsky | 845 | */ |
1780 | jermar | 846 | uintptr_t zone_conf_size(count_t count) |
814 | palkovsky | 847 | { |
3057 | decky | 848 | int size = sizeof(zone_t) + count * sizeof(frame_t); |
814 | palkovsky | 849 | int max_order; |
850 | |||
822 | palkovsky | 851 | max_order = fnzb(count); |
814 | palkovsky | 852 | size += buddy_conf_size(max_order); |
853 | return size; |
||
854 | } |
||
855 | |||
3185 | jermar | 856 | /** Create and add zone to system. |
814 | palkovsky | 857 | * |
3185 | jermar | 858 | * @param start First frame number (absolute). |
859 | * @param count Size of zone in frames. |
||
860 | * @param confframe Where configuration frames are supposed to be. |
||
861 | * Automatically checks, that we will not disturb the |
||
862 | * kernel and possibly init. If confframe is given |
||
863 | * _outside_ this zone, it is expected, that the area is |
||
864 | * already marked BUSY and big enough to contain |
||
865 | * zone_conf_size() amount of data. If the confframe is |
||
866 | * inside the area, the zone free frame information is |
||
867 | * modified not to include it. |
||
822 | palkovsky | 868 | * |
3185 | jermar | 869 | * @return Zone number or -1 on error. |
814 | palkovsky | 870 | */ |
822 | palkovsky | 871 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
814 | palkovsky | 872 | { |
873 | zone_t *z; |
||
1780 | jermar | 874 | uintptr_t addr; |
820 | jermar | 875 | count_t confcount; |
2122 | decky | 876 | unsigned int i; |
822 | palkovsky | 877 | int znum; |
814 | palkovsky | 878 | |
879 | /* Theoretically we could have here 0, practically make sure |
||
880 | * nobody tries to do that. If some platform requires, remove |
||
881 | * the assert |
||
882 | */ |
||
883 | ASSERT(confframe); |
||
884 | /* If conframe is supposed to be inside our zone, then make sure |
||
885 | * it does not span kernel & init |
||
886 | */ |
||
822 | palkovsky | 887 | confcount = SIZE2FRAMES(zone_conf_size(count)); |
3188 | jermar | 888 | if (confframe >= start && confframe < start + count) { |
889 | for (; confframe < start + count; confframe++) { |
||
814 | palkovsky | 890 | addr = PFN2ADDR(confframe); |
2133 | jermar | 891 | if (overlaps(addr, PFN2ADDR(confcount), |
892 | KA2PA(config.base), config.kernel_size)) |
||
814 | palkovsky | 893 | continue; |
1037 | decky | 894 | |
2133 | jermar | 895 | if (overlaps(addr, PFN2ADDR(confcount), |
896 | KA2PA(config.stack_base), config.stack_size)) |
||
1833 | decky | 897 | continue; |
898 | |||
1037 | decky | 899 | bool overlap = false; |
900 | count_t i; |
||
901 | for (i = 0; i < init.cnt; i++) |
||
2133 | jermar | 902 | if (overlaps(addr, PFN2ADDR(confcount), |
903 | KA2PA(init.tasks[i].addr), |
||
904 | init.tasks[i].size)) { |
||
1037 | decky | 905 | overlap = true; |
906 | break; |
||
907 | } |
||
908 | if (overlap) |
||
909 | continue; |
||
910 | |||
814 | palkovsky | 911 | break; |
912 | } |
||
1037 | decky | 913 | if (confframe >= start + count) |
814 | palkovsky | 914 | panic("Cannot find configuration data for zone."); |
915 | } |
||
916 | |||
2725 | decky | 917 | z = (zone_t *) PA2KA(PFN2ADDR(confframe)); |
822 | palkovsky | 918 | zone_construct(start, count, z, flags); |
919 | znum = zones_add_zone(z); |
||
920 | if (znum == -1) |
||
921 | return -1; |
||
922 | |||
3188 | jermar | 923 | mutex_lock(&mem_avail_mtx); |
924 | mem_avail_frames += count; |
||
925 | mutex_unlock(&mem_avail_mtx); |
||
926 | |||
814 | palkovsky | 927 | /* If confdata in zone, mark as unavailable */ |
2122 | decky | 928 | if (confframe >= start && confframe < start + count) |
929 | for (i = confframe; i < confframe + confcount; i++) { |
||
814 | palkovsky | 930 | zone_mark_unavailable(z, i - z->base); |
931 | } |
||
3188 | jermar | 932 | |
822 | palkovsky | 933 | return znum; |
814 | palkovsky | 934 | } |
935 | |||
936 | /***************************************/ |
||
937 | /* Frame functions */ |
||
938 | |||
3185 | jermar | 939 | /** Set parent of frame. */ |
2122 | decky | 940 | void frame_set_parent(pfn_t pfn, void *data, unsigned int hint) |
814 | palkovsky | 941 | { |
942 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
||
943 | |||
944 | ASSERT(zone); |
||
945 | |||
3188 | jermar | 946 | zone_get_frame(zone, pfn - zone->base)->parent = data; |
814 | palkovsky | 947 | spinlock_unlock(&zone->lock); |
948 | } |
||
949 | |||
2133 | jermar | 950 | void *frame_get_parent(pfn_t pfn, unsigned int hint) |
814 | palkovsky | 951 | { |
952 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
||
953 | void *res; |
||
954 | |||
955 | ASSERT(zone); |
||
956 | res = zone_get_frame(zone, pfn - zone->base)->parent; |
||
957 | |||
958 | spinlock_unlock(&zone->lock); |
||
959 | return res; |
||
960 | } |
||
961 | |||
962 | /** Allocate power-of-two frames of physical memory. |
||
963 | * |
||
3185 | jermar | 964 | * @param order Allocate exactly 2^order frames. |
965 | * @param flags Flags for host zone selection and address processing. |
||
966 | * @param pzone Preferred zone. |
||
814 | palkovsky | 967 | * |
3185 | jermar | 968 | * @return Physical address of the allocated frame. |
1269 | decky | 969 | * |
814 | palkovsky | 970 | */ |
3185 | jermar | 971 | void *frame_alloc_generic(uint8_t order, int flags, unsigned int *pzone) |
814 | palkovsky | 972 | { |
973 | ipl_t ipl; |
||
974 | int freed; |
||
975 | pfn_t v; |
||
976 | zone_t *zone; |
||
3188 | jermar | 977 | unsigned long gen = 0; |
814 | palkovsky | 978 | |
979 | loop: |
||
980 | ipl = interrupts_disable(); |
||
1269 | decky | 981 | |
814 | palkovsky | 982 | /* |
983 | * First, find suitable frame zone. |
||
984 | */ |
||
1981 | jermar | 985 | zone = find_free_zone_and_lock(order, pzone); |
1269 | decky | 986 | |
814 | palkovsky | 987 | /* If no memory, reclaim some slab memory, |
988 | if it does not help, reclaim all */ |
||
989 | if (!zone && !(flags & FRAME_NO_RECLAIM)) { |
||
990 | freed = slab_reclaim(0); |
||
991 | if (freed) |
||
1981 | jermar | 992 | zone = find_free_zone_and_lock(order, pzone); |
814 | palkovsky | 993 | if (!zone) { |
994 | freed = slab_reclaim(SLAB_RECLAIM_ALL); |
||
995 | if (freed) |
||
1981 | jermar | 996 | zone = find_free_zone_and_lock(order, pzone); |
814 | palkovsky | 997 | } |
998 | } |
||
999 | if (!zone) { |
||
1000 | /* |
||
3187 | jermar | 1001 | * Sleep until some frames are available again. |
814 | palkovsky | 1002 | */ |
3187 | jermar | 1003 | if (flags & FRAME_ATOMIC) { |
1004 | interrupts_restore(ipl); |
||
1760 | palkovsky | 1005 | return 0; |
3187 | jermar | 1006 | } |
814 | palkovsky | 1007 | |
3187 | jermar | 1008 | #ifdef CONFIG_DEBUG |
3188 | jermar | 1009 | unsigned long avail; |
1010 | |||
1011 | mutex_lock(&mem_avail_mtx); |
||
1012 | avail = mem_avail_frames; |
||
1013 | mutex_unlock(&mem_avail_mtx); |
||
1014 | |||
1015 | printf("Thread %" PRIu64 " waiting for %u frames, " |
||
1016 | "%u available.\n", THREAD->tid, 1ULL << order, avail); |
||
3187 | jermar | 1017 | #endif |
1018 | |||
3188 | jermar | 1019 | mutex_lock(&mem_avail_mtx); |
1020 | while ((mem_avail_frames < (1ULL << order)) || |
||
1021 | gen == mem_avail_gen) |
||
1022 | condvar_wait(&mem_avail_cv, &mem_avail_mtx); |
||
1023 | gen = mem_avail_gen; |
||
1024 | mutex_unlock(&mem_avail_mtx); |
||
3187 | jermar | 1025 | |
1026 | #ifdef CONFIG_DEBUG |
||
3188 | jermar | 1027 | mutex_lock(&mem_avail_mtx); |
1028 | avail = mem_avail_frames; |
||
1029 | mutex_unlock(&mem_avail_mtx); |
||
1030 | |||
1031 | printf("Thread %" PRIu64 " woken up, %u frames available.\n", |
||
1032 | THREAD->tid, avail); |
||
3187 | jermar | 1033 | #endif |
1034 | |||
1035 | interrupts_restore(ipl); |
||
814 | palkovsky | 1036 | goto loop; |
1037 | } |
||
1269 | decky | 1038 | |
1039 | v = zone_frame_alloc(zone, order); |
||
814 | palkovsky | 1040 | v += zone->base; |
1041 | |||
1042 | spinlock_unlock(&zone->lock); |
||
3188 | jermar | 1043 | |
1044 | mutex_lock(&mem_avail_mtx); |
||
1045 | mem_avail_frames -= (1ULL << order); |
||
1046 | mutex_unlock(&mem_avail_mtx); |
||
1047 | |||
814 | palkovsky | 1048 | interrupts_restore(ipl); |
1049 | |||
1760 | palkovsky | 1050 | if (flags & FRAME_KA) |
1051 | return (void *)PA2KA(PFN2ADDR(v)); |
||
1052 | return (void *)PFN2ADDR(v); |
||
814 | palkovsky | 1053 | } |
1054 | |||
1055 | /** Free a frame. |
||
1056 | * |
||
1854 | jermar | 1057 | * Find respective frame structure for supplied physical frame address. |
814 | palkovsky | 1058 | * Decrement frame reference count. |
1059 | * If it drops to zero, move the frame structure to free list. |
||
1060 | * |
||
3185 | jermar | 1061 | * @param frame Physical Address of of the frame to be freed. |
814 | palkovsky | 1062 | */ |
1780 | jermar | 1063 | void frame_free(uintptr_t frame) |
814 | palkovsky | 1064 | { |
1065 | ipl_t ipl; |
||
1066 | zone_t *zone; |
||
1760 | palkovsky | 1067 | pfn_t pfn = ADDR2PFN(frame); |
814 | palkovsky | 1068 | |
1069 | ipl = interrupts_disable(); |
||
3187 | jermar | 1070 | |
814 | palkovsky | 1071 | /* |
1072 | * First, find host frame zone for addr. |
||
1073 | */ |
||
3187 | jermar | 1074 | zone = find_zone_and_lock(pfn, NULL); |
814 | palkovsky | 1075 | ASSERT(zone); |
1076 | |||
3187 | jermar | 1077 | zone_frame_free(zone, pfn - zone->base); |
814 | palkovsky | 1078 | |
1079 | spinlock_unlock(&zone->lock); |
||
3187 | jermar | 1080 | |
1081 | /* |
||
1082 | * Signal that some memory has been freed. |
||
1083 | */ |
||
3188 | jermar | 1084 | mutex_lock(&mem_avail_mtx); |
1085 | mem_avail_frames++; |
||
1086 | mem_avail_gen++; |
||
1087 | condvar_broadcast(&mem_avail_cv); |
||
1088 | mutex_unlock(&mem_avail_mtx); |
||
3187 | jermar | 1089 | |
814 | palkovsky | 1090 | interrupts_restore(ipl); |
1091 | } |
||
1092 | |||
1236 | jermar | 1093 | /** Add reference to frame. |
1094 | * |
||
1095 | * Find respective frame structure for supplied PFN and |
||
1096 | * increment frame reference count. |
||
1097 | * |
||
3185 | jermar | 1098 | * @param pfn Frame number of the frame to be freed. |
1236 | jermar | 1099 | */ |
1100 | void frame_reference_add(pfn_t pfn) |
||
1101 | { |
||
1102 | ipl_t ipl; |
||
1103 | zone_t *zone; |
||
1104 | frame_t *frame; |
||
814 | palkovsky | 1105 | |
1236 | jermar | 1106 | ipl = interrupts_disable(); |
1107 | |||
1108 | /* |
||
1109 | * First, find host frame zone for addr. |
||
1110 | */ |
||
3185 | jermar | 1111 | zone = find_zone_and_lock(pfn, NULL); |
1236 | jermar | 1112 | ASSERT(zone); |
1113 | |||
3185 | jermar | 1114 | frame = &zone->frames[pfn - zone->base]; |
1236 | jermar | 1115 | frame->refcount++; |
1116 | |||
1117 | spinlock_unlock(&zone->lock); |
||
1118 | interrupts_restore(ipl); |
||
1119 | } |
||
814 | palkovsky | 1120 | |
3185 | jermar | 1121 | /** Mark given range unavailable in frame zones. */ |
820 | jermar | 1122 | void frame_mark_unavailable(pfn_t start, count_t count) |
814 | palkovsky | 1123 | { |
2122 | decky | 1124 | unsigned int i; |
814 | palkovsky | 1125 | zone_t *zone; |
2122 | decky | 1126 | unsigned int prefzone = 0; |
852 | palkovsky | 1127 | |
2122 | decky | 1128 | for (i = 0; i < count; i++) { |
1981 | jermar | 1129 | zone = find_zone_and_lock(start + i, &prefzone); |
814 | palkovsky | 1130 | if (!zone) /* PFN not found */ |
1131 | continue; |
||
1981 | jermar | 1132 | zone_mark_unavailable(zone, start + i - zone->base); |
814 | palkovsky | 1133 | |
1134 | spinlock_unlock(&zone->lock); |
||
1135 | } |
||
1136 | } |
||
1137 | |||
3185 | jermar | 1138 | /** Initialize physical memory management. */ |
814 | palkovsky | 1139 | void frame_init(void) |
1140 | { |
||
1141 | if (config.cpu_active == 1) { |
||
1142 | zones.count = 0; |
||
1981 | jermar | 1143 | spinlock_initialize(&zones.lock, "zones.lock"); |
3188 | jermar | 1144 | mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE); |
1145 | condvar_initialize(&mem_avail_cv); |
||
814 | palkovsky | 1146 | } |
1147 | /* Tell the architecture to create some memory */ |
||
1148 | frame_arch_init(); |
||
1149 | if (config.cpu_active == 1) { |
||
2133 | jermar | 1150 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), |
1151 | SIZE2FRAMES(config.kernel_size)); |
||
1152 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), |
||
1153 | SIZE2FRAMES(config.stack_size)); |
||
1037 | decky | 1154 | |
1155 | count_t i; |
||
2133 | jermar | 1156 | for (i = 0; i < init.cnt; i++) { |
1157 | pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr)); |
||
1158 | frame_mark_unavailable(pfn, |
||
1159 | SIZE2FRAMES(init.tasks[i].size)); |
||
1160 | } |
||
1599 | palkovsky | 1161 | |
1894 | jermar | 1162 | if (ballocs.size) |
2133 | jermar | 1163 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)), |
1164 | SIZE2FRAMES(ballocs.size)); |
||
1894 | jermar | 1165 | |
1599 | palkovsky | 1166 | /* Black list first frame, as allocating NULL would |
1790 | jermar | 1167 | * fail in some places */ |
1599 | palkovsky | 1168 | frame_mark_unavailable(0, 1); |
814 | palkovsky | 1169 | } |
1170 | } |
||
1171 | |||
1172 | |||
3185 | jermar | 1173 | /** Return total size of all zones. */ |
1174 | uint64_t zone_total_size(void) |
||
1175 | { |
||
2725 | decky | 1176 | zone_t *zone = NULL; |
1177 | unsigned int i; |
||
1178 | ipl_t ipl; |
||
1179 | uint64_t total = 0; |
||
814 | palkovsky | 1180 | |
2725 | decky | 1181 | ipl = interrupts_disable(); |
1182 | spinlock_lock(&zones.lock); |
||
1183 | |||
1184 | for (i = 0; i < zones.count; i++) { |
||
1185 | zone = zones.info[i]; |
||
1186 | spinlock_lock(&zone->lock); |
||
1187 | total += (uint64_t) FRAMES2SIZE(zone->count); |
||
1188 | spinlock_unlock(&zone->lock); |
||
1189 | } |
||
1190 | |||
1191 | spinlock_unlock(&zones.lock); |
||
1192 | interrupts_restore(ipl); |
||
1193 | |||
1194 | return total; |
||
1195 | } |
||
1196 | |||
3185 | jermar | 1197 | /** Prints list of zones. */ |
1198 | void zone_print_list(void) |
||
1199 | { |
||
676 | bondari | 1200 | zone_t *zone = NULL; |
2122 | decky | 1201 | unsigned int i; |
701 | jermar | 1202 | ipl_t ipl; |
1203 | |||
3057 | decky | 1204 | #ifdef __32_BITS__ |
1205 | printf("# base address free frames busy frames\n"); |
||
1206 | printf("-- ------------ ------------ ------------\n"); |
||
1207 | #endif |
||
1208 | |||
1209 | #ifdef __64_BITS__ |
||
1210 | printf("# base address free frames busy frames\n"); |
||
1211 | printf("-- -------------------- ------------ ------------\n"); |
||
1212 | #endif |
||
2712 | decky | 1213 | |
3205 | jermar | 1214 | /* |
1215 | * Because printing may require allocation of memory, we may not hold |
||
1216 | * the frame allocator locks when printing zone statistics. Therefore, |
||
1217 | * we simply gather the statistics under the protection of the locks and |
||
1218 | * print the statistics when the locks have been released. |
||
1219 | * |
||
1220 | * When someone adds/removes zones while we are printing the statistics, |
||
1221 | * we may end up with inaccurate output (e.g. a zone being skipped from |
||
1222 | * the listing). |
||
1223 | */ |
||
1224 | |||
1225 | for (i = 0; ; i++) { |
||
1226 | uintptr_t base; |
||
1227 | count_t free_count; |
||
1228 | count_t busy_count; |
||
1229 | |||
1230 | ipl = interrupts_disable(); |
||
1231 | spinlock_lock(&zones.lock); |
||
1232 | |||
1233 | if (i >= zones.count) { |
||
1234 | spinlock_unlock(&zones.lock); |
||
1235 | interrupts_restore(ipl); |
||
1236 | break; |
||
1237 | } |
||
1238 | |||
814 | palkovsky | 1239 | zone = zones.info[i]; |
683 | bondari | 1240 | spinlock_lock(&zone->lock); |
3057 | decky | 1241 | |
3205 | jermar | 1242 | base = PFN2ADDR(zone->base); |
1243 | free_count = zone->free_count; |
||
1244 | busy_count = zone->busy_count; |
||
1245 | |||
1246 | spinlock_unlock(&zone->lock); |
||
1247 | |||
1248 | spinlock_unlock(&zones.lock); |
||
1249 | interrupts_restore(ipl); |
||
1250 | |||
3057 | decky | 1251 | #ifdef __32_BITS__ |
3205 | jermar | 1252 | printf("%-2u %10p %12" PRIc " %12" PRIc "\n", i, base, |
1253 | free_count, busy_count); |
||
3057 | decky | 1254 | #endif |
1255 | |||
1256 | #ifdef __64_BITS__ |
||
3205 | jermar | 1257 | printf("%-2u %18p %12" PRIc " %12" PRIc "\n", i, base, |
1258 | free_count, busy_count); |
||
3057 | decky | 1259 | #endif |
2712 | decky | 1260 | |
676 | bondari | 1261 | } |
1262 | } |
||
1263 | |||
1708 | jermar | 1264 | /** Prints zone details. |
677 | bondari | 1265 | * |
3185 | jermar | 1266 | * @param num Zone base address or zone number. |
677 | bondari | 1267 | */ |
3185 | jermar | 1268 | void zone_print_one(unsigned int num) |
1269 | { |
||
814 | palkovsky | 1270 | zone_t *zone = NULL; |
701 | jermar | 1271 | ipl_t ipl; |
2122 | decky | 1272 | unsigned int i; |
3206 | jermar | 1273 | uintptr_t base; |
1274 | count_t count; |
||
1275 | count_t busy_count; |
||
1276 | count_t free_count; |
||
701 | jermar | 1277 | |
1278 | ipl = interrupts_disable(); |
||
814 | palkovsky | 1279 | spinlock_lock(&zones.lock); |
822 | palkovsky | 1280 | |
1037 | decky | 1281 | for (i = 0; i < zones.count; i++) { |
2725 | decky | 1282 | if ((i == num) || (PFN2ADDR(zones.info[i]->base) == num)) { |
822 | palkovsky | 1283 | zone = zones.info[i]; |
1284 | break; |
||
1285 | } |
||
676 | bondari | 1286 | } |
822 | palkovsky | 1287 | if (!zone) { |
3206 | jermar | 1288 | spinlock_unlock(&zones.lock); |
1289 | interrupts_restore(ipl); |
||
822 | palkovsky | 1290 | printf("Zone not found.\n"); |
3206 | jermar | 1291 | return; |
822 | palkovsky | 1292 | } |
676 | bondari | 1293 | |
683 | bondari | 1294 | spinlock_lock(&zone->lock); |
3206 | jermar | 1295 | base = PFN2ADDR(zone->base); |
1296 | count = zone->count; |
||
1297 | busy_count = zone->busy_count; |
||
1298 | free_count = zone->free_count; |
||
2725 | decky | 1299 | spinlock_unlock(&zone->lock); |
814 | palkovsky | 1300 | spinlock_unlock(&zones.lock); |
701 | jermar | 1301 | interrupts_restore(ipl); |
3206 | jermar | 1302 | |
1303 | printf("Zone base address: %p\n", base); |
||
1304 | printf("Zone size: %" PRIc " frames (%" PRIs " KiB)\n", count, |
||
1305 | SIZE2KB(FRAMES2SIZE(count))); |
||
1306 | printf("Allocated space: %" PRIc " frames (%" PRIs " KiB)\n", |
||
1307 | busy_count, SIZE2KB(FRAMES2SIZE(busy_count))); |
||
1308 | printf("Available space: %" PRIc " frames (%" PRIs " KiB)\n", |
||
1309 | free_count, SIZE2KB(FRAMES2SIZE(free_count))); |
||
676 | bondari | 1310 | } |
1311 | |||
1757 | jermar | 1312 | /** @} |
1702 | cejka | 1313 | */ |
2133 | jermar | 1314 |