Updated userspace

This commit is contained in:
Alex
2022-12-24 09:18:45 +02:00
parent 0ce6433311
commit 40410cba41
25 changed files with 472 additions and 36 deletions

View File

@ -4,9 +4,11 @@ build:
make --quiet -C libinit build
make --quiet -C libssp build
make --quiet -C libsys build
make --quiet -C libgraph build
clean:
make -C libgcc clean
make -C libinit clean
make -C libssp 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
OBJECT_NAME=lib$(NAME).a
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
@ -30,7 +32,7 @@ CFLAGS := -fPIC -I../include -I../../libc/include
build: $(OBJECT_NAME)
$(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
%.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
OBJECT_NAME=lib$(NAME).so
SO_NAME=libsys
SO_NAME=$(OBJECT_NAME)
OUTPUT_DIR=../../out/system/lib/
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);
}