Update kernel code

This commit is contained in:
Alex
2023-08-22 06:21:17 +03:00
parent ef3b761d4f
commit 8898791257
49 changed files with 3389 additions and 2313 deletions

View File

@ -66,6 +66,18 @@ namespace Execute
std::vector<AuxiliaryVector> Elfauxv;
Tasking::IP ip;
void GenerateAuxiliaryVector_x86_32(Memory::MemMgr *mm,
int fd,
Elf32_Ehdr ELFHeader,
uint32_t EntryPoint,
uint32_t BaseAddress);
void GenerateAuxiliaryVector_x86_64(Memory::MemMgr *mm,
int fd,
Elf64_Ehdr ELFHeader,
uint64_t EntryPoint,
uint64_t BaseAddress);
void LoadExec_x86_32(int fd,
Tasking::PCB *TargetProcess);
@ -99,7 +111,8 @@ namespace Execute
int Spawn(char *Path, const char **argv, const char **envp,
Tasking::PCB *Parent = nullptr,
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native);
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native,
bool Critical = false);
bool ELFIs64(void *Header);
Elf64_Shdr *GetELFSheader(Elf64_Ehdr *Header);

View File

@ -357,7 +357,23 @@ namespace VirtualFileSystem
class FileDescriptorTable
{
public:
struct FileDescriptor
struct winsize
{
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel;
unsigned short ws_ypixel;
};
struct Fildes
{
RefNode *Handle{};
mode_t Mode = 0;
int Flags = 0;
int Descriptor = -1;
};
struct DupFildes
{
RefNode *Handle{};
mode_t Mode = 0;
@ -366,10 +382,13 @@ namespace VirtualFileSystem
};
private:
std::vector<FileDescriptor> FileDescriptors;
std::vector<Fildes> FileDescriptors;
std::vector<DupFildes> FildesDuplicates;
VirtualFileSystem::Node *fdDir = nullptr;
FileDescriptor GetFileDescriptor(int FileDescriptor);
Fildes GetFileDescriptor(int FileDescriptor);
FileDescriptorTable::DupFildes GetDupFildes(int FileDescriptor);
int ProbeMode(mode_t Mode, int Flags);
int AddFileDescriptor(const char *AbsolutePath, mode_t Mode, int Flags);
int RemoveFileDescriptor(int FileDescriptor);
@ -377,7 +396,7 @@ namespace VirtualFileSystem
public:
std::string GetAbsolutePath(int FileDescriptor);
std::vector<FileDescriptor> &GetFileDescriptors() { return FileDescriptors; }
std::vector<Fildes> &GetFileDescriptors() { return FileDescriptors; }
int _open(const char *pathname, int flags, mode_t mode);
int _creat(const char *pathname, mode_t mode);
@ -388,6 +407,9 @@ namespace VirtualFileSystem
int _stat(const char *pathname, struct stat *statbuf);
int _fstat(int fd, struct stat *statbuf);
int _lstat(const char *pathname, struct stat *statbuf);
int _dup(int oldfd);
int _dup2(int oldfd, int newfd);
int _ioctl(int fd, unsigned long request, void *argp);
FileDescriptorTable(void *Owner);
~FileDescriptorTable();

View File

@ -22,93 +22,9 @@
#include <filesystem.hpp>
namespace VirtualFileSystem
{
/* Manage /dev */
class Device
{
public:
Node *AddFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Device();
~Device();
};
/* Manage /mnt */
class Mount
{
public:
Node *MountFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name);
void DetectAndMountFS(void *drive);
Mount();
~Mount();
};
/* Manage /prc */
class Process
{
public:
Process();
~Process();
};
/* Manage /drv */
class Driver
{
public:
Node *AddDriver(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Driver();
~Driver();
};
/* Manage /net */
class Network
{
public:
Node *AddNetworkCard(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Network();
~Network();
};
/* Manage /dev/serialX */
class Serial
{
public:
Serial();
~Serial();
};
/* Manage /dev/random */
class Random
{
public:
Random();
~Random();
};
/* Manage /dev/null */
class Null
{
public:
Null();
~Null();
};
/* Manage /dev/zero */
class Zero
{
public:
Zero();
~Zero();
};
/* Manage /dev/fbX */
class FB
{
public:
void SetFrameBufferData(uintptr_t Address, size_t Size, uint32_t Width, uint32_t Height, uint32_t PixelsPerScanLine);
FB();
~FB();
};
}
void Init_Null(VirtualFileSystem::Virtual *vfs_ctx);
void Init_Random(VirtualFileSystem::Virtual *vfs_ctx);
void Init_Teletype(VirtualFileSystem::Virtual *vfs_ctx);
void Init_Zero(VirtualFileSystem::Virtual *vfs_ctx);
#endif // !__FENNIX_KERNEL_FILESYSTEM_DEV_H__

View File

@ -66,16 +66,12 @@ extern uintptr_t _kernel_bss_start, _kernel_bss_end;
#define FROM_PAGES(d) ((d)*PAGE_SIZE)
#if defined(a64) || defined(aa64)
#define NORMAL_VMA_OFFSET 0xFFFF800000000000
#define KERNEL_VMA_OFFSET 0xFFFFFFFF80000000
#define KERNEL_HEAP_BASE 0xFFFFA00000000000
#define USER_HEAP_BASE 0xFFFFB00000000000
#define KERNEL_HEAP_BASE 0xFFFFFF0000000000
#define USER_STACK_BASE 0xFFFFEFFFFFFF0000
#elif defined(a32)
#define NORMAL_VMA_OFFSET 0x80000000
#define KERNEL_VMA_OFFSET 0xC0000000
#define KERNEL_HEAP_BASE 0xA0000000
#define USER_HEAP_BASE 0xB0000000
#define USER_STACK_BASE 0xEFFFFFFF
#endif
@ -86,6 +82,7 @@ namespace Memory
None,
Pages,
XallocV1,
XallocV2,
liballoc11
};
@ -584,8 +581,35 @@ namespace Memory
* @return A new PageTable with the same content
*/
PageTable Fork();
template <typename T>
T Get(T Address);
} __aligned(0x1000);
class TempSwitchPT
{
private:
PageTable *Replace = nullptr;
PageTable *Restore = nullptr;
public:
TempSwitchPT(PageTable *ReplaceWith,
PageTable *RestoreWith = nullptr)
: Replace(ReplaceWith)
{
extern PageTable *KernelPageTable;
if (RestoreWith)
Restore = RestoreWith;
else
Restore = KernelPageTable;
Replace->Update();
}
~TempSwitchPT() { Restore->Update(); }
};
class Physical
{
private:
@ -1077,6 +1101,30 @@ namespace Memory
std::vector<AllocatedPages> AllocatedPagesList;
};
class ProgramBreak
{
private:
PageTable *Table = nullptr;
MemMgr *mm = nullptr;
uintptr_t HeapStart = 0x0;
uintptr_t Break = 0x0;
public:
/* Directly to syscall */
void *brk(void *Address);
void InitBrk(uintptr_t Address)
{
function("%#lx", Address);
HeapStart = Address;
Break = Address;
}
ProgramBreak(PageTable *Table, MemMgr *mm);
~ProgramBreak();
};
class SmartHeap
{
private:

View File

@ -159,17 +159,35 @@ namespace Tasking
private:
class Task *ctx = nullptr;
void SetupUserStack_x86_64(const char **argv,
const char **envp,
const std::vector<AuxiliaryVector> &auxv);
void SetupUserStack_x86_32(const char **argv,
const char **envp,
const std::vector<AuxiliaryVector> &auxv);
void SetupUserStack_aarch64(const char **argv,
const char **envp,
const std::vector<AuxiliaryVector> &auxv);
public:
/* Basic info */
TID ID = -1;
const char * Name = nullptr;
const char *Name = nullptr;
class PCB *Parent = nullptr;
IP EntryPoint = 0;
/* Statuses */
std::atomic_int ExitCode;
std::atomic<TaskStatus> Status = TaskStatus::UnknownStatus;
Memory::StackGuard *Stack;
Memory::MemMgr *Memory;
std::atomic<TaskStatus> Status = TaskStatus::Zombie;
int ErrorNumber;
/* Memory */
Memory::MemMgr *Memory;
Memory::StackGuard *Stack;
/* CPU state */
#if defined(a64)
CPU::x64::TrapFrame Registers{};
uintptr_t ShadowGSBase, GSBase, FSBase;
@ -180,6 +198,9 @@ namespace Tasking
uintptr_t Registers; // TODO
#endif
uintptr_t IPHistory[128];
CPU::x64::FXState *FPU;
/* Info & Security info */
struct
{
TaskExecutionMode ExecutionMode = UnknownExecutionMode;
@ -188,7 +209,13 @@ namespace Tasking
bool IsKernelDebugEnabled = false;
} Security{};
TaskInfo Info{};
CPU::x64::FXState *FPU;
/* Compatibility structures */
struct
{
int *set_child_tid{};
int *clear_child_tid{};
} Linux{};
void Rename(const char *name);
void SetPriority(TaskPriority priority);
@ -228,11 +255,16 @@ namespace Tasking
bool OwnPageTable = false;
public:
/* Basic info */
PID ID = -1;
const char * Name = nullptr;
const char *Name = nullptr;
PCB *Parent = nullptr;
/* Statuses */
std::atomic_int ExitCode;
std::atomic<TaskStatus> Status = Zombie;
/* Info & Security info */
struct
{
TaskExecutionMode ExecutionMode = UnknownExecutionMode;
@ -246,17 +278,26 @@ namespace Tasking
} Real, Effective;
} Security{};
TaskInfo Info{};
std::vector<TCB *> Threads;
std::vector<PCB *> Children;
InterProcessCommunication::IPC *IPC;
Memory::PageTable *PageTable;
SymbolResolver::Symbols *ELFSymbolTable;
/* Filesystem */
Node *CurrentWorkingDirectory;
Node *ProcessDirectory;
Node *memDirectory;
Memory::MemMgr *Memory;
FileDescriptorTable *FileDescriptors;
/* Memory */
Memory::PageTable *PageTable;
Memory::MemMgr *Memory;
Memory::ProgramBreak *ProgramBreak;
/* Other */
InterProcessCommunication::IPC *IPC;
SymbolResolver::Symbols *ELFSymbolTable;
/* Threads & Children */
std::vector<TCB *> Threads;
std::vector<PCB *> Children;
public:
void Rename(const char *name);
void SetWorkingDirectory(Node *node);

View File

@ -21,10 +21,10 @@
#ifdef __cplusplus
#define EXTERNC extern "C"
#define START_EXTERNC \
EXTERNC \
{
EXTERNC \
{
#define END_EXTERNC \
}
}
#else // __cplusplus
#define EXTERNC
#define START_EXTERNC
@ -52,8 +52,8 @@
#define in :
#define forItr(itr, container) \
for (auto itr = container.begin(); \
itr != container.end(); ++itr)
for (auto itr = container.begin(); \
itr != container.end(); ++itr)
#define r_cst(t, v) reinterpret_cast<t>(v)
#define c_cst(t, v) const_cast<t>(v)
@ -83,18 +83,18 @@ typedef __builtin_va_list va_list;
#define offsetof(type, member) __builtin_offsetof(type, member)
#define MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
#define MIN(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#define ROUND_UP(x, y) (((x) + (y)-1) & ~((y)-1))
#define ROUND_DOWN(x, y) ((x) & ~((y)-1))
@ -291,19 +291,19 @@ typedef unsigned gid_t;
#if defined(a64)
#define BREAK __asm__ __volatile__("int $0x3" \
: \
: \
: "memory");
: \
: \
: "memory");
#elif defined(a32)
#define BREAK __asm__ __volatile__("int $0x3" \
: \
: \
: "memory");
: \
: \
: "memory");
#elif defined(aa64)
#define BREAK __asm__ __volatile__("brk #0" \
: \
: \
: "memory");
: \
: \
: "memory");
#endif
#ifdef __INT48_TYPE__
@ -327,11 +327,11 @@ typedef uint48_t uint_fast48_t;
#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)))
(((x)&0x00000000ff00) << 24) | \
(((x)&0x000000ff0000) << 8) | \
(((x)&0x0000ff000000) >> 8) | \
(((x)&0x00ff00000000) >> 24) | \
(((x)&0xff0000000000) >> 40)))
#define b64(x) __builtin_bswap64(x)
/* https://gcc.gnu.org/onlinedocs/gcc-9.5.0/gnat_ugn/Optimization-Levels.html */
@ -411,16 +411,10 @@ typedef uint48_t uint_fast48_t;
#define NIF __no_instrument_function
#define int1 \
__asm__ __volatile__("int $0x1" \
: \
: \
: "memory")
#define int3 \
__asm__ __volatile__("int3" \
: \
: \
: "memory")
__asm__ __volatile__("int3" \
: \
: \
: "memory")
#endif // !__FENNIX_KERNEL_TYPES_H__