From 9c8ea7a0c8389aabfcd5c4454db4124544c9f995 Mon Sep 17 00:00:00 2001 From: AceVest Date: Sun, 27 Jul 2014 19:20:18 +0800 Subject: [PATCH] rewrite scheduler --- Makefile | 1 + bin/hello.c | 5 +---- bin/shell.c | 27 +++++++++++++-------------- include/printk.h | 9 ++++++++- include/syscall.h | 3 ++- include/task.h | 3 ++- kernel/exec.c | 1 - kernel/init.c | 10 ++++++---- kernel/sched.c | 26 +++++++++++++------------- kernel/syscall.c | 12 +++++++++--- lib/exec.c | 6 ++++++ lib/syscall.c | 2 +- 12 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 45a147e..39b76d6 100644 --- a/Makefile +++ b/Makefile @@ -40,5 +40,6 @@ install: cp bin/hello /kernel/bin/ cp bin/shell /kernel/bin/ cp: + cd bin && make clean cd bin && make ./scripts/copy.sh diff --git a/bin/hello.c b/bin/hello.c index 917720f..532fe4b 100644 --- a/bin/hello.c +++ b/bin/hello.c @@ -15,10 +15,7 @@ int main() { printf("hello world\n"); - //syscall3(SYSC_WRITE, 0, "fuck", 5); - //write(0, "hello world", sizeof("hello world")); - //write(0, "hello world", 12); - + while(1); exit(0); return 0; diff --git a/bin/shell.c b/bin/shell.c index 65eeaac..1b3d251 100644 --- a/bin/shell.c +++ b/bin/shell.c @@ -14,35 +14,34 @@ #include int systest(); +int sysdebug(); int main() { - char x[10]; - x[0] = 'a'; - x[1] = 'b'; - x[2] = 'c'; - x[3] = '\n'; -#if 1 - //while(1) write(0, x, 4); - //while(1) systest(); int pid = fork(); if(pid > 0) { - //write(0, "parent\n", 7); - //write(0, x, 4); + printf("prarent child pid %u\n", pid); while(1) - ; systest(); + { + systest(); + sysdebug(0x112233); + } } else if(pid == 0) -#endif { - //write(0, "child\n", 6); - //write(0, x, 4); + printf("child\n"); + execv("/bin/hello", 0); while(1) { systest(); + sysdebug(0xAABBCCDD); } } + else + { + + } return 0; } diff --git a/include/printk.h b/include/printk.h index 9527a9f..ecaf06b 100644 --- a/include/printk.h +++ b/include/printk.h @@ -34,10 +34,17 @@ enum { MPL_IDE, MPL_IDE_INTR, MPL_PREEMPT, + MPL_TEST, + MPL_DEBUG, MPL_ROOT, MPL_TASK_1, MPL_TASK_2, - MPL_TEST, + MPL_TASK_3, + MPL_TASK_4, + MPL_TASK_5, + MPL_TASK_6, + MPL_TASK_7, + MPL_TASK_8, MPL_END }; diff --git a/include/syscall.h b/include/syscall.h index 7530188..da40042 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -48,7 +48,8 @@ enum SYSC_STAT, SYSC_EXIT, SYSC_PAUSE, - SYSC_TEST + SYSC_TEST, + SYSC_DEBUG }; #endif // ASM diff --git a/include/task.h b/include/task.h index 3503079..9d59ad9 100644 --- a/include/task.h +++ b/include/task.h @@ -64,6 +64,7 @@ typedef union task_union //pFile fps[NR_OPENS]; + unsigned int cnt; // debug only }; unsigned char stack[TASK_SIZE]; @@ -88,7 +89,7 @@ static inline pid_t sysc_getpid() #define ROOT_TSK_PID (0) -#define TASK_INIT_WEIGHT 10 +#define TASK_INIT_WEIGHT 0 #define get_tsk_from_list(p) list_entry((p), Task, list) #define del_tsk_from_list(tsk) list_del((&tsk->list)) diff --git a/kernel/exec.c b/kernel/exec.c index 393e29e..04e84de 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -110,7 +110,6 @@ int sysc_exec(const char *path, char *const argv[]) load_cr3(current); - disable_irq(); pt_regs_t *regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1; memset((void*)regs, 0, sizeof(pt_regs_t)); diff --git a/kernel/init.c b/kernel/init.c index 66b6bd5..eb3f6aa 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -42,9 +42,10 @@ void init_task_entry() while(1) { - printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); - int v = debug_wait_queue_get(); - printk("task:%d wait queue get %d\n", id, v); + sysc_test(); + //printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); + int v = 0; //debug_wait_queue_get(); + //printk("task:%d wait queue get %d\n", id, v); } } @@ -68,7 +69,8 @@ void root_task_entry() int cnt = 0; while(1) { - printl(MPL_ROOT, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++); + sysc_test(); + //printl(MPL_ROOT, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++); asm("sti;hlt;"); //sysc_test(); //syscall0(SYSC_TEST); diff --git a/kernel/sched.c b/kernel/sched.c index d8acd73..cb864b1 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -126,12 +126,11 @@ inline void context_switch(task_union * prev, task_union * next) unsigned long schedule() { + static turn = 0; task_union *sel = &root_task; task_union *p = 0; list_head_t *pos = 0, *t=0; - unsigned int max_weight = 0; - unsigned long iflags; irq_save(iflags); list_for_each_safe(pos, t, &root_task.list) @@ -140,32 +139,33 @@ unsigned long schedule() if(p->state != TASK_RUNNING) { - if(p->state == TASK_EXEC) - p->state = TASK_RUNNING; continue; } - if(p->weight > max_weight) - { - max_weight = p->weight; - sel = p; - } + printd("%08x weight %d\n", p, p->weight); - else if(p->weight == 0) + if(p->weight != turn) { - p->weight = TASK_INIT_WEIGHT; + p->weight = turn; + sel = p; + break; } } irq_restore(iflags); - sel->weight--; + if(sel == &root_task) + { + turn ++; + } + + printl(MPL_ROOT+sel->pid, "task:%d [%08x] turn %d cnt %u", sel->pid, sel, sel->weight, sel->cnt); task_union *prev = current; task_union *next = sel; if(prev != next) { - printd("select %08x\n", next); + printd("[s:%08x]\n", next); context_switch(prev, next); } } diff --git a/kernel/syscall.c b/kernel/syscall.c index 3c7292d..5c658d5 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -51,10 +51,15 @@ int sysc_pause() int sysc_test() { static unsigned int cnt=0; - printl(MPL_TEST, "sysc_test %u", cnt++); + current->cnt++; + printl(MPL_TEST, "sysc_test cnt %u current %08x cnt %u", + cnt++, current, current->cnt); +} - //unsigned long *pde = (unsigned long *)current->cr3; - //printk("sysctest %08x\n", pde[797]); +int sysc_debug(unsigned int v) +{ + static unsigned int cnt=0; + printl(MPL_DEBUG, "Task Debug Syscall %u Value %08x", cnt++, v); } void init_sysc_handler_table() @@ -80,6 +85,7 @@ void init_sysc_handler_table() _sysc_(SYSC_EXIT, sysc_exit); _sysc_(SYSC_PAUSE, sysc_pause); _sysc_(SYSC_TEST, sysc_test); + _sysc_(SYSC_DEBUG, sysc_debug); } int sysc_bad_syscnr() diff --git a/lib/exec.c b/lib/exec.c index d693fe1..17453b9 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -20,3 +20,9 @@ int systest() { return syscall0(SYSC_TEST); } + +int sysdebug(unsigned int v) +{ + //return syscall1(SYSC_DEBUG, v); + return syscall1(SYSC_DEBUG, 1); +} diff --git a/lib/syscall.c b/lib/syscall.c index 0cfa8d1..7ca1420 100644 --- a/lib/syscall.c +++ b/lib/syscall.c @@ -10,7 +10,7 @@ // "movl $1f, %%edi;" // #define SYSENTER_ASM \ - "pushl $1f;" \ + "pushl $1f;" \ "pushl %%ecx;" \ "pushl %%edx;" \ "pushl %%ebp;" \ -- 2.44.0