[f761f1eb] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <arch/types.h>
|
---|
| 30 | #include <func.h>
|
---|
| 31 |
|
---|
| 32 | #include <mm/heap.h>
|
---|
| 33 | #include <mm/frame.h>
|
---|
| 34 | #include <mm/page.h>
|
---|
| 35 | #include <mm/vm.h>
|
---|
| 36 | #include <arch/mm/page.h>
|
---|
| 37 |
|
---|
| 38 | #include <config.h>
|
---|
| 39 | #include <memstr.h>
|
---|
| 40 |
|
---|
| 41 | #include <panic.h>
|
---|
| 42 |
|
---|
| 43 | #include <synch/spinlock.h>
|
---|
| 44 |
|
---|
[18e0a6c] | 45 | #include <arch/asm.h>
|
---|
[9c0a9b3] | 46 | #include <arch.h>
|
---|
[18e0a6c] | 47 |
|
---|
[2b50d7c] | 48 | count_t frames = 0;
|
---|
| 49 | count_t frames_free;
|
---|
[f761f1eb] | 50 |
|
---|
| 51 | __u8 *frame_bitmap;
|
---|
[2b50d7c] | 52 | count_t frame_bitmap_octets;
|
---|
[f761f1eb] | 53 |
|
---|
| 54 | static spinlock_t framelock;
|
---|
| 55 |
|
---|
| 56 | void frame_init(void)
|
---|
| 57 | {
|
---|
[76cec1e] | 58 | if (config.cpu_active == 1) {
|
---|
| 59 |
|
---|
| 60 | /*
|
---|
| 61 | * The bootstrap processor will allocate all necessary memory for frame allocation.
|
---|
| 62 | */
|
---|
| 63 |
|
---|
| 64 | frames = config.memory_size / FRAME_SIZE;
|
---|
| 65 | frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
|
---|
| 66 | frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
|
---|
| 67 | if (!frame_bitmap)
|
---|
| 68 | panic("malloc/frame_bitmap\n");
|
---|
| 69 |
|
---|
| 70 | /*
|
---|
| 71 | * Mark all frames free.
|
---|
| 72 | */
|
---|
| 73 | memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
|
---|
| 74 | frames_free = frames;
|
---|
[f761f1eb] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /*
|
---|
[a7a10630] | 78 | * No frame allocations/reservations prior this point.
|
---|
[f761f1eb] | 79 | */
|
---|
| 80 |
|
---|
| 81 | frame_arch_init();
|
---|
| 82 |
|
---|
| 83 | if (config.cpu_active == 1) {
|
---|
[76cec1e] | 84 | /*
|
---|
| 85 | * Create the memory address space map. Marked frames and frame
|
---|
| 86 | * regions cannot be used for allocation.
|
---|
| 87 | */
|
---|
[f761f1eb] | 88 | frame_region_not_free(config.base, config.base + config.kernel_size);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | * Allocate a frame.
|
---|
| 94 | */
|
---|
| 95 | __address frame_alloc(int flags)
|
---|
| 96 | {
|
---|
| 97 | int i;
|
---|
| 98 | pri_t pri;
|
---|
| 99 |
|
---|
| 100 | loop:
|
---|
| 101 | pri = cpu_priority_high();
|
---|
| 102 | spinlock_lock(&framelock);
|
---|
[a7a10630] | 103 | if (frames_free) {
|
---|
| 104 | for (i=0; i < frames; i++) {
|
---|
[f761f1eb] | 105 | int m, n;
|
---|
| 106 |
|
---|
| 107 | m = i / 8;
|
---|
| 108 | n = i % 8;
|
---|
| 109 |
|
---|
[a7a10630] | 110 | if ((frame_bitmap[m] & (1<<n)) == 0) {
|
---|
| 111 | frame_bitmap[m] |= (1<<n);
|
---|
| 112 | frames_free--;
|
---|
[f761f1eb] | 113 | spinlock_unlock(&framelock);
|
---|
| 114 | cpu_priority_restore(pri);
|
---|
| 115 | if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
|
---|
| 116 | return i*FRAME_SIZE;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
[02a99d2] | 119 | panic("frames_free inconsistent (%d)\n", frames_free);
|
---|
[f761f1eb] | 120 | }
|
---|
| 121 | spinlock_unlock(&framelock);
|
---|
| 122 | cpu_priority_restore(pri);
|
---|
| 123 |
|
---|
| 124 | if (flags & FRAME_PANIC)
|
---|
[02a99d2] | 125 | panic("unable to allocate frame\n");
|
---|
[f761f1eb] | 126 |
|
---|
| 127 | /* TODO: implement sleeping logic here */
|
---|
[02a99d2] | 128 | panic("sleep not supported\n");
|
---|
[f761f1eb] | 129 |
|
---|
| 130 | goto loop;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * Free a frame.
|
---|
| 135 | */
|
---|
| 136 | void frame_free(__address addr)
|
---|
| 137 | {
|
---|
| 138 | pri_t pri;
|
---|
| 139 | __u32 frame;
|
---|
| 140 |
|
---|
| 141 | pri = cpu_priority_high();
|
---|
| 142 | spinlock_lock(&framelock);
|
---|
| 143 |
|
---|
| 144 | frame = IS_KA(addr) ? KA2PA(addr) : addr;
|
---|
| 145 | frame /= FRAME_SIZE;
|
---|
[a7a10630] | 146 | if (frame < frames) {
|
---|
[f761f1eb] | 147 | int m, n;
|
---|
| 148 |
|
---|
| 149 | m = frame / 8;
|
---|
| 150 | n = frame % 8;
|
---|
| 151 |
|
---|
[a7a10630] | 152 | if (frame_bitmap[m] & (1<<n)) {
|
---|
| 153 | frame_bitmap[m] &= ~(1<<n);
|
---|
| 154 | frames_free++;
|
---|
[f761f1eb] | 155 | }
|
---|
[2b50d7c] | 156 | else panic("frame already free\n");
|
---|
[f761f1eb] | 157 | }
|
---|
[2b50d7c] | 158 | else panic("frame number too big\n");
|
---|
[f761f1eb] | 159 |
|
---|
| 160 | spinlock_unlock(&framelock);
|
---|
| 161 | cpu_priority_restore(pri);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /*
|
---|
| 165 | * Don't use this function for normal allocation. Use frame_alloc() instead.
|
---|
| 166 | * Use this function to declare that some special frame is not free.
|
---|
| 167 | */
|
---|
| 168 | void frame_not_free(__address addr)
|
---|
| 169 | {
|
---|
| 170 | pri_t pri;
|
---|
| 171 | __u32 frame;
|
---|
| 172 |
|
---|
| 173 | pri = cpu_priority_high();
|
---|
| 174 | spinlock_lock(&framelock);
|
---|
| 175 | frame = IS_KA(addr) ? KA2PA(addr) : addr;
|
---|
| 176 | frame /= FRAME_SIZE;
|
---|
[a7a10630] | 177 | if (frame < frames) {
|
---|
[f761f1eb] | 178 | int m, n;
|
---|
| 179 |
|
---|
| 180 | m = frame / 8;
|
---|
| 181 | n = frame % 8;
|
---|
| 182 |
|
---|
[a7a10630] | 183 | if ((frame_bitmap[m] & (1<<n)) == 0) {
|
---|
| 184 | frame_bitmap[m] |= (1<<n);
|
---|
| 185 | frames_free--;
|
---|
[f761f1eb] | 186 | }
|
---|
| 187 | }
|
---|
| 188 | spinlock_unlock(&framelock);
|
---|
| 189 | cpu_priority_restore(pri);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void frame_region_not_free(__address start, __address stop)
|
---|
| 193 | {
|
---|
[d9f81af3] | 194 | __address a;
|
---|
[f761f1eb] | 195 |
|
---|
| 196 | start /= FRAME_SIZE;
|
---|
| 197 | stop /= FRAME_SIZE;
|
---|
[d9f81af3] | 198 | for (a = start; a <= stop; a++)
|
---|
| 199 | frame_not_free(a * FRAME_SIZE);
|
---|
[f761f1eb] | 200 | }
|
---|