From 7ec5b2a7423762da4cc8d318fb74116815b10b7f Mon Sep 17 00:00:00 2001 From: TunaCici <36923239+TunaCici@users.noreply.github.com> Date: Mon, 15 Apr 2024 22:06:38 +0300 Subject: [PATCH] westeros: Remove libkern dependency from early boot stage. Dangerous... Signed-off-by: TunaCici <36923239+TunaCici@users.noreply.github.com> --- Kernel/Main.c | 2 +- Kernel/Start.c | 152 ++++++++++++++++++++++++++++--------------------- 2 files changed, 89 insertions(+), 65 deletions(-) diff --git a/Kernel/Main.c b/Kernel/Main.c index 93e692b..d222808 100644 --- a/Kernel/Main.c +++ b/Kernel/Main.c @@ -52,7 +52,7 @@ void kmain(void) } /* X. Setup vector tables */ - MSR("vbar_el1", vector_table); + MSR("VBAR_EL1", vector_table); isb(); /* 1. Init BootMem */ diff --git a/Kernel/Start.c b/Kernel/Start.c index 7f8db09..13cf42e 100644 --- a/Kernel/Start.c +++ b/Kernel/Start.c @@ -12,70 +12,116 @@ #include "MemoryLayout.h" -#include "LibKern/DeviceTree.h" -#include "LibKern/Time.h" -#include "LibKern/Console.h" - extern void kmain(void); +void _utoa(uint64_t uval, char *buff, uint8_t base) { + const char digits[] = "0123456789ABCDEF"; + int i = 0; + + /* Convert to string. Digits are in reverse order */ + do { + buff[i++] = digits[uval % base]; + } while((uval /= base) != 0); + buff[i--] = '\0'; + + /* Reverse the string */ + for (int j = 0; j < i; j++, i--) { + char tmp = buff[j]; + + buff[j] = buff[i]; + buff[i] = tmp; + } +} + +void _uart_putc(const int c) +{ + volatile uint8_t *uart0 = (uint8_t*) PL011_BASE; + *uart0 = c; +} + + +void _puts(const char *s) +{ + while (*s != '\0') { + _uart_putc(*s); + s++; + } +} + void _halt(const char *s) { - klog("Halting due to: %s", s); + _puts("Halting due to: "); + _puts(s); + wfi(); } void start(void) { - volatile uint32_t arch; - volatile uint32_t val32; - volatile uint64_t val64; + volatile uint32_t arch = 0; + volatile uint32_t val32 = 0; + volatile uint64_t val64 = 0; + + char buff[64] = {0}; /* Hard-coded device/board info */ /* TODO: Replace this with a DTB parser */ const char *_cpuModel = "Cortex A-72"; const uint32_t _coreCount = 2u; - klog("WesterOS early boot stage\n"); - klog("Running sanity checks...\n"); + _puts("WesterOS early boot stage\n"); + _puts("Running sanity checks\n"); /* TODO: Any better way to early print? */ - klog("WARN: Raw printing directly to PL011 @ 0x%x\n", PL011_BASE); + _puts("WARN: Raw printing directly to PL011 @ 0x"); + _utoa(PL011_BASE, buff, 16); + _puts(buff); + _puts("\n"); /* -------- CPU -------- */ - klog("Checking CPU\n"); + _puts("Checking CPU\n"); MRS("MIDR_EL1", val32); arch = (val32 & 0xFF000000) >> 24; if (arch == 0x41) { - klog("---- Implementer: ARM\n"); + _puts("---- Implementer: ARM\n"); } else { _halt("---- Unknown Implementer\n"); } - klog("---- Model: %s\n", _cpuModel); - klog("---- SMP: %u\n", _coreCount); + _puts("---- Model: "); + _puts(_cpuModel); + _puts("\n"); + + _puts("---- SMP: "); + _utoa(_coreCount, buff, 10); + _puts(buff); + _puts("\n"); MRS("CNTFRQ_EL0", val64); val64 = val64 / 1000000; /* Hz to MHz */ - klog("---- Running @ %lu MHz\n", val64); + _puts("---- Running @ "); + _utoa(val64, buff, 10); + _puts(buff); + _puts(" MHz\n"); - klog("---- Current exception level: "); + _puts("---- Current exception level: "); MRS("CurrentEL", val32); val32 = (val32 & 0x0C) >> 2; switch(val32) { case 0: - kprintf("EL0 (User mode)\n"); + _puts("EL0 (User mode)\n"); break; case 1: - kprintf("EL1 (Kernel mode)\n"); + _puts("EL1 (Kernel mode)\n"); break; case 2: - kprintf("EL2 (Hypervisor mode)\n"); + _puts("EL2 (Hypervisor mode)\n"); break; case 3: - kprintf("EL3 (Secure Monitor mode)\n"); + _puts("EL3 (Secure Monitor mode)\n"); break; default: _halt("Unknown exception level\n"); @@ -89,71 +135,49 @@ void start(void) serror_enable(); isb(); - klog("---- IRQ: "); + _puts("---- IRQ: "); MRS("DAIF", val32); if (val32 & DAIF_IRQ) { - kprintf("Masked\n"); + _puts("Masked\n"); } else { - kprintf("Unmasked\n"); + _puts("Unmasked\n"); } - klog("---- FIQ: "); + _puts("---- FIQ: "); if (val32 & DAIF_FIQ) { - kprintf("Masked\n"); + _puts("Masked\n"); } else { - kprintf("Unmasked\n"); + _puts("Unmasked\n"); } - klog("---- Debug: "); + _puts("---- Debug: "); if (val32 & DAIF_DEBUG) { - kprintf("Masked\n"); + _puts("Masked\n"); } else { - kprintf("Unmasked\n"); + _puts("Unmasked\n"); } - klog("---- SError: "); + _puts("---- SError: "); if (val32 & DAIF_SERROR) { - kprintf("Masked\n"); + _puts("Masked\n"); } else { - kprintf("Unmasked\n"); + _puts("Unmasked\n"); } - /* -------- Machine Layout -------- */ - klog("QEMU ARM Virt Machine memory layout:\n"); - - klog("---- BootROM Code: 0x%x - 0x%x (reserved)\n", - BOOTROM_START, BOOTROM_END - ); - - klog ("---- GICv2: 0x%x - 0x%x (controller)\n", - GIC_BASE, GIC_END - ); - - klog ("---- PL011 UART: 0x%x - 0x%x (mmio)\n", - PL011_BASE, PL011_END - ); - - klog ("---- PL031 RTC: 0x%x - 0x%x (mmio)\n", - PL031_BASE, PL031_END - ); - - klog ("---- PL061 GPIO: 0x%x - 0x%x (mmio)\n", - PL061_BASE, PL061_END - ); - - klog ("---- QEMU fw_cfg: 0x%x - 0x%x (qemu)\n", - FW_CFG_BASE, FW_CFG_END - ); - - klog ("---- Device Tree Blob: 0x%x - 0x%x (reserved)\n", - DTB_START, DTB_END - ); + /*TODO: Initialize page tables here */ + _puts("+----------------------------------------------------+\n"); + _puts("| __ _______ __, ____________ _ __ ___ __, |\n"); + _puts("| ( / /( / ( ( / ( / ( / ) / ()( |\n"); + _puts("| / / / /-- `. / /-- /--< / / `. |\n"); + _puts("| (_/_/ (/____/(___)_/ (/____// \\_(___/ (___) |\n"); + _puts("| |\n"); + _puts("| Everything is OK. Calling the kernel now... |\n"); + _puts("+----------------------------------------------------+\n"); - klog("Everything's OK. Calling the Kernel now...\n"); kmain(); }