From 6de42c3dba144fef39bcc0133793e6f80fd10cff Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 23 May 2024 21:39:00 +0300 Subject: [PATCH] asd --- .cargo/config.toml | 7 ++ .gitignore | 2 + Cargo.lock | 16 ++++ Cargo.toml | 7 ++ boot/boot.s | 182 +++++++++++++++++++++++++++++++++++++++++++ boot/grub.cfg | 7 ++ boot/long_mode.s | 22 ++++++ boot/multiboot.s | 15 ++++ build.sh | 105 +++++++++++++++++++++++++ kernel/.gitignore | 1 + kernel/Cargo.toml | 14 ++++ kernel/src/lib.rs | 20 +++++ kernel/src/logger.rs | 19 +++++ linker.ld | 27 +++++++ rust_toolchain | 1 + 15 files changed, 445 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 boot/boot.s create mode 100644 boot/grub.cfg create mode 100644 boot/long_mode.s create mode 100644 boot/multiboot.s create mode 100755 build.sh create mode 100644 kernel/.gitignore create mode 100644 kernel/Cargo.toml create mode 100644 kernel/src/lib.rs create mode 100644 kernel/src/logger.rs create mode 100644 linker.ld create mode 100644 rust_toolchain diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..2610a48 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,7 @@ +[unstable] +bindeps = true +build-std = ["core", "compiler_builtins"] +build-std-features = ["compiler-builtins-mem"] + +[build] +target = "x86_64-unknown-none" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f40686d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/boot/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d607f21 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "poppin-kernel" +version = "0.1.0" +dependencies = [ + "log", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..73f0eb7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = [ + "kernel" +] + +exclude = ["boot"] +resolver = "2" diff --git a/boot/boot.s b/boot/boot.s new file mode 100644 index 0000000..b41f2ef --- /dev/null +++ b/boot/boot.s @@ -0,0 +1,182 @@ +global _start +extern long_mode_start + +section .text +bits 32 +_start: + ; mov edx, error_messages.no_long_mode + ; mov ecx, error_messages.attrib + ; call _puts + +; .loop: +; jmp .loop + mov esp, stack_top + mov edi, ebx ; move Multiboot info pointer to edi + + call check_multiboot + call check_cpuid + call check_long_mode + + call set_up_page_tables + call enable_paging + + ; load the 64-bit GDT + lgdt [gdt64.pointer] + + jmp gdt64.code:long_mode_start + + ; print `OK` to screen + mov dword [0xb8000], 0x2f4b2f4f + 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 + +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 + + + + +section .bss +align 4096 +global mem_start +mem_start: + resb 4096 * 9 +mem_end: + +p4_table: + resb 4096 +p3_table: + resb 4096 +p2_table: + resb 4096 +stack_bottom: + resb 4096 * 4 +stack_top: + +section .data + + +gdt64: + dq 0 ; zero entry +.code: equ $ - gdt64 ; new + dq (1<<43) | (1<<44) | (1<<47) | (1<<53) ; code segment +.pointer: + dw $ - gdt64 - 1 + dq gdt64 + diff --git a/boot/grub.cfg b/boot/grub.cfg new file mode 100644 index 0000000..a396edd --- /dev/null +++ b/boot/grub.cfg @@ -0,0 +1,7 @@ +set timeout=0 +set default=0 + + +menuentry "poppin" { + multiboot2 /boot/poppin.bin +} diff --git a/boot/long_mode.s b/boot/long_mode.s new file mode 100644 index 0000000..7abde2c --- /dev/null +++ b/boot/long_mode.s @@ -0,0 +1,22 @@ +global long_mode_start +extern kmain + +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 + + ; call the rust main + extern kmain + call kmain + + ; print `OKAY` to screen + mov rax, 0x2f592f412f4b2f4f + mov qword [0xb8000], rax + hlt diff --git a/boot/multiboot.s b/boot/multiboot.s new file mode 100644 index 0000000..9a9289c --- /dev/null +++ b/boot/multiboot.s @@ -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/build.sh b/build.sh new file mode 100755 index 0000000..2044fb2 --- /dev/null +++ b/build.sh @@ -0,0 +1,105 @@ +BUILD_DIR="./target" +BIN="$BUILD_DIR/poppin.bin" +ISO="$BUILD_DIR/poppin.iso" + +set -e + +function cmd_exists() { + if ! command -v $1 &> /dev/null + then + echo "The command '$1' could not be found please install it" + exit 1 + fi +} + +function pre_build() { + cmd_exists cargo + cmd_exists xorriso + cmd_exists grub-file + cmd_exists ld + cmd_exists grub-mkrescue + cmd_exists qemu-system-x86_64 +} + +function build_kernel() { + echo "INFO: Building poppin-kernel" + pushd kernel > /dev/null + cargo build --release --target-dir ../${BUILD_DIR} + popd > /dev/null +} + + +function build_boot() { + echo "INFO: Building boot code" + nasm -felf64 -o $BUILD_DIR/long_mode.o boot/long_mode.s + nasm -felf64 -o $BUILD_DIR/boot.o boot/boot.s + nasm -felf64 -o $BUILD_DIR/multiboot.o boot/multiboot.s +} + + + +function link(){ + echo "INFO: Linking" + ld -melf_x86_64 -n \ + -T linker.ld \ + -o ${BIN} \ + ${BUILD_DIR}/multiboot.o \ + ${BUILD_DIR}/boot.o \ + ${BUILD_DIR}/long_mode.o \ + ${BUILD_DIR}/x86_64-unknown-none/release/libpoppin_kernel.a +} + +function check_mb() { + if grub-file --is-x86-multiboot2 $BIN; then + echo "INFO: Kernel conatins multiboot2" + else + echo "ERROR: Kernel doesnt contain multiboot2" + exit 1 + fi +} + +function make_iso() { + echo "INFO: Building ISO" + mkdir -p ${BUILD_DIR}/iso/boot/grub + cp ${BUILD_DIR}/poppin.bin ${BUILD_DIR}/iso/boot/poppin.bin + cp boot/grub.cfg ${BUILD_DIR}/iso/boot/grub/grub.cfg + grub-mkrescue -o ${ISO} ${BUILD_DIR}/iso > /dev/null 2>&1 +} + +function run_qemu() { + qemu-system-x86_64 -cdrom $ISO +} + +function print_help() { + echo "Usage: ./build.sh [command]" + echo "Commands:" + echo " run-qemu - Compiles and Runs the built iso with qemu" + echo " build - Compiles everything" +} + +function build() { + pre_build + build_kernel + build_boot + link + check_mb + make_iso +} + + +case $1 in + run-qemu) + build + run_qemu + ;; + build) + build + ;; + *) + if [ $# -gt 0 ]; then + echo "ERROR: Unknown option '$1'" + fi + print_help + exit 1 + ;; +esac diff --git a/kernel/.gitignore b/kernel/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/kernel/.gitignore @@ -0,0 +1 @@ +/target diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml new file mode 100644 index 0000000..e9a1263 --- /dev/null +++ b/kernel/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "poppin-kernel" +version = "0.1.0" +edition = "2021" +test=false + +[lib] +name="poppin_kernel" +test=false +crate-type=["staticlib"] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.21" diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs new file mode 100644 index 0000000..99afa80 --- /dev/null +++ b/kernel/src/lib.rs @@ -0,0 +1,20 @@ +#![no_std] +#![no_main] + + +mod logger; + +#[no_mangle] +extern "C" fn kmain() -> ! { + + loop { + } +} + + +#[panic_handler] +fn panic(_pi: &core::panic::PanicInfo) -> ! { + loop {} +} + + diff --git a/kernel/src/logger.rs b/kernel/src/logger.rs new file mode 100644 index 0000000..6200b74 --- /dev/null +++ b/kernel/src/logger.rs @@ -0,0 +1,19 @@ +use log::{Record, Level, Metadata}; + +struct SimpleLogger { + +} + +impl log::Log for SimpleLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= Level::Info + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + println!("{} - {}", record.level(), record.args()); + } + } + + fn flush(&self) {} +} diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..16c3d3d --- /dev/null +++ b/linker.ld @@ -0,0 +1,27 @@ +ENTRY(_start) + +SECTIONS { + . = 1M; + + .boot : + { + /* ensure that the multiboot header is at the beginning */ + KEEP(*(.multiboot_header)) + + } + + .text : + { + *(.text .text.*) + } + + .rodata : + { + *(.rodata .rodata.*) + } + + .data.rel.ro : + { + *(.data.rel.ro.local*) *(.data.rel.ro .data.rel.ro.*) + } +} diff --git a/rust_toolchain b/rust_toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/rust_toolchain @@ -0,0 +1 @@ +nightly