Skip to content

Commit

Permalink
shim: New handover mechanism from Shim (a.k.a. Entry.S + Start.c) to …
Browse files Browse the repository at this point in the history
…kernel. WIP.

See the 'Bootstrapping' section from README.md.

Signed-off-by: TunaCici <[email protected]>
  • Loading branch information
TunaCici committed May 1, 2024
1 parent 9412847 commit 0ea16d6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 39 deletions.
14 changes: 7 additions & 7 deletions Kernel/Arch/ARM64/Entry.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Entry point for the WesterOS kernel for ARMv8
* Initialize the Stack pointer, clear the BSS and jump to Start.c
* Entry point for the WesterOS kernel for ARM64
* Initializes stack pointer, clears the BSS and jump to Start.c
*
* Originally from: sudharson14/xv6-OS-for-arm-v8
* Modified by: Tuna CICI
Expand All @@ -16,13 +16,13 @@ _start:
msr spsel, x0
isb

/* Init stack (`init_stktop` bytes) */
adrp x0, init_stktop
/* Init stack (`_shim_stack_top` bytes) */
adrp x0, _shim_stack_top
mov sp, x0

/* Clear the entry bss section, the svc stack, and kernel page table */
ldr x1, =edata_entry
ldr x2, =end_entry
/* Clear the shim bss section & the svc stack */
ldr x1, =_shim_bss_start
ldr x2, =_shim_end
mov x3, #0x00

/* Enable access to FPU & Advanced SIMD (NEON) */
Expand Down
54 changes: 37 additions & 17 deletions Kernel/Start.c → Kernel/Arch/ARM64/Start.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
/*
* Early boot stage. Sanity checks for hardware devices & "calls" kmain(...)
* Entry point for the WesterOS kernel for ARM64 cntd.
* Initialize CPU cores, setup the iniital page tables & enable MMU
* Finally, pass information & control to the kernel
*
* Author: Tuna CICI
*/

#include <stdint.h>
#include <stdarg.h>

#include "ARM64/Machine.h"
#include "ARM64/RegisterSet.h"
#include "ARM64/Memory.h"

#include "Boot.h"
#include "MemoryLayout.h"

/* in Main.c */
extern void kmain(uint64_t l0_pgtbl[], uint64_t l1_pgtbl[], uint64_t vector_tbl,
void* dtb, uint32_t dtb_size);
extern void kmain(boot_sysinfo, uint64_t);

/* in Kernel/kernel.ld */
extern uint64_t kstart;
extern uint64_t kend;
extern uint64_t kvma_base;
uint64_t kernel_start = (uint64_t) (&kstart);
extern uint64_t _k_phy_base;
extern uint64_t _k_vir_base;
extern uint64_t _k_size;
extern uint64_t _k_stack_top;
uint64_t k_phy_base = (uint64_t) (&_k_phy_base);
uint64_t k_vir_base = (uint64_t) (&_k_vir_base);
uint64_t k_size = (uint64_t) (&_k_size);
uint64_t k_stack_top = (uint64_t) (&_k_stack_top);

/* in Kernel/Arch/ARM64/Vector.S */
extern uint64_t vector_table;
uint64_t vector_tbl = (uint64_t) (&vector_table);
extern uint64_t _vector_table;
uint64_t vector_table = (uint64_t) (&_vector_table);

/* Higher half - TTBR1_EL1 */
uint64_t k_l0_pgtbl[ENTRY_SIZE] __attribute__((aligned(GRANULE_SIZE)));
Expand Down Expand Up @@ -103,7 +108,7 @@ void _init_kernel_pgtbl(void)
blk = BLK_SET_AF(blk, 1);
blk = BLK_SET_NG(blk, 0);

blk = BLK_SET_L1_OA(blk, kernel_start);
blk = BLK_SET_L1_OA(blk, k_phy_base);

blk = BLK_SET_HINT(blk, 0);
blk = BLK_SET_PXN(blk, 0);
Expand Down Expand Up @@ -174,7 +179,7 @@ void _init_user_pgtbl(void)
blk = BLK_SET_AIDX(blk, DEVICE_nGnRE_IDX);
blk = BLK_SET_NS(blk, 0);
blk = BLK_SET_AP(blk, AP_PRIV_RW);
blk = BLK_SET_SH(blk, SH_OUTER);
blk = BLK_SET_SH(blk, SH_NON);
blk = BLK_SET_AF(blk, 1);
blk = BLK_SET_NG(blk, 0);

Expand Down Expand Up @@ -280,11 +285,13 @@ void _init_sctlr(void)

void start(void)
{
volatile uint32_t arch = 0;
volatile uint32_t val32 = 0;
volatile uint64_t val64 = 0;
uint32_t arch = 0;
uint32_t val32 = 0;
uint64_t val64 = 0;
char buff[64] = {0};

boot_sysinfo boot_params = {0};

/* Hard-coded device/board info */
/* TODO: Replace this with a DTB parser */
const char *_cpuModel = "Cortex A-72";
Expand Down Expand Up @@ -351,6 +358,16 @@ void start(void)
break;
}

/* Populate boot_sysinfo - will be passed to the kernel */
boot_params.k_phy_base = k_phy_base;
boot_params.k_vir_base = k_vir_base;
boot_params.k_size = k_size;

boot_params.vector_base = vector_table;

boot_params.dtb_base = DTB_START;
boot_params.dtb_size = DTB_SIZE;

/* Disable interrupts */
debug_disable();
irq_enable();
Expand Down Expand Up @@ -391,7 +408,7 @@ void start(void)
_puts("Unmasked\n");
}

MSR("VBAR_EL1", vector_tbl);
MSR("VBAR_EL1", vector_table);
isb();

_puts("Initializing Translation Control Register (TCR)\n");
Expand Down Expand Up @@ -423,5 +440,8 @@ void start(void)
_puts("| Everything is OK. Calling the kernel now... |\n");
_puts("+----------------------------------------------------+\n");

kmain(0 , 0, 0, 0, 0);
/* Jump the stack pointer the stack pointer */
asm volatile ("mov sp, %0" :: "r" (k_stack_top));

kmain(boot_params, 0);
}
4 changes: 2 additions & 2 deletions Kernel/Arch/ARM64/Vector.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

.text
.balign 2048
.global vector_table
.global _vector_table

.macro store_regs
stp x0, x1, [sp, #16 * 0]
Expand Down Expand Up @@ -63,7 +63,7 @@
ldp x0, x1, [sp, #16 * 0]
.endm

vector_table:
_vector_table:

/*
* 1. Current EL where SP -> SP_EL0 - TODO
Expand Down
20 changes: 9 additions & 11 deletions Kernel/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <stdint.h>

#include "ARM64/Machine.h"
#include "Memory/Virtual.h"

#include "Boot.h"
#include "MemoryLayout.h"

#include "LibKern/String.h"
Expand All @@ -20,8 +21,6 @@
#include "Memory/Physical.h"
#include "Memory/Virtual.h"

extern uint64_t kend; /* in Kernel/kernel.ld */

/*
* Kernel entry.
*
Expand All @@ -32,12 +31,11 @@ extern uint64_t kend; /* in Kernel/kernel.ld */
* dtb: Device Tree Blob base address
* dtb_size: Device Tree Blob size in bytes
*/
void kmain(uint64_t l0_pgtbl[], uint64_t l1_pgtbl[], uint64_t vector_tbl,
void* dtb, uint32_t dtb_size)
void kmain(boot_sysinfo boot_params)
{
const void *kernel_end = &kend;

volatile void *important_ahh = &boot_params;
uint64_t mem_start = 0x0;
volatile void *important_ahh2 = &mem_start;
uint64_t mem_end = 0x0;

/* 0. Get HW Information */
Expand All @@ -62,8 +60,8 @@ void kmain(uint64_t l0_pgtbl[], uint64_t l1_pgtbl[], uint64_t vector_tbl,
}

/* X. Setup vector tables */
if (vector_tbl) {
MSR("VBAR_EL1", vector_tbl);
if (boot_params.vector_base) {
MSR("VBAR_EL1", boot_params.vector_base);
isb();
} else {
klog("[kmain] NULL vector table is given!\n");
Expand All @@ -72,7 +70,7 @@ void kmain(uint64_t l0_pgtbl[], uint64_t l1_pgtbl[], uint64_t vector_tbl,
/* 1. Init BootMem */
klog("[kmain] Initializing early memory manager...\n");

uint64_t pageCount = bootmem_init(kernel_end);
uint64_t pageCount = bootmem_init((const void*) (boot_params.k_phy_base + boot_params.k_size));

klog("[kmain] Pages available: %lu (%lu KiB) in bootmem\n",
pageCount, (pageCount * PAGE_SIZE) / 1024
Expand All @@ -82,7 +80,7 @@ void kmain(uint64_t l0_pgtbl[], uint64_t l1_pgtbl[], uint64_t vector_tbl,
klog("[kmain] Initializing physical memory manager...\n");

uint64_t blockCount = init_allocator(
(const void *) kernel_end + pageCount * PAGE_SIZE,
(const void *) boot_params.k_phy_base + boot_params.k_size + pageCount * PAGE_SIZE,
(const void *) mem_end
);

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ GTEST_CXXFLAGS = ${INCLUDES} -g -Wall -Wextra -std=c++20 \
# Without it I'd get errors like: undefined symbol "_kernel_pgtbl".
# The files like Kernel/Main.c uses symbols defined in Kernel/Kernel.ld
# When compiling tests ALL objects are are given to the clang/ld.
# Since Start.C and Main.c will [probably] never gonna get unit tested
# Since Start.c and Main.c will [probably] never gonna get unit tested
# We can just ignore "unreslved symbols" with -dead-strip.
# > maybe in the future I can find a better way. who knows...

# Project source files
SRCS = \
Kernel/Arch/ARM64/Start.c \
Kernel/Arch/ARM64/Exception.c \
Kernel/Start.c \
Kernel/Main.c \
Kernel/Library/LibKern/DeviceTree.c \
Kernel/Library/LibKern/Console.c \
Expand Down

0 comments on commit 0ea16d6

Please sign in to comment.