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