Stability fixes (i hope); attempt to implement argc, argv, envp, auxv; Syscalls

This commit is contained in:
Alex
2022-11-10 07:09:32 +02:00
parent 40b1da9dd1
commit 77081b4e1e
35 changed files with 3116 additions and 211 deletions

72
include/abi.h Normal file
View File

@ -0,0 +1,72 @@
#ifndef __FENNIX_KERNEL_ABI_H__
#define __FENNIX_KERNEL_ABI_H__
#include <types.h>
#define AT_NULL 0
#define AT_IGNORE 1
#define AT_EXECFD 2
#define AT_PHDR 3
#define AT_PHENT 4
#define AT_PHNUM 5
#define AT_PAGESZ 6
#define AT_BASE 7
#define AT_FLAGS 8
#define AT_ENTRY 9
#define AT_NOTELF 10
#define AT_UID 11
#define AT_EUID 12
#define AT_GID 13
#define AT_EGID 14
#define AT_PLATFORM 15
#define AT_HWCAP 16
#define AT_CLKTCK 17
#define AT_SECURE 23
#define AT_BASE_PLATFORM 24
#define AT_RANDOM 25
#define AT_HWCAP2 26
#define AT_EXECFN 31
#define AT_SYSINFO 32
#define AT_SYSINFO_EHDR 33
#define AT_L1I_CACHESHAPE 34
#define AT_L1D_CACHESHAPE 35
#define AT_L2_CACHESHAPE 36
#define AT_L3_CACHESHAPE 37
#define AT_L1I_CACHESIZE 40
#define AT_L1I_CACHEGEOMETRY 41
#define AT_L1D_CACHESIZE 42
#define AT_L1D_CACHEGEOMETRY 43
#define AT_L2_CACHESIZE 44
#define AT_L2_CACHEGEOMETRY 45
#define AT_L3_CACHESIZE 46
#define AT_L3_CACHEGEOMETRY 47
#define AT_MINSIGSTKSZ 51
typedef struct
{
uint32_t a_type;
union
{
uint32_t a_val;
} a_un;
} Elf32_auxv_t;
typedef struct
{
uint64_t a_type;
union
{
uint64_t a_val;
} a_un;
} Elf64_auxv_t;
typedef struct
{
#if defined(__amd64__)
Elf64_auxv_t archaux;
#elif defined(__i386__)
#elif defined(__aarch64__)
#endif
} AuxiliaryVector;
#endif // !__FENNIX_KERNEL_ABI_H__

View File

@ -3,12 +3,28 @@
#include <types.h>
#define assert(x) \
do \
{ \
if (!(x)) \
while (1) \
; \
#include <debug.h>
#define assert(x) \
do \
{ \
if (!(x)) \
{ \
void *CallerAddress = __builtin_extract_return_addr(__builtin_return_address(0)); \
error("Assertion failed! [%s] [%#lx => %s:%s:%d]", #x, CallerAddress, __FILE__, __FUNCTION__, __LINE__); \
while (1) \
; \
} \
} while (0)
#define assert_allow_continue(x) \
do \
{ \
if (!(x)) \
{ \
void *CallerAddress = __builtin_extract_return_addr(__builtin_return_address(0)); \
error("Assertion failed! [%s] [%#lx => %s:%s:%d]", #x, CallerAddress, __FILE__, __FUNCTION__, __LINE__); \
} \
} while (0)
#define static_assert(x) \

View File

@ -1585,6 +1585,9 @@ namespace CPU
__attribute__((no_stack_protector)) static inline void fxsave(char *FXSaveArea)
{
#if defined(__amd64__)
if (!FXSaveArea || FXSaveArea >= (char *)0xfffffffffffff000)
return;
_amd64_fxsave(FXSaveArea);
// asmv("fxsaveq (%0)"
// :
@ -1596,6 +1599,9 @@ namespace CPU
__attribute__((no_stack_protector)) static inline void fxrstor(char *FXRstorArea)
{
#if defined(__amd64__)
if (!FXRstorArea || FXRstorArea >= (char *)0xfffffffffffff000)
return;
_amd64_fxrstor(FXRstorArea);
// asmv("fxrstorq (%0)"
// :

View File

@ -40,7 +40,7 @@ namespace Execute
};
BinaryType GetBinaryType(char *Path);
SpawnData Spawn(char *Path, uint64_t Arg0, uint64_t Arg1);
SpawnData Spawn(char *Path, Vector<const char *> &argv, Vector<const char *> &envp);
void *ELFLoadRel(Elf64_Ehdr *Header);
}

View File

@ -10,6 +10,7 @@ struct KernelConfig
bool SchedulerType;
char DriverDirectory[256];
char InitPath[256];
bool InterruptsOnCrash;
int Cores;
};

View File

@ -38,7 +38,7 @@ struct CPUData
/** @brief Current running thread */
Tasking::TCB *CurrentThread;
/** @brief Architecture-specific CPU data. */
/** @brief Architecture-specific data. */
CPUArchData *Data;
/** @brief Checksum. Used to verify the integrity of the data. Must be equal to CPU_DATA_CHECKSUM (0xC0FFEE). */
int Checksum;

View File

@ -3,7 +3,7 @@
#include <types.h>
typedef struct SyscallsRegs
typedef struct SyscallsFrame
{
#if defined(__amd64__)
uint64_t r15, r14, r13, r12, r11, r10, r9, r8;
@ -15,7 +15,10 @@ typedef struct SyscallsRegs
uint64_t InterruptNumber, ErrorCode, eip, cs, eflags, esp, ss;
#elif defined(__aarch64__)
#endif
} SyscallsRegs;
} SyscallsFrame;
uint64_t HandleNativeSyscalls(SyscallsFrame *Frame);
uint64_t HandleLinuxSyscalls(SyscallsFrame *Frame);
/**
* @brief Initialize syscalls for the current CPU. (Function is available on x32, x64 & aarch64)

View File

@ -9,11 +9,11 @@
#include <hashmap.hpp>
#include <ipc.hpp>
#include <debug.h>
#include <abi.h>
namespace Tasking
{
typedef unsigned long IP;
typedef unsigned long Arg;
typedef unsigned long IPOffset;
typedef unsigned long UPID;
typedef unsigned long UTID;
@ -84,8 +84,6 @@ namespace Tasking
struct PCB *Parent;
IP EntryPoint;
IPOffset Offset;
Arg Argument0;
Arg Argument1;
int ExitCode;
void *Stack;
TaskStatus Status;
@ -217,6 +215,17 @@ namespace Tasking
public:
void Schedule();
long GetUsage(int Core) { return 100 - IdleProcess->Info.Usage[Core]; }
void KillThread(TCB *tcb, int Code)
{
tcb->Status = TaskStatus::Terminated;
tcb->ExitCode = Code;
}
void KillProcess(PCB *pcb, int Code)
{
pcb->Status = TaskStatus::Terminated;
pcb->ExitCode = Code;
}
/**
* @brief Get the Current Process object
@ -242,8 +251,9 @@ namespace Tasking
TCB *CreateThread(PCB *Parent,
IP EntryPoint,
Arg Argument0 = 0,
Arg Argument1 = 0,
Vector<const char *> &argv,
Vector<const char *> &envp,
Vector<AuxiliaryVector> &auxv,
IPOffset Offset = 0,
TaskArchitecture Architecture = TaskArchitecture::x64,
TaskCompatibility Compatibility = TaskCompatibility::Native);

View File

@ -200,4 +200,45 @@ typedef __SIZE_TYPE__ size_t;
#define WINT_MAX __WINT_MAX__
#define WINT_MIN __WINT_MIN__
#define b4(x) ((x & 0x0F) << 4 | (x & 0xF0) >> 4)
#define b8(x) ((x)&0xFF)
#define b16(x) __builtin_bswap16(x)
#define b32(x) __builtin_bswap32(x)
#define b48(x) (((((x)&0x0000000000ff) << 40) | (((x)&0x00000000ff00) << 24) | (((x)&0x000000ff0000) << 8) | (((x)&0x0000ff000000) >> 8) | (((x)&0x00ff00000000) >> 24) | (((x)&0xff0000000000) >> 40)))
#define b64(x) __builtin_bswap64(x)
#define O0 __attribute__((optimize("O0")))
#define O1 __attribute__((optimize("O1")))
#define O2 __attribute__((optimize("O2")))
#define O3 __attribute__((optimize("O3")))
#define Os __attribute__((optimize("Os")))
#define Ofast __attribute__((optimize("Ofast")))
/** @brief dbg */
#define OPTMZ O0
#define __unused __attribute__((unused))
#define __packed __attribute__((packed))
#define __aligned(x) __attribute__((aligned(x)))
#define __section(x) __attribute__((section(x)))
#define __noreturn __attribute__((noreturn))
#define __weak __attribute__((weak))
#define __alias(x) __attribute__((alias(x)))
#define __always_inline __attribute__((always_inline))
#define __noinline __attribute__((noinline))
#define __pure __attribute__((pure))
#define __const __attribute__((const))
#define __malloc __attribute__((malloc))
#define __returns_twice __attribute__((returns_twice))
#define __used __attribute__((used))
#define __deprecated __attribute__((deprecated))
#define __deprecated_msg(x) __attribute__((deprecated(x)))
#define __weakref(x) __attribute__((weakref(x)))
#define __weakrefalias(x) __attribute__((weakref(#x)))
#define __visibility(x) __attribute__((visibility(x)))
#define __constructor __attribute__((constructor))
#define __destructor __attribute__((destructor))
#define __cleanup(x) __attribute__((cleanup(x)))
#endif // !__FENNIX_KERNEL_TYPES_H__