source: mainline/kernel/generic/src/mm/frame.c@ 28295955

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 28295955 was 28295955, checked in by Jakub Jermar <jakub@…>, 14 years ago

frame_free_generic() can actually free multiple frames.

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