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

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

Feed memory from the newly created zone into the reservation system.

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