From 30c103a6b3f5d4242535e8262567eda425cce789 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Mon, 29 Nov 2021 14:59:34 +0100 Subject: [PATCH] feat(grub): add multi-boot support and long mode swap Signed-off-by: Julien CLEMENT --- .cargo/config.toml | 3 - .gitignore | 4 + Cargo.lock | 9 -- Cargo.toml | 4 +- Makefile | 39 +++++++++ grub/grub.cfg | 4 + src/boot.asm | 190 ++++++++++++++++++++++++++++++++++++++++ src/{main.rs => lib.rs} | 2 +- src/linker.ld | 16 ++++ src/multiboot.asm | 15 ++++ tools/create-iso.sh | 8 ++ 11 files changed, 279 insertions(+), 15 deletions(-) create mode 100644 Makefile create mode 100644 grub/grub.cfg create mode 100644 src/boot.asm rename src/{main.rs => lib.rs} (92%) create mode 100644 src/linker.ld create mode 100644 src/multiboot.asm create mode 100755 tools/create-iso.sh diff --git a/.cargo/config.toml b/.cargo/config.toml index c24a154..7137ff2 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -4,6 +4,3 @@ target = "x86_64-julios.json" [unstable] build-std-features = ["compiler-builtins-mem"] build-std = ["core", "compiler_builtins"] - -[target.'cfg(target_os = "none")'] -runner = "bootimage runner" diff --git a/.gitignore b/.gitignore index ea8c4bf..7b230e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ /target +iso +*.iso +*.o +julios diff --git a/Cargo.lock b/Cargo.lock index b6a65b7..10a21f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bootloader" -version = "0.9.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7c452074efc3c0bfb241fb7bc87df04741c7c85e926f6a07c05f8fbd6008240" - [[package]] name = "julios" version = "0.1.0" -dependencies = [ - "bootloader", -] diff --git a/Cargo.toml b/Cargo.toml index a889f07..cd4a4ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -bootloader = "0.9.8" +[lib] +crate-type = ["staticlib"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f75b385 --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +KERNEL = julios +ISO = julios.iso +INSTALL_ROOT = iso +ABS_INSTALL = $(abspath $(INSTALL_ROOT)) + +LINKER_SCRIPT = src/linker.ld +BOOT_OBJS = src/multiboot.o src/boot.o +LIB_JULIOS = target/x86_64-julios/debug/libjulios.a + +GRUB_CFG = grub/grub.cfg + +all: $(ISO) + +$(ISO): install + ./tools/create-iso.sh $@ $(INSTALL_ROOT) + +install: $(KERNEL) $(GRUB_CFG) + mkdir -p $(ABS_INSTALL) + mkdir -p $(ABS_INSTALL)/boot/grub + cp $(KERNEL) $(ABS_INSTALL)/boot + cp grub/grub.cfg $(ABS_INSTALL)/boot/grub + +$(KERNEL): $(LIB_JULIOS) $(LINKER_SCRIPT) $(BOOT_OBJS) + ld -n -T $(LINKER_SCRIPT) -o $(KERNEL) $(BOOT_OBJS) $(LIB_JULIOS) + +$(LIB_JULIOS): + cargo build + + +%.o: %.asm + nasm -f elf64 $^ -o $@ + +clean: + $(RM) $(BOOT_OBJS) + $(RM) $(KERNEL) + $(RM) julios.iso + $(RM) -r iso + +.PHONY: $(INSTALL_ROOT) install clean all diff --git a/grub/grub.cfg b/grub/grub.cfg new file mode 100644 index 0000000..77cb9ba --- /dev/null +++ b/grub/grub.cfg @@ -0,0 +1,4 @@ +menuentry "julios" { + multiboot2 /boot/julios + boot +} diff --git a/src/boot.asm b/src/boot.asm new file mode 100644 index 0000000..e2b1679 --- /dev/null +++ b/src/boot.asm @@ -0,0 +1,190 @@ +section .rodata +gdt64: + dq 0 +.code: equ $ - gdt64 + dq (1<<43) | (1<<44) | (1<<47) | (1<<53) + +.pointer: + dw $ - gdt64 - 1 + dq gdt64 + + +global _start +section .text +bits 32 + +_start: + mov esp, stack_top + + call check_multiboot + call check_cpuid + call check_long_mode + + call set_up_page_tables + call enable_paging + + lgdt [gdt64.pointer] + jmp gdt64.code:long_mode_start + +set_up_page_tables: + ; map first P4 entry to P3 table + mov eax, p3_table + or eax, 0b11 ; present + writable + mov [p4_table], eax + + ; map first P3 entry to P2 table + mov eax, p2_table + or eax, 0b11 ; present + writable + mov [p3_table], eax + + ; map each P2 entry to a huge 2MiB page + mov ecx, 0 ; counter variable + +.map_p2_table: + ; map ecx-th P2 entry to a huge page that starts at address 2MiB*ecx + mov eax, 0x200000 ; 2MiB + mul ecx ; start address of ecx-th page + or eax, 0b10000011 ; present + writable + huge + mov [p2_table + ecx * 8], eax ; map ecx-th entry + + inc ecx ; increase counter + cmp ecx, 512 ; if counter == 512, the whole P2 table is mapped + jne .map_p2_table ; else map the next entry + + ret + + +enable_paging: + ; load P4 to cr3 register (cpu uses this to access the P4 table) + mov eax, p4_table + mov cr3, eax + + ; enable PAE-flag in cr4 (Physical Address Extension) + mov eax, cr4 + or eax, 1 << 5 + mov cr4, eax + + ; set the long mode bit in the EFER MSR (model specific register) + mov ecx, 0xC0000080 + rdmsr + or eax, 1 << 8 + wrmsr + + ; enable paging in the cr0 register + mov eax, cr0 + or eax, 1 << 31 + mov cr0, eax + + ret + + + +; Prints `ERR: ` and the given error code to screen and hangs. +; parameter: error code (in ascii) in al +error: + mov dword [0xb8000], 0x4f524f45 + mov dword [0xb8004], 0x4f3a4f52 + mov dword [0xb8008], 0x4f204f20 + mov byte [0xb800a], al + hlt + + +check_multiboot: + cmp eax, 0x36d76289 + jne .no_multiboot + ret +.no_multiboot: + mov al, "0" + jmp error + + +check_cpuid: + ; Check if CPUID is supported by attempting to flip the ID bit (bit 21) + ; in the FLAGS register. If we can flip it, CPUID is available. + + ; Copy FLAGS in to EAX via stack + pushfd + pop eax + + ; Copy to ECX as well for comparing later on + mov ecx, eax + + ; Flip the ID bit + xor eax, 1 << 21 + + ; Copy EAX to FLAGS via the stack + push eax + popfd + + ; Copy FLAGS back to EAX (with the flipped bit if CPUID is supported) + pushfd + pop eax + + ; Restore FLAGS from the old version stored in ECX (i.e. flipping the + ; ID bit back if it was ever flipped). + push ecx + popfd + + ; Compare EAX and ECX. If they are equal then that means the bit + ; wasn't flipped, and CPUID isn't supported. + cmp eax, ecx + je .no_cpuid + ret +.no_cpuid: + mov al, "1" + jmp error + + + +check_long_mode: + ; test if extended processor info in available + mov eax, 0x80000000 ; implicit argument for cpuid + cpuid ; get highest supported argument + cmp eax, 0x80000001 ; it needs to be at least 0x80000001 + jb .no_long_mode ; if it's less, the CPU is too old for long mode + + ; use extended info to test if long mode is available + mov eax, 0x80000001 ; argument for extended processor info + cpuid ; returns various feature bits in ecx and edx + test edx, 1 << 29 ; test if the LM-bit is set in the D-register + jz .no_long_mode ; If it's not set, there is no long mode + ret +.no_long_mode: + mov al, "2" + jmp error + + +section .bss +align 4096 +p4_table: + resb 4096 +p3_table: + resb 4096 +p2_table: + resb 4096 + +stack_bottom: + resb 64 +stack_top: + + +global long_mode_start +section .text +bits 64 + +long_mode_start: + ; load 0 into all data segment registers + mov ax, 0 + mov ss, ax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + extern julios_main + call julios_main + + mov rax, 0x2f592f412f4b2f4f + mov qword [0xb8000], rax +.loop: + jmp .loop diff --git a/src/main.rs b/src/lib.rs similarity index 92% rename from src/main.rs rename to src/lib.rs index ef6fd42..4a4742d 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! static HELLO: &[u8] = b"Welcome to the JuliOS"; #[no_mangle] -pub extern "C" fn _start() -> ! +pub extern "C" fn julios_main() -> ! { let vga_buffer: *mut u8 = 0xb8000 as *mut u8; diff --git a/src/linker.ld b/src/linker.ld new file mode 100644 index 0000000..deea04f --- /dev/null +++ b/src/linker.ld @@ -0,0 +1,16 @@ +ENTRY(_start) + +SECTIONS { + . = 1M; + + .boot : + { + /* ensure that the multiboot header is at the beginning */ + *(.multiboot_header) + } + + .text : + { + *(.text) + } +} diff --git a/src/multiboot.asm b/src/multiboot.asm new file mode 100644 index 0000000..9a9289c --- /dev/null +++ b/src/multiboot.asm @@ -0,0 +1,15 @@ +section .multiboot_header +header_start: + dd 0xe85250d6 ; magic number (multiboot 2) + dd 0 ; architecture 0 (protected mode i386) + dd header_end - header_start ; header length + ; checksum + dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start)) + + ; insert optional multiboot tags here + + ; required end tag + dw 0 ; type + dw 0 ; flags + dd 8 ; size +header_end: diff --git a/tools/create-iso.sh b/tools/create-iso.sh new file mode 100755 index 0000000..99131a3 --- /dev/null +++ b/tools/create-iso.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +iso_filename=$1 +base_dir=$2 + +unset MFLAGS MAKEFLAGS + +grub-mkrescue -o $iso_filename $base_dir