make it boot on my macbook

This commit is contained in:
fekhesk 2022-10-27 14:29:40 -07:00
parent eb1e469fdc
commit 8fcc70bee1
No known key found for this signature in database
GPG Key ID: D17BA3F38ED319DF
6 changed files with 37 additions and 47 deletions

View File

@ -4,6 +4,10 @@ iso := build/arch/$(arch)/wukkOS.iso
target ?= $(arch)-custom target ?= $(arch)-custom
final := build/arch/$(arch)/wukkOS.bin final := build/arch/$(arch)/wukkOS.bin
efi_bios := build/arch/$(arch)/OVMF-pure-efi.fd efi_bios := build/arch/$(arch)/OVMF-pure-efi.fd
gcc := x86_64-elf-gcc
ld := x86_64-elf-ld
# set grub-mkrescue to either $GRUB_MKRESCUE or /usr/bin/grub-mkrescue
grub-mkrescue ?= $(shell which grub-mkrescue)
linker_script := arch/$(arch)/linker.ld linker_script := arch/$(arch)/linker.ld
grub_cfg := arch/$(arch)/grub.cfg grub_cfg := arch/$(arch)/grub.cfg
@ -11,10 +15,12 @@ assembly_source_files := $(wildcard arch/$(arch)/*.asm)
assembly_object_files := $(patsubst arch/$(arch)/%.asm, \ assembly_object_files := $(patsubst arch/$(arch)/%.asm, \
build/arch/$(arch)/%.o, $(assembly_source_files)) build/arch/$(arch)/%.o, $(assembly_source_files))
.PHONY: all clean run iso quick_invalidate .PHONY: all clean run iso quick_invalidate build_no_iso
all: $(final) $(iso) all: $(final) $(iso)
build_no_iso: $(final)
clean: clean:
@xargo clean @xargo clean
@rm -rf build @rm -rf build
@ -36,12 +42,12 @@ $(iso): $(final) $(grub_cfg)
@mkdir -p isodir/boot/grub @mkdir -p isodir/boot/grub
@cp $(final) isodir/boot/wukkOS.bin @cp $(final) isodir/boot/wukkOS.bin
@cp $(grub_cfg) isodir/boot/grub/grub.cfg @cp $(grub_cfg) isodir/boot/grub/grub.cfg
@grub-mkrescue -o $(iso) isodir @$(grub-mkrescue) -o $(iso) isodir
@rm -rf isodir @rm -rf isodir
$(final): $(kernel) $(linker_script) $(assembly_object_files) $(final): $(kernel) $(linker_script) $(assembly_object_files)
@ld -n -T $(linker_script) -o $(final) $(assembly_object_files) $(kernel) \ @$(ld) -n -T $(linker_script) -o $(final) $(assembly_object_files) $(kernel) \
--gc-sections --gc-sections -z noexecstack
$(kernel): $(kernel):
@RUST_TARGET_PATH=$(shell pwd) xargo build --target $(target) -Zbuild-std=core,alloc --features "f_multiboot2" @RUST_TARGET_PATH=$(shell pwd) xargo build --target $(target) -Zbuild-std=core,alloc --features "f_multiboot2"

View File

@ -1,8 +1,6 @@
[=3h[=3h[=3h[=3hBdsDxe: loading Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0) [=3hBdsDxe: loading Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0)
BdsDxe: starting Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0) BdsDxe: starting Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0)
Welcome to GRUB! Booting `wukkOS basic kernel'
 Booting `wukkOS basic kernel'
WARNING: no console will be available to OS WARNING: no console will be available to OS
error: no suitable video mode found. error: no suitable video mode found.
@ -29,22 +27,4 @@ initialising apic...[debug] read_phys_memory32: addr fee000f0 not mapped
[debug] mapped page: Page[4KiB](0xfee00000) [debug] mapped page: Page[4KiB](0xfee00000)
[debug] map_to_result: Ok(MapperFlush(Page[4KiB](0xfee00000))) [debug] map_to_result: Ok(MapperFlush(Page[4KiB](0xfee00000)))
[OK] [OK]
setting up apic interrupts...[debug] write_phys_memory32: addr fec00000 not mapped setting up apic interrupts...[debug] read_phys_memory32: addr 1fb7d000 not mapped
[debug] allocated frame: PhysFrame[4KiB](0x1f000)
[debug] mapped page: Page[4KiB](0xfec00000)
[debug] map_to_result: Ok(MapperFlush(Page[4KiB](0xfec00000)))
[OK]
[debug] keyboard interrupt
---KERNEL FUCKY WUKKY UWU---
double fault!
stack frame: InterruptStackFrame {
instruction_pointer: VirtAddr(
0x120ef1,
),
code_segment: 8,
cpu_flags: 0x200002,
stack_pointer: VirtAddr(
0x140c00,
),
stack_segment: 16,
}

View File

@ -6,8 +6,9 @@ use crate::{debug, KernelArgs, println};
#[cfg(feature = "f_multiboot2")] #[cfg(feature = "f_multiboot2")]
use multiboot2::{load, MemoryMapTag, BootInformation}; use multiboot2::{load, MemoryMapTag, BootInformation};
use x86_64::structures::paging::{FrameAllocator, Mapper, OffsetPageTable, Translate}; use x86_64::structures::paging::{FrameAllocator, Mapper, OffsetPageTable, Page, Translate};
use crate::memory::{BootInfoFrameAllocator, FRAME_ALLOC, MEM_MAPPER}; use x86_64::VirtAddr;
use crate::memory::{BootInfoFrameAllocator, FRAME_ALLOC, MEM_MAPPER, PageSize};
pub struct KernelInfo { pub struct KernelInfo {
kernel_start: u64, kernel_start: u64,
@ -27,14 +28,14 @@ impl AcpiHandler for Handler {
let frame = FRAME_ALLOC.lock().as_mut().unwrap().allocate_frame().unwrap(); let frame = FRAME_ALLOC.lock().as_mut().unwrap().allocate_frame().unwrap();
debug!("allocated frame: {:?}", frame); debug!("allocated frame: {:?}", frame);
let flags = x86_64::structures::paging::PageTableFlags::PRESENT | x86_64::structures::paging::PageTableFlags::WRITABLE; let flags = x86_64::structures::paging::PageTableFlags::PRESENT | x86_64::structures::paging::PageTableFlags::WRITABLE;
let page = x86_64::structures::paging::Page::containing_address(initaladdr); let page: Page<PageSize> = Page::containing_address(VirtAddr::new(physical_address as u64));
debug!("mapped page: {:?}", page); debug!("mapped page: {:?}", page);
let map_to_result = unsafe { MEM_MAPPER.lock().as_mut().unwrap().map_to(page, frame, flags, FRAME_ALLOC.lock().as_mut().unwrap()) }; let map_to_result = unsafe { MEM_MAPPER.lock().as_mut().unwrap().map_to(page, frame, flags, FRAME_ALLOC.lock().as_mut().unwrap()) };
debug!("map_to_result: {:?}", map_to_result); debug!("map_to_result: {:?}", map_to_result);
if map_to_result.is_err() { if map_to_result.is_err() {
panic!("Failed to map page"); panic!("Failed to map page");
} }
let addr = unsafe { MEM_MAPPER.lock().as_mut().unwrap().translate_addr(initaladdr) }; let addr = unsafe { MEM_MAPPER.lock().as_mut().unwrap().translate_addr(VirtAddr::new(physical_address as u64)) };
if let Some(addr) = addr { if let Some(addr) = addr {
// physical start, virtual start, region length, mapped length, Self // physical start, virtual start, region length, mapped length, Self
PhysicalMapping::new( PhysicalMapping::new(
@ -49,7 +50,7 @@ impl AcpiHandler for Handler {
fn unmap_physical_region<T>(region: &PhysicalMapping<Self, T>) { fn unmap_physical_region<T>(region: &PhysicalMapping<Self, T>) {
// get page // get page
let page = x86_64::structures::paging::Page::containing_address(region.start_address()); let page: Page<PageSize> = Page::containing_address(VirtAddr::new(region.physical_start() as u64));
// unmap page // unmap page
let res = unsafe { MEM_MAPPER.lock().as_mut().unwrap().unmap(page) }; let res = unsafe { MEM_MAPPER.lock().as_mut().unwrap().unmap(page) };
// it isn't *that* important if we don't unmap successfully at the moment, so just write a warning if we fail // it isn't *that* important if we don't unmap successfully at the moment, so just write a warning if we fail
@ -108,7 +109,7 @@ impl KernelInfo {
let rsdt = rsdp.rsdt_address(); let rsdt = rsdp.rsdt_address();
let rsdt = unsafe { let rsdt = unsafe {
acpi::AcpiTables::from_rsdt( acpi::AcpiTables::from_rsdt(
AcpiHandler, 0, Handler, 0,
rsdt) rsdt)
.expect("failed to get acpi tables") .expect("failed to get acpi tables")
}; };

View File

@ -37,6 +37,7 @@ mod memory;
mod macros; mod macros;
lazy_static! { lazy_static! {
pub static ref KERN_INFO: Mutex<Option<KernelInfo>> = Mutex::new(None);
static ref GDT: Mutex<GlobalDescriptorTable> = { static ref GDT: Mutex<GlobalDescriptorTable> = {
let mut gdt = GlobalDescriptorTable::new(); let mut gdt = GlobalDescriptorTable::new();
Mutex::new(gdt) Mutex::new(gdt)
@ -163,7 +164,7 @@ pub extern fn kernel_main(args: KernelArgs) -> ! {
println!(); println!();
println!("welcome to wukkOS!"); println!("welcome to wukkOS!");
println!("(c) 2022 Real Microsoft, LLC"); println!("(c) 2022 Real Microsoft, LLC");
let kern_info = Mutex::new(KernelInfo::init_from_kernel_args(args)); KERN_INFO.lock().replace(KernelInfo::init_from_kernel_args(args));
// memory stuff // memory stuff
{ {
@ -171,7 +172,7 @@ pub extern fn kernel_main(args: KernelArgs) -> ! {
MEM_MAPPER.lock().replace(unsafe { memory::init(VirtAddr::new(0)) }); MEM_MAPPER.lock().replace(unsafe { memory::init(VirtAddr::new(0)) });
println!("[OK]"); println!("[OK]");
print!("initialising frame allocator..."); print!("initialising frame allocator...");
FRAME_ALLOC.lock().replace(unsafe { memory::BootInfoFrameAllocator::init(kern_info) }); FRAME_ALLOC.lock().replace(unsafe { memory::BootInfoFrameAllocator::init() });
println!("[OK]"); println!("[OK]");
print!("initialising heap..."); print!("initialising heap...");
memory::allocator::init_heap(MEM_MAPPER.lock().as_mut().unwrap(), FRAME_ALLOC.lock().as_mut().unwrap()).expect("heap init failed"); memory::allocator::init_heap(MEM_MAPPER.lock().as_mut().unwrap(), FRAME_ALLOC.lock().as_mut().unwrap()).expect("heap init failed");
@ -208,7 +209,7 @@ pub extern fn kernel_main(args: KernelArgs) -> ! {
unsafe { internals::cpu::enable_apic() }; unsafe { internals::cpu::enable_apic() };
println!("[OK]"); println!("[OK]");
print!("setting up apic interrupts..."); print!("setting up apic interrupts...");
unsafe { internals::cpu::setup_apic_interrupts(kern_info.lock().acpi_get_ioapic_addr()) }; unsafe { internals::cpu::setup_apic_interrupts(KERN_INFO.lock().as_ref().unwrap().acpi_get_ioapic_addr()) };
println!("[OK]"); println!("[OK]");
// enable interrupts // enable interrupts
x86_64::instructions::interrupts::enable(); x86_64::instructions::interrupts::enable();

View File

@ -4,6 +4,7 @@ use core::ptr::NonNull;
use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB}; use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB};
use x86_64::structures::paging::mapper::MapToError; use x86_64::structures::paging::mapper::MapToError;
use x86_64::VirtAddr; use x86_64::VirtAddr;
use crate::memory::PageSize;
use super::Locked; use super::Locked;
pub const HEAP_START: u64 = 0x_4444_4444_0000; pub const HEAP_START: u64 = 0x_4444_4444_0000;
@ -99,9 +100,9 @@ fn list_index(layout: &core::alloc::Layout) -> Option<usize> {
} }
pub fn init_heap( pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>, mapper: &mut impl Mapper<PageSize>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>, frame_allocator: &mut impl FrameAllocator<PageSize>,
) -> Result<(), MapToError<Size4KiB>> { ) -> Result<(), MapToError<PageSize>> {
let page_range = { let page_range = {
let heap_start = VirtAddr::new(HEAP_START as u64); let heap_start = VirtAddr::new(HEAP_START as u64);
let heap_end = heap_start + HEAP_SIZE - 1u64; let heap_end = heap_start + HEAP_SIZE - 1u64;

View File

@ -7,10 +7,12 @@ use x86_64::structures::paging::{FrameAllocator, Mapper, OffsetPageTable, PageTa
use x86_64::{PhysAddr, VirtAddr}; use x86_64::{PhysAddr, VirtAddr};
lazy_static!{ lazy_static!{
pub static ref MEM_MAPPER: Mutex<Option<OffsetPageTable>> = Mutex::new(None); pub static ref MEM_MAPPER: Mutex<Option<OffsetPageTable<'static>>> = Mutex::new(None);
pub static ref FRAME_ALLOC: Mutex<Option<BootInfoFrameAllocator>> = Mutex::new(None); pub static ref FRAME_ALLOC: Mutex<Option<BootInfoFrameAllocator>> = Mutex::new(None);
} }
pub type PageSize = Size4KiB;
pub struct Locked<A> { pub struct Locked<A> {
inner: spin::Mutex<A>, inner: spin::Mutex<A>,
} }
@ -48,18 +50,16 @@ unsafe fn active_level_4_table(phys_mem_offset: VirtAddr) -> &'static mut PageTa
use multiboot2::{MemoryMapTag, BootInformation}; use multiboot2::{MemoryMapTag, BootInformation};
use spin::Mutex; use spin::Mutex;
use crate::boot::KernelInfo; use crate::boot::KernelInfo;
use crate::{debug, print, println}; use crate::{debug, KERN_INFO, print, println};
pub struct BootInfoFrameAllocator { pub struct BootInfoFrameAllocator {
kern_info: Mutex<KernelInfo>,
next: usize, next: usize,
} }
impl BootInfoFrameAllocator { impl BootInfoFrameAllocator {
#[cfg(feature = "f_multiboot2")] #[cfg(feature = "f_multiboot2")]
pub unsafe fn init(kern_info: Mutex<crate::boot::KernelInfo>) -> Self { pub unsafe fn init() -> Self {
Self { Self {
kern_info,
next: 0, next: 0,
} }
} }
@ -68,8 +68,9 @@ impl BootInfoFrameAllocator {
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator { unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame> { fn allocate_frame(&mut self) -> Option<PhysFrame> {
#[cfg(feature = "f_multiboot2")] { #[cfg(feature = "f_multiboot2")] {
let mut kern_lock = self.kern_info.lock(); let mut kern_lock = KERN_INFO.lock();
let mut usable_frames = kern_lock let mut kern_info = kern_lock.as_mut().unwrap();
let mut usable_frames = kern_info
.memory_areas(); .memory_areas();
let mut usable_frames = usable_frames let mut usable_frames = usable_frames
.map(|area| { .map(|area| {
@ -85,7 +86,7 @@ unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
self.next += 1; self.next += 1;
// ensure unlock // ensure unlock
unsafe { self.kern_info.force_unlock() }; unsafe { KERN_INFO.force_unlock() };
frame frame
} }