Rev 430 | Rev 480 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 430 | Rev 479 | ||
---|---|---|---|
Line 39... | Line 39... | ||
39 | #include <arch.h> |
39 | #include <arch.h> |
40 | 40 | ||
41 | spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
41 | spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
42 | link_t zone_head; /**< list of all zones in the system */ |
42 | link_t zone_head; /**< list of all zones in the system */ |
43 | 43 | ||
- | 44 | static struct buddy_system_operations zone_buddy_system_operations = { |
|
- | 45 | .find_buddy = zone_buddy_find_buddy, |
|
- | 46 | .bisect = zone_buddy_bisect, |
|
- | 47 | .coalesce = zone_buddy_coalesce, |
|
- | 48 | .set_order = zone_buddy_set_order, |
|
- | 49 | .get_order = zone_buddy_get_order, |
|
- | 50 | }; |
|
- | 51 | ||
44 | /** Initialize physical memory management |
52 | /** Initialize physical memory management |
45 | * |
53 | * |
46 | * Initialize physical memory managemnt. |
54 | * Initialize physical memory managemnt. |
47 | */ |
55 | */ |
48 | void frame_init(void) |
56 | void frame_init(void) |
Line 116... | Line 124... | ||
116 | frame->refcount++; |
124 | frame->refcount++; |
117 | list_remove(tmp); /* remove frame from free_head */ |
125 | list_remove(tmp); /* remove frame from free_head */ |
118 | zone->free_count--; |
126 | zone->free_count--; |
119 | zone->busy_count++; |
127 | zone->busy_count++; |
120 | 128 | ||
121 | v = zone->base + (frame - zone->frames) * FRAME_SIZE; |
129 | //v = zone->base + (frame - zone->frames) * FRAME_SIZE; |
- | 130 | v = FRAME2ADDR(zone, frame); |
|
122 | 131 | ||
123 | if (flags & FRAME_KA) |
132 | if (flags & FRAME_KA) |
124 | v = PA2KA(v); |
133 | v = PA2KA(v); |
125 | 134 | ||
126 | spinlock_unlock(&zone->lock); |
135 | spinlock_unlock(&zone->lock); |
Line 173... | Line 182... | ||
173 | spinlock_unlock(&z->lock); |
182 | spinlock_unlock(&z->lock); |
174 | } |
183 | } |
175 | 184 | ||
176 | ASSERT(zone != NULL); |
185 | ASSERT(zone != NULL); |
177 | 186 | ||
- | 187 | frame = ADDR2FRAME(zone, addr); |
|
178 | frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
188 | // frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
179 | ASSERT(frame->refcount); |
189 | ASSERT(frame->refcount); |
180 | 190 | ||
181 | if (!--frame->refcount) { |
191 | if (!--frame->refcount) { |
182 | list_append(&frame->link, &zone->free_head); /* append frame to free_head */ |
192 | list_append(&frame->link, &zone->free_head); /* append frame to free_head */ |
183 | zone->free_count++; |
193 | zone->free_count++; |
Line 231... | Line 241... | ||
231 | spinlock_unlock(&z->lock); |
241 | spinlock_unlock(&z->lock); |
232 | } |
242 | } |
233 | 243 | ||
234 | ASSERT(zone != NULL); |
244 | ASSERT(zone != NULL); |
235 | 245 | ||
236 | frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
246 | //frame = &zone->frames[(addr - zone->base)/FRAME_SIZE]; |
- | 247 | frame = ADDR2FRAME(zone, addr); |
|
237 | 248 | ||
238 | if (!frame->refcount) { |
249 | if (!frame->refcount) { |
239 | frame->refcount++; |
250 | frame->refcount++; |
240 | 251 | ||
241 | list_remove(&frame->link); /* remove frame from free_head */ |
252 | list_remove(&frame->link); /* remove frame from free_head */ |
Line 290... | Line 301... | ||
290 | zone_t *zone_create(__address start, size_t size, int flags) |
301 | zone_t *zone_create(__address start, size_t size, int flags) |
291 | { |
302 | { |
292 | zone_t *z; |
303 | zone_t *z; |
293 | count_t cnt; |
304 | count_t cnt; |
294 | int i; |
305 | int i; |
- | 306 | __u8 max_order; |
|
295 | 307 | ||
296 | ASSERT(start % FRAME_SIZE == 0); |
308 | ASSERT(start % FRAME_SIZE == 0); |
297 | ASSERT(size % FRAME_SIZE == 0); |
309 | ASSERT(size % FRAME_SIZE == 0); |
298 | 310 | ||
299 | cnt = size / FRAME_SIZE; |
311 | cnt = size / FRAME_SIZE; |
Line 320... | Line 332... | ||
320 | for (i = 0; i<cnt; i++) { |
332 | for (i = 0; i<cnt; i++) { |
321 | frame_initialize(&z->frames[i], z); |
333 | frame_initialize(&z->frames[i], z); |
322 | list_append(&z->frames[i].link, &z->free_head); |
334 | list_append(&z->frames[i].link, &z->free_head); |
323 | } |
335 | } |
324 | 336 | ||
- | 337 | /* |
|
- | 338 | * Create buddy system for the zone |
|
- | 339 | */ |
|
- | 340 | for (max_order = 0; cnt >> max_order; max_order++); |
|
- | 341 | z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations); |
|
325 | } |
342 | } |
326 | 343 | ||
327 | return z; |
344 | return z; |
328 | } |
345 | } |
329 | 346 | ||
Line 356... | Line 373... | ||
356 | void frame_initialize(frame_t *frame, zone_t *zone) |
373 | void frame_initialize(frame_t *frame, zone_t *zone) |
357 | { |
374 | { |
358 | frame->refcount = 0; |
375 | frame->refcount = 0; |
359 | link_initialize(&frame->link); |
376 | link_initialize(&frame->link); |
360 | } |
377 | } |
- | 378 | ||
- | 379 | ||
- | 380 | ||
- | 381 | /* |
|
- | 382 | * buddy system functions (under construction) |
|
- | 383 | * |
|
- | 384 | */ |
|
- | 385 | ||
- | 386 | ||
- | 387 | /** Allocate 2^order frames |
|
- | 388 | * |
|
- | 389 | */ |
|
- | 390 | __address zone_buddy_frame_alloc(int flags, __u8 order) { |
|
- | 391 | ipl_t ipl; |
|
- | 392 | link_t *cur, *tmp; |
|
- | 393 | zone_t *z; |
|
- | 394 | zone_t *zone = NULL; |
|
- | 395 | frame_t *frame = NULL; |
|
- | 396 | __address v; |
|
- | 397 | ||
- | 398 | loop: |
|
- | 399 | ipl = interrupts_disable(); |
|
- | 400 | spinlock_lock(&zone_head_lock); |
|
- | 401 | ||
- | 402 | /* |
|
- | 403 | * First, find suitable frame zone. |
|
- | 404 | */ |
|
- | 405 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
|
- | 406 | z = list_get_instance(cur, zone_t, link); |
|
- | 407 | ||
- | 408 | spinlock_lock(&z->lock); |
|
- | 409 | /* |
|
- | 410 | * Check if the zone has 2^order frames area available |
|
- | 411 | * TODO: Must check if buddy system has at least block in order >= given order |
|
- | 412 | */ |
|
- | 413 | if (z->free_count == (1 >> order)) { |
|
- | 414 | zone = z; |
|
- | 415 | break; |
|
- | 416 | } |
|
- | 417 | ||
- | 418 | spinlock_unlock(&z->lock); |
|
- | 419 | } |
|
- | 420 | ||
- | 421 | if (!zone) { |
|
- | 422 | if (flags & FRAME_PANIC) |
|
- | 423 | panic("Can't allocate frame.\n"); |
|
- | 424 | ||
- | 425 | /* |
|
- | 426 | * TODO: Sleep until frames are available again. |
|
- | 427 | */ |
|
- | 428 | spinlock_unlock(&zone_head_lock); |
|
- | 429 | interrupts_restore(ipl); |
|
- | 430 | ||
- | 431 | panic("Sleep not implemented.\n"); |
|
- | 432 | goto loop; |
|
- | 433 | } |
|
- | 434 | ||
- | 435 | ||
- | 436 | /* Allocate frames from zone buddy system */ |
|
- | 437 | cur = buddy_system_alloc(zone->buddy_system, order); |
|
- | 438 | ||
- | 439 | /* frame will be actually a first frame of the block */ |
|
- | 440 | frame = list_get_instance(cur, frame_t, buddy_link); |
|
- | 441 | ||
- | 442 | /* get frame address */ |
|
- | 443 | v = FRAME2ADDR(zone, frame); |
|
- | 444 | ||
- | 445 | if (flags & FRAME_KA) |
|
- | 446 | v = PA2KA(v); |
|
- | 447 | ||
- | 448 | spinlock_unlock(&zone->lock); |
|
- | 449 | spinlock_unlock(&zone_head_lock); |
|
- | 450 | interrupts_restore(ipl); |
|
- | 451 | ||
- | 452 | return v; |
|
- | 453 | } |
|
- | 454 | ||
- | 455 | ||
- | 456 | /** Free frame(s) |
|
- | 457 | * |
|
- | 458 | * @param addr Address of the frame(s) to be freed. It must be a multiple of FRAME_SIZE. |
|
- | 459 | */ |
|
- | 460 | void zone_buddy_frame_free(__address addr) |
|
- | 461 | { |
|
- | 462 | ipl_t ipl; |
|
- | 463 | link_t *cur; |
|
- | 464 | zone_t *z; |
|
- | 465 | zone_t *zone = NULL; |
|
- | 466 | frame_t *frame; |
|
- | 467 | ||
- | 468 | ASSERT(addr % FRAME_SIZE == 0); |
|
- | 469 | ||
- | 470 | ipl = interrupts_disable(); |
|
- | 471 | spinlock_lock(&zone_head_lock); |
|
- | 472 | ||
- | 473 | /* |
|
- | 474 | * First, find host frame zone for addr. |
|
- | 475 | */ |
|
- | 476 | for (cur = zone_head.next; cur != &zone_head; cur = cur->next) { |
|
- | 477 | z = list_get_instance(cur, zone_t, link); |
|
- | 478 | ||
- | 479 | spinlock_lock(&z->lock); |
|
- | 480 | ||
- | 481 | if (IS_KA(addr)) |
|
- | 482 | addr = KA2PA(addr); |
|
- | 483 | ||
- | 484 | /* |
|
- | 485 | * Check if addr belongs to z. |
|
- | 486 | */ |
|
- | 487 | if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) { |
|
- | 488 | zone = z; |
|
- | 489 | break; |
|
- | 490 | } |
|
- | 491 | spinlock_unlock(&z->lock); |
|
- | 492 | } |
|
- | 493 | ||
- | 494 | ASSERT(zone != NULL); |
|
- | 495 | ||
- | 496 | frame = ADDR2FRAME(zone, addr); |
|
- | 497 | ||
- | 498 | ASSERT(frame->refcount); |
|
- | 499 | ||
- | 500 | if (!--frame->refcount) { |
|
- | 501 | buddy_system_free(zone->buddy_system, &frame->buddy_link); |
|
- | 502 | } |
|
- | 503 | ||
- | 504 | spinlock_unlock(&zone->lock); |
|
- | 505 | ||
- | 506 | spinlock_unlock(&zone_head_lock); |
|
- | 507 | interrupts_restore(ipl); |
|
- | 508 | } |
|
- | 509 | ||
- | 510 | ||
- | 511 | /** Buddy system find_buddy implementation |
|
- | 512 | * |
|
- | 513 | */ |
|
- | 514 | link_t * zone_buddy_find_buddy(link_t * buddy) { |
|
- | 515 | ||
- | 516 | } |
|
- | 517 | ||
- | 518 | /** Buddy system bisect implementation |
|
- | 519 | * |
|
- | 520 | */ |
|
- | 521 | link_t * zone_buddy_bisect(link_t * block) { |
|
- | 522 | ||
- | 523 | } |
|
- | 524 | ||
- | 525 | /** Buddy system coalesce implementation |
|
- | 526 | * |
|
- | 527 | */ |
|
- | 528 | link_t * zone_buddy_coalesce(link_t * buddy_l, link_t * buddy_r) { |
|
- | 529 | ||
- | 530 | } |
|
- | 531 | ||
- | 532 | /** Buddy system set_order implementation |
|
- | 533 | * |
|
- | 534 | */ |
|
- | 535 | void zone_buddy_set_order(link_t * block, __u8 order) { |
|
- | 536 | ||
- | 537 | } |
|
- | 538 | ||
- | 539 | /** Buddy system get_order implementation |
|
- | 540 | * |
|
- | 541 | */ |
|
- | 542 | __u8 zone_buddy_get_order(link_t * block) { |
|
- | 543 | ||
- | 544 | ||
- | 545 | } |