diff --git a/.idea/misc.xml b/.idea/misc.xml index b678402dc..1a0e8a89a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,9 @@ + + + diff --git a/os3-toolchain/newlib/newlib-4.4.0.20231231/newlib/libc/sys/os3/syscalls.c b/os3-toolchain/newlib/newlib-4.4.0.20231231/newlib/libc/sys/os3/syscalls.c index 6ef4513e8..69bc372bb 100644 --- a/os3-toolchain/newlib/newlib-4.4.0.20231231/newlib/libc/sys/os3/syscalls.c +++ b/os3-toolchain/newlib/newlib-4.4.0.20231231/newlib/libc/sys/os3/syscalls.c @@ -1,29 +1,89 @@ /* note these headers are all provided by newlib - you don't need to provide them */ -#include -#include -#include -#include -#include -#include #include - -void _exit(){} -int close(int file){} +#include +#include +#include +#include +#include +#include + +#define SYSCALL_EXIT_ID 0 + +#define SYSCALL_PUTCHAR_ID 1 +#define SYSCALL_SLEEP_ID 2 +#define SYSCALL_READCHAR_ID 3 + +#define SYSCALL_OPEN_ID 4 +#define SYSCALL_CLOSE_ID 5 + +#define SYSCALL_READ_ID 6 +#define SYSCALL_WRITE_ID 7 +#define SYSCALL_LSEEK_ID 8 + +#define SYSCALL_OPENDIR_ID 9 +#define SYSCALL_READDIR_ID 10 +#define SYSCALL_CLOSEDIR_ID 11 +#define SYSCALL_MKDIR_ID 12 +#define SYSCALL_UNLINK_ID 13 + +#define SYSCALL_EXECVE_ID 50 + + +#define SYSCALL_PRINT_MEM 1000 +#define SYSCALL_PRINT_TASKS 1001 + +uint64_t _do_syscall(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t a3_rcx) { + uint64_t res; + asm volatile("syscall; mov (0x10016), %%rsp" // TASK_POINTER->ret_sp_val + : "=r"(res) + : "D"(id_rdi), "S"(a1_rsi), "d"(a2_rdx), "a"(a3_rcx) + : "cc", "rcx", "r8", + "r9", "r10", "r11", "r15", "memory"); + return res; +} + +void _exit() { + _do_syscall(SYSCALL_EXIT_ID, 0, 0, 0); +} +int close(int file) { + return _do_syscall(SYSCALL_CLOSE_ID, file, 0, 0); +} char **environ; /* pointer to array of char * strings that define the current environment variables */ -int execve(char *name, char **argv, char **env){} -int fork(){} -int fstat(int file, struct stat *st){} -int getpid(){} -int isatty(int file){} -int kill(int pid, int sig){} -int link(char *old, char *new){} -int lseek(int file, int ptr, int dir){} -int open(const char *name, int flags, ...){} -int read(int file, char *ptr, int len){} -caddr_t sbrk(int incr){} -int stat(const char *file, struct stat *st){} -clock_t times(struct tms *buf){} -int unlink(char *name){} -int wait(int *status){} -int write(int file, char *ptr, int len){} -int gettimeofday(struct timeval * restrict p, void * restrict z){} \ No newline at end of file +int execve(char *name, char **argv, char **env) { + return _do_syscall(SYSCALL_EXECVE_ID, (uint64_t) name, (uint64_t) argv, (uint64_t) env); +} +int fork() {} +int fstat(int file, struct stat *st) {} +int getpid() {} +int isatty(int file) {} +int kill(int pid, int sig) {} +int link(char *old, char *new) {} +int lseek(int file, int ptr, int dir) { + return _do_syscall(SYSCALL_LSEEK_ID, file, ptr, dir); +} +int open(const char *name, int flags, ...) { + return _do_syscall(SYSCALL_OPEN_ID, (uint64_t) name, flags, 0); +} +int read(int file, char *ptr, int len) { + return _do_syscall(SYSCALL_READ_ID, file, (uint64_t) ptr, len); +} +caddr_t sbrk(int incr) {} +int stat(const char *file, struct stat *st) {} +clock_t times(struct tms *buf) {} +int unlink(char *name) {} +int wait(int *status) {} + +int write(int file, char *ptr, int len) { + return _do_syscall(SYSCALL_WRITE_ID, file, (uint64_t) ptr, len); +} + +int sleep(int seconds) { + return _do_syscall(SYSCALL_SLEEP_ID, seconds * 1000, 0, 0); +} + +int usleep(useconds_t useconds) { + return _do_syscall(SYSCALL_SLEEP_ID, useconds, 0, 0); +} + + +int gettimeofday(struct timeval *restrict p, void *restrict z) {} \ No newline at end of file diff --git a/src/arch/x86/kmain.cpp b/src/arch/x86/kmain.cpp index c109aa290..bc3f5606d 100644 --- a/src/arch/x86/kmain.cpp +++ b/src/arch/x86/kmain.cpp @@ -126,15 +126,6 @@ void stress_tester() { } -void user_task() { - while (true) { - putchar('h'); - putchar('i'); - putchar('\n'); - sleep(100000); - } -} - void vfs_tester() { VFSTester vfsTester; vfsTester.test(); diff --git a/src/test/hello2.c b/src/test/hello2.c index cf92a2f0a..e0e5bb9c7 100644 --- a/src/test/hello2.c +++ b/src/test/hello2.c @@ -6,11 +6,12 @@ volatile int w = 0; volatile const char *hello = "hello xd"; -int main() { - if (x == 3) putchar('x'); - if (w == 2) putchar('w'); - if (asdfasdf[0] == '\0') putchar('a'); - putchar('h'); - putchar('i'); - putchar('\n'); +// +int main() { + if (x == 3) sputchar('x'); + if (w == 2) sputchar('w'); + if (asdfasdf[0] == '\0') sputchar('a'); + sputchar('h'); + sputchar('i'); + sputchar('\n'); } \ No newline at end of file diff --git a/src/test/init.c b/src/test/init.c index cbd4bfa48..43885903c 100644 --- a/src/test/init.c +++ b/src/test/init.c @@ -1,10 +1,14 @@ #include "syscalls_interface.h" +#include "stdio.h" +#include +#include +#include int main() { - // putchar('h'); - // putchar('i'); - // putchar('\n'); + // sputchar('h'); + // sputchar('i'); + // sputchar('\n'); uint64_t test123 = open("/test123", O_CREAT | O_RDWR); const char *teststr = "test str"; write(test123, teststr, 9); @@ -14,26 +18,26 @@ int main() { char buf[123]; read(test123, buf, 9); - putchar('\n'); + sputchar('\n'); for (int i = 0; i < 8; i++) { - putchar(buf[i]); + sputchar(buf[i]); } - putchar('\n'); + sputchar('\n'); - sleep(100); + usleep(100); execve("/hello2", 0, 0); while (1) { - // putchar('h'); - // putchar('i'); - putchar('\n'); - char read = readchar(); + // sputchar('h'); + // sputchar('i'); + sputchar('\n'); + char read = sreadchar(); if (read == 'm') print_mem(); if (read == 't') print_tasks(); if (read == 'h') execve("/hello2", 0, 0); - putchar('\n'); - putchar(read); + sputchar('\n'); + sputchar(read); // sleep(100000); } } \ No newline at end of file diff --git a/src/uapi/syscalls_interface.c b/src/uapi/syscalls_interface.c index 83e792b9e..87cf4eec3 100644 --- a/src/uapi/syscalls_interface.c +++ b/src/uapi/syscalls_interface.c @@ -16,46 +16,14 @@ uint64_t do_syscall(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t return res; } -void exit() { - do_syscall(SYSCALL_EXIT_ID, 0, 0, 0); -} - -uint64_t readchar() { +uint64_t sreadchar() { return do_syscall(SYSCALL_READCHAR_ID, 0, 0, 0); } -uint64_t putchar(char c) { +uint64_t sputchar(char c) { return do_syscall(SYSCALL_PUTCHAR_ID, c, 0, 0); } -uint64_t sleep(uint64_t micros) { - return do_syscall(SYSCALL_SLEEP_ID, micros, 0, 0); -} - -uint64_t open(const char *pathname, int flags) { - return do_syscall(SYSCALL_OPEN_ID, (uint64_t) pathname, flags, 0); -} - -uint64_t close(uint64_t FD) { - return do_syscall(SYSCALL_CLOSE_ID, FD, 0, 0); -} - -uint64_t read(uint64_t fd, char *buf, uint64_t len) { - return do_syscall(SYSCALL_READ_ID, fd, (uint64_t) buf, len); -} - -uint64_t write(uint64_t fd, const char *buf, uint64_t len) { - return do_syscall(SYSCALL_WRITE_ID, fd, (uint64_t) buf, len); -} - -uint64_t lseek(uint64_t fd, uint64_t off, uint64_t whence) { - return do_syscall(SYSCALL_LSEEK_ID, fd, off, whence); -} - -uint64_t execve(const char *pathname, char *const argv[], char *const envp[]) { - return do_syscall(SYSCALL_EXECVE_ID, (uint64_t) pathname, (uint64_t) argv, (uint64_t) envp); -} - void print_mem() { do_syscall(SYSCALL_PRINT_MEM, 0, 0, 0); } diff --git a/src/uapi/syscalls_interface.h b/src/uapi/syscalls_interface.h index 32c279726..8f13ab18a 100644 --- a/src/uapi/syscalls_interface.h +++ b/src/uapi/syscalls_interface.h @@ -16,20 +16,8 @@ extern "C" { #include "FileOpts.h" -void exit(); - -uint64_t putchar(char c); -uint64_t readchar(); -uint64_t sleep(uint64_t micros); - -uint64_t open(const char *pathname, int flags); -uint64_t close(uint64_t FD); - -uint64_t read(uint64_t fd, char *buf, uint64_t len); -uint64_t write(uint64_t fd, const char *buf, uint64_t len); -uint64_t lseek(uint64_t fd, uint64_t off, uint64_t whence); - -uint64_t execve(const char *pathname, char *const argv[], char *const envp[]); +uint64_t sputchar(char c); +uint64_t sreadchar(); void print_mem(); void print_tasks();