Skip to content

Commit a52890b

Browse files
cpercivaaljimenezb
authored andcommitted
pvh/arch: Introduce EntryPoint struct
In order to properly configure the initial vCPU register state and boot parameters in guest memory, we must specify which boot protocol to use with the kernel entry point address. On x86-64 (the only architecture where multiple boot protocols are supported) we print the protocol used to load the kernel at the debug log level. Create an EntryPoint struct that contains the required information. This structure will later be used in the vCPU configuration methods to set the appropriate initial conditions for the guest. This commit also splits the load_kernel function into an x86-64 specific version and an aarch64 specific version. Signed-off-by: Colin Percival <cperciva@freebsd.org> Co-authored-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
1 parent bcfefab commit a52890b

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/vmm/src/arch/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::fmt;
55

66
use serde::{Deserialize, Serialize};
7+
use vm_memory::GuestAddress;
78

89
/// Module for aarch64 related functionality.
910
#[cfg(target_arch = "aarch64")]
@@ -60,3 +61,34 @@ impl fmt::Display for DeviceType {
6061
write!(f, "{:?}", self)
6162
}
6263
}
64+
65+
/// Suported boot protocols for
66+
#[derive(Debug, Copy, Clone, PartialEq)]
67+
pub enum BootProtocol {
68+
/// Linux 64-bit boot protocol
69+
LinuxBoot,
70+
#[cfg(target_arch = "x86_64")]
71+
/// PVH boot protocol (x86/HVM direct boot ABI)
72+
PvhBoot,
73+
}
74+
75+
impl fmt::Display for BootProtocol {
76+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
77+
match self {
78+
BootProtocol::LinuxBoot => write!(f, "Linux 64-bit boot protocol"),
79+
#[cfg(target_arch = "x86_64")]
80+
BootProtocol::PvhBoot => write!(f, "PVH boot protocol"),
81+
}
82+
}
83+
}
84+
85+
#[derive(Debug, Copy, Clone)]
86+
/// Specifies the entry point address where the guest must start
87+
/// executing code, as well as which boot protocol is to be used
88+
/// to configure the guest initial state.
89+
pub struct EntryPoint {
90+
/// Address in guest memory where the guest must start execution
91+
pub entry_addr: GuestAddress,
92+
/// Specifies which boot protocol to use
93+
pub protocol: BootProtocol,
94+
}

src/vmm/src/builder.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use libc::EFD_NONBLOCK;
1616
use linux_loader::cmdline::Cmdline as LoaderKernelCmdline;
1717
#[cfg(target_arch = "x86_64")]
1818
use linux_loader::loader::elf::Elf as Loader;
19+
#[cfg(target_arch = "x86_64")]
20+
use linux_loader::loader::elf::PvhBootCapability;
1921
#[cfg(target_arch = "aarch64")]
2022
use linux_loader::loader::pe::PE as Loader;
2123
use linux_loader::loader::KernelLoader;
@@ -30,7 +32,7 @@ use vmm_sys_util::eventfd::EventFd;
3032

3133
#[cfg(target_arch = "x86_64")]
3234
use crate::acpi;
33-
use crate::arch::InitrdConfig;
35+
use crate::arch::{BootProtocol, EntryPoint, InitrdConfig};
3436
#[cfg(target_arch = "aarch64")]
3537
use crate::construct_kvm_mpidrs;
3638
use crate::cpu_config::templates::{
@@ -266,7 +268,7 @@ pub fn build_microvm_for_boot(
266268
.allocate_guest_memory()
267269
.map_err(StartMicrovmError::GuestMemory)?;
268270

269-
let entry_addr = load_kernel(boot_config, &guest_memory)?;
271+
let entry_point = load_kernel(boot_config, &guest_memory)?;
270272
let initrd = load_initrd_from_config(boot_config, &guest_memory)?;
271273
// Clone the command-line so that a failed boot doesn't pollute the original.
272274
#[allow(unused_mut)]
@@ -338,7 +340,7 @@ pub fn build_microvm_for_boot(
338340
vcpus.as_mut(),
339341
&vm_resources.vm_config,
340342
&cpu_template,
341-
entry_addr,
343+
entry_point.entry_addr,
342344
&initrd,
343345
boot_cmdline,
344346
)?;
@@ -570,13 +572,12 @@ pub fn build_microvm_from_snapshot(
570572
fn load_kernel(
571573
boot_config: &BootConfig,
572574
guest_memory: &GuestMemoryMmap,
573-
) -> Result<GuestAddress, StartMicrovmError> {
575+
) -> Result<EntryPoint, StartMicrovmError> {
574576
let mut kernel_file = boot_config
575577
.kernel_file
576578
.try_clone()
577579
.map_err(|err| StartMicrovmError::Internal(VmmError::KernelFile(err)))?;
578580

579-
#[cfg(target_arch = "x86_64")]
580581
let entry_addr = Loader::load::<std::fs::File, GuestMemoryMmap>(
581582
guest_memory,
582583
None,
@@ -585,7 +586,32 @@ fn load_kernel(
585586
)
586587
.map_err(StartMicrovmError::KernelLoader)?;
587588

588-
#[cfg(target_arch = "aarch64")]
589+
let mut entry_point_addr: GuestAddress = entry_addr.kernel_load;
590+
let mut boot_prot: BootProtocol = BootProtocol::LinuxBoot;
591+
if let PvhBootCapability::PvhEntryPresent(pvh_entry_addr) = entry_addr.pvh_boot_cap {
592+
// Use the PVH kernel entry point to boot the guest
593+
entry_point_addr = pvh_entry_addr;
594+
boot_prot = BootProtocol::PvhBoot;
595+
}
596+
597+
debug!("Kernel loaded using {boot_prot}");
598+
599+
Ok(EntryPoint {
600+
entry_addr: entry_point_addr,
601+
protocol: boot_prot,
602+
})
603+
}
604+
605+
#[cfg(target_arch = "aarch64")]
606+
fn load_kernel(
607+
boot_config: &BootConfig,
608+
guest_memory: &GuestMemoryMmap,
609+
) -> Result<EntryPoint, StartMicrovmError> {
610+
let mut kernel_file = boot_config
611+
.kernel_file
612+
.try_clone()
613+
.map_err(|err| StartMicrovmError::Internal(VmmError::KernelFile(err)))?;
614+
589615
let entry_addr = Loader::load::<std::fs::File, GuestMemoryMmap>(
590616
guest_memory,
591617
Some(GuestAddress(crate::arch::get_kernel_start())),
@@ -594,7 +620,10 @@ fn load_kernel(
594620
)
595621
.map_err(StartMicrovmError::KernelLoader)?;
596622

597-
Ok(entry_addr.kernel_load)
623+
Ok(EntryPoint {
624+
entry_addr: entry_addr.kernel_load,
625+
protocol: BootProtocol::LinuxBoot,
626+
})
598627
}
599628

600629
fn load_initrd_from_config(

0 commit comments

Comments
 (0)