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

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