diff --git a/Makefile b/Makefile index 3a8882b19..c8d5ecf99 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ kernel_impl = impl-c # kernel_impl = impl-rs -OPT_BUILD_BOOTLOADER = 1 +OPT_BUILD_BOOTLOADER = 0 bootloader_impl = bootloader diff --git a/impl-c/include/mem.h b/impl-c/include/mem.h index 131e047a6..8752e321a 100644 --- a/impl-c/include/mem.h +++ b/impl-c/include/mem.h @@ -44,7 +44,6 @@ typedef struct Frame { // allocate contiguous frames typedef struct BuddyAllocater { list_head_t free_lists[BUDDY_NUM_FREE_LISTS]; - Frame *frame_array; } BuddyAllocater; typedef struct SlabAllocator { @@ -62,7 +61,7 @@ typedef struct SlabAllocator { typedef struct AllocationManager { SlabAllocator obj_allocator_list[SLAB_NUM_SLAB_SIZES]; - BuddyAllocater *frame_allocator; + BuddyAllocater frame_allocator; } AllocationManager; void *kalloc(int size); @@ -84,7 +83,7 @@ void *slab_alloc(SlabAllocator *alloc); // Free an object void slab_free(void *obj); -void buddy_init(BuddyAllocater *alloc, Frame *frame_arr); +void buddy_init(BuddyAllocater *alloc); struct Frame *buddy_alloc(BuddyAllocater *alloc, int size_in_byte); void buddy_free(BuddyAllocater *alloc, struct Frame *frame); void buddy_dump(BuddyAllocater *alloc); diff --git a/impl-c/kernel/main.c b/impl-c/kernel/main.c index 9cad52cb6..aab1a8941 100644 --- a/impl-c/kernel/main.c +++ b/impl-c/kernel/main.c @@ -4,6 +4,7 @@ int main() { uart_init(); + uart_println("uart initialized"); KAllocManager_init(); // KAllocManager_show_status(); @@ -14,7 +15,7 @@ int main() { void *a[30]; for (int i = 0; i < 5; i++) { a[i] = kalloc(8192); - uart_println("i:%d, a: %x", i, a); + uart_println("i:%d, a: %x", i, a[i]); } for (int i = 0; i < 5; i++) { @@ -23,7 +24,7 @@ int main() { for (int i = 0; i < 5; i++) { a[i] = kalloc(13); - uart_println("i:%d, a: %x", i, a); + uart_println("i:%d, a: %x", i, a[i]); }; if (a[0] == a[1]) { uart_println("nooo"); @@ -31,14 +32,6 @@ int main() { for (int i = 0; i < 5; i++) { kfree(a[i]); } - - // KAllocManager_show_status(); - // int d = buddy_alloc(&buddy, 10); - // kfree(a); - // kfree(c); - - // kfree(b); - // KAllocManager_show_status(); uart_println(" input filename to see file content"); while (1) { shellPrintPrompt(); diff --git a/impl-c/mm/buddy.c b/impl-c/mm/buddy.c index aa8778fd2..a9f0338b3 100644 --- a/impl-c/mm/buddy.c +++ b/impl-c/mm/buddy.c @@ -17,7 +17,7 @@ void buddy_dump(BuddyAllocater *alloc) { uart_println("========Status========"); uart_println("Framenodes (idx, exp)"); for (int i = 0; i < (1 << BUDDY_MAX_EXPONENT); i++) { - Frame *node = &alloc->frame_array[i]; + Frame *node = &Frames[i]; uart_printf("[%d,%d,%x]", node->arr_index, node->exp, node->addr); } uart_println(""); @@ -53,14 +53,14 @@ void buddy_free(BuddyAllocater *alloc, struct Frame *frame) { Frame *low, *high; int frame_idx; for (frame_idx = frame->arr_index;;) { - node = &alloc->frame_array[frame_idx]; + node = &Frames[frame_idx]; if (buddy_idx(node) >= (1 << BUDDY_MAX_EXPONENT)) { list_push(&node->list_base, &alloc->free_lists[node->exp]); uart_println(" push to freelist: node(idx:%d,exp:%d)", node->arr_index, node->exp); break; } - buddy = &alloc->frame_array[buddy_idx(node)]; + buddy = &Frames[buddy_idx(node)]; uart_printf("Try to merge buddy(idx:%d,exp:%d) node(idx:%d,exp:%d)", buddy->arr_index, buddy->exp, node->arr_index, node->exp); @@ -103,8 +103,8 @@ bool provide_frame_with_exp(BuddyAllocater *alloc, int required_exp) { Frame *node = (Frame *)list_pop(&alloc->free_lists[exp]); int child_exp = exp - 1; - Frame *child1 = &alloc->frame_array[node->arr_index]; - Frame *child2 = &alloc->frame_array[node->arr_index + (1 << child_exp)]; + Frame *child1 = &Frames[node->arr_index]; + Frame *child2 = &Frames[node->arr_index + (1 << child_exp)]; child1->exp = child_exp; child2->exp = child_exp; list_push(&child1->list_base, &alloc->free_lists[child_exp]); @@ -116,22 +116,21 @@ bool provide_frame_with_exp(BuddyAllocater *alloc, int required_exp) { return false; } -void buddy_init(BuddyAllocater *alloc, Frame *frame_arr) { - +void buddy_init(BuddyAllocater *alloc) { // Bind the physical frame for manipulation - alloc->frame_array = frame_arr; for (int i = 0; i < (1 << BUDDY_MAX_EXPONENT); i++) { - alloc->frame_array[i].arr_index = i; - alloc->frame_array[i].exp = -1; - alloc->frame_array[i].addr = - (void *)(((long)i << FRAME_SHIFT) + MEMORY_START); + Frames[i].arr_index = i; + Frames[i].exp = -1; + Frames[i].addr = (void *)(((long)i << FRAME_SHIFT) + MEMORY_START); } for (int i = 0; i < BUDDY_NUM_FREE_LISTS; i++) { + uart_println("addr for list %d, %x", i, &(alloc->free_lists[i])); list_init(&alloc->free_lists[i]); } + // push a root frame - Frame *root_frame = alloc->frame_array + 0; + Frame *root_frame = &Frames[0]; root_frame->exp = BUDDY_MAX_EXPONENT; list_push(&root_frame->list_base, &alloc->free_lists[BUDDY_MAX_EXPONENT]); } diff --git a/impl-c/mm/kalloc.c b/impl-c/mm/kalloc.c index b87041d08..a806f975a 100644 --- a/impl-c/mm/kalloc.c +++ b/impl-c/mm/kalloc.c @@ -6,8 +6,8 @@ void KAllocManager_init() { AllocationManager *am = &KAllocManager; + buddy_init(&am->frame_allocator); - buddy_init(am->frame_allocator, Frames); for (int i = 0; i < (1 << BUDDY_MAX_EXPONENT); i++) { Frames[i].list_base.next = NULL; Frames[i].list_base.prev = NULL; @@ -23,14 +23,14 @@ void KAllocManager_init() { slab_alloc = &am->obj_allocator_list[i - SLAB_OBJ_MIN_SIZE_EXP]; slab_alloc->unit_size = 1 << i; slab_alloc->max_slab_num_obj = 4096 / slab_alloc->unit_size; - slab_alloc->frame_allocator = am->frame_allocator; + slab_alloc->frame_allocator = &am->frame_allocator; slab_alloc->cur_frame = NULL; list_init(&slab_alloc->partial_list); list_init(&slab_alloc->full_list); } } -void KAllocManager_show_status() { buddy_dump(KAllocManager.frame_allocator); } +void KAllocManager_show_status() { buddy_dump(&KAllocManager.frame_allocator); } void *kalloc(int size) { void *addr; @@ -48,7 +48,7 @@ void *kalloc(int size) { for (int i = 0; i < BUDDY_MAX_EXPONENT; i++) { if ((i << FRAME_SHIFT) >= size) { uart_println("Allocate Request exp:%d", i); - Frame *frame = buddy_alloc(KAllocManager.frame_allocator, i); + Frame *frame = buddy_alloc(&KAllocManager.frame_allocator, i); uart_println("Allocated addr:%x, frame_idx:%d", frame->addr, frame->arr_index); return frame->addr; @@ -65,6 +65,6 @@ void kfree(void *addr) { } else { uart_println("Free Request addr:%x, frame_idx:%d", frame->addr, frame->arr_index); - buddy_free(KAllocManager.frame_allocator, frame); + buddy_free(&KAllocManager.frame_allocator, frame); } }