diff --git a/include/c/stddef.h b/include/c/stddef.h new file mode 100644 index 0000000..fbe6e30 --- /dev/null +++ b/include/c/stddef.h @@ -0,0 +1,9 @@ +#ifndef STDDEF_H +#define STDDEF_H + +#define NULL ((void*)0) + +typedef unsigned int size_t; +typedef signed int ssize_t; + +#endif diff --git a/include/c/stdint.h b/include/c/stdint.h index ae0b2a3..acc4ea0 100644 --- a/include/c/stdint.h +++ b/include/c/stdint.h @@ -1,7 +1,7 @@ #ifndef STDINT_H #define STDINT_H -#define NULL ((void*)0) +#include // Signed ints typedef signed char int8_t; @@ -15,7 +15,6 @@ typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef unsigned long long uint64_t; -// Other -typedef unsigned int size_t; +// TODO: floats #endif diff --git a/include/c/stdio.h b/include/c/stdio.h index 4c8c1ae..d219cd5 100644 --- a/include/c/stdio.h +++ b/include/c/stdio.h @@ -17,31 +17,6 @@ FILE* stdin; FILE* stdout; FILE* stderr; -/* --- Syscall Wrappers --- */ -int write(int fd, const void* ptr, size_t count) { - int result = 0; - __asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_WRITE), "b"(fd), "c"(ptr), "d"(count)); - return result; -} - -int read(int fd, void* ptr, size_t count) { - int result = 0; - __asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_GETS), "b"(fd), "c"(ptr), "d"(count)); - return result; -} - -int open(const char* path, int oflag) { - int fd = -1; - __asm__ volatile ("int $0x80" : "=a"(fd) : "a"(SYS_OPEN), "b"(path), "c"(oflag)); - return fd; -} - -int close(int fd) { - int result = -1; - __asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_CLOSE), "b"(fd)); - return result; -} - /* --- file I/O --- */ FILE* fopen(const char* filename, const char* mode) { FILE* stream = malloc(sizeof(FILE)); @@ -90,7 +65,6 @@ size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream) { } return writtenEl; - } int fseek(FILE* stream, long int origin, int offset) { diff --git a/include/c/stdlib.h b/include/c/stdlib.h index 00ad0f1..e8bc904 100644 --- a/include/c/stdlib.h +++ b/include/c/stdlib.h @@ -5,15 +5,11 @@ #include void* malloc(const uint32_t size) { - void* ptr = 0; - __asm__ volatile ("int $0x80" : : "a"(SYS_MALLOC), "b"(size)); - __asm__ volatile ("movl %%eax, %0" : "=r"(ptr)); - - return ptr; + return (void*)syscall(SYS_MALLOC, size, 0, 0); } void free(const void* ptr) { - __asm__ volatile ("int $0x80" : : "a"(SYS_FREE), "b"(ptr)); + syscall(SYS_FREE, (int)ptr, 0, 0); } #endif diff --git a/include/c/syscall.h b/include/c/syscall.h index 93821e8..2070b58 100644 --- a/include/c/syscall.h +++ b/include/c/syscall.h @@ -1,12 +1,14 @@ -#ifndef SYSCALLNUMS_H -#define SYSCALLNUMS_H +#ifndef SYSCALL_H +#define SYSCALL_H + +#include #define MAX_SYSCALLS 9 typedef enum { SYS_RESERVED, SYS_SLEEP, - SYS_GETS, + SYS_READ, SYS_WRITE, SYS_OPEN, SYS_CLOSE, @@ -25,4 +27,31 @@ typedef enum { O_BIN = 0x40, // Read bytes, not text } OFLAGS; +int syscall(int nr, int arg0, int arg1, int arg2) { + int ret = 0; + __asm__ volatile ("int $0x80" : "=a"(ret) : "a"(nr), "b"(arg0), "c"(arg1), "d"(arg2)); + return ret; +} + +/* Syscall wrappers */ +int read(int fd, void* ptr, size_t count) { + // Returns: int fd + return syscall(SYS_READ, fd, (int)ptr, count); +} + +int write(int fd, const void* ptr, size_t count) { + // Returns: int count + return syscall(SYS_WRITE, fd, (int)ptr, count); +} + +int open(const char* path, int oflag) { + // Returns: int fd + return syscall(SYS_OPEN, (int)path, oflag, 0); +} + +int close(int fd) { + // Returns: int status + return syscall(SYS_CLOSE, fd, 0, 0); +} + #endif diff --git a/include/interrupt/syscalls.h b/include/interrupt/syscalls.h index a79ad34..5d2d6d7 100644 --- a/include/interrupt/syscalls.h +++ b/include/interrupt/syscalls.h @@ -1,5 +1,5 @@ -#ifndef SYSCALL_H -#define SYSCALL_H +#ifndef INTERRUPT_SYSCALLS_H +#define INTERRUPT_SYSCALLS_H #include #include @@ -47,7 +47,7 @@ void sys_write() { // TODO: Use a Read() sycall and read from stdin // Args: ebx - char* buffer -void sys_gets() { +void sys_read() { char* s; __asm__ volatile ("movl %%ebx, %0" : "=r"(s)); reads(s); @@ -102,7 +102,7 @@ void sys_seek() {} void (*syscalls[MAX_SYSCALLS])(void) = { [SYS_SLEEP] = sys_sleep, [SYS_WRITE] = sys_write, - [SYS_GETS] = sys_gets, + [SYS_READ] = sys_read, [SYS_MALLOC] = sys_malloc, [SYS_FREE] = sys_free, [SYS_OPEN] = sys_open, diff --git a/kern.iso b/kern.iso index 52350e6..68bb134 100644 Binary files a/kern.iso and b/kern.iso differ