Luna/kernel/src/main.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2022-11-30 13:29:28 +01:00
#include "Log.h"
2022-11-15 19:10:32 +01:00
#include "arch/CPU.h"
2022-11-13 14:29:15 +01:00
#include "arch/MMU.h"
2022-11-13 10:30:10 +01:00
#include "arch/Serial.h"
2022-11-19 20:01:01 +01:00
#include "arch/Timer.h"
2022-11-19 17:59:49 +01:00
#include "boot/Init.h"
#include "memory/Heap.h"
2022-11-19 17:59:49 +01:00
#include "memory/MemoryManager.h"
#include "video/TextConsole.h"
2022-11-30 12:42:11 +01:00
#include <Units.h>
2022-10-16 15:31:58 +02:00
Result<void> init()
2022-09-05 16:13:51 +02:00
{
2022-11-13 12:20:53 +01:00
Serial::println("Hello, world!");
2022-11-13 11:25:15 +01:00
2022-11-19 12:30:36 +01:00
Serial::printf("Current platform: %s\n", CPU::platform_string());
Serial::println(TRY(CPU::identify()));
2022-11-13 14:29:15 +01:00
const u64 address = 0xfffffffff8000000;
Serial::printf("Mapping address 0x%lx\n", address);
TRY(MemoryManager::alloc_at(address, 1, MMU::ReadWrite));
2022-11-19 17:52:24 +01:00
TRY(MMU::remap(address, MMU::ReadWrite | MMU::User));
2022-11-13 16:31:32 +01:00
int flags = TRY(MMU::get_flags(address));
2022-11-15 19:10:32 +01:00
if (flags & MMU::ReadWrite) Serial::println("Mapping is now writable");
if (flags & MMU::User) Serial::println("Mapping is now user accessible");
u64 old = TRY(MMU::unmap(address));
Serial::printf("Unmapped page, was pointing to %#lx\n", old);
TextConsole::set_foreground(0xff000055);
TextConsole::set_background(0xff88ff00);
2022-11-30 13:29:28 +01:00
TextConsole::printf("Hello from Moon for the %s architecture!\n", CPU::platform_string());
2022-11-13 16:31:32 +01:00
2022-11-19 20:01:01 +01:00
Timer::init();
2022-11-16 17:37:18 +01:00
2022-11-30 13:29:28 +01:00
kinfoln("Hello, world!");
kwarnln("THIS IS A WARNING");
kerrorln("ERROR: Please do something.");
2022-11-19 20:01:01 +01:00
CPU::platform_finish_init();
CPU::enable_interrupts();
usize start = 0;
int* mem = TRY(make<int>());
*(volatile int*)mem = 6;
Serial::printf("Read %d from memory\n", *mem);
TRY(destroy(mem));
2022-11-30 12:42:11 +01:00
char buffer[64];
to_dynamic_unit(MemoryManager::free(), buffer, sizeof(buffer));
2022-11-30 13:29:28 +01:00
kinfoln("Free memory: %s", buffer);
2022-11-30 12:42:11 +01:00
to_dynamic_unit(MemoryManager::used(), buffer, sizeof(buffer));
2022-11-30 13:29:28 +01:00
kinfoln("Used memory: %s", buffer);
2022-11-30 12:42:11 +01:00
to_dynamic_unit(MemoryManager::reserved(), buffer, sizeof(buffer));
2022-11-30 13:29:28 +01:00
kinfoln("Reserved memory: %s", buffer);
2022-11-30 12:42:11 +01:00
2022-11-19 20:01:01 +01:00
while (1)
{
2022-11-30 13:29:28 +01:00
while ((Timer::ticks_ms() - start) < 100) { CPU::wait_for_interrupt(); }
2022-11-19 20:01:01 +01:00
start = Timer::ticks_ms();
2022-11-30 13:29:28 +01:00
kdbgln("%8zu milliseconds have passed!", start);
2022-11-19 20:01:01 +01:00
}
return {};
}
extern "C" [[noreturn]] void _start()
{
Init::check_magic();
Init::early_init();
auto rc = init();
rc.release_value();
2022-11-15 19:10:32 +01:00
CPU::efficient_halt();
2022-09-05 16:13:51 +02:00
}