Skip to content

Commit 8b622ce

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 50b9fce commit 8b622ce

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/vmm/src/arch/mod.rs

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

src/vmm/src/builder.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use libc::EFD_NONBLOCK;
1414
use linux_loader::cmdline::Cmdline as LoaderKernelCmdline;
1515
#[cfg(target_arch = "x86_64")]
1616
use linux_loader::loader::elf::Elf as Loader;
17+
#[cfg(target_arch = "x86_64")]
18+
use linux_loader::loader::elf::PvhBootCapability;
1719
#[cfg(target_arch = "aarch64")]
1820
use linux_loader::loader::pe::PE as Loader;
1921
use linux_loader::loader::KernelLoader;
@@ -28,7 +30,7 @@ use utils::vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap, ReadVolatile}
2830
use vm_superio::Rtc;
2931
use vm_superio::Serial;
3032

31-
use crate::arch::InitrdConfig;
33+
use crate::arch::{BootProtocol, EntryPoint, InitrdConfig};
3234
#[cfg(target_arch = "aarch64")]
3335
use crate::construct_kvm_mpidrs;
3436
use crate::cpu_config::templates::{
@@ -256,7 +258,7 @@ pub fn build_microvm_for_boot(
256258

257259
let track_dirty_pages = vm_resources.track_dirty_pages();
258260
let guest_memory = create_guest_memory(vm_resources.vm_config.mem_size_mib, track_dirty_pages)?;
259-
let entry_addr = load_kernel(boot_config, &guest_memory)?;
261+
let entry_point = load_kernel(boot_config, &guest_memory)?;
260262
let initrd = load_initrd_from_config(boot_config, &guest_memory)?;
261263
// Clone the command-line so that a failed boot doesn't pollute the original.
262264
#[allow(unused_mut)]
@@ -310,7 +312,7 @@ pub fn build_microvm_for_boot(
310312
&vmm,
311313
vcpus.as_mut(),
312314
&vm_resources.vm_config,
313-
entry_addr,
315+
entry_point.entry_addr,
314316
&initrd,
315317
boot_cmdline,
316318
)?;
@@ -547,7 +549,7 @@ pub fn create_guest_memory(
547549
fn load_kernel(
548550
boot_config: &BootConfig,
549551
guest_memory: &GuestMemoryMmap,
550-
) -> Result<GuestAddress, StartMicrovmError> {
552+
) -> Result<EntryPoint, StartMicrovmError> {
551553
let mut kernel_file = boot_config
552554
.kernel_file
553555
.try_clone()
@@ -571,7 +573,26 @@ fn load_kernel(
571573
)
572574
.map_err(StartMicrovmError::KernelLoader)?;
573575

574-
Ok(entry_addr.kernel_load)
576+
#[cfg(target_arch = "x86_64")]
577+
let mut entry_point_addr: GuestAddress = entry_addr.kernel_load;
578+
#[cfg(target_arch = "x86_64")]
579+
let mut boot_prot: BootProtocol = BootProtocol::LinuxBoot;
580+
#[cfg(target_arch = "x86_64")]
581+
if let PvhBootCapability::PvhEntryPresent(pvh_entry_addr) = entry_addr.pvh_boot_cap {
582+
// Use the PVH kernel entry point to boot the guest
583+
entry_point_addr = pvh_entry_addr;
584+
boot_prot = BootProtocol::PvhBoot;
585+
}
586+
587+
#[cfg(not(target_arch = "x86_64"))]
588+
let entry_point_addr: GuestAddress = entry_addr.kernel_load;
589+
#[cfg(not(target_arch = "x86_64"))]
590+
let boot_prot: BootProtocol = BootProtocol::LinuxBoot;
591+
592+
Ok(EntryPoint {
593+
entry_addr: entry_point_addr,
594+
protocol: boot_prot,
595+
})
575596
}
576597

577598
fn load_initrd_from_config(

0 commit comments

Comments
 (0)