Move initialization into an init() function returning Result<void> so we can use TRY

And remove a lot of testing code as well.
This commit is contained in:
apio 2022-11-20 17:56:07 +01:00
parent a58b60d0cf
commit 35b7194fb7

View File

@ -5,94 +5,37 @@
#include "boot/Init.h"
#include "memory/Heap.h"
#include "memory/MemoryManager.h"
#include "video/Framebuffer.h"
#include "video/TextConsole.h"
extern u8 fb[1];
extern "C" [[noreturn]] void _start()
Result<void> init()
{
Init::check_magic();
Init::early_init();
Serial::println("Hello, world!");
Serial::printf("Current platform: %s\n", CPU::platform_string());
Framebuffer::rect(0, 0, 200, 200, 0xFF00FF00);
auto cpu_name_or_error = CPU::identify();
Serial::println(cpu_name_or_error.has_error() ? "Unable to determine CPU name" : cpu_name_or_error.release_value());
Serial::println(MMU::get_physical((u64)fb).has_error() ? "fb is not mapped" : "fb is mapped!!");
Serial::println(TRY(CPU::identify()));
const u64 address = 0xfffffffff8000000;
Serial::printf("Mapping address 0x%lx\n", address);
u64 physical = MemoryManager::alloc_frame().release_value();
TRY(MemoryManager::alloc_at(address, 1, MMU::ReadWrite));
Serial::printf("Allocated physical frame %#lx\n", physical);
TRY(MMU::remap(address, MMU::ReadWrite | MMU::User));
auto rc = MMU::map(address, physical, MMU::ReadWrite);
bool success = !rc.has_error();
int flags;
volatile u8* ptr;
if (success) Serial::println("Mapped page :)");
else
{
Serial::println("Failed to map page");
CPU::efficient_halt();
}
if (MMU::get_physical(address).release_value() == physical) Serial::println("Mapping is active ;)");
else
{
Serial::println("Mapping is not active");
CPU::efficient_halt();
}
flags = MMU::get_flags(address).release_value();
if (flags & MMU::ReadWrite) Serial::println("Mapping is writable");
if (flags & MMU::User) Serial::println("Mapping is user accessible");
auto rrc = MMU::remap(address, MMU::ReadWrite | MMU::User);
if (rrc.has_error())
{
Serial::println("Failed to change flags of mapping");
CPU::efficient_halt();
}
flags = MMU::get_flags(address).release_value();
int flags = TRY(MMU::get_flags(address));
if (flags & MMU::ReadWrite) Serial::println("Mapping is now writable");
if (flags & MMU::User) Serial::println("Mapping is now user accessible");
ptr = (volatile u8*)address;
u64 old = TRY(MMU::unmap(address));
*ptr = 8;
Serial::printf("Unmapped page, was pointing to %#lx\n", old);
Serial::println("Can write to pointer");
TextConsole::set_foreground(0xff000055);
TextConsole::set_background(0xff88ff00);
auto urc = MMU::unmap(address);
if (urc.has_error())
{
Serial::println("Failed to unmap page");
CPU::efficient_halt();
}
Serial::printf("Unmapped page, was pointing to %#lx\n", urc.value());
if (urc.release_value() != physical)
{
Serial::println("unmap returned a different address than the one we mapped");
CPU::efficient_halt();
}
Serial::println("Successfully unmapped address");
TextConsole::printf("Hello from Moon for the %s architecture!", CPU::platform_string());
Timer::init();
@ -102,15 +45,11 @@ extern "C" [[noreturn]] void _start()
usize start = 0;
int* mem = make<int>().release_value();
int* mem = TRY(make<int>());
*(volatile int*)mem = 6;
Serial::printf("Read %d from memory\n", *mem);
mem = (int*)krealloc(mem, 60).release_value();
Serial::printf("Resized to %p\n", (void*)mem);
destroy(mem).release_value();
TRY(destroy(mem));
while (1)
{
@ -119,5 +58,14 @@ extern "C" [[noreturn]] void _start()
Serial::printf("%8zu milliseconds have passed!\n", start);
}
return {};
}
extern "C" [[noreturn]] void _start()
{
Init::check_magic();
Init::early_init();
auto rc = init();
rc.release_value();
CPU::efficient_halt();
}