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

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

size_t as a return type of zone_conf_size() is more natural than uintptr_t

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