Skip to content
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

add dynamic range configuration #229

Merged
merged 1 commit into from
Apr 10, 2022
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
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ required-features = ["uefi_bin"]

[dependencies]
xmas-elf = { version = "0.8.0", optional = true }
x86_64 = { version = "0.14.7", optional = true, default-features = false, features = ["instructions", "inline_asm"] }
x86_64 = { version = "0.14.9", optional = true, default-features = false, features = ["instructions", "inline_asm", "step_trait"] }
usize_conversions = { version = "0.2.0", optional = true }
bit_field = { version = "0.10.0", optional = true }
log = { version = "0.4.8", optional = true }
Expand Down
8 changes: 7 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ mod binary {
pub framebuffer_address: Option<AlignedAddress>,
pub minimum_framebuffer_height: Option<usize>,
pub minimum_framebuffer_width: Option<usize>,
pub dynamic_range_start: Option<AlignedAddress>,
pub dynamic_range_end: Option<AlignedAddress>,
}

/// Convert to tokens suitable for initializing the `Config` struct.
Expand All @@ -387,6 +389,8 @@ mod binary {
let framebuffer_address = optional(self.framebuffer_address);
let minimum_framebuffer_height = optional(self.minimum_framebuffer_height);
let minimum_framebuffer_width = optional(self.minimum_framebuffer_width);
let dynamic_range_start = optional(self.dynamic_range_start);
let dynamic_range_end = optional(self.dynamic_range_end);

tokens.extend(quote! { Config {
map_physical_memory: #map_physical_memory,
Expand All @@ -400,7 +404,9 @@ mod binary {
boot_info_address: #boot_info_address,
framebuffer_address: #framebuffer_address,
minimum_framebuffer_height: #minimum_framebuffer_height,
minimum_framebuffer_width: #minimum_framebuffer_width
minimum_framebuffer_width: #minimum_framebuffer_width,
dynamic_range_start: #dynamic_range_start,
dynamic_range_end: #dynamic_range_end,
}});
}
}
Expand Down
24 changes: 23 additions & 1 deletion src/binary/level_4_entries.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{alloc::Layout, convert::TryInto};
use core::{alloc::Layout, convert::TryInto, iter::Step};
use rand::{
distributions::{Distribution, Uniform},
seq::IteratorRandom,
Expand Down Expand Up @@ -70,6 +70,28 @@ impl UsedLevel4Entries {
}
}

// Mark everything before the dynamic range as unusable.
if let Some(dynamic_range_start) = CONFIG.dynamic_range_start {
let dynamic_range_start = VirtAddr::new(dynamic_range_start);
let start_page: Page = Page::containing_address(dynamic_range_start);
if let Some(unusable_page) = Step::backward_checked(start_page, 1) {
for i in 0..=u16::from(unusable_page.p4_index()) {
used.mark_p4_index_as_used(PageTableIndex::new(i));
}
}
}

// Mark everything after the dynamic range as unusable.
if let Some(dynamic_range_end) = CONFIG.dynamic_range_end {
let dynamic_range_end = VirtAddr::new(dynamic_range_end);
let end_page: Page = Page::containing_address(dynamic_range_end);
if let Some(unusable_page) = Step::forward_checked(end_page, 1) {
for i in u16::from(unusable_page.p4_index())..512 {
used.mark_p4_index_as_used(PageTableIndex::new(i));
}
}
}

used
}

Expand Down
10 changes: 9 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const PAGE_SIZE: u64 = 4096;
///
/// All memory addresses are optional, even if their corresponding switch is enabled. If no
/// address is specified, the bootloader will choose an unused entry of the level 4 page table
/// at runtime.
/// at runtime. The addresses can be restricted by setting the `dynamic-range` configuration key.
#[derive(Debug)]
pub struct Config {
/// Whether to create a virtual mapping of the complete physical memory.
Expand Down Expand Up @@ -96,6 +96,14 @@ pub struct Config {
/// `minimum_framebuffer_width` is supplied, and using the last available mode that
/// fits them if 1 or more is set.
pub minimum_framebuffer_width: Option<usize>,
/// The lowest virtual address for dynamic addresses.
///
/// Defaults to `0`.
pub dynamic_range_start: Option<u64>,
/// The highest virtual address for dynamic addresses.
///
/// Defaults to `0xffff_ffff_ffff_f000`.
pub dynamic_range_end: Option<u64>,
}

#[cfg(feature = "binary")]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ for all possible configuration options.

#![cfg_attr(not(feature = "builder"), no_std)]
#![feature(maybe_uninit_slice)]
#![feature(step_trait)]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]

Expand Down
3 changes: 3 additions & 0 deletions tests/test_kernels/higher_half/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ edition = "2018"
bootloader = { path = "../../.." }
x86_64 = { version = "0.14.7", default-features = false, features = ["instructions", "inline_asm"] }
uart_16550 = "0.2.10"

[package.metadata.bootloader]
dynamic-range-start = "0xffff_8000_0000_0000"
16 changes: 15 additions & 1 deletion tests/test_kernels/higher_half/src/bin/verify_higher_half.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ use test_kernel_higher_half::{exit_qemu, QemuExitCode};

entry_point!(kernel_main);

fn kernel_main(_boot_info: &'static mut BootInfo) -> ! {
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
// verify that kernel is really running in the higher half of the address space
// (set in `x86_64-higher_half.json` custom target)
let rip = x86_64::registers::read_rip().as_u64();
assert_eq!(rip & 0xffffffffffff0000, 0xffff800000000000);

// verify that the boot info is located in the higher half of the address space
assert_eq!(
(boot_info as *const _ as usize) & 0xffff800000000000,
0xffff800000000000
);

// verify that the stack is located in the higher half of the address space.
let stack_addr = &rip;
assert_eq!(
(boot_info as *const _ as usize) & 0xffff800000000000,
0xffff800000000000
);

exit_qemu(QemuExitCode::Success);
}

Expand Down