diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ad14b4..6260e7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,13 +8,13 @@ set(CMAKE_NASM_COMPILE_OBJECT "nasm -o ") add_library(libc) add_library(libc_asm OBJECT) target_include_directories(libc PRIVATE inc) -file(GLOB_RECURSE libc_src_c "src/**/*.c") -file(GLOB_RECURSE libc_inc "inc/**/*.h") -file(GLOB_RECURSE libc_src_asm "src/**/*.asm") +file(GLOB_RECURSE libc_src_c "src/**.c") +file(GLOB_RECURSE libc_inc "inc/**.h") +file(GLOB_RECURSE libc_src_asm "src/**.asm") -target_compile_options(libc PRIVATE "-g -fno-stack-protector -fno-stack-check -ffreestanding -m64 -march=x86-64 -mabi=sysv") -target_link_options(libc PRIVATE "-Bsymbolic -nostdlib -shared -fno-stack-protector") +target_compile_options(libc PRIVATE -g -fno-stack-protector -fno-stack-check -ffreestanding -m64 -march=x86-64 -mabi=sysv) +target_link_options(libc PRIVATE -Bsymbolic -nostdlib -shared -fno-stack-protector) enable_language(ASM_NASM) set(CAN_USE_ASSEMBLER TRUE) diff --git a/inc/nox/status.h b/inc/nox/status.h new file mode 100644 index 0000000..4ddec42 --- /dev/null +++ b/inc/nox/status.h @@ -0,0 +1,13 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef NOX_LIBC_STATUS_H +#define NOX_LIBC_STATUS_H + +typedef enum { + STATUS_SUCCESS, + + STATUS_PERMISSION_DENIED, + STATUS_RESOURCE_NOT_AVAILABLE +} status_E; + +#endif //NOX_LIBC_STATUS_H diff --git a/inc/nox/stdio.h b/inc/nox/stdio.h new file mode 100644 index 0000000..0c1d547 --- /dev/null +++ b/inc/nox/stdio.h @@ -0,0 +1,23 @@ +#ifndef NOX_LIBC_SYSCALL_H +#define NOX_LIBC_SYSCALL_H + +#include "stdtypes.h" +#include "string.h" + +typedef uint32_t file_t; + +enum { + FD_INVALID, + STDOUT, + STDIN, + STDERR +}; + +file_t fopen (string_t path); +void fclose (file_t file); +uint32_t fread (file_t file, uint32_t offset, void* buffer, uint32_t num); +uint32_t fwrite (file_t file, uint32_t offset, void* buffer, uint32_t num); + +void printf (string_t format); + +#endif //NOX_LIBC_SYSCALL_H \ No newline at end of file diff --git a/inc/nox/stdtypes.h b/inc/nox/stdtypes.h new file mode 100644 index 0000000..dcfe6d6 --- /dev/null +++ b/inc/nox/stdtypes.h @@ -0,0 +1,22 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef NOX_LIBC_STDTYPES_H +#define NOX_LIBC_STDTYPES_H + +typedef unsigned char uint8_t; +typedef signed char int8_t; +typedef unsigned short uint16_t; +typedef signed short int16_t; +typedef unsigned int uint32_t; +typedef signed int int32_t; +typedef unsigned long uint64_t; +typedef signed long int64_t; + +typedef _Bool bool; + +#define true 1 +#define false 0 + +#define NULL (void*)0 + +#endif //NOX_LIBC_STDTYPES_H diff --git a/inc/nox/string.h b/inc/nox/string.h new file mode 100644 index 0000000..f2f7dc0 --- /dev/null +++ b/inc/nox/string.h @@ -0,0 +1,12 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef NOX_LIBC_STRING_H +#define NOX_LIBC_STRING_H + +#include "nox/stdtypes.h" + +typedef const char* string_t; + +uint64_t strlen(string_t str); + +#endif //NOX_LIBC_STRING_H diff --git a/inc/syscall.h b/inc/syscall.h new file mode 100644 index 0000000..d880cca --- /dev/null +++ b/inc/syscall.h @@ -0,0 +1,45 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef LIBC_SYSCALL_H +#define LIBC_SYSCALL_H + +#include "nox/status.h" +#include "nox/stdtypes.h" + +typedef enum { + SYSCALL_FILES_OPEN = 0x0101, + SYSCALL_FILES_CLOSE = 0x0102, + SYSCALL_FILES_READ = 0x0103, + SYSCALL_FILES_WRITE = 0x0104, + SYSCALL_FILES_DELETE = 0x0105, + SYSCALL_FILES_LIST = 0x0106, + SYSCALL_FILES_INFO = 0x0107, + + SYSCALL_MEMORY_MAP = 0x0201, + SYSCALL_MEMORY_UNMAP = 0x0202, + SYSCALL_MEMORY_LABEL = 0x0203, + SYSCALL_MEMORY_RANGE = 0x0204, + SYSCALL_MEMORY_ACCESS = 0x0205, + + SYSCALL_PROCESS_CREATE = 0x0301, + SYSCALL_PROCESS_ENVFILE = 0x0302, + SYSCALL_PROCESS_START = 0x0303, + SYSCALL_PROCESS_SIGNAL = 0x0304, + + SYSCALL_RUNTIME_LINKER_OPEN = 0x0401, + SYSCALL_RUNTIME_LINKER_CLOSE = 0x0402, + SYSCALL_RUNTIME_LINKER_LOAD_SYMBOL = 0x0403, + SYSCALL_RUNTIME_LINKER_STATUS = 0x0404, + SYSCALL_RUNTIME_LINKER_STANDARD_MOD = 0x0405, + + SYSCALL_COMPATABILITY_ABI_TYPE = 0x0501, + SYSCALL_COMPATABILITY_ABI_VERSION = 0x0502, + SYSCALL_COMPATABILITY_ACTION = 0x0503, + + SYSCALL_KERNEL_SCHEDULER_START = 0xFF00, + SYSCALL_KERNEL_PANIC = 0xFF01 +} syscall_E; + +extern status_E syscall_perform(uint64_t id, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); + +#endif //LIBC_SYSCALL_H diff --git a/src/stdio.c b/src/stdio.c new file mode 100644 index 0000000..7c17540 --- /dev/null +++ b/src/stdio.c @@ -0,0 +1,26 @@ +// This file is part of noxos and licensed under the MIT open source license + +#include "nox/stdio.h" +#include "syscall.h" + +file_t fopen(string_t path) { + file_t fd; + syscall_perform(SYSCALL_FILES_OPEN, (uint64_t)path, 0, (uint64_t)&fd, 0); + return fd; +} + +void fclose(file_t file) { + syscall_perform(SYSCALL_FILES_CLOSE, file, 0, 0, 0); +} + +uint32_t fread(file_t file, uint32_t offset, void* buffer, uint32_t num) { + syscall_perform(SYSCALL_FILES_READ, file, offset, (uint64_t)buffer, num); +} + +uint32_t fwrite(file_t file, uint32_t offset, void* buffer, uint32_t num) { + syscall_perform(SYSCALL_FILES_WRITE, file, offset, (uint64_t)buffer, num); +} + +void printf(string_t format) { + fwrite(STDOUT, 0, (void*)format, strlen(format)); +} \ No newline at end of file diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..b338e5e --- /dev/null +++ b/src/string.c @@ -0,0 +1,9 @@ +// This file is part of noxos and licensed under the MIT open source license + +#include "nox/string.h" + +uint64_t strlen(string_t str) { + uint64_t n = 0; + while (*str++ != '\0') n++; + return n; +} \ No newline at end of file