Rev 430 | Rev 480 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 430 | Rev 479 | ||
---|---|---|---|
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 <typedefs.h> |
29 | #include <typedefs.h> |
30 | #include <arch/types.h> |
30 | #include <arch/types.h> |
31 | #include <mm/heap.h> |
31 | #include <mm/heap.h> |
32 | #include <mm/frame.h> |
32 | #include <mm/frame.h> |
33 | #include <mm/vm.h> |
33 | #include <mm/vm.h> |
34 | #include <panic.h> |
34 | #include <panic.h> |
35 | #include <debug.h> |
35 | #include <debug.h> |
36 | #include <list.h> |
36 | #include <list.h> |
37 | #include <synch/spinlock.h> |
37 | #include <synch/spinlock.h> |
38 | #include <arch/asm.h> |
38 | #include <arch/asm.h> |
39 | #include <arch.h> |
39 | #include <arch.h> |
40 | 40 | ||
41 | spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
41 | spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
42 | link_t zone_head; /**< list of all zones in the system */ |
42 | link_t zone_head; /**< list of all zones in the system */ |
43 | 43 | ||
- | 44 | static struct buddy_system_operations zone_buddy_system_operations = { |
|
- | 45 | .find_buddy = zone_buddy_find_buddy, |
|
- | 46 | .bisect = zone_buddy_bisect, |
|
- | 47 | .coalesce = zone_buddy_coalesce, |
|
- | 48 | .set_order = zone_buddy_set_order, |
|
- | 49 | .get_order = zone_buddy_get_order, |
|
- | 50 | }; |
|
- | 51 | ||
44 | /** Initialize physical memory management |
52 | /** Initialize physical memory management |
45 | * |
53 | * |
46 | * Initialize physical memory managemnt. |
54 | * Initialize physical memory managemnt. |
47 | */ |
55 | */ |
48 | void frame_init(void) |
56 | void frame_init(void) |
49 | { |
57 | { |
50 | if (config.cpu_active == 1) { |
58 | if (config.cpu_active == 1) { |
51 | zone_init(); |
59 | zone_init(); |
52 | } |
60 | } |
53 | 61 | ||
54 | frame_arch_init(); |
62 | frame_arch_init(); |
55 | 63 | ||
56 | if (config.cpu_active == 1) { |
64 | if (config.cpu_active == 1) { |
57 | frame_region_not_free(config.base, config.base + config.kernel_size + CONFIG_STACK_SIZE); |
65 | frame_region_not_free(config.base, config.base + config.kernel_size + CONFIG_STACK_SIZE); |
58 | } |
66 | } |
59 | } |
67 | } |
60 | 68 | ||
61 | /** Allocate a frame |
69 | /** Allocate a frame |
62 | * |
70 | * |
63 | * Allocate a frame of physical memory. |
71 | * Allocate a frame of physical memory. |
64 | * |
72 | * |
65 | * @param flags Flags for host zone selection and address processing. |
73 | * @param flags Flags for host zone selection and address processing. |
66 | * |
74 | * |
67 | * @return Allocated frame. |
75 | * @return Allocated frame. |
68 | */ |
76 | */ |
69 | __address frame_alloc(int flags) |
77 | __address frame_alloc(int flags) |
70 | { |
78 | { |
71 | ipl_t ipl; |
79 | ipl_t ipl; |
72 | link_t *cur, *tmp; |
80 | link_t *cur, *tmp; |
73 | zone_t *z; |
81 | zone_t *z; |
74 | zone_t *zone = NULL; |
82 | zone_t *zone = NULL; |
75 | frame_t *frame = NULL; |
83 | frame_t *frame = NULL; |
76 | __address v; |
84 | __address v; |
77 | 85 | ||
78 | loop: |
86 | loop: |
79 | ipl = interrupts_disable(); |
87 | ipl = interrupts_disable(); |
80 | spinlock_lock(&zone_head_lock); |
88 | spinlock_lock(&zone_head_lock); |
81 | 89 | ||
82 | /* |
90 | /* |
83 | * First, find suitable frame zone. |
91 | * First, find suitable frame zone. |
84 | */ |
92 | */ |
85 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
93 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
86 | z = list_get_instance(cur, zone_t, link); |
94 | z = list_get_instance(cur, zone_t, link); |
87 | 95 | ||
88 | spinlock_lock(&z->lock); |
96 | spinlock_lock(&z->lock); |
89 | /* |
97 | /* |
90 | * Check if the zone has any free frames. |
98 | * Check if the zone has any free frames. |
91 | */ |
99 | */ |
92 | if (z->free_count) { |
100 | if (z->free_count) { |
93 | zone = z; |
101 | zone = z; |
94 | break; |
102 | break; |
95 | } |
103 | } |
96 | spinlock_unlock(&z->lock); |
104 | spinlock_unlock(&z->lock); |
97 | } |
105 | } |
98 | 106 | ||
99 | if (!zone) { |
107 | if (!zone) { |
100 | if (flags & FRAME_PANIC) |
108 | if (flags & FRAME_PANIC) |
101 | panic("Can't allocate frame.\n"); |
109 | panic("Can't allocate frame.\n"); |
102 | 110 | ||
103 | /* |
111 | /* |
104 | * TODO: Sleep until frames are available again. |
112 | * TODO: Sleep until frames are available again. |
105 | */ |
113 | */ |
106 | spinlock_unlock(&zone_head_lock); |
114 | spinlock_unlock(&zone_head_lock); |
107 | interrupts_restore(ipl); |
115 | interrupts_restore(ipl); |
108 | 116 | ||
109 | panic("Sleep not implemented.\n"); |
117 | panic("Sleep not implemented.\n"); |
110 | goto loop; |
118 | goto loop; |
111 | } |
119 | } |
112 | 120 | ||
113 | tmp = zone->free_head.next; |
121 | tmp = zone->free_head.next; |
114 | frame = list_get_instance(tmp, frame_t, link); |
122 | frame = list_get_instance(tmp, frame_t, link); |
115 | 123 | ||
116 | frame->refcount++; |
124 | frame->refcount++; |
117 | list_remove(tmp); /* remove frame from free_head */ |
125 | list_remove(tmp); /* remove frame from free_head */ |
118 | zone->free_count--; |
126 | zone->free_count--; |
119 | zone->busy_count++; |
127 | zone->busy_count++; |
120 | 128 | ||
121 | v = zone->base + (frame - zone->frames) * FRAME_SIZE; |
129 | //v = zone->base + (frame - zone->frames) * FRAME_SIZE; |
- | 130 | v = FRAME2ADDR(zone, frame); |
|
122 | 131 | ||
123 | if (flags & FRAME_KA) |
132 | if (flags & FRAME_KA) |
124 | v = PA2KA(v); |
133 | v = PA2KA(v); |
125 | 134 | ||
126 | spinlock_unlock(&zone->lock); |
135 | spinlock_unlock(&zone->lock); |
127 | 136 | ||
128 | spinlock_unlock(&zone_head_lock); |
137 | spinlock_unlock(&zone_head_lock); |
129 | interrupts_restore(ipl); |
138 | interrupts_restore(ipl); |
130 | 139 | ||
131 | return v; |
140 | return v; |
132 | } |
141 | } |
133 | 142 | ||
134 | /** Free a frame. |
143 | /** Free a frame. |
135 | * |
144 | * |
136 | * Find respective frame structrue for supplied addr. |
145 | * Find respective frame structrue for supplied addr. |
137 | * Decrement frame reference count. |
146 | * Decrement frame reference count. |
138 | * If it drops to zero, move the frame structure to free list. |
147 | * If it drops to zero, move the frame structure to free list. |
139 | * |
148 | * |
140 | * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE. |
149 | * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE. |
141 | */ |
150 | */ |
142 | void frame_free(__address addr) |
151 | void frame_free(__address addr) |
143 | { |
152 | { |
144 | ipl_t ipl; |
153 | ipl_t ipl; |
145 | link_t *cur; |
154 | link_t *cur; |
146 | zone_t *z; |
155 | zone_t *z; |
147 | zone_t *zone = NULL; |
156 | zone_t *zone = NULL; |
148 | frame_t *frame; |
157 | frame_t *frame; |
149 | 158 | ||
150 | ASSERT(addr % FRAME_SIZE == 0); |
159 | ASSERT(addr % FRAME_SIZE == 0); |
151 | 160 | ||
152 | ipl = interrupts_disable(); |
161 | ipl = interrupts_disable(); |
153 | spinlock_lock(&zone_head_lock); |
162 | spinlock_lock(&zone_head_lock); |
154 | 163 | ||
155 | /* |
164 | /* |
156 | * First, find host frame zone for addr. |
165 | * First, find host frame zone for addr. |
157 | */ |
166 | */ |
158 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
167 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
159 | z = list_get_instance(cur, zone_t, link); |
168 | z = list_get_instance(cur, zone_t, link); |
160 | 169 | ||
161 | spinlock_lock(&z->lock); |
170 | spinlock_lock(&z->lock); |
162 | 171 | ||
163 | if (IS_KA(addr)) |
172 | if (IS_KA(addr)) |
164 | addr = KA2PA(addr); |
173 | addr = KA2PA(addr); |
165 | 174 | ||
166 | /* |
175 | /* |
167 | * Check if addr belongs to z. |
176 | * Check if addr belongs to z. |
168 | */ |
177 | */ |
169 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
178 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
170 | zone = z; |
179 | zone = z; |
171 | break; |
180 | break; |
172 | } |
181 | } |
173 | spinlock_unlock(&z->lock); |
182 | spinlock_unlock(&z->lock); |
174 | } |
183 | } |
175 | 184 | ||
176 | ASSERT(zone != NULL); |
185 | ASSERT(zone != NULL); |
177 | 186 | ||
- | 187 | frame = ADDR2FRAME(zone, addr); |
|
178 | frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
188 | // frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
179 | ASSERT(frame->refcount); |
189 | ASSERT(frame->refcount); |
180 | 190 | ||
181 | if (!--frame->refcount) { |
191 | if (!--frame->refcount) { |
182 | list_append(&frame->link, &zone->free_head); /* append frame to free_head */ |
192 | list_append(&frame->link, &zone->free_head); /* append frame to free_head */ |
183 | zone->free_count++; |
193 | zone->free_count++; |
184 | zone->busy_count--; |
194 | zone->busy_count--; |
185 | } |
195 | } |
186 | 196 | ||
187 | spinlock_unlock(&zone->lock); |
197 | spinlock_unlock(&zone->lock); |
188 | 198 | ||
189 | spinlock_unlock(&zone_head_lock); |
199 | spinlock_unlock(&zone_head_lock); |
190 | interrupts_restore(ipl); |
200 | interrupts_restore(ipl); |
191 | } |
201 | } |
192 | 202 | ||
193 | /** Mark frame not free. |
203 | /** Mark frame not free. |
194 | * |
204 | * |
195 | * Find respective frame structrue for supplied addr. |
205 | * Find respective frame structrue for supplied addr. |
196 | * Increment frame reference count and remove the frame structure from free list. |
206 | * Increment frame reference count and remove the frame structure from free list. |
197 | * |
207 | * |
198 | * @param addr Address of the frame to be marked. It must be a multiple of FRAME_SIZE. |
208 | * @param addr Address of the frame to be marked. It must be a multiple of FRAME_SIZE. |
199 | */ |
209 | */ |
200 | void frame_not_free(__address addr) |
210 | void frame_not_free(__address addr) |
201 | { |
211 | { |
202 | ipl_t ipl; |
212 | ipl_t ipl; |
203 | link_t *cur; |
213 | link_t *cur; |
204 | zone_t *z; |
214 | zone_t *z; |
205 | zone_t *zone = NULL; |
215 | zone_t *zone = NULL; |
206 | frame_t *frame; |
216 | frame_t *frame; |
207 | 217 | ||
208 | ASSERT(addr % FRAME_SIZE == 0); |
218 | ASSERT(addr % FRAME_SIZE == 0); |
209 | 219 | ||
210 | ipl = interrupts_disable(); |
220 | ipl = interrupts_disable(); |
211 | spinlock_lock(&zone_head_lock); |
221 | spinlock_lock(&zone_head_lock); |
212 | 222 | ||
213 | /* |
223 | /* |
214 | * First, find host frame zone for addr. |
224 | * First, find host frame zone for addr. |
215 | */ |
225 | */ |
216 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
226 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
217 | z = list_get_instance(cur, zone_t, link); |
227 | z = list_get_instance(cur, zone_t, link); |
218 | 228 | ||
219 | spinlock_lock(&z->lock); |
229 | spinlock_lock(&z->lock); |
220 | 230 | ||
221 | if (IS_KA(addr)) |
231 | if (IS_KA(addr)) |
222 | addr = KA2PA(addr); |
232 | addr = KA2PA(addr); |
223 | 233 | ||
224 | /* |
234 | /* |
225 | * Check if addr belongs to z. |
235 | * Check if addr belongs to z. |
226 | */ |
236 | */ |
227 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
237 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
228 | zone = z; |
238 | zone = z; |
229 | break; |
239 | break; |
230 | } |
240 | } |
231 | spinlock_unlock(&z->lock); |
241 | spinlock_unlock(&z->lock); |
232 | } |
242 | } |
233 | 243 | ||
234 | ASSERT(zone != NULL); |
244 | ASSERT(zone != NULL); |
235 | 245 | ||
236 | frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
246 | //frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
- | 247 | frame = ADDR2FRAME(zone, addr); |
|
237 | 248 | ||
238 | if (!frame->refcount) { |
249 | if (!frame->refcount) { |
239 | frame->refcount++; |
250 | frame->refcount++; |
240 | 251 | ||
241 | list_remove(&frame->link); /* remove frame from free_head */ |
252 | list_remove(&frame->link); /* remove frame from free_head */ |
242 | zone->free_count--; |
253 | zone->free_count--; |
243 | zone->busy_count++; |
254 | zone->busy_count++; |
244 | } |
255 | } |
245 | 256 | ||
246 | spinlock_unlock(&zone->lock); |
257 | spinlock_unlock(&zone->lock); |
247 | 258 | ||
248 | spinlock_unlock(&zone_head_lock); |
259 | spinlock_unlock(&zone_head_lock); |
249 | interrupts_restore(ipl); |
260 | interrupts_restore(ipl); |
250 | } |
261 | } |
251 | 262 | ||
252 | /** Mark frame region not free. |
263 | /** Mark frame region not free. |
253 | * |
264 | * |
254 | * Mark frame region not free. |
265 | * Mark frame region not free. |
255 | * |
266 | * |
256 | * @param start First address. |
267 | * @param start First address. |
257 | * @param stop Last address. |
268 | * @param stop Last address. |
258 | */ |
269 | */ |
259 | void frame_region_not_free(__address start, __address stop) |
270 | void frame_region_not_free(__address start, __address stop) |
260 | { |
271 | { |
261 | __address a; |
272 | __address a; |
262 | 273 | ||
263 | start /= FRAME_SIZE; |
274 | start /= FRAME_SIZE; |
264 | stop /= FRAME_SIZE; |
275 | stop /= FRAME_SIZE; |
265 | for (a = start; a <= stop; a++) |
276 | for (a = start; a <= stop; a++) |
266 | frame_not_free(a * FRAME_SIZE); |
277 | frame_not_free(a * FRAME_SIZE); |
267 | } |
278 | } |
268 | 279 | ||
269 | 280 | ||
270 | /** Initialize zonekeeping |
281 | /** Initialize zonekeeping |
271 | * |
282 | * |
272 | * Initialize zonekeeping. |
283 | * Initialize zonekeeping. |
273 | */ |
284 | */ |
274 | void zone_init(void) |
285 | void zone_init(void) |
275 | { |
286 | { |
276 | spinlock_initialize(&zone_head_lock); |
287 | spinlock_initialize(&zone_head_lock); |
277 | list_initialize(&zone_head); |
288 | list_initialize(&zone_head); |
278 | } |
289 | } |
279 | 290 | ||
280 | /** Create frame zone |
291 | /** Create frame zone |
281 | * |
292 | * |
282 | * Create new frame zone. |
293 | * Create new frame zone. |
283 | * |
294 | * |
284 | * @param start Physical address of the first frame within the zone. |
295 | * @param start Physical address of the first frame within the zone. |
285 | * @param size Size of the zone. Must be a multiple of FRAME_SIZE. |
296 | * @param size Size of the zone. Must be a multiple of FRAME_SIZE. |
286 | * @param flags Zone flags. |
297 | * @param flags Zone flags. |
287 | * |
298 | * |
288 | * @return Initialized zone. |
299 | * @return Initialized zone. |
289 | */ |
300 | */ |
290 | zone_t *zone_create(__address start, size_t size, int flags) |
301 | zone_t *zone_create(__address start, size_t size, int flags) |
291 | { |
302 | { |
292 | zone_t *z; |
303 | zone_t *z; |
293 | count_t cnt; |
304 | count_t cnt; |
294 | int i; |
305 | int i; |
- | 306 | __u8 max_order; |
|
295 | 307 | ||
296 | ASSERT(start % FRAME_SIZE == 0); |
308 | ASSERT(start % FRAME_SIZE == 0); |
297 | ASSERT(size % FRAME_SIZE == 0); |
309 | ASSERT(size % FRAME_SIZE == 0); |
298 | 310 | ||
299 | cnt = size / FRAME_SIZE; |
311 | cnt = size / FRAME_SIZE; |
300 | 312 | ||
301 | z = (zone_t *) early_malloc(sizeof(zone_t)); |
313 | z = (zone_t *) early_malloc(sizeof(zone_t)); |
302 | if (z) { |
314 | if (z) { |
303 | link_initialize(&z->link); |
315 | link_initialize(&z->link); |
304 | spinlock_initialize(&z->lock); |
316 | spinlock_initialize(&z->lock); |
305 | 317 | ||
306 | z->base = start; |
318 | z->base = start; |
307 | z->flags = flags; |
319 | z->flags = flags; |
308 | 320 | ||
309 | z->free_count = cnt; |
321 | z->free_count = cnt; |
310 | list_initialize(&z->free_head); |
322 | list_initialize(&z->free_head); |
311 | 323 | ||
312 | z->busy_count = 0; |
324 | z->busy_count = 0; |
313 | 325 | ||
314 | z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t)); |
326 | z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t)); |
315 | if (!z->frames) { |
327 | if (!z->frames) { |
316 | early_free(z); |
328 | early_free(z); |
317 | return NULL; |
329 | return NULL; |
318 | } |
330 | } |
319 | 331 | ||
320 | for (i = 0; i<cnt; i++) { |
332 | for (i = 0; i<cnt; i++) { |
321 | frame_initialize(&z->frames[i], z); |
333 | frame_initialize(&z->frames[i], z); |
322 | list_append(&z->frames[i].link, &z->free_head); |
334 | list_append(&z->frames[i].link, &z->free_head); |
323 | } |
335 | } |
324 | 336 | ||
- | 337 | /* |
|
- | 338 | * Create buddy system for the zone |
|
- | 339 | */ |
|
- | 340 | for (max_order = 0; cnt >> max_order; max_order++); |
|
- | 341 | z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations); |
|
325 | } |
342 | } |
326 | 343 | ||
327 | return z; |
344 | return z; |
328 | } |
345 | } |
329 | 346 | ||
330 | /** Attach frame zone |
347 | /** Attach frame zone |
331 | * |
348 | * |
332 | * Attach frame zone to zone list. |
349 | * Attach frame zone to zone list. |
333 | * |
350 | * |
334 | * @param zone Zone to be attached. |
351 | * @param zone Zone to be attached. |
335 | */ |
352 | */ |
336 | void zone_attach(zone_t *zone) |
353 | void zone_attach(zone_t *zone) |
337 | { |
354 | { |
338 | ipl_t ipl; |
355 | ipl_t ipl; |
339 | 356 | ||
340 | ipl = interrupts_disable(); |
357 | ipl = interrupts_disable(); |
341 | spinlock_lock(&zone_head_lock); |
358 | spinlock_lock(&zone_head_lock); |
342 | 359 | ||
343 | list_append(&zone->link, &zone_head); |
360 | list_append(&zone->link, &zone_head); |
344 | 361 | ||
345 | spinlock_unlock(&zone_head_lock); |
362 | spinlock_unlock(&zone_head_lock); |
346 | interrupts_restore(ipl); |
363 | interrupts_restore(ipl); |
347 | } |
364 | } |
348 | 365 | ||
349 | /** Initialize frame structure |
366 | /** Initialize frame structure |
350 | * |
367 | * |
351 | * Initialize frame structure. |
368 | * Initialize frame structure. |
352 | * |
369 | * |
353 | * @param frame Frame structure to be initialized. |
370 | * @param frame Frame structure to be initialized. |
354 | * @param zone Host frame zone. |
371 | * @param zone Host frame zone. |
355 | */ |
372 | */ |
356 | void frame_initialize(frame_t *frame, zone_t *zone) |
373 | void frame_initialize(frame_t *frame, zone_t *zone) |
357 | { |
374 | { |
358 | frame->refcount = 0; |
375 | frame->refcount = 0; |
359 | link_initialize(&frame->link); |
376 | link_initialize(&frame->link); |
360 | } |
377 | } |
- | 378 | ||
- | 379 | ||
- | 380 | ||
- | 381 | /* |
|
- | 382 | * buddy system functions (under construction) |
|
- | 383 | * |
|
- | 384 | */ |
|
- | 385 | ||
- | 386 | ||
- | 387 | /** Allocate 2^order frames |
|
- | 388 | * |
|
- | 389 | */ |
|
- | 390 | __address zone_buddy_frame_alloc(int flags, __u8 order) { |
|
- | 391 | ipl_t ipl; |
|
- | 392 | link_t *cur, *tmp; |
|
- | 393 | zone_t *z; |
|
- | 394 | zone_t *zone = NULL; |
|
- | 395 | frame_t *frame = NULL; |
|
- | 396 | __address v; |
|
- | 397 | ||
- | 398 | loop: |
|
- | 399 | ipl = interrupts_disable(); |
|
- | 400 | spinlock_lock(&zone_head_lock); |
|
- | 401 | ||
- | 402 | /* |
|
- | 403 | * First, find suitable frame zone. |
|
- | 404 | */ |
|
- | 405 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
|
- | 406 | z = list_get_instance(cur, zone_t, link); |
|
- | 407 | ||
- | 408 | spinlock_lock(&z->lock); |
|
- | 409 | /* |
|
- | 410 | * Check if the zone has 2^order frames area available |
|
- | 411 | * TODO: Must check if buddy system has at least block in order >= given order |
|
- | 412 | */ |
|
- | 413 | if (z->free_count == (1 >> order)) { |
|
- | 414 | zone = z; |
|
- | 415 | break; |
|
- | 416 | } |
|
- | 417 | ||
- | 418 | spinlock_unlock(&z->lock); |
|
- | 419 | } |
|
- | 420 | ||
- | 421 | if (!zone) { |
|
- | 422 | if (flags & FRAME_PANIC) |
|
- | 423 | panic("Can't allocate frame.\n"); |
|
- | 424 | ||
- | 425 | /* |
|
- | 426 | * TODO: Sleep until frames are available again. |
|
- | 427 | */ |
|
- | 428 | spinlock_unlock(&zone_head_lock); |
|
- | 429 | interrupts_restore(ipl); |
|
- | 430 | ||
- | 431 | panic("Sleep not implemented.\n"); |
|
- | 432 | goto loop; |
|
- | 433 | } |
|
- | 434 | ||
- | 435 | ||
- | 436 | /* Allocate frames from zone buddy system */ |
|
- | 437 | cur = buddy_system_alloc(zone->buddy_system, order); |
|
- | 438 | ||
- | 439 | /* frame will be actually a first frame of the block */ |
|
- | 440 | frame = list_get_instance(cur, frame_t, buddy_link); |
|
- | 441 | ||
- | 442 | /* get frame address */ |
|
- | 443 | v = FRAME2ADDR(zone, frame); |
|
- | 444 | ||
- | 445 | if (flags & FRAME_KA) |
|
- | 446 | v = PA2KA(v); |
|
- | 447 | ||
- | 448 | spinlock_unlock(&zone->lock); |
|
- | 449 | spinlock_unlock(&zone_head_lock); |
|
- | 450 | interrupts_restore(ipl); |
|
- | 451 | ||
- | 452 | return v; |
|
- | 453 | } |
|
- | 454 | ||
- | 455 | ||
- | 456 | /** Free frame(s) |
|
- | 457 | * |
|
- | 458 | * @param addr Address of the frame(s) to be freed. It must be a multiple of FRAME_SIZE. |
|
- | 459 | */ |
|
- | 460 | void zone_buddy_frame_free(__address addr) |
|
- | 461 | { |
|
- | 462 | ipl_t ipl; |
|
- | 463 | link_t *cur; |
|
- | 464 | zone_t *z; |
|
- | 465 | zone_t *zone = NULL; |
|
- | 466 | frame_t *frame; |
|
- | 467 | ||
- | 468 | ASSERT(addr % FRAME_SIZE == 0); |
|
- | 469 | ||
- | 470 | ipl = interrupts_disable(); |
|
- | 471 | spinlock_lock(&zone_head_lock); |
|
- | 472 | ||
- | 473 | /* |
|
- | 474 | * First, find host frame zone for addr. |
|
- | 475 | */ |
|
- | 476 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
|
- | 477 | z = list_get_instance(cur, zone_t, link); |
|
- | 478 | ||
- | 479 | spinlock_lock(&z->lock); |
|
- | 480 | ||
- | 481 | if (IS_KA(addr)) |
|
- | 482 | addr = KA2PA(addr); |
|
- | 483 | ||
- | 484 | /* |
|
- | 485 | * Check if addr belongs to z. |
|
- | 486 | */ |
|
- | 487 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
|
- | 488 | zone = z; |
|
- | 489 | break; |
|
- | 490 | } |
|
- | 491 | spinlock_unlock(&z->lock); |
|
- | 492 | } |
|
- | 493 | ||
- | 494 | ASSERT(zone != NULL); |
|
- | 495 | ||
- | 496 | frame = ADDR2FRAME(zone, addr); |
|
- | 497 | ||
- | 498 | ASSERT(frame->refcount); |
|
- | 499 | ||
- | 500 | if (!--frame->refcount) { |
|
- | 501 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
|
- | 502 | } |
|
- | 503 | ||
- | 504 | spinlock_unlock(&zone->lock); |
|
- | 505 | ||
- | 506 | spinlock_unlock(&zone_head_lock); |
|
- | 507 | interrupts_restore(ipl); |
|
- | 508 | } |
|
- | 509 | ||
- | 510 | ||
- | 511 | /** Buddy system find_buddy implementation |
|
- | 512 | * |
|
- | 513 | */ |
|
- | 514 | link_t * zone_buddy_find_buddy(link_t * buddy) { |
|
- | 515 | ||
- | 516 | } |
|
- | 517 | ||
- | 518 | /** Buddy system bisect implementation |
|
- | 519 | * |
|
- | 520 | */ |
|
- | 521 | link_t * zone_buddy_bisect(link_t * block) { |
|
- | 522 | ||
- | 523 | } |
|
- | 524 | ||
- | 525 | /** Buddy system coalesce implementation |
|
- | 526 | * |
|
- | 527 | */ |
|
- | 528 | link_t * zone_buddy_coalesce(link_t * buddy_l, link_t * buddy_r) { |
|
- | 529 | ||
- | 530 | } |
|
- | 531 | ||
- | 532 | /** Buddy system set_order implementation |
|
- | 533 | * |
|
- | 534 | */ |
|
- | 535 | void zone_buddy_set_order(link_t * block, __u8 order) { |
|
- | 536 | ||
- | 537 | } |
|
- | 538 | ||
- | 539 | /** Buddy system get_order implementation |
|
- | 540 | * |
|
- | 541 | */ |
|
- | 542 | __u8 zone_buddy_get_order(link_t * block) { |
|
- | 543 | ||
- | 544 | ||
- | 545 | } |
|
361 | 546 |