hack up some syscalls

This commit is contained in:
2024-03-29 21:56:36 +01:00
parent 517f549f8f
commit d8f302dc96
7 changed files with 118 additions and 103 deletions

3
.idea/misc.xml generated
View File

@@ -3,6 +3,9 @@
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<excludeRoots>
<file path="$PROJECT_DIR$/os3-toolchain/binutils-x86_64-os3/build" />
<file path="$PROJECT_DIR$/os3-toolchain/gcc-x86_64-os3/build" />
<file path="$PROJECT_DIR$/os3-toolchain/newlib/build" />
<file path="$PROJECT_DIR$/toolchain" />
</excludeRoots>
</component>

View File

@@ -1,29 +1,89 @@
/* note these headers are all provided by newlib - you don't need to provide them */
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/times.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <stdio.h>
void _exit(){}
int close(int file){}
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#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){}
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) {}

View File

@@ -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();

View File

@@ -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');
}

View File

@@ -1,10 +1,14 @@
#include "syscalls_interface.h"
#include "stdio.h"
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/unistd.h>
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);
}
}

View File

@@ -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);
}

View File

@@ -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();