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