Skip to content

Identity-map GDT into kernel address space #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2021
Merged
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Identity-map GDT into kernel address space to fix `iretq` ([#175](https://github.com/rust-osdev/bootloader/pull/175))
- Uefi: Look for an ACPI2 RSDP first ([#174](https://github.com/rust-osdev/bootloader/pull/174))

# 0.10.5 – 2021-05-21
Expand Down
32 changes: 32 additions & 0 deletions src/binary/gdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use x86_64::{
instructions::segmentation,
structures::{
gdt::{Descriptor, GlobalDescriptorTable},
paging::PhysFrame,
},
VirtAddr,
};

pub fn create_and_load(frame: PhysFrame) {
let phys_addr = frame.start_address();
log::info!("Creating GDT at {:?}", phys_addr);
let virt_addr = VirtAddr::new(phys_addr.as_u64()); // utilize identity mapping

let ptr: *mut GlobalDescriptorTable = virt_addr.as_mut_ptr();

let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
let data_selector = gdt.add_entry(Descriptor::kernel_data_segment());
let gdt = unsafe {
ptr.write(gdt);
&*ptr
};

gdt.load();
unsafe {
segmentation::set_cs(code_selector);
segmentation::load_ds(data_selector);
segmentation::load_es(data_selector);
segmentation::load_ss(data_selector);
}
}
13 changes: 13 additions & 0 deletions src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod bios;
#[cfg(feature = "uefi_bin")]
mod uefi;

mod gdt;
/// Provides a frame allocator based on a BIOS or UEFI memory map.
pub mod legacy_memory_region;
/// Provides a type to keep track of used entries in a level 4 page table.
Expand Down Expand Up @@ -170,6 +171,18 @@ where
}
}

// create, load, and identity-map GDT (required for working `iretq`)
let gdt_frame = frame_allocator
.allocate_frame()
.expect("failed to allocate GDT frame");
gdt::create_and_load(gdt_frame);
match unsafe {
kernel_page_table.identity_map(gdt_frame, PageTableFlags::PRESENT, frame_allocator)
} {
Ok(tlb) => tlb.flush(),
Err(err) => panic!("failed to identity map frame {:?}: {:?}", gdt_frame, err),
}

// map framebuffer
let framebuffer_virt_addr = if CONFIG.map_framebuffer {
log::info!("Map framebuffer");
Expand Down
5 changes: 4 additions & 1 deletion tests/test_kernels/default_settings/src/bin/basic_boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fn kernel_main(_boot_info: &'static mut BootInfo) -> ! {

/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
use core::fmt::Write;

let _ = writeln!(test_kernel_default_settings::serial(), "PANIC: {}", info);
exit_qemu(QemuExitCode::Failed);
}