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

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

Small cleanup in frame.[ch].

  • 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 */
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 if (zones.info[i - 1].buddy_system != NULL)
765 zones.info[i - 1].buddy_system->data =
766 (void *) &zones.info[i - 1];
767 }
768
769 zones.count--;
770
771errout:
772 irq_spinlock_unlock(&zones.lock, true);
773
774 return ret;
775}
776
777/** Merge all mergeable zones into one big zone.
778 *
779 * It is reasonable to do this on systems where
780 * BIOS reports parts in chunks, so that we could
781 * have 1 zone (it's faster).
782 *
783 */
784void zone_merge_all(void)
785{
786 size_t i = 0;
787 while (i < zones.count) {
788 if (!zone_merge(i, i + 1))
789 i++;
790 }
791}
792
793/** Create new frame zone.
794 *
795 * @param zone Zone to construct.
796 * @param buddy Address of buddy system configuration information.
797 * @param start Physical address of the first frame within the zone.
798 * @param count Count of frames in zone.
799 * @param flags Zone flags.
800 *
801 * @return Initialized zone.
802 *
803 */
804NO_TRACE static void zone_construct(zone_t *zone, buddy_system_t *buddy,
805 pfn_t start, size_t count, zone_flags_t flags)
806{
807 zone->base = start;
808 zone->count = count;
809 zone->flags = flags;
810 zone->free_count = count;
811 zone->busy_count = 0;
812 zone->buddy_system = buddy;
813
814 if (zone_flags_available(flags)) {
815 /*
816 * Compute order for buddy system and initialize
817 */
818 uint8_t order = fnzb(count);
819 buddy_system_create(zone->buddy_system, order,
820 &zone_buddy_system_operations, (void *) zone);
821
822 /* Allocate frames _after_ the confframe */
823
824 /* Check sizes */
825 zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
826 buddy_conf_size(order));
827
828 size_t i;
829 for (i = 0; i < count; i++)
830 frame_initialize(&zone->frames[i]);
831
832 /* Stuffing frames */
833 for (i = 0; i < count; i++) {
834 zone->frames[i].refcount = 0;
835 buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
836 }
837 } else
838 zone->frames = NULL;
839}
840
841/** Compute configuration data size for zone.
842 *
843 * @param count Size of zone in frames.
844 *
845 * @return Size of zone configuration info (in bytes).
846 *
847 */
848size_t zone_conf_size(size_t count)
849{
850 return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
851}
852
853/** Create and add zone to system.
854 *
855 * @param start First frame number (absolute).
856 * @param count Size of zone in frames.
857 * @param confframe Where configuration frames are supposed to be.
858 * Automatically checks, that we will not disturb the
859 * kernel and possibly init. If confframe is given
860 * _outside_ this zone, it is expected, that the area is
861 * already marked BUSY and big enough to contain
862 * zone_conf_size() amount of data. If the confframe is
863 * inside the area, the zone free frame information is
864 * modified not to include it.
865 *
866 * @return Zone number or -1 on error.
867 *
868 */
869size_t zone_create(pfn_t start, size_t count, pfn_t confframe,
870 zone_flags_t flags)
871{
872 irq_spinlock_lock(&zones.lock, true);
873
874 if (zone_flags_available(flags)) { /* Create available zone */
875 /* Theoretically we could have NULL here, practically make sure
876 * nobody tries to do that. If some platform requires, remove
877 * the assert
878 */
879 ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
880
881 /* If confframe is supposed to be inside our zone, then make sure
882 * it does not span kernel & init
883 */
884 size_t confcount = SIZE2FRAMES(zone_conf_size(count));
885 if ((confframe >= start) && (confframe < start + count)) {
886 for (; confframe < start + count; confframe++) {
887 uintptr_t addr = PFN2ADDR(confframe);
888 if (overlaps(addr, PFN2ADDR(confcount),
889 KA2PA(config.base), config.kernel_size))
890 continue;
891
892 if (overlaps(addr, PFN2ADDR(confcount),
893 KA2PA(config.stack_base), config.stack_size))
894 continue;
895
896 bool overlap = false;
897 size_t i;
898 for (i = 0; i < init.cnt; i++)
899 if (overlaps(addr, PFN2ADDR(confcount),
900 KA2PA(init.tasks[i].addr),
901 init.tasks[i].size)) {
902 overlap = true;
903 break;
904 }
905 if (overlap)
906 continue;
907
908 break;
909 }
910
911 if (confframe >= start + count)
912 panic("Cannot find configuration data for zone.");
913 }
914
915 size_t znum = zones_insert_zone(start, count, flags);
916 if (znum == (size_t) -1) {
917 irq_spinlock_unlock(&zones.lock, true);
918 return (size_t) -1;
919 }
920
921 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
922 zone_construct(&zones.info[znum], buddy, start, count, flags);
923
924 /* If confdata in zone, mark as unavailable */
925 if ((confframe >= start) && (confframe < start + count)) {
926 size_t i;
927 for (i = confframe; i < confframe + confcount; i++)
928 zone_mark_unavailable(&zones.info[znum],
929 i - zones.info[znum].base);
930 }
931
932 irq_spinlock_unlock(&zones.lock, true);
933
934 return znum;
935 }
936
937 /* Non-available zone */
938 size_t znum = zones_insert_zone(start, count, flags);
939 if (znum == (size_t) -1) {
940 irq_spinlock_unlock(&zones.lock, true);
941 return (size_t) -1;
942 }
943 zone_construct(&zones.info[znum], NULL, start, count, flags);
944
945 irq_spinlock_unlock(&zones.lock, true);
946
947 return znum;
948}
949
950/*******************/
951/* Frame functions */
952/*******************/
953
954/** Set parent of frame. */
955void frame_set_parent(pfn_t pfn, void *data, size_t hint)
956{
957 irq_spinlock_lock(&zones.lock, true);
958
959 size_t znum = find_zone(pfn, 1, hint);
960
961 ASSERT(znum != (size_t) -1);
962
963 zone_get_frame(&zones.info[znum],
964 pfn - zones.info[znum].base)->parent = data;
965
966 irq_spinlock_unlock(&zones.lock, true);
967}
968
969void *frame_get_parent(pfn_t pfn, size_t hint)
970{
971 irq_spinlock_lock(&zones.lock, true);
972
973 size_t znum = find_zone(pfn, 1, hint);
974
975 ASSERT(znum != (size_t) -1);
976
977 void *res = zone_get_frame(&zones.info[znum],
978 pfn - zones.info[znum].base)->parent;
979
980 irq_spinlock_unlock(&zones.lock, true);
981
982 return res;
983}
984
985/** Allocate power-of-two frames of physical memory.
986 *
987 * @param order Allocate exactly 2^order frames.
988 * @param flags Flags for host zone selection and address processing.
989 * @param pzone Preferred zone.
990 *
991 * @return Physical address of the allocated frame.
992 *
993 */
994void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
995{
996 size_t size = ((size_t) 1) << order;
997 size_t hint = pzone ? (*pzone) : 0;
998
999loop:
1000 irq_spinlock_lock(&zones.lock, true);
1001
1002 /*
1003 * First, find suitable frame zone.
1004 */
1005 size_t znum = find_free_zone(order,
1006 FRAME_TO_ZONE_FLAGS(flags), hint);
1007
1008 /* If no memory, reclaim some slab memory,
1009 if it does not help, reclaim all */
1010 if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
1011 irq_spinlock_unlock(&zones.lock, true);
1012 size_t freed = slab_reclaim(0);
1013 irq_spinlock_lock(&zones.lock, true);
1014
1015 if (freed > 0)
1016 znum = find_free_zone(order,
1017 FRAME_TO_ZONE_FLAGS(flags), hint);
1018
1019 if (znum == (size_t) -1) {
1020 irq_spinlock_unlock(&zones.lock, true);
1021 freed = slab_reclaim(SLAB_RECLAIM_ALL);
1022 irq_spinlock_lock(&zones.lock, true);
1023
1024 if (freed > 0)
1025 znum = find_free_zone(order,
1026 FRAME_TO_ZONE_FLAGS(flags), hint);
1027 }
1028 }
1029
1030 if (znum == (size_t) -1) {
1031 if (flags & FRAME_ATOMIC) {
1032 irq_spinlock_unlock(&zones.lock, true);
1033 return NULL;
1034 }
1035
1036#ifdef CONFIG_DEBUG
1037 size_t avail = total_frames_free();
1038#endif
1039
1040 irq_spinlock_unlock(&zones.lock, true);
1041
1042 if (!THREAD)
1043 panic("Cannot wait for memory to become available.");
1044
1045 /*
1046 * Sleep until some frames are available again.
1047 */
1048
1049#ifdef CONFIG_DEBUG
1050 printf("Thread %" PRIu64 " waiting for %zu frames, "
1051 "%zu available.\n", THREAD->tid, size, avail);
1052#endif
1053
1054 mutex_lock(&mem_avail_mtx);
1055
1056 if (mem_avail_req > 0)
1057 mem_avail_req = min(mem_avail_req, size);
1058 else
1059 mem_avail_req = size;
1060 size_t gen = mem_avail_gen;
1061
1062 while (gen == mem_avail_gen)
1063 condvar_wait(&mem_avail_cv, &mem_avail_mtx);
1064
1065 mutex_unlock(&mem_avail_mtx);
1066
1067#ifdef CONFIG_DEBUG
1068 printf("Thread %" PRIu64 " woken up.\n", THREAD->tid);
1069#endif
1070
1071 goto loop;
1072 }
1073
1074 pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
1075 + zones.info[znum].base;
1076
1077 irq_spinlock_unlock(&zones.lock, true);
1078
1079 if (pzone)
1080 *pzone = znum;
1081
1082 if (flags & FRAME_KA)
1083 return (void *) PA2KA(PFN2ADDR(pfn));
1084
1085 return (void *) PFN2ADDR(pfn);
1086}
1087
1088/** Free a frame.
1089 *
1090 * Find respective frame structure for supplied physical frame address.
1091 * Decrement frame reference count. If it drops to zero, move the frame
1092 * structure to free list.
1093 *
1094 * @param frame Physical Address of of the frame to be freed.
1095 *
1096 */
1097void frame_free(uintptr_t frame)
1098{
1099 irq_spinlock_lock(&zones.lock, true);
1100
1101 /*
1102 * First, find host frame zone for addr.
1103 */
1104 pfn_t pfn = ADDR2PFN(frame);
1105 size_t znum = find_zone(pfn, 1, 0);
1106
1107 ASSERT(znum != (size_t) -1);
1108
1109 zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
1110
1111 irq_spinlock_unlock(&zones.lock, true);
1112
1113 /*
1114 * Signal that some memory has been freed.
1115 */
1116 mutex_lock(&mem_avail_mtx);
1117 if (mem_avail_req > 0)
1118 mem_avail_req--;
1119
1120 if (mem_avail_req == 0) {
1121 mem_avail_gen++;
1122 condvar_broadcast(&mem_avail_cv);
1123 }
1124 mutex_unlock(&mem_avail_mtx);
1125}
1126
1127/** Add reference to frame.
1128 *
1129 * Find respective frame structure for supplied PFN and
1130 * increment frame reference count.
1131 *
1132 * @param pfn Frame number of the frame to be freed.
1133 *
1134 */
1135NO_TRACE void frame_reference_add(pfn_t pfn)
1136{
1137 irq_spinlock_lock(&zones.lock, true);
1138
1139 /*
1140 * First, find host frame zone for addr.
1141 */
1142 size_t znum = find_zone(pfn, 1, 0);
1143
1144 ASSERT(znum != (size_t) -1);
1145
1146 zones.info[znum].frames[pfn - zones.info[znum].base].refcount++;
1147
1148 irq_spinlock_unlock(&zones.lock, true);
1149}
1150
1151/** Mark given range unavailable in frame zones.
1152 *
1153 */
1154NO_TRACE void frame_mark_unavailable(pfn_t start, size_t count)
1155{
1156 irq_spinlock_lock(&zones.lock, true);
1157
1158 size_t i;
1159 for (i = 0; i < count; i++) {
1160 size_t znum = find_zone(start + i, 1, 0);
1161 if (znum == (size_t) -1) /* PFN not found */
1162 continue;
1163
1164 zone_mark_unavailable(&zones.info[znum],
1165 start + i - zones.info[znum].base);
1166 }
1167
1168 irq_spinlock_unlock(&zones.lock, true);
1169}
1170
1171/** Initialize physical memory management.
1172 *
1173 */
1174void frame_init(void)
1175{
1176 if (config.cpu_active == 1) {
1177 zones.count = 0;
1178 irq_spinlock_initialize(&zones.lock, "frame.zones.lock");
1179 mutex_initialize(&mem_avail_mtx, MUTEX_ACTIVE);
1180 condvar_initialize(&mem_avail_cv);
1181 }
1182
1183 /* Tell the architecture to create some memory */
1184 frame_arch_init();
1185 if (config.cpu_active == 1) {
1186 frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
1187 SIZE2FRAMES(config.kernel_size));
1188 frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)),
1189 SIZE2FRAMES(config.stack_size));
1190
1191 size_t i;
1192 for (i = 0; i < init.cnt; i++) {
1193 pfn_t pfn = ADDR2PFN(KA2PA(init.tasks[i].addr));
1194 frame_mark_unavailable(pfn,
1195 SIZE2FRAMES(init.tasks[i].size));
1196 }
1197
1198 if (ballocs.size)
1199 frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)),
1200 SIZE2FRAMES(ballocs.size));
1201
1202 /* Black list first frame, as allocating NULL would
1203 * fail in some places
1204 */
1205 frame_mark_unavailable(0, 1);
1206 }
1207}
1208
1209/** Return total size of all zones.
1210 *
1211 */
1212uint64_t zones_total_size(void)
1213{
1214 irq_spinlock_lock(&zones.lock, true);
1215
1216 uint64_t total = 0;
1217 size_t i;
1218 for (i = 0; i < zones.count; i++)
1219 total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1220
1221 irq_spinlock_unlock(&zones.lock, true);
1222
1223 return total;
1224}
1225
1226void zones_stats(uint64_t *total, uint64_t *unavail, uint64_t *busy,
1227 uint64_t *free)
1228{
1229 ASSERT(total != NULL);
1230 ASSERT(unavail != NULL);
1231 ASSERT(busy != NULL);
1232 ASSERT(free != NULL);
1233
1234 irq_spinlock_lock(&zones.lock, true);
1235
1236 *total = 0;
1237 *unavail = 0;
1238 *busy = 0;
1239 *free = 0;
1240
1241 size_t i;
1242 for (i = 0; i < zones.count; i++) {
1243 *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1244
1245 if (zone_flags_available(zones.info[i].flags)) {
1246 *busy += (uint64_t) FRAMES2SIZE(zones.info[i].busy_count);
1247 *free += (uint64_t) FRAMES2SIZE(zones.info[i].free_count);
1248 } else
1249 *unavail += (uint64_t) FRAMES2SIZE(zones.info[i].count);
1250 }
1251
1252 irq_spinlock_unlock(&zones.lock, true);
1253}
1254
1255/** Prints list of zones.
1256 *
1257 */
1258void zones_print_list(void)
1259{
1260#ifdef __32_BITS__
1261 printf("[nr] [base addr] [frames ] [flags ] [free frames ] [busy frames ]\n");
1262#endif
1263
1264#ifdef __64_BITS__
1265 printf("[nr] [base address ] [frames ] [flags ] [free frames ] [busy frames ]\n");
1266#endif
1267
1268 /*
1269 * Because printing may require allocation of memory, we may not hold
1270 * the frame allocator locks when printing zone statistics. Therefore,
1271 * we simply gather the statistics under the protection of the locks and
1272 * print the statistics when the locks have been released.
1273 *
1274 * When someone adds/removes zones while we are printing the statistics,
1275 * we may end up with inaccurate output (e.g. a zone being skipped from
1276 * the listing).
1277 */
1278
1279 size_t i;
1280 for (i = 0;; i++) {
1281 irq_spinlock_lock(&zones.lock, true);
1282
1283 if (i >= zones.count) {
1284 irq_spinlock_unlock(&zones.lock, true);
1285 break;
1286 }
1287
1288 uintptr_t base = PFN2ADDR(zones.info[i].base);
1289 size_t count = zones.info[i].count;
1290 zone_flags_t flags = zones.info[i].flags;
1291 size_t free_count = zones.info[i].free_count;
1292 size_t busy_count = zones.info[i].busy_count;
1293
1294 irq_spinlock_unlock(&zones.lock, true);
1295
1296 bool available = zone_flags_available(flags);
1297
1298 printf("%-4zu", i);
1299
1300#ifdef __32_BITS__
1301 printf(" %p", (void *) base);
1302#endif
1303
1304#ifdef __64_BITS__
1305 printf(" %p", (void *) base);
1306#endif
1307
1308 printf(" %12zu %c%c%c ", count,
1309 available ? 'A' : ' ',
1310 (flags & ZONE_RESERVED) ? 'R' : ' ',
1311 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1312
1313 if (available)
1314 printf("%14zu %14zu",
1315 free_count, busy_count);
1316
1317 printf("\n");
1318 }
1319}
1320
1321/** Prints zone details.
1322 *
1323 * @param num Zone base address or zone number.
1324 *
1325 */
1326void zone_print_one(size_t num)
1327{
1328 irq_spinlock_lock(&zones.lock, true);
1329 size_t znum = (size_t) -1;
1330
1331 size_t i;
1332 for (i = 0; i < zones.count; i++) {
1333 if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
1334 znum = i;
1335 break;
1336 }
1337 }
1338
1339 if (znum == (size_t) -1) {
1340 irq_spinlock_unlock(&zones.lock, true);
1341 printf("Zone not found.\n");
1342 return;
1343 }
1344
1345 uintptr_t base = PFN2ADDR(zones.info[i].base);
1346 zone_flags_t flags = zones.info[i].flags;
1347 size_t count = zones.info[i].count;
1348 size_t free_count = zones.info[i].free_count;
1349 size_t busy_count = zones.info[i].busy_count;
1350
1351 irq_spinlock_unlock(&zones.lock, true);
1352
1353 bool available = zone_flags_available(flags);
1354
1355 printf("Zone number: %zu\n", znum);
1356 printf("Zone base address: %p\n", (void *) base);
1357 printf("Zone size: %zu frames (%zu KiB)\n", count,
1358 SIZE2KB(FRAMES2SIZE(count)));
1359 printf("Zone flags: %c%c%c\n",
1360 available ? 'A' : ' ',
1361 (flags & ZONE_RESERVED) ? 'R' : ' ',
1362 (flags & ZONE_FIRMWARE) ? 'F' : ' ');
1363
1364 if (available) {
1365 printf("Allocated space: %zu frames (%zu KiB)\n",
1366 busy_count, SIZE2KB(FRAMES2SIZE(busy_count)));
1367 printf("Available space: %zu frames (%zu KiB)\n",
1368 free_count, SIZE2KB(FRAMES2SIZE(free_count)));
1369 }
1370}
1371
1372/** @}
1373 */
Note: See TracBrowser for help on using the repository browser.