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

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

avoid tracing of several memory-related functions which are called from tight loops
make the reporting of overlaping zones more informative

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