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