Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions example-kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

use core::arch::asm;

#[cfg(target_arch = "x86_64")]
const PHYSICAL_MEMORY_OFFSET: u64 = 0xffff_8000_0000_0000;

#[cfg(target_arch = "x86_64")]
#[repr(C, packed)]
struct DescriptorTablePointer {
limit: u16,
base: u64,
}

/// Write a byte to COM1 serial port (0x3F8).
#[cfg(target_arch = "x86_64")]
fn serial_putchar(c: u8) {
Expand Down Expand Up @@ -32,14 +42,25 @@ fn serial_print(s: &str) {
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
serial_print("\n[test-kernel] Hello from rboot test kernel!\n");
serial_print("[test-kernel] rboot is working correctly.\n");

#[cfg(target_arch = "x86_64")]
unsafe {
// Shutdown QEMU via ISA debug exit device (port 0x501)
asm!("out dx, al", in("dx") 0x501u16, in("al") 0x31u8);
{
let mut gdtr = DescriptorTablePointer { limit: 0, base: 0 };
unsafe {
asm!("sgdt [{}]", in(reg) &mut gdtr, options(nostack, preserves_flags));
}
let gdt_base = unsafe { core::ptr::addr_of!(gdtr.base).read_unaligned() };
if gdt_base < PHYSICAL_MEMORY_OFFSET {
serial_print("[test-kernel] GDTR still points to low memory!\n");
exit_qemu(1);
}
}

serial_print("[test-kernel] rboot is working correctly.\n");

#[cfg(target_arch = "x86_64")]
exit_qemu(0);

loop {
#[cfg(target_arch = "x86_64")]
unsafe {
Expand All @@ -52,6 +73,14 @@ pub extern "C" fn _start() -> ! {
}
}

#[cfg(target_arch = "x86_64")]
fn exit_qemu(code: u8) {
unsafe {
// Shutdown QEMU via ISA debug exit device (port 0x501).
asm!("out dx, al", in("dx") 0x501u16, in("al") code);
}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
serial_print("[test-kernel] PANIC!\n");
Expand Down
31 changes: 30 additions & 1 deletion src/arch/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use log::{debug, info};
use rboot::BootInfo;
use uefi::boot::{self, AllocateType, MemoryType};
use x86_64::instructions::tables::{lgdt, sgdt};
use x86_64::registers::control::*;
use x86_64::structures::paging::{mapper::*, *};
use x86_64::structures::{
DescriptorTablePointer,
paging::{mapper::*, *},
};
use x86_64::{PhysAddr, VirtAddr, align_up};
use xmas_elf::{ElfFile, program};

Expand All @@ -25,6 +29,31 @@ unsafe impl FrameAllocator<Size4KiB> for UEFIFrameAllocator {
}
}

/// Copy the firmware GDT into loader-owned memory and return a descriptor that
/// addresses the copy through the kernel's physical-memory mapping.
pub fn prepare_gdt(physical_memory_offset: u64) -> DescriptorTablePointer {
let firmware_gdt = sgdt();
let size = usize::from(firmware_gdt.limit) + 1;
let pages = size.div_ceil(Size4KiB::SIZE as usize);
let gdt_phys = boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, pages)
.expect("failed to allocate GDT");

unsafe {
core::ptr::copy_nonoverlapping(firmware_gdt.base.as_ptr::<u8>(), gdt_phys.as_ptr(), size);
}

DescriptorTablePointer {
limit: firmware_gdt.limit,
base: VirtAddr::new(gdt_phys.as_ptr() as u64 + physical_memory_offset),
}
}

/// Load the relocated GDT after exiting boot services, so its address remains
/// valid when the kernel replaces the firmware page tables.
pub unsafe fn load_gdt(gdt: &DescriptorTablePointer) {
unsafe { lgdt(gdt) };
}

pub fn map_elf(
elf: &ElfFile,
page_table: &mut impl Mapper<Size4KiB>,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn efi_main() -> Status {
&mut page_table,
&mut arch::UEFIFrameAllocator,
);
let gdt = arch::prepare_gdt(config.physical_memory_offset);
unsafe {
Cr0::update(|f| f.insert(Cr0Flags::WRITE_PROTECT));
}
Expand All @@ -146,6 +147,7 @@ fn efi_main() -> Status {
bootinfo.memory_map.push(*desc);
}
unsafe {
arch::load_gdt(&gdt);
arch::jump_to_entry(entry, &bootinfo, stacktop);
}
}
Expand Down
Loading