From 65a859ac5b721c8d3e0123351ad99690e2e7a876 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Fri, 21 Nov 2014 15:37:45 +0100 Subject: preparing switch to ELF userspace --- userspace/sys/crt0.S | 23 ++++++++ userspace/sys/oldlinker.ld | 25 +++++++++ userspace/sys/sys.c | 55 ++++++++++++++++++ userspace/sys/syscalls.c | 136 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 userspace/sys/crt0.S create mode 100644 userspace/sys/oldlinker.ld create mode 100644 userspace/sys/sys.c create mode 100644 userspace/sys/syscalls.c (limited to 'userspace/sys') diff --git a/userspace/sys/crt0.S b/userspace/sys/crt0.S new file mode 100644 index 0000000..48eadd9 --- /dev/null +++ b/userspace/sys/crt0.S @@ -0,0 +1,23 @@ +.global _start + +.extern main +.extern exit + +_start: + +push $0 +call sbrk + +#push $[_BSS_END_] +#call sbrk + +call main + + +push %eax +call _exit + +# this should never be reached anyway! +.wait: + hlt +jmp .wait diff --git a/userspace/sys/oldlinker.ld b/userspace/sys/oldlinker.ld new file mode 100644 index 0000000..4eee8eb --- /dev/null +++ b/userspace/sys/oldlinker.ld @@ -0,0 +1,25 @@ +OUTPUT_FORMAT(binary) + +SECTIONS +{ + . = 0x800000; + + .text : ALIGN(0x1000) { + _TEXT_START_ = .; + *(.text) + _TEXT_END_ = .; + } + + .data : ALIGN(0x1000) { + _DATA_START_ = .; + *(.data) + _DATA_END_ = .; + } + + .bss : ALIGN(0x1000) { + _BSS_START_ = .; + *(.bss) + _BSS_END_ = .; + } + +} diff --git a/userspace/sys/sys.c b/userspace/sys/sys.c new file mode 100644 index 0000000..7dcfaee --- /dev/null +++ b/userspace/sys/sys.c @@ -0,0 +1,55 @@ +#include +#include +#include + + +// building binutils required this stubs! +long sysconf(int name) +{ + return 0; +} + + +mode_t umask(mode_t mask) +{ + return mask; +} + +int chmod(const char * path, mode_t mode) +{ + return 0; +} + +int fcntl(int fd, int cmd, ...) +{ + return -1; +} + +int lstat(const char *path, struct stat *buf) +{ + return -1; +} + +char *getwd(char *buf) +{ + return buf; +} +int access(const char *pathname, int mode) +{ + return 0; +} + +int utime(const char *filename, const int *x) +{ + return 0; +} + +int rmdir (const char *pathname) +{ + return -1; +} + +int chown(const char *path, uid_t owner, gid_t group) +{ + return 0; +} diff --git a/userspace/sys/syscalls.c b/userspace/sys/syscalls.c new file mode 100644 index 0000000..51436fb --- /dev/null +++ b/userspace/sys/syscalls.c @@ -0,0 +1,136 @@ +#include "../kernel/syscalls.h" + +/* +char *__env3[]={0}; +char *__env1[]={"a=10","b=20"}; +char *__env2[]={"dupa=test2"}; +char **environ={__env1,__env2,__env3}; +*/ + +void _exit(int ret) +{ + return syscall(SYSCALL_EXIT,ret,0,0); +} + +// 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) +{ + return syscall(SYSCALL_CLOSE,file,0,0); +} + +int fstat(int file, struct stat *st) +{ + return syscall(SYSCALL_FSTAT,file,st,0); +} + +int isatty(int file) +{ + return syscall(SYSCALL_ISATTY,file,0,0); +} + +int lseek(int file, int ptr, int dir) +{ + return syscall(SYSCALL_LSEEK,file,ptr,dir); +} + +int read(int file, char *ptr, int len) +{ + + return syscall(SYSCALL_READ,file,ptr,len); +} + +int readdir(const char *name,fs_dirent *dirs,int max) +{ + + return syscall(SYSCALL_READDIR,name,dirs,max); +} + +int open(const char *name, int flags, int mode) +{ + return syscall(SYSCALL_OPEN,name,flags,mode); +} + +int write(int file, char *ptr, int len) +{ + return syscall(SYSCALL_WRITE,file,ptr,len); +} + +int execve(char *name, char **argv, char **env) +{ + return syscall(SYSCALL_EXECVE,name,argv,env); +} + +uint32_t sbrk(int incr) +{ + return syscall(SYSCALL_SBRK,incr,0,0); +} + +int gettimeofday(struct timeval *tv, void *tz) +{ + return syscall(SYSCALL_GETTIMEOFDAY,tv,tz,0); +} + +int fork(void) +{ + return syscall(SYSCALL_FORK,0,0,0); +} + +int getpid(void) +{ + return syscall(SYSCALL_GETPID,0,0,0); +} + +int kill(int pid, int sig) +{ + return syscall(SYSCALL_KILL,pid,sig,0); +} + +int link(char *old, char *ne) +{ + return syscall(SYSCALL_LINK,old,ne,0); +} + +int stat(const char *file, struct stat *st) +{ + return syscall(SYSCALL_STAT,file,st,0); +} + +int times(struct tms *buf) +{ + return syscall(SYSCALL_TIMES,buf,0,0); +} + +int unlink(char *name) +{ + return syscall(SYSCALL_UNLINK,name,0,0); +} + +int wait(int *status) +{ + return syscall(SYSCALL_WAIT,status,0,0); +} -- cgit v1.2.3