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