Rev 1063 | Rev 1196 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1063 | Rev 1093 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2005 Jakub Jermar |
2 | * Copyright (C) 2001-2005 Jakub Jermar |
3 | * Copyright (C) 2005 Sergey Bondari |
3 | * Copyright (C) 2005 Sergey Bondari |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /* |
30 | /* |
31 | * Locking order |
31 | * Locking order |
32 | * |
32 | * |
33 | * In order to access particular zone, the process must first lock |
33 | * In order to access particular zone, the process must first lock |
34 | * the zones.lock, then lock the zone and then unlock the zones.lock. |
34 | * the zones.lock, then lock the zone and then unlock the zones.lock. |
35 | * This insures, that we can fiddle with the zones in runtime without |
35 | * This insures, that we can fiddle with the zones in runtime without |
36 | * affecting the processes. |
36 | * affecting the processes. |
37 | * |
37 | * |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <typedefs.h> |
40 | #include <typedefs.h> |
41 | #include <arch/types.h> |
41 | #include <arch/types.h> |
42 | #include <mm/frame.h> |
42 | #include <mm/frame.h> |
43 | #include <mm/as.h> |
43 | #include <mm/as.h> |
44 | #include <panic.h> |
44 | #include <panic.h> |
45 | #include <debug.h> |
45 | #include <debug.h> |
46 | #include <adt/list.h> |
46 | #include <adt/list.h> |
47 | #include <synch/spinlock.h> |
47 | #include <synch/spinlock.h> |
48 | #include <arch/asm.h> |
48 | #include <arch/asm.h> |
49 | #include <arch.h> |
49 | #include <arch.h> |
50 | #include <print.h> |
50 | #include <print.h> |
51 | #include <align.h> |
51 | #include <align.h> |
52 | #include <mm/slab.h> |
52 | #include <mm/slab.h> |
53 | #include <bitops.h> |
53 | #include <bitops.h> |
54 | #include <macros.h> |
54 | #include <macros.h> |
55 | 55 | ||
56 | typedef struct { |
56 | typedef struct { |
57 | count_t refcount; /**< tracking of shared frames */ |
57 | count_t refcount; /**< tracking of shared frames */ |
58 | __u8 buddy_order; /**< buddy system block order */ |
58 | __u8 buddy_order; /**< buddy system block order */ |
59 | link_t buddy_link; /**< link to the next free block inside one order */ |
59 | link_t buddy_link; /**< link to the next free block inside one order */ |
60 | void *parent; /**< If allocated by slab, this points there */ |
60 | void *parent; /**< If allocated by slab, this points there */ |
61 | }frame_t; |
61 | }frame_t; |
62 | 62 | ||
63 | typedef struct { |
63 | typedef struct { |
64 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
64 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
65 | pfn_t base; /**< frame_no of the first frame in the frames array */ |
65 | pfn_t base; /**< frame_no of the first frame in the frames array */ |
66 | count_t count; /**< Size of zone */ |
66 | count_t count; /**< Size of zone */ |
67 | 67 | ||
68 | frame_t *frames; /**< array of frame_t structures in this zone */ |
68 | frame_t *frames; /**< array of frame_t structures in this zone */ |
69 | count_t free_count; /**< number of free frame_t structures */ |
69 | count_t free_count; /**< number of free frame_t structures */ |
70 | count_t busy_count; /**< number of busy frame_t structures */ |
70 | count_t busy_count; /**< number of busy frame_t structures */ |
71 | 71 | ||
72 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
72 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
73 | int flags; |
73 | int flags; |
74 | }zone_t; |
74 | }zone_t; |
75 | 75 | ||
76 | /* |
76 | /* |
77 | * The zoneinfo.lock must be locked when accessing zoneinfo structure. |
77 | * The zoneinfo.lock must be locked when accessing zoneinfo structure. |
78 | * Some of the attributes in zone_t structures are 'read-only' |
78 | * Some of the attributes in zone_t structures are 'read-only' |
79 | */ |
79 | */ |
80 | 80 | ||
81 | struct { |
81 | struct { |
82 | SPINLOCK_DECLARE(lock); |
82 | SPINLOCK_DECLARE(lock); |
83 | int count; |
83 | int count; |
84 | zone_t *info[ZONES_MAX]; |
84 | zone_t *info[ZONES_MAX]; |
85 | }zones; |
85 | }zones; |
86 | 86 | ||
87 | 87 | ||
88 | /*********************************/ |
88 | /*********************************/ |
89 | /* Helper functions */ |
89 | /* Helper functions */ |
90 | static inline index_t frame_index(zone_t *zone, frame_t *frame) |
90 | static inline index_t frame_index(zone_t *zone, frame_t *frame) |
91 | { |
91 | { |
92 | return (index_t)(frame - zone->frames); |
92 | return (index_t)(frame - zone->frames); |
93 | } |
93 | } |
94 | static inline index_t frame_index_abs(zone_t *zone, frame_t *frame) |
94 | static inline index_t frame_index_abs(zone_t *zone, frame_t *frame) |
95 | { |
95 | { |
96 | return (index_t)(frame - zone->frames) + zone->base; |
96 | return (index_t)(frame - zone->frames) + zone->base; |
97 | } |
97 | } |
98 | static inline int frame_index_valid(zone_t *zone, index_t index) |
98 | static inline int frame_index_valid(zone_t *zone, index_t index) |
99 | { |
99 | { |
100 | return index >= 0 && index < zone->count; |
100 | return index >= 0 && index < zone->count; |
101 | } |
101 | } |
102 | 102 | ||
103 | /** Compute pfn_t from frame_t pointer & zone pointer */ |
103 | /** Compute pfn_t from frame_t pointer & zone pointer */ |
104 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
104 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
105 | { |
105 | { |
106 | return frame - zone->frames; |
106 | return frame - zone->frames; |
107 | } |
107 | } |
108 | 108 | ||
109 | /** Initialize frame structure |
109 | /** Initialize frame structure |
110 | * |
110 | * |
111 | * Initialize frame structure. |
111 | * Initialize frame structure. |
112 | * |
112 | * |
113 | * @param frame Frame structure to be initialized. |
113 | * @param frame Frame structure to be initialized. |
114 | */ |
114 | */ |
115 | static void frame_initialize(frame_t *frame) |
115 | static void frame_initialize(frame_t *frame) |
116 | { |
116 | { |
117 | frame->refcount = 1; |
117 | frame->refcount = 1; |
118 | frame->buddy_order = 0; |
118 | frame->buddy_order = 0; |
119 | } |
119 | } |
120 | 120 | ||
121 | /*************************************/ |
121 | /*************************************/ |
122 | /* Zoneinfo functions */ |
122 | /* Zoneinfo functions */ |
123 | 123 | ||
124 | /** |
124 | /** |
125 | * Insert-sort zone into zones list |
125 | * Insert-sort zone into zones list |
126 | * |
126 | * |
127 | * @return zone number on success, -1 on error |
127 | * @return zone number on success, -1 on error |
128 | */ |
128 | */ |
129 | static int zones_add_zone(zone_t *newzone) |
129 | static int zones_add_zone(zone_t *newzone) |
130 | { |
130 | { |
131 | int i,j; |
131 | int i,j; |
132 | ipl_t ipl; |
132 | ipl_t ipl; |
133 | zone_t *z; |
133 | zone_t *z; |
134 | 134 | ||
135 | ipl = interrupts_disable(); |
135 | ipl = interrupts_disable(); |
136 | spinlock_lock(&zones.lock); |
136 | spinlock_lock(&zones.lock); |
137 | /* Try to merge */ |
137 | /* Try to merge */ |
138 | if (zones.count + 1 == ZONES_MAX) |
138 | if (zones.count + 1 == ZONES_MAX) |
139 | panic("Maximum zone(%d) count exceeded.", ZONES_MAX); |
139 | panic("Maximum zone(%d) count exceeded.", ZONES_MAX); |
140 | for (i = 0; i < zones.count; i++) { |
140 | for (i = 0; i < zones.count; i++) { |
141 | /* Check for overflow */ |
141 | /* Check for overflow */ |
142 | z = zones.info[i]; |
142 | z = zones.info[i]; |
143 | if (overlaps(newzone->base,newzone->count, |
143 | if (overlaps(newzone->base,newzone->count, |
144 | z->base, z->count)) { |
144 | z->base, z->count)) { |
145 | printf("Zones overlap!\n"); |
145 | printf("Zones overlap!\n"); |
146 | return -1; |
146 | return -1; |
147 | } |
147 | } |
148 | if (newzone->base < z->base) |
148 | if (newzone->base < z->base) |
149 | break; |
149 | break; |
150 | } |
150 | } |
151 | /* Move other zones up */ |
151 | /* Move other zones up */ |
152 | for (j = i;j < zones.count; j++) |
152 | for (j = i;j < zones.count; j++) |
153 | zones.info[j + 1] = zones.info[j]; |
153 | zones.info[j + 1] = zones.info[j]; |
154 | zones.info[i] = newzone; |
154 | zones.info[i] = newzone; |
155 | zones.count++; |
155 | zones.count++; |
156 | spinlock_unlock(&zones.lock); |
156 | spinlock_unlock(&zones.lock); |
157 | interrupts_restore(ipl); |
157 | interrupts_restore(ipl); |
158 | 158 | ||
159 | return i; |
159 | return i; |
160 | } |
160 | } |
161 | 161 | ||
162 | /** |
162 | /** |
163 | * Try to find a zone where can we find the frame |
163 | * Try to find a zone where can we find the frame |
164 | * |
164 | * |
165 | * @param hint Start searching in zone 'hint' |
165 | * @param hint Start searching in zone 'hint' |
166 | * @param lock Lock zone if true |
166 | * @param lock Lock zone if true |
167 | * |
167 | * |
168 | * Assume interrupts disable |
168 | * Assume interrupts disable |
169 | */ |
169 | */ |
170 | static zone_t * find_zone_and_lock(pfn_t frame, int *pzone) |
170 | static zone_t * find_zone_and_lock(pfn_t frame, int *pzone) |
171 | { |
171 | { |
172 | int i; |
172 | int i; |
173 | int hint = pzone ? *pzone : 0; |
173 | int hint = pzone ? *pzone : 0; |
174 | zone_t *z; |
174 | zone_t *z; |
175 | 175 | ||
176 | spinlock_lock(&zones.lock); |
176 | spinlock_lock(&zones.lock); |
177 | 177 | ||
178 | if (hint >= zones.count || hint < 0) |
178 | if (hint >= zones.count || hint < 0) |
179 | hint = 0; |
179 | hint = 0; |
180 | 180 | ||
181 | i = hint; |
181 | i = hint; |
182 | do { |
182 | do { |
183 | z = zones.info[i]; |
183 | z = zones.info[i]; |
184 | spinlock_lock(&z->lock); |
184 | spinlock_lock(&z->lock); |
185 | if (z->base <= frame && z->base + z->count > frame) { |
185 | if (z->base <= frame && z->base + z->count > frame) { |
186 | spinlock_unlock(&zones.lock); /* Unlock the global lock */ |
186 | spinlock_unlock(&zones.lock); /* Unlock the global lock */ |
187 | if (pzone) |
187 | if (pzone) |
188 | *pzone = i; |
188 | *pzone = i; |
189 | return z; |
189 | return z; |
190 | } |
190 | } |
191 | spinlock_unlock(&z->lock); |
191 | spinlock_unlock(&z->lock); |
192 | 192 | ||
193 | i++; |
193 | i++; |
194 | if (i >= zones.count) |
194 | if (i >= zones.count) |
195 | i = 0; |
195 | i = 0; |
196 | } while(i != hint); |
196 | } while(i != hint); |
197 | 197 | ||
198 | spinlock_unlock(&zones.lock); |
198 | spinlock_unlock(&zones.lock); |
199 | return NULL; |
199 | return NULL; |
200 | } |
200 | } |
201 | 201 | ||
202 | /** @return True if zone can allocate specified order */ |
202 | /** @return True if zone can allocate specified order */ |
203 | static int zone_can_alloc(zone_t *z, __u8 order) |
203 | static int zone_can_alloc(zone_t *z, __u8 order) |
204 | { |
204 | { |
205 | return buddy_system_can_alloc(z->buddy_system, order); |
205 | return buddy_system_can_alloc(z->buddy_system, order); |
206 | } |
206 | } |
207 | 207 | ||
208 | /** |
208 | /** |
209 | * Find AND LOCK zone that can allocate order frames |
209 | * Find AND LOCK zone that can allocate order frames |
210 | * |
210 | * |
211 | * Assume interrupts are disabled!! |
211 | * Assume interrupts are disabled!! |
212 | * |
212 | * |
213 | * @param pzone Pointer to preferred zone or NULL, on return contains zone number |
213 | * @param pzone Pointer to preferred zone or NULL, on return contains zone number |
214 | */ |
214 | */ |
215 | static zone_t * find_free_zone_lock(__u8 order, int *pzone) |
215 | static zone_t * find_free_zone_lock(__u8 order, int *pzone) |
216 | { |
216 | { |
217 | int i; |
217 | int i; |
218 | zone_t *z; |
218 | zone_t *z; |
219 | int hint = pzone ? *pzone : 0; |
219 | int hint = pzone ? *pzone : 0; |
220 | 220 | ||
221 | spinlock_lock(&zones.lock); |
221 | spinlock_lock(&zones.lock); |
222 | if (hint >= zones.count) |
222 | if (hint >= zones.count) |
223 | hint = 0; |
223 | hint = 0; |
224 | i = hint; |
224 | i = hint; |
225 | do { |
225 | do { |
226 | z = zones.info[i]; |
226 | z = zones.info[i]; |
227 | 227 | ||
228 | spinlock_lock(&z->lock); |
228 | spinlock_lock(&z->lock); |
229 | 229 | ||
230 | /* Check if the zone has 2^order frames area available */ |
230 | /* Check if the zone has 2^order frames area available */ |
231 | if (zone_can_alloc(z, order)) { |
231 | if (zone_can_alloc(z, order)) { |
232 | spinlock_unlock(&zones.lock); |
232 | spinlock_unlock(&zones.lock); |
233 | if (pzone) |
233 | if (pzone) |
234 | *pzone = i; |
234 | *pzone = i; |
235 | return z; |
235 | return z; |
236 | } |
236 | } |
237 | spinlock_unlock(&z->lock); |
237 | spinlock_unlock(&z->lock); |
238 | if (++i >= zones.count) |
238 | if (++i >= zones.count) |
239 | i = 0; |
239 | i = 0; |
240 | } while(i != hint); |
240 | } while(i != hint); |
241 | spinlock_unlock(&zones.lock); |
241 | spinlock_unlock(&zones.lock); |
242 | return NULL; |
242 | return NULL; |
243 | } |
243 | } |
244 | 244 | ||
245 | /********************************************/ |
245 | /********************************************/ |
246 | /* Buddy system functions */ |
246 | /* Buddy system functions */ |
247 | 247 | ||
248 | /** Buddy system find_block implementation |
248 | /** Buddy system find_block implementation |
249 | * |
249 | * |
250 | * Find block that is parent of current list. |
250 | * Find block that is parent of current list. |
251 | * That means go to lower addresses, until such block is found |
251 | * That means go to lower addresses, until such block is found |
252 | * |
252 | * |
253 | * @param order - Order of parent must be different then this parameter!! |
253 | * @param order - Order of parent must be different then this parameter!! |
254 | */ |
254 | */ |
255 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
255 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
256 | __u8 order) |
256 | __u8 order) |
257 | { |
257 | { |
258 | frame_t * frame; |
258 | frame_t * frame; |
259 | zone_t * zone; |
259 | zone_t * zone; |
260 | index_t index; |
260 | index_t index; |
261 | 261 | ||
262 | frame = list_get_instance(child, frame_t, buddy_link); |
262 | frame = list_get_instance(child, frame_t, buddy_link); |
263 | zone = (zone_t *) b->data; |
263 | zone = (zone_t *) b->data; |
264 | 264 | ||
265 | index = frame_index(zone, frame); |
265 | index = frame_index(zone, frame); |
266 | do { |
266 | do { |
267 | if (zone->frames[index].buddy_order != order) { |
267 | if (zone->frames[index].buddy_order != order) { |
268 | return &zone->frames[index].buddy_link; |
268 | return &zone->frames[index].buddy_link; |
269 | } |
269 | } |
270 | } while(index-- > 0); |
270 | } while(index-- > 0); |
271 | return NULL; |
271 | return NULL; |
272 | } |
272 | } |
273 | 273 | ||
274 | static void zone_buddy_print_id(buddy_system_t *b, link_t *block) |
274 | static void zone_buddy_print_id(buddy_system_t *b, link_t *block) |
275 | { |
275 | { |
276 | frame_t * frame; |
276 | frame_t * frame; |
277 | zone_t * zone; |
277 | zone_t * zone; |
278 | index_t index; |
278 | index_t index; |
279 | 279 | ||
280 | frame = list_get_instance(block, frame_t, buddy_link); |
280 | frame = list_get_instance(block, frame_t, buddy_link); |
281 | zone = (zone_t *) b->data; |
281 | zone = (zone_t *) b->data; |
282 | index = frame_index(zone, frame); |
282 | index = frame_index(zone, frame); |
283 | printf("%d", index); |
283 | printf("%d", index); |
284 | } |
284 | } |
285 | 285 | ||
286 | /** Buddy system find_buddy implementation |
286 | /** Buddy system find_buddy implementation |
287 | * |
287 | * |
288 | * @param b Buddy system. |
288 | * @param b Buddy system. |
289 | * @param block Block for which buddy should be found |
289 | * @param block Block for which buddy should be found |
290 | * |
290 | * |
291 | * @return Buddy for given block if found |
291 | * @return Buddy for given block if found |
292 | */ |
292 | */ |
293 | static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) |
293 | static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) |
294 | { |
294 | { |
295 | frame_t * frame; |
295 | frame_t * frame; |
296 | zone_t * zone; |
296 | zone_t * zone; |
297 | index_t index; |
297 | index_t index; |
298 | bool is_left, is_right; |
298 | bool is_left, is_right; |
299 | 299 | ||
300 | frame = list_get_instance(block, frame_t, buddy_link); |
300 | frame = list_get_instance(block, frame_t, buddy_link); |
301 | zone = (zone_t *) b->data; |
301 | zone = (zone_t *) b->data; |
302 | ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order)); |
302 | ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order)); |
303 | 303 | ||
304 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
304 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
305 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
305 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
306 | 306 | ||
307 | ASSERT(is_left ^ is_right); |
307 | ASSERT(is_left ^ is_right); |
308 | if (is_left) { |
308 | if (is_left) { |
309 | index = (frame_index(zone, frame)) + (1 << frame->buddy_order); |
309 | index = (frame_index(zone, frame)) + (1 << frame->buddy_order); |
310 | } else { // if (is_right) |
310 | } else { // if (is_right) |
311 | index = (frame_index(zone, frame)) - (1 << frame->buddy_order); |
311 | index = (frame_index(zone, frame)) - (1 << frame->buddy_order); |
312 | } |
312 | } |
313 | 313 | ||
314 | if (frame_index_valid(zone, index)) { |
314 | if (frame_index_valid(zone, index)) { |
315 | if (zone->frames[index].buddy_order == frame->buddy_order && |
315 | if (zone->frames[index].buddy_order == frame->buddy_order && |
316 | zone->frames[index].refcount == 0) { |
316 | zone->frames[index].refcount == 0) { |
317 | return &zone->frames[index].buddy_link; |
317 | return &zone->frames[index].buddy_link; |
318 | } |
318 | } |
319 | } |
319 | } |
320 | 320 | ||
321 | return NULL; |
321 | return NULL; |
322 | } |
322 | } |
323 | 323 | ||
324 | /** Buddy system bisect implementation |
324 | /** Buddy system bisect implementation |
325 | * |
325 | * |
326 | * @param b Buddy system. |
326 | * @param b Buddy system. |
327 | * @param block Block to bisect |
327 | * @param block Block to bisect |
328 | * |
328 | * |
329 | * @return right block |
329 | * @return right block |
330 | */ |
330 | */ |
331 | static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) { |
331 | static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) { |
332 | frame_t * frame_l, * frame_r; |
332 | frame_t * frame_l, * frame_r; |
333 | 333 | ||
334 | frame_l = list_get_instance(block, frame_t, buddy_link); |
334 | frame_l = list_get_instance(block, frame_t, buddy_link); |
335 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
335 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
336 | 336 | ||
337 | return &frame_r->buddy_link; |
337 | return &frame_r->buddy_link; |
338 | } |
338 | } |
339 | 339 | ||
340 | /** Buddy system coalesce implementation |
340 | /** Buddy system coalesce implementation |
341 | * |
341 | * |
342 | * @param b Buddy system. |
342 | * @param b Buddy system. |
343 | * @param block_1 First block |
343 | * @param block_1 First block |
344 | * @param block_2 First block's buddy |
344 | * @param block_2 First block's buddy |
345 | * |
345 | * |
346 | * @return Coalesced block (actually block that represents lower address) |
346 | * @return Coalesced block (actually block that represents lower address) |
347 | */ |
347 | */ |
348 | static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, |
348 | static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, |
349 | link_t * block_2) |
349 | link_t * block_2) |
350 | { |
350 | { |
351 | frame_t *frame1, *frame2; |
351 | frame_t *frame1, *frame2; |
352 | 352 | ||
353 | frame1 = list_get_instance(block_1, frame_t, buddy_link); |
353 | frame1 = list_get_instance(block_1, frame_t, buddy_link); |
354 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
354 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
355 | 355 | ||
356 | return frame1 < frame2 ? block_1 : block_2; |
356 | return frame1 < frame2 ? block_1 : block_2; |
357 | } |
357 | } |
358 | 358 | ||
359 | /** Buddy system set_order implementation |
359 | /** Buddy system set_order implementation |
360 | * |
360 | * |
361 | * @param b Buddy system. |
361 | * @param b Buddy system. |
362 | * @param block Buddy system block |
362 | * @param block Buddy system block |
363 | * @param order Order to set |
363 | * @param order Order to set |
364 | */ |
364 | */ |
365 | static void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) { |
365 | static void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) { |
366 | frame_t * frame; |
366 | frame_t * frame; |
367 | frame = list_get_instance(block, frame_t, buddy_link); |
367 | frame = list_get_instance(block, frame_t, buddy_link); |
368 | frame->buddy_order = order; |
368 | frame->buddy_order = order; |
369 | } |
369 | } |
370 | 370 | ||
371 | /** Buddy system get_order implementation |
371 | /** Buddy system get_order implementation |
372 | * |
372 | * |
373 | * @param b Buddy system. |
373 | * @param b Buddy system. |
374 | * @param block Buddy system block |
374 | * @param block Buddy system block |
375 | * |
375 | * |
376 | * @return Order of block |
376 | * @return Order of block |
377 | */ |
377 | */ |
378 | static __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) { |
378 | static __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) { |
379 | frame_t * frame; |
379 | frame_t * frame; |
380 | frame = list_get_instance(block, frame_t, buddy_link); |
380 | frame = list_get_instance(block, frame_t, buddy_link); |
381 | return frame->buddy_order; |
381 | return frame->buddy_order; |
382 | } |
382 | } |
383 | 383 | ||
384 | /** Buddy system mark_busy implementation |
384 | /** Buddy system mark_busy implementation |
385 | * |
385 | * |
386 | * @param b Buddy system |
386 | * @param b Buddy system |
387 | * @param block Buddy system block |
387 | * @param block Buddy system block |
388 | * |
388 | * |
389 | */ |
389 | */ |
390 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) { |
390 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) { |
391 | frame_t * frame; |
391 | frame_t * frame; |
392 | 392 | ||
393 | frame = list_get_instance(block, frame_t, buddy_link); |
393 | frame = list_get_instance(block, frame_t, buddy_link); |
394 | frame->refcount = 1; |
394 | frame->refcount = 1; |
395 | } |
395 | } |
396 | 396 | ||
397 | /** Buddy system mark_available implementation |
397 | /** Buddy system mark_available implementation |
398 | * |
398 | * |
399 | * @param b Buddy system |
399 | * @param b Buddy system |
400 | * @param block Buddy system block |
400 | * @param block Buddy system block |
401 | * |
401 | * |
402 | */ |
402 | */ |
403 | static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) { |
403 | static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) { |
404 | frame_t * frame; |
404 | frame_t * frame; |
405 | frame = list_get_instance(block, frame_t, buddy_link); |
405 | frame = list_get_instance(block, frame_t, buddy_link); |
406 | frame->refcount = 0; |
406 | frame->refcount = 0; |
407 | } |
407 | } |
408 | 408 | ||
409 | static struct buddy_system_operations zone_buddy_system_operations = { |
409 | static struct buddy_system_operations zone_buddy_system_operations = { |
410 | .find_buddy = zone_buddy_find_buddy, |
410 | .find_buddy = zone_buddy_find_buddy, |
411 | .bisect = zone_buddy_bisect, |
411 | .bisect = zone_buddy_bisect, |
412 | .coalesce = zone_buddy_coalesce, |
412 | .coalesce = zone_buddy_coalesce, |
413 | .set_order = zone_buddy_set_order, |
413 | .set_order = zone_buddy_set_order, |
414 | .get_order = zone_buddy_get_order, |
414 | .get_order = zone_buddy_get_order, |
415 | .mark_busy = zone_buddy_mark_busy, |
415 | .mark_busy = zone_buddy_mark_busy, |
416 | .mark_available = zone_buddy_mark_available, |
416 | .mark_available = zone_buddy_mark_available, |
417 | .find_block = zone_buddy_find_block, |
417 | .find_block = zone_buddy_find_block, |
418 | .print_id = zone_buddy_print_id |
418 | .print_id = zone_buddy_print_id |
419 | }; |
419 | }; |
420 | 420 | ||
421 | /*************************************/ |
421 | /*************************************/ |
422 | /* Zone functions */ |
422 | /* Zone functions */ |
423 | 423 | ||
424 | /** Allocate frame in particular zone |
424 | /** Allocate frame in particular zone |
425 | * |
425 | * |
426 | * Assume zone is locked |
426 | * Assume zone is locked |
427 | * Panics, if allocation is impossible. |
427 | * Panics, if allocation is impossible. |
428 | * |
428 | * |
429 | * @return Frame index in zone |
429 | * @return Frame index in zone |
430 | */ |
430 | */ |
431 | static pfn_t zone_frame_alloc(zone_t *zone,__u8 order) |
431 | static pfn_t zone_frame_alloc(zone_t *zone,__u8 order) |
432 | { |
432 | { |
433 | pfn_t v; |
433 | pfn_t v; |
434 | link_t *tmp; |
434 | link_t *tmp; |
435 | frame_t *frame; |
435 | frame_t *frame; |
436 | 436 | ||
437 | /* Allocate frames from zone buddy system */ |
437 | /* Allocate frames from zone buddy system */ |
438 | tmp = buddy_system_alloc(zone->buddy_system, order); |
438 | tmp = buddy_system_alloc(zone->buddy_system, order); |
439 | 439 | ||
440 | ASSERT(tmp); |
440 | ASSERT(tmp); |
441 | 441 | ||
442 | /* Update zone information. */ |
442 | /* Update zone information. */ |
443 | zone->free_count -= (1 << order); |
443 | zone->free_count -= (1 << order); |
444 | zone->busy_count += (1 << order); |
444 | zone->busy_count += (1 << order); |
445 | 445 | ||
446 | /* Frame will be actually a first frame of the block. */ |
446 | /* Frame will be actually a first frame of the block. */ |
447 | frame = list_get_instance(tmp, frame_t, buddy_link); |
447 | frame = list_get_instance(tmp, frame_t, buddy_link); |
448 | 448 | ||
449 | /* get frame address */ |
449 | /* get frame address */ |
450 | v = make_frame_index(zone, frame); |
450 | v = make_frame_index(zone, frame); |
451 | return v; |
451 | return v; |
452 | } |
452 | } |
453 | 453 | ||
454 | /** Free frame from zone |
454 | /** Free frame from zone |
455 | * |
455 | * |
456 | * Assume zone is locked |
456 | * Assume zone is locked |
457 | */ |
457 | */ |
458 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
458 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
459 | { |
459 | { |
460 | frame_t *frame; |
460 | frame_t *frame; |
461 | __u8 order; |
461 | __u8 order; |
462 | 462 | ||
463 | frame = &zone->frames[frame_idx]; |
463 | frame = &zone->frames[frame_idx]; |
464 | 464 | ||
465 | /* remember frame order */ |
465 | /* remember frame order */ |
466 | order = frame->buddy_order; |
466 | order = frame->buddy_order; |
467 | 467 | ||
468 | ASSERT(frame->refcount); |
468 | ASSERT(frame->refcount); |
469 | 469 | ||
470 | if (!--frame->refcount) { |
470 | if (!--frame->refcount) { |
471 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
471 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
472 | 472 | ||
473 | /* Update zone information. */ |
473 | /* Update zone information. */ |
474 | zone->free_count += (1 << order); |
474 | zone->free_count += (1 << order); |
475 | zone->busy_count -= (1 << order); |
475 | zone->busy_count -= (1 << order); |
476 | } |
476 | } |
477 | } |
477 | } |
478 | 478 | ||
479 | /** Return frame from zone */ |
479 | /** Return frame from zone */ |
480 | static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) |
480 | static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) |
481 | { |
481 | { |
482 | ASSERT(frame_idx < zone->count); |
482 | ASSERT(frame_idx < zone->count); |
483 | return &zone->frames[frame_idx]; |
483 | return &zone->frames[frame_idx]; |
484 | } |
484 | } |
485 | 485 | ||
486 | /** Mark frame in zone unavailable to allocation */ |
486 | /** Mark frame in zone unavailable to allocation */ |
487 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
487 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
488 | { |
488 | { |
489 | frame_t *frame; |
489 | frame_t *frame; |
490 | link_t *link; |
490 | link_t *link; |
491 | 491 | ||
492 | frame = zone_get_frame(zone, frame_idx); |
492 | frame = zone_get_frame(zone, frame_idx); |
493 | if (frame->refcount) |
493 | if (frame->refcount) |
494 | return; |
494 | return; |
495 | link = buddy_system_alloc_block(zone->buddy_system, |
495 | link = buddy_system_alloc_block(zone->buddy_system, |
496 | &frame->buddy_link); |
496 | &frame->buddy_link); |
497 | ASSERT(link); |
497 | ASSERT(link); |
498 | zone->free_count--; |
498 | zone->free_count--; |
499 | } |
499 | } |
500 | 500 | ||
501 | /** |
501 | /** |
502 | * Join 2 zones |
502 | * Join 2 zones |
503 | * |
503 | * |
504 | * Expect zone_t *z to point to space at least zone_conf_size large |
504 | * Expect zone_t *z to point to space at least zone_conf_size large |
505 | * |
505 | * |
506 | * Assume z1 & z2 are locked |
506 | * Assume z1 & z2 are locked |
507 | */ |
507 | */ |
508 | 508 | ||
509 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
509 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
510 | { |
510 | { |
511 | __u8 max_order; |
511 | __u8 max_order; |
512 | int i, z2idx; |
512 | int i, z2idx; |
513 | pfn_t frame_idx; |
513 | pfn_t frame_idx; |
514 | frame_t *frame; |
514 | frame_t *frame; |
515 | 515 | ||
516 | ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count)); |
516 | ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count)); |
517 | ASSERT(z1->base < z2->base); |
517 | ASSERT(z1->base < z2->base); |
518 | 518 | ||
519 | spinlock_initialize(&z->lock, "zone_lock"); |
519 | spinlock_initialize(&z->lock, "zone_lock"); |
520 | z->base = z1->base; |
520 | z->base = z1->base; |
521 | z->count = z2->base+z2->count - z1->base; |
521 | z->count = z2->base+z2->count - z1->base; |
522 | z->flags = z1->flags & z2->flags; |
522 | z->flags = z1->flags & z2->flags; |
523 | 523 | ||
524 | z->free_count = z1->free_count + z2->free_count; |
524 | z->free_count = z1->free_count + z2->free_count; |
525 | z->busy_count = z1->busy_count + z2->busy_count; |
525 | z->busy_count = z1->busy_count + z2->busy_count; |
526 | 526 | ||
527 | max_order = fnzb(z->count); |
527 | max_order = fnzb(z->count); |
528 | 528 | ||
529 | z->buddy_system = (buddy_system_t *)&z[1]; |
529 | z->buddy_system = (buddy_system_t *)&z[1]; |
530 | buddy_system_create(z->buddy_system, max_order, |
530 | buddy_system_create(z->buddy_system, max_order, |
531 | &zone_buddy_system_operations, |
531 | &zone_buddy_system_operations, |
532 | (void *) z); |
532 | (void *) z); |
533 | 533 | ||
534 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
534 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
535 | for (i = 0; i < z->count; i++) { |
535 | for (i = 0; i < z->count; i++) { |
536 | /* This marks all frames busy */ |
536 | /* This marks all frames busy */ |
537 | frame_initialize(&z->frames[i]); |
537 | frame_initialize(&z->frames[i]); |
538 | } |
538 | } |
539 | /* Copy frames from both zones to preserve full frame orders, |
539 | /* Copy frames from both zones to preserve full frame orders, |
540 | * parents etc. Set all frames with refcount=0 to 1, because |
540 | * parents etc. Set all free frames with refcount=0 to 1, because |
541 | * we add all free frames to buddy allocator later again, clear |
541 | * we add all free frames to buddy allocator later again, clear |
- | 542 | * order to 0. Don't set busy frames with refcount=0, as they |
|
- | 543 | * will not be reallocated during merge and it would make later |
|
542 | * order to 0. |
544 | * problems with allocation/free. |
543 | */ |
545 | */ |
544 | for (i=0; i<z1->count; i++) |
546 | for (i=0; i<z1->count; i++) |
545 | z->frames[i] = z1->frames[i]; |
547 | z->frames[i] = z1->frames[i]; |
546 | for (i=0; i < z2->count; i++) { |
548 | for (i=0; i < z2->count; i++) { |
547 | z2idx = i + (z2->base - z1->base); |
549 | z2idx = i + (z2->base - z1->base); |
548 | z->frames[z2idx] = z2->frames[i]; |
550 | z->frames[z2idx] = z2->frames[i]; |
549 | } |
551 | } |
- | 552 | i = 0; |
|
550 | for (i=0; i < z->count; i++) { |
553 | while (i < z->count) { |
551 | if (!z->frames[i].refcount) { |
554 | if (z->frames[i].refcount) { |
- | 555 | /* skip busy frames */ |
|
- | 556 | i += 1 << z->frames[i].buddy_order; |
|
- | 557 | } else { /* Free frames, set refcount=1 */ |
|
- | 558 | /* All free frames have refcount=0, we need not |
|
- | 559 | * to check the order */ |
|
552 | z->frames[i].refcount = 1; |
560 | z->frames[i].refcount = 1; |
553 | z->frames[i].buddy_order = 0; |
561 | z->frames[i].buddy_order = 0; |
- | 562 | i++; |
|
554 | } |
563 | } |
555 | } |
564 | } |
556 | /* Add free blocks from the 2 original zones */ |
565 | /* Add free blocks from the 2 original zones */ |
557 | while (zone_can_alloc(z1, 0)) { |
566 | while (zone_can_alloc(z1, 0)) { |
558 | frame_idx = zone_frame_alloc(z1, 0); |
567 | frame_idx = zone_frame_alloc(z1, 0); |
559 | frame = &z->frames[frame_idx]; |
568 | frame = &z->frames[frame_idx]; |
560 | frame->refcount = 0; |
569 | frame->refcount = 0; |
561 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
570 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
562 | } |
571 | } |
563 | while (zone_can_alloc(z2, 0)) { |
572 | while (zone_can_alloc(z2, 0)) { |
564 | frame_idx = zone_frame_alloc(z2, 0); |
573 | frame_idx = zone_frame_alloc(z2, 0); |
565 | frame = &z->frames[frame_idx + (z2->base-z1->base)]; |
574 | frame = &z->frames[frame_idx + (z2->base-z1->base)]; |
566 | frame->refcount = 0; |
575 | frame->refcount = 0; |
567 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
576 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
568 | } |
577 | } |
569 | } |
578 | } |
570 | 579 | ||
571 | /** Return old configuration frames into the zone |
580 | /** Return old configuration frames into the zone |
572 | * |
581 | * |
573 | * We have several cases |
582 | * We have several cases |
574 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
583 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
575 | * - the conf. data was created by zone_create or |
584 | * - the conf. data was created by zone_create or |
576 | * updated with reduce_region -> free every frame |
585 | * updated with reduce_region -> free every frame |
577 | * |
586 | * |
578 | * @param newzone The actual zone where freeing should occur |
587 | * @param newzone The actual zone where freeing should occur |
579 | * @param oldzone Pointer to old zone configuration data that should |
588 | * @param oldzone Pointer to old zone configuration data that should |
580 | * be freed from new zone |
589 | * be freed from new zone |
581 | */ |
590 | */ |
582 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
591 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
583 | { |
592 | { |
584 | pfn_t pfn; |
593 | pfn_t pfn; |
585 | frame_t *frame; |
594 | frame_t *frame; |
586 | count_t cframes; |
595 | count_t cframes; |
587 | int i; |
596 | int i; |
588 | 597 | ||
589 | pfn = ADDR2PFN((__address)KA2PA(oldzone)); |
598 | pfn = ADDR2PFN((__address)KA2PA(oldzone)); |
590 | cframes = SIZE2FRAMES(zone_conf_size(oldzone->count)); |
599 | cframes = SIZE2FRAMES(zone_conf_size(oldzone->count)); |
591 | 600 | ||
592 | if (pfn < newzone->base || pfn >= newzone->base + newzone->count) |
601 | if (pfn < newzone->base || pfn >= newzone->base + newzone->count) |
593 | return; |
602 | return; |
594 | 603 | ||
595 | frame = &newzone->frames[pfn - newzone->base]; |
604 | frame = &newzone->frames[pfn - newzone->base]; |
596 | ASSERT(!frame->buddy_order); |
605 | ASSERT(!frame->buddy_order); |
597 | 606 | ||
598 | for (i=0; i < cframes; i++) { |
607 | for (i=0; i < cframes; i++) { |
599 | newzone->busy_count++; |
608 | newzone->busy_count++; |
600 | zone_frame_free(newzone, pfn+i-newzone->base); |
609 | zone_frame_free(newzone, pfn+i-newzone->base); |
601 | } |
610 | } |
602 | } |
611 | } |
603 | 612 | ||
604 | /** Reduce allocated block to count of order 0 frames |
613 | /** Reduce allocated block to count of order 0 frames |
605 | * |
614 | * |
606 | * The allocated block need 2^order frames of space. Reduce all frames |
615 | * The allocated block need 2^order frames of space. Reduce all frames |
607 | * in block to order 0 and free the unneded frames. This means, that |
616 | * in block to order 0 and free the unneded frames. This means, that |
608 | * when freeing the block, you have to free every frame from block. |
617 | * when freeing the block, you have to free every frame from block. |
609 | * |
618 | * |
610 | * @param zone |
619 | * @param zone |
611 | * @param frame_idx Index to block |
620 | * @param frame_idx Index to block |
612 | * @param count Allocated space in block |
621 | * @param count Allocated space in block |
613 | */ |
622 | */ |
614 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
623 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
615 | { |
624 | { |
616 | count_t i; |
625 | count_t i; |
617 | __u8 order; |
626 | __u8 order; |
618 | frame_t *frame; |
627 | frame_t *frame; |
619 | 628 | ||
620 | ASSERT(frame_idx+count < zone->count); |
629 | ASSERT(frame_idx+count < zone->count); |
621 | 630 | ||
622 | order = zone->frames[frame_idx].buddy_order; |
631 | order = zone->frames[frame_idx].buddy_order; |
623 | ASSERT((1 << order) >= count); |
632 | ASSERT((1 << order) >= count); |
624 | 633 | ||
625 | /* Reduce all blocks to order 0 */ |
634 | /* Reduce all blocks to order 0 */ |
626 | for (i=0; i < (1 << order); i++) { |
635 | for (i=0; i < (1 << order); i++) { |
627 | frame = &zone->frames[i + frame_idx]; |
636 | frame = &zone->frames[i + frame_idx]; |
628 | frame->buddy_order = 0; |
637 | frame->buddy_order = 0; |
629 | if (! frame->refcount) |
638 | if (! frame->refcount) |
630 | frame->refcount = 1; |
639 | frame->refcount = 1; |
631 | ASSERT(frame->refcount == 1); |
640 | ASSERT(frame->refcount == 1); |
632 | } |
641 | } |
633 | /* Free unneeded frames */ |
642 | /* Free unneeded frames */ |
634 | for (i=count; i < (1 << order); i++) { |
643 | for (i=count; i < (1 << order); i++) { |
635 | zone_frame_free(zone, i + frame_idx); |
644 | zone_frame_free(zone, i + frame_idx); |
636 | } |
645 | } |
637 | } |
646 | } |
638 | 647 | ||
639 | /** Merge zones z1 and z2 |
648 | /** Merge zones z1 and z2 |
640 | * |
649 | * |
641 | * - the zones must be 2 zones with no zone existing in between, |
650 | * - the zones must be 2 zones with no zone existing in between, |
642 | * which means that z2 = z1+1 |
651 | * which means that z2 = z1+1 |
643 | * |
652 | * |
644 | * - When you create a new zone, the frame allocator configuration does |
653 | * - When you create a new zone, the frame allocator configuration does |
645 | * not to be 2^order size. Once the allocator is running it is no longer |
654 | * not to be 2^order size. Once the allocator is running it is no longer |
646 | * possible, merged configuration data occupies more space :-/ |
655 | * possible, merged configuration data occupies more space :-/ |
647 | */ |
656 | */ |
648 | void zone_merge(int z1, int z2) |
657 | void zone_merge(int z1, int z2) |
649 | { |
658 | { |
650 | ipl_t ipl; |
659 | ipl_t ipl; |
651 | zone_t *zone1, *zone2, *newzone; |
660 | zone_t *zone1, *zone2, *newzone; |
652 | int cframes; |
661 | int cframes; |
653 | __u8 order; |
662 | __u8 order; |
654 | int i; |
663 | int i; |
655 | pfn_t pfn; |
664 | pfn_t pfn; |
656 | 665 | ||
657 | ipl = interrupts_disable(); |
666 | ipl = interrupts_disable(); |
658 | spinlock_lock(&zones.lock); |
667 | spinlock_lock(&zones.lock); |
659 | 668 | ||
660 | if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count) |
669 | if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count) |
661 | goto errout; |
670 | goto errout; |
662 | /* We can join only 2 zones with none existing inbetween */ |
671 | /* We can join only 2 zones with none existing inbetween */ |
663 | if (z2-z1 != 1) |
672 | if (z2-z1 != 1) |
664 | goto errout; |
673 | goto errout; |
665 | 674 | ||
666 | zone1 = zones.info[z1]; |
675 | zone1 = zones.info[z1]; |
667 | zone2 = zones.info[z2]; |
676 | zone2 = zones.info[z2]; |
668 | spinlock_lock(&zone1->lock); |
677 | spinlock_lock(&zone1->lock); |
669 | spinlock_lock(&zone2->lock); |
678 | spinlock_lock(&zone2->lock); |
670 | 679 | ||
671 | cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base)); |
680 | cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base)); |
672 | order = fnzb(cframes) + 1; |
681 | order = fnzb(cframes) + 1; |
673 | 682 | ||
674 | /* Allocate zonedata inside one of the zones */ |
683 | /* Allocate zonedata inside one of the zones */ |
675 | if (zone_can_alloc(zone1, order)) |
684 | if (zone_can_alloc(zone1, order)) |
676 | pfn = zone1->base + zone_frame_alloc(zone1, order); |
685 | pfn = zone1->base + zone_frame_alloc(zone1, order); |
677 | else if (zone_can_alloc(zone2, order)) |
686 | else if (zone_can_alloc(zone2, order)) |
678 | pfn = zone2->base + zone_frame_alloc(zone2, order); |
687 | pfn = zone2->base + zone_frame_alloc(zone2, order); |
679 | else |
688 | else |
680 | goto errout2; |
689 | goto errout2; |
681 | 690 | ||
682 | newzone = (zone_t *)PA2KA(PFN2ADDR(pfn)); |
691 | newzone = (zone_t *)PA2KA(PFN2ADDR(pfn)); |
683 | 692 | ||
684 | _zone_merge(newzone, zone1, zone2); |
693 | _zone_merge(newzone, zone1, zone2); |
685 | 694 | ||
686 | /* Free unneeded config frames */ |
695 | /* Free unneeded config frames */ |
687 | zone_reduce_region(newzone, pfn - newzone->base, cframes); |
696 | zone_reduce_region(newzone, pfn - newzone->base, cframes); |
688 | /* Subtract zone information from busy frames */ |
697 | /* Subtract zone information from busy frames */ |
689 | newzone->busy_count -= cframes; |
698 | newzone->busy_count -= cframes; |
690 | 699 | ||
691 | /* Replace existing zones in zoneinfo list */ |
700 | /* Replace existing zones in zoneinfo list */ |
692 | zones.info[z1] = newzone; |
701 | zones.info[z1] = newzone; |
693 | for (i = z2 + 1; i < zones.count; i++) |
702 | for (i = z2 + 1; i < zones.count; i++) |
694 | zones.info[i - 1] = zones.info[i]; |
703 | zones.info[i - 1] = zones.info[i]; |
695 | zones.count--; |
704 | zones.count--; |
696 | 705 | ||
697 | /* Free old zone information */ |
706 | /* Free old zone information */ |
698 | return_config_frames(newzone, zone1); |
707 | return_config_frames(newzone, zone1); |
699 | return_config_frames(newzone, zone2); |
708 | return_config_frames(newzone, zone2); |
700 | errout2: |
709 | errout2: |
701 | /* Nobody is allowed to enter to zone, so we are safe |
710 | /* Nobody is allowed to enter to zone, so we are safe |
702 | * to touch the spinlocks last time */ |
711 | * to touch the spinlocks last time */ |
703 | spinlock_unlock(&zone1->lock); |
712 | spinlock_unlock(&zone1->lock); |
704 | spinlock_unlock(&zone2->lock); |
713 | spinlock_unlock(&zone2->lock); |
705 | errout: |
714 | errout: |
706 | spinlock_unlock(&zones.lock); |
715 | spinlock_unlock(&zones.lock); |
707 | interrupts_restore(ipl); |
716 | interrupts_restore(ipl); |
708 | } |
717 | } |
709 | 718 | ||
710 | /** |
719 | /** |
711 | * Merge all zones into one big zone |
720 | * Merge all zones into one big zone |
712 | * |
721 | * |
713 | * It is reasonable to do this on systems whose bios reports parts in chunks, |
722 | * It is reasonable to do this on systems whose bios reports parts in chunks, |
714 | * so that we could have 1 zone (it's faster). |
723 | * so that we could have 1 zone (it's faster). |
715 | */ |
724 | */ |
716 | void zone_merge_all(void) |
725 | void zone_merge_all(void) |
717 | { |
726 | { |
718 | int count = zones.count; |
727 | int count = zones.count; |
719 | 728 | ||
720 | while (zones.count > 1 && --count) { |
729 | while (zones.count > 1 && --count) { |
721 | zone_merge(0,1); |
730 | zone_merge(0,1); |
722 | break; |
731 | break; |
723 | } |
732 | } |
724 | } |
733 | } |
725 | 734 | ||
726 | /** Create frame zone |
735 | /** Create frame zone |
727 | * |
736 | * |
728 | * Create new frame zone. |
737 | * Create new frame zone. |
729 | * |
738 | * |
730 | * @param start Physical address of the first frame within the zone. |
739 | * @param start Physical address of the first frame within the zone. |
731 | * @param size Size of the zone. Must be a multiple of FRAME_SIZE. |
740 | * @param size Size of the zone. Must be a multiple of FRAME_SIZE. |
732 | * @param conffram Address of configuration frame |
741 | * @param conffram Address of configuration frame |
733 | * @param flags Zone flags. |
742 | * @param flags Zone flags. |
734 | * |
743 | * |
735 | * @return Initialized zone. |
744 | * @return Initialized zone. |
736 | */ |
745 | */ |
737 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
746 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
738 | { |
747 | { |
739 | int i; |
748 | int i; |
740 | __u8 max_order; |
749 | __u8 max_order; |
741 | 750 | ||
742 | spinlock_initialize(&z->lock, "zone_lock"); |
751 | spinlock_initialize(&z->lock, "zone_lock"); |
743 | z->base = start; |
752 | z->base = start; |
744 | z->count = count; |
753 | z->count = count; |
745 | z->flags = flags; |
754 | z->flags = flags; |
746 | z->free_count = count; |
755 | z->free_count = count; |
747 | z->busy_count = 0; |
756 | z->busy_count = 0; |
748 | 757 | ||
749 | /* |
758 | /* |
750 | * Compute order for buddy system, initialize |
759 | * Compute order for buddy system, initialize |
751 | */ |
760 | */ |
752 | max_order = fnzb(count); |
761 | max_order = fnzb(count); |
753 | z->buddy_system = (buddy_system_t *)&z[1]; |
762 | z->buddy_system = (buddy_system_t *)&z[1]; |
754 | 763 | ||
755 | buddy_system_create(z->buddy_system, max_order, |
764 | buddy_system_create(z->buddy_system, max_order, |
756 | &zone_buddy_system_operations, |
765 | &zone_buddy_system_operations, |
757 | (void *) z); |
766 | (void *) z); |
758 | 767 | ||
759 | /* Allocate frames _after_ the conframe */ |
768 | /* Allocate frames _after_ the conframe */ |
760 | /* Check sizes */ |
769 | /* Check sizes */ |
761 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
770 | z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order)); |
762 | for (i = 0; i<count; i++) { |
771 | for (i = 0; i<count; i++) { |
763 | frame_initialize(&z->frames[i]); |
772 | frame_initialize(&z->frames[i]); |
764 | } |
773 | } |
765 | 774 | ||
766 | /* Stuffing frames */ |
775 | /* Stuffing frames */ |
767 | for (i = 0; i < count; i++) { |
776 | for (i = 0; i < count; i++) { |
768 | z->frames[i].refcount = 0; |
777 | z->frames[i].refcount = 0; |
769 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
778 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
770 | } |
779 | } |
771 | } |
780 | } |
772 | 781 | ||
773 | /** Compute configuration data size for zone */ |
782 | /** Compute configuration data size for zone */ |
774 | __address zone_conf_size(count_t count) |
783 | __address zone_conf_size(count_t count) |
775 | { |
784 | { |
776 | int size = sizeof(zone_t) + count*sizeof(frame_t); |
785 | int size = sizeof(zone_t) + count*sizeof(frame_t); |
777 | int max_order; |
786 | int max_order; |
778 | 787 | ||
779 | max_order = fnzb(count); |
788 | max_order = fnzb(count); |
780 | size += buddy_conf_size(max_order); |
789 | size += buddy_conf_size(max_order); |
781 | return size; |
790 | return size; |
782 | } |
791 | } |
783 | 792 | ||
784 | /** Create and add zone to system |
793 | /** Create and add zone to system |
785 | * |
794 | * |
786 | * @param confframe Where configuration frame is supposed to be. |
795 | * @param confframe Where configuration frame is supposed to be. |
787 | * Always check, that we will not disturb the kernel and possibly init. |
796 | * Always check, that we will not disturb the kernel and possibly init. |
788 | * If confframe is given _outside_ this zone, it is expected, |
797 | * If confframe is given _outside_ this zone, it is expected, |
789 | * that the area is already marked BUSY and big enough |
798 | * that the area is already marked BUSY and big enough |
790 | * to contain zone_conf_size() amount of data |
799 | * to contain zone_conf_size() amount of data |
791 | * |
800 | * |
792 | * @return Zone number or -1 on error |
801 | * @return Zone number or -1 on error |
793 | */ |
802 | */ |
794 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
803 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
795 | { |
804 | { |
796 | zone_t *z; |
805 | zone_t *z; |
797 | __address addr; |
806 | __address addr; |
798 | count_t confcount; |
807 | count_t confcount; |
799 | int i; |
808 | int i; |
800 | int znum; |
809 | int znum; |
801 | 810 | ||
802 | /* Theoretically we could have here 0, practically make sure |
811 | /* Theoretically we could have here 0, practically make sure |
803 | * nobody tries to do that. If some platform requires, remove |
812 | * nobody tries to do that. If some platform requires, remove |
804 | * the assert |
813 | * the assert |
805 | */ |
814 | */ |
806 | ASSERT(confframe); |
815 | ASSERT(confframe); |
807 | /* If conframe is supposed to be inside our zone, then make sure |
816 | /* If conframe is supposed to be inside our zone, then make sure |
808 | * it does not span kernel & init |
817 | * it does not span kernel & init |
809 | */ |
818 | */ |
810 | confcount = SIZE2FRAMES(zone_conf_size(count)); |
819 | confcount = SIZE2FRAMES(zone_conf_size(count)); |
811 | if (confframe >= start && confframe < start+count) { |
820 | if (confframe >= start && confframe < start+count) { |
812 | for (;confframe < start + count; confframe++) { |
821 | for (;confframe < start + count; confframe++) { |
813 | addr = PFN2ADDR(confframe); |
822 | addr = PFN2ADDR(confframe); |
814 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size)) |
823 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size)) |
815 | continue; |
824 | continue; |
816 | 825 | ||
817 | bool overlap = false; |
826 | bool overlap = false; |
818 | count_t i; |
827 | count_t i; |
819 | for (i = 0; i < init.cnt; i++) |
828 | for (i = 0; i < init.cnt; i++) |
820 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) { |
829 | if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) { |
821 | overlap = true; |
830 | overlap = true; |
822 | break; |
831 | break; |
823 | } |
832 | } |
824 | if (overlap) |
833 | if (overlap) |
825 | continue; |
834 | continue; |
826 | 835 | ||
827 | break; |
836 | break; |
828 | } |
837 | } |
829 | if (confframe >= start + count) |
838 | if (confframe >= start + count) |
830 | panic("Cannot find configuration data for zone."); |
839 | panic("Cannot find configuration data for zone."); |
831 | } |
840 | } |
832 | 841 | ||
833 | z = (zone_t *)PA2KA(PFN2ADDR(confframe)); |
842 | z = (zone_t *)PA2KA(PFN2ADDR(confframe)); |
834 | zone_construct(start, count, z, flags); |
843 | zone_construct(start, count, z, flags); |
835 | znum = zones_add_zone(z); |
844 | znum = zones_add_zone(z); |
836 | if (znum == -1) |
845 | if (znum == -1) |
837 | return -1; |
846 | return -1; |
838 | 847 | ||
839 | /* If confdata in zone, mark as unavailable */ |
848 | /* If confdata in zone, mark as unavailable */ |
840 | if (confframe >= start && confframe < start+count) |
849 | if (confframe >= start && confframe < start+count) |
841 | for (i=confframe; i<confframe+confcount; i++) { |
850 | for (i=confframe; i<confframe+confcount; i++) { |
842 | zone_mark_unavailable(z, i - z->base); |
851 | zone_mark_unavailable(z, i - z->base); |
843 | } |
852 | } |
844 | return znum; |
853 | return znum; |
845 | } |
854 | } |
846 | 855 | ||
847 | /***************************************/ |
856 | /***************************************/ |
848 | /* Frame functions */ |
857 | /* Frame functions */ |
849 | 858 | ||
850 | /** Set parent of frame */ |
859 | /** Set parent of frame */ |
851 | void frame_set_parent(pfn_t pfn, void *data, int hint) |
860 | void frame_set_parent(pfn_t pfn, void *data, int hint) |
852 | { |
861 | { |
853 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
862 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
854 | 863 | ||
855 | ASSERT(zone); |
864 | ASSERT(zone); |
856 | 865 | ||
857 | zone_get_frame(zone, pfn-zone->base)->parent = data; |
866 | zone_get_frame(zone, pfn-zone->base)->parent = data; |
858 | spinlock_unlock(&zone->lock); |
867 | spinlock_unlock(&zone->lock); |
859 | } |
868 | } |
860 | 869 | ||
861 | void * frame_get_parent(pfn_t pfn, int hint) |
870 | void * frame_get_parent(pfn_t pfn, int hint) |
862 | { |
871 | { |
863 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
872 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
864 | void *res; |
873 | void *res; |
865 | 874 | ||
866 | ASSERT(zone); |
875 | ASSERT(zone); |
867 | res = zone_get_frame(zone, pfn - zone->base)->parent; |
876 | res = zone_get_frame(zone, pfn - zone->base)->parent; |
868 | 877 | ||
869 | spinlock_unlock(&zone->lock); |
878 | spinlock_unlock(&zone->lock); |
870 | return res; |
879 | return res; |
871 | } |
880 | } |
872 | 881 | ||
873 | /** Allocate power-of-two frames of physical memory. |
882 | /** Allocate power-of-two frames of physical memory. |
874 | * |
883 | * |
875 | * @param flags Flags for host zone selection and address processing. |
884 | * @param flags Flags for host zone selection and address processing. |
876 | * @param order Allocate exactly 2^order frames. |
885 | * @param order Allocate exactly 2^order frames. |
877 | * @param pzone Preferred zone |
886 | * @param pzone Preferred zone |
878 | * |
887 | * |
879 | * @return Allocated frame. |
888 | * @return Allocated frame. |
880 | */ |
889 | */ |
881 | pfn_t frame_alloc_generic(__u8 order, int flags, int * status, int *pzone) |
890 | pfn_t frame_alloc_generic(__u8 order, int flags, int * status, int *pzone) |
882 | { |
891 | { |
883 | ipl_t ipl; |
892 | ipl_t ipl; |
884 | int freed; |
893 | int freed; |
885 | pfn_t v; |
894 | pfn_t v; |
886 | zone_t *zone; |
895 | zone_t *zone; |
887 | 896 | ||
888 | loop: |
897 | loop: |
889 | ipl = interrupts_disable(); |
898 | ipl = interrupts_disable(); |
890 | /* |
899 | /* |
891 | * First, find suitable frame zone. |
900 | * First, find suitable frame zone. |
892 | */ |
901 | */ |
893 | zone = find_free_zone_lock(order,pzone); |
902 | zone = find_free_zone_lock(order,pzone); |
894 | /* If no memory, reclaim some slab memory, |
903 | /* If no memory, reclaim some slab memory, |
895 | if it does not help, reclaim all */ |
904 | if it does not help, reclaim all */ |
896 | if (!zone && !(flags & FRAME_NO_RECLAIM)) { |
905 | if (!zone && !(flags & FRAME_NO_RECLAIM)) { |
897 | freed = slab_reclaim(0); |
906 | freed = slab_reclaim(0); |
898 | if (freed) |
907 | if (freed) |
899 | zone = find_free_zone_lock(order,pzone); |
908 | zone = find_free_zone_lock(order,pzone); |
900 | if (!zone) { |
909 | if (!zone) { |
901 | freed = slab_reclaim(SLAB_RECLAIM_ALL); |
910 | freed = slab_reclaim(SLAB_RECLAIM_ALL); |
902 | if (freed) |
911 | if (freed) |
903 | zone = find_free_zone_lock(order,pzone); |
912 | zone = find_free_zone_lock(order,pzone); |
904 | } |
913 | } |
905 | } |
914 | } |
906 | if (!zone) { |
915 | if (!zone) { |
907 | if (flags & FRAME_PANIC) |
916 | if (flags & FRAME_PANIC) |
908 | panic("Can't allocate frame.\n"); |
917 | panic("Can't allocate frame.\n"); |
909 | 918 | ||
910 | /* |
919 | /* |
911 | * TODO: Sleep until frames are available again. |
920 | * TODO: Sleep until frames are available again. |
912 | */ |
921 | */ |
913 | interrupts_restore(ipl); |
922 | interrupts_restore(ipl); |
914 | 923 | ||
915 | if (flags & FRAME_ATOMIC) { |
924 | if (flags & FRAME_ATOMIC) { |
916 | ASSERT(status != NULL); |
925 | ASSERT(status != NULL); |
917 | if (status) |
926 | if (status) |
918 | *status = FRAME_NO_MEMORY; |
927 | *status = FRAME_NO_MEMORY; |
919 | return NULL; |
928 | return NULL; |
920 | } |
929 | } |
921 | 930 | ||
922 | panic("Sleep not implemented.\n"); |
931 | panic("Sleep not implemented.\n"); |
923 | goto loop; |
932 | goto loop; |
924 | } |
933 | } |
925 | v = zone_frame_alloc(zone,order); |
934 | v = zone_frame_alloc(zone,order); |
926 | v += zone->base; |
935 | v += zone->base; |
927 | 936 | ||
928 | spinlock_unlock(&zone->lock); |
937 | spinlock_unlock(&zone->lock); |
929 | interrupts_restore(ipl); |
938 | interrupts_restore(ipl); |
930 | 939 | ||
931 | if (status) |
940 | if (status) |
932 | *status = FRAME_OK; |
941 | *status = FRAME_OK; |
933 | return v; |
942 | return v; |
934 | } |
943 | } |
935 | 944 | ||
936 | /** Free a frame. |
945 | /** Free a frame. |
937 | * |
946 | * |
938 | * Find respective frame structure for supplied addr. |
947 | * Find respective frame structure for supplied addr. |
939 | * Decrement frame reference count. |
948 | * Decrement frame reference count. |
940 | * If it drops to zero, move the frame structure to free list. |
949 | * If it drops to zero, move the frame structure to free list. |
941 | * |
950 | * |
942 | * @param frame Frame no to be freed. |
951 | * @param frame Frame no to be freed. |
943 | */ |
952 | */ |
944 | void frame_free(pfn_t pfn) |
953 | void frame_free(pfn_t pfn) |
945 | { |
954 | { |
946 | ipl_t ipl; |
955 | ipl_t ipl; |
947 | zone_t *zone; |
956 | zone_t *zone; |
948 | 957 | ||
949 | ipl = interrupts_disable(); |
958 | ipl = interrupts_disable(); |
950 | 959 | ||
951 | /* |
960 | /* |
952 | * First, find host frame zone for addr. |
961 | * First, find host frame zone for addr. |
953 | */ |
962 | */ |
954 | zone = find_zone_and_lock(pfn,NULL); |
963 | zone = find_zone_and_lock(pfn,NULL); |
955 | ASSERT(zone); |
964 | ASSERT(zone); |
956 | 965 | ||
957 | zone_frame_free(zone, pfn-zone->base); |
966 | zone_frame_free(zone, pfn-zone->base); |
958 | 967 | ||
959 | spinlock_unlock(&zone->lock); |
968 | spinlock_unlock(&zone->lock); |
960 | interrupts_restore(ipl); |
969 | interrupts_restore(ipl); |
961 | } |
970 | } |
962 | 971 | ||
963 | 972 | ||
964 | 973 | ||
965 | /** Mark given range unavailable in frame zones */ |
974 | /** Mark given range unavailable in frame zones */ |
966 | void frame_mark_unavailable(pfn_t start, count_t count) |
975 | void frame_mark_unavailable(pfn_t start, count_t count) |
967 | { |
976 | { |
968 | int i; |
977 | int i; |
969 | zone_t *zone; |
978 | zone_t *zone; |
970 | int prefzone = 0; |
979 | int prefzone = 0; |
971 | 980 | ||
972 | for (i=0; i < count; i++) { |
981 | for (i=0; i < count; i++) { |
973 | zone = find_zone_and_lock(start+i,&prefzone); |
982 | zone = find_zone_and_lock(start+i,&prefzone); |
974 | if (!zone) /* PFN not found */ |
983 | if (!zone) /* PFN not found */ |
975 | continue; |
984 | continue; |
976 | zone_mark_unavailable(zone, start+i-zone->base); |
985 | zone_mark_unavailable(zone, start+i-zone->base); |
977 | 986 | ||
978 | spinlock_unlock(&zone->lock); |
987 | spinlock_unlock(&zone->lock); |
979 | } |
988 | } |
980 | } |
989 | } |
981 | 990 | ||
982 | /** Initialize physical memory management |
991 | /** Initialize physical memory management |
983 | * |
992 | * |
984 | * Initialize physical memory managemnt. |
993 | * Initialize physical memory managemnt. |
985 | */ |
994 | */ |
986 | void frame_init(void) |
995 | void frame_init(void) |
987 | { |
996 | { |
988 | if (config.cpu_active == 1) { |
997 | if (config.cpu_active == 1) { |
989 | zones.count = 0; |
998 | zones.count = 0; |
990 | spinlock_initialize(&zones.lock,"zones_glob_lock"); |
999 | spinlock_initialize(&zones.lock,"zones_glob_lock"); |
991 | } |
1000 | } |
992 | /* Tell the architecture to create some memory */ |
1001 | /* Tell the architecture to create some memory */ |
993 | frame_arch_init(); |
1002 | frame_arch_init(); |
994 | if (config.cpu_active == 1) { |
1003 | if (config.cpu_active == 1) { |
995 | pfn_t firstframe = ADDR2PFN(KA2PA(config.base)); |
1004 | pfn_t firstframe = ADDR2PFN(KA2PA(config.base)); |
996 | pfn_t lastframe = ADDR2PFN(KA2PA(config.base+config.kernel_size)); |
1005 | pfn_t lastframe = ADDR2PFN(KA2PA(config.base+config.kernel_size)); |
997 | frame_mark_unavailable(firstframe,lastframe-firstframe+1); |
1006 | frame_mark_unavailable(firstframe,lastframe-firstframe+1); |
998 | 1007 | ||
999 | count_t i; |
1008 | count_t i; |
1000 | for (i = 0; i < init.cnt; i++) |
1009 | for (i = 0; i < init.cnt; i++) |
1001 | frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size)); |
1010 | frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size)); |
1002 | } |
1011 | } |
1003 | } |
1012 | } |
1004 | 1013 | ||
1005 | 1014 | ||
1006 | 1015 | ||
1007 | /** Prints list of zones |
1016 | /** Prints list of zones |
1008 | * |
1017 | * |
1009 | */ |
1018 | */ |
1010 | void zone_print_list(void) { |
1019 | void zone_print_list(void) { |
1011 | zone_t *zone = NULL; |
1020 | zone_t *zone = NULL; |
1012 | int i; |
1021 | int i; |
1013 | ipl_t ipl; |
1022 | ipl_t ipl; |
1014 | 1023 | ||
1015 | ipl = interrupts_disable(); |
1024 | ipl = interrupts_disable(); |
1016 | spinlock_lock(&zones.lock); |
1025 | spinlock_lock(&zones.lock); |
1017 | printf("# Base address\tFree Frames\tBusy Frames\n"); |
1026 | printf("# Base address\tFree Frames\tBusy Frames\n"); |
1018 | printf(" ------------\t-----------\t-----------\n"); |
1027 | printf(" ------------\t-----------\t-----------\n"); |
1019 | for (i = 0; i < zones.count; i++) { |
1028 | for (i = 0; i < zones.count; i++) { |
1020 | zone = zones.info[i]; |
1029 | zone = zones.info[i]; |
1021 | spinlock_lock(&zone->lock); |
1030 | spinlock_lock(&zone->lock); |
1022 | printf("%d: %L\t%d\t\t%d\n",i,PFN2ADDR(zone->base), |
1031 | printf("%d: %L\t%d\t\t%d\n",i,PFN2ADDR(zone->base), |
1023 | zone->free_count, zone->busy_count); |
1032 | zone->free_count, zone->busy_count); |
1024 | spinlock_unlock(&zone->lock); |
1033 | spinlock_unlock(&zone->lock); |
1025 | } |
1034 | } |
1026 | spinlock_unlock(&zones.lock); |
1035 | spinlock_unlock(&zones.lock); |
1027 | interrupts_restore(ipl); |
1036 | interrupts_restore(ipl); |
1028 | } |
1037 | } |
1029 | 1038 | ||
1030 | /** Prints zone details |
1039 | /** Prints zone details |
1031 | * |
1040 | * |
1032 | * @param base Zone base address OR zone number |
1041 | * @param base Zone base address OR zone number |
1033 | */ |
1042 | */ |
1034 | void zone_print_one(int num) { |
1043 | void zone_print_one(int num) { |
1035 | zone_t *zone = NULL; |
1044 | zone_t *zone = NULL; |
1036 | ipl_t ipl; |
1045 | ipl_t ipl; |
1037 | int i; |
1046 | int i; |
1038 | 1047 | ||
1039 | ipl = interrupts_disable(); |
1048 | ipl = interrupts_disable(); |
1040 | spinlock_lock(&zones.lock); |
1049 | spinlock_lock(&zones.lock); |
1041 | 1050 | ||
1042 | for (i = 0; i < zones.count; i++) { |
1051 | for (i = 0; i < zones.count; i++) { |
1043 | if (i == num || PFN2ADDR(zones.info[i]->base) == num) { |
1052 | if (i == num || PFN2ADDR(zones.info[i]->base) == num) { |
1044 | zone = zones.info[i]; |
1053 | zone = zones.info[i]; |
1045 | break; |
1054 | break; |
1046 | } |
1055 | } |
1047 | } |
1056 | } |
1048 | if (!zone) { |
1057 | if (!zone) { |
1049 | printf("Zone not found.\n"); |
1058 | printf("Zone not found.\n"); |
1050 | goto out; |
1059 | goto out; |
1051 | } |
1060 | } |
1052 | 1061 | ||
1053 | spinlock_lock(&zone->lock); |
1062 | spinlock_lock(&zone->lock); |
1054 | printf("Memory zone information\n"); |
1063 | printf("Memory zone information\n"); |
1055 | printf("Zone base address: %P\n", PFN2ADDR(zone->base)); |
1064 | printf("Zone base address: %P\n", PFN2ADDR(zone->base)); |
1056 | printf("Zone size: %d frames (%dK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10); |
1065 | printf("Zone size: %d frames (%dK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10); |
1057 | printf("Allocated space: %d frames (%dK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10); |
1066 | printf("Allocated space: %d frames (%dK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10); |
1058 | printf("Available space: %d (%dK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10); |
1067 | printf("Available space: %d (%dK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10); |
1059 | buddy_system_structure_print(zone->buddy_system, FRAME_SIZE); |
1068 | buddy_system_structure_print(zone->buddy_system, FRAME_SIZE); |
1060 | 1069 | ||
1061 | spinlock_unlock(&zone->lock); |
1070 | spinlock_unlock(&zone->lock); |
1062 | out: |
1071 | out: |
1063 | spinlock_unlock(&zones.lock); |
1072 | spinlock_unlock(&zones.lock); |
1064 | interrupts_restore(ipl); |
1073 | interrupts_restore(ipl); |
1065 | } |
1074 | } |
1066 | 1075 | ||
1067 | 1076 |