Skip to content

Commit e0b0f71

Browse files
cpercivaaljimenezb
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. 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. Signed-off-by: Colin Percival <cperciva@freebsd.org> Co-authored-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
1 parent cea28a0 commit e0b0f71

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/arch/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,32 @@ impl fmt::Display for DeviceType {
6565
write!(f, "{:?}", self)
6666
}
6767
}
68+
69+
/// Suported boot protocols for
70+
#[derive(Debug, Copy, Clone)]
71+
pub enum BootProtocol {
72+
/// Linux 64-bit boot protocol
73+
LinuxBoot,
74+
/// PVH boot protocol (x86/HVM direct boot ABI)
75+
PvhBoot,
76+
}
77+
78+
impl fmt::Display for BootProtocol {
79+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
80+
match self {
81+
BootProtocol::LinuxBoot => write!(f, "Linux 64-bit boot protocol"),
82+
BootProtocol::PvhBoot => write!(f, "PVH boot protocol"),
83+
}
84+
}
85+
}
86+
87+
#[derive(Debug, Copy, Clone)]
88+
/// Specifies the entry point address where the guest must start
89+
/// executing code, as well as which boot protocol is to be used
90+
/// to configure the guest initial state.
91+
pub struct EntryPoint {
92+
/// Address in guest memory where the guest must start execution
93+
pub entry_addr: vm_memory::GuestAddress,
94+
/// Specifies which boot protocol to use
95+
pub protocol: BootProtocol,
96+
}

src/vmm/src/builder.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::{self, Read, Seek, SeekFrom};
99
use std::os::unix::io::{AsRawFd, RawFd};
1010
use std::sync::{Arc, Mutex};
1111

12-
use arch::InitrdConfig;
12+
use arch::{BootProtocol, EntryPoint, InitrdConfig};
1313
#[cfg(target_arch = "x86_64")]
1414
use cpuid::common::is_same_model;
1515
use devices::legacy::serial::ReadableFd;
@@ -22,6 +22,7 @@ use libc::EFD_NONBLOCK;
2222
use linux_loader::cmdline::Cmdline as LoaderKernelCmdline;
2323
#[cfg(target_arch = "x86_64")]
2424
use linux_loader::loader::elf::Elf as Loader;
25+
use linux_loader::loader::elf::PvhBootCapability;
2526
#[cfg(target_arch = "aarch64")]
2627
use linux_loader::loader::pe::PE as Loader;
2728
use linux_loader::loader::KernelLoader;
@@ -334,7 +335,7 @@ pub fn build_microvm_for_boot(
334335
let guest_memory =
335336
create_guest_memory(vm_resources.vm_config().mem_size_mib, track_dirty_pages)?;
336337
let vcpu_config = vm_resources.vcpu_config();
337-
let entry_addr = load_kernel(boot_config, &guest_memory)?;
338+
let entry_point = load_kernel(boot_config, &guest_memory)?;
338339
let initrd = load_initrd_from_config(boot_config, &guest_memory)?;
339340
// Clone the command-line so that a failed boot doesn't pollute the original.
340341
#[allow(unused_mut)]
@@ -407,7 +408,7 @@ pub fn build_microvm_for_boot(
407408
&vmm,
408409
vcpus.as_mut(),
409410
vcpu_config,
410-
entry_addr,
411+
entry_point.entry_addr,
411412
&initrd,
412413
boot_cmdline,
413414
)?;
@@ -636,7 +637,7 @@ pub fn create_guest_memory(
636637
fn load_kernel(
637638
boot_config: &BootConfig,
638639
guest_memory: &GuestMemoryMmap,
639-
) -> std::result::Result<GuestAddress, StartMicrovmError> {
640+
) -> std::result::Result<EntryPoint, StartMicrovmError> {
640641
let mut kernel_file = boot_config
641642
.kernel_file
642643
.try_clone()
@@ -660,7 +661,21 @@ fn load_kernel(
660661
)
661662
.map_err(StartMicrovmError::KernelLoader)?;
662663

663-
Ok(entry_addr.kernel_load)
664+
let mut entry_point_addr: GuestAddress = entry_addr.kernel_load;
665+
let mut boot_prot: BootProtocol = BootProtocol::LinuxBoot;
666+
667+
if cfg!(target_arch = "x86_64") {
668+
if let PvhBootCapability::PvhEntryPresent(pvh_entry_addr) = entry_addr.pvh_boot_cap {
669+
// Use the PVH kernel entry point to boot the guest
670+
entry_point_addr = pvh_entry_addr;
671+
boot_prot = BootProtocol::PvhBoot;
672+
}
673+
}
674+
675+
Ok(EntryPoint {
676+
entry_addr: entry_point_addr,
677+
protocol: boot_prot,
678+
})
664679
}
665680

666681
fn load_initrd_from_config(

0 commit comments

Comments
 (0)