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