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