#define FOOLOS_MODULE_NAME "syscalls" #include "lib/buffer/ringbuffer.h" #include "lib/logger/log.h" #include "lib/bool/bool.h" #include "fs/fs.h" #include "fs/ext2.h" #include "kernel/console.h" #include "kernel/config.h" #include static uint32_t alloc; // TODO: implement on a per process basis! int syscall_unhandled(int nr) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"syscall: %d", nr); panic(FOOLOS_MODULE_NAME,"unhandled syscall"); } int syscall_lseek(int file,int ptr,int dir) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"lseek (file=%d, ptr=%d, dir=%d)", file,ptr,dir); #endif panic(FOOLOS_MODULE_NAME,"unhandled syscall"); return 0; } // TODO: /dev/console or /dev/tty1 - /dev/ttyN int syscall_write(int file, char *buf, int len) { //x86_int_disable(); #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"[%d] write(file=%d, buf=0x%08X, len=%d)", task_get_current_pid(),file,buf,len); #endif if(file!=1&&file!=2) panic(FOOLOS_MODULE_NAME,"unhandled syscall"); //stderr and stdout go to console for(int i=0;i0) { console_del_char(); buf--; l--; } } else{ *buf=c; buf++; l++; if(c!=0x04)console_put_char_white(c); if(c=='\n')return l; if(c==0x04) { l--; buf--; eof=true; return l; } } } } } } //TODO: replace with dirent! int syscall_readdir(const char *name,fs_dirent *dirs,int max) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"readdir(name=0x%08X, dirs=0x%08X, %d)", name,dirs,max); #endif return fs_readdir(name,dirs,max); } int syscall_wait(int *wait, int none1, int none2) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"[%d] wait ",task_get_current_pid()); #endif panic(FOOLOS_MODULE_NAME,"unhandled syscall"); } int syscall_execve(char *name, char **argv1, char **env1) { char *argv[]={"/bin/foolshell",NULL}; char *env[]={"PATH=/bin","PWD=/home/miguel","PS1=$ ",NULL}; #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"execve (name=0x%08X(%s), argvs=0x%08X, env=0x%08X)", name,name,argv,env); #endif uint32_t entry=load_elf(name,&alloc); if(!entry) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"execve: bailing out!"); return -1; // errror loading } int i=0; do{ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"envr %d : 0x%08X : %s" ,i,env[i],env[i]); i++; }while(env[i]!=NULL); int argc=0; do{ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"arg %d : 0x%08X : %s" ,argc,argv[argc],argv[argc]); argc++; }while(argv[argc]!=NULL); asm("mov $0x08248000,%esp"); // set stack pointer // 2 mb over start. asm("push %0" :: "r" (argv)); asm("push %0" :: "r" (argc)); asm("push %0" :: "r" (env)); // push addr and return to it asm("push %0"::"r"(entry)); asm("sti"); asm("ret"); // this is never reached! } int syscall_open(char *name, int flags, int mode) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"open (name=0x%08X(\"%s\"), flags=%d, mode=%d)",name, name,flags,mode); #endif panic(FOOLOS_MODULE_NAME,"unhandled syscall"); } //newcomers // int syscall_close(int file,int none1,int none2) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"close (file=%d)", file); #endif if(file!=0&&file!=1&&file!=2) panic(FOOLOS_MODULE_NAME,"unhandled syscall"); return -1; } // TODO: check if file is termminal! int syscall_isatty(int file,int none1,int none2) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"isatty (file=%d)", file); #endif return 1; } // TODO: per process basis! 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; } int syscall_exit(int ret, char **env, int none2) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"exit (ret=%d) (env=0x%08X)", ret, env); #endif panic(FOOLOS_MODULE_NAME,"exit not supported yet" ); } // stat, fstat, lstat int syscall_stat(const char *path, struct stat *st,int none) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"stat (path=0x%08X,stat=0x%08X)", path,st); #endif st->st_mode = S_IFCHR; return 0; } int syscall_fstat(int file, struct stat *st,int none) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fstat (file=%d,stat=0x%08X)", file,st); #endif st->st_mode = S_IFCHR; return 0; } int syscall_lstat(const char *path, struct stat *st,int none) { #ifdef LOG_SYSCALLS log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"lstat (path=0x%08X,stat=0x%08X)", path,st); #endif st->st_mode = S_IFCHR; return 0; }