From e7b9569041521da7e0b67c30af7179c0af1e3738 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Thu, 27 Nov 2014 01:44:25 +0100 Subject: struggling with new multitasking --- kernel/config.h | 2 +- kernel/kernel.c | 6 +-- kernel/syscalls.c | 88 +++++---------------------------- kernel/syscalls.h | 3 +- kernel/task.c | 142 +++++++++++++++++------------------------------------- 5 files changed, 61 insertions(+), 180 deletions(-) (limited to 'kernel') diff --git a/kernel/config.h b/kernel/config.h index fb68dec..4238578 100644 --- a/kernel/config.h +++ b/kernel/config.h @@ -8,7 +8,7 @@ #define FOOLOS_CONFIG_H #define FOOLOS_CONSOLE_AUTOBREAK // add newline automatically at end of line -#define FOOLOS_LOG_OFF // do not log anything +//#define FOOLOS_LOG_OFF // do not log anything #define FOOLOS_CONSOLE // otherwise VESA will be used! #define MEM_PRINT_MEMORYMAP #define LOG_BUF_SIZE 4069 diff --git a/kernel/kernel.c b/kernel/kernel.c index ed632c3..b03e3cd 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -67,7 +67,7 @@ void kernel_main(uint32_t initial_stack, int mp) // // Activate Virtual Memory (paging) // 0x8048000 is where user programms start! - vmem_init(); + //vmem_init(); // // init output to screen @@ -107,8 +107,8 @@ void kernel_main(uint32_t initial_stack, int mp) */ - // task_init(); - // while(1); + task_init(); + while(1); // load and run foolshell // we will come back into the kernel only on interrupts... diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 765316b..585950d 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -72,7 +72,7 @@ typedef struct { } Elf32_Phdr; -static uint32_t alloc=0x900000; +static uint32_t alloc; int syscall_unhandled(int nr) { @@ -102,10 +102,9 @@ int syscall_write(int file, char *buf, int len) if(file!=1&&file!=2) panic(FOOLOS_MODULE_NAME,"unhandled syscall"); - // ALL output to stdout + //stderr and stdout go to console for(int i=0;ie_phnum;phidx++) { @@ -372,7 +307,6 @@ int syscall_execve(char *name, char **argv, char **env) asm("ret"); // this is never reached! - } @@ -413,16 +347,13 @@ int syscall_isatty(int file,int none1,int none2) uint32_t syscall_sbrk(int incr, int none1, int none2) { - uint32_t oldalloc=alloc; alloc+=incr; #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"sbrk (incr=%d) = 0x%08X", incr,oldalloc); #endif - return oldalloc; - } @@ -432,6 +363,9 @@ int syscall_exit(int ret, char **env, int none2) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"exit (ret=%d) (env=0x%08X)", ret, env); #endif + panic(FOOLOS_MODULE_NAME,"exit not supported yet" ); + + /* int i=0; while(env[i]!=NULL) { @@ -442,11 +376,11 @@ int syscall_exit(int ret, char **env, int none2) asm("mov $0x05bff,%esp"); // set stack pointer static char *argv[]={"shell","--silent",NULL}; syscall_execve("/bin/foolshell",argv,env); // start shell + */ } // stat, fstat, lstat - int syscall_stat(const char *path, struct stat *st,int none) { #ifdef LOG_SYSCALLS diff --git a/kernel/syscalls.h b/kernel/syscalls.h index fc5f5bf..d9a338d 100644 --- a/kernel/syscalls.h +++ b/kernel/syscalls.h @@ -32,12 +32,13 @@ #define SYSCALL_GETPID 78 int syscall_execve(char *name, char **argv, char **env); +int syscall_write(int file, char *buf, int len); + /* int syscall_readdir(const char *name,struct fs_dirent *dirs,int max); int syscall_exit(int ret, int none1, int none2); int syscall_open(char *name, int flags, int len); -int syscall_write(int file, char *buf, int len); int syscall_read(int file, char *buf, int len); int syscall_close(int file,int none1,int none2); int syscall_fstat(int file, struct stat *st,int none); diff --git a/kernel/task.c b/kernel/task.c index 08301cf..620977e 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -14,75 +14,27 @@ #include "fs/ext2.h" #define FOOLOS_MODULE_NAME "task" -int started; -static volatile int c1,c2,c3; -void task_test1() -{ - // simple built-in shell - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"spawning build-in shell"); - - while(1) - { - - char c; - if(ringbuffer_get(&c)) - { - console_put_char_white(c); - } - } - - /* - c1++; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 1 progress: %d", c1); - sleep(2); - */ - - - -} +#define MAX_TASKS 10 -void task_test2() -{ - while(1) - { - c2++; - //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 2 progress: %d", c2); -// sleep(2); - - } +static volatile int current_task=-2; -} - -void task_test3() +static volatile struct task_list_struct { - while(1) - { - c3++; -// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 3 progress: %d", c3); -// sleep(2); - } - -} - -////////////// -// -typedef struct{ //Simple structure for a thread - unsigned int esp0; //Stack for kernel - unsigned int esp3; //Stack for process -} Thread; - - -Thread Threads[3]; //Space for our simple threads. -volatile int CurrentTask; //The thread currenlty running (-1 == none) + bool active; + uint32_t esp; // stack pointer of the task; + uint32_t vmem; // number of virtual memory table to switch to + +}task_list[MAX_TASKS]; void task_create(int pid,void(*thread)()) { unsigned int *stack; - Threads[pid].esp0 = pmmngr_alloc_block(); - stack = (unsigned int*)Threads[pid].esp0+4095; //This makes a pointer to the stack for us + task_list[pid].esp = pmmngr_alloc_block(); + stack = (unsigned int*)task_list[pid].esp+4095; //This makes a pointer to the stack for us + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"new task: stack: 0x%08X thread: 0x%08X", stack, thread); //First, this stuff is pushed by the processor @@ -108,60 +60,54 @@ void task_create(int pid,void(*thread)()) *--stack = 0x10; //GS */ - Threads[pid].esp0 = (uint32_t)stack; //Update the stack pointer - + task_list[pid].esp = (uint32_t)stack; //Update the stack pointer + task_list[pid].active=true; }; +// this gets called by our clock interrupt regularly! uint32_t task_switch_next(uint32_t oldesp) { + timer_tick(); - //console_put_char('.'); - timer_tick(); - if(started!=0xabcde) return oldesp; - if(CurrentTask!=-1)Threads[CurrentTask].esp0=oldesp; - - CurrentTask++; - if(CurrentTask>2)CurrentTask=0; - - /* - log( - FOOLOS_MODULE_NAME, - FOOLOS_LOG_INFO, - "oldesp: 0x%08X saved / next task: %d (esp: 0x%08X) ", - oldesp, - CurrentTask, - Threads[CurrentTask].esp0); - -// return oldesp; -// */ - - return Threads[CurrentTask].esp0; //Return new stack pointer to ASM -} + if(current_task==-2)return oldesp; + if(current_task!=-1)task_list[current_task].esp=oldesp; -void stack_trace(uint32_t *stack,int size) -{ - for(int i=size;i>=0;i--) + for(int i=0;i 0x%08X", stack, *stack); - stack++; + int pid=(current_task+1+i)%MAX_TASKS; // schedule round robin style + if(task_list[pid].active) + { + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"switch from %d to pid: %d (0x%08X)", current_task,pid,task_list[pid].esp); + current_task=pid; + return task_list[pid].esp; + } } -} - -void task_init() -{ + // if no task found retunr the old esp + return oldesp; +} - CurrentTask=-1; +volatile int c1,c2; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init multitasking."); - task_create(0,task_test1); - task_create(1,task_test2); - task_create(2,task_test3); - started=0xabcde; +volatile void test1() +{ + c1++; + // syscall_write(1,">",1); } +volatile void test2() +{ + c2++; + // syscall_write(1,"<",1); +} +void task_init() +{ + task_create(1,test1); + task_create(2,test2); + current_task=-1; +} -- cgit v1.2.3