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