From 51d4dd040a291b62c648ff6cc0d7e0058cf4056f Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 17 Aug 2018 17:51:51 +0200 Subject: starting implicit function calls cleanup --- xxx/sys/Makefile | 28 ++++++++ xxx/sys/crt0.S | 23 ++++++ xxx/sys/sgtty.h | 19 +++++ xxx/sys/sys.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++ xxx/sys/sys/ioctl.h | 0 xxx/sys/sys/termios.h | 27 ++++++++ xxx/sys/syscalls.c | 158 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 443 insertions(+) create mode 100644 xxx/sys/Makefile create mode 100644 xxx/sys/crt0.S create mode 100644 xxx/sys/sgtty.h create mode 100644 xxx/sys/sys.c create mode 100644 xxx/sys/sys/ioctl.h create mode 100644 xxx/sys/sys/termios.h create mode 100644 xxx/sys/syscalls.c (limited to 'xxx') diff --git a/xxx/sys/Makefile b/xxx/sys/Makefile new file mode 100644 index 0000000..289da1e --- /dev/null +++ b/xxx/sys/Makefile @@ -0,0 +1,28 @@ +CC=i686-elf-gcc +LD=i686-elf-ld + +#CFLAGS= +#CFLAGS+=-w +#CFLAGS+=-I../.. +#CFLAGS+=-gstabs +#CFLAGS+=-I. + +#LDFLAGS=-L/home/miguel/foolos/usr/i686-foolos/lib/ -lc -lm -lg -lnosys +#CFLAGS+=-I/home/miguel/foolos/usr/i686-foolos/include + +#install: crt_install header_install + +#crt_install: crt0.o +# cp crt0.o $(SYSROOT)/usr/lib/ + +#crt0.o: crt.o sys.o #syscalls.o +# $(LD) -r $^ -o $@ + +#header_install: +# cp *.h $(SYSROOT)/usr/include/ +# cp sys/*.h $(SYSROOT)/usr/include/sys/ + +crt0.o: crt0.S + +clean: + -rm -f *.o *.a diff --git a/xxx/sys/crt0.S b/xxx/sys/crt0.S new file mode 100644 index 0000000..2d6cbbd --- /dev/null +++ b/xxx/sys/crt0.S @@ -0,0 +1,23 @@ +.global _start + +_start: + +jmp . + +##pop %eax +##mov %eax, environ + +##pop %eax +#mov %eax, _impure_ptr + +##call main + + +##push environ +##push %eax +##call _exit2 + +# this should never be reached anyway! +.wait: + hlt +jmp .wait diff --git a/xxx/sys/sgtty.h b/xxx/sys/sgtty.h new file mode 100644 index 0000000..24d0218 --- /dev/null +++ b/xxx/sys/sgtty.h @@ -0,0 +1,19 @@ +#define TIOCFLUSH 0x01 +#define RAW 0x02 +#define CBREAK 0x04 +#define XTABS 0x08 +#define CRMOD 0x10 +#define ECHO 0x20 + +// TODO: same struct should be used for /terminal/terminal.h ? +struct sgttyb{ + + int sg_ospeed; + int sg_ispeed; + + char sg_erase; + char sg_kill; + + int sg_flags; + +}; diff --git a/xxx/sys/sys.c b/xxx/sys/sys.c new file mode 100644 index 0000000..4ff77f3 --- /dev/null +++ b/xxx/sys/sys.c @@ -0,0 +1,188 @@ +#include +#include +#include +#include + +// CODE FOR Stack Smashing Protector, TODO: MOVE / and do not duplicate +// with kernel.c +// http://wiki.osdev.org/Stack_Smashing_Protector +#include +#include + +#if UINT32_MAX == UINTPTR_MAX +#define STACK_CHK_GUARD 0xe2dee396 +#else +#define STACK_CHK_GUARD 0x595e9fbd94fda766 +#endif + +uintptr_t __stack_chk_guard = STACK_CHK_GUARD; + +__attribute__((noreturn)) +void __stack_chk_fail(void) +{ + write(1,"stack smashing!\n",16); + exit(99); +} + + +/////////////////// + +// required by binutils! + +long sysconf(int name) +{ + printf("UNIMPL: sysconf\n"); + printf("SYSCONF CALLED WITH : %s\n",name); + return 0; +} + +// set file mode creation mask +mode_t umask(mode_t mask) +{ + printf("UNIMPL: umask\n"); + return mask; +} + +// chmod +int chmod(const char * path, mode_t mode) +{ + printf("UNIMPL: chmod\n"); + return -1; +} + +// manipulating file descriptor +int fcntl(int fd, int cmd, ...) +{ + printf("UNIMPL: fcntl\n"); + return -1; +} + +// working directory +char *getwd(char *buf) +{ + printf("UNIMPL: getwd\n"); + static char wd[]="/"; + buf=wd; + return buf; +} + +// check if access allowed +int access(const char *pathname, int mode) +{ + printf("UNIMPL: access\n"); + //TODO: at leas check if this file exists! + return 0; +} + +// update time +int utime(const char *filename, const int *x) +{ + printf("UNIMPL: utime\n"); + return -1; +} + +// rmdir +int rmdir (const char *pathname) +{ + printf("UNIMPL: rmdir\n"); + return -1; +} + +// chonw +int chown(const char *path, uid_t owner, gid_t group) +{ + printf("UNIMPL: chown\n"); + return -1; +} + +// termios / ncurses +int tcgetattr(int fd, struct termios *termios_p) +{ + printf("UNIMPL: tcgetattr\n"); + return 0; +} +int tcsetattr(int fd, int optional_actions, const struct termios *termios_p){ + printf("UNIMPL: tsetattr\n"); + return 0; +} +int mkdir(const char *pathname, mode_t mode) +{ + printf("UNIMPL: mkdir\n"); + return -1; +} +int chdir (const char *pathname) +{ + printf("UNIMPL: chdir\n"); + return -1; +} + +// TODO; check if this is allright for not-real serial line!? +// http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC242 +// here they write something about padding may be affected? experiment. +speed_t cfgetospeed(const struct termios *termios_p) +{ +// printf("UNIMPL: cfgetospeed\n"); + return 9600; +} + +char *ttyname(int fd) +{ + printf("UNIMPL: ttyname\n"); + return "foolterm"; +} + + +DIR *opendir(const char *name) +{ + printf("UNIMPL: opendir\n"); + return 0; +} +int closedir(DIR *dirp) +{ + printf("UNIMPL: closedir\n"); + return 0; +} + + +int tcflush(int fd, int queue_selector) +{ + printf("UNIMPL: tcflush\n"); + return -1; +} +long fpathconf(int fd, int name) +{ + printf("UNIMPL: fpathconf\n"); + return -1; +} + + +unsigned int sleep(unsigned int seconds) +{ + printf("UNIMPL: sleep\n"); + return 0; +} + +char *getlogin(void) +{ + printf("UNIMPL: getlogin\n"); + return NULL; +} + +int ioctl(int fd, unsigned long request, ...) +{ + printf("UNIMPL: ioctl\n"); + return -1; +} + +int gtty(int fd, void *buf) +{ + // printf("UNIMPL: gettty\n"); + return -1; +} + +int stty(int fd, void *buf) +{ + // printf("UNIMPL: settty\n"); + return -1; +} + diff --git a/xxx/sys/sys/ioctl.h b/xxx/sys/sys/ioctl.h new file mode 100644 index 0000000..e69de29 diff --git a/xxx/sys/sys/termios.h b/xxx/sys/sys/termios.h new file mode 100644 index 0000000..9b9cdc4 --- /dev/null +++ b/xxx/sys/sys/termios.h @@ -0,0 +1,27 @@ +#include + +typedef uint32_t speed_t; +typedef uint32_t DIR; + +#define B0 0000000 /* hang up */ +#define B50 0000001 +#define B75 0000002 +#define B110 0000003 +#define B134 0000004 +#define B150 0000005 +#define B200 0000006 +#define B300 0000007 +#define B600 0000010 +#define B1200 0000011 +#define B1800 0000012 +#define B2400 0000013 +#define B4800 0000014 +#define B9600 0000015 +#define B19200 0000016 +#define B38400 0000017 + +struct direct{ + char d_name[256]; + int d_namlen +}; + diff --git a/xxx/sys/syscalls.c b/xxx/sys/syscalls.c new file mode 100644 index 0000000..10e035c --- /dev/null +++ b/xxx/sys/syscalls.c @@ -0,0 +1,158 @@ +#include "kernel/syscalls.h" + +extern char **environ; +//struct _reent *_impure_ptr; + +// generic syscall interface! + +int __attribute__ ((noinline)) syscall(int call, int p1, int p2, int p3) +{ + int ebx; // will hold return value; + + // select syscall and pass params + asm("pusha"); + + asm("mov %0, %%eax"::"m"(call)); + + 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; +} + +// fool os custom +int readdir(const char *name,fs_dirent *dirs,int max) +{ + + return syscall(SYSCALL_READDIR,name,dirs,max); +} + +int has_data_waiting() +{ + + return syscall(SYSCALL_HAS_DATA,0,0,0); +} + +int fool_tune(int v1, int v2, int v3) +{ + + return syscall(SYSCALL_TUNE,v1,v2,v3); +} + +void _exit(int ret) +{ + _exit2(ret,environ); +} + +void _exit2(int ret,char **environ) +{ + return syscall(SYSCALL_EXIT,ret,environ,0); +} + +//required by newlibc +int close(int file) +{ + return syscall(SYSCALL_CLOSE,file,0,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 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 unlink(char *name) +{ + return syscall(SYSCALL_UNLINK,name,0,0); +} + +int times(struct tms *buf) +{ + return syscall(SYSCALL_TIMES,buf,0,0); +} + +int wait(int *status) +{ + return syscall(SYSCALL_WAIT,status,0,0); +} + +int stat(const char *file, struct stat *st) +{ + return syscall(SYSCALL_STAT,file,st,0); +} + +int lstat(const char *file, struct stat *st) +{ + return syscall(SYSCALL_LSTAT,file,st,0); +} + +int fstat(int file, struct stat *st) +{ + return syscall(SYSCALL_FSTAT,file,st,0); +} + + -- cgit v1.2.3