From 58f0877fc16da760e2ed6910648ec67e9afff890 Mon Sep 17 00:00:00 2001 From: Minep Date: Thu, 10 Nov 2022 12:53:53 +0000 Subject: [PATCH] fix: add dummy process to keep scheduler busy refactor: reduce boot stack size fix: regression --- lunaix-os/.gitignore | 3 +- lunaix-os/arch/x86/boot.S | 6 +-- lunaix-os/kernel/asm/x86/interrupt.S | 7 +++- lunaix-os/kernel/block/blkpart_gpt.c | 2 +- lunaix-os/kernel/block/block.c | 8 ---- lunaix-os/kernel/k_init.c | 12 +----- lunaix-os/kernel/process/dummy.c | 7 ++++ lunaix-os/kernel/process/sched.c | 56 ++++++++++++++++++++++++---- 8 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 lunaix-os/kernel/process/dummy.c diff --git a/lunaix-os/.gitignore b/lunaix-os/.gitignore index a54615d..b20e749 100644 --- a/lunaix-os/.gitignore +++ b/lunaix-os/.gitignore @@ -6,4 +6,5 @@ playground/ .idea bx_enh_dbg.ini machine/ -draft/ \ No newline at end of file +draft/ +iso_inspect/ \ No newline at end of file diff --git a/lunaix-os/arch/x86/boot.S b/lunaix-os/arch/x86/boot.S index 938db87..9e44437 100644 --- a/lunaix-os/arch/x86/boot.S +++ b/lunaix-os/arch/x86/boot.S @@ -18,8 +18,8 @@ /* 根据System V ABI,栈地址必须16字节对齐 */ /* 这里只是一个临时栈,在_hhk_init里面我们会初始化内核专用栈 */ stack_bottom: - .skip 16318 * 2, 0 - stack_top: + .skip 4096, 0 + __stack_top: /* @@ -44,7 +44,7 @@ cld # 确保屏蔽所有外中断,直到我们准备好PIC为止 cli - movl $stack_top, %esp + movl $__stack_top, %esp subl $16, %esp diff --git a/lunaix-os/kernel/asm/x86/interrupt.S b/lunaix-os/kernel/asm/x86/interrupt.S index 14bafb6..2961f3b 100644 --- a/lunaix-os/kernel/asm/x86/interrupt.S +++ b/lunaix-os/kernel/asm/x86/interrupt.S @@ -104,8 +104,11 @@ #endif movl (__current), %eax movl 92(%eax), %eax - fxrstor (%eax) - + + test %eax, %eax # do we have stored x87 context? + jz 1f + fxrstor (%eax) +1: popl %eax popl %ebx popl %ecx diff --git a/lunaix-os/kernel/block/blkpart_gpt.c b/lunaix-os/kernel/block/blkpart_gpt.c index 4f9beec..1a087af 100644 --- a/lunaix-os/kernel/block/blkpart_gpt.c +++ b/lunaix-os/kernel/block/blkpart_gpt.c @@ -7,7 +7,7 @@ #include #define GPT_BLKSIZE 512 -#define LBA2OFF(lba) (lba * GPT_BLKSIZE) +#define LBA2OFF(lba) ((lba)*GPT_BLKSIZE) #define ENT_PER_BLK (GPT_BLKSIZE / sizeof(struct gpt_entry)) #define GPTSIG_LO 0x20494645UL diff --git a/lunaix-os/kernel/block/block.c b/lunaix-os/kernel/block/block.c index 216c0ef..aa6e52e 100644 --- a/lunaix-os/kernel/block/block.c +++ b/lunaix-os/kernel/block/block.c @@ -75,10 +75,6 @@ __block_read(struct device* dev, void* buf, size_t offset, size_t len) pwait(&req->wait); - // XXX temporary work-around - // in case pwait used in proc0. Need a dummy process! - wait_if((req->flags & BLKIO_PENDING)); - if (!(errno = req->errcode)) { memcpy(buf, tmp_buf + r, rd_size); errno = len; @@ -125,10 +121,6 @@ __block_write(struct device* dev, void* buf, size_t offset, size_t len) pwait(&req->wait); - // XXX temporary work-around - // in case pwait used in proc0. Need a dummy process! - wait_if((req->flags & BLKIO_PENDING)); - int errno = req->errcode; if (!errno) { errno = len; diff --git a/lunaix-os/kernel/k_init.c b/lunaix-os/kernel/k_init.c index 32164c8..d1643b4 100644 --- a/lunaix-os/kernel/k_init.c +++ b/lunaix-os/kernel/k_init.c @@ -33,8 +33,6 @@ multiboot_info_t* _k_init_mb_info; x86_page_table* __kernel_ptd; -struct proc_info tmp; - extern void __proc0(); /* proc0.c */ @@ -60,12 +58,6 @@ _kernel_pre_init() _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t); setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size); - - __kernel_ptd = cpu_rcr3(); - - tmp = (struct proc_info){ .page_table = __kernel_ptd }; - - __current = &tmp; } void @@ -77,6 +69,8 @@ _kernel_init() cake_init(); valloc_init(); + sched_init(); + // crt tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE)); tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); @@ -98,8 +92,6 @@ _kernel_init() lxconsole_init(); - sched_init(); - syscall_install(); spawn_proc0(); diff --git a/lunaix-os/kernel/process/dummy.c b/lunaix-os/kernel/process/dummy.c new file mode 100644 index 0000000..40f4db1 --- /dev/null +++ b/lunaix-os/kernel/process/dummy.c @@ -0,0 +1,7 @@ +void +my_dummy() +{ + while (1) { + asm("hlt"); + } +} diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index c2a2426..91932ca 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -20,6 +20,8 @@ volatile struct proc_info* __current; +static struct proc_info dummy_proc; + struct proc_info dummy; struct scheduler sched_ctx; @@ -28,23 +30,54 @@ struct cake_pile* proc_pile; LOG_MODULE("SCHED") +void +sched_init_dummy(); + void sched_init() { - // size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000); - - // for (size_t i = 0; i <= pg_size; i += 4096) { - // uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST); - // vmm_set_mapping( - // PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL); - // } - proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0); cake_set_constructor(proc_pile, cake_ctor_zeroing); sched_ctx = (struct scheduler){ ._procs = vzalloc(PROC_TABLE_SIZE), .ptable_len = 0, .procs_index = 0 }; + + // TODO initialize dummy_proc + sched_init_dummy(); +} + +void +sched_init_dummy() +{ + // This surely need to be simplified or encapsulated! + // It is a living nightmare! + + extern void my_dummy(); + static char dummy_stack[1024] __attribute__((aligned(16))); + + // memset to 0 + dummy_proc = (struct proc_info){}; + dummy_proc.intr_ctx = + (isr_param){ .registers = { .ds = KDATA_SEG, + .es = KDATA_SEG, + .fs = KDATA_SEG, + .gs = KDATA_SEG, + .esp = (void*)dummy_stack + 1004 }, + .cs = KCODE_SEG, + .eip = (void*)my_dummy, + .ss = KDATA_SEG, + .eflags = cpu_reflags() | 0x0200 }; + + *(u32_t*)(&dummy_stack[1020]) = dummy_proc.intr_ctx.eflags; + *(u32_t*)(&dummy_stack[1016]) = KCODE_SEG; + *(u32_t*)(&dummy_stack[1012]) = dummy_proc.intr_ctx.eip; + + dummy_proc.page_table = cpu_rcr3(); + dummy_proc.state = PS_READY; + dummy_proc.parent = &dummy_proc; + + __current = &dummy_proc; } void @@ -142,11 +175,18 @@ redo: sched_ctx.procs_index = ptr; + if (next->state != PS_READY) { + // schedule the dummy process if we're out of choice + next = &dummy_proc; + goto done; + } + if (!can_schedule(next)) { // 如果该进程不给予调度,则尝试重新选择 goto redo; } +done: run(next); } -- 2.27.0