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