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