Luna/kernel/src/main.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

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