Updated userspace

This commit is contained in:
Alex 2022-12-24 09:18:45 +02:00
parent 0ce6433311
commit 40410cba41
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
25 changed files with 472 additions and 36 deletions

View File

@ -4,7 +4,9 @@
"name": "Linux", "name": "Linux",
"includePath": [ "includePath": [
"${workspaceFolder}/libc/include/**", "${workspaceFolder}/libc/include/**",
"${workspaceFolder}/out/system/include/**" "${workspaceFolder}/out/system/include/**",
"${workspaceFolder}/libs/include/**",
"${workspaceFolder}/libs/include"
], ],
"defines": [ "defines": [
"__debug_vscode__", "__debug_vscode__",
@ -19,6 +21,10 @@
"configurationProvider": "ms-vscode.makefile-tools", "configurationProvider": "ms-vscode.makefile-tools",
"compilerArgs": [ "compilerArgs": [
"-pipe", "-pipe",
"-nostdinc++",
"-nostdinc",
"-fno-builtin",
"-ffreestanding"
] ]
} }
], ],

View File

@ -53,7 +53,7 @@ build: $(FILENAME)
mv $(FILENAME) ../../../out/system/$(FILENAME) mv $(FILENAME) ../../../out/system/$(FILENAME)
$(FILENAME): $(OBJ) $(FILENAME): $(OBJ)
$(CC) $(LDFLAGS) $(SYSROOT) $(OBJ) -lssp -linit -o $@ $(CC) $(LDFLAGS) $(SYSROOT) $(OBJ) -lssp -linit -lsys -lgraph -o $@
%.o: %.c $(HEADERS) %.o: %.c $(HEADERS)
$(info Compiling $<) $(info Compiling $<)

View File

@ -1,13 +1,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <init.h>
#include <aux.h> #include <aux.h>
#include <syslib.h>
#include <sysbase.h>
#include <sysfile.h>
#include <init.h>
void PutCharToKernelConsole(char c)
{
__asm__ __volatile__("syscall"
:
: "a"(1), "D"(c), "S"(0)
: "rcx", "r11", "memory");
}
#define print(m, ...) init_log(m, ##__VA_ARGS__) #define print(m, ...) init_log(m, ##__VA_ARGS__)
int main(int argc, char *argv[], char *envp[]) int main(int argc, char *argv[], char *envp[])
{ {
print("Hello World!\n"); for (int i = 0; i < 14; i++)
PutCharToKernelConsole("\nHello World!\n"[i]);
print("%p %p %p\n", (void *)(uint64_t)&argc, (void *)&argv, (void *)&envp); print("%p %p %p\n", (void *)(uint64_t)&argc, (void *)&argv, (void *)&envp);
print("I have %d arguments\n", argc); print("I have %d arguments\n", argc);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
@ -30,5 +41,17 @@ int main(int argc, char *argv[], char *envp[])
for (auxv = (Elf64_auxv_t *)e; auxv->a_type != AT_NULL; auxv++) for (auxv = (Elf64_auxv_t *)e; auxv->a_type != AT_NULL; auxv++)
print("auxv: %ld %#lx\n", auxv->a_type, auxv->a_un.a_val); print("auxv: %ld %#lx\n", auxv->a_type, auxv->a_un.a_val);
File *test = FileOpen("/Test.txt", FILE_READ);
if (test == NULL)
{
print("Failed to open file\n");
return 1;
}
char buf[1024];
uint64_t read = FileRead(test, (uint8_t *)buf, 1024);
print("Read %ld bytes from file\n", read);
print("File contents: %s\n", buf);
FileClose(test);
return 0; return 0;
} }

53
libc/Interpreter/Makefile Normal file
View File

@ -0,0 +1,53 @@
# Config file
include ../../../Makefile.conf
NAME=ld
OBJECT_NAME=$(NAME).so
SO_NAME=$(OBJECT_NAME)
OUTPUT_DIR=../../out/system/
SYSROOT = --sysroot=../../out/system/
CC = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)gcc
AS = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)as
AR = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)ar
OBJDUMP = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)objdump
NASM = /usr/bin/nasm
C_SOURCES = $(shell find ./ -type f -name '*.c')
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp')
S_SOURCES = $(shell find ./ -type f -name '*.S')
ASM_SOURCES = $(shell find ./ -type f -name '*.asm')
OBJ = ${C_SOURCES:.c=.o} ${CPP_SOURCES:.cpp=.o} ${ASM_SOURCES:.asm=.o} ${S_SOURCES:.S=.o}
INCLUDE = ../include
ifeq ($(OSARCH), amd64)
ASM_ARCH := elf64
else ifeq ($(OSARCH), i686)
ASM_ARCH := elf32
endif
CFLAGS := -fPIC -I$(INCLUDE)
build: $(OBJECT_NAME)
$(OBJECT_NAME): $(OBJ)
$(CC) -nostdlib -static -fPIC -fPIE -Wl,-soname,$(SO_NAME) $(SYSROOT) $(OBJ) -o $(OUTPUT_DIR)$@
$(OBJDUMP) -d $(OUTPUT_DIR)$@ > file_dump.map
%.o: %.c
$(CC) $(CFLAGS) -std=c17 -c $< -o $@
%.o: %.cpp
$(CC) $(CFLAGS) -std=c++20 -c $< -o $@
%.o: %.S
$(AS) -c $< -o $@
%.o: %.asm
$(NASM) $< -f $(ASM_ARCH) -o $@
clean:
rm -f file_dump.map $(OBJ)

14
libc/Interpreter/hash.c Normal file
View File

@ -0,0 +1,14 @@
#include "ld.h"
uint32_t ElfHash(const unsigned char *Name)
{
uint32_t i = 0, j;
while (*Name)
{
i = (i << 4) + *Name++;
if ((j = i & 0xF0000000) != 0)
i ^= j >> 24;
i &= ~j;
}
return i;
}

12
libc/Interpreter/ld.c Normal file
View File

@ -0,0 +1,12 @@
#include "ld.h"
int main(int argc, char *argv[], char *envp[])
{
if (argc < 2)
return -1;
int envc = 0;
while (envp[envc] != NULL)
envc++;
return -0xD0D0;
}

8
libc/Interpreter/ld.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __FENNIX_LIBC_LD_H__
#define __FENNIX_LIBC_LD_H__
#include <types.h>
uint32_t ElfHash(const unsigned char *Name);
#endif // !__FENNIX_LIBC_LD_H__

View File

@ -3,7 +3,9 @@ build:
cp ../../Kernel/syscalls.h ../out/system/include/sys cp ../../Kernel/syscalls.h ../out/system/include/sys
make --quiet -C runtime build make --quiet -C runtime build
make --quiet -C src build make --quiet -C src build
make --quiet -C Interpreter build
clean: clean:
make -C runtime clean make -C runtime clean
make -C src clean make -C src clean
make -C Interpreter clean

View File

@ -12,6 +12,7 @@ extern "C"
void *memset(void *, int, size_t); void *memset(void *, int, size_t);
char *strcpy(char *, const char *); char *strcpy(char *, const char *);
size_t strlen(const char *); size_t strlen(const char *);
int strncmp(const char *s1, const char *s2, size_t n);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include "../mem/liballoc_1_1.h" #include "../mem/liballoc_1_1.h"
@ -37,10 +38,20 @@ int atoi(const char *nptr)
return OutBuffer; return OutBuffer;
} }
char **environ = NULL;
char *getenv(const char *name) char *getenv(const char *name)
{ {
static char *env = "PATH=/bin"; char **env = environ;
return env; if (env == NULL)
return NULL;
size_t len = strlen(name);
while (*env != NULL)
{
if ((strncmp(*env, name, len) == 0) && ((*env)[len] == '='))
return &(*env)[len + 1];
++env;
}
} }
void *malloc(size_t Size) { return PREFIX(malloc)(Size); } void *malloc(size_t Size) { return PREFIX(malloc)(Size); }

14
libc/src/string.c Normal file
View File

@ -0,0 +1,14 @@
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++)
{
char c1 = s1[i], c2 = s2[i];
if (c1 != c2)
return c1 - c2;
if (!c1)
return 0;
}
return 0;
}

View File

@ -4,9 +4,11 @@ build:
make --quiet -C libinit build make --quiet -C libinit build
make --quiet -C libssp build make --quiet -C libssp build
make --quiet -C libsys build make --quiet -C libsys build
make --quiet -C libgraph build
clean: clean:
make -C libgcc clean make -C libgcc clean
make -C libinit clean make -C libinit clean
make -C libssp clean make -C libssp clean
make -C libsys clean make -C libsys clean
make -C libgraph clean

21
libs/include/sysbase.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __FENNIX_LIBS_BASE_H__
#define __FENNIX_LIBS_BASE_H__
#include <stddef.h>
enum KCtl
{
KCTL_NULL,
KCTL_GET_PID,
KCTL_GET_TID,
KCTL_GET_UID,
KCTL_GET_GID,
KCTL_GET_PAGE_SIZE,
};
long DoCtl(uint64_t Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4);
uintptr_t KrnlRequestPages(size_t Count);
void KrnlFreePages(uintptr_t Address, size_t Count);
#endif // !__FENNIX_LIBS_BASE_H__

32
libs/include/sysfile.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef __FENNIX_LIBS_SYS_FILE_H__
#define __FENNIX_LIBS_SYS_FILE_H__
#include <stddef.h>
typedef struct
{
void *KernelPrivate;
} File;
enum FileFlags
{
FILE_READ = 1,
FILE_WRITE = 2,
FILE_APPEND = 4,
FILE_CREATE = 8,
FILE_TRUNCATE = 16,
FILE_EXCLUSIVE = 32,
FILE_DIRECTORY = 64,
FILE_SYMLINK = 128,
FILE_NONBLOCK = 256,
FILE_CLOEXEC = 512,
};
File *FileOpen(const char *Path, uint64_t Flags);
void FileClose(File *File);
uint64_t FileRead(File *File, uint8_t *Buffer, uint64_t Size);
uint64_t FileWrite(File *File, uint8_t *Buffer, uint64_t Size);
uint64_t FileSeek(File *File, uint64_t Offset, uint64_t Whence);
uint64_t FileStatus(File *File);
#endif // !__FENNIX_LIBS_SYS_FILE_H__

View File

@ -1,17 +0,0 @@
#ifndef __FENNIX_LIBS_SYS_H__
#define __FENNIX_LIBS_SYS_H__
#include <types.h>
enum KCtl
{
KCTL_NULL,
KCTL_GETPID,
KCTL_GETTID,
KCTL_GETUID,
KCTL_GETGID,
};
long DoCtl(uint64_t Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4);
#endif // !__FENNIX_LIBS_SYS_H__

122
libs/include/sysproc.h Normal file
View File

@ -0,0 +1,122 @@
#ifndef __FENNIX_LIBS_SYS_PROC_H__
#define __FENNIX_LIBS_SYS_PROC_H__
#include <stddef.h>
enum ProcessState
{
PROCESS_STATE_READY,
PROCESS_STATE_RUNNING,
PROCESS_STATE_SLEEPING,
PROCESS_STATE_WAITING,
PROCESS_STATE_STOPPED,
PROCESS_STATE_TERMINATED
};
typedef struct
{
char Name[256];
unsigned long ID;
enum ProcessState State;
void *KernelPrivate;
} Process;
typedef struct
{
char Name[256];
unsigned long ID;
enum ProcessState State;
void *KernelPrivate;
} Thread;
/**
* @brief Create a new process from a path
*
* @param Path Path to the executable
* @return Process* Pointer to the process structure
*/
Process *Spawn(const char *Path);
/**
* @brief Create a new thread
*
* @param EntryPoint Entry point of the thread
* @return Thread* Pointer to the thread structure
*/
Thread *SpawnThread(uintptr_t EntryPoint);
/**
* @brief Get list of threads
*
* @param Process Process to get the threads from
* @return Thread** Pointer to the thread list (NULL terminated)
*/
Thread **GetThreadList(Process *Process);
/**
* @brief Get process by ID
*
* @param ID Process ID
* @return Process* Pointer to the process structure
*/
Process *GetProcessByID(unsigned long ID);
/**
* @brief Get thread by ID
*
* @param ID Thread ID
* @return Thread* Pointer to the thread structure
*/
Thread *GetThreadByID(unsigned long ID);
/**
* @brief Get current process
*
* @return Process* Pointer to the process structure
*/
Process *GetCurrentProcess();
/**
* @brief Get current thread
*
* @return Thread* Pointer to the thread structure
*/
Thread *GetCurrentThread();
/**
* @brief [SYSTEM] Create a new empty process
*
* @param KernelPrivate Process parent
* @param Name Process name
* @param TrustLevel Process trust level [RESERVED FOR TRUSTED PROCESSES]
* @param Image Process file image already loaded in memory
* @return Process* Pointer to the process structure
*/
Process *KrnlCreateProcess(void *KernelPrivate,
const char *Name,
long TrustLevel,
void *Image);
/**
* @brief [SYSTEM] Create a new thread
*
* @param KernelPrivate Process parent
* @param EntryPoint Entry point of the thread
* @param argv Arguments
* @param envp Environment variables
* @param auxv Auxiliary variables
* @param Offset Offset of the entry point
* @param Architecture Architecture of the thread
* @param Compatibility Compatibility of the thread
* @return Thread* Pointer to the thread structure
*/
Thread *KrnlCreateThread(void *KernelPrivate,
unsigned long EntryPoint,
const char **argv,
const char **envp,
void *auxv,
unsigned long Offset,
long Architecture,
long Compatibility);
#endif // !__FENNIX_LIBS_SYS_PROC_H__

18
libs/libgraph/Graphics.c Normal file
View File

@ -0,0 +1,18 @@
#include <sysbase.h>
#include "../../../Kernel/syscalls.h"
long DoCtl(uint64_t Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4)
{
return syscall5(_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
}
uintptr_t KrnlRequestPages(size_t Count)
{
return syscall1(_RequestPages, Count);
}
void KrnlFreePages(uintptr_t Address, size_t Count)
{
syscall2(_FreePages, Address, Count);
}

51
libs/libgraph/Makefile Normal file
View File

@ -0,0 +1,51 @@
# Config file
include ../../../Makefile.conf
NAME=graph
OBJECT_NAME=lib$(NAME).so
SO_NAME=$(OBJECT_NAME)
OUTPUT_DIR=../../out/system/lib/
SYSROOT = --sysroot=../../out/system/
CC = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)gcc
AS = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)as
AR = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)ar
OBJDUMP = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)objdump
NASM = /usr/bin/nasm
C_SOURCES = $(shell find ./ -type f -name '*.c')
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp')
S_SOURCES = $(shell find ./ -type f -name '*.S')
ASM_SOURCES = $(shell find ./ -type f -name '*.asm')
OBJ = ${C_SOURCES:.c=.o} ${CPP_SOURCES:.cpp=.o} ${ASM_SOURCES:.asm=.o} ${S_SOURCES:.S=.o}
ifeq ($(OSARCH), amd64)
ASM_ARCH := elf64
else ifeq ($(OSARCH), i686)
ASM_ARCH := elf32
endif
CFLAGS := -fPIC -I../include -I../../libc/include
build: $(OBJECT_NAME)
$(OBJECT_NAME): $(OBJ)
$(CC) -nostdlib -shared -fPIC -Wl,-soname,$(SO_NAME) $(SYSROOT) $(OBJ) -o $(OUTPUT_DIR)$@
$(OBJDUMP) -d $(OUTPUT_DIR)$@ > file_dump.map
%.o: %.c
$(CC) $(CFLAGS) -std=c17 -c $< -o $@
%.o: %.cpp
$(CC) $(CFLAGS) -std=c++20 -c $< -o $@
%.o: %.S
$(AS) -c $< -o $@
%.o: %.asm
$(NASM) $< -f $(ASM_ARCH) -o $@
clean:
rm -f file_dump.map $(OBJ)

View File

@ -3,9 +3,11 @@ include ../../../Makefile.conf
NAME=init NAME=init
OBJECT_NAME=lib$(NAME).a OBJECT_NAME=lib$(NAME).so
SO_NAME=$(OBJECT_NAME)
OUTPUT_DIR=../../out/system/lib/ OUTPUT_DIR=../../out/system/lib/
SYSROOT = --sysroot=../../out/system/
CC = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)gcc CC = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)gcc
AS = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)as AS = ../../../$(TC_COMPILER_PATH)/$(TC_COMPILER_ARCH)as
@ -30,7 +32,7 @@ CFLAGS := -fPIC -I../include -I../../libc/include
build: $(OBJECT_NAME) build: $(OBJECT_NAME)
$(OBJECT_NAME): $(OBJ) $(OBJECT_NAME): $(OBJ)
$(AR) rcs $(OUTPUT_DIR)$@ $(OBJ) $(CC) -nostdlib -shared -fPIC -Wl,-soname,$(SO_NAME) $(SYSROOT) $(OBJ) -o $(OUTPUT_DIR)$@
$(OBJDUMP) -d $(OUTPUT_DIR)$@ > file_dump.map $(OBJDUMP) -d $(OUTPUT_DIR)$@ > file_dump.map
%.o: %.c %.o: %.c

18
libs/libsys/Base.c Normal file
View File

@ -0,0 +1,18 @@
#include <sysbase.h>
#include "../../../Kernel/syscalls.h"
long DoCtl(uint64_t Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4)
{
return syscall5(_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
}
uintptr_t KrnlRequestPages(size_t Count)
{
return syscall1(_RequestPages, Count);
}
void KrnlFreePages(uintptr_t Address, size_t Count)
{
syscall2(_FreePages, Address, Count);
}

45
libs/libsys/File.c Normal file
View File

@ -0,0 +1,45 @@
#include <sysbase.h>
#include <sysfile.h>
#include "../../../Kernel/syscalls.h"
long __FILE_GetPageSize()
{
static long PageSize = 0;
if (PageSize == 0)
PageSize = DoCtl(KCTL_GET_PAGE_SIZE, 0, 0, 0, 0);
return PageSize;
}
File *FileOpen(const char *Path, uint64_t Flags)
{
File *FilePtr = (File *)KrnlRequestPages(sizeof(File) / __FILE_GetPageSize() + 1);
FilePtr->KernelPrivate = (void *)syscall2(_FileOpen, (uint64_t)Path, Flags);
return FilePtr;
}
void FileClose(File *File)
{
syscall1(_FileClose, (uint64_t)File->KernelPrivate);
KrnlFreePages((uintptr_t)File, sizeof(File) / __FILE_GetPageSize() + 1);
}
uint64_t FileRead(File *File, uint8_t *Buffer, uint64_t Size)
{
return syscall3(_FileRead, (uint64_t)File->KernelPrivate, (uint64_t)Buffer, Size);
}
uint64_t FileWrite(File *File, uint8_t *Buffer, uint64_t Size)
{
return syscall3(_FileWrite, (uint64_t)File->KernelPrivate, (uint64_t)Buffer, Size);
}
uint64_t FileSeek(File *File, uint64_t Offset, uint64_t Whence)
{
return syscall3(_FileSeek, (uint64_t)File->KernelPrivate, Offset, Whence);
}
uint64_t FileStatus(File *File)
{
return syscall1(_FileStatus, (uint64_t)File->KernelPrivate);
}

View File

@ -4,7 +4,7 @@ include ../../../Makefile.conf
NAME=sys NAME=sys
OBJECT_NAME=lib$(NAME).so OBJECT_NAME=lib$(NAME).so
SO_NAME=libsys SO_NAME=$(OBJECT_NAME)
OUTPUT_DIR=../../out/system/lib/ OUTPUT_DIR=../../out/system/lib/
SYSROOT = --sysroot=../../out/system/ SYSROOT = --sysroot=../../out/system/

6
libs/libsys/Process.c Normal file
View File

@ -0,0 +1,6 @@
#include <sysbase.h>
#include <sysproc.h>
#include "../../../Kernel/syscalls.h"

View File

@ -1,8 +0,0 @@
#include <syslib.h>
#include "../../../Kernel/syscalls.h"
long DoCtl(uint64_t Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4)
{
return syscall5(_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
}