diff options
| author | Michal Idziorek <m.i@gmx.at> | 2014-10-23 08:41:34 +0200 |
|---|---|---|
| committer | Michal Idziorek <m.i@gmx.at> | 2014-10-23 08:41:34 +0200 |
| commit | 463736887fbb6439fe5e676f6fd7990adc6bb727 (patch) | |
| tree | 00dcb1fb5d426d13ef854820a8672470e49530a7 | |
| parent | fde24d20cd6a7d5fd1f1df71515e377586b91e24 (diff) | |
added execve syscall
| -rw-r--r-- | asm/int_syscall_handler.asm | 30 | ||||
| -rw-r--r-- | kernel/kernel.c | 6 | ||||
| -rw-r--r-- | kernel/syscalls.c | 46 | ||||
| -rw-r--r-- | userspace/Makefile | 18 | ||||
| -rw-r--r-- | userspace/foolshell.c | 7 | ||||
| -rw-r--r-- | userspace/simple.c | 18 | ||||
| -rw-r--r-- | userspace/syscalls.c | 112 |
7 files changed, 107 insertions, 130 deletions
diff --git a/asm/int_syscall_handler.asm b/asm/int_syscall_handler.asm index c36b9b5..25de51a 100644 --- a/asm/int_syscall_handler.asm +++ b/asm/int_syscall_handler.asm @@ -1,11 +1,9 @@ global int_syscall_handler -[extern example_syscall] -[extern example_syscall_2] - [extern syscall_write] [extern syscall_read] [extern syscall_readdir] +[extern syscall_execve] [bits 32] int_syscall_handler: @@ -16,12 +14,6 @@ int_syscall_handler: push ecx push edx - cmp eax, 10 - je call_example_syscall - - cmp eax, 20 - je call_example_syscall_2 - cmp eax, 61 je call_write @@ -31,6 +23,9 @@ int_syscall_handler: cmp eax, 63 je call_readdir + cmp eax, 64 + je call_execve + done: mov ebx,eax @@ -54,15 +49,6 @@ done_blocking: iret ;Interrupt-Return - -call_example_syscall: - call example_syscall - jmp done - -call_example_syscall_2: - call example_syscall_2 - jmp done - call_write: call syscall_write jmp done @@ -72,7 +58,6 @@ call_readdir: jmp done call_read: - mov al, 0x20 ;Port number AND command number to Acknowledge IRQ out 0x20, al ;Acknowledge IRQ, so we keep getting interrupts sti @@ -81,3 +66,10 @@ call_read: jmp done_blocking +call_execve: + mov al, 0x20 ;Port number AND command number to Acknowledge IRQ + out 0x20, al ;Acknowledge IRQ, so we keep getting interrupts + sti + call syscall_execve + jmp $ ; this should never return TODO: pop stack!? + diff --git a/kernel/kernel.c b/kernel/kernel.c index 23246e1..208d1cd 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -197,11 +197,7 @@ void kernel_main(uint32_t initial_stack, int mp) //vesa_init_doublebuff(); ext2_check(0x80800); - ext2_inode_content(0x80800,15,0x100000,0x100000); - - // autorun "user-space" prog - asm("push $0x100000"); - asm("ret"); + syscall_execve(15,0,0); // Just hang around here, if its reached. // we do our tasks anyway. on the next clock tick. diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 0fe1542..564a604 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -6,6 +6,7 @@ // int syscall_write(int file, char *buf, int len) { + // ALL output to stdout for(int i=0;i<len;i++) { PutConsoleChar(buf[i],0b1111111111000000); @@ -16,21 +17,25 @@ int syscall_write(int file, char *buf, int len) int syscall_read(int file, char *buf, int len) { - char c; - - while(1) + // stdin + if(file==1) { - asm("cli"); - bool ret=ringbuffer_get(&c); - asm("sti"); - - if(ret) + + char c; + + while(1) { - *buf=c; - return 1; - } + asm("cli"); + bool ret=ringbuffer_get(&c); + asm("sti"); + + if(ret) + { + *buf=c; + return 1; + } -// if(ret) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"read kb buffer: %d",c); + } } } @@ -39,19 +44,12 @@ int syscall_readdir(const char *name,fs_dirent *dirs,int max) { return fs_readdir(name,dirs,max); } -// -int example_syscall(int x,int y) +int syscall_execve(char *name, char **argv, char **env) { -// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"example syscall called with %d + %d",x,y); - return x+y; + ext2_inode_content(0x80800,name,0x100000,0x100000); + // autorun "user-space" prog + asm("push $0x100000"); + asm("ret"); } - -int example_syscall_2(int x,int y) -{ - // log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"example syscall 2 called with %d - %d",x,y); - return x-y; -} - -// diff --git a/userspace/Makefile b/userspace/Makefile index a26d554..ceba7d0 100644 --- a/userspace/Makefile +++ b/userspace/Makefile @@ -8,7 +8,7 @@ LDFLAGS=-L/home/miguel/temp/fool-os-stuff/newlib-build-clean/i686-elf/newlib/ \ -L/home/miguel/temp/fool-os-stuff/newlib-build-clean/i686-elf/libgloss/libnosys/ \ -lnosys -ext2.img: userprog +ext2.img: shell simple dd if=/dev/zero of=ext2.img bs=512 count=200 sudo mkfs.ext2 -O none ext2.img -F mkdir mnt @@ -17,16 +17,24 @@ ext2.img: userprog mkdir mnt/miguel echo "hello one" > mnt/miguel/test1.txt echo "hello two" > mnt/test2.txt - cp userprog mnt/shell + cp shell mnt + cp simple mnt sync sudo umount mnt rm mnt -rf -userprog: foolshell.o crt0.o - ${CC} -T linker.ld ${LDFLAGS} $< -Wl,--oformat,binary -o userprog +shell: foolshell.o crt0.o + ${CC} -T linker.ld ${LDFLAGS} $< -Wl,--oformat,binary -o shell + +simple: simple.o crt0.o + ${CC} -T linker.ld ${LDFLAGS} $< -Wl,--oformat,binary -o simple clean: - -rm *.o *.out userprog ext2.img + -rm *.o *.out shell simple ext2.img + +umount: + sudo umount mnt + rm mnt -rf diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 8bf8288..3fe7353 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -86,7 +86,7 @@ int process(char *buf) if(!strcmp(command,"help")) { - puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'ls [inode_nr]', 'malloc [bytes]', 'free [address]', "); + puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'ls [inode_nr]', exec [inode_nr],'malloc [bytes]', 'free [address]'"); } else if(!strcmp(command,"ls")) { @@ -101,6 +101,11 @@ int process(char *buf) } } + else if(!strcmp(command,"exec")) + { + execve(atoi(token[1]),0,0); + + } else if(!strcmp(command,"echo")) { printf("foolshell: \"%s\"\n",token[1]); diff --git a/userspace/simple.c b/userspace/simple.c new file mode 100644 index 0000000..36f0580 --- /dev/null +++ b/userspace/simple.c @@ -0,0 +1,18 @@ +#include "syscalls.c" + +int main(int argc, char **argv) +{ + syscalls_init(); + + int i; + + for(i=0;i<10;i++) + { + write(1,"dupa\n",5); + } + + // replace with foolshell after finishing! + execve(15,0,0); +} + + diff --git a/userspace/syscalls.c b/userspace/syscalls.c index 11976bc..b5959eb 100644 --- a/userspace/syscalls.c +++ b/userspace/syscalls.c @@ -10,36 +10,58 @@ static int preread; static int alloc; easywrite(char *c); +// init syscalls void syscalls_init() { - preread=0; + // TODO dynamic! alloc=0x300000; } -// +// generic syscall interface! +int syscall(int call, int p1, int p2, int p3) +{ + int ebx; // will hold return value; + + asm("pusha"); + + // select syscall + asm("mov %0, %%eax"::"m"(call)); + + // pass params + asm("mov %0,%%edx"::"m"(p1)); + asm("mov %0,%%ecx"::"m"(p2)); + asm("mov %0,%%ebx"::"m"(p3)); + // interrrupt + asm("int $0x80"); + + // get return value + asm("mov %%ebx, %0": "=b" (ebx)); + + asm("popa"); + + return ebx; +} + +// int close(int file) { -// easywrite("syscall: close\n"); return -1; } int fstat(int file, struct stat *st) { -// easywrite("syscall: fstat\n"); st->st_mode = S_IFCHR; return 0; } int isatty(int file) { -// easywrite("syscall: isatty\n"); return 1; } int lseek(int file, int ptr, int dir) { -// printf("syscall: lseek %i %i %i",file,ptr,dir); if(dir==0)preread=ptr; else{ puts("other modes unsupported sorry"); while(1);} return preread; @@ -47,86 +69,29 @@ int lseek(int file, int ptr, int dir) int read(int file, char *ptr, int len) { - int ebx; // will hold return value; - - asm("pusha"); - - // select syscall - asm("mov $62,%eax"); - - // pass params - asm("mov %0,%%edx"::"m"(file)); - asm("mov %0,%%ecx"::"m"(ptr)); - asm("mov %0,%%ebx"::"m"(len)); - - // interrrupt - asm("int $0x80"); - - // get return value - asm("mov %%ebx, %0": "=b" (ebx)); - - asm("popa"); - - return ebx; + return syscall(62,file,ptr,len); } int readdir(const char *name,fs_dirent *dirs,int max) { - int ebx; // WILL Hold return value; - - asm("pusha"); - - // select syscall - asm("mov $63,%eax"); - - // pass params - asm("mov %0,%%edx"::"m"(name)); - asm("mov %0,%%ecx"::"m"(dirs)); - asm("mov %0,%%ebx"::"m"(max)); - - // interrrupt - asm("int $0x80"); - - // get return value - asm("mov %%ebx, %0": "=b" (ebx)); - - asm("popa"); - - return ebx; + return syscall(63,name,dirs,max); } int open(const char *name, int flags, int mode) { -// easywrite("syscall: open\n"); - return 99; - return -1; + return name; } int write(int file, char *ptr, int len) { + return syscall(61,file,ptr,len); +} - int ebx; // will hold return value; - - asm("pusha"); - - // select syscall - asm("mov $61,%eax"); - - // pass params - asm("mov %0,%%edx"::"m"(file)); - asm("mov %0,%%ecx"::"m"(ptr)); - asm("mov %0,%%ebx"::"m"(len)); - - // interrupt - asm("int $0x80"); - - // get return value - asm("mov %%ebx, %0": "=b" (ebx)); +int execve(char *name, char **argv, char **env) +{ - asm("popa"); - - return ebx; + return syscall(64,name,argv,env); } @@ -160,8 +125,3 @@ caddr_t sbrk(int incr) } // -// helpers -easywrite(char *c) -{ - write(1,c,strlen(c)); -} |
