source: mainline/src/mm/frame.c@ f761f1eb

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

Initial import

  • Property mode set to 100644
File size: 6.4 KB
Line 
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
45__u32 frames;
46__u32 frames_free;
47
48__u8 *frame_bitmap;
49__u32 frame_bitmap_octets;
50
51/*
52 * This is for kernel address space frames (allocated with FRAME_KA).
53 * Their addresses may not interfere with user address space.
54 */
55__u8 *frame_kernel_bitmap;
56__u32 kernel_frames;
57__u32 kernel_frames_free;
58
59static spinlock_t framelock;
60
61void frame_init(void)
62{
63 if (config.cpu_active == 1) {
64
65 /*
66 * The bootstrap processor will allocate all necessary memory for frame allocation.
67 */
68
69 frames = config.memory_size / FRAME_SIZE;
70 frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
71
72 frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
73 if (!frame_bitmap)
74 panic(PANIC "malloc/frame_bitmap\n");
75
76 /*
77 * Mark all frames free.
78 */
79 memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
80 frames_free = frames;
81
82 /*
83 * Will be properly set up by architecture dependent frame init.
84 */
85 frame_kernel_bitmap = NULL;
86 kernel_frames_free = 0;
87 kernel_frames = 0;
88 }
89
90 /*
91 * No frame allocations/reservations prior this point.
92 */
93
94 frame_arch_init();
95
96 if (config.cpu_active == 1) {
97 /*
98 * Create the memory address space map. Marked frames and frame
99 * regions cannot be used for allocation.
100 */
101 frame_region_not_free(config.base, config.base + config.kernel_size);
102 }
103}
104
105/*
106 * Allocate a frame.
107 */
108__address frame_alloc(int flags)
109{
110 int i;
111 pri_t pri;
112 __u8 **frame_bitmap_ptr = &frame_bitmap;
113 __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free;
114
115 if (flags & FRAME_KA) {
116 frame_bitmap_ptr = &frame_kernel_bitmap;
117 frames_ptr = &kernel_frames;
118 frames_free_ptr = &kernel_frames_free;
119 }
120
121loop:
122 pri = cpu_priority_high();
123 spinlock_lock(&framelock);
124 if (*frames_free_ptr) {
125 for (i=0; i < *frames_ptr; i++) {
126 int m, n;
127
128 m = i / 8;
129 n = i % 8;
130
131 if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {
132 (*frame_bitmap_ptr)[m] |= (1<<n);
133 *frames_free_ptr--;
134 if (flags & FRAME_KA) {
135 /*
136 * frames_free_ptr points to kernel_frames_free
137 * It is still necessary to decrement frames_free.
138 */
139 frames_free--;
140 }
141 spinlock_unlock(&framelock);
142 cpu_priority_restore(pri);
143 if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
144 return i*FRAME_SIZE;
145 }
146 }
147 panic(PANIC "frames_free inconsistent (%d)\n", frames_free);
148 }
149 spinlock_unlock(&framelock);
150 cpu_priority_restore(pri);
151
152 if (flags & FRAME_PANIC)
153 panic(PANIC "unable to allocate frame\n");
154
155 /* TODO: implement sleeping logic here */
156 panic(PANIC "sleep not supported\n");
157
158 goto loop;
159}
160
161/*
162 * Free a frame.
163 */
164void frame_free(__address addr)
165{
166 pri_t pri;
167 __u32 frame;
168 __u32 *frames_free_ptr = &frames_free, *frames_ptr = &frames;
169 __u8 **frame_bitmap_ptr = &frame_bitmap;
170
171 if (IS_KA(addr)) {
172 frames_free_ptr = &kernel_frames_free;
173 frame_bitmap_ptr = &frame_kernel_bitmap;
174 }
175
176 pri = cpu_priority_high();
177 spinlock_lock(&framelock);
178
179 frame = IS_KA(addr) ? KA2PA(addr) : addr;
180 frame /= FRAME_SIZE;
181 if (frame < *frames_ptr) {
182 int m, n;
183
184 m = frame / 8;
185 n = frame % 8;
186
187 if ((*frame_bitmap_ptr)[m] & (1<<n)) {
188 (*frame_bitmap_ptr)[m] &= ~(1<<n);
189 *frames_free_ptr++;
190 if (IS_KA(addr)) {
191 /*
192 * frames_free_ptr points to kernel_frames_free
193 * It is still necessary to increment frames_free.
194 */
195 frames_free++;
196 }
197 }
198 else panic(PANIC "frame_free: frame already free\n");
199 }
200 else panic(PANIC "frame_free: frame number too big\n");
201
202 spinlock_unlock(&framelock);
203 cpu_priority_restore(pri);
204}
205
206/*
207 * Don't use this function for normal allocation. Use frame_alloc() instead.
208 * Use this function to declare that some special frame is not free.
209 */
210void frame_not_free(__address addr)
211{
212 pri_t pri;
213 __u32 frame;
214 __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free;
215 __u8 **frame_bitmap_ptr = &frame_bitmap;
216
217 pri = cpu_priority_high();
218 spinlock_lock(&framelock);
219 frame = IS_KA(addr) ? KA2PA(addr) : addr;
220 frame /= FRAME_SIZE;
221 if (frame < *frames_ptr) {
222 int m, n;
223
224 m = frame / 8;
225 n = frame % 8;
226
227 if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {
228 (*frame_bitmap_ptr)[m] |= (1<<n);
229 *frames_free_ptr--;
230 if (IS_KA(addr)) {
231 /*
232 * frames_free_ptr points to kernel_frames_free
233 * It is still necessary to decrement frames_free.
234 */
235 frames_free--;
236 }
237 }
238 }
239 spinlock_unlock(&framelock);
240 cpu_priority_restore(pri);
241}
242
243void frame_region_not_free(__address start, __address stop)
244{
245 __u32 i;
246
247 start /= FRAME_SIZE;
248 stop /= FRAME_SIZE;
249 for (i = start; i <= stop; i++)
250 frame_not_free(i * FRAME_SIZE);
251}
Note: See TracBrowser for help on using the repository browser.