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

UEFI: Add support for different framebuffer configs on real hardware vs VMs #364

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a2fc6ce
Refactor UEFI implementation for compliance with rust-lang/rust#10745…
kennystrawnmusic Mar 27, 2023
fd818fc
rustfmt
kennystrawnmusic Mar 27, 2023
57bd85d
rust-osdev/bootloader#360: Resolve @phil-opp's concerns (1/5)
kennystrawnmusic Apr 12, 2023
ec7cbaa
rust-osdev/bootloader#360: Resolve @phil-opp's concerns (2/5)
kennystrawnmusic Apr 12, 2023
d18076e
rust-osdev/bootloader#360: Resolve @phil-opp's concerns (3/5 + 4/5)
kennystrawnmusic Apr 12, 2023
5d153e3
Update tests/runner/src/lib.rs
kennystrawnmusic Apr 12, 2023
02ecbb2
Fix unused import warning
phil-opp Apr 13, 2023
2aa4abf
Merge branch 'rust-osdev:main' into master
kennystrawnmusic Apr 15, 2023
84d3f62
Use highest resolution available on real hardware
kennystrawnmusic Apr 15, 2023
d8d4b9c
rustfmt
kennystrawnmusic Apr 15, 2023
ccb8bb2
Merge branch 'rust-osdev:main' into master
kennystrawnmusic Apr 30, 2023
16330cd
#364: Use different configs for VMs vs real hardware
kennystrawnmusic Apr 30, 2023
2f66e58
#364: rustfmt
kennystrawnmusic Apr 30, 2023
fb182e7
Merge branch 'rust-osdev:main' into master
kennystrawnmusic Sep 6, 2023
299a670
Pass the UEFI runtime services table address to the BootInfo if runni…
kennystrawnmusic Oct 2, 2023
68a123e
Pass the UEFI runtime services table address to the BootInfo if runni…
kennystrawnmusic Oct 2, 2023
c5b6e04
rustfmt
kennystrawnmusic Oct 2, 2023
31f77fa
Merge remote-tracking branch 'origin/patch-1'
kennystrawnmusic Oct 2, 2023
0b028db
Remove redundant dependencies
kennystrawnmusic Oct 2, 2023
35bcae6
Add back the Copy and Clone impls
kennystrawnmusic Oct 2, 2023
5578f36
Fix doc comments
kennystrawnmusic Oct 2, 2023
20dbee7
rustfmt
kennystrawnmusic Oct 2, 2023
82ea900
Merge commits pushed to pull request
kennystrawnmusic Oct 2, 2023
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 Cargo.lock

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

4 changes: 0 additions & 4 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ repository.workspace = true
edition = "2021"
description = "Makes a kernel compatible with the bootloader crate"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dev-dependencies]
rand = "0.8.4"
4 changes: 4 additions & 0 deletions api/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub struct BootInfo {
/// Virtual address of the loaded kernel image.
pub kernel_image_offset: u64,

/// UEFI runtime table address (if running on UEFI)
pub rt_table_addr: Optional<u64>,

#[doc(hidden)]
pub _test_sentinel: u64,
}
Expand All @@ -85,6 +88,7 @@ impl BootInfo {
kernel_addr: 0,
kernel_len: 0,
kernel_image_offset: 0,
rt_table_addr: Optional::None,
_test_sentinel: 0,
}
}
Expand Down
17 changes: 13 additions & 4 deletions bios/stage-4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,21 @@ pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
};

#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_height.is_none() {
config.frame_buffer.minimum_framebuffer_height =
if config
.frame_buffer_physical
.minimum_framebuffer_height
.is_none()
{
config.frame_buffer_physical.minimum_framebuffer_height =
kernel.config.frame_buffer.minimum_framebuffer_height;
}
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_width.is_none() {
config.frame_buffer.minimum_framebuffer_width =
if config
.frame_buffer_physical
.minimum_framebuffer_width
.is_none()
{
config.frame_buffer_physical.minimum_framebuffer_width =
kernel.config.frame_buffer.minimum_framebuffer_width;
}
let framebuffer_info = init_logger(
Expand Down Expand Up @@ -164,6 +172,7 @@ pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
_ => Some(info.ramdisk.start),
},
ramdisk_len: info.ramdisk.len,
rt_table_addr: None,
};

load_and_switch_to_kernel(kernel, config, frame_allocator, page_tables, system_info);
Expand Down
10 changes: 7 additions & 3 deletions common/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use serde::{Deserialize, Serialize};
#[serde(default)]
#[non_exhaustive]
pub struct BootConfig {
/// Configuration for the frame buffer setup.
pub frame_buffer: FrameBuffer,
/// Configuration for the frame buffer setup on physical hardware.
pub frame_buffer_physical: FrameBuffer,

/// Configuration for the frame buffer setup in a virtual machine
pub frame_buffer_virtual: FrameBuffer,

/// The minimum log level that is printed to the screen during boot.
///
Expand All @@ -32,7 +35,8 @@ pub struct BootConfig {
impl Default for BootConfig {
fn default() -> Self {
Self {
frame_buffer: Default::default(),
frame_buffer_virtual: Default::default(),
frame_buffer_physical: Default::default(),
log_level: Default::default(),
frame_buffer_logging: true,
serial_logging: true,
Expand Down
12 changes: 11 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::legacy_memory_region::{LegacyFrameAllocator, LegacyMemoryRegion};
use bootloader_api::{
config::Mapping,
info::{FrameBuffer, FrameBufferInfo, MemoryRegion, TlsTemplate},
info::{FrameBuffer, FrameBufferInfo, MemoryRegion, Optional, TlsTemplate},
BootInfo, BootloaderConfig,
};
use bootloader_boot_config::{BootConfig, LevelFilter};
Expand Down Expand Up @@ -80,6 +80,11 @@ pub struct SystemInfo {
pub rsdp_addr: Option<PhysAddr>,
pub ramdisk_addr: Option<u64>,
pub ramdisk_len: u64,

/// UEFI runtime table address (if running on UEFI).
///
/// Use a raw pointer from your kernel to this address to access UEFI Runtime Services.
pub rt_table_addr: Option<u64>,
}

/// The physical address of the framebuffer and information about the framebuffer.
Expand Down Expand Up @@ -551,6 +556,11 @@ where
info.kernel_len = mappings.kernel_slice_len as _;
info.kernel_image_offset = mappings.kernel_image_offset.as_u64();
info._test_sentinel = boot_config._test_sentinel;
info.rt_table_addr = if let Some(addr) = system_info.rt_table_addr {
Optional::Some(addr)
} else {
Optional::None
};
info
});

Expand Down
1 change: 1 addition & 0 deletions uefi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ log = "0.4.14"
x86_64 = "0.14.8"
serde-json-core = "0.5.0"
uefi = "0.20.0"
raw-cpuid = "10.7.0"
113 changes: 90 additions & 23 deletions uefi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ops::{Deref, DerefMut},
ptr, slice,
};
use raw_cpuid::{CpuId, Hypervisor};
use uefi::{
prelude::{entry, Boot, Handle, Status, SystemTable},
proto::{
Expand Down Expand Up @@ -97,13 +98,21 @@
};

#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_height.is_none() {
config.frame_buffer.minimum_framebuffer_height =
if config
.frame_buffer_physical
.minimum_framebuffer_height
.is_none()
{
config.frame_buffer_physical.minimum_framebuffer_height =
kernel.config.frame_buffer.minimum_framebuffer_height;
}
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_width.is_none() {
config.frame_buffer.minimum_framebuffer_width =
if config
.frame_buffer_physical
.minimum_framebuffer_width
.is_none()
{
config.frame_buffer_physical.minimum_framebuffer_width =
kernel.config.frame_buffer.minimum_framebuffer_width;
}
let framebuffer = init_logger(image, &st, &config);
Expand Down Expand Up @@ -166,6 +175,7 @@
},
ramdisk_addr,
ramdisk_len,
rt_table_addr: Some(system_table.get_current_system_table_addr()),
};

bootloader_x86_64_common::load_and_switch_to_kernel(
Expand Down Expand Up @@ -210,7 +220,7 @@

fn load_file_from_boot_method(
image: Handle,
st: &mut SystemTable<Boot>,

Check warning on line 223 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

this argument is a mutable reference, but not used mutably
filename: &str,
boot_mode: BootMode,
) -> Option<&'static mut [u8]> {
Expand Down Expand Up @@ -472,25 +482,82 @@

let mode = {
let modes = gop.modes();
match (
config
.frame_buffer
.minimum_framebuffer_height
.map(|v| usize::try_from(v).unwrap()),
config
.frame_buffer
.minimum_framebuffer_width
.map(|v| usize::try_from(v).unwrap()),
) {
(Some(height), Some(width)) => modes
.filter(|m| {
let res = m.info().resolution();
res.1 >= height && res.0 >= width
})
.last(),
(Some(height), None) => modes.filter(|m| m.info().resolution().1 >= height).last(),
(None, Some(width)) => modes.filter(|m| m.info().resolution().0 >= width).last(),
_ => None,

if let Some(hypervisor) = CpuId::new().get_hypervisor_info() {
if let Hypervisor::Xen = hypervisor.identify() {
// Use same rules as real hardware since Xen uses the whole screen
Comment on lines +486 to +488

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit puzzled about what this PR is doing.

I often use VMWare and QEMU/KVM for running guest VMs as I would a host, especially when enabling paravirtualization features for performance similar to host system. These guests may run windowed or fullscreen.


It's not too concerning for me as this is only applicable to a bootloader based on this project, but the motivation seems focused on QEMU windowed for development/testing purposes?

As a user, if I have a VM guest fullscreen on a display(s), I'd find this different implicit behaviour a bit confusing, especially since there's an exception made for Xen.

I don't think you find GRUB, rEFInd, or systemd-boot handling resolution used differently? I believe they have a config setting if you want to explicitly prefer a resolution or scaling/fit?

I'm not that familiar with the project, but is this just a default with a way to opt-out? Is there actual value in a physical + virtual framebuffer structs that this PR introduces for this distinction?

Or would it be better to match what other bootloader/managers do, and just provide a default with build (or runtime) config to have the scaling behaviour you prefer?

You could then more easily build for your VM testing, with a different config for production builds on real hardware (or VM guests). The detection to switch based on environment if desired beyond testing may be better served as an opt-in feature/config?


If you disagree, no worries 👍

Maybe consider documenting the behaviour then so it's easier to troubleshoot when a dev/user encounters it and tries to understand why it scales differently on most hypervisors.

match (
config
.frame_buffer_physical
.minimum_framebuffer_height
.map(|v| usize::try_from(v).unwrap()),
config
.frame_buffer_physical
.minimum_framebuffer_width
.map(|v| usize::try_from(v).unwrap()),
) {
(Some(height), Some(width)) => modes
.filter(|m| {
let res = m.info().resolution();
res.1 >= height && res.0 >= width
})
.last(),
(Some(height), None) => {
modes.filter(|m| m.info().resolution().1 >= height).last()
}
(None, Some(width)) => {
modes.filter(|m| m.info().resolution().0 >= width).last()
}
_ => None,
}
} else {
match (
config
.frame_buffer_virtual
.minimum_framebuffer_height
.map(|v| usize::try_from(v).unwrap()),
config
.frame_buffer_virtual
.minimum_framebuffer_width
.map(|v| usize::try_from(v).unwrap()),
) {
(Some(height), Some(width)) => modes
.filter(|m| {
let res = m.info().resolution();
res.1 >= height && res.0 >= width
})
.last(),
(Some(height), None) => {
modes.filter(|m| m.info().resolution().1 >= height).last()
}
(None, Some(width)) => {
modes.filter(|m| m.info().resolution().0 >= width).last()
}
_ => None,
}
}
} else {
// On real hardware; set rules accordingly
match (
config
.frame_buffer_physical
.minimum_framebuffer_height
.map(|v| usize::try_from(v).unwrap()),
config
.frame_buffer_physical
.minimum_framebuffer_width
.map(|v| usize::try_from(v).unwrap()),
) {
(Some(height), Some(width)) => modes
.filter(|m| {
let res = m.info().resolution();
res.1 >= height && res.0 >= width
})
.last(),
(Some(height), None) => modes.filter(|m| m.info().resolution().1 >= height).last(),
(None, Some(width)) => modes.filter(|m| m.info().resolution().0 >= width).last(),
_ => None,
}
}
};
if let Some(mode) = mode {
Expand Down
Loading