feature (base): started basic libc setup

This commit is contained in:
antifallobst 2023-04-21 00:03:39 +02:00
parent 0f52c02435
commit a26fb4035e
8 changed files with 155 additions and 5 deletions

View File

@ -8,13 +8,13 @@ set(CMAKE_NASM_COMPILE_OBJECT "nasm <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")
add_library(libc) add_library(libc)
add_library(libc_asm OBJECT) add_library(libc_asm OBJECT)
target_include_directories(libc PRIVATE inc) target_include_directories(libc PRIVATE inc)
file(GLOB_RECURSE libc_src_c "src/**/*.c") file(GLOB_RECURSE libc_src_c "src/**.c")
file(GLOB_RECURSE libc_inc "inc/**/*.h") file(GLOB_RECURSE libc_inc "inc/**.h")
file(GLOB_RECURSE libc_src_asm "src/**/*.asm") 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_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_link_options(libc PRIVATE -Bsymbolic -nostdlib -shared -fno-stack-protector)
enable_language(ASM_NASM) enable_language(ASM_NASM)
set(CAN_USE_ASSEMBLER TRUE) set(CAN_USE_ASSEMBLER TRUE)

13
inc/nox/status.h Normal file
View File

@ -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

23
inc/nox/stdio.h Normal file
View File

@ -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

22
inc/nox/stdtypes.h Normal file
View File

@ -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

12
inc/nox/string.h Normal file
View File

@ -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

45
inc/syscall.h Normal file
View File

@ -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

26
src/stdio.c Normal file
View File

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

9
src/string.c Normal file
View File

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