Rev 1 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 68 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <arch/types.h> |
29 | #include <arch/types.h> |
30 | #include <func.h> |
30 | #include <func.h> |
31 | 31 | ||
32 | #include <mm/heap.h> |
32 | #include <mm/heap.h> |
33 | #include <mm/frame.h> |
33 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
34 | #include <mm/page.h> |
35 | #include <mm/vm.h> |
35 | #include <mm/vm.h> |
36 | #include <arch/mm/page.h> |
36 | #include <arch/mm/page.h> |
37 | 37 | ||
38 | #include <config.h> |
38 | #include <config.h> |
39 | #include <memstr.h> |
39 | #include <memstr.h> |
40 | 40 | ||
41 | #include <panic.h> |
41 | #include <panic.h> |
42 | 42 | ||
43 | #include <synch/spinlock.h> |
43 | #include <synch/spinlock.h> |
44 | 44 | ||
45 | __u32 frames; |
45 | __u32 frames; |
46 | __u32 frames_free; |
46 | __u32 frames_free; |
47 | 47 | ||
48 | __u8 *frame_bitmap; |
48 | __u8 *frame_bitmap; |
49 | __u32 frame_bitmap_octets; |
49 | __u32 frame_bitmap_octets; |
50 | 50 | ||
51 | /* |
51 | /* |
52 | * This is for kernel address space frames (allocated with FRAME_KA). |
52 | * This is for kernel address space frames (allocated with FRAME_KA). |
53 | * Their addresses may not interfere with user address space. |
53 | * Their addresses may not interfere with user address space. |
54 | */ |
54 | */ |
55 | __u8 *frame_kernel_bitmap; |
55 | __u8 *frame_kernel_bitmap; |
56 | __u32 kernel_frames; |
56 | __u32 kernel_frames; |
57 | __u32 kernel_frames_free; |
57 | __u32 kernel_frames_free; |
58 | 58 | ||
59 | static spinlock_t framelock; |
59 | static spinlock_t framelock; |
60 | 60 | ||
61 | void frame_init(void) |
61 | void frame_init(void) |
62 | { |
62 | { |
63 | if (config.cpu_active == 1) { |
63 | if (config.cpu_active == 1) { |
64 | 64 | ||
65 | /* |
65 | /* |
66 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
66 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
67 | */ |
67 | */ |
68 | 68 | ||
69 | frames = config.memory_size / FRAME_SIZE; |
69 | frames = config.memory_size / FRAME_SIZE; |
70 | frame_bitmap_octets = frames / 8 + (frames % 8 > 0); |
70 | frame_bitmap_octets = frames / 8 + (frames % 8 > 0); |
71 | 71 | ||
72 | frame_bitmap = (__u8 *) malloc(frame_bitmap_octets); |
72 | frame_bitmap = (__u8 *) malloc(frame_bitmap_octets); |
73 | if (!frame_bitmap) |
73 | if (!frame_bitmap) |
74 | panic(PANIC "malloc/frame_bitmap\n"); |
74 | panic("malloc/frame_bitmap\n"); |
75 | 75 | ||
76 | /* |
76 | /* |
77 | * Mark all frames free. |
77 | * Mark all frames free. |
78 | */ |
78 | */ |
79 | memsetb((__address) frame_bitmap, frame_bitmap_octets, 0); |
79 | memsetb((__address) frame_bitmap, frame_bitmap_octets, 0); |
80 | frames_free = frames; |
80 | frames_free = frames; |
81 | 81 | ||
82 | /* |
82 | /* |
83 | * Will be properly set up by architecture dependent frame init. |
83 | * Will be properly set up by architecture dependent frame init. |
84 | */ |
84 | */ |
85 | frame_kernel_bitmap = NULL; |
85 | frame_kernel_bitmap = NULL; |
86 | kernel_frames_free = 0; |
86 | kernel_frames_free = 0; |
87 | kernel_frames = 0; |
87 | kernel_frames = 0; |
88 | } |
88 | } |
89 | 89 | ||
90 | /* |
90 | /* |
91 | * No frame allocations/reservations prior this point. |
91 | * No frame allocations/reservations prior this point. |
92 | */ |
92 | */ |
93 | 93 | ||
94 | frame_arch_init(); |
94 | frame_arch_init(); |
95 | 95 | ||
96 | if (config.cpu_active == 1) { |
96 | if (config.cpu_active == 1) { |
97 | /* |
97 | /* |
98 | * Create the memory address space map. Marked frames and frame |
98 | * Create the memory address space map. Marked frames and frame |
99 | * regions cannot be used for allocation. |
99 | * regions cannot be used for allocation. |
100 | */ |
100 | */ |
101 | frame_region_not_free(config.base, config.base + config.kernel_size); |
101 | frame_region_not_free(config.base, config.base + config.kernel_size); |
102 | } |
102 | } |
103 | } |
103 | } |
104 | 104 | ||
105 | /* |
105 | /* |
106 | * Allocate a frame. |
106 | * Allocate a frame. |
107 | */ |
107 | */ |
108 | __address frame_alloc(int flags) |
108 | __address frame_alloc(int flags) |
109 | { |
109 | { |
110 | int i; |
110 | int i; |
111 | pri_t pri; |
111 | pri_t pri; |
112 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
112 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
113 | __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free; |
113 | __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free; |
114 | 114 | ||
115 | if (flags & FRAME_KA) { |
115 | if (flags & FRAME_KA) { |
116 | frame_bitmap_ptr = &frame_kernel_bitmap; |
116 | frame_bitmap_ptr = &frame_kernel_bitmap; |
117 | frames_ptr = &kernel_frames; |
117 | frames_ptr = &kernel_frames; |
118 | frames_free_ptr = &kernel_frames_free; |
118 | frames_free_ptr = &kernel_frames_free; |
119 | } |
119 | } |
120 | 120 | ||
121 | loop: |
121 | loop: |
122 | pri = cpu_priority_high(); |
122 | pri = cpu_priority_high(); |
123 | spinlock_lock(&framelock); |
123 | spinlock_lock(&framelock); |
124 | if (*frames_free_ptr) { |
124 | if (*frames_free_ptr) { |
125 | for (i=0; i < *frames_ptr; i++) { |
125 | for (i=0; i < *frames_ptr; i++) { |
126 | int m, n; |
126 | int m, n; |
127 | 127 | ||
128 | m = i / 8; |
128 | m = i / 8; |
129 | n = i % 8; |
129 | n = i % 8; |
130 | 130 | ||
131 | if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) { |
131 | if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) { |
132 | (*frame_bitmap_ptr)[m] |= (1<<n); |
132 | (*frame_bitmap_ptr)[m] |= (1<<n); |
133 | *frames_free_ptr--; |
133 | *frames_free_ptr--; |
134 | if (flags & FRAME_KA) { |
134 | if (flags & FRAME_KA) { |
135 | /* |
135 | /* |
136 | * frames_free_ptr points to kernel_frames_free |
136 | * frames_free_ptr points to kernel_frames_free |
137 | * It is still necessary to decrement frames_free. |
137 | * It is still necessary to decrement frames_free. |
138 | */ |
138 | */ |
139 | frames_free--; |
139 | frames_free--; |
140 | } |
140 | } |
141 | spinlock_unlock(&framelock); |
141 | spinlock_unlock(&framelock); |
142 | cpu_priority_restore(pri); |
142 | cpu_priority_restore(pri); |
143 | if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE); |
143 | if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE); |
144 | return i*FRAME_SIZE; |
144 | return i*FRAME_SIZE; |
145 | } |
145 | } |
146 | } |
146 | } |
147 | panic(PANIC "frames_free inconsistent (%d)\n", frames_free); |
147 | panic("frames_free inconsistent (%d)\n", frames_free); |
148 | } |
148 | } |
149 | spinlock_unlock(&framelock); |
149 | spinlock_unlock(&framelock); |
150 | cpu_priority_restore(pri); |
150 | cpu_priority_restore(pri); |
151 | 151 | ||
152 | if (flags & FRAME_PANIC) |
152 | if (flags & FRAME_PANIC) |
153 | panic(PANIC "unable to allocate frame\n"); |
153 | panic("unable to allocate frame\n"); |
154 | 154 | ||
155 | /* TODO: implement sleeping logic here */ |
155 | /* TODO: implement sleeping logic here */ |
156 | panic(PANIC "sleep not supported\n"); |
156 | panic("sleep not supported\n"); |
157 | 157 | ||
158 | goto loop; |
158 | goto loop; |
159 | } |
159 | } |
160 | 160 | ||
161 | /* |
161 | /* |
162 | * Free a frame. |
162 | * Free a frame. |
163 | */ |
163 | */ |
164 | void frame_free(__address addr) |
164 | void frame_free(__address addr) |
165 | { |
165 | { |
166 | pri_t pri; |
166 | pri_t pri; |
167 | __u32 frame; |
167 | __u32 frame; |
168 | __u32 *frames_free_ptr = &frames_free, *frames_ptr = &frames; |
168 | __u32 *frames_free_ptr = &frames_free, *frames_ptr = &frames; |
169 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
169 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
170 | 170 | ||
171 | if (IS_KA(addr)) { |
171 | if (IS_KA(addr)) { |
172 | frames_free_ptr = &kernel_frames_free; |
172 | frames_free_ptr = &kernel_frames_free; |
173 | frame_bitmap_ptr = &frame_kernel_bitmap; |
173 | frame_bitmap_ptr = &frame_kernel_bitmap; |
174 | } |
174 | } |
175 | 175 | ||
176 | pri = cpu_priority_high(); |
176 | pri = cpu_priority_high(); |
177 | spinlock_lock(&framelock); |
177 | spinlock_lock(&framelock); |
178 | 178 | ||
179 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
179 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
180 | frame /= FRAME_SIZE; |
180 | frame /= FRAME_SIZE; |
181 | if (frame < *frames_ptr) { |
181 | if (frame < *frames_ptr) { |
182 | int m, n; |
182 | int m, n; |
183 | 183 | ||
184 | m = frame / 8; |
184 | m = frame / 8; |
185 | n = frame % 8; |
185 | n = frame % 8; |
186 | 186 | ||
187 | if ((*frame_bitmap_ptr)[m] & (1<<n)) { |
187 | if ((*frame_bitmap_ptr)[m] & (1<<n)) { |
188 | (*frame_bitmap_ptr)[m] &= ~(1<<n); |
188 | (*frame_bitmap_ptr)[m] &= ~(1<<n); |
189 | *frames_free_ptr++; |
189 | *frames_free_ptr++; |
190 | if (IS_KA(addr)) { |
190 | if (IS_KA(addr)) { |
191 | /* |
191 | /* |
192 | * frames_free_ptr points to kernel_frames_free |
192 | * frames_free_ptr points to kernel_frames_free |
193 | * It is still necessary to increment frames_free. |
193 | * It is still necessary to increment frames_free. |
194 | */ |
194 | */ |
195 | frames_free++; |
195 | frames_free++; |
196 | } |
196 | } |
197 | } |
197 | } |
198 | else panic(PANIC "frame_free: frame already free\n"); |
198 | else panic("frame_free: frame already free\n"); |
199 | } |
199 | } |
200 | else panic(PANIC "frame_free: frame number too big\n"); |
200 | else panic("frame_free: frame number too big\n"); |
201 | 201 | ||
202 | spinlock_unlock(&framelock); |
202 | spinlock_unlock(&framelock); |
203 | cpu_priority_restore(pri); |
203 | cpu_priority_restore(pri); |
204 | } |
204 | } |
205 | 205 | ||
206 | /* |
206 | /* |
207 | * Don't use this function for normal allocation. Use frame_alloc() instead. |
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. |
208 | * Use this function to declare that some special frame is not free. |
209 | */ |
209 | */ |
210 | void frame_not_free(__address addr) |
210 | void frame_not_free(__address addr) |
211 | { |
211 | { |
212 | pri_t pri; |
212 | pri_t pri; |
213 | __u32 frame; |
213 | __u32 frame; |
214 | __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free; |
214 | __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free; |
215 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
215 | __u8 **frame_bitmap_ptr = &frame_bitmap; |
216 | 216 | ||
217 | pri = cpu_priority_high(); |
217 | pri = cpu_priority_high(); |
218 | spinlock_lock(&framelock); |
218 | spinlock_lock(&framelock); |
219 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
219 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
220 | frame /= FRAME_SIZE; |
220 | frame /= FRAME_SIZE; |
221 | if (frame < *frames_ptr) { |
221 | if (frame < *frames_ptr) { |
222 | int m, n; |
222 | int m, n; |
223 | 223 | ||
224 | m = frame / 8; |
224 | m = frame / 8; |
225 | n = frame % 8; |
225 | n = frame % 8; |
226 | 226 | ||
227 | if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) { |
227 | if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) { |
228 | (*frame_bitmap_ptr)[m] |= (1<<n); |
228 | (*frame_bitmap_ptr)[m] |= (1<<n); |
229 | *frames_free_ptr--; |
229 | *frames_free_ptr--; |
230 | if (IS_KA(addr)) { |
230 | if (IS_KA(addr)) { |
231 | /* |
231 | /* |
232 | * frames_free_ptr points to kernel_frames_free |
232 | * frames_free_ptr points to kernel_frames_free |
233 | * It is still necessary to decrement frames_free. |
233 | * It is still necessary to decrement frames_free. |
234 | */ |
234 | */ |
235 | frames_free--; |
235 | frames_free--; |
236 | } |
236 | } |
237 | } |
237 | } |
238 | } |
238 | } |
239 | spinlock_unlock(&framelock); |
239 | spinlock_unlock(&framelock); |
240 | cpu_priority_restore(pri); |
240 | cpu_priority_restore(pri); |
241 | } |
241 | } |
242 | 242 | ||
243 | void frame_region_not_free(__address start, __address stop) |
243 | void frame_region_not_free(__address start, __address stop) |
244 | { |
244 | { |
245 | __u32 i; |
245 | __u32 i; |
246 | 246 | ||
247 | start /= FRAME_SIZE; |
247 | start /= FRAME_SIZE; |
248 | stop /= FRAME_SIZE; |
248 | stop /= FRAME_SIZE; |
249 | for (i = start; i <= stop; i++) |
249 | for (i = start; i <= stop; i++) |
250 | frame_not_free(i * FRAME_SIZE); |
250 | frame_not_free(i * FRAME_SIZE); |
251 | } |
251 | } |
252 | 252 |