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

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

fix two possible NULL dereferences

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