Refactor filesystem & stl code

This commit is contained in:
EnderIce2
2024-05-18 07:42:01 +03:00
parent 77a291d08b
commit 6801475243
186 changed files with 15784 additions and 9746 deletions

28
library/c/ctype.c Normal file
View File

@ -0,0 +1,28 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
int tolower(int c)
{
return _tolower(c);
}
int toupper(int c)
{
return _toupper(c);
}

100
library/c/stdio.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <debug.h>
FILE __local_stdin = {.st = 0};
FILE __local_stdout = {.st = 1};
FILE __local_stderr = {.st = 2};
FILE *stdin = &__local_stdin;
FILE *stdout = &__local_stdout;
FILE *stderr = &__local_stderr;
EXTERNC int asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
#define va_copy(dest, src) __builtin_va_copy(dest, src)
EXTERNC int vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
va_copy(ap2, ap);
int len = vsnprintf(NULL, 0, fmt, ap2);
va_end(ap2);
if (len < 0)
{
return -1;
}
*strp = (char *)malloc(len + 1);
if (!*strp)
{
return -1;
}
int ret = vsnprintf(*strp, len + 1, fmt, ap);
if (ret < 0)
{
free(*strp);
*strp = NULL;
}
return ret;
}
int fprintf(FILE *stream, const char *format, ...)
{
switch (stream->st)
{
case 0:
{
error("fprintf() called with stdin");
return -1;
}
case 1:
{
va_list ap;
va_start(ap, format);
int ret = vprintf(format, ap);
va_end(ap);
return ret;
}
case 2:
{
va_list ap;
va_start(ap, format);
int ret = vprintf(format, ap);
va_end(ap);
return ret;
}
default:
return -1;
}
}
int fputs(const char *s, FILE *stream)
{
for (const char *c = s; *c; c++)
uart_wrapper(*c, NULL);
return 0;
}

27
library/c/stdlib.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <debug.h>
#include <cpu.hpp>
EXTERNC void abort()
{
error("abort() called");
CPU::Stop();
__builtin_unreachable();
}

99
library/cbuf.cpp Normal file
View File

@ -0,0 +1,99 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cbuf.hpp>
CircularBuffer::CircularBuffer(size_t Size)
: Buffer(new uint8_t[Size]),
BufferSize(Size),
BufferCount(0),
Head(0),
Tail(0) {}
CircularBuffer::~CircularBuffer() { delete[] Buffer; }
size_t CircularBuffer::Write(const uint8_t *Data, size_t Size)
{
sl_guard(Lock);
size_t written = 0;
while (Size > 0)
{
if (BufferCount == BufferSize)
break;
Buffer[Head] = *Data++;
Head = (Head + 1) % BufferSize;
BufferCount++;
written++;
Size--;
}
return written;
}
size_t CircularBuffer::Read(uint8_t *Data, size_t Size)
{
sl_guard(Lock);
size_t read = 0;
while (Size > 0)
{
if (BufferCount == 0)
break;
*Data++ = Buffer[Tail];
Tail = (Tail + 1) % BufferSize;
BufferCount--;
read++;
Size--;
}
return read;
}
size_t CircularBuffer::Peek(uint8_t *Data, size_t Size)
{
sl_guard(Lock);
size_t read = 0;
size_t tail = Tail;
while (Size > 0)
{
if (read == BufferCount)
break;
*Data++ = Buffer[tail];
tail = (tail + 1) % BufferSize;
read++;
Size--;
}
return read;
}
size_t CircularBuffer::Count()
{
sl_guard(Lock);
return BufferCount;
}
size_t CircularBuffer::Free()
{
sl_guard(Lock);
return BufferSize - BufferCount;
}

View File

@ -21,6 +21,7 @@
#include <limits.h>
#include <debug.h>
#include <cpu.hpp>
#include <cctype>
#ifdef DEBUG
#include <uart.hpp>
#endif

View File

@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2020 Leonard Iklé
Copyright (c) 2024 Leonard Iklé
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -23,25 +23,24 @@ SOFTWARE.
*/
#include <assert.h>
#include <ctype.h>
#include <cwalk.h>
#include <convert.h>
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
#pragma GCC diagnostic ignored "-Wsequence-point"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/**
* We try to default to a different path style depending on the operating
* system. So this should detect whether we should use windows or unix paths.
*/
/*
#if defined(WIN32) || defined(_WIN32) || \
/*#if defined(WIN32) || defined(_WIN32) || \
defined(__WIN32) && !defined(__CYGWIN__)
static enum cwk_path_style path_style = CWK_STYLE_WINDOWS;
#else
static enum cwk_path_style path_style = CWK_STYLE_UNIX;
#endif
*/
#endif*/
/* This will extract the path style of the running process */
extern enum cwk_path_style *__cwalk_path_style(void);
#define path_style (*__cwalk_path_style())
@ -153,7 +152,9 @@ static void cwk_path_terminate_output(char *buffer, size_t buffer_size,
static bool cwk_path_is_string_equal(const char *first, const char *second,
size_t first_size, size_t second_size)
{
// THe two strings are not equal if the sizes are not equal.
bool are_both_separators;
// The two strings are not equal if the sizes are not equal.
if (first_size != second_size) {
return false;
}
@ -169,11 +170,18 @@ static bool cwk_path_is_string_equal(const char *first, const char *second,
// own.
while (*first && *second && first_size > 0) {
// We can consider the string to be not equal if the two lowercase
// characters are not equal.
if (tolower(*first++) != tolower(*second++)) {
// characters are not equal. The two chars may also be separators, which
// means they would be equal.
are_both_separators = strchr(separators[path_style], *first) != NULL &&
strchr(separators[path_style], *second) != NULL;
if (tolower(*first) != tolower(*second) && !are_both_separators) {
return false;
}
first++;
second++;
--first_size;
}
@ -511,7 +519,7 @@ static void cwk_path_get_root_windows(const char *path, size_t *length)
if (cwk_path_is_separator(c)) {
++c;
// Check whether the path starts with a single back slash, which means this
// Check whether the path starts with a single backslash, which means this
// is not a network path - just a normal path starting with a backslash.
if (!cwk_path_is_separator(c)) {
// Okay, this is not a network path but we still use the backslash as a
@ -599,6 +607,29 @@ static bool cwk_path_is_root_absolute(const char *path, size_t length)
return cwk_path_is_separator(&path[length - 1]);
}
static void cwk_path_fix_root(char *buffer, size_t buffer_size, size_t length)
{
size_t i;
// This only affects windows.
if (path_style != CWK_STYLE_WINDOWS) {
return;
}
// Make sure we are not writing further than we are actually allowed to.
if (length > buffer_size) {
length = buffer_size;
}
// Replace all forward slashes with backwards slashes. Since this is windows
// we can't have any forward slashes in the root.
for (i = 0; i < length; ++i) {
if (cwk_path_is_separator(&buffer[i])) {
buffer[i] = *separators[CWK_STYLE_WINDOWS];
}
}
}
static size_t cwk_path_join_and_normalize_multiple(const char **paths,
char *buffer, size_t buffer_size)
{
@ -613,8 +644,10 @@ static size_t cwk_path_join_and_normalize_multiple(const char **paths,
// later on whether we can remove superfluous "../" or not.
absolute = cwk_path_is_root_absolute(paths[0], pos);
// First copy the root to the output. We will not modify the root.
// First copy the root to the output. After copying, we will normalize the
// root.
cwk_path_output_sized(buffer, buffer_size, 0, paths[0], pos);
cwk_path_fix_root(buffer, buffer_size, pos);
// So we just grab the first segment. If there is no segment we will always
// output a "/", since we currently only support absolute paths here.

254
library/kexcept/cxxabi.cpp Normal file
View File

@ -0,0 +1,254 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <kexcept/cxxabi.h>
#include <memory.hpp>
#include <debug.h>
#include <smp.hpp>
#include "../../kernel.h"
extern "C" _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *Exception);
using namespace __cxxabiv1;
atexit_func_entry_t __atexit_funcs[ATEXIT_MAX_FUNCS];
uarch_t __atexit_func_count = 0;
__cxa_eh_globals *__cxa_get_globals() noexcept
{
return &GetCurrentCPU()->Exception.globals;
}
/**
* @param f The destructor
* @param objptr The object to be destructed
* @param dso The DSO from which the object was obtained (unused in our case)
* @return Zero on success, non-zero on failure
*/
extern "C" int __cxa_atexit(void (*f)(void *), void *objptr, void *dso)
{
if (KernelSymbolTable)
{
debug("Registering atexit function for \"%s\" with destructor \"%s\"",
KernelSymbolTable->GetSymbol((uintptr_t)objptr),
KernelSymbolTable->GetSymbol((uintptr_t)f));
}
else
{
debug("Registering atexit function for %p with destructor %p",
objptr, f);
}
if (__atexit_func_count >= ATEXIT_MAX_FUNCS)
return -1;
__atexit_funcs[__atexit_func_count].destructor_func = f;
__atexit_funcs[__atexit_func_count].obj_ptr = objptr;
__atexit_funcs[__atexit_func_count].dso_handle = dso;
__atexit_func_count++;
return 0;
}
extern "C" void __cxa_finalize(void *f)
{
func("%p", f);
uarch_t i = __atexit_func_count;
if (f == nullptr)
{
while (i--)
{
if (__atexit_funcs[i].destructor_func)
{
if (KernelSymbolTable)
{
debug("Calling atexit function \"%s\"",
KernelSymbolTable->GetSymbol((uintptr_t)__atexit_funcs[i].destructor_func));
}
else
{
debug("Calling atexit function %p",
__atexit_funcs[i].destructor_func);
}
(*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr);
}
}
return;
}
while (i--)
{
if (__atexit_funcs[i].destructor_func == f)
{
(*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr);
__atexit_funcs[i].destructor_func = 0;
}
}
}
extern "C" _Unwind_Reason_Code __gxx_personality_v0(int version, _Unwind_Action actions, _Unwind_Exception_Class exception_class, _Unwind_Exception *ue_header, _Unwind_Context *context)
{
fixme("__gxx_personality_v0( %d %p %p %p %p ) called.", version, actions, exception_class, ue_header, context);
return _URC_NO_REASON;
}
extern "C" void *__cxa_begin_catch(void *thrown_object) noexcept
{
func("%p", thrown_object);
__cxa_exception *Exception = (__cxa_exception *)thrown_object - 1;
__cxa_eh_globals *Globals = __cxa_get_globals();
Exception->handlerCount++;
Globals->uncaughtExceptions--;
Exception->nextException = Globals->caughtExceptions;
Globals->caughtExceptions = Exception;
return Exception + 1;
}
extern "C" void __cxa_end_catch()
{
fixme("__cxa_end_catch() called.");
}
static __always_inline inline size_t align_exception_allocation_size(size_t a, size_t b)
{
return (a + b - 1) & ~(b - 1);
}
static __always_inline inline void INIT_EXCEPTION_CLASS(_Unwind_Exception_Class *c)
{
char *ptr = (char *)c;
ptr[0] = 'G';
ptr[1] = 'N';
ptr[2] = 'U';
ptr[3] = 'C';
ptr[4] = 'C';
ptr[5] = '+';
ptr[6] = '+';
ptr[7] = '\0';
}
void unexpected_header_stub() { fixme("unexpected() called."); }
void terminate_header_stub()
{
if (TaskManager && !TaskManager->IsPanic())
{
TaskManager->KillThread(thisThread, Tasking::KILL_CXXABI_EXCEPTION);
TaskManager->Yield();
}
error("No task manager to kill thread!");
CPU::Stop(); /* FIXME: Panic */
}
void exception_cleanup_stub(_Unwind_Reason_Code Code,
_Unwind_Exception *Exception)
{
fixme("exception_cleanup( %d %p ) called.",
Code, Exception);
}
extern "C" void *__cxa_allocate_exception(size_t thrown_size) throw()
{
debug("Allocating exception of size %d.", thrown_size);
size_t alloc_size = align_exception_allocation_size(thrown_size + sizeof(__cxa_exception), alignof(__cxa_exception));
__cxa_exception *Exception = (__cxa_exception *)kmalloc(alloc_size);
memset(Exception, 0, alloc_size);
return Exception + 1;
}
extern "C" __noreturn void __cxa_throw(void *thrown_object,
std::type_info *tinfo,
void (*dest)(void *))
{
trace("Throwing exception of type \"%s\". ( object: %p, destructor: %p )",
tinfo->name(), thrown_object, dest);
__cxa_eh_globals *Globals = __cxa_get_globals();
Globals->uncaughtExceptions++;
__cxa_exception *Exception = (__cxa_exception *)thrown_object - 1;
Exception->exceptionType = (std::type_info *)tinfo;
Exception->exceptionDestructor = dest;
Exception->unexpectedHandler = &unexpected_header_stub;
Exception->terminateHandler = &terminate_header_stub;
Exception->unwindHeader.exception_cleanup = &exception_cleanup_stub;
INIT_EXCEPTION_CLASS(&Exception->unwindHeader.exception_class);
Exception->adjustedPtr = thrown_object;
_Unwind_RaiseException(&Exception->unwindHeader);
__cxa_begin_catch(&Exception->unwindHeader);
error("Uncaught exception!");
CPU::Stop(); /* FIXME: Panic */
}
extern "C" void __cxa_rethrow()
{
fixme("__cxa_rethrow() called.");
}
extern "C" void __cxa_pure_virtual()
{
fixme("__cxa_pure_virtual() called.");
}
extern "C" void __cxa_throw_bad_array_new_length()
{
fixme("__cxa_throw_bad_array_new_length() called.");
}
extern "C" void __cxa_free_exception(void *thrown_exception)
{
fixme("__cxa_free_exception( %p ) called.",
thrown_exception);
}
__extension__ typedef int __guard __attribute__((mode(__DI__)));
extern "C" int __cxa_guard_acquire(__guard *g)
{
fixme("__cxa_guard_acquire( %p ) called.", g);
return !*(char *)(g);
}
extern "C" void __cxa_guard_release(__guard *g)
{
fixme("__cxa_guard_release( %p ) called.", g);
*(char *)g = 1;
}
extern "C" void __cxa_guard_abort(__guard *g)
{
fixme("__cxa_guard_abort( %p ) called.", g);
}
extern "C" __noreturn void __cxa_bad_typeid()
{
fixme("__cxa_bad_typeid() called.");
CPU::Stop(); /* FIXME: Crash the system */
}
extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
{
stub;
return nullptr;
}

25
library/kexcept/dso.c Normal file
View File

@ -0,0 +1,25 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <debug.h>
void __dso_handle_stub()
{
stub;
}
/* extern */ void *__dso_handle = (void *)&__dso_handle_stub;

View File

@ -15,8 +15,8 @@
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <unwind.h>
#include <cxxabi.h>
#include <kexcept/unwind.h>
#include <kexcept/cxxabi.h>
#include <debug.h>
#include <cpu.hpp>
#include <smp.hpp>
@ -25,17 +25,23 @@
using namespace __cxxabiv1;
#if (1) /* Stubs if libgcc is not present */
extern "C" _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *Exception)
#if (1) /* Set to 0 to use a library or 1 to use this code */
extern "C" _Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *object)
{
fixme("_Unwind_RaiseException( %p ) called.", Exception);
error("Unhandled exception.");
return _URC_FATAL_PHASE1_ERROR;
// return _URC_NO_REASON;
stub;
return _URC_NO_REASON;
}
extern "C" void _Unwind_Resume(struct _Unwind_Exception *Exception)
extern "C" void _Unwind_Resume(struct _Unwind_Exception *object)
{
fixme("_Unwind_Resume( %p ) called.", Exception);
stub;
}
#endif
extern "C" _Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *exception_object)
{
stub;
return _URC_NO_REASON;
}
#endif // 0 or 1

View File

@ -1,251 +0,0 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cxxabi.h>
#include <memory.hpp>
#include <debug.h>
#include <smp.hpp>
#include "../../kernel.h"
void __dso_handle_stub() { stub; }
/* extern */ void *__dso_handle = (void *)&__dso_handle_stub;
namespace __cxxabiv1
{
atexit_func_entry_t __atexit_funcs[ATEXIT_MAX_FUNCS];
uarch_t __atexit_func_count = 0;
__cxa_eh_globals *__cxa_get_globals() noexcept
{
return &GetCurrentCPU()->EHGlobals;
}
/**
* @param f The destructor
* @param objptr The object to be destructed
* @param dso The DSO from which the object was obtained (unused in our case)
* @return Zero on success, non-zero on failure
*/
extern "C" int __cxa_atexit(void (*f)(void *), void *objptr, void *dso)
{
if (KernelSymbolTable)
{
debug("Registering atexit function for \"%s\" with destructor \"%s\"",
KernelSymbolTable->GetSymbol((uintptr_t)objptr),
KernelSymbolTable->GetSymbol((uintptr_t)f));
}
else
{
debug("Registering atexit function for %p with destructor %p",
objptr, f);
}
if (__atexit_func_count >= ATEXIT_MAX_FUNCS)
return -1;
__atexit_funcs[__atexit_func_count].destructor_func = f;
__atexit_funcs[__atexit_func_count].obj_ptr = objptr;
__atexit_funcs[__atexit_func_count].dso_handle = dso;
__atexit_func_count++;
return 0;
}
extern "C" void __cxa_finalize(void *f)
{
function("%p", f);
uarch_t i = __atexit_func_count;
if (f == nullptr)
{
while (i--)
{
if (__atexit_funcs[i].destructor_func)
{
if (KernelSymbolTable)
{
debug("Calling atexit function \"%s\"",
KernelSymbolTable->GetSymbol((uintptr_t)__atexit_funcs[i].destructor_func));
}
else
{
debug("Calling atexit function %p",
__atexit_funcs[i].destructor_func);
}
(*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr);
}
}
return;
}
while (i--)
{
if (__atexit_funcs[i].destructor_func == f)
{
(*__atexit_funcs[i].destructor_func)(__atexit_funcs[i].obj_ptr);
__atexit_funcs[i].destructor_func = 0;
}
}
}
extern "C" _Unwind_Reason_Code __gxx_personality_v0(int version, _Unwind_Action actions, _Unwind_Exception_Class exception_class, _Unwind_Exception *ue_header, _Unwind_Context *context)
{
fixme("__gxx_personality_v0( %d %p %p %p %p ) called.", version, actions, exception_class, ue_header, context);
return _URC_NO_REASON;
}
extern "C" void *__cxa_begin_catch(void *thrown_object) noexcept
{
function("%p", thrown_object);
__cxa_exception *Exception = (__cxa_exception *)thrown_object - 1;
__cxa_eh_globals *Globals = __cxa_get_globals();
Exception->handlerCount++;
Globals->uncaughtExceptions--;
Exception->nextException = Globals->caughtExceptions;
Globals->caughtExceptions = Exception;
return Exception + 1;
}
extern "C" void __cxa_end_catch()
{
fixme("__cxa_end_catch() called.");
}
static __always_inline inline size_t align_exception_allocation_size(size_t a, size_t b)
{
return (a + b - 1) & ~(b - 1);
}
static __always_inline inline void INIT_EXCEPTION_CLASS(_Unwind_Exception_Class *c)
{
char *ptr = (char *)c;
ptr[0] = 'G';
ptr[1] = 'N';
ptr[2] = 'U';
ptr[3] = 'C';
ptr[4] = 'C';
ptr[5] = '+';
ptr[6] = '+';
ptr[7] = '\0';
}
void unexpected_header_stub() { fixme("unexpected() called."); }
void terminate_header_stub()
{
if (TaskManager && !TaskManager->IsPanic())
{
TaskManager->KillThread(thisThread, Tasking::KILL_CXXABI_EXCEPTION);
TaskManager->Yield();
}
error("No task manager to kill thread!");
CPU::Stop(); /* FIXME: Panic */
}
void exception_cleanup_stub(_Unwind_Reason_Code Code,
_Unwind_Exception *Exception)
{
fixme("exception_cleanup( %d %p ) called.",
Code, Exception);
}
extern "C" void *__cxa_allocate_exception(size_t thrown_size) throw()
{
debug("Allocating exception of size %d.", thrown_size);
size_t alloc_size = align_exception_allocation_size(thrown_size + sizeof(__cxa_exception), alignof(__cxa_exception));
__cxa_exception *Exception = (__cxa_exception *)kmalloc(alloc_size);
memset(Exception, 0, alloc_size);
return Exception + 1;
}
extern "C" __noreturn void __cxa_throw(void *thrown_object,
std::type_info *tinfo,
void (*dest)(void *))
{
trace("Throwing exception of type \"%s\". ( object: %p, destructor: %p )",
tinfo->name(), thrown_object, dest);
__cxa_eh_globals *Globals = __cxa_get_globals();
Globals->uncaughtExceptions++;
__cxa_exception *Exception = (__cxa_exception *)thrown_object - 1;
Exception->exceptionType = (std::type_info *)tinfo;
Exception->exceptionDestructor = dest;
Exception->unexpectedHandler = &unexpected_header_stub;
Exception->terminateHandler = &terminate_header_stub;
Exception->unwindHeader.exception_cleanup = &exception_cleanup_stub;
INIT_EXCEPTION_CLASS(&Exception->unwindHeader.exception_class);
Exception->adjustedPtr = thrown_object;
_Unwind_RaiseException(&Exception->unwindHeader);
__cxa_begin_catch(&Exception->unwindHeader);
error("Uncaught exception!");
CPU::Stop(); /* FIXME: Panic */
}
extern "C" void __cxa_rethrow()
{
fixme("__cxa_rethrow() called.");
}
extern "C" void __cxa_pure_virtual()
{
fixme("__cxa_pure_virtual() called.");
}
extern "C" void __cxa_throw_bad_array_new_length()
{
fixme("__cxa_throw_bad_array_new_length() called.");
}
extern "C" void __cxa_free_exception(void *thrown_exception)
{
fixme("__cxa_free_exception( %p ) called.",
thrown_exception);
}
__extension__ typedef int __guard __attribute__((mode(__DI__)));
extern "C" int __cxa_guard_acquire(__guard *g)
{
fixme("__cxa_guard_acquire( %p ) called.", g);
return !*(char *)(g);
}
extern "C" void __cxa_guard_release(__guard *g)
{
fixme("__cxa_guard_release( %p ) called.", g);
*(char *)g = 1;
}
extern "C" void __cxa_guard_abort(__guard *g)
{
fixme("__cxa_guard_abort( %p ) called.", g);
}
extern "C" __noreturn void __cxa_bad_typeid()
{
fixme("__cxa_bad_typeid() called.");
CPU::Stop(); /* FIXME: Crash the system */
}
}

View File

@ -0,0 +1,42 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <typeinfo>
#include <debug.h>
namespace __cxxabiv1
{
__vmi_class_type_info::~__vmi_class_type_info()
{
}
void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
{
if (__do_upcast(other, &obj))
return obj;
return 0;
}
bool __vmi_class_type_info::__do_upcast(const __class_type_info *target, void **thrown_object) const
{
if (this == target)
return true;
stub;
return 0;
}
}

View File

@ -32,274 +32,178 @@ EXTERNC int *__errno_location(void)
return &thisThread->ErrorNumber;
}
const char *strerror(int errnum)
char *strerror(int errnum)
{
if (errnum < 0)
errnum = -errnum;
switch (errnum)
{
case 0:
return "Success";
case EPERM:
return "Operation not permitted";
case ENOENT:
return "No such file or directory";
case ESRCH:
return "No such process";
case EINTR:
return "Interrupted system call";
case EIO:
return "Input/output error";
case ENXIO:
return "No such device or address";
case EOK:
return (char *)"No error";
case E2BIG:
return "Argument list too long";
case ENOEXEC:
return "Exec format error";
case EBADF:
return "Bad file descriptor";
case ECHILD:
return "No child processes";
case EAGAIN:
return "Resource temporarily unavailable";
case ENOMEM:
return "Cannot allocate memory";
return (char *)"Argument list too long";
case EACCES:
return "Permission denied";
case EFAULT:
return "Bad address";
case ENOTBLK:
return "Block device required";
case EBUSY:
return "Device or resource busy";
case EEXIST:
return "File exists";
case EXDEV:
return "Invalid cross-device link";
case ENODEV:
return "No such device";
case ENOTDIR:
return "Not a directory";
case EISDIR:
return "Is a directory";
case EINVAL:
return "Invalid argument";
case ENFILE:
return "Too many open files in system";
case EMFILE:
return "Too many open files";
case ENOTTY:
return "Inappropriate ioctl for device";
case ETXTBSY:
return "Text file busy";
case EFBIG:
return "File too large";
case ENOSPC:
return "No space left on device";
case ESPIPE:
return "Illegal seek";
case EROFS:
return "Read-only file system";
case EMLINK:
return "Too many links";
case EPIPE:
return "Broken pipe";
case EDOM:
return "Numerical argument out of domain";
case ERANGE:
return "Numerical result out of range";
case EDEADLK:
return "Resource deadlock avoided";
case ENAMETOOLONG:
return "File name too long";
case ENOLCK:
return "No locks available";
case ENOSYS:
return "Function not implemented";
case ENOTEMPTY:
return "Directory not empty";
case ELOOP:
return "Too many levels of symbolic links";
case ENOMSG:
return "No message of desired type";
case EIDRM:
return "Identifier removed";
case ECHRNG:
return "Channel number out of range";
case EL2NSYNC:
return "Level 2 not synchronized";
case EL3HLT:
return "Level 3 halted";
case EL3RST:
return "Level 3 reset";
case ELNRNG:
return "Link number out of range";
case EUNATCH:
return "Protocol driver not attached";
case ENOCSI:
return "No CSI structure available";
case EL2HLT:
return "Level 2 halted";
case EBADE:
return "Invalid exchange";
case EBADR:
return "Invalid request descriptor";
case EXFULL:
return "Exchange full";
case ENOANO:
return "No anode";
case EBADRQC:
return "Invalid request code";
case EBADSLT:
return "Invalid slot";
case EBFONT:
return "Bad font file format";
case ENOSTR:
return "Device not a stream";
case ENODATA:
return "No data available";
case ETIME:
return "Timer expired";
case ENOSR:
return "Out of streams resources";
case ENONET:
return "Machine is not on the network";
case ENOPKG:
return "Package not installed";
case EREMOTE:
return "Object is remote";
case ENOLINK:
return "Link has been severed";
case EADV:
return "Advertise error";
case ESRMNT:
return "Srmount error";
case ECOMM:
return "Communication error on send";
case EPROTO:
return "Protocol error";
case EMULTIHOP:
return "Multihop attempted";
case EDOTDOT:
return "RFS specific error";
case EBADMSG:
return "Bad message";
case EOVERFLOW:
return "Value too large for defined data type";
case ENOTUNIQ:
return "Name not unique on network";
case EBADFD:
return "File descriptor in bad state";
case EREMCHG:
return "Remote address changed";
case ELIBACC:
return "Can not access a needed shared library";
case ELIBBAD:
return "Accessing a corrupted shared library";
case ELIBSCN:
return ".lib section in a.out corrupted";
case ELIBMAX:
return "Attempting to link in too many shared libraries";
case ELIBEXEC:
return "Cannot exec a shared library directly";
case EILSEQ:
return "Illegal byte sequence";
case ERESTART:
return "Interrupted system call should be restarted";
case ESTRPIPE:
return "Streams pipe error";
case EUSERS:
return "Too many users";
case ENOTSOCK:
return "Socket operation on non-socket";
case EDESTADDRREQ:
return "Destination address required";
case EMSGSIZE:
return "Message too long";
case EPROTOTYPE:
return "Protocol wrong type for socket";
case ENOPROTOOPT:
return "Protocol not available";
case EPROTONOSUPPORT:
return "Protocol not supported";
case ESOCKTNOSUPPORT:
return "Socket type not supported";
case EOPNOTSUPP:
return "Operation not supported";
case EPFNOSUPPORT:
return "Protocol family not supported";
case EAFNOSUPPORT:
return "Address family not supported by protocol";
return (char *)"Permission denied";
case EADDRINUSE:
return "Address already in use";
return (char *)"Address in use";
case EADDRNOTAVAIL:
return "Cannot assign requested address";
case ENETDOWN:
return "Network is down";
case ENETUNREACH:
return "Network is unreachable";
case ENETRESET:
return "Network dropped connection on reset";
case ECONNABORTED:
return "Software caused connection abort";
case ECONNRESET:
return "Connection reset by peer";
case ENOBUFS:
return "No buffer space available";
case EISCONN:
return "Transport endpoint is already connected";
case ENOTCONN:
return "Transport endpoint is not connected";
case ESHUTDOWN:
return "Cannot send after transport endpoint shutdown";
case ETOOMANYREFS:
return "Too many references: cannot splice";
case ETIMEDOUT:
return "Connection timed out";
case ECONNREFUSED:
return "Connection refused";
case EHOSTDOWN:
return "Host is down";
case EHOSTUNREACH:
return "No route to host";
return (char *)"Address not available";
case EAFNOSUPPORT:
return (char *)"Address family not supported";
case EAGAIN:
return (char *)"Resource unavailable, try again";
case EALREADY:
return "Operation already in progress";
case EINPROGRESS:
return "Operation now in progress";
case ESTALE:
return "Stale file handle";
case EUCLEAN:
return "Structure needs cleaning";
case ENOTNAM:
return "Not a XENIX named type file";
case ENAVAIL:
return "No XENIX semaphores available";
case EISNAM:
return "Is a named type file";
case EREMOTEIO:
return "Remote I/O error";
case EDQUOT:
return "Quota exceeded";
case ENOMEDIUM:
return "No medium found";
case EMEDIUMTYPE:
return "Wrong medium type";
return (char *)"Connection already in progress";
case EBADF:
return (char *)"Bad file descriptor";
case EBADMSG:
return (char *)"Bad message";
case EBUSY:
return (char *)"Device or resource busy";
case ECANCELED:
return "Operation Canceled";
case ENOKEY:
return "Required key not available";
case EKEYEXPIRED:
return "Key has expired";
case EKEYREVOKED:
return "Key has been revoked";
case EKEYREJECTED:
return "Key was rejected by service";
case EOWNERDEAD:
return "Owner died";
return (char *)"Operation canceled";
case ECHILD:
return (char *)"No child processes";
case ECONNABORTED:
return (char *)"Connection aborted";
case ECONNREFUSED:
return (char *)"Connection refused";
case ECONNRESET:
return (char *)"Connection reset";
case EDEADLK:
return (char *)"Resource deadlock would occur";
case EDESTADDRREQ:
return (char *)"Destination address required";
case EDOM:
return (char *)"Mathematics argument out of domain of function";
case EDQUOT:
return (char *)"Reserved";
case EEXIST:
return (char *)"File exists";
case EFAULT:
return (char *)"Bad address";
case EFBIG:
return (char *)"File too large";
case EHOSTUNREACH:
return (char *)"Host is unreachable";
case EIDRM:
return (char *)"Identifier removed";
case EILSEQ:
return (char *)"Illegal byte sequence";
case EINPROGRESS:
return (char *)"Operation in progress";
case EINTR:
return (char *)"Interrupted function";
case EINVAL:
return (char *)"Invalid argument";
case EIO:
return (char *)"I/O error";
case EISCONN:
return (char *)"Socket is connected";
case EISDIR:
return (char *)"Is a directory";
case ELOOP:
return (char *)"Too many levels of symbolic links";
case EMFILE:
return (char *)"File descriptor value too large";
case EMLINK:
return (char *)"Too many links";
case EMSGSIZE:
return (char *)"Message too large";
case EMULTIHOP:
return (char *)"Reserved";
case ENAMETOOLONG:
return (char *)"Filename too long";
case ENETDOWN:
return (char *)"Network is down";
case ENETRESET:
return (char *)"Connection aborted by network";
case ENETUNREACH:
return (char *)"Network unreachable";
case ENFILE:
return (char *)"Too many files open in system";
case ENOBUFS:
return (char *)"No buffer space available";
case ENODATA:
return (char *)"No message available on the STREAM head read queue";
case ENODEV:
return (char *)"No such device";
case ENOENT:
return (char *)"No such file or directory";
case ENOEXEC:
return (char *)"Executable file format error";
case ENOLCK:
return (char *)"No locks available";
case ENOLINK:
return (char *)"Reserved";
case ENOMEM:
return (char *)"Not enough space";
case ENOMSG:
return (char *)"No message of the desired type";
case ENOPROTOOPT:
return (char *)"Protocol not available";
case ENOSPC:
return (char *)"No space left on device";
case ENOSR:
return (char *)"No STREAM resources";
case ENOSTR:
return (char *)"Not a STREAM";
case ENOSYS:
return (char *)"Functionality not supported";
case ENOTCONN:
return (char *)"The socket is not connected";
case ENOTDIR:
return (char *)"Not a directory or a symbolic link to a directory";
case ENOTEMPTY:
return (char *)"Directory not empty";
case ENOTRECOVERABLE:
return "State not recoverable";
return (char *)"State not recoverable";
case ENOTSOCK:
return (char *)"Not a socket";
case ENOTSUP:
return (char *)"Not supported";
case ENOTTY:
return (char *)"Inappropriate I/O control operation";
case ENXIO:
return (char *)"No such device or address";
case EOPNOTSUPP:
return (char *)"Operation not supported on socket";
case EOVERFLOW:
return (char *)"Value too large to be stored in data type";
case EOWNERDEAD:
return (char *)"Previous owner died";
case EPERM:
return (char *)"Operation not permitted";
case EPIPE:
return (char *)"Broken pipe";
case EPROTO:
return (char *)"Protocol error";
case EPROTONOSUPPORT:
return (char *)"Protocol not supported";
case EPROTOTYPE:
return (char *)"Protocol wrong type for socket";
case ERANGE:
return (char *)"Result too large";
case EROFS:
return (char *)"Read-only file system";
case ESPIPE:
return (char *)"Invalid seek";
case ESRCH:
return (char *)"No such process";
case ESTALE:
return (char *)"Reserved";
case ETIME:
return (char *)"Stream ioctl() timeout";
case ETIMEDOUT:
return (char *)"Connection timed out";
case ETXTBSY:
return (char *)"Text file busy";
case EWOULDBLOCK:
return (char *)"Operation would block";
case EXDEV:
return (char *)"Cross-device link";
default:
return "Unknown error";
return (char *)"Unknown error";
}
}
}

View File

@ -33,6 +33,7 @@ namespace std
bool Result = this->Locked.exchange(true, std::memory_order_acquire);
TCB *tcb = thisThread;
assert(tcb != nullptr);
if (Result == true)
{
debug("%#lx: Mutex is locked, blocking task %d (\"%s\" : %d)", this,
@ -56,6 +57,7 @@ namespace std
bool Result = this->Locked.exchange(true, std::memory_order_acquire);
TCB *tcb = thisThread;
assert(tcb != nullptr);
if (Result == true)
{
debug("%#lx: Mutex is locked, task %d (\"%s\" : %d) failed to lock", this,
@ -74,6 +76,7 @@ namespace std
void mutex::unlock()
{
TCB *tcb = thisThread;
assert(tcb != nullptr);
assert(this->Holder == tcb);
this->Holder = nullptr;

82
library/std/new.cpp Normal file
View File

@ -0,0 +1,82 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <new>
#include <memory.hpp>
#include <stdexcept>
void *operator new(std::size_t count)
{
if (count == 0)
++count;
if (void *ptr = kmalloc(count))
return ptr;
throw std::bad_alloc{};
}
void *operator new[](std::size_t count)
{
if (count == 0)
++count;
if (void *ptr = kmalloc(count))
return ptr;
throw std::bad_alloc{};
}
// void *operator new(std::size_t count, std::align_val_t al)
// void *operator new[](std::size_t count, std::align_val_t al)
// void *operator new(std::size_t count, const std::nothrow_t &tag)
// void *operator new[](std::size_t count, const std::nothrow_t &tag)
// void *operator new(std::size_t count, std::align_val_t al, const std::nothrow_t &)
// void *operator new[](std::size_t count, std::align_val_t al, const std::nothrow_t &)
void *operator new(std::size_t, void *ptr) noexcept { return ptr; }
// void *operator new[](std::size_t count, void *ptr) noexcept
// void *operator new(std::size_t count, ...)
// void *operator new[](std::size_t count, ...)
// void *operator new(std::size_t count, std::align_val_t al, ...)
// void *operator new[](std::size_t count, std::align_val_t al, ...)
void operator delete(void *ptr) noexcept { kfree(ptr); }
void operator delete[](void *ptr) noexcept { kfree(ptr); }
// void operator delete(void *ptr, std::align_val_t al) noexcept
// void operator delete[](void *ptr, std::align_val_t al) noexcept
void operator delete(void *ptr, std::size_t) noexcept { kfree(ptr); }
void operator delete[](void *ptr, std::size_t sz) noexcept { kfree(ptr); }
void operator delete(void *ptr, std::size_t sz, std::align_val_t al) noexcept { kfree(ptr); }
// void operator delete[](void *ptr, std::size_t sz, std::align_val_t al) noexcept
// void operator delete(void *ptr, const std::nothrow_t &tag) noexcept
// void operator delete[](void *ptr, const std::nothrow_t &tag) noexcept
// void operator delete(void *ptr, std::align_val_t al, const std::nothrow_t &tag) noexcept
// void operator delete[](void *ptr, std::align_val_t al, const std::nothrow_t &tag) noexcept
// void operator delete(void *ptr, void *place) noexcept
// void operator delete[](void *ptr, void *place) noexcept
// void operator delete(void *ptr, ...)
// void operator delete[](void *ptr, ...)

41
library/std/printf.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string>
#include <printf.h>
namespace std
{
int sprintf(char *s, const char *format, ...)
{
va_list args;
va_start(args, format);
int ret = vsprintf(s, format, args);
va_end(args);
return ret;
}
int snprintf(char *s, size_t count, const char *format, ...)
{
va_list args;
va_start(args, format);
int ret = vsnprintf(s, count, format, args);
va_end(args);
return ret;
}
}