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