Rev 3057 | Rev 3187 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3057 | Rev 3185 | ||
|---|---|---|---|
| Line 127... | Line 127... | ||
| 127 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
127 | static index_t make_frame_index(zone_t *zone, frame_t *frame) |
| 128 | { |
128 | { |
| 129 | return (frame - zone->frames); |
129 | return (frame - zone->frames); |
| 130 | } |
130 | } |
| 131 | 131 | ||
| 132 | /** Initialize frame structure |
132 | /** Initialize frame structure. |
| 133 | * |
133 | * |
| 134 | * Initialize frame structure. |
- | |
| 135 | * |
- | |
| 136 | * @param frame Frame structure to be initialized. |
134 | * @param framei Frame structure to be initialized. |
| 137 | */ |
135 | */ |
| 138 | static void frame_initialize(frame_t *frame) |
136 | static void frame_initialize(frame_t *frame) |
| 139 | { |
137 | { |
| 140 | frame->refcount = 1; |
138 | frame->refcount = 1; |
| 141 | frame->buddy_order = 0; |
139 | frame->buddy_order = 0; |
| Line 143... | Line 141... | ||
| 143 | 141 | ||
| 144 | /**********************/ |
142 | /**********************/ |
| 145 | /* Zoneinfo functions */ |
143 | /* Zoneinfo functions */ |
| 146 | /**********************/ |
144 | /**********************/ |
| 147 | 145 | ||
| 148 | /** |
- | |
| 149 | * Insert-sort zone into zones list |
146 | /** Insert-sort zone into zones list. |
| 150 | * |
147 | * |
| 151 | * @param newzone New zone to be inserted into zone list |
148 | * @param newzone New zone to be inserted into zone list. |
| 152 | * @return zone number on success, -1 on error |
149 | * @return Zone number on success, -1 on error. |
| 153 | */ |
150 | */ |
| 154 | static int zones_add_zone(zone_t *newzone) |
151 | static int zones_add_zone(zone_t *newzone) |
| 155 | { |
152 | { |
| 156 | unsigned int i, j; |
153 | unsigned int i, j; |
| 157 | ipl_t ipl; |
154 | ipl_t ipl; |
| Line 169... | Line 166... | ||
| 169 | } |
166 | } |
| 170 | 167 | ||
| 171 | for (i = 0; i < zones.count; i++) { |
168 | for (i = 0; i < zones.count; i++) { |
| 172 | /* Check for overflow */ |
169 | /* Check for overflow */ |
| 173 | z = zones.info[i]; |
170 | z = zones.info[i]; |
| 174 | if (overlaps(newzone->base, newzone->count, z->base, z->count)) { |
171 | if (overlaps(newzone->base, newzone->count, z->base, |
| - | 172 | z->count)) { |
|
| 175 | printf("Zones overlap!\n"); |
173 | printf("Zones overlap!\n"); |
| 176 | return -1; |
174 | return -1; |
| 177 | } |
175 | } |
| 178 | if (newzone->base < z->base) |
176 | if (newzone->base < z->base) |
| 179 | break; |
177 | break; |
| Line 191... | Line 189... | ||
| 191 | 189 | ||
| 192 | return i; |
190 | return i; |
| 193 | } |
191 | } |
| 194 | 192 | ||
| 195 | /** |
193 | /** |
| 196 | * Try to find a zone where can we find the frame |
194 | * Try to find a zone where can we find the frame. |
| 197 | * |
195 | * |
| 198 | * Assume interrupts are disabled. |
196 | * Assume interrupts are disabled. |
| 199 | * |
197 | * |
| 200 | * @param frame Frame number contained in zone |
198 | * @param frame Frame number contained in zone. |
| 201 | * @param pzone If not null, it is used as zone hint. Zone index |
199 | * @param pzone If not null, it is used as zone hint. Zone index is |
| 202 | * is filled into the variable on success. |
200 | * filled into the variable on success. |
| 203 | * @return Pointer to locked zone containing frame |
201 | * @return Pointer to locked zone containing frame. |
| 204 | */ |
202 | */ |
| 205 | static zone_t * find_zone_and_lock(pfn_t frame, unsigned int *pzone) |
203 | static zone_t *find_zone_and_lock(pfn_t frame, unsigned int *pzone) |
| 206 | { |
204 | { |
| 207 | unsigned int i; |
205 | unsigned int i; |
| 208 | unsigned int hint = pzone ? *pzone : 0; |
206 | unsigned int hint = pzone ? *pzone : 0; |
| 209 | zone_t *z; |
207 | zone_t *z; |
| 210 | 208 | ||
| Line 243... | Line 241... | ||
| 243 | 241 | ||
| 244 | /** Find and lock zone that can allocate order frames. |
242 | /** Find and lock zone that can allocate order frames. |
| 245 | * |
243 | * |
| 246 | * Assume interrupts are disabled. |
244 | * Assume interrupts are disabled. |
| 247 | * |
245 | * |
| 248 | * @param order Size (2^order) of free space we are trying to find |
246 | * @param order Size (2^order) of free space we are trying to find. |
| 249 | * @param pzone Pointer to preferred zone or NULL, on return contains zone |
247 | * @param pzone Pointer to preferred zone or NULL, on return contains |
| 250 | * number |
248 | * zone number. |
| 251 | */ |
249 | */ |
| 252 | static zone_t * find_free_zone_and_lock(uint8_t order, unsigned int *pzone) |
250 | static zone_t *find_free_zone_and_lock(uint8_t order, unsigned int *pzone) |
| 253 | { |
251 | { |
| 254 | unsigned int i; |
252 | unsigned int i; |
| 255 | zone_t *z; |
253 | zone_t *z; |
| 256 | unsigned int hint = pzone ? *pzone : 0; |
254 | unsigned int hint = pzone ? *pzone : 0; |
| 257 | 255 | ||
| Line 281... | Line 279... | ||
| 281 | 279 | ||
| 282 | /**************************/ |
280 | /**************************/ |
| 283 | /* Buddy system functions */ |
281 | /* Buddy system functions */ |
| 284 | /**************************/ |
282 | /**************************/ |
| 285 | 283 | ||
| 286 | /** Buddy system find_block implementation |
284 | /** Buddy system find_block implementation. |
| 287 | * |
285 | * |
| 288 | * Find block that is parent of current list. |
286 | * Find block that is parent of current list. |
| 289 | * That means go to lower addresses, until such block is found |
287 | * That means go to lower addresses, until such block is found |
| 290 | * |
288 | * |
| 291 | * @param order - Order of parent must be different then this parameter!! |
289 | * @param order Order of parent must be different then this |
| - | 290 | * parameter!! |
|
| 292 | */ |
291 | */ |
| 293 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
292 | static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child, |
| 294 | uint8_t order) |
293 | uint8_t order) |
| 295 | { |
294 | { |
| 296 | frame_t *frame; |
295 | frame_t *frame; |
| Line 319... | Line 318... | ||
| 319 | zone = (zone_t *) b->data; |
318 | zone = (zone_t *) b->data; |
| 320 | index = frame_index(zone, frame); |
319 | index = frame_index(zone, frame); |
| 321 | printf("%" PRIi, index); |
320 | printf("%" PRIi, index); |
| 322 | } |
321 | } |
| 323 | 322 | ||
| 324 | /** Buddy system find_buddy implementation |
323 | /** Buddy system find_buddy implementation. |
| 325 | * |
324 | * |
| 326 | * @param b Buddy system. |
325 | * @param b Buddy system. |
| 327 | * @param block Block for which buddy should be found |
326 | * @param block Block for which buddy should be found. |
| 328 | * |
327 | * |
| 329 | * @return Buddy for given block if found |
328 | * @return Buddy for given block if found. |
| 330 | */ |
329 | */ |
| 331 | static link_t *zone_buddy_find_buddy(buddy_system_t *b, link_t *block) |
330 | static link_t *zone_buddy_find_buddy(buddy_system_t *b, link_t *block) |
| 332 | { |
331 | { |
| 333 | frame_t *frame; |
332 | frame_t *frame; |
| 334 | zone_t *zone; |
333 | zone_t *zone; |
| Line 343... | Line 342... | ||
| 343 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
342 | is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame); |
| 344 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
343 | is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame); |
| 345 | 344 | ||
| 346 | ASSERT(is_left ^ is_right); |
345 | ASSERT(is_left ^ is_right); |
| 347 | if (is_left) { |
346 | if (is_left) { |
| 348 | index = (frame_index(zone, frame)) + (1 << frame->buddy_order); |
347 | index = (frame_index(zone, frame)) + |
| - | 348 | (1 << frame->buddy_order); |
|
| 349 | } else { /* if (is_right) */ |
349 | } else { /* if (is_right) */ |
| 350 | index = (frame_index(zone, frame)) - (1 << frame->buddy_order); |
350 | index = (frame_index(zone, frame)) - |
| - | 351 | (1 << frame->buddy_order); |
|
| 351 | } |
352 | } |
| 352 | 353 | ||
| 353 | if (frame_index_valid(zone, index)) { |
354 | if (frame_index_valid(zone, index)) { |
| 354 | if (zone->frames[index].buddy_order == frame->buddy_order && |
355 | if (zone->frames[index].buddy_order == frame->buddy_order && |
| 355 | zone->frames[index].refcount == 0) { |
356 | zone->frames[index].refcount == 0) { |
| Line 358... | Line 359... | ||
| 358 | } |
359 | } |
| 359 | 360 | ||
| 360 | return NULL; |
361 | return NULL; |
| 361 | } |
362 | } |
| 362 | 363 | ||
| 363 | /** Buddy system bisect implementation |
364 | /** Buddy system bisect implementation. |
| 364 | * |
365 | * |
| 365 | * @param b Buddy system. |
366 | * @param b Buddy system. |
| 366 | * @param block Block to bisect |
367 | * @param block Block to bisect. |
| 367 | * |
368 | * |
| 368 | * @return right block |
369 | * @return Right block. |
| 369 | */ |
370 | */ |
| 370 | static link_t * zone_buddy_bisect(buddy_system_t *b, link_t *block) { |
371 | static link_t *zone_buddy_bisect(buddy_system_t *b, link_t *block) |
| - | 372 | { |
|
| 371 | frame_t *frame_l, *frame_r; |
373 | frame_t *frame_l, *frame_r; |
| 372 | 374 | ||
| 373 | frame_l = list_get_instance(block, frame_t, buddy_link); |
375 | frame_l = list_get_instance(block, frame_t, buddy_link); |
| 374 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
376 | frame_r = (frame_l + (1 << (frame_l->buddy_order - 1))); |
| 375 | 377 | ||
| 376 | return &frame_r->buddy_link; |
378 | return &frame_r->buddy_link; |
| 377 | } |
379 | } |
| 378 | 380 | ||
| 379 | /** Buddy system coalesce implementation |
381 | /** Buddy system coalesce implementation. |
| 380 | * |
382 | * |
| 381 | * @param b Buddy system. |
383 | * @param b Buddy system. |
| 382 | * @param block_1 First block |
384 | * @param block_1 First block. |
| 383 | * @param block_2 First block's buddy |
385 | * @param block_2 First block's buddy. |
| 384 | * |
386 | * |
| 385 | * @return Coalesced block (actually block that represents lower address) |
387 | * @return Coalesced block (actually block that represents lower |
| - | 388 | * address). |
|
| 386 | */ |
389 | */ |
| 387 | static link_t *zone_buddy_coalesce(buddy_system_t *b, link_t *block_1, |
390 | static link_t *zone_buddy_coalesce(buddy_system_t *b, link_t *block_1, |
| 388 | link_t *block_2) |
391 | link_t *block_2) |
| 389 | { |
392 | { |
| 390 | frame_t *frame1, *frame2; |
393 | frame_t *frame1, *frame2; |
| Line 393... | Line 396... | ||
| 393 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
396 | frame2 = list_get_instance(block_2, frame_t, buddy_link); |
| 394 | 397 | ||
| 395 | return frame1 < frame2 ? block_1 : block_2; |
398 | return frame1 < frame2 ? block_1 : block_2; |
| 396 | } |
399 | } |
| 397 | 400 | ||
| 398 | /** Buddy system set_order implementation |
401 | /** Buddy system set_order implementation. |
| 399 | * |
402 | * |
| 400 | * @param b Buddy system. |
403 | * @param b Buddy system. |
| 401 | * @param block Buddy system block |
404 | * @param block Buddy system block. |
| 402 | * @param order Order to set |
405 | * @param order Order to set. |
| 403 | */ |
406 | */ |
| 404 | static void zone_buddy_set_order(buddy_system_t *b, link_t *block, |
407 | static void zone_buddy_set_order(buddy_system_t *b, link_t *block, |
| 405 | uint8_t order) { |
408 | uint8_t order) |
| - | 409 | { |
|
| 406 | frame_t *frame; |
410 | frame_t *frame; |
| 407 | frame = list_get_instance(block, frame_t, buddy_link); |
411 | frame = list_get_instance(block, frame_t, buddy_link); |
| 408 | frame->buddy_order = order; |
412 | frame->buddy_order = order; |
| 409 | } |
413 | } |
| 410 | 414 | ||
| 411 | /** Buddy system get_order implementation |
415 | /** Buddy system get_order implementation. |
| 412 | * |
416 | * |
| 413 | * @param b Buddy system. |
417 | * @param b Buddy system. |
| 414 | * @param block Buddy system block |
418 | * @param block Buddy system block. |
| 415 | * |
419 | * |
| 416 | * @return Order of block |
420 | * @return Order of block. |
| 417 | */ |
421 | */ |
| 418 | static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t *block) { |
422 | static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t *block) |
| - | 423 | { |
|
| 419 | frame_t *frame; |
424 | frame_t *frame; |
| 420 | frame = list_get_instance(block, frame_t, buddy_link); |
425 | frame = list_get_instance(block, frame_t, buddy_link); |
| 421 | return frame->buddy_order; |
426 | return frame->buddy_order; |
| 422 | } |
427 | } |
| 423 | 428 | ||
| 424 | /** Buddy system mark_busy implementation |
429 | /** Buddy system mark_busy implementation. |
| 425 | * |
- | |
| 426 | * @param b Buddy system |
- | |
| 427 | * @param block Buddy system block |
- | |
| 428 | * |
430 | * |
| - | 431 | * @param b Buddy system. |
|
| - | 432 | * @param block Buddy system block. |
|
| 429 | */ |
433 | */ |
| 430 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) { |
434 | static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) |
| - | 435 | { |
|
| 431 | frame_t * frame; |
436 | frame_t * frame; |
| 432 | 437 | ||
| 433 | frame = list_get_instance(block, frame_t, buddy_link); |
438 | frame = list_get_instance(block, frame_t, buddy_link); |
| 434 | frame->refcount = 1; |
439 | frame->refcount = 1; |
| 435 | } |
440 | } |
| 436 | 441 | ||
| 437 | /** Buddy system mark_available implementation |
442 | /** Buddy system mark_available implementation. |
| 438 | * |
- | |
| 439 | * @param b Buddy system |
- | |
| 440 | * @param block Buddy system block |
- | |
| 441 | * |
443 | * |
| - | 444 | * @param b Buddy system. |
|
| - | 445 | * @param block Buddy system block. |
|
| 442 | */ |
446 | */ |
| 443 | static void zone_buddy_mark_available(buddy_system_t *b, link_t *block) { |
447 | static void zone_buddy_mark_available(buddy_system_t *b, link_t *block) |
| - | 448 | { |
|
| 444 | frame_t *frame; |
449 | frame_t *frame; |
| 445 | frame = list_get_instance(block, frame_t, buddy_link); |
450 | frame = list_get_instance(block, frame_t, buddy_link); |
| 446 | frame->refcount = 0; |
451 | frame->refcount = 0; |
| 447 | } |
452 | } |
| 448 | 453 | ||
| Line 460... | Line 465... | ||
| 460 | 465 | ||
| 461 | /******************/ |
466 | /******************/ |
| 462 | /* Zone functions */ |
467 | /* Zone functions */ |
| 463 | /******************/ |
468 | /******************/ |
| 464 | 469 | ||
| 465 | /** Allocate frame in particular zone |
470 | /** Allocate frame in particular zone. |
| 466 | * |
471 | * |
| 467 | * Assume zone is locked |
472 | * Assume zone is locked. |
| 468 | * Panics if allocation is impossible. |
473 | * Panics if allocation is impossible. |
| 469 | * |
474 | * |
| 470 | * @param zone Zone to allocate from. |
475 | * @param zone Zone to allocate from. |
| 471 | * @param order Allocate exactly 2^order frames. |
476 | * @param order Allocate exactly 2^order frames. |
| 472 | * |
477 | * |
| 473 | * @return Frame index in zone |
478 | * @return Frame index in zone. |
| 474 | * |
479 | * |
| 475 | */ |
480 | */ |
| 476 | static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order) |
481 | static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order) |
| 477 | { |
482 | { |
| 478 | pfn_t v; |
483 | pfn_t v; |
| Line 494... | Line 499... | ||
| 494 | /* get frame address */ |
499 | /* get frame address */ |
| 495 | v = make_frame_index(zone, frame); |
500 | v = make_frame_index(zone, frame); |
| 496 | return v; |
501 | return v; |
| 497 | } |
502 | } |
| 498 | 503 | ||
| 499 | /** Free frame from zone |
504 | /** Free frame from zone. |
| 500 | * |
505 | * |
| 501 | * Assume zone is locked |
506 | * Assume zone is locked. |
| 502 | * |
507 | * |
| 503 | * @param zone Pointer to zone from which the frame is to be freed |
508 | * @param zone Pointer to zone from which the frame is to be freed. |
| 504 | * @param frame_idx Frame index relative to zone |
509 | * @param frame_idx Frame index relative to zone. |
| 505 | */ |
510 | */ |
| 506 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
511 | static void zone_frame_free(zone_t *zone, index_t frame_idx) |
| 507 | { |
512 | { |
| 508 | frame_t *frame; |
513 | frame_t *frame; |
| 509 | uint8_t order; |
514 | uint8_t order; |
| Line 522... | Line 527... | ||
| 522 | zone->free_count += (1 << order); |
527 | zone->free_count += (1 << order); |
| 523 | zone->busy_count -= (1 << order); |
528 | zone->busy_count -= (1 << order); |
| 524 | } |
529 | } |
| 525 | } |
530 | } |
| 526 | 531 | ||
| 527 | /** Return frame from zone */ |
532 | /** Return frame from zone. */ |
| 528 | static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) |
533 | static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) |
| 529 | { |
534 | { |
| 530 | ASSERT(frame_idx < zone->count); |
535 | ASSERT(frame_idx < zone->count); |
| 531 | return &zone->frames[frame_idx]; |
536 | return &zone->frames[frame_idx]; |
| 532 | } |
537 | } |
| 533 | 538 | ||
| 534 | /** Mark frame in zone unavailable to allocation */ |
539 | /** Mark frame in zone unavailable to allocation. */ |
| 535 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
540 | static void zone_mark_unavailable(zone_t *zone, index_t frame_idx) |
| 536 | { |
541 | { |
| 537 | frame_t *frame; |
542 | frame_t *frame; |
| 538 | link_t *link; |
543 | link_t *link; |
| 539 | 544 | ||
| Line 544... | Line 549... | ||
| 544 | &frame->buddy_link); |
549 | &frame->buddy_link); |
| 545 | ASSERT(link); |
550 | ASSERT(link); |
| 546 | zone->free_count--; |
551 | zone->free_count--; |
| 547 | } |
552 | } |
| 548 | 553 | ||
| 549 | /** |
- | |
| 550 | * Join 2 zones |
554 | /** Join two zones. |
| 551 | * |
555 | * |
| 552 | * Expect zone_t *z to point to space at least zone_conf_size large |
556 | * Expect zone_t *z to point to space at least zone_conf_size large. |
| 553 | * |
557 | * |
| 554 | * Assume z1 & z2 are locked |
558 | * Assume z1 & z2 are locked. |
| 555 | * |
559 | * |
| 556 | * @param z Target zone structure pointer |
560 | * @param z Target zone structure pointer. |
| 557 | * @param z1 Zone to merge |
561 | * @param z1 Zone to merge. |
| 558 | * @param z2 Zone to merge |
562 | * @param z2 Zone to merge. |
| 559 | */ |
563 | */ |
| 560 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
564 | static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2) |
| 561 | { |
565 | { |
| 562 | uint8_t max_order; |
566 | uint8_t max_order; |
| 563 | unsigned int i; |
567 | unsigned int i; |
| Line 627... | Line 631... | ||
| 627 | frame->refcount = 0; |
631 | frame->refcount = 0; |
| 628 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
632 | buddy_system_free(z->buddy_system, &frame->buddy_link); |
| 629 | } |
633 | } |
| 630 | } |
634 | } |
| 631 | 635 | ||
| 632 | /** Return old configuration frames into the zone |
636 | /** Return old configuration frames into the zone. |
| 633 | * |
637 | * |
| 634 | * We have several cases |
638 | * We have several cases |
| 635 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
639 | * - the conf. data is outside of zone -> exit, shall we call frame_free?? |
| 636 | * - the conf. data was created by zone_create or |
640 | * - the conf. data was created by zone_create or |
| 637 | * updated with reduce_region -> free every frame |
641 | * updated with reduce_region -> free every frame |
| 638 | * |
642 | * |
| 639 | * @param newzone The actual zone where freeing should occur |
643 | * @param newzone The actual zone where freeing should occur. |
| 640 | * @param oldzone Pointer to old zone configuration data that should |
644 | * @param oldzone Pointer to old zone configuration data that should |
| 641 | * be freed from new zone |
645 | * be freed from new zone. |
| 642 | */ |
646 | */ |
| 643 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
647 | static void return_config_frames(zone_t *newzone, zone_t *oldzone) |
| 644 | { |
648 | { |
| 645 | pfn_t pfn; |
649 | pfn_t pfn; |
| 646 | frame_t *frame; |
650 | frame_t *frame; |
| Line 660... | Line 664... | ||
| 660 | newzone->busy_count++; |
664 | newzone->busy_count++; |
| 661 | zone_frame_free(newzone, pfn+i-newzone->base); |
665 | zone_frame_free(newzone, pfn+i-newzone->base); |
| 662 | } |
666 | } |
| 663 | } |
667 | } |
| 664 | 668 | ||
| 665 | /** Reduce allocated block to count of order 0 frames |
669 | /** Reduce allocated block to count of order 0 frames. |
| 666 | * |
670 | * |
| 667 | * The allocated block need 2^order frames of space. Reduce all frames |
671 | * 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 |
672 | * in block to order 0 and free the unneeded frames. This means, that |
| 669 | * when freeing the previously allocated block starting with frame_idx, |
673 | * when freeing the previously allocated block starting with frame_idx, |
| 670 | * you have to free every frame. |
674 | * you have to free every frame. |
| 671 | * |
675 | * |
| 672 | * @param zone |
676 | * @param zone |
| 673 | * @param frame_idx Index to block |
677 | * @param frame_idx Index to block. |
| 674 | * @param count Allocated space in block |
678 | * @param count Allocated space in block. |
| 675 | */ |
679 | */ |
| 676 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
680 | static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count) |
| 677 | { |
681 | { |
| 678 | count_t i; |
682 | count_t i; |
| 679 | uint8_t order; |
683 | uint8_t order; |
| Line 696... | Line 700... | ||
| 696 | for (i = count; i < (count_t) (1 << order); i++) { |
700 | for (i = count; i < (count_t) (1 << order); i++) { |
| 697 | zone_frame_free(zone, i + frame_idx); |
701 | zone_frame_free(zone, i + frame_idx); |
| 698 | } |
702 | } |
| 699 | } |
703 | } |
| 700 | 704 | ||
| 701 | /** Merge zones z1 and z2 |
705 | /** Merge zones z1 and z2. |
| 702 | * |
706 | * |
| 703 | * - the zones must be 2 zones with no zone existing in between, |
707 | * - the zones must be 2 zones with no zone existing in between, |
| 704 | * which means that z2 = z1+1 |
708 | * which means that z2 = z1+1 |
| 705 | * |
709 | * |
| 706 | * - When you create a new zone, the frame allocator configuration does |
710 | * - When you create a new zone, the frame allocator configuration does |
| Line 771... | Line 775... | ||
| 771 | errout: |
775 | errout: |
| 772 | spinlock_unlock(&zones.lock); |
776 | spinlock_unlock(&zones.lock); |
| 773 | interrupts_restore(ipl); |
777 | interrupts_restore(ipl); |
| 774 | } |
778 | } |
| 775 | 779 | ||
| 776 | /** |
- | |
| 777 | * Merge all zones into one big zone |
780 | /** Merge all zones into one big zone. |
| 778 | * |
781 | * |
| 779 | * It is reasonable to do this on systems whose bios reports parts in chunks, |
782 | * 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). |
783 | * so that we could have 1 zone (it's faster). |
| 781 | */ |
784 | */ |
| 782 | void zone_merge_all(void) |
785 | void zone_merge_all(void) |
| Line 787... | Line 790... | ||
| 787 | zone_merge(0,1); |
790 | zone_merge(0,1); |
| 788 | break; |
791 | break; |
| 789 | } |
792 | } |
| 790 | } |
793 | } |
| 791 | 794 | ||
| 792 | /** Create frame zone |
- | |
| 793 | * |
- | |
| 794 | * Create new frame zone. |
795 | /** Create new frame zone. |
| 795 | * |
796 | * |
| 796 | * @param start Physical address of the first frame within the zone. |
797 | * @param start Physical address of the first frame within the zone. |
| 797 | * @param count Count of frames in zone |
798 | * @param count Count of frames in zone. |
| 798 | * @param z Address of configuration information of zone |
799 | * @param z Address of configuration information of zone. |
| 799 | * @param flags Zone flags. |
800 | * @param flags Zone flags. |
| 800 | * |
801 | * |
| 801 | * @return Initialized zone. |
802 | * @return Initialized zone. |
| 802 | */ |
803 | */ |
| 803 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
804 | static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags) |
| 804 | { |
805 | { |
| 805 | unsigned int i; |
806 | unsigned int i; |
| 806 | uint8_t max_order; |
807 | uint8_t max_order; |
| Line 817... | Line 818... | ||
| 817 | */ |
818 | */ |
| 818 | max_order = fnzb(count); |
819 | max_order = fnzb(count); |
| 819 | z->buddy_system = (buddy_system_t *)&z[1]; |
820 | z->buddy_system = (buddy_system_t *)&z[1]; |
| 820 | 821 | ||
| 821 | buddy_system_create(z->buddy_system, max_order, |
822 | buddy_system_create(z->buddy_system, max_order, |
| 822 | &zone_buddy_system_operations, |
823 | &zone_buddy_system_operations, (void *) z); |
| 823 | (void *) z); |
- | |
| 824 | 824 | ||
| 825 | /* Allocate frames _after_ the conframe */ |
825 | /* Allocate frames _after_ the conframe */ |
| 826 | /* Check sizes */ |
826 | /* Check sizes */ |
| 827 | z->frames = (frame_t *)((uint8_t *) z->buddy_system + |
827 | z->frames = (frame_t *)((uint8_t *) z->buddy_system + |
| 828 | buddy_conf_size(max_order)); |
828 | buddy_conf_size(max_order)); |
| Line 835... | Line 835... | ||
| 835 | z->frames[i].refcount = 0; |
835 | z->frames[i].refcount = 0; |
| 836 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
836 | buddy_system_free(z->buddy_system, &z->frames[i].buddy_link); |
| 837 | } |
837 | } |
| 838 | } |
838 | } |
| 839 | 839 | ||
| 840 | /** Compute configuration data size for zone |
840 | /** Compute configuration data size for zone. |
| 841 | * |
841 | * |
| 842 | * @param count Size of zone in frames |
842 | * @param count Size of zone in frames. |
| 843 | * @return Size of zone configuration info (in bytes) |
843 | * @return Size of zone configuration info (in bytes). |
| 844 | */ |
844 | */ |
| 845 | uintptr_t zone_conf_size(count_t count) |
845 | uintptr_t zone_conf_size(count_t count) |
| 846 | { |
846 | { |
| 847 | int size = sizeof(zone_t) + count * sizeof(frame_t); |
847 | int size = sizeof(zone_t) + count * sizeof(frame_t); |
| 848 | int max_order; |
848 | int max_order; |
| Line 850... | Line 850... | ||
| 850 | max_order = fnzb(count); |
850 | max_order = fnzb(count); |
| 851 | size += buddy_conf_size(max_order); |
851 | size += buddy_conf_size(max_order); |
| 852 | return size; |
852 | return size; |
| 853 | } |
853 | } |
| 854 | 854 | ||
| 855 | /** Create and add zone to system |
855 | /** Create and add zone to system. |
| 856 | * |
856 | * |
| 857 | * @param start First frame number (absolute) |
857 | * @param start First frame number (absolute). |
| 858 | * @param count Size of zone in frames |
858 | * @param count Size of zone in frames. |
| 859 | * @param confframe Where configuration frames are supposed to be. |
859 | * @param confframe Where configuration frames are supposed to be. |
| 860 | * Automatically checks, that we will not disturb the |
860 | * Automatically checks, that we will not disturb the |
| 861 | * kernel and possibly init. |
861 | * kernel and possibly init. If confframe is given |
| 862 | * If confframe is given _outside_ this zone, it is expected, |
862 | * _outside_ this zone, it is expected, that the area is |
| 863 | * that the area is already marked BUSY and big enough |
863 | * already marked BUSY and big enough to contain |
| 864 | * to contain zone_conf_size() amount of data. |
864 | * zone_conf_size() amount of data. If the confframe is |
| 865 | * If the confframe is inside the area, the zone free frame |
865 | * inside the area, the zone free frame information is |
| 866 | * information is modified not to include it. |
866 | * modified not to include it. |
| 867 | * |
867 | * |
| 868 | * @return Zone number or -1 on error |
868 | * @return Zone number or -1 on error. |
| 869 | */ |
869 | */ |
| 870 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
870 | int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags) |
| 871 | { |
871 | { |
| 872 | zone_t *z; |
872 | zone_t *z; |
| 873 | uintptr_t addr; |
873 | uintptr_t addr; |
| Line 928... | Line 928... | ||
| 928 | } |
928 | } |
| 929 | 929 | ||
| 930 | /***************************************/ |
930 | /***************************************/ |
| 931 | /* Frame functions */ |
931 | /* Frame functions */ |
| 932 | 932 | ||
| 933 | /** Set parent of frame */ |
933 | /** Set parent of frame. */ |
| 934 | void frame_set_parent(pfn_t pfn, void *data, unsigned int hint) |
934 | void frame_set_parent(pfn_t pfn, void *data, unsigned int hint) |
| 935 | { |
935 | { |
| 936 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
936 | zone_t *zone = find_zone_and_lock(pfn, &hint); |
| 937 | 937 | ||
| 938 | ASSERT(zone); |
938 | ASSERT(zone); |
| Line 953... | Line 953... | ||
| 953 | return res; |
953 | return res; |
| 954 | } |
954 | } |
| 955 | 955 | ||
| 956 | /** Allocate power-of-two frames of physical memory. |
956 | /** Allocate power-of-two frames of physical memory. |
| 957 | * |
957 | * |
| 958 | * @param order Allocate exactly 2^order frames. |
958 | * @param order Allocate exactly 2^order frames. |
| 959 | * @param flags Flags for host zone selection and address processing. |
959 | * @param flags Flags for host zone selection and address processing. |
| 960 | * @param pzone Preferred zone |
960 | * @param pzone Preferred zone. |
| 961 | * |
961 | * |
| 962 | * @return Physical address of the allocated frame. |
962 | * @return Physical address of the allocated frame. |
| 963 | * |
963 | * |
| 964 | */ |
964 | */ |
| 965 | void * frame_alloc_generic(uint8_t order, int flags, unsigned int *pzone) |
965 | void *frame_alloc_generic(uint8_t order, int flags, unsigned int *pzone) |
| 966 | { |
966 | { |
| 967 | ipl_t ipl; |
967 | ipl_t ipl; |
| 968 | int freed; |
968 | int freed; |
| 969 | pfn_t v; |
969 | pfn_t v; |
| 970 | zone_t *zone; |
970 | zone_t *zone; |
| Line 1017... | Line 1017... | ||
| 1017 | * |
1017 | * |
| 1018 | * Find respective frame structure for supplied physical frame address. |
1018 | * Find respective frame structure for supplied physical frame address. |
| 1019 | * Decrement frame reference count. |
1019 | * Decrement frame reference count. |
| 1020 | * If it drops to zero, move the frame structure to free list. |
1020 | * If it drops to zero, move the frame structure to free list. |
| 1021 | * |
1021 | * |
| 1022 | * @param Frame Physical Address of of the frame to be freed. |
1022 | * @param frame Physical Address of of the frame to be freed. |
| 1023 | */ |
1023 | */ |
| 1024 | void frame_free(uintptr_t frame) |
1024 | void frame_free(uintptr_t frame) |
| 1025 | { |
1025 | { |
| 1026 | ipl_t ipl; |
1026 | ipl_t ipl; |
| 1027 | zone_t *zone; |
1027 | zone_t *zone; |
| Line 1044... | Line 1044... | ||
| 1044 | /** Add reference to frame. |
1044 | /** Add reference to frame. |
| 1045 | * |
1045 | * |
| 1046 | * Find respective frame structure for supplied PFN and |
1046 | * Find respective frame structure for supplied PFN and |
| 1047 | * increment frame reference count. |
1047 | * increment frame reference count. |
| 1048 | * |
1048 | * |
| 1049 | * @param pfn Frame number of the frame to be freed. |
1049 | * @param pfn Frame number of the frame to be freed. |
| 1050 | */ |
1050 | */ |
| 1051 | void frame_reference_add(pfn_t pfn) |
1051 | void frame_reference_add(pfn_t pfn) |
| 1052 | { |
1052 | { |
| 1053 | ipl_t ipl; |
1053 | ipl_t ipl; |
| 1054 | zone_t *zone; |
1054 | zone_t *zone; |
| Line 1057... | Line 1057... | ||
| 1057 | ipl = interrupts_disable(); |
1057 | ipl = interrupts_disable(); |
| 1058 | 1058 | ||
| 1059 | /* |
1059 | /* |
| 1060 | * First, find host frame zone for addr. |
1060 | * First, find host frame zone for addr. |
| 1061 | */ |
1061 | */ |
| 1062 | zone = find_zone_and_lock(pfn,NULL); |
1062 | zone = find_zone_and_lock(pfn, NULL); |
| 1063 | ASSERT(zone); |
1063 | ASSERT(zone); |
| 1064 | 1064 | ||
| 1065 | frame = &zone->frames[pfn-zone->base]; |
1065 | frame = &zone->frames[pfn - zone->base]; |
| 1066 | frame->refcount++; |
1066 | frame->refcount++; |
| 1067 | 1067 | ||
| 1068 | spinlock_unlock(&zone->lock); |
1068 | spinlock_unlock(&zone->lock); |
| 1069 | interrupts_restore(ipl); |
1069 | interrupts_restore(ipl); |
| 1070 | } |
1070 | } |
| 1071 | 1071 | ||
| 1072 | /** Mark given range unavailable in frame zones */ |
1072 | /** Mark given range unavailable in frame zones. */ |
| 1073 | void frame_mark_unavailable(pfn_t start, count_t count) |
1073 | void frame_mark_unavailable(pfn_t start, count_t count) |
| 1074 | { |
1074 | { |
| 1075 | unsigned int i; |
1075 | unsigned int i; |
| 1076 | zone_t *zone; |
1076 | zone_t *zone; |
| 1077 | unsigned int prefzone = 0; |
1077 | unsigned int prefzone = 0; |
| Line 1084... | Line 1084... | ||
| 1084 | 1084 | ||
| 1085 | spinlock_unlock(&zone->lock); |
1085 | spinlock_unlock(&zone->lock); |
| 1086 | } |
1086 | } |
| 1087 | } |
1087 | } |
| 1088 | 1088 | ||
| 1089 | /** Initialize physical memory management |
1089 | /** Initialize physical memory management. */ |
| 1090 | * |
- | |
| 1091 | * Initialize physical memory managemnt. |
- | |
| 1092 | */ |
- | |
| 1093 | void frame_init(void) |
1090 | void frame_init(void) |
| 1094 | { |
1091 | { |
| 1095 | if (config.cpu_active == 1) { |
1092 | if (config.cpu_active == 1) { |
| 1096 | zones.count = 0; |
1093 | zones.count = 0; |
| 1097 | spinlock_initialize(&zones.lock, "zones.lock"); |
1094 | spinlock_initialize(&zones.lock, "zones.lock"); |
| Line 1120... | Line 1117... | ||
| 1120 | frame_mark_unavailable(0, 1); |
1117 | frame_mark_unavailable(0, 1); |
| 1121 | } |
1118 | } |
| 1122 | } |
1119 | } |
| 1123 | 1120 | ||
| 1124 | 1121 | ||
| 1125 | /** Return total size of all zones |
1122 | /** Return total size of all zones. */ |
| 1126 | * |
- | |
| 1127 | */ |
- | |
| 1128 | uint64_t zone_total_size(void) { |
1123 | uint64_t zone_total_size(void) |
| - | 1124 | { |
|
| 1129 | zone_t *zone = NULL; |
1125 | zone_t *zone = NULL; |
| 1130 | unsigned int i; |
1126 | unsigned int i; |
| 1131 | ipl_t ipl; |
1127 | ipl_t ipl; |
| 1132 | uint64_t total = 0; |
1128 | uint64_t total = 0; |
| 1133 | 1129 | ||
| Line 1145... | Line 1141... | ||
| 1145 | interrupts_restore(ipl); |
1141 | interrupts_restore(ipl); |
| 1146 | 1142 | ||
| 1147 | return total; |
1143 | return total; |
| 1148 | } |
1144 | } |
| 1149 | 1145 | ||
| 1150 | - | ||
| 1151 | - | ||
| 1152 | /** Prints list of zones |
1146 | /** Prints list of zones. */ |
| 1153 | * |
- | |
| 1154 | */ |
- | |
| 1155 | void zone_print_list(void) { |
1147 | void zone_print_list(void) |
| - | 1148 | { |
|
| 1156 | zone_t *zone = NULL; |
1149 | zone_t *zone = NULL; |
| 1157 | unsigned int i; |
1150 | unsigned int i; |
| 1158 | ipl_t ipl; |
1151 | ipl_t ipl; |
| 1159 | 1152 | ||
| 1160 | ipl = interrupts_disable(); |
1153 | ipl = interrupts_disable(); |
| Line 1173... | Line 1166... | ||
| 1173 | for (i = 0; i < zones.count; i++) { |
1166 | for (i = 0; i < zones.count; i++) { |
| 1174 | zone = zones.info[i]; |
1167 | zone = zones.info[i]; |
| 1175 | spinlock_lock(&zone->lock); |
1168 | spinlock_lock(&zone->lock); |
| 1176 | 1169 | ||
| 1177 | #ifdef __32_BITS__ |
1170 | #ifdef __32_BITS__ |
| 1178 | printf("%-2u %10p %12" PRIc " %12" PRIc "\n", i, PFN2ADDR(zone->base), |
1171 | printf("%-2u %10p %12" PRIc " %12" PRIc "\n", |
| - | 1172 | i, PFN2ADDR(zone->base), zone->free_count, |
|
| 1179 | zone->free_count, zone->busy_count); |
1173 | zone->busy_count); |
| 1180 | #endif |
1174 | #endif |
| 1181 | 1175 | ||
| 1182 | #ifdef __64_BITS__ |
1176 | #ifdef __64_BITS__ |
| 1183 | printf("%-2u %18p %12" PRIc " %12" PRIc "\n", i, PFN2ADDR(zone->base), |
1177 | printf("%-2u %18p %12" PRIc " %12" PRIc "\n", i, |
| 1184 | zone->free_count, zone->busy_count); |
1178 | PFN2ADDR(zone->base), zone->free_count, zone->busy_count); |
| 1185 | #endif |
1179 | #endif |
| 1186 | 1180 | ||
| 1187 | spinlock_unlock(&zone->lock); |
1181 | spinlock_unlock(&zone->lock); |
| 1188 | } |
1182 | } |
| 1189 | 1183 | ||
| Line 1191... | Line 1185... | ||
| 1191 | interrupts_restore(ipl); |
1185 | interrupts_restore(ipl); |
| 1192 | } |
1186 | } |
| 1193 | 1187 | ||
| 1194 | /** Prints zone details. |
1188 | /** Prints zone details. |
| 1195 | * |
1189 | * |
| 1196 | * @param num Zone base address or zone number. |
1190 | * @param num Zone base address or zone number. |
| 1197 | */ |
1191 | */ |
| 1198 | void zone_print_one(unsigned int num) { |
1192 | void zone_print_one(unsigned int num) |
| - | 1193 | { |
|
| 1199 | zone_t *zone = NULL; |
1194 | zone_t *zone = NULL; |
| 1200 | ipl_t ipl; |
1195 | ipl_t ipl; |
| 1201 | unsigned int i; |
1196 | unsigned int i; |
| 1202 | 1197 | ||
| 1203 | ipl = interrupts_disable(); |
1198 | ipl = interrupts_disable(); |
| Line 1216... | Line 1211... | ||
| 1216 | 1211 | ||
| 1217 | spinlock_lock(&zone->lock); |
1212 | spinlock_lock(&zone->lock); |
| 1218 | printf("Memory zone information\n"); |
1213 | printf("Memory zone information\n"); |
| 1219 | printf("Zone base address: %p\n", PFN2ADDR(zone->base)); |
1214 | printf("Zone base address: %p\n", PFN2ADDR(zone->base)); |
| 1220 | printf("Zone size: %" PRIc " frames (%" PRIs " KB)\n", zone->count, |
1215 | printf("Zone size: %" PRIc " frames (%" PRIs " KB)\n", zone->count, |
| 1221 | SIZE2KB(FRAMES2SIZE(zone->count))); |
1216 | SIZE2KB(FRAMES2SIZE(zone->count))); |
| 1222 | printf("Allocated space: %" PRIc " frames (%" PRIs " KB)\n", zone->busy_count, |
1217 | printf("Allocated space: %" PRIc " frames (%" PRIs " KB)\n", |
| 1223 | SIZE2KB(FRAMES2SIZE(zone->busy_count))); |
1218 | zone->busy_count, SIZE2KB(FRAMES2SIZE(zone->busy_count))); |
| 1224 | printf("Available space: %" PRIc " frames (%" PRIs " KB)\n", zone->free_count, |
1219 | printf("Available space: %" PRIc " frames (%" PRIs " KB)\n", |
| 1225 | SIZE2KB(FRAMES2SIZE(zone->free_count))); |
1220 | zone->free_count, SIZE2KB(FRAMES2SIZE(zone->free_count))); |
| 1226 | buddy_system_structure_print(zone->buddy_system, FRAME_SIZE); |
1221 | buddy_system_structure_print(zone->buddy_system, FRAME_SIZE); |
| 1227 | spinlock_unlock(&zone->lock); |
1222 | spinlock_unlock(&zone->lock); |
| 1228 | 1223 | ||
| 1229 | out: |
1224 | out: |
| 1230 | spinlock_unlock(&zones.lock); |
1225 | spinlock_unlock(&zones.lock); |