garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/x86_64/boot.rs

raw

//! The first instructions on a PC, from the loader's jump to Rust.
//!
//! Two loaders can enter, both at `pvh_start` in 32-bit protected mode with
//! paging off, with the physical address of what they hand over in EBX
//! (`loader` reads it):
//!
//! - PVH (the Xen boot protocol QEMU's `-kernel` also speaks), by the ELF
//!   note below.
//! - Multiboot2 (GRUB's, on a real PC's UEFI), by the header below, as the
//!   ELF's entry; it says so with its magic in EAX, which goes to Rust beside
//!   EBX. GRUB has left UEFI's boot services by then.
//!
//! This builds the page tables, turns on long mode and jumps to the kernel's
//! top half: 32-bit code cannot reach a 64-bit address, so it runs at its
//! physical one, and so do its tables.
//!
//! The tables map the first 4 GiB of physical memory three times, in 2 MiB
//! pages: at 0 (the switch runs there), at `mmu::KERNEL_OFFSET` (all of
//! memory, as the kernel reaches it) and its first GiB at
//! `mmu::IMAGE_OFFSET` (the kernel's code and data). The fourth GiB, where a
//! PC keeps its devices' registers (the APICs, PCI windows), is uncached.
//! What lies above 4 GiB, RAM and devices, `mmu::map_above_4gib` maps from
//! the loader's map once in Rust.

core::arch::global_asm!(
    // The Multiboot2 header (the specification's section 3.1), in the
    // file's first 32 KiB: the information asked for (the command line, the
    // memory map, the framebuffer, ACPI's old and new root pointers), all
    // optional, and a framebuffer: whichever mode UEFI's GOP has on.
    ".pushsection .multiboot2, \"a\"",
    ".balign 8",
    "multiboot2_header:",
    ".long 0xe85250d6", // the magic
    ".long 0",          // i386: entered in 32-bit protected mode
    ".long multiboot2_end - multiboot2_header",
    ".long 0x100000000 - (0xe85250d6 + (multiboot2_end - multiboot2_header))",
    ".balign 8",
    ".short 1, 1", // the information request, optional
    ".long 8 + 5 * 4",
    ".long 1, 6, 8, 14, 15",
    ".balign 8",
    ".short 5, 1", // the framebuffer, optional
    ".long 20",
    ".long 0, 0, 0", // the mode GOP has on: any width, height and depth
    ".balign 8",
    ".short 0, 0", // the end
    ".long 8",
    "multiboot2_end:",
    ".popsection",
    //
    // The PVH entry the loader reads: XEN_ELFNOTE_PHYS32_ENTRY.
    ".pushsection .note.Xen, \"a\", @note",
    ".balign 4",
    ".long 4",  // the name's length
    ".long 4",  // the entry's length
    ".long 18", // XEN_ELFNOTE_PHYS32_ENTRY
    ".asciz \"Xen\"",
    ".balign 4",
    ".long pvh_start",
    ".balign 4",
    ".popsection",
    //
    ".pushsection .data.boot32, \"aw\"",
    ".balign 4096",
    "boot_pml4: .fill 512, 8, 0",
    "boot_pdpt_low: .fill 512, 8, 0",
    "boot_pdpt_high: .fill 512, 8, 0",
    "boot_pd: .fill 4 * 512, 8, 0",
    ".balign 16",
    "boot_gdt:",
    ".quad 0",
    ".quad 0x00af9a000000ffff", // 0x08: 64-bit code
    ".quad 0x00cf92000000ffff", // 0x10: data
    "boot_gdt_end:",
    "boot_gdtr:",
    ".word boot_gdt_end - boot_gdt - 1",
    ".quad boot_gdt",
    // PVH gives no stack; the far return to 64-bit code needs one.
    ".balign 16",
    ".fill 64, 1, 0",
    "boot_stack:",
    ".popsection",
    //
    ".pushsection .text.boot32, \"ax\"",
    ".code32",
    ".global pvh_start",
    "pvh_start:",
    "cli",
    "lea esp, [boot_stack]",
    "mov esi, ebx", // what the loader hands over, kept for Rust
    "mov ebp, eax", // and Multiboot2's magic, if it is the one
    // 2048 entries of 2 MiB: present, writable, large; the fourth GiB
    // uncached (PWT | PCD).
    "lea edi, [boot_pd]",
    "xor ecx, ecx",
    "2:",
    "mov eax, ecx",
    "shl eax, 21",
    "or eax, 0x83",
    "cmp ecx, 1536",
    "jb 3f",
    "or eax, 0x18",
    "3:",
    "mov [edi + ecx * 8], eax",
    "mov dword ptr [edi + ecx * 8 + 4], 0",
    "inc ecx",
    "cmp ecx, 2048",
    "jb 2b",
    // The low directory pointers: the four directories, one a GiB.
    "lea eax, [boot_pd + 3]",
    "mov [boot_pdpt_low], eax",
    "add eax, 4096",
    "mov [boot_pdpt_low + 8], eax",
    "add eax, 4096",
    "mov [boot_pdpt_low + 16], eax",
    "add eax, 4096",
    "mov [boot_pdpt_low + 24], eax",
    // The image's: its 2 GiB start at entry 510, the first GiB there.
    "lea eax, [boot_pd + 3]",
    "mov [boot_pdpt_high + 510 * 8], eax",
    // At 0, at KERNEL_OFFSET (entry 256) and at IMAGE_OFFSET (entry 511).
    "lea eax, [boot_pdpt_low + 3]",
    "mov [boot_pml4], eax",
    "mov [boot_pml4 + 256 * 8], eax",
    "lea eax, [boot_pdpt_high + 3]",
    "mov [boot_pml4 + 511 * 8], eax",
    // PAE, the tables, long mode with no-execute, paging.
    "mov eax, cr4",
    "or eax, 1 << 5",
    "mov cr4, eax",
    "lea eax, [boot_pml4]",
    "mov cr3, eax",
    "mov ecx, 0xc0000080",
    "rdmsr",
    "or eax, (1 << 8) | (1 << 11)",
    "wrmsr",
    "mov eax, cr0",
    "or eax, (1 << 31) | 1",
    "mov cr0, eax",
    "lgdt [boot_gdtr]",
    "push 0x08",
    "lea eax, [4f]",
    "push eax",
    "retf",
    ".code64",
    "4:",
    "mov ax, 0x10",
    "mov ds, ax",
    "mov es, ax",
    "mov ss, ax",
    "xor eax, eax",
    "mov fs, ax",
    "mov gs, ax",
    // Up to the image's own addresses, with its stack.
    "movabs rsp, offset __stack_top",
    "mov edi, esi",
    "mov esi, ebp",
    "movabs rax, offset {kmain}",
    "call rax",
    "5:",
    "hlt",
    "jmp 5b",
    ".popsection",
    kmain = sym crate::kmain,
);