From 4574150d2b1edf59950479d0963f75a074c05153 Mon Sep 17 00:00:00 2001 From: fekhesk Date: Wed, 26 Oct 2022 01:50:22 -0700 Subject: [PATCH] memory --- Cargo.toml | 7 ++- Makefile | 14 +++--- arch/x86_64/boot.asm | 3 +- serial.log | 8 ++++ src/boot/mod.rs | 27 +++++++++++ src/boot/multiboot2/mod.rs | 41 ++++++++++++++++ src/lib.rs | 58 ++++++++++++++++++++++- src/security/mod.rs | 1 + src/security/stack_smashing_protection.rs | 8 ++++ 9 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 src/boot/mod.rs create mode 100644 src/boot/multiboot2/mod.rs create mode 100644 src/security/mod.rs create mode 100644 src/security/stack_smashing_protection.rs diff --git a/Cargo.toml b/Cargo.toml index 3c02c27..dd2f3c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,11 @@ crate-type = ["staticlib"] spin = "0.9.1" x86_64 = "0.14.10" rlibc = "1.0" +multiboot2 = { version = "0.14.0", optional = true } [dependencies.lazy_static] version = "1.4.0" -features = ["spin_no_std"] \ No newline at end of file +features = ["spin_no_std"] + +[features] +default = ["f_multiboot2"] +f_multiboot2 = ["dep:multiboot2"] \ No newline at end of file diff --git a/Makefile b/Makefile index a485b88..2de5bc9 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ assembly_source_files := $(wildcard arch/$(arch)/*.asm) assembly_object_files := $(patsubst arch/$(arch)/%.asm, \ build/arch/$(arch)/%.o, $(assembly_source_files)) -.PHONY: all clean run iso +.PHONY: all clean run iso quick_invalidate all: $(final) $(iso) @@ -24,6 +24,11 @@ run: $(final) $(iso) -chardev stdio,id=char0,mux=on,logfile=serial.log,signal=off \ -serial chardev:char0 -mon chardev=char0 +quick_invalidate: + @echo "quick invalidation" + @rm -rf build/arch/$(arch) + @rm -rf $(kernel) + iso: $(iso) $(iso): $(final) $(grub_cfg) @@ -43,9 +48,4 @@ $(kernel): build/arch/$(arch)/%.o: arch/$(arch)/%.asm @mkdir -p $(shell dirname $@) - @nasm -felf64 $< -o $@ - -quick_invalidate: - @echo "quick invalidation" - @rm -rf build/arch/$(arch) - @rm -rf $(kernel) \ No newline at end of file + @nasm -felf64 $< -o $@ \ No newline at end of file diff --git a/arch/x86_64/boot.asm b/arch/x86_64/boot.asm index 20eb87b..bb20481 100644 --- a/arch/x86_64/boot.asm +++ b/arch/x86_64/boot.asm @@ -8,6 +8,7 @@ bits 32 start: mov esp, stack_top + mov edi, ebx call check_multiboot call check_cpuid @@ -147,7 +148,7 @@ p3_table: p2_table: resb 4096 stack_bottom: - resb 64 + resb 4096 * 4 ;; 16 KiB stack_top: section .rodata diff --git a/serial.log b/serial.log index aed8b29..107d5ab 100644 --- a/serial.log +++ b/serial.log @@ -11,3 +11,11 @@ BdsDxe: starting Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1 welcome to wukkOS! (c) 2022 Real Microsoft, LLC +initialising memory maps...[OK] +memory map: +0 - a0000 : Available +100000 - 800000 : Available +808000 - 80b000 : Available +80c000 - 810000 : Available +900000 - 78ef000 : Available +7bff000 - 7f58000 : Available diff --git a/src/boot/mod.rs b/src/boot/mod.rs new file mode 100644 index 0000000..bb6f30e --- /dev/null +++ b/src/boot/mod.rs @@ -0,0 +1,27 @@ +use alloc::vec::Vec; +use crate::KernelArgs; + +#[cfg(feature = "f_multiboot2")] +pub mod multiboot2; + +pub enum MemoryType { + Available, + Reserved, + AcpiReclaimable, + Nvs, + BadMemory, + Kernel, + Bootloader, + Unknown(u32) +} + +pub struct MemoryArea { + pub start: usize, + pub end: usize, + pub area_type: MemoryType, +} + +pub trait KernelInfo { + fn init_from_kernel_args(&mut self, args: KernelArgs); + fn get_memory_areas(&self) -> Vec; +} \ No newline at end of file diff --git a/src/boot/multiboot2/mod.rs b/src/boot/multiboot2/mod.rs new file mode 100644 index 0000000..9dab24d --- /dev/null +++ b/src/boot/multiboot2/mod.rs @@ -0,0 +1,41 @@ +extern crate multiboot2; + +use alloc::vec; +use alloc::vec::Vec; +use multiboot2::{BootInformation, load, MemoryAreaType}; +use crate::boot::{KernelInfo, MemoryArea, MemoryType}; +use crate::KernelArgs; + +#[derive(Default)] +pub struct Multiboot2Bootloader { + pub boot_info: Option, +} + +impl KernelInfo for Multiboot2Bootloader { + fn init_from_kernel_args(&mut self, args: KernelArgs) { + let boot_info = unsafe { + load(args.multiboot_information_address) + }.expect("invalid kernel args!"); + self.boot_info = Some(boot_info); + } + + fn get_memory_areas(&self) -> Vec { + let mut memory_areas = vec![]; + let boot_info = self.boot_info.as_ref().unwrap(); + let memory_map_tag = boot_info.memory_map_tag().expect("memory map tag required but not found!"); + for area in memory_map_tag.memory_areas() { + memory_areas.push(MemoryArea { + start: area.start_address() as usize, + end: area.end_address() as usize, + area_type: match area.typ() { + MemoryAreaType::Available => MemoryType::Available, + MemoryAreaType::Reserved => MemoryType::Reserved, + MemoryAreaType::AcpiAvailable => MemoryType::AcpiReclaimable, + MemoryAreaType::ReservedHibernate => MemoryType::Reserved, + MemoryAreaType::Defective => MemoryType::BadMemory, + } + }) + } + memory_areas + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f78fde2..8973e97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,12 @@ #![feature(abi_x86_interrupt)] #![feature(default_alloc_error_handler)] +#![feature(panic_info_message)] #![no_std] #![no_main] +use alloc::boxed::Box; use alloc::format; +use alloc::string::ToString; use alloc::sync::Arc; use allocator::*; @@ -17,6 +20,7 @@ use core::ops::Deref; use lazy_static::lazy_static; use core::panic::PanicInfo; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use crate::boot::KernelInfo; use crate::internals::WhyDoTheyCallItOvenWhenYouOfInTheColdFoodOfOutHotEatTheFood::*; use crate::serial::{Port, potential_serial_ports, terminal_helpers, terminal::ST}; @@ -24,6 +28,8 @@ mod font; mod serial; mod internals; mod allocator; +mod security; +mod boot; lazy_static! { static ref IDT: InterruptDescriptorTable = { @@ -38,10 +44,33 @@ const RAINBOW : [Colour; 6] = [Colour{r:255,g:0,b:0}, Colour{r:255,g:127,b:0}, C #[panic_handler] -fn panic(_info: &PanicInfo) -> ! { loop {} } +fn panic(info: &PanicInfo) -> ! { + ST.logln("---KERNEL FUCKY WUKKY UWU (panic)---"); + ST.logln(if let Some(s) = info.payload().downcast_ref::<&str>() { + format!("panic payload: {s:?}") + } else { + format!("no panic payload") + }.as_str()); + ST.logln(if let Some(msg) = info.message() { + format!("panic msg: {}", msg.as_str().unwrap_or("no message")) + } else { + "no message".to_string() + }.as_str()); + ST.logln(if let Some(location) = info.location() { + format!("location: file {} line {}", location.file(), location.line()) + } else { + "no location".to_string() + }.as_str()); + loop {} +} + +pub struct KernelArgs { + #[cfg(feature = "f_multiboot2")] + multiboot_information_address: usize +} #[no_mangle] -pub extern fn kernel_main() -> ! { +pub extern fn kernel_main(args: KernelArgs) -> ! { // initialise serial let mut serial_ports = serial::init_serial(); let mut console_port = None; @@ -56,11 +85,36 @@ pub extern fn kernel_main() -> ! { ST.init_from_port(*port); } + let kern_info: Box = { + #[cfg(feature = "f_multiboot2")] + { + let mut kern_info = boot::multiboot2::Multiboot2Bootloader::default(); + kern_info.init_from_kernel_args(args); + Box::new(kern_info) + } + }; + ST.logln(""); ST.logln(""); ST.logln(""); ST.logln("welcome to wukkOS!"); ST.logln("(c) 2022 Real Microsoft, LLC"); + ST.log("initialising memory maps..."); + let mem_areas = kern_info.get_memory_areas(); + ST.logln("[OK]"); + ST.logln("memory map:"); + for area in mem_areas { + ST.logln(format!("{:x} - {:x} : {}", area.start, area.end, match area.area_type { + boot::MemoryType::Available => "Available", + boot::MemoryType::Reserved => "Reserved", + boot::MemoryType::AcpiReclaimable => "ACPI Reclaimable", + boot::MemoryType::Nvs => "NVS", + boot::MemoryType::BadMemory => "Bad Memory", + boot::MemoryType::Kernel => "Kernel", + boot::MemoryType::Bootloader => "Bootloader", + boot::MemoryType::Unknown(_) => "Unknown" + }).as_str()); + } loop {} } \ No newline at end of file diff --git a/src/security/mod.rs b/src/security/mod.rs new file mode 100644 index 0000000..d5dfa22 --- /dev/null +++ b/src/security/mod.rs @@ -0,0 +1 @@ +pub mod stack_smashing_protection; \ No newline at end of file diff --git a/src/security/stack_smashing_protection.rs b/src/security/stack_smashing_protection.rs new file mode 100644 index 0000000..794cfcb --- /dev/null +++ b/src/security/stack_smashing_protection.rs @@ -0,0 +1,8 @@ +pub const STACK_CHK_GUARD: usize = usize::MAX; + +pub const STACK_CHK_PTR: *const usize = &STACK_CHK_GUARD; + +pub extern "C" fn __stack_chk_fail() -> ! { + panic!("stack fucking or uh smashing detected!"); + loop {} +} \ No newline at end of file