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

Allow overriding the physical memory offset through an environment variable #58

Merged
merged 4 commits into from
Jun 24, 2019
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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Make the physical memory offset configurable through a `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable ([#58](https://github.com/rust-osdev/bootloader/pull/58)).

# 0.6.0

- **Breaking**: Don't set the `#[cfg(not(test))]` attribute for the entry point function in the `entry_point` macro
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The bootloader crate can be configured through some cargo features:
- `vga_320x200`: This feature switches the VGA hardware to mode 0x13, a graphics mode with resolution 320x200 and 256 colors per pixel. The framebuffer is linear and lives at address `0xa0000`.
- `recursive_page_table`: Maps the level 4 page table recursively and adds the [`recursive_page_table_address`](https://docs.rs/bootloader/0.4.0/bootloader/bootinfo/struct.BootInfo.html#structfield.recursive_page_table_addr) field to the passed `BootInfo`.
- `map_physical_memory`: Maps the complete physical memory in the virtual address space and passes a [`physical_memory_offset`](https://docs.rs/bootloader/0.4.0/bootloader/bootinfo/struct.BootInfo.html#structfield.physical_memory_offset) field in the `BootInfo`.
- The virtual address where the physical memory should be mapped is configurable by setting the `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable (supports decimal and hex numbers (prefixed with `0x`)).

## License

Expand Down
31 changes: 31 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
process::{self, Command},
};
Expand Down Expand Up @@ -123,6 +125,34 @@ fn main() {
process::exit(1);
}

// create a file with the `PHYSICAL_MEMORY_OFFSET` constant
let file_path = out_dir.join("physical_memory_offset.rs");
let mut file = File::create(file_path).expect("failed to create physical_memory_offset.rs");
let physical_memory_offset = match env::var("BOOTLOADER_PHYSICAL_MEMORY_OFFSET") {
Err(env::VarError::NotPresent) => 0o_177777_770_000_000_000_0000u64,
Err(env::VarError::NotUnicode(_)) => panic!(
"The `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable must be valid unicode"
),
Ok(s) => if s.starts_with("0x") {
u64::from_str_radix(&s[2..], 16)
} else {
u64::from_str_radix(&s, 10)
}
.expect(&format!(
"The `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable must be an integer\
(is `{}`).",
s
)),
};
file.write_all(
format!(
"const PHYSICAL_MEMORY_OFFSET: u64 = {:#x};",
physical_memory_offset
)
.as_bytes(),
)
.expect("write to physical_memory_offset.rs failed");

// pass link arguments to rustc
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!(
Expand All @@ -131,6 +161,7 @@ fn main() {
);

println!("cargo:rerun-if-env-changed=KERNEL");
println!("cargo:rerun-if-env-changed=BOOTLOADER_PHYSICAL_MEMORY_OFFSET");
println!("cargo:rerun-if-changed={}", kernel.display());
println!("cargo:rerun-if-changed=build.rs");
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use x86_64::structures::paging::{
use x86_64::ux::u9;
use x86_64::{PhysAddr, VirtAddr};

/// The offset into the virtual address space where the physical memory is mapped if
/// the `map_physical_memory` is activated.
const PHYSICAL_MEMORY_OFFSET: u64 = 0o_177777_770_000_000_000_0000;
// The offset into the virtual address space where the physical memory is mapped if
// the `map_physical_memory` is activated. Set by the build script.
include!(concat!(env!("OUT_DIR"), "/physical_memory_offset.rs"));

global_asm!(include_str!("stage_1.s"));
global_asm!(include_str!("stage_2.s"));
Expand Down