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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2650d3 was e2650d3, checked in by Martin Decky <martin@…>, 15 years ago

rename PA_overlaps to PA_OVERLAPS
be more tolerant about some cases of overlapping zones (when it is actually not an issue)

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