Updated kernel (tl;dr: improved filesystem, tasking, loading files, etc..)

This commit is contained in:
Alex
2023-02-06 19:35:44 +02:00
parent 640f6a412a
commit a592b85ce5
46 changed files with 3503 additions and 2412 deletions

View File

@ -13,7 +13,7 @@ extern "C"
int isalpha(int c);
int isupper(int c);
unsigned int isdelim(char c, char *delim);
int abs(int i);
long abs(long i);
void swap(char *x, char *y);
char *reverse(char *Buffer, int i, int j);

View File

@ -8,7 +8,7 @@
/* 32-bit ELF base types. */
typedef uint32_t Elf32_Addr;
typedef uint64_t Elf32_Half;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Word;
@ -153,6 +153,13 @@ struct Elf64_Dyn
} d_un;
};
typedef struct
{
Elf64_Addr r_offset;
Elf64_Xword r_info;
Elf64_Sxword r_addend;
} Elf64_Rela;
enum Elf_Ident
{
EI_MAG0 = 0, // 0x7F
@ -184,7 +191,7 @@ enum Elf_OSABI
ELFOSABI_OPENVMS = 13,
ELFOSABI_NSK = 14,
ELFOSABI_AROS = 15,
ELFOSABI_FENIXOS = 16, /* Wait... what? */
ELFOSABI_FENIXOS = 16,
ELFOSABI_CLOUDABI = 17,
ELFOSABI_OPENVOS = 18,
ELFOSABI_C6000_ELFABI = 64,
@ -208,7 +215,28 @@ enum RtT_Types
{
R_386_NONE = 0, // No relocation
R_386_32 = 1, // Symbol + Offset
R_386_PC32 = 2 // Symbol + Offset - Section Offset
R_386_PC32 = 2, // Symbol + Offset - Section Offset
R_X86_64_NONE = 0,
R_X86_64_64 = 1,
R_X86_64_PC32 = 2,
R_X86_64_GOT32 = 3,
R_X86_64_PLT32 = 4,
R_X86_64_COPY = 5,
R_X86_64_GLOB_DAT = 6,
R_X86_64_JUMP_SLOT = 7,
R_X86_64_RELATIVE = 8,
R_X86_64_GOTPCREL = 9,
R_X86_64_32 = 10,
R_X86_64_32S = 11,
R_X86_64_16 = 12,
};
enum ProgFlags_Types
{
PF_X = 1,
PF_W = 2,
PF_R = 4
};
enum StT_Bindings
@ -359,11 +387,13 @@ enum DynamicArrayTags
#define DO_64_64(S, A) ((S) + (A))
#define DO_64_PC32(S, A, P) ((S) + (A) - (P))
#define ELF32_R_SYM(INFO) ((INFO) >> 8)
#define ELF32_R_TYPE(INFO) ((uint8_t)(INFO))
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((unsigned char)(i))
#define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
#define ELF64_R_SYM(INFO) ((INFO) >> 8)
#define ELF64_R_TYPE(INFO) ((uint8_t)(INFO))
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i)&0xffffffffL)
#define ELF64_R_INFO(s, t) (((s) << 32) + ((t)&0xffffffffL))
#define SHN_UNDEF 0
#define SHN_ABS 0xfff1
@ -374,10 +404,12 @@ enum DynamicArrayTags
#define SHF_WRITE 0x1
#define SHF_ALLOC 0x2
#define EM_386 (3) // x86 Machine Type
#define EM_AMD64 (0x3E) // 64bit
#define EM_AARCH64 (0xb7) // ARM64
#define EV_CURRENT (1) // ELF Current Version
#define EM_386 0x3 // x86 Machine Type
#define EM_X86_64 0x3E // 64bit
#define EM_ARM 0x28 // ARM
#define EM_AARCH64 0xb7 // ARM64
#define EV_CURRENT 0x1 // ELF Current Version
#define ELFMAG0 0x7F // e_ident[EI_MAG0]
#define ELFMAG1 'E' // e_ident[EI_MAG1]

View File

@ -3,6 +3,7 @@
#include <types.h>
#include <filesystem.hpp>
#include <task.hpp>
#include <elf.h>
@ -21,9 +22,10 @@ namespace Execute
enum ExStatus
{
OK,
Unknown,
OK,
Unsupported,
GenericError,
InvalidFile,
InvalidFileFormat,
InvalidFileHeader,
@ -39,21 +41,78 @@ namespace Execute
Tasking::TCB *Thread;
};
struct SharedLibraries
{
char Identifier[256];
uint64_t Timeout;
long RefCount;
void *Address;
void *MemoryImage;
size_t Length;
};
struct ELFBaseLoad
{
bool Success;
SpawnData sd;
Tasking::IP InstructionPointer;
/* This should be deleted after copying the allocated pages to the thread
Intended to be used only inside BaseLoad.cpp */
Memory::MemMgr *TmpMem;
/* Same as above, for BaseLoad.cpp only */
Vector<AuxiliaryVector> auxv;
};
BinaryType GetBinaryType(void *Image);
BinaryType GetBinaryType(char *Path);
SpawnData Spawn(char *Path, const char **argv, const char **envp);
void *ELFLoadRel(Elf64_Ehdr *Header);
void ELFLoadExec(void *BaseImage,
size_t Length,
Elf64_Ehdr *ELFHeader,
Memory::Virtual &pva,
SpawnData *ret,
char *Path,
Tasking::PCB *Process,
const char **argv,
const char **envp,
Tasking::TaskArchitecture Arch,
Tasking::TaskCompatibility Comp);
ELFBaseLoad ELFLoad(char *Path, const char **argv, const char **envp,
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native);
Elf64_Shdr *GetELFSheader(Elf64_Ehdr *Header);
Elf64_Shdr *GetELFSection(Elf64_Ehdr *Header, uint64_t Index);
char *GetELFStringTable(Elf64_Ehdr *Header);
char *ELFLookupString(Elf64_Ehdr *Header, uintptr_t Offset);
void *ELFLookupSymbol(Elf64_Ehdr *Header, const char *Name);
uintptr_t ELFGetSymbolValue(Elf64_Ehdr *Header, uint64_t Table, uint64_t Index);
Elf64_Dyn *ELFGetDynamicTag(void *ElfFile, enum DynamicArrayTags Tag);
/**
* @brief Create a ELF Memory Image
*
* @param mem The memory manager to use
* @param pV Memory::Virtual object to use
* @param ElfFile ELF file loaded in memory (FULL FILE)
* @param Length Length of @p ElfFile
* @return void* The Memory Image
*/
void *ELFCreateMemoryImage(Memory::MemMgr *mem, Memory::Virtual &pV, void *ElfFile, size_t Length);
uintptr_t LoadELFInterpreter(Memory::MemMgr *mem, Memory::Virtual &pV, const char *Interpreter);
ELFBaseLoad ELFLoadRel(void *ElfFile,
VirtualFileSystem::File *ExFile,
Tasking::PCB *Process);
ELFBaseLoad ELFLoadExec(void *ElfFile,
VirtualFileSystem::File *ExFile,
Tasking::PCB *Process);
ELFBaseLoad ELFLoadDyn(void *ElfFile,
VirtualFileSystem::File *ExFile,
Tasking::PCB *Process);
void StartExecuteService();
SharedLibraries *AddLibrary(char *Identifier,
void *ElfImage,
size_t Length,
const Memory::Virtual &pV = Memory::Virtual());
void SearchLibrary(char *Identifier);
}
#endif // !__FENNIX_KERNEL_FILE_EXECUTE_H__

View File

@ -3,90 +3,75 @@
#include <types.h>
#include <smartptr.hpp>
#include <vector.hpp>
// show debug messages
// #define DEBUG_FILESYSTEM 1
#ifdef DEBUG_FILESYSTEM
#define vfsdbg(m, ...) debug(m, ##__VA_ARGS__)
#else
#define vfsdbg(m, ...)
#endif
namespace FileSystem
namespace VirtualFileSystem
{
#define FILENAME_LENGTH 256
struct FileSystemNode;
struct Node;
typedef size_t (*OperationMount)(const char *, unsigned long, const void *);
typedef size_t (*OperationUmount)(int);
typedef size_t (*OperationRead)(FileSystemNode *Node, size_t Offset, size_t Size, uint8_t *Buffer);
typedef size_t (*OperationWrite)(FileSystemNode *Node, size_t Offset, size_t Size, uint8_t *Buffer);
typedef void (*OperationOpen)(FileSystemNode *Node, uint8_t Mode, uint8_t Flags);
typedef void (*OperationClose)(FileSystemNode *Node);
typedef size_t (*OperationRead)(Node *node, size_t Offset, size_t Size, uint8_t *Buffer);
typedef size_t (*OperationWrite)(Node *node, size_t Offset, size_t Size, uint8_t *Buffer);
typedef void (*OperationOpen)(Node *node, uint8_t Mode, uint8_t Flags);
typedef void (*OperationClose)(Node *node);
typedef size_t (*OperationSync)(void);
typedef void (*OperationCreate)(FileSystemNode *Node, char *Name, uint16_t NameLength);
typedef void (*OperationMkdir)(FileSystemNode *Node, char *Name, uint16_t NameLength);
typedef void (*OperationCreate)(Node *node, char *Name, uint16_t NameLength);
typedef void (*OperationMkdir)(Node *node, char *Name, uint16_t NameLength);
#define MountFSFunction(name) size_t name(const char *unknown0, unsigned long unknown1, const uint8_t *unknown2)
#define UMountFSFunction(name) size_t name(int unknown0)
#define ReadFSFunction(name) size_t name(FileSystem::FileSystemNode *Node, size_t Offset, size_t Size, uint8_t *Buffer)
#define WriteFSFunction(name) size_t name(FileSystem::FileSystemNode *Node, size_t Offset, size_t Size, uint8_t *Buffer)
#define OpenFSFunction(name) void name(FileSystem::FileSystemNode *Node, uint8_t Mode, uint8_t Flags)
#define CloseFSFunction(name) void name(FileSystem::FileSystemNode *Node)
#define ReadFSFunction(name) size_t name(VirtualFileSystem::Node *node, size_t Offset, size_t Size, uint8_t *Buffer)
#define WriteFSFunction(name) size_t name(VirtualFileSystem::Node *node, size_t Offset, size_t Size, uint8_t *Buffer)
#define OpenFSFunction(name) void name(VirtualFileSystem::Node *node, uint8_t Mode, uint8_t Flags)
#define CloseFSFunction(name) void name(VirtualFileSystem::Node *node)
#define SyncFSFunction(name) size_t name(void)
#define CreateFSFunction(name) void name(FileSystem::FileSystemNode *Node, char *Name, uint16_t NameLength)
#define MkdirFSFunction(name) void name(FileSystem::FileSystemNode *Node, char *Name, uint16_t NameLength)
#define CreateFSFunction(name) void name(VirtualFileSystem::Node *node, char *Name, uint16_t NameLength)
#define MkdirFSFunction(name) void name(VirtualFileSystem::Node *node, char *Name, uint16_t NameLength)
enum FileStatus
{
OK = 0,
NOT_FOUND = 1,
ACCESS_DENIED = 2,
INVALID_NAME = 3,
INVALID_PARAMETER = 4,
INVALID_HANDLE = 5,
INVALID_PATH = 6,
INVALID_FILE = 7,
INVALID_DEVICE = 8,
NOT_EMPTY = 9,
NOT_SUPPORTED = 10,
INVALID_DRIVE = 11,
VOLUME_IN_USE = 12,
TIMEOUT = 13,
NO_MORE_FILES = 14,
END_OF_FILE = 15,
FILE_EXISTS = 16,
PIPE_BUSY = 17,
PIPE_DISCONNECTED = 18,
MORE_DATA = 19,
NO_DATA = 20,
PIPE_NOT_CONNECTED = 21,
MORE_ENTRIES = 22,
DIRECTORY_NOT_EMPTY = 23,
NOT_A_DIRECTORY = 24,
FILE_IS_A_DIRECTORY = 25,
DIRECTORY_NOT_ROOT = 26,
DIRECTORY_NOT_EMPTY_2 = 27,
END_OF_MEDIA = 28,
NO_MEDIA = 29,
UNRECOGNIZED_MEDIA = 30,
SECTOR_NOT_FOUND = 31
OK,
NotFound,
NotEmpty,
NotSupported,
AccessDenied,
Timeout,
SectorNotFound,
PartiallyCompleted,
InvalidName,
InvalidParameter,
InvalidHandle,
InvalidPath,
InvalidDevice,
InvalidOperator,
InvalidNode,
FileExists,
FileIsADirectory,
FileIsInvalid,
DirectoryNotEmpty,
NotADirectory,
UnknownFileStatusError
};
enum NodeFlags
{
FS_ERROR = 0x0,
FS_FILE = 0x01,
FS_DIRECTORY = 0x02,
FS_CHARDEVICE = 0x03,
FS_BLOCKDEVICE = 0x04,
FS_PIPE = 0x05,
FS_SYMLINK = 0x06,
FS_MOUNTPOINT = 0x08
NODE_FLAG_ERROR = 0x0,
FILE = 0x01,
DIRECTORY = 0x02,
CHARDEVICE = 0x03,
BLOCKDEVICE = 0x04,
PIPE = 0x05,
SYMLINK = 0x06,
MOUNTPOINT = 0x08
};
struct FileSystemOperations
@ -102,62 +87,67 @@ namespace FileSystem
OperationMkdir MakeDirectory = nullptr;
};
struct FileSystemNode
struct Node
{
char Name[FILENAME_LENGTH];
uint64_t IndexNode = 0;
uint64_t Mask = 0;
uint64_t Mode = 0;
int Flags = NodeFlags::FS_ERROR;
NodeFlags Flags = NodeFlags::NODE_FLAG_ERROR;
uint64_t UserIdentifier = 0, GroupIdentifier = 0;
uintptr_t Address = 0;
size_t Length = 0;
FileSystemNode *Parent = nullptr;
Node *Parent = nullptr;
FileSystemOperations *Operator = nullptr;
/* For root node:
0 - root "/"
1 - etc
...
*/
Vector<FileSystemNode *> Children;
Vector<Node *> Children;
};
struct FILE
struct File
{
const char *Name;
char Name[FILENAME_LENGTH];
FileStatus Status;
FileSystemNode *Node;
Node *node;
};
/* Manage / etc.. */
class Virtual
{
private:
FileSystemNode *FileSystemRoot = nullptr;
Node *FileSystemRoot = nullptr;
public:
FileSystemNode *GetRootNode() { return FileSystemRoot; }
FILE *ConvertNodeToFILE(FileSystemNode *Node)
{
FILE *File = new FILE;
File->Name = Node->Name;
File->Status = FileStatus::OK;
File->Node = Node;
return File;
}
char *GetPathFromNode(FileSystemNode *Node);
FileSystemNode *GetNodeFromPath(FileSystemNode *Parent, const char *Path);
char *NormalizePath(FileSystemNode *Parent, const char *Path);
shared_ptr<char> GetPathFromNode(Node *node);
Node *GetNodeFromPath(const char *Path, Node *Parent = nullptr);
shared_ptr<File> ConvertNodeToFILE(Node *node);
FileStatus FileExists(FileSystemNode *Parent, const char *Path);
FILE *Mount(FileSystemOperations *Operator, const char *Path);
FileStatus Unmount(FILE *File);
FILE *Open(const char *Path, FileSystemNode *Parent = nullptr);
size_t Read(FILE *File, size_t Offset, uint8_t *Buffer, size_t Size);
size_t Write(FILE *File, size_t Offset, uint8_t *Buffer, size_t Size);
FileStatus Close(FILE *File);
FileSystemNode *CreateRoot(FileSystemOperations *Operator, const char *RootName);
FileSystemNode *Create(FileSystemNode *Parent, const char *Path);
Node *GetParent(const char *Path, Node *Parent);
Node *GetRootNode() { return FileSystemRoot; }
Node *AddNewChild(const char *Name, Node *Parent);
Node *GetChild(const char *Name, Node *Parent);
FileStatus RemoveChild(const char *Name, Node *Parent);
shared_ptr<char> NormalizePath(const char *Path, Node *Parent = nullptr);
bool PathExists(const char *Path, Node *Parent = nullptr);
Node *CreateRoot(const char *RootName, FileSystemOperations *Operator);
Node *Create(const char *Path, NodeFlags Flag, Node *Parent = nullptr);
FileStatus Delete(const char *Path, bool Recursive = false, Node *Parent = nullptr);
FileStatus Delete(Node *Path, bool Recursive = false, Node *Parent = nullptr);
shared_ptr<File> Mount(const char *Path, FileSystemOperations *Operator);
FileStatus Unmount(shared_ptr<File> File);
size_t Read(shared_ptr<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
size_t Write(shared_ptr<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
shared_ptr<File> Open(const char *Path, Node *Parent = nullptr);
FileStatus Close(shared_ptr<File> File);
Virtual();
~Virtual();

View File

@ -5,7 +5,7 @@
#include <filesystem.hpp>
namespace FileSystem
namespace VirtualFileSystem
{
class EXT2
{

View File

@ -5,7 +5,7 @@
#include <filesystem.hpp>
namespace FileSystem
namespace VirtualFileSystem
{
class FAT
{

View File

@ -5,7 +5,7 @@
#include <filesystem.hpp>
namespace FileSystem
namespace VirtualFileSystem
{
class Initrd
{

View File

@ -5,13 +5,13 @@
#include <filesystem.hpp>
namespace FileSystem
namespace VirtualFileSystem
{
/* Manage /system/dev */
class Device
{
public:
FileSystemNode *AddFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Node *AddFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Device();
~Device();
};
@ -20,7 +20,7 @@ namespace FileSystem
class Mount
{
public:
FileSystemNode *MountFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name);
Node *MountFileSystem(FileSystemOperations *Operator, uint64_t Mode, const char *Name);
void DetectAndMountFS(void *drive);
Mount();
~Mount();
@ -38,7 +38,7 @@ namespace FileSystem
class Driver
{
public:
FileSystemNode *AddDriver(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Node *AddDriver(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Driver();
~Driver();
};
@ -47,7 +47,7 @@ namespace FileSystem
class Network
{
public:
FileSystemNode *AddNetworkCard(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Node *AddNetworkCard(struct FileSystemOperations *Operator, uint64_t Mode, const char *Name, int Flags);
Network();
~Network();
};

View File

@ -5,7 +5,7 @@
#include <filesystem.hpp>
namespace FileSystem
namespace VirtualFileSystem
{
class USTAR
{

View File

@ -1,5 +1,7 @@
#pragma once
#define HASHMAP_ERROR -0x8A50
template <typename K, typename V>
class HashNode
{
@ -33,6 +35,15 @@ public:
DummyNode = new HashNode<K, V>(-1, -1);
}
~HashMap()
{
for (int i = 0; i < HashMapCapacity; i++)
if (Nodes[i] != nullptr)
delete Nodes[i];
delete[] Nodes;
delete DummyNode;
}
int HashCode(K Key) { return Key % HashMapCapacity; }
void AddNode(K Key, V Value)
@ -67,7 +78,7 @@ public:
Index++;
Index %= HashMapCapacity;
}
return 0xdeadbeef;
return HASHMAP_ERROR;
}
V Get(int Key)
@ -78,14 +89,14 @@ public:
while (Nodes[Index] != nullptr)
{
if (Iterate++ > HashMapCapacity)
return 0xdeadbeef;
return HASHMAP_ERROR;
if (Nodes[Index]->Key == (K)Key)
return Nodes[Index]->Value;
Index++;
Index %= HashMapCapacity;
}
return 0xdeadbeef;
return HASHMAP_ERROR;
}
int Size() { return HashMapSize; }

View File

@ -2,75 +2,72 @@
#define __FENNIX_KERNEL_IPC_H__
#include <types.h>
#include <filesystem.hpp>
#include <vector.hpp>
#include <memory.hpp>
#include <lock.hpp>
namespace InterProcessCommunication
{
typedef int IPCPort;
typedef int IPCID;
enum IPCOperationType
enum IPCType
{
IPCOperationNone,
IPCOperationWrite,
IPCOperationRead
IPCNone,
IPCMessagePassing,
IPCPort,
IPCSharedMemory,
IPCPipe,
IPCSocket
};
enum IPCErrorCode
{
IPCUnknown,
IPCError = -1,
IPCSuccess,
IPCNotListening,
IPCTimeout,
IPCInvalidPort,
IPCPortInUse,
IPCPortNotRegistered,
IPCAlreadyAllocated,
IPCNotAllocated,
IPCIDInUse,
IPCIDNotRegistered,
IPCIDNotFound
};
typedef struct
struct IPCHandle
{
int ID;
IPCID ID;
long Length;
uint8_t *Buffer;
bool Listening;
IPCOperationType Operation;
VirtualFileSystem::Node *Node;
IPCErrorCode Error;
LockClass Lock;
} IPCHandle;
typedef struct
{
int ID;
long Length;
IPCOperationType Operation;
IPCErrorCode Error;
uint8_t *Buffer;
// Reserved
IPCHandle *HandleBuffer;
} __attribute__((packed)) IPCSyscallHandle;
struct IPCError
{
uint64_t ErrorCode;
};
class IPC
{
private:
NewLock(IPCLock);
IPCID NextID = 0;
Vector<IPCHandle *> Handles;
Memory::MemMgr *mem;
VirtualFileSystem::Node *IPCNode;
void *Process;
public:
IPC();
IPC(void *Process);
~IPC();
IPCHandle *RegisterHandle(IPCPort Port);
IPCError Listen(IPCPort Port);
IPCHandle *Wait(IPCPort Port);
IPCError Read(unsigned long /* Tasking::UPID */ ID, IPCPort Port, uint8_t *&Buffer, long &Size);
IPCError Write(unsigned long /* Tasking::UPID */ ID, IPCPort Port, uint8_t *Buffer, long Size);
IPCHandle *Create(IPCType Type, char UniqueToken[16]);
IPCErrorCode Destroy(IPCID ID);
IPCErrorCode Read(IPCID ID, uint8_t *Buffer, long Size);
IPCErrorCode Write(IPCID ID, uint8_t *Buffer, long Size);
IPCErrorCode Listen(IPCID ID);
IPCHandle *Wait(IPCID ID);
IPCErrorCode Allocate(IPCID ID, long Size);
IPCErrorCode Deallocate(IPCID ID);
};
}
extern InterProcessCommunication::IPC *ipc;
#endif // !__FENNIX_KERNEL_IPC_H__

View File

@ -2,6 +2,7 @@
#define __FENNIX_KERNEL_INTERNAL_MEMORY_H__
#ifdef __cplusplus
#include <filesystem.hpp>
#include <boot/binfo.h>
#include <bitmap.hpp>
#include <vector.hpp>
@ -634,28 +635,32 @@ namespace Memory
class MemMgr
{
private:
Bitmap PageBitmap;
PageTable4 *PageTable;
public:
struct AllocatedPages
{
void *Address;
size_t PageCount;
};
Vector<AllocatedPages> AllocatedPagesList;
public:
Vector<AllocatedPages> GetAllocatedPagesList() { return AllocatedPagesList; }
uint64_t GetAllocatedMemorySize();
bool Add(void *Address, size_t Count);
void *RequestPages(size_t Count);
void *RequestPages(size_t Count, bool User = false);
void FreePages(void *Address, size_t Count);
MemMgr(PageTable4 *PageTable = nullptr);
void DetachAddress(void *Address);
MemMgr(PageTable4 *PageTable = nullptr, VirtualFileSystem::Node *Directory = nullptr);
~MemMgr();
private:
Bitmap PageBitmap;
PageTable4 *PageTable;
VirtualFileSystem::Node *Directory;
Vector<AllocatedPages> AllocatedPagesList;
};
}

View File

@ -29,31 +29,31 @@
template <class T>
class smart_ptr
{
T *RealPointer;
T *m_RealPointer;
public:
explicit smart_ptr(T *p = nullptr)
explicit smart_ptr(T *Pointer = nullptr)
{
spdbg("Smart pointer created (%#lx)", RealPointer);
RealPointer = p;
spdbg("Smart pointer created (%#lx)", m_RealPointer);
m_RealPointer = Pointer;
}
~smart_ptr()
{
spdbg("Smart pointer deleted (%#lx)", RealPointer);
delete (RealPointer);
spdbg("Smart pointer deleted (%#lx)", m_RealPointer);
delete (m_RealPointer);
}
T &operator*()
{
spdbg("Smart pointer dereferenced (%#lx)", RealPointer);
return *RealPointer;
spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer);
return *m_RealPointer;
}
T *operator->()
{
spdbg("Smart pointer dereferenced (%#lx)", RealPointer);
return RealPointer;
spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer);
return m_RealPointer;
}
};
@ -67,6 +67,11 @@ class unique_ptr
{
};
template <class T>
class weak_ptr
{
};
template <typename T>
class shared_ptr
{
@ -74,81 +79,190 @@ private:
class Counter
{
private:
unsigned int RefCount{};
unsigned int m_RefCount{};
public:
Counter() : RefCount(0){};
Counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); };
Counter(const Counter &) = delete;
Counter &operator=(const Counter &) = delete;
~Counter() {}
void Reset() { RefCount = 0; }
unsigned int Get() { return RefCount; }
void operator++() { RefCount++; }
void operator++(int) { RefCount++; }
void operator--() { RefCount--; }
void operator--(int) { RefCount--; }
~Counter() { spdbg("Counter %#lx deleted", this); }
void Reset()
{
m_RefCount = 0;
spdbg("Counter reset");
}
unsigned int Get()
{
return m_RefCount;
spdbg("Counter returned");
}
void operator++()
{
m_RefCount++;
spdbg("Counter incremented");
}
void operator++(int)
{
m_RefCount++;
spdbg("Counter incremented");
}
void operator--()
{
m_RefCount--;
spdbg("Counter decremented");
}
void operator--(int)
{
m_RefCount--;
spdbg("Counter decremented");
}
};
Counter *ReferenceCounter;
T *RealPointer;
Counter *m_ReferenceCounter;
T *m_RealPointer;
public:
explicit shared_ptr(T *Pointer = nullptr)
{
spdbg("Shared pointer created (%#lx)", RealPointer);
RealPointer = Pointer;
ReferenceCounter = new Counter();
m_RealPointer = Pointer;
m_ReferenceCounter = new Counter();
spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
if (Pointer)
(*ReferenceCounter)++;
(*m_ReferenceCounter)++;
}
shared_ptr(shared_ptr<T> &SPtr)
{
spdbg("Shared pointer copied (%#lx)", RealPointer);
RealPointer = SPtr.RealPointer;
ReferenceCounter = SPtr.ReferenceCounter;
(*ReferenceCounter)++;
spdbg("[%#lx] Shared pointer copied (ptr=%#lx, ref=%#lx)", this, SPtr.m_RealPointer, SPtr.m_ReferenceCounter);
m_RealPointer = SPtr.m_RealPointer;
m_ReferenceCounter = SPtr.m_ReferenceCounter;
(*m_ReferenceCounter)++;
}
~shared_ptr()
{
spdbg("Shared pointer deleted (%#lx)", RealPointer);
(*ReferenceCounter)--;
if (ReferenceCounter->Get() == 0)
spdbg("[%#lx] Shared pointer destructor called", this);
(*m_ReferenceCounter)--;
if (m_ReferenceCounter->Get() == 0)
{
delete ReferenceCounter;
delete RealPointer;
spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter);
delete m_ReferenceCounter;
delete m_RealPointer;
}
}
unsigned int GetCount()
{
spdbg("Shared pointer count (%#lx)", RealPointer);
return ReferenceCounter->Get();
spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->Get());
return m_ReferenceCounter->Get();
}
T *Get()
{
spdbg("Shared pointer get (%#lx)", RealPointer);
return RealPointer;
spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer);
return m_RealPointer;
}
T &operator*()
{
spdbg("Shared pointer dereference (%#lx)", RealPointer);
return *RealPointer;
spdbg("[%#lx] Shared pointer dereference (ptr*=%#lx)", this, *m_RealPointer);
return *m_RealPointer;
}
T *operator->()
{
spdbg("Shared pointer dereference (%#lx)", RealPointer);
return RealPointer;
spdbg("[%#lx] Shared pointer dereference (ptr->%#lx)", this, m_RealPointer);
return m_RealPointer;
}
void reset(T *Pointer = nullptr)
{
if (m_RealPointer == Pointer)
return;
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
(*m_ReferenceCounter)--;
if (m_ReferenceCounter->Get() == 0)
{
delete m_ReferenceCounter;
delete m_RealPointer;
}
m_RealPointer = Pointer;
m_ReferenceCounter = new Counter();
if (Pointer)
(*m_ReferenceCounter)++;
}
void reset()
{
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter);
if (m_ReferenceCounter->Get() == 1)
{
delete m_RealPointer;
delete m_ReferenceCounter;
}
else
{
(*m_ReferenceCounter)--;
}
m_RealPointer = nullptr;
m_ReferenceCounter = nullptr;
}
void swap(shared_ptr<T> &Other)
{
spdbg("[%#lx] Shared pointer swap (ptr=%#lx, ref=%#lx <=> ptr=%#lx, ref=%#lx)",
this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter);
T *tempRealPointer = m_RealPointer;
Counter *tempReferenceCounter = m_ReferenceCounter;
m_RealPointer = Other.m_RealPointer;
m_ReferenceCounter = Other.m_ReferenceCounter;
Other.m_RealPointer = tempRealPointer;
Other.m_ReferenceCounter = tempReferenceCounter;
}
};
template <class T>
class weak_ptr
template <typename T>
struct remove_reference
{
typedef T type;
};
template <typename T>
struct remove_reference<T &>
{
typedef T type;
};
template <typename T>
struct remove_reference<T &&>
{
typedef T type;
};
template <typename T>
using remove_reference_t = typename remove_reference<T>::type;
template <typename T>
T &&forward(remove_reference_t<T> &t)
{
return static_cast<T &&>(t);
};
template <typename T>
T &&forward(remove_reference_t<T> &&t)
{
return static_cast<T &&>(t);
};
template <typename T, typename... Args>
shared_ptr<T> make_shared(Args &&...args)
{
return shared_ptr<T>(new T(forward<Args>(args)...));
};
#endif // !__FENNIX_KERNEL_SMART_POINTER_H__

6
include/stddef.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __FENNIX_KERNEL_STDDEF_STUB_H__
#define __FENNIX_KERNEL_STDDEF_STUB_H__
#include <types.h>
#endif // !__FENNIX_KERNEL_STDDEF_STUB_H__

View File

@ -19,6 +19,7 @@ namespace SymbolResolver
Symbols(uintptr_t ImageAddress);
~Symbols();
const char *GetSymbolFromAddress(uintptr_t Address);
void AddSymbol(uintptr_t Address, const char *Name);
};
}

View File

@ -3,8 +3,8 @@
#include <types.h>
#include <filesystem.hpp>
#include <interrupts.hpp>
#include <hashmap.hpp>
#include <symbols.hpp>
#include <vector.hpp>
#include <memory.hpp>
@ -25,7 +25,7 @@ namespace Tasking
UnknownArchitecture,
x32,
x64,
ARM,
ARM32,
ARM64
};
@ -42,7 +42,6 @@ namespace Tasking
UnknownElevation,
Kernel,
System,
Idle,
User
};
@ -57,6 +56,16 @@ namespace Tasking
Terminated
};
enum TaskPriority
{
UnknownPriority = 0,
Idle = 1,
Low = 25,
Normal = 50,
High = 75,
Critical = 100
};
struct TaskSecurity
{
TaskTrustLevel TrustLevel;
@ -76,7 +85,7 @@ namespace Tasking
uint64_t Year, Month, Day, Hour, Minute, Second;
uint64_t Usage[256]; // MAX_CPU
bool Affinity[256]; // MAX_CPU
int Priority;
TaskPriority Priority;
TaskArchitecture Architecture;
TaskCompatibility Compatibility;
};
@ -123,7 +132,7 @@ namespace Tasking
}
}
void SetPriority(int priority)
void SetPriority(TaskPriority priority)
{
CriticalSection cs;
trace("Setting priority of thread %s to %d", Name, priority);
@ -165,27 +174,46 @@ namespace Tasking
TaskInfo Info;
Vector<TCB *> Threads;
Vector<PCB *> Children;
HashMap<InterProcessCommunication::IPCPort, uintptr_t> *IPCHandles;
InterProcessCommunication::IPC *IPC;
Memory::PageTable4 *PageTable;
SymbolResolver::Symbols *ELFSymbolTable;
VirtualFileSystem::Node *ProcessDirectory;
VirtualFileSystem::Node *memDirectory;
};
enum TokenTrustLevel
/** @brief Token Trust Level */
enum TTL
{
UnknownTrustLevel,
Untrusted,
Trusted,
TrustedByKernel
UnknownTrustLevel = 0b0001,
Untrusted = 0b0010,
Trusted = 0b0100,
TrustedByKernel = 0b1000,
FullTrust = Trusted | TrustedByKernel
};
class Security
{
private:
struct TokenData
{
Token token;
int TrustLevel;
uint64_t OwnerID;
bool Process;
};
Vector<TokenData> Tokens;
public:
Token CreateToken();
bool TrustToken(Token token,
TokenTrustLevel TrustLevel);
bool TrustToken(Token token, TTL TrustLevel);
bool AddTrustLevel(Token token, TTL TrustLevel);
bool RemoveTrustLevel(Token token, TTL TrustLevel);
bool UntrustToken(Token token);
bool DestroyToken(Token token);
bool IsTokenTrusted(Token token, TTL TrustLevel);
bool IsTokenTrusted(Token token, int TrustLevel);
int GetTokenTrustLevel(Token token);
Security();
~Security();
};
@ -194,7 +222,6 @@ namespace Tasking
{
private:
Security SecurityManager;
InterProcessCommunication::IPC *IPCManager = nullptr;
UPID NextPID = 0;
UTID NextTID = 0;
@ -217,6 +244,7 @@ namespace Tasking
bool GetNextAvailableProcess(void *CPUDataPointer);
void SchedulerCleanupProcesses();
bool SchedulerSearchProcessThread(void *CPUDataPointer);
void UpdateProcessStatus();
void WakeUpThreads(void *CPUDataPointer);
#if defined(__amd64__)
@ -232,16 +260,13 @@ namespace Tasking
bool StopScheduler = false;
public:
void InitIPC()
{
static int once = 0;
if (!once++)
this->IPCManager = new InterProcessCommunication::IPC();
}
Vector<PCB *> GetProcessList() { return ListProcess; }
Security *GetSecurityManager() { return &SecurityManager; }
void Panic() { StopScheduler = true; }
void Schedule();
void SignalShutdown();
void RevertProcessCreation(PCB *Process);
void RevertThreadCreation(TCB *Thread);
long GetUsage(int Core)
{
if (IdleProcess)
@ -279,6 +304,9 @@ namespace Tasking
/** @brief Wait for thread to terminate */
void WaitForThread(TCB *tcb);
void WaitForProcessStatus(PCB *pcb, TaskStatus Status);
void WaitForThreadStatus(TCB *tcb, TaskStatus Status);
/**
* @brief Sleep for a given amount of milliseconds
*
@ -294,9 +322,9 @@ namespace Tasking
TCB *CreateThread(PCB *Parent,
IP EntryPoint,
const char **argv,
const char **envp,
Vector<AuxiliaryVector> &auxv,
const char **argv = nullptr,
const char **envp = nullptr,
const Vector<AuxiliaryVector> &auxv = Vector<AuxiliaryVector>(),
IPOffset Offset = 0,
TaskArchitecture Architecture = TaskArchitecture::x64,
TaskCompatibility Compatibility = TaskCompatibility::Native);
@ -306,4 +334,6 @@ namespace Tasking
};
}
extern "C" void TaskingScheduler_OneShot(int TimeSlice);
#endif // !__FENNIX_KERNEL_TASKING_H__