From 6b95307b549c78c8244df8cd41f806312b3d98f4 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 13 Nov 2022 12:20:53 +0100 Subject: [PATCH] Add init --- .gitignore | 3 +- CMakeLists.txt | 1 - Makefile | 26 ----- kernel/CMakeLists.txt | 1 + kernel/src/Init.cpp | 22 ++++ kernel/src/Init.h | 7 ++ kernel/src/main.cpp | 6 +- kernel/src/string.cpp | 5 +- luna/Move.h | 6 + luna/PlacementNew.h | 13 +++ luna/Result.h | 234 +++++++++++++++++++++++++++++++++++++++ luna/String.h | 95 +++++++++------- tools/build-iso.sh | 11 +- tools/build.sh | 9 +- tools/buildstep.sh | 10 -- tools/env.sh | 12 +- tools/install-headers.sh | 5 +- tools/install.sh | 2 +- tools/test.sh | 10 -- 19 files changed, 362 insertions(+), 116 deletions(-) delete mode 100644 Makefile create mode 100644 kernel/src/Init.cpp create mode 100644 kernel/src/Init.h create mode 100644 luna/Move.h create mode 100644 luna/PlacementNew.h create mode 100644 luna/Result.h delete mode 100755 tools/buildstep.sh delete mode 100755 tools/test.sh diff --git a/.gitignore b/.gitignore index 1a09d8ef..616233bd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ Luna.iso toolchain/ .vscode/ build/ -initrd/boot/moon \ No newline at end of file +initrd/boot/moon +env-local.sh \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 97cc6d2b..38fe3a8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.8..3.22) set(CMAKE_C_COMPILER_WORKS 1) set(CMAKE_CXX_COMPILER_WORKS 1) -set(CMAKE_SYSTEM_NAME Luna) set(CMAKE_CROSSCOMPILING true) project(Luna LANGUAGES C CXX ASM) diff --git a/Makefile b/Makefile deleted file mode 100644 index 2eb69633..00000000 --- a/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -CC := x86_64-luna-gcc -CXX := x86_64-luna-g++ -ASM := nasm -AR := x86_64-luna-ar -LD := x86_64-luna-ld - -build: - +@tools/sync-libc.sh - +@tools/buildstep.sh kernel build - +@tools/buildstep.sh libs build - +@tools/buildstep.sh apps build - -clean: initrd-clean - +@tools/buildstep.sh kernel clean - +@tools/buildstep.sh libs clean - +@tools/buildstep.sh apps clean - -initrd-clean: - rm -f $(LUNA_ROOT)/initrd/boot/moon $(LUNA_ROOT)/Luna.iso - rm -rf $(LUNA_ROOT)/initrd/bin - -install: - +@tools/buildstep.sh kernel install - +@tools/buildstep.sh libs install - +@tools/buildstep.sh apps install - +@tools/install-built-ports.sh \ No newline at end of file diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index c2fa7223..592841e1 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES src/main.cpp src/string.cpp src/Framebuffer.cpp + src/Init.cpp src/arch/Serial.cpp ) diff --git a/kernel/src/Init.cpp b/kernel/src/Init.cpp new file mode 100644 index 00000000..872d55d4 --- /dev/null +++ b/kernel/src/Init.cpp @@ -0,0 +1,22 @@ +#include "Init.h" +#include "Framebuffer.h" +#include "arch/Serial.h" +#include "bootboot.h" +#include + +extern BOOTBOOT bootboot; + +void Init::check_magic() +{ + if (memcmp(bootboot.magic, BOOTBOOT_MAGIC, 4)) + { + Serial::println("ERROR: Invalid magic value from bootloader"); + for (;;) + ; + } +} + +void Init::early_init() +{ + Framebuffer::init(); +} \ No newline at end of file diff --git a/kernel/src/Init.h b/kernel/src/Init.h new file mode 100644 index 00000000..5c5479c1 --- /dev/null +++ b/kernel/src/Init.h @@ -0,0 +1,7 @@ +#pragma once + +namespace Init +{ + void early_init(); + void check_magic(); +} \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index b2721fe4..0668b1bd 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -1,11 +1,13 @@ #include "Framebuffer.h" +#include "Init.h" #include "arch/Serial.h" extern "C" void _start() { - Serial::println("Hello, world!"); + Init::check_magic(); + Init::early_init(); - Framebuffer::init(); + Serial::println("Hello, world!"); Framebuffer::rect(0, 0, 200, 200, 0xFF00FF00); diff --git a/kernel/src/string.cpp b/kernel/src/string.cpp index 68167cd2..7c431e6c 100644 --- a/kernel/src/string.cpp +++ b/kernel/src/string.cpp @@ -1,5 +1,2 @@ -extern "C" -{ #define _LUNA_IMPLEMENTATION -#include -} \ No newline at end of file +#include \ No newline at end of file diff --git a/luna/Move.h b/luna/Move.h new file mode 100644 index 00000000..9006d68a --- /dev/null +++ b/luna/Move.h @@ -0,0 +1,6 @@ +#pragma once + +template inline T&& move(T& lvalue) +{ + return (T &&) lvalue; +} \ No newline at end of file diff --git a/luna/PlacementNew.h b/luna/PlacementNew.h new file mode 100644 index 00000000..1e6afcd1 --- /dev/null +++ b/luna/PlacementNew.h @@ -0,0 +1,13 @@ +#pragma once +#include + +inline void* operator new(size_t, void* p) noexcept +{ + return p; +} +inline void* operator new[](size_t, void* p) noexcept +{ + return p; +} +inline void operator delete(void*, void*) noexcept {}; +inline void operator delete[](void*, void*) noexcept {}; \ No newline at end of file diff --git a/luna/Result.h b/luna/Result.h new file mode 100644 index 00000000..c2e79d6c --- /dev/null +++ b/luna/Result.h @@ -0,0 +1,234 @@ +#pragma once +#include +#include +#include + +struct Error +{ + Error(int err) + { + error = err; + } + + int error; +}; + +template class Result +{ + public: + Result(const T& value) + { + m_storage.store_reference(value); + m_has_value = true; + m_has_error = false; + } + + Result(T&& value) + { + m_storage.store_movable_reference(value); + m_has_value = true; + m_has_error = false; + } + + Result(const Result& other) + { + if (!other.m_has_error) + { + m_storage.store_reference(other.m_storage.fetch_reference()); + m_has_value = true; + m_has_error = false; + } + else + { + m_has_error = true; + m_has_value = false; + m_error = other.m_error; + } + } + + Result(Result&& other) + { + if (!other.m_has_error) + { + m_storage.store_movable_reference(move(other.m_storage.fetch_reference())); + m_has_value = true; + m_has_error = false; + } + else + { + m_has_error = true; + m_has_value = false; + m_error = other.m_error; + } + } + + Result(const Error& err) + { + m_error = err.error; + m_has_error = true; + m_has_value = false; + } + + bool has_error() + { + return m_has_error; + } + + bool has_value() + { + return m_has_value; + } + + int error() + { + // ensure(has_error()); + return m_error; + } + + Error release_error() + { + // ensure(has_error()); + return {m_error}; + } + + T value() + { + // ensure(has_value()); + return m_storage.fetch_reference(); + } + + T value_or(T other) + { + if (has_value()) return m_storage.fetch_reference(); + return other; + } + + T release_value() + { + // ensure(has_value()); + T item = m_storage.fetch_reference(); + m_has_value = false; + m_storage.destroy(); + return move(item); + } + + ~Result() + { + if (has_value()) m_storage.destroy(); + } + + private: + struct Storage + { + u8 buffer[sizeof(T)]; + + T* fetch_ptr() + { + return (T*)buffer; + } + + T& fetch_reference() + { + return *fetch_ptr(); + } + + const T* fetch_ptr() const + { + return (const T*)buffer; + } + + const T& fetch_reference() const + { + return *fetch_ptr(); + } + + void store_ptr(T* ptr) + { + new (buffer) T(*ptr); + } + + void store_reference(const T& ref) + { + new (buffer) T(ref); + } + + void store_movable_reference(T&& ref) + { + new (buffer) T(ref); + } + + void destroy() + { + fetch_reference().~T(); + } + }; + Storage m_storage; + int m_error; + bool m_has_error; + bool m_has_value; +}; + +template <> class Result +{ + public: + Result() + { + m_has_error = false; + } + + Result(const Result& other) + { + m_has_error = other.m_has_error; + m_error = other.m_error; + } + + Result(Result&& other) + { + m_has_error = other.m_has_error; + m_error = other.m_error; + } + + Result(const Error& err) + { + m_error = err.error; + m_has_error = true; + } + + bool has_error() + { + return m_has_error; + } + + bool has_value() + { + return !m_has_error; + } + + int error() + { + // ensure(has_error()); + return m_error; + } + + Error release_error() + { + // ensure(has_error()); + return {m_error}; + } + + void value() + { + // ensure(has_value()); + return; + } + + void release_value() + { + // ensure(has_value()); + return; + } + + private: + int m_error; + bool m_has_error; +}; \ No newline at end of file diff --git a/luna/String.h b/luna/String.h index bba602be..9bfc8230 100644 --- a/luna/String.h +++ b/luna/String.h @@ -1,65 +1,74 @@ #pragma once #include -void* memcpy(void* dest, const void* src, size_t n) -#ifdef _LUNA_IMPLEMENTATION +#ifndef NO_EXTERN_C +extern "C" { - for (size_t i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); } - return dest; -} -#else - ; #endif -void* memset(void* buf, int c, size_t n) + void* memcpy(void* dest, const void* src, size_t n) #ifdef _LUNA_IMPLEMENTATION -{ - for (size_t i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; } - return buf; -} -#else - ; -#endif - -int memcmp(const void* a, const void* b, size_t n) -#ifdef _LUNA_IMPLEMENTATION -{ - if (!n) return 0; - const u8* ap = (const u8*)a; - const u8* bp = (const u8*)b; - while (--n && *ap == *bp) { - ap++; - bp++; + for (size_t i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); } + return dest; } - return *ap - *bp; -} #else ; #endif -void* memmove(void* dest, const void* src, size_t n) + void* memset(void* buf, int c, size_t n) #ifdef _LUNA_IMPLEMENTATION -{ - if (dest == src) return dest; - if (dest > src) - for (long i = n - 1; i >= 0; i++) { *((u8*)dest + i) = *((const u8*)src + i); } - else - for (long i = 0; i < (long)n; i++) { *((u8*)dest + i) = *((const u8*)src + i); } - return dest; -} + { + for (size_t i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; } + return buf; + } #else ; #endif -size_t strlen(const char* str) + int memcmp(const void* a, const void* b, size_t n) #ifdef _LUNA_IMPLEMENTATION -{ - const char* i = str; - for (; *i; ++i) - ; - return (i - str); -} + { + if (!n) return 0; + const u8* ap = (const u8*)a; + const u8* bp = (const u8*)b; + while (--n && *ap == *bp) + { + ap++; + bp++; + } + return *ap - *bp; + } #else ; +#endif + + void* memmove(void* dest, const void* src, size_t n) +#ifdef _LUNA_IMPLEMENTATION + { + if (dest == src) return dest; + if (dest > src) + for (long i = n - 1; i >= 0; i++) { *((u8*)dest + i) = *((const u8*)src + i); } + else + for (long i = 0; i < (long)n; i++) { *((u8*)dest + i) = *((const u8*)src + i); } + return dest; + } +#else + ; +#endif + + size_t strlen(const char* str) +#ifdef _LUNA_IMPLEMENTATION + { + const char* i = str; + for (; *i; ++i) + ; + return (i - str); + } +#else + ; +#endif + +#ifndef NO_EXTERN_C +} #endif \ No newline at end of file diff --git a/tools/build-iso.sh b/tools/build-iso.sh index c810c1e9..ac9c3c6e 100755 --- a/tools/build-iso.sh +++ b/tools/build-iso.sh @@ -7,9 +7,14 @@ cd $LUNA_ROOT tools/setup.sh -tools/install-headers.sh +#tools/install-headers.sh -make -j$(nproc) -make install +mkdir -p build +if [ "$USE_NINJA" = "1" ] +then +cmake -S . -B build -G Ninja +fi +cmake --build build +cmake --install build mkbootimg luna.json Luna.iso \ No newline at end of file diff --git a/tools/build.sh b/tools/build.sh index 0d0e5d9e..b5855836 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -7,6 +7,11 @@ cd $LUNA_ROOT tools/setup.sh -tools/install-headers.sh +#tools/install-headers.sh -make -j$(nproc) \ No newline at end of file +mkdir -p build +if [ "$USE_NINJA" = "1" ] +then +cmake -S . -B build -G Ninja +fi +cmake --build build \ No newline at end of file diff --git a/tools/buildstep.sh b/tools/buildstep.sh deleted file mode 100755 index fe06e0d3..00000000 --- a/tools/buildstep.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash -set -e - -unset -f filter-lines -filter-lines() -{ - sed $'s|^|\x1b[33m('"$1/$2"$')\x1b[39m |' -} - -make -C $1 $2 | filter-lines $1 $2 \ No newline at end of file diff --git a/tools/env.sh b/tools/env.sh index 4a9e8061..6d99f72b 100755 --- a/tools/env.sh +++ b/tools/env.sh @@ -3,14 +3,4 @@ export LUNA_ROOT=${LUNA_ROOT:-$(realpath $(dirname $0)/..)} export LUNA_BASE=${LUNA_BASE:-$LUNA_ROOT/base} export PATH=$LUNA_ROOT/toolchain/x86-64-luna/bin:$LUNA_ROOT/toolchain/dist:$PATH -export CC=x86_64-luna-gcc -export CXX=x86_64-luna-g++ -export LD=x86_64-luna-ld -export AR=x86_64-luna-ar -export ASM=nasm -export STRIP=x86_64-luna-strip - -filter-lines() -{ - sed $'s|^|\x1b[32m('"$1/$2"$')\x1b[39m |' -} \ No newline at end of file +[ -f "$LUNA_ROOT/env-local.sh" ] && source $LUNA_ROOT/env-local.sh \ No newline at end of file diff --git a/tools/install-headers.sh b/tools/install-headers.sh index cbacbd09..eed3b849 100755 --- a/tools/install-headers.sh +++ b/tools/install-headers.sh @@ -8,6 +8,7 @@ cd $LUNA_ROOT mkdir -p base mkdir -p base/usr/include mkdir -p base/usr/include/moon +mkdir -p base/usr/include/luna -cp -RT libs/libc/include base/usr/include -cp -RT kernel/include base/usr/include/moon \ No newline at end of file +cp -RT kernel/**/*.h base/usr/include/moon +cp -RT luna/*.h base/usr/include/luna \ No newline at end of file diff --git a/tools/install.sh b/tools/install.sh index 7b903dc2..99247915 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -5,4 +5,4 @@ source $(dirname $0)/env.sh cd $LUNA_ROOT -make install \ No newline at end of file +cmake --install build \ No newline at end of file diff --git a/tools/test.sh b/tools/test.sh deleted file mode 100755 index d73fd091..00000000 --- a/tools/test.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -set -e -source $(dirname $0)/env.sh - -cd $LUNA_ROOT - -make -C tests build -make -C tests install -make -C tests test \ No newline at end of file