From 02b2a87fed811c01a6d375a165712105771f0043 Mon Sep 17 00:00:00 2001 From: Yi-Ting Shih Date: Tue, 15 Apr 2025 23:56:40 +0800 Subject: [PATCH] Draft: lab 4 demo fix --- include/logger.h | 8 +++---- kernel/lib/interrupt.c | 7 +++++- kernel/lib/kmalloc.c | 4 ++++ kernel/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/include/logger.h b/include/logger.h index f472760..3cec69d 100644 --- a/include/logger.h +++ b/include/logger.h @@ -101,11 +101,11 @@ logger_cur = _Generic((msg), \ // #define DEBUG_DTB(val) DEBUG(val) #define DEBUG_DTB(val) CLEAN -// #define DEBUG_EXCEP(val) DEBUG(val) -#define DEBUG_EXCEP(val) CLEAN +#define DEBUG_EXCEP(val) DEBUG(val) +// #define DEBUG_EXCEP(val) CLEAN -#define DEBUG_MEM(val) DEBUG(val) -// #define DEBUG_MEM(val) CLEAN +// #define DEBUG_MEM(val) DEBUG(val) +#define DEBUG_MEM(val) CLEAN // #define DEBUG_INITRD(val) DEBUG(val) #define DEBUG_INITRD(val) CLEAN diff --git a/kernel/lib/interrupt.c b/kernel/lib/interrupt.c index 1157ca4..c10ce20 100644 --- a/kernel/lib/interrupt.c +++ b/kernel/lib/interrupt.c @@ -65,6 +65,9 @@ void add_interrupt_task(uint64_t priority, uint64_t param) { // DEBUG_EXCEP("add interrupt"); + uint64_t last_priority = 0xfff; + if (global_interrupt_pool) + last_priority = global_interrupt_pool->priority; interrupt_t *newint = kmalloc(sizeof(interrupt_t)); *newint = (interrupt_t){ ._l = (interrupt_t *)0x0, @@ -82,7 +85,7 @@ void add_interrupt_task(uint64_t priority, while (global_interrupt_pool) { // LOG("check interrupt"); DEBUG_EXCEP(global_interrupt_pool->priority); - if (global_interrupt_pool->is_start) + if (global_interrupt_pool->is_start && global_interrupt_pool->priority < last_priority) return; global_interrupt_pool->is_start = true; global_interrupt_pool->func(global_interrupt_pool->param); @@ -101,6 +104,8 @@ void irq_handler(void) // uint64_t core0_irq_source = *CORE0_IRQ_SOURCE; // DEBUG_EXCEP(core0_irq_source); + // TODO: turnoff global interrupt + if (_irq_source_timer()) timer_irq_handler(); diff --git a/kernel/lib/kmalloc.c b/kernel/lib/kmalloc.c index 16c519b..3892ee5 100644 --- a/kernel/lib/kmalloc.c +++ b/kernel/lib/kmalloc.c @@ -80,6 +80,7 @@ void *mman_alloc(size_t unit) if (!pool->page_begin->left) pool->page_begin = *(void **)pool->page_begin->page_next; + LOG("mman_alloc"); LOG((uint64_t)unit); DEBUG_MEM(ret); return ret; } @@ -103,6 +104,9 @@ void mman_free(void *ptr) ++pool->left; ++PAGE_HEADER(ptr)->left; + + LOG("mman_free"); LOG((uint64_t)unit); DEBUG_MEM(ptr); + size_t cap = (PAGE_SIZE - sizeof(kmalloc_allocator_t)) / unit; if (PAGE_HEADER(ptr)->left == cap && pool->left >= 2 * cap) { if (PAGE_HEADER(ptr)->page_prev) diff --git a/kernel/main.c b/kernel/main.c index f148785..186ef95 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -57,6 +57,59 @@ void _print_time(uint64_t) add_timer_task(task); } +static inline +void _test() { + INFOR("start test"); + void *p1, *p2, *p3, *p4, *p5, *p6, *p7; + void *c1, *c2, *c3, *c4, *c5, *c6, *c7, *c8; + p1 = kmalloc(4095); + INFOR(p1); + kfree(p1); // 4095 + p1 = kmalloc(4095); + + c1 = kmalloc(1000); + INFOR(c1); + c2 = kmalloc(1023); + INFOR(c2); + c3 = kmalloc(999); + INFOR(c3); + c4 = kmalloc(1010); + INFOR(c4); + kfree(c3); // 999 + c5 = kmalloc(989); + INFOR(c5); + c3 = kmalloc(88); + INFOR(c3); + c6 = kmalloc(1001); + INFOR(c6); + kfree(c3); // 88 + c7 = kmalloc(2045); + c8 = kmalloc(1); + + p2 = kmalloc(4096); + kfree(c8); // 1 + p3 = kmalloc(16000); + kfree(p1); // 4095 + kfree(c3); // 88 + kfree(c7); // 2045 + p4 = kmalloc(4097); + p5 = kmalloc(4096 + 1); + p6 = kmalloc(4096); + kfree(p2); // 4096 + kfree(p5); // MAX_BLOCK_SIZE + 1 + kfree(p4); // 4097 + p7 = kmalloc(7197); + + kfree(p6); // MAX_BLOCK_SIZE + kfree(p3); // 16000 + kfree(p7); // 7197 + kfree(c1); // 1000 + kfree(c6); // 1001 + kfree(c2); // 1023 + kfree(c5); // 989 + kfree(c4); // 1010 +} + void main(void *dtb) { file_node_t *initrd_root = 0x0;