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