This commit is contained in:
apio 2022-11-13 12:20:53 +01:00
parent ffbe5260a5
commit 6b95307b54
19 changed files with 362 additions and 116 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ toolchain/
.vscode/
build/
initrd/boot/moon
env-local.sh

View File

@ -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)

View File

@ -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

View File

@ -2,6 +2,7 @@ set(SOURCES
src/main.cpp
src/string.cpp
src/Framebuffer.cpp
src/Init.cpp
src/arch/Serial.cpp
)

22
kernel/src/Init.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "Init.h"
#include "Framebuffer.h"
#include "arch/Serial.h"
#include "bootboot.h"
#include <String.h>
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();
}

7
kernel/src/Init.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
namespace Init
{
void early_init();
void check_magic();
}

View File

@ -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);

View File

@ -1,5 +1,2 @@
extern "C"
{
#define _LUNA_IMPLEMENTATION
#include <String.h>
}

6
luna/Move.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
template <typename T> inline T&& move(T& lvalue)
{
return (T &&) lvalue;
}

13
luna/PlacementNew.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <stddef.h>
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 {};

234
luna/Result.h Normal file
View File

@ -0,0 +1,234 @@
#pragma once
#include <Move.h>
#include <PlacementNew.h>
#include <Types.h>
struct Error
{
Error(int err)
{
error = err;
}
int error;
};
template <typename T> 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<T>& 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<T>&& 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<void>
{
public:
Result()
{
m_has_error = false;
}
Result(const Result<void>& other)
{
m_has_error = other.m_has_error;
m_error = other.m_error;
}
Result(Result<void>&& 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;
};

View File

@ -1,6 +1,11 @@
#pragma once
#include <Types.h>
#ifndef NO_EXTERN_C
extern "C"
{
#endif
void* memcpy(void* dest, const void* src, size_t n)
#ifdef _LUNA_IMPLEMENTATION
{
@ -63,3 +68,7 @@ size_t strlen(const char* str)
#else
;
#endif
#ifndef NO_EXTERN_C
}
#endif

View File

@ -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

View File

@ -7,6 +7,11 @@ cd $LUNA_ROOT
tools/setup.sh
tools/install-headers.sh
#tools/install-headers.sh
make -j$(nproc)
mkdir -p build
if [ "$USE_NINJA" = "1" ]
then
cmake -S . -B build -G Ninja
fi
cmake --build build

View File

@ -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

View File

@ -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 |'
}
[ -f "$LUNA_ROOT/env-local.sh" ] && source $LUNA_ROOT/env-local.sh

View File

@ -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
cp -RT kernel/**/*.h base/usr/include/moon
cp -RT luna/*.h base/usr/include/luna

View File

@ -5,4 +5,4 @@ source $(dirname $0)/env.sh
cd $LUNA_ROOT
make install
cmake --install build

View File

@ -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