mirror of
https://github.com/taigrr/gopher-os
synced 2025-01-18 04:43:13 -08:00
The switch to 64-bit mode allows us to use 48-bit addressing and to relocate the kernel to virtual address 0xffff800000000000 + 1M. The actual kernel is loaded by the bootloader at physical address 1M. The rt0 code has been split in two parts. The 32-bit part provides the entrypoint that the bootloader jumps to after loading the kernel. Its purpose is to make sure that: - the kernel was booted by a multiboot-compliant bootloader - the multiboot info structures are copied to a reserved memory block where they can be accessed after enabling paging - the CPU meets the minimum requirements for the kernel (CPUID, SSE, support for long-mode) Since paging is not enabled when the 32-bit code runs, it needs to translate all memory addresses it accesses to physical memory addresses by subtracting PAGE_OFFSET. The 32-bit rt0 code will set up a page table that identity-maps region: 0 to 8M and region: PAGE_OFFSET to PAGE_OFFSET+8M. This ensures that when paging gets enabled, we will still be able to access the kernel using both physical and virtual memory addresses. After enabling paging, the 32-bit rt0 will jump to a small 64-bit trampoline function that updates the stack pointer to use the proper virtual address and jumps to the virtual address of the 64-bit entry point. The 64-bit entrypoint sets up the minimal g0 structure required by the go function prologue for stack checks and sets up the FS register to point to it. The principle is the same as with 32-bit code (a segment register has the address of a pointer to the active g) with the difference that in 64-bit mode, the FS register is used instead of GS and that in order to set its value we need to write to a MSR.
34 lines
957 B
Go
34 lines
957 B
Go
package kernel
|
|
|
|
import (
|
|
_ "unsafe" // required for go:linkname
|
|
|
|
"github.com/achilleasa/gopher-os/kernel/hal"
|
|
"github.com/achilleasa/gopher-os/kernel/hal/multiboot"
|
|
"github.com/achilleasa/gopher-os/kernel/kfmt/early"
|
|
)
|
|
|
|
// Kmain is the only Go symbol that is visible (exported) from the rt0 initialization
|
|
// code. This function is invoked by the rt0 assembly code after setting up the GDT
|
|
// and setting up a a minimal g0 struct that allows Go code using the 4K stack
|
|
// allocated by the assembly code.
|
|
//
|
|
// The rt0 code passes the address of the multiboot info payload provided by the
|
|
// bootloader.
|
|
//
|
|
// Kmain is not expected to return. If it does, the rt0 code will halt the CPU.
|
|
//
|
|
//go:noinline
|
|
func Kmain(multibootInfoPtr uintptr) {
|
|
multiboot.SetInfoPtr(multibootInfoPtr)
|
|
|
|
// Initialize and clear the terminal
|
|
hal.InitTerminal()
|
|
hal.ActiveTerminal.Clear()
|
|
early.Printf("Starting gopher-os\n")
|
|
|
|
// Prevent Kmain from returning
|
|
for {
|
|
}
|
|
}
|