Skip to content

Commit

Permalink
🐛 fix on raspi3
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen-tw committed Apr 2, 2021
1 parent 0679472 commit 4e5c953
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
kernel_impl = impl-c
# kernel_impl = impl-rs

OPT_BUILD_BOOTLOADER = 1
OPT_BUILD_BOOTLOADER = 0
bootloader_impl = bootloader


Expand Down
5 changes: 2 additions & 3 deletions impl-c/include/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
13 changes: 3 additions & 10 deletions impl-c/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

int main() {
uart_init();
uart_println("uart initialized");
KAllocManager_init();
// KAllocManager_show_status();

Expand All @@ -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++) {
Expand All @@ -23,22 +24,14 @@ 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");
}
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();
Expand Down
25 changes: 12 additions & 13 deletions impl-c/mm/buddy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
}
10 changes: 5 additions & 5 deletions impl-c/mm/kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 4e5c953

Please sign in to comment.