Skip to content

Commit 97476d0

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 1644b3c commit 97476d0

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
@@ -68,3 +68,32 @@ impl fmt::Display for DeviceType {
6868
write!(f, "{:?}", self)
6969
}
7070
}
71+
72+
/// Suported boot protocols for
73+
#[derive(Debug, Copy, Clone)]
74+
pub enum BootProtocol {
75+
/// Linux 64-bit boot protocol
76+
LinuxBoot,
77+
/// PVH boot protocol (x86/HVM direct boot ABI)
78+
PvhBoot,
79+
}
80+
81+
impl fmt::Display for BootProtocol {
82+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
83+
match self {
84+
BootProtocol::LinuxBoot => write!(f, "Linux 64-bit boot protocol"),
85+
BootProtocol::PvhBoot => write!(f, "PVH boot protocol"),
86+
}
87+
}
88+
}
89+
90+
#[derive(Debug, Copy, Clone)]
91+
/// Specifies the entry point address where the guest must start
92+
/// executing code, as well as which boot protocol is to be used
93+
/// to configure the guest initial state.
94+
pub struct EntryPoint {
95+
/// Address in guest memory where the guest must start execution
96+
pub entry_addr: vm_memory::GuestAddress,
97+
/// Specifies which boot protocol to use
98+
pub protocol: BootProtocol,
99+
}

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)]
@@ -383,7 +384,7 @@ pub fn build_microvm_for_boot(
383384
&vmm,
384385
vcpus.as_mut(),
385386
vcpu_config,
386-
entry_addr,
387+
entry_point.entry_addr,
387388
&initrd,
388389
boot_cmdline,
389390
)?;
@@ -612,7 +613,7 @@ pub fn create_guest_memory(
612613
fn load_kernel(
613614
boot_config: &BootConfig,
614615
guest_memory: &GuestMemoryMmap,
615-
) -> std::result::Result<GuestAddress, StartMicrovmError> {
616+
) -> std::result::Result<EntryPoint, StartMicrovmError> {
616617
let mut kernel_file = boot_config
617618
.kernel_file
618619
.try_clone()
@@ -636,7 +637,21 @@ fn load_kernel(
636637
)
637638
.map_err(StartMicrovmError::KernelLoader)?;
638639

639-
Ok(entry_addr.kernel_load)
640+
let mut entry_point_addr: GuestAddress = entry_addr.kernel_load;
641+
let mut boot_prot: BootProtocol = BootProtocol::LinuxBoot;
642+
643+
if cfg!(target_arch = "x86_64") {
644+
if let PvhBootCapability::PvhEntryPresent(pvh_entry_addr) = entry_addr.pvh_boot_cap {
645+
// Use the PVH kernel entry point to boot the guest
646+
entry_point_addr = pvh_entry_addr;
647+
boot_prot = BootProtocol::PvhBoot;
648+
}
649+
}
650+
651+
Ok(EntryPoint {
652+
entry_addr: entry_point_addr,
653+
protocol: boot_prot,
654+
})
640655
}
641656

642657
fn load_initrd_from_config(

0 commit comments

Comments
 (0)