Rev 2083 | Rev 2121 | 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> |
115 | jermar | 61 | #include <arch/asm.h> |
195 | vana | 62 | #include <arch.h> |
533 | bondari | 63 | #include <print.h> |
536 | bondari | 64 | #include <align.h> |
814 | palkovsky | 65 | #include <mm/slab.h> |
822 | palkovsky | 66 | #include <bitops.h> |
1063 | palkovsky | 67 | #include <macros.h> |
115 | jermar | 68 | |
814 | palkovsky | 69 | typedef struct { |
70 | count_t refcount; /**< tracking of shared frames */ |
||
1780 | jermar | 71 | uint8_t buddy_order; /**< buddy system block order */ |
814 | palkovsky | 72 | link_t buddy_link; /**< link to the next free block inside one order */ |
73 | void *parent; /**< If allocated by slab, this points there */ |
||
1236 | jermar | 74 | } frame_t; |
367 | jermar | 75 | |
814 | palkovsky | 76 | typedef struct { |
77 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
||
1236 | jermar | 78 | pfn_t base; /**< frame_no of the first frame in the frames array */ |
820 | jermar | 79 | count_t count; /**< Size of zone */ |
533 | bondari | 80 | |
814 | palkovsky | 81 | frame_t *frames; /**< array of frame_t structures in this zone */ |
82 | count_t free_count; /**< number of free frame_t structures */ |
||
83 | count_t busy_count; /**< number of busy frame_t structures */ |
||
84 | |||
2059 | jermar | 85 | buddy_system_t *buddy_system; /**< buddy system for the zone */ |
814 | palkovsky | 86 | int flags; |
1236 | jermar | 87 | } zone_t; |
479 | bondari | 88 | |
814 | palkovsky | 89 | /* |
90 | * The zoneinfo.lock must be locked when accessing zoneinfo structure. |
||
91 | * Some of the attributes in zone_t structures are 'read-only' |
||
368 | jermar | 92 | */ |
1 | jermar | 93 | |
814 | palkovsky | 94 | struct { |
95 | SPINLOCK_DECLARE(lock); |
||
96 | int count; |
||
97 | zone_t *info[ZONES_MAX]; |
||
1236 | jermar | 98 | } zones; |
1 | jermar | 99 | |
814 | palkovsky | 100 | |
101 | /*********************************/ |
||
102 | /* Helper functions */ |
||
103 | static inline index_t frame_index(zone_t *zone, frame_t *frame) |
||
762 | palkovsky | 104 | { |
814 | palkovsky | 105 | return (index_t)(frame - zone->frames); |
762 | palkovsky | 106 | } |
814 | palkovsky | 107 | static inline index_t frame_index_abs(zone_t *zone, frame_t *frame) |
1 | jermar | 108 | { |
814 | palkovsky | 109 | return (index_t)(frame - zone->frames) + zone->base; |
1 | jermar | 110 | } |
814 | palkovsky | 111 | static inline int frame_index_valid(zone_t *zone, index_t index) |
112 | { |
||
113 | return index >= 0 && index < zone->count; |
||
114 | } |
||
1 | jermar | 115 | |
814 | palkovsky | 116 | /** Compute pfn_t from frame_t pointer & zone pointer */ |
820 | jermar | 117 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
762 | palkovsky | 118 | { |
814 | palkovsky | 119 | return frame - zone->frames; |
762 | palkovsky | 120 | } |
121 | |||
814 | palkovsky | 122 | /** Initialize frame structure |
762 | palkovsky | 123 | * |
814 | palkovsky | 124 | * Initialize frame structure. |
125 | * |
||
126 | * @param frame Frame structure to be initialized. |
||
762 | palkovsky | 127 | */ |
814 | palkovsky | 128 | static void frame_initialize(frame_t *frame) |
762 | palkovsky | 129 | { |
814 | palkovsky | 130 | frame->refcount = 1; |
131 | frame->buddy_order = 0; |
||
762 | palkovsky | 132 | } |
133 | |||
814 | palkovsky | 134 | /*************************************/ |
135 | /* Zoneinfo functions */ |
||
762 | palkovsky | 136 | |
814 | palkovsky | 137 | /** |
138 | * Insert-sort zone into zones list |
||
822 | palkovsky | 139 | * |
1568 | palkovsky | 140 | * @param newzone New zone to be inserted into zone list |
822 | palkovsky | 141 | * @return zone number on success, -1 on error |
1 | jermar | 142 | */ |
822 | palkovsky | 143 | static int zones_add_zone(zone_t *newzone) |
1 | jermar | 144 | { |
822 | palkovsky | 145 | int i,j; |
146 | ipl_t ipl; |
||
147 | zone_t *z; |
||
762 | palkovsky | 148 | |
822 | palkovsky | 149 | ipl = interrupts_disable(); |
814 | palkovsky | 150 | spinlock_lock(&zones.lock); |
151 | /* Try to merge */ |
||
1037 | decky | 152 | if (zones.count + 1 == ZONES_MAX) |
822 | palkovsky | 153 | panic("Maximum zone(%d) count exceeded.", ZONES_MAX); |
1037 | decky | 154 | for (i = 0; i < zones.count; i++) { |
822 | palkovsky | 155 | /* Check for overflow */ |
852 | palkovsky | 156 | z = zones.info[i]; |
822 | palkovsky | 157 | if (overlaps(newzone->base,newzone->count, |
158 | z->base, z->count)) { |
||
159 | printf("Zones overlap!\n"); |
||
160 | return -1; |
||
814 | palkovsky | 161 | } |
852 | palkovsky | 162 | if (newzone->base < z->base) |
822 | palkovsky | 163 | break; |
368 | jermar | 164 | } |
822 | palkovsky | 165 | /* Move other zones up */ |
1037 | decky | 166 | for (j = i;j < zones.count; j++) |
167 | zones.info[j + 1] = zones.info[j]; |
||
822 | palkovsky | 168 | zones.info[i] = newzone; |
169 | zones.count++; |
||
814 | palkovsky | 170 | spinlock_unlock(&zones.lock); |
822 | palkovsky | 171 | interrupts_restore(ipl); |
172 | |||
173 | return i; |
||
1 | jermar | 174 | } |
175 | |||
814 | palkovsky | 176 | /** |
177 | * Try to find a zone where can we find the frame |
||
2059 | jermar | 178 | |
179 | * Assume interrupts are disabled. |
||
180 | |||
1568 | palkovsky | 181 | * @param frame Frame number contained in zone |
182 | * @param pzone If not null, it is used as zone hint. Zone index |
||
183 | * is filled into the variable on success. |
||
2059 | jermar | 184 | * @return Pointer to locked zone containing frame |
368 | jermar | 185 | */ |
814 | palkovsky | 186 | static zone_t * find_zone_and_lock(pfn_t frame, int *pzone) |
1 | jermar | 187 | { |
533 | bondari | 188 | int i; |
814 | palkovsky | 189 | int hint = pzone ? *pzone : 0; |
190 | zone_t *z; |
||
533 | bondari | 191 | |
814 | palkovsky | 192 | spinlock_lock(&zones.lock); |
193 | |||
194 | if (hint >= zones.count || hint < 0) |
||
195 | hint = 0; |
||
533 | bondari | 196 | |
814 | palkovsky | 197 | i = hint; |
198 | do { |
||
199 | z = zones.info[i]; |
||
200 | spinlock_lock(&z->lock); |
||
201 | if (z->base <= frame && z->base + z->count > frame) { |
||
202 | spinlock_unlock(&zones.lock); /* Unlock the global lock */ |
||
203 | if (pzone) |
||
204 | *pzone = i; |
||
205 | return z; |
||
533 | bondari | 206 | } |
814 | palkovsky | 207 | spinlock_unlock(&z->lock); |
533 | bondari | 208 | |
814 | palkovsky | 209 | i++; |
210 | if (i >= zones.count) |
||
211 | i = 0; |
||
212 | } while(i != hint); |
||
213 | |||
214 | spinlock_unlock(&zones.lock); |
||
215 | return NULL; |
||
533 | bondari | 216 | } |
217 | |||
822 | palkovsky | 218 | /** @return True if zone can allocate specified order */ |
1780 | jermar | 219 | static int zone_can_alloc(zone_t *z, uint8_t order) |
822 | palkovsky | 220 | { |
221 | return buddy_system_can_alloc(z->buddy_system, order); |
||
222 | } |
||
223 | |||
2059 | jermar | 224 | /** Find and lock zone that can allocate order frames. |
367 | jermar | 225 | * |
2059 | jermar | 226 | * Assume interrupts are disabled. |
367 | jermar | 227 | * |
1568 | palkovsky | 228 | * @param order Size (2^order) of free space we are trying to find |
814 | palkovsky | 229 | * @param pzone Pointer to preferred zone or NULL, on return contains zone number |
367 | jermar | 230 | */ |
1981 | jermar | 231 | static zone_t * find_free_zone_and_lock(uint8_t order, int *pzone) |
367 | jermar | 232 | { |
814 | palkovsky | 233 | int i; |
367 | jermar | 234 | zone_t *z; |
814 | palkovsky | 235 | int hint = pzone ? *pzone : 0; |
367 | jermar | 236 | |
814 | palkovsky | 237 | spinlock_lock(&zones.lock); |
238 | if (hint >= zones.count) |
||
239 | hint = 0; |
||
240 | i = hint; |
||
241 | do { |
||
242 | z = zones.info[i]; |
||
724 | palkovsky | 243 | |
814 | palkovsky | 244 | spinlock_lock(&z->lock); |
367 | jermar | 245 | |
814 | palkovsky | 246 | /* Check if the zone has 2^order frames area available */ |
822 | palkovsky | 247 | if (zone_can_alloc(z, order)) { |
814 | palkovsky | 248 | spinlock_unlock(&zones.lock); |
249 | if (pzone) |
||
250 | *pzone = i; |
||
251 | return z; |
||
367 | jermar | 252 | } |
814 | palkovsky | 253 | spinlock_unlock(&z->lock); |
254 | if (++i >= zones.count) |
||
255 | i = 0; |
||
256 | } while(i != hint); |
||
257 | spinlock_unlock(&zones.lock); |
||
258 | return NULL; |
||
367 | jermar | 259 | } |
260 | |||
2059 | jermar | 261 | /**************************/ |
814 | palkovsky | 262 | /* Buddy system functions */ |
2059 | jermar | 263 | /**************************/ |
814 | palkovsky | 264 | |
265 | /** Buddy system find_block implementation |
||
367 | jermar | 266 | * |
814 | palkovsky | 267 | * Find block that is parent of current list. |
268 | * That means go to lower addresses, until such block is found |
||
367 | jermar | 269 | * |
814 | palkovsky | 270 | * @param order - Order of parent must be different then this parameter!! |
367 | jermar | 271 | */ |
814 | palkovsky | 272 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
1780 | jermar | 273 | uint8_t order) |
367 | jermar | 274 | { |
814 | palkovsky | 275 | frame_t * frame; |
276 | zone_t * zone; |
||
277 | index_t index; |
||
367 | jermar | 278 | |
814 | palkovsky | 279 | frame = list_get_instance(child, frame_t, buddy_link); |
280 | zone = (zone_t *) b->data; |
||
367 | jermar | 281 | |
814 | palkovsky | 282 | index = frame_index(zone, frame); |
283 | do { |
||
284 | if (zone->frames[index].buddy_order != order) { |
||
285 | return &zone->frames[index].buddy_link; |
||
286 | } |
||
287 | } while(index-- > 0); |
||
288 | return NULL; |
||
367 | jermar | 289 | } |
479 | bondari | 290 | |
822 | palkovsky | 291 | static void zone_buddy_print_id(buddy_system_t *b, link_t *block) |
292 | { |
||
293 | frame_t * frame; |
||
294 | zone_t * zone; |
||
295 | index_t index; |
||
479 | bondari | 296 | |
822 | palkovsky | 297 | frame = list_get_instance(block, frame_t, buddy_link); |
298 | zone = (zone_t *) b->data; |
||
299 | index = frame_index(zone, frame); |
||
1196 | cejka | 300 | printf("%zd", index); |
822 | palkovsky | 301 | } |
302 | |||
479 | bondari | 303 | /** Buddy system find_buddy implementation |
489 | jermar | 304 | * |
305 | * @param b Buddy system. |
||
480 | bondari | 306 | * @param block Block for which buddy should be found |
479 | bondari | 307 | * |
480 | bondari | 308 | * @return Buddy for given block if found |
479 | bondari | 309 | */ |
814 | palkovsky | 310 | static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) |
311 | { |
||
539 | jermar | 312 | frame_t * frame; |
480 | bondari | 313 | zone_t * zone; |
564 | jermar | 314 | index_t index; |
480 | bondari | 315 | bool is_left, is_right; |
479 | bondari | 316 | |
480 | bondari | 317 | frame = list_get_instance(block, frame_t, buddy_link); |
533 | bondari | 318 | zone = (zone_t *) b->data; |
814 | palkovsky | 319 | ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order)); |
480 | bondari | 320 | |
724 | palkovsky | 321 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
322 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
||
814 | palkovsky | 323 | |
564 | jermar | 324 | ASSERT(is_left ^ is_right); |
533 | bondari | 325 | if (is_left) { |
814 | palkovsky | 326 | index = (frame_index(zone, frame)) + (1 << frame->buddy_order); |
1981 | jermar | 327 | } else { /* if (is_right) */ |
814 | palkovsky | 328 | index = (frame_index(zone, frame)) - (1 << frame->buddy_order); |
533 | bondari | 329 | } |
330 | |||
814 | palkovsky | 331 | if (frame_index_valid(zone, index)) { |
332 | if (zone->frames[index].buddy_order == frame->buddy_order && |
||
333 | zone->frames[index].refcount == 0) { |
||
533 | bondari | 334 | return &zone->frames[index].buddy_link; |
480 | bondari | 335 | } |
336 | } |
||
814 | palkovsky | 337 | |
539 | jermar | 338 | return NULL; |
479 | bondari | 339 | } |
340 | |||
341 | /** Buddy system bisect implementation |
||
342 | * |
||
489 | jermar | 343 | * @param b Buddy system. |
480 | bondari | 344 | * @param block Block to bisect |
345 | * |
||
346 | * @return right block |
||
479 | bondari | 347 | */ |
814 | palkovsky | 348 | static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) { |
480 | bondari | 349 | frame_t * frame_l, * frame_r; |
564 | jermar | 350 | |
480 | bondari | 351 | frame_l = list_get_instance(block, frame_t, buddy_link); |
533 | bondari | 352 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
564 | jermar | 353 | |
480 | bondari | 354 | return &frame_r->buddy_link; |
479 | bondari | 355 | } |
356 | |||
357 | /** Buddy system coalesce implementation |
||
358 | * |
||
489 | jermar | 359 | * @param b Buddy system. |
480 | bondari | 360 | * @param block_1 First block |
361 | * @param block_2 First block's buddy |
||
362 | * |
||
363 | * @return Coalesced block (actually block that represents lower address) |
||
479 | bondari | 364 | */ |
814 | palkovsky | 365 | static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, |
822 | palkovsky | 366 | link_t * block_2) |
367 | { |
||
814 | palkovsky | 368 | frame_t *frame1, *frame2; |
564 | jermar | 369 | |
480 | bondari | 370 | frame1 = list_get_instance(block_1, frame_t, buddy_link); |
371 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
||
564 | jermar | 372 | |
533 | bondari | 373 | return frame1 < frame2 ? block_1 : block_2; |
479 | bondari | 374 | } |
375 | |||
376 | /** Buddy system set_order implementation |
||
489 | jermar | 377 | * |
378 | * @param b Buddy system. |
||
480 | bondari | 379 | * @param block Buddy system block |
380 | * @param order Order to set |
||
479 | bondari | 381 | */ |
1780 | jermar | 382 | static void zone_buddy_set_order(buddy_system_t *b, link_t * block, uint8_t order) { |
480 | bondari | 383 | frame_t * frame; |
384 | frame = list_get_instance(block, frame_t, buddy_link); |
||
385 | frame->buddy_order = order; |
||
479 | bondari | 386 | } |
387 | |||
388 | /** Buddy system get_order implementation |
||
489 | jermar | 389 | * |
390 | * @param b Buddy system. |
||
480 | bondari | 391 | * @param block Buddy system block |
479 | bondari | 392 | * |
480 | bondari | 393 | * @return Order of block |
479 | bondari | 394 | */ |
1780 | jermar | 395 | static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t * block) { |
480 | bondari | 396 | frame_t * frame; |
397 | frame = list_get_instance(block, frame_t, buddy_link); |
||
398 | return frame->buddy_order; |
||
479 | bondari | 399 | } |
533 | bondari | 400 | |
401 | /** Buddy system mark_busy implementation |
||
402 | * |
||
403 | * @param b Buddy system |
||
404 | * @param block Buddy system block |
||
405 | * |
||
406 | */ |
||
814 | palkovsky | 407 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) { |
533 | bondari | 408 | frame_t * frame; |
822 | palkovsky | 409 | |
533 | bondari | 410 | frame = list_get_instance(block, frame_t, buddy_link); |
411 | frame->refcount = 1; |
||
412 | } |
||
676 | bondari | 413 | |
814 | palkovsky | 414 | /** Buddy system mark_available implementation |
415 | * |
||
416 | * @param b Buddy system |
||
417 | * @param block Buddy system block |
||
418 | * |
||
419 | */ |
||
420 | static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) { |
||
421 | frame_t * frame; |
||
422 | frame = list_get_instance(block, frame_t, buddy_link); |
||
423 | frame->refcount = 0; |
||
424 | } |
||
425 | |||
2083 | decky | 426 | static buddy_system_operations_t zone_buddy_system_operations = { |
814 | palkovsky | 427 | .find_buddy = zone_buddy_find_buddy, |
428 | .bisect = zone_buddy_bisect, |
||
429 | .coalesce = zone_buddy_coalesce, |
||
430 | .set_order = zone_buddy_set_order, |
||
431 | .get_order = zone_buddy_get_order, |
||
432 | .mark_busy = zone_buddy_mark_busy, |
||
433 | .mark_available = zone_buddy_mark_available, |
||
822 | palkovsky | 434 | .find_block = zone_buddy_find_block, |
435 | .print_id = zone_buddy_print_id |
||
814 | palkovsky | 436 | }; |
437 | |||
2059 | jermar | 438 | /******************/ |
814 | palkovsky | 439 | /* Zone functions */ |
2059 | jermar | 440 | /******************/ |
814 | palkovsky | 441 | |
442 | /** Allocate frame in particular zone |
||
443 | * |
||
444 | * Assume zone is locked |
||
1269 | decky | 445 | * Panics if allocation is impossible. |
814 | palkovsky | 446 | * |
1269 | decky | 447 | * @param zone Zone to allocate from. |
448 | * @param order Allocate exactly 2^order frames. |
||
449 | * |
||
814 | palkovsky | 450 | * @return Frame index in zone |
1269 | decky | 451 | * |
814 | palkovsky | 452 | */ |
1780 | jermar | 453 | static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order) |
814 | palkovsky | 454 | { |
455 | pfn_t v; |
||
456 | link_t *tmp; |
||
457 | frame_t *frame; |
||
458 | |||
459 | /* Allocate frames from zone buddy system */ |
||
460 | tmp = buddy_system_alloc(zone->buddy_system, order); |
||
461 | |||
462 | ASSERT(tmp); |
||
463 | |||
464 | /* Update zone information. */ |
||
465 | zone->free_count -= (1 << order); |
||
466 | zone->busy_count += (1 << order); |
||
467 | |||
468 | /* Frame will be actually a first frame of the block. */ |
||
469 | frame = list_get_instance(tmp, frame_t, buddy_link); |
||
470 | |||
471 | /* get frame address */ |
||
472 | v = make_frame_index(zone, frame); |
||
473 | return v; |
||
474 | } |
||
475 | |||
476 | /** Free frame from zone |
||
477 | * |
||
478 | * Assume zone is locked |
||
1568 | palkovsky | 479 | * |
480 | * @param zone Pointer to zone from which the frame is to be freed |
||
481 | * @param frame_idx Frame index relative to zone |
||
814 | palkovsky | 482 | */ |
820 | jermar | 483 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 484 | { |
485 | frame_t *frame; |
||
1780 | jermar | 486 | uint8_t order; |
814 | palkovsky | 487 | |
488 | frame = &zone->frames[frame_idx]; |
||
489 | |||
490 | /* remember frame order */ |
||
491 | order = frame->buddy_order; |
||
492 | |||
493 | ASSERT(frame->refcount); |
||
494 | |||
495 | if (!--frame->refcount) { |
||
496 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
||
946 | jermar | 497 | |
498 | /* Update zone information. */ |
||
499 | zone->free_count += (1 << order); |
||
500 | zone->busy_count -= (1 << order); |
||
814 | palkovsky | 501 | } |
502 | } |
||
503 | |||
504 | /** Return frame from zone */ |
||
820 | jermar | 505 | static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 506 | { |
507 | ASSERT(frame_idx < zone->count); |
||
508 | return &zone->frames[frame_idx]; |
||
509 | } |
||
510 | |||
511 | /** Mark frame in zone unavailable to allocation */ |
||
820 | jermar | 512 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
814 | palkovsky | 513 | { |
514 | frame_t *frame; |
||
515 | link_t *link; |
||
516 | |||
517 | frame = zone_get_frame(zone, frame_idx); |
||
822 | palkovsky | 518 | if (frame->refcount) |
519 | return; |
||
814 | palkovsky | 520 | link = buddy_system_alloc_block(zone->buddy_system, |
521 | &frame->buddy_link); |
||
522 | ASSERT(link); |
||
523 | zone->free_count--; |
||
524 | } |
||
525 | |||
822 | palkovsky | 526 | /** |
527 | * Join 2 zones |
||
528 | * |
||
529 | * Expect zone_t *z to point to space at least zone_conf_size large |
||
530 | * |
||
531 | * Assume z1 & z2 are locked |
||
1568 | palkovsky | 532 | * |
533 | * @param z Target zone structure pointer |
||
534 | * @param z1 Zone to merge |
||
535 | * @param z2 Zone to merge |
||
822 | palkovsky | 536 | */ |
537 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
||
538 | { |
||
1780 | jermar | 539 | uint8_t max_order; |
822 | palkovsky | 540 | int i, z2idx; |
541 | pfn_t frame_idx; |
||
542 | frame_t *frame; |
||
543 | |||
544 | ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count)); |
||
545 | ASSERT(z1->base < z2->base); |
||
546 | |||
547 | spinlock_initialize(&z->lock, "zone_lock"); |
||
548 | z->base = z1->base; |
||
549 | z->count = z2->base+z2->count - z1->base; |
||
550 | z->flags = z1->flags & z2->flags; |
||
551 | |||
552 | z->free_count = z1->free_count + z2->free_count; |
||
553 | z->busy_count = z1->busy_count + z2->busy_count; |
||
554 | |||
555 | max_order = fnzb(z->count); |
||
556 | |||
557 | z->buddy_system = (buddy_system_t *)&z[1]; |
||
558 | buddy_system_create(z->buddy_system, max_order, |
||
559 | &zone_buddy_system_operations, |
||
560 | (void *) z); |
||
561 | |||
562 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
||
563 | for (i = 0; i < z->count; i++) { |
||
564 | /* This marks all frames busy */ |
||
565 | frame_initialize(&z->frames[i]); |
||
566 | } |
||
567 | /* Copy frames from both zones to preserve full frame orders, |
||
1093 | palkovsky | 568 | * parents etc. Set all free frames with refcount=0 to 1, because |
822 | palkovsky | 569 | * we add all free frames to buddy allocator later again, clear |
1093 | palkovsky | 570 | * order to 0. Don't set busy frames with refcount=0, as they |
571 | * will not be reallocated during merge and it would make later |
||
572 | * problems with allocation/free. |
||
822 | palkovsky | 573 | */ |
574 | for (i=0; i<z1->count; i++) |
||
575 | z->frames[i] = z1->frames[i]; |
||
576 | for (i=0; i < z2->count; i++) { |
||
577 | z2idx = i + (z2->base - z1->base); |
||
578 | z->frames[z2idx] = z2->frames[i]; |
||
579 | } |
||
1093 | palkovsky | 580 | i = 0; |
581 | while (i < z->count) { |
||
582 | if (z->frames[i].refcount) { |
||
583 | /* skip busy frames */ |
||
584 | i += 1 << z->frames[i].buddy_order; |
||
585 | } else { /* Free frames, set refcount=1 */ |
||
586 | /* All free frames have refcount=0, we need not |
||
587 | * to check the order */ |
||
822 | palkovsky | 588 | z->frames[i].refcount = 1; |
589 | z->frames[i].buddy_order = 0; |
||
1093 | palkovsky | 590 | i++; |
822 | palkovsky | 591 | } |
592 | } |
||
593 | /* Add free blocks from the 2 original zones */ |
||
594 | while (zone_can_alloc(z1, 0)) { |
||
595 | frame_idx = zone_frame_alloc(z1, 0); |
||
596 | frame = &z->frames[frame_idx]; |
||
597 | frame->refcount = 0; |
||
598 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
||
599 | } |
||
600 | while (zone_can_alloc(z2, 0)) { |
||
601 | frame_idx = zone_frame_alloc(z2, 0); |
||
602 | frame = &z->frames[frame_idx + (z2->base-z1->base)]; |
||
603 | frame->refcount = 0; |
||
604 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
||
605 | } |
||
606 | } |
||
607 | |||
608 | /** Return old configuration frames into the zone |
||
609 | * |
||
610 | * We have several cases |
||
611 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
||
824 | palkovsky | 612 | * - the conf. data was created by zone_create or |
613 | * updated with reduce_region -> free every frame |
||
614 | * |
||
615 | * @param newzone The actual zone where freeing should occur |
||
616 | * @param oldzone Pointer to old zone configuration data that should |
||
617 | * be freed from new zone |
||
822 | palkovsky | 618 | */ |
619 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
||
620 | { |
||
621 | pfn_t pfn; |
||
622 | frame_t *frame; |
||
623 | count_t cframes; |
||
624 | int i; |
||
625 | |||
1780 | jermar | 626 | pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone)); |
822 | palkovsky | 627 | cframes = SIZE2FRAMES(zone_conf_size(oldzone->count)); |
628 | |||
629 | if (pfn < newzone->base || pfn >= newzone->base + newzone->count) |
||
630 | return; |
||
631 | |||
632 | frame = &newzone->frames[pfn - newzone->base]; |
||
824 | palkovsky | 633 | ASSERT(!frame->buddy_order); |
822 | palkovsky | 634 | |
635 | for (i=0; i < cframes; i++) { |
||
636 | newzone->busy_count++; |
||
637 | zone_frame_free(newzone, pfn+i-newzone->base); |
||
638 | } |
||
639 | } |
||
640 | |||
824 | palkovsky | 641 | /** Reduce allocated block to count of order 0 frames |
642 | * |
||
643 | * The allocated block need 2^order frames of space. Reduce all frames |
||
1568 | palkovsky | 644 | * in block to order 0 and free the unneeded frames. This means, that |
645 | * when freeing the previously allocated block starting with frame_idx, |
||
646 | * you have to free every frame. |
||
824 | palkovsky | 647 | * |
648 | * @param zone |
||
649 | * @param frame_idx Index to block |
||
650 | * @param count Allocated space in block |
||
651 | */ |
||
652 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
||
653 | { |
||
654 | count_t i; |
||
1780 | jermar | 655 | uint8_t order; |
824 | palkovsky | 656 | frame_t *frame; |
657 | |||
658 | ASSERT(frame_idx+count < zone->count); |
||
659 | |||
660 | order = zone->frames[frame_idx].buddy_order; |
||
661 | ASSERT((1 << order) >= count); |
||
662 | |||
663 | /* Reduce all blocks to order 0 */ |
||
664 | for (i=0; i < (1 << order); i++) { |
||
665 | frame = &zone->frames[i + frame_idx]; |
||
666 | frame->buddy_order = 0; |
||
667 | if (! frame->refcount) |
||
668 | frame->refcount = 1; |
||
669 | ASSERT(frame->refcount == 1); |
||
670 | } |
||
671 | /* Free unneeded frames */ |
||
672 | for (i=count; i < (1 << order); i++) { |
||
673 | zone_frame_free(zone, i + frame_idx); |
||
674 | } |
||
675 | } |
||
676 | |||
822 | palkovsky | 677 | /** Merge zones z1 and z2 |
678 | * |
||
679 | * - the zones must be 2 zones with no zone existing in between, |
||
680 | * which means that z2 = z1+1 |
||
681 | * |
||
682 | * - When you create a new zone, the frame allocator configuration does |
||
683 | * not to be 2^order size. Once the allocator is running it is no longer |
||
684 | * possible, merged configuration data occupies more space :-/ |
||
685 | */ |
||
686 | void zone_merge(int z1, int z2) |
||
687 | { |
||
688 | ipl_t ipl; |
||
689 | zone_t *zone1, *zone2, *newzone; |
||
690 | int cframes; |
||
1780 | jermar | 691 | uint8_t order; |
822 | palkovsky | 692 | int i; |
693 | pfn_t pfn; |
||
694 | |||
695 | ipl = interrupts_disable(); |
||
696 | spinlock_lock(&zones.lock); |
||
697 | |||
698 | if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count) |
||
699 | goto errout; |
||
700 | /* We can join only 2 zones with none existing inbetween */ |
||
701 | if (z2-z1 != 1) |
||
702 | goto errout; |
||
703 | |||
704 | zone1 = zones.info[z1]; |
||
705 | zone2 = zones.info[z2]; |
||
706 | spinlock_lock(&zone1->lock); |
||
707 | spinlock_lock(&zone2->lock); |
||
708 | |||
709 | cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base)); |
||
1700 | palkovsky | 710 | if (cframes == 1) |
711 | order = 0; |
||
712 | else |
||
713 | order = fnzb(cframes - 1) + 1; |
||
822 | palkovsky | 714 | |
715 | /* Allocate zonedata inside one of the zones */ |
||
716 | if (zone_can_alloc(zone1, order)) |
||
717 | pfn = zone1->base + zone_frame_alloc(zone1, order); |
||
718 | else if (zone_can_alloc(zone2, order)) |
||
719 | pfn = zone2->base + zone_frame_alloc(zone2, order); |
||
720 | else |
||
721 | goto errout2; |
||
722 | |||
723 | newzone = (zone_t *)PA2KA(PFN2ADDR(pfn)); |
||
724 | |||
725 | _zone_merge(newzone, zone1, zone2); |
||
726 | |||
824 | palkovsky | 727 | /* Free unneeded config frames */ |
728 | zone_reduce_region(newzone, pfn - newzone->base, cframes); |
||
822 | palkovsky | 729 | /* Subtract zone information from busy frames */ |
824 | palkovsky | 730 | newzone->busy_count -= cframes; |
822 | palkovsky | 731 | |
824 | palkovsky | 732 | /* Replace existing zones in zoneinfo list */ |
822 | palkovsky | 733 | zones.info[z1] = newzone; |
1037 | decky | 734 | for (i = z2 + 1; i < zones.count; i++) |
735 | zones.info[i - 1] = zones.info[i]; |
||
822 | palkovsky | 736 | zones.count--; |
737 | |||
738 | /* Free old zone information */ |
||
739 | return_config_frames(newzone, zone1); |
||
740 | return_config_frames(newzone, zone2); |
||
741 | errout2: |
||
742 | /* Nobody is allowed to enter to zone, so we are safe |
||
743 | * to touch the spinlocks last time */ |
||
744 | spinlock_unlock(&zone1->lock); |
||
745 | spinlock_unlock(&zone2->lock); |
||
746 | errout: |
||
747 | spinlock_unlock(&zones.lock); |
||
748 | interrupts_restore(ipl); |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * Merge all zones into one big zone |
||
753 | * |
||
754 | * It is reasonable to do this on systems whose bios reports parts in chunks, |
||
755 | * so that we could have 1 zone (it's faster). |
||
756 | */ |
||
757 | void zone_merge_all(void) |
||
758 | { |
||
759 | int count = zones.count; |
||
760 | |||
761 | while (zones.count > 1 && --count) { |
||
762 | zone_merge(0,1); |
||
763 | break; |
||
764 | } |
||
765 | } |
||
766 | |||
814 | palkovsky | 767 | /** Create frame zone |
768 | * |
||
769 | * Create new frame zone. |
||
770 | * |
||
771 | * @param start Physical address of the first frame within the zone. |
||
1568 | palkovsky | 772 | * @param count Count of frames in zone |
773 | * @param z Address of configuration information of zone |
||
814 | palkovsky | 774 | * @param flags Zone flags. |
775 | * |
||
776 | * @return Initialized zone. |
||
777 | */ |
||
822 | palkovsky | 778 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
814 | palkovsky | 779 | { |
780 | int i; |
||
1780 | jermar | 781 | uint8_t max_order; |
814 | palkovsky | 782 | |
783 | spinlock_initialize(&z->lock, "zone_lock"); |
||
784 | z->base = start; |
||
785 | z->count = count; |
||
786 | z->flags = flags; |
||
787 | z->free_count = count; |
||
788 | z->busy_count = 0; |
||
789 | |||
790 | /* |
||
791 | * Compute order for buddy system, initialize |
||
792 | */ |
||
822 | palkovsky | 793 | max_order = fnzb(count); |
814 | palkovsky | 794 | z->buddy_system = (buddy_system_t *)&z[1]; |
795 | |||
796 | buddy_system_create(z->buddy_system, max_order, |
||
797 | &zone_buddy_system_operations, |
||
798 | (void *) z); |
||
799 | |||
800 | /* Allocate frames _after_ the conframe */ |
||
801 | /* Check sizes */ |
||
802 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
||
803 | for (i = 0; i<count; i++) { |
||
804 | frame_initialize(&z->frames[i]); |
||
805 | } |
||
852 | palkovsky | 806 | |
814 | palkovsky | 807 | /* Stuffing frames */ |
808 | for (i = 0; i < count; i++) { |
||
809 | z->frames[i].refcount = 0; |
||
810 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
||
811 | } |
||
812 | } |
||
813 | |||
1568 | palkovsky | 814 | /** Compute configuration data size for zone |
815 | * |
||
816 | * @param count Size of zone in frames |
||
817 | * @return Size of zone configuration info (in bytes) |
||
818 | */ |
||
1780 | jermar | 819 | uintptr_t zone_conf_size(count_t count) |
814 | palkovsky | 820 | { |
821 | int size = sizeof(zone_t) + count*sizeof(frame_t); |
||
822 | int max_order; |
||
823 | |||
822 | palkovsky | 824 | max_order = fnzb(count); |
814 | palkovsky | 825 | size += buddy_conf_size(max_order); |
826 | return size; |
||
827 | } |
||
828 | |||
829 | /** Create and add zone to system |
||
830 | * |
||
1568 | palkovsky | 831 | * @param start First frame number (absolute) |
832 | * @param count Size of zone in frames |
||
833 | * @param confframe Where configuration frames are supposed to be. |
||
834 | * Automatically checks, that we will not disturb the |
||
835 | * kernel and possibly init. |
||
814 | palkovsky | 836 | * If confframe is given _outside_ this zone, it is expected, |
837 | * that the area is already marked BUSY and big enough |
||
1568 | palkovsky | 838 | * to contain zone_conf_size() amount of data. |
839 | * If the confframe is inside the area, the zone free frame |
||
840 | * information is modified not to include it. |
||
822 | palkovsky | 841 | * |
842 | * @return Zone number or -1 on error |
||
814 | palkovsky | 843 | */ |
822 | palkovsky | 844 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
814 | palkovsky | 845 | { |
846 | zone_t *z; |
||
1780 | jermar | 847 | uintptr_t addr; |
820 | jermar | 848 | count_t confcount; |
814 | palkovsky | 849 | int i; |
822 | palkovsky | 850 | int znum; |
814 | palkovsky | 851 | |
852 | /* Theoretically we could have here 0, practically make sure |
||
853 | * nobody tries to do that. If some platform requires, remove |
||
854 | * the assert |
||
855 | */ |
||
856 | ASSERT(confframe); |
||
857 | /* If conframe is supposed to be inside our zone, then make sure |
||
858 | * it does not span kernel & init |
||
859 | */ |
||
822 | palkovsky | 860 | confcount = SIZE2FRAMES(zone_conf_size(count)); |
814 | palkovsky | 861 | if (confframe >= start && confframe < start+count) { |
1037 | decky | 862 | for (;confframe < start + count; confframe++) { |
814 | palkovsky | 863 | addr = PFN2ADDR(confframe); |
1037 | decky | 864 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size)) |
814 | palkovsky | 865 | continue; |
1037 | decky | 866 | |
1833 | decky | 867 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.stack_base), config.stack_size)) |
868 | continue; |
||
869 | |||
1037 | decky | 870 | bool overlap = false; |
871 | count_t i; |
||
872 | for (i = 0; i < init.cnt; i++) |
||
873 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) { |
||
874 | overlap = true; |
||
875 | break; |
||
876 | } |
||
877 | if (overlap) |
||
878 | continue; |
||
879 | |||
814 | palkovsky | 880 | break; |
881 | } |
||
1037 | decky | 882 | if (confframe >= start + count) |
814 | palkovsky | 883 | panic("Cannot find configuration data for zone."); |
884 | } |
||
885 | |||
822 | palkovsky | 886 | z = (zone_t *)PA2KA(PFN2ADDR(confframe)); |
887 | zone_construct(start, count, z, flags); |
||
888 | znum = zones_add_zone(z); |
||
889 | if (znum == -1) |
||
890 | return -1; |
||
891 | |||
814 | palkovsky | 892 | /* If confdata in zone, mark as unavailable */ |
893 | if (confframe >= start && confframe < start+count) |
||
894 | for (i=confframe; i<confframe+confcount; i++) { |
||
895 | zone_mark_unavailable(z, i - z->base); |
||
896 | } |
||
822 | palkovsky | 897 | return znum; |
814 | palkovsky | 898 | } |
899 | |||
900 | /***************************************/ |
||
901 | /* Frame functions */ |
||
902 | |||
903 | /** Set parent of frame */ |
||
904 | void frame_set_parent(pfn_t pfn, void *data, int hint) |
||
905 | { |
||
906 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
||
907 | |||
908 | ASSERT(zone); |
||
909 | |||
910 | zone_get_frame(zone, pfn-zone->base)->parent = data; |
||
911 | spinlock_unlock(&zone->lock); |
||
912 | } |
||
913 | |||
914 | void * frame_get_parent(pfn_t pfn, int hint) |
||
915 | { |
||
916 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
||
917 | void *res; |
||
918 | |||
919 | ASSERT(zone); |
||
920 | res = zone_get_frame(zone, pfn - zone->base)->parent; |
||
921 | |||
922 | spinlock_unlock(&zone->lock); |
||
923 | return res; |
||
924 | } |
||
925 | |||
926 | /** Allocate power-of-two frames of physical memory. |
||
927 | * |
||
1269 | decky | 928 | * @param order Allocate exactly 2^order frames. |
929 | * @param flags Flags for host zone selection and address processing. |
||
930 | * @param pzone Preferred zone |
||
814 | palkovsky | 931 | * |
1760 | palkovsky | 932 | * @return Physical address of the allocated frame. |
1269 | decky | 933 | * |
814 | palkovsky | 934 | */ |
1780 | jermar | 935 | void * frame_alloc_generic(uint8_t order, int flags, int *pzone) |
814 | palkovsky | 936 | { |
937 | ipl_t ipl; |
||
938 | int freed; |
||
939 | pfn_t v; |
||
940 | zone_t *zone; |
||
941 | |||
942 | loop: |
||
943 | ipl = interrupts_disable(); |
||
1269 | decky | 944 | |
814 | palkovsky | 945 | /* |
946 | * First, find suitable frame zone. |
||
947 | */ |
||
1981 | jermar | 948 | zone = find_free_zone_and_lock(order, pzone); |
1269 | decky | 949 | |
814 | palkovsky | 950 | /* If no memory, reclaim some slab memory, |
951 | if it does not help, reclaim all */ |
||
952 | if (!zone && !(flags & FRAME_NO_RECLAIM)) { |
||
953 | freed = slab_reclaim(0); |
||
954 | if (freed) |
||
1981 | jermar | 955 | zone = find_free_zone_and_lock(order, pzone); |
814 | palkovsky | 956 | if (!zone) { |
957 | freed = slab_reclaim(SLAB_RECLAIM_ALL); |
||
958 | if (freed) |
||
1981 | jermar | 959 | zone = find_free_zone_and_lock(order, pzone); |
814 | palkovsky | 960 | } |
961 | } |
||
962 | if (!zone) { |
||
963 | /* |
||
964 | * TODO: Sleep until frames are available again. |
||
965 | */ |
||
966 | interrupts_restore(ipl); |
||
967 | |||
1766 | palkovsky | 968 | if (flags & FRAME_ATOMIC) |
1760 | palkovsky | 969 | return 0; |
814 | palkovsky | 970 | |
971 | panic("Sleep not implemented.\n"); |
||
972 | goto loop; |
||
973 | } |
||
1269 | decky | 974 | |
975 | v = zone_frame_alloc(zone, order); |
||
814 | palkovsky | 976 | v += zone->base; |
977 | |||
978 | spinlock_unlock(&zone->lock); |
||
979 | interrupts_restore(ipl); |
||
980 | |||
1760 | palkovsky | 981 | if (flags & FRAME_KA) |
982 | return (void *)PA2KA(PFN2ADDR(v)); |
||
983 | return (void *)PFN2ADDR(v); |
||
814 | palkovsky | 984 | } |
985 | |||
986 | /** Free a frame. |
||
987 | * |
||
1854 | jermar | 988 | * Find respective frame structure for supplied physical frame address. |
814 | palkovsky | 989 | * Decrement frame reference count. |
990 | * If it drops to zero, move the frame structure to free list. |
||
991 | * |
||
1760 | palkovsky | 992 | * @param Frame Physical Address of of the frame to be freed. |
814 | palkovsky | 993 | */ |
1780 | jermar | 994 | void frame_free(uintptr_t frame) |
814 | palkovsky | 995 | { |
996 | ipl_t ipl; |
||
997 | zone_t *zone; |
||
1760 | palkovsky | 998 | pfn_t pfn = ADDR2PFN(frame); |
814 | palkovsky | 999 | |
1000 | ipl = interrupts_disable(); |
||
1001 | |||
1002 | /* |
||
1003 | * First, find host frame zone for addr. |
||
1004 | */ |
||
1005 | zone = find_zone_and_lock(pfn,NULL); |
||
1006 | ASSERT(zone); |
||
1007 | |||
1008 | zone_frame_free(zone, pfn-zone->base); |
||
1009 | |||
1010 | spinlock_unlock(&zone->lock); |
||
1011 | interrupts_restore(ipl); |
||
1012 | } |
||
1013 | |||
1236 | jermar | 1014 | /** Add reference to frame. |
1015 | * |
||
1016 | * Find respective frame structure for supplied PFN and |
||
1017 | * increment frame reference count. |
||
1018 | * |
||
1708 | jermar | 1019 | * @param pfn Frame number of the frame to be freed. |
1236 | jermar | 1020 | */ |
1021 | void frame_reference_add(pfn_t pfn) |
||
1022 | { |
||
1023 | ipl_t ipl; |
||
1024 | zone_t *zone; |
||
1025 | frame_t *frame; |
||
814 | palkovsky | 1026 | |
1236 | jermar | 1027 | ipl = interrupts_disable(); |
1028 | |||
1029 | /* |
||
1030 | * First, find host frame zone for addr. |
||
1031 | */ |
||
1032 | zone = find_zone_and_lock(pfn,NULL); |
||
1033 | ASSERT(zone); |
||
1034 | |||
1035 | frame = &zone->frames[pfn-zone->base]; |
||
1036 | frame->refcount++; |
||
1037 | |||
1038 | spinlock_unlock(&zone->lock); |
||
1039 | interrupts_restore(ipl); |
||
1040 | } |
||
814 | palkovsky | 1041 | |
1042 | /** Mark given range unavailable in frame zones */ |
||
820 | jermar | 1043 | void frame_mark_unavailable(pfn_t start, count_t count) |
814 | palkovsky | 1044 | { |
1045 | int i; |
||
1046 | zone_t *zone; |
||
1047 | int prefzone = 0; |
||
852 | palkovsky | 1048 | |
822 | palkovsky | 1049 | for (i=0; i < count; i++) { |
1981 | jermar | 1050 | zone = find_zone_and_lock(start + i, &prefzone); |
814 | palkovsky | 1051 | if (!zone) /* PFN not found */ |
1052 | continue; |
||
1981 | jermar | 1053 | zone_mark_unavailable(zone, start + i - zone->base); |
814 | palkovsky | 1054 | |
1055 | spinlock_unlock(&zone->lock); |
||
1056 | } |
||
1057 | } |
||
1058 | |||
1059 | /** Initialize physical memory management |
||
1060 | * |
||
1061 | * Initialize physical memory managemnt. |
||
1062 | */ |
||
1063 | void frame_init(void) |
||
1064 | { |
||
1065 | if (config.cpu_active == 1) { |
||
1066 | zones.count = 0; |
||
1981 | jermar | 1067 | spinlock_initialize(&zones.lock, "zones.lock"); |
814 | palkovsky | 1068 | } |
1069 | /* Tell the architecture to create some memory */ |
||
1070 | frame_arch_init(); |
||
1071 | if (config.cpu_active == 1) { |
||
1833 | decky | 1072 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), SIZE2FRAMES(config.kernel_size)); |
1073 | frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), SIZE2FRAMES(config.stack_size)); |
||
1037 | decky | 1074 | |
1075 | count_t i; |
||
1076 | for (i = 0; i < init.cnt; i++) |
||
1077 | frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size)); |
||
1599 | palkovsky | 1078 | |
1894 | jermar | 1079 | if (ballocs.size) |
1080 | frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)), SIZE2FRAMES(ballocs.size)); |
||
1081 | |||
1599 | palkovsky | 1082 | /* Black list first frame, as allocating NULL would |
1790 | jermar | 1083 | * fail in some places */ |
1599 | palkovsky | 1084 | frame_mark_unavailable(0, 1); |
814 | palkovsky | 1085 | } |
1086 | } |
||
1087 | |||
1088 | |||
1089 | |||
677 | bondari | 1090 | /** Prints list of zones |
1091 | * |
||
1092 | */ |
||
676 | bondari | 1093 | void zone_print_list(void) { |
1094 | zone_t *zone = NULL; |
||
814 | palkovsky | 1095 | int i; |
701 | jermar | 1096 | ipl_t ipl; |
1097 | |||
1098 | ipl = interrupts_disable(); |
||
814 | palkovsky | 1099 | spinlock_lock(&zones.lock); |
2035 | decky | 1100 | printf("# base address free frames busy frames\n"); |
1101 | printf("-- ------------ ------------ ------------\n"); |
||
1037 | decky | 1102 | for (i = 0; i < zones.count; i++) { |
814 | palkovsky | 1103 | zone = zones.info[i]; |
683 | bondari | 1104 | spinlock_lock(&zone->lock); |
2035 | decky | 1105 | printf("%-2d %12p %12zd %12zd\n", i, PFN2ADDR(zone->base), zone->free_count, zone->busy_count); |
701 | jermar | 1106 | spinlock_unlock(&zone->lock); |
676 | bondari | 1107 | } |
814 | palkovsky | 1108 | spinlock_unlock(&zones.lock); |
701 | jermar | 1109 | interrupts_restore(ipl); |
676 | bondari | 1110 | } |
1111 | |||
1708 | jermar | 1112 | /** Prints zone details. |
677 | bondari | 1113 | * |
1708 | jermar | 1114 | * @param num Zone base address or zone number. |
677 | bondari | 1115 | */ |
822 | palkovsky | 1116 | void zone_print_one(int num) { |
814 | palkovsky | 1117 | zone_t *zone = NULL; |
701 | jermar | 1118 | ipl_t ipl; |
822 | palkovsky | 1119 | int i; |
701 | jermar | 1120 | |
1121 | ipl = interrupts_disable(); |
||
814 | palkovsky | 1122 | spinlock_lock(&zones.lock); |
822 | palkovsky | 1123 | |
1037 | decky | 1124 | for (i = 0; i < zones.count; i++) { |
824 | palkovsky | 1125 | if (i == num || PFN2ADDR(zones.info[i]->base) == num) { |
822 | palkovsky | 1126 | zone = zones.info[i]; |
1127 | break; |
||
1128 | } |
||
676 | bondari | 1129 | } |
822 | palkovsky | 1130 | if (!zone) { |
1131 | printf("Zone not found.\n"); |
||
1132 | goto out; |
||
1133 | } |
||
676 | bondari | 1134 | |
683 | bondari | 1135 | spinlock_lock(&zone->lock); |
822 | palkovsky | 1136 | printf("Memory zone information\n"); |
1780 | jermar | 1137 | printf("Zone base address: %#.*p\n", sizeof(uintptr_t) * 2, PFN2ADDR(zone->base)); |
1196 | cejka | 1138 | printf("Zone size: %zd frames (%zdK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10); |
1139 | printf("Allocated space: %zd frames (%zdK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10); |
||
2048 | jermar | 1140 | printf("Available space: %zd frames (%zdK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10); |
686 | bondari | 1141 | buddy_system_structure_print(zone->buddy_system, FRAME_SIZE); |
683 | bondari | 1142 | |
1143 | spinlock_unlock(&zone->lock); |
||
822 | palkovsky | 1144 | out: |
814 | palkovsky | 1145 | spinlock_unlock(&zones.lock); |
701 | jermar | 1146 | interrupts_restore(ipl); |
676 | bondari | 1147 | } |
1148 | |||
1757 | jermar | 1149 | /** @} |
1702 | cejka | 1150 | */ |