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