Skip to content

Commit c414f1b

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 <[email protected]> Co-authored-by: Alejandro Jimenez <[email protected]>
1 parent 1e1d8ae commit c414f1b

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
@@ -6,6 +6,7 @@ use std::sync::LazyLock;
66

77
use log::warn;
88
use serde::{Deserialize, Serialize};
9+
use vm_memory::GuestAddress;
910

1011
/// Module for aarch64 related functionality.
1112
#[cfg(target_arch = "aarch64")]
@@ -77,3 +78,34 @@ impl fmt::Display for DeviceType {
7778
write!(f, "{:?}", self)
7879
}
7980
}
81+
82+
/// Suported boot protocols for
83+
#[derive(Debug, Copy, Clone, PartialEq)]
84+
pub enum BootProtocol {
85+
/// Linux 64-bit boot protocol
86+
LinuxBoot,
87+
#[cfg(target_arch = "x86_64")]
88+
/// PVH boot protocol (x86/HVM direct boot ABI)
89+
PvhBoot,
90+
}
91+
92+
impl fmt::Display for BootProtocol {
93+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
94+
match self {
95+
BootProtocol::LinuxBoot => write!(f, "Linux 64-bit boot protocol"),
96+
#[cfg(target_arch = "x86_64")]
97+
BootProtocol::PvhBoot => write!(f, "PVH boot protocol"),
98+
}
99+
}
100+
}
101+
102+
#[derive(Debug, Copy, Clone)]
103+
/// Specifies the entry point address where the guest must start
104+
/// executing code, as well as which boot protocol is to be used
105+
/// to configure the guest initial state.
106+
pub struct EntryPoint {
107+
/// Address in guest memory where the guest must start execution
108+
pub entry_addr: GuestAddress,
109+
/// Specifies which boot protocol to use
110+
pub protocol: BootProtocol,
111+
}

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;
@@ -29,7 +31,7 @@ use vmm_sys_util::eventfd::EventFd;
2931

3032
#[cfg(target_arch = "x86_64")]
3133
use crate::acpi;
32-
use crate::arch::InitrdConfig;
34+
use crate::arch::{BootProtocol, EntryPoint, InitrdConfig};
3335
#[cfg(target_arch = "aarch64")]
3436
use crate::construct_kvm_mpidrs;
3537
use crate::cpu_config::templates::{
@@ -256,7 +258,7 @@ pub fn build_microvm_for_boot(
256258
.allocate_guest_memory()
257259
.map_err(StartMicrovmError::GuestMemory)?;
258260

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)]
@@ -330,7 +332,7 @@ pub fn build_microvm_for_boot(
330332
vcpus.as_mut(),
331333
&vm_resources.machine_config,
332334
&cpu_template,
333-
entry_addr,
335+
entry_point.entry_addr,
334336
&initrd,
335337
boot_cmdline,
336338
)?;
@@ -562,13 +564,12 @@ pub fn build_microvm_from_snapshot(
562564
fn load_kernel(
563565
boot_config: &BootConfig,
564566
guest_memory: &GuestMemoryMmap,
565-
) -> Result<GuestAddress, StartMicrovmError> {
567+
) -> Result<EntryPoint, StartMicrovmError> {
566568
let mut kernel_file = boot_config
567569
.kernel_file
568570
.try_clone()
569571
.map_err(|err| StartMicrovmError::Internal(VmmError::KernelFile(err)))?;
570572

571-
#[cfg(target_arch = "x86_64")]
572573
let entry_addr = Loader::load::<std::fs::File, GuestMemoryMmap>(
573574
guest_memory,
574575
None,
@@ -577,7 +578,32 @@ fn load_kernel(
577578
)
578579
.map_err(StartMicrovmError::KernelLoader)?;
579580

580-
#[cfg(target_arch = "aarch64")]
581+
let mut entry_point_addr: GuestAddress = entry_addr.kernel_load;
582+
let mut boot_prot: BootProtocol = BootProtocol::LinuxBoot;
583+
if let PvhBootCapability::PvhEntryPresent(pvh_entry_addr) = entry_addr.pvh_boot_cap {
584+
// Use the PVH kernel entry point to boot the guest
585+
entry_point_addr = pvh_entry_addr;
586+
boot_prot = BootProtocol::PvhBoot;
587+
}
588+
589+
debug!("Kernel loaded using {boot_prot}");
590+
591+
Ok(EntryPoint {
592+
entry_addr: entry_point_addr,
593+
protocol: boot_prot,
594+
})
595+
}
596+
597+
#[cfg(target_arch = "aarch64")]
598+
fn load_kernel(
599+
boot_config: &BootConfig,
600+
guest_memory: &GuestMemoryMmap,
601+
) -> Result<EntryPoint, StartMicrovmError> {
602+
let mut kernel_file = boot_config
603+
.kernel_file
604+
.try_clone()
605+
.map_err(|err| StartMicrovmError::Internal(VmmError::KernelFile(err)))?;
606+
581607
let entry_addr = Loader::load::<std::fs::File, GuestMemoryMmap>(
582608
guest_memory,
583609
Some(GuestAddress(crate::arch::get_kernel_start())),
@@ -586,7 +612,10 @@ fn load_kernel(
586612
)
587613
.map_err(StartMicrovmError::KernelLoader)?;
588614

589-
Ok(entry_addr.kernel_load)
615+
Ok(EntryPoint {
616+
entry_addr: entry_addr.kernel_load,
617+
protocol: BootProtocol::LinuxBoot,
618+
})
590619
}
591620

592621
fn load_initrd_from_config(

0 commit comments

Comments
 (0)