Skip to content

Commit 9b6098a

Browse files
Asphalttdanobi
authored andcommitted
[qemu] Add custom kvm cpu args support
When improve `bpf_get_branch_snapshot()` helper, it requires passing LBR into the VM like `-cpu,host,pmu=on,lbr-fmt=0x5`. Then, in the VM, confirm the LBR is supported: ```bash root@(none):/# dmesg | grep -i lbr [ 0.394406] Performance Events: Skylake events, 32-deep LBR, full-width counters, Intel PMU driver. ``` With this commit, the VM can be created by `vmtest -k $(make -s image_name) --kvm-cpu-args 'host,pmu=on,lbr-fmt=0x5' -`. Signed-off-by: Leon Hwang <[email protected]>
1 parent 854a802 commit 9b6098a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ pub struct Target {
100100
///
101101
/// Arguments are only valid for kernel targets.
102102
pub kernel_args: Option<String>,
103+
/// KVM -cpu arguments are only valid for KVM enabled targets.
104+
///
105+
/// Default: host
106+
#[serde(default = "Target::default_kvm_cpu_args")]
107+
pub kvm_cpu_args: Option<String>,
103108
/// Path to rootfs to test against.
104109
///
105110
/// * The path is relative to `vmtest.toml`.
@@ -130,6 +135,10 @@ impl Target {
130135
pub fn default_arch() -> String {
131136
ARCH.to_string()
132137
}
138+
/// Default kvm cpu args to use if none are specified.
139+
pub fn default_kvm_cpu_args() -> Option<String> {
140+
Some("host".into())
141+
}
133142
}
134143

135144
impl Default for Target {
@@ -140,6 +149,7 @@ impl Default for Target {
140149
uefi: false,
141150
kernel: None,
142151
kernel_args: None,
152+
kvm_cpu_args: None,
143153
rootfs: Self::default_rootfs(),
144154
arch: Self::default_arch(),
145155
qemu_command: None,

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct Args {
3737
/// Additional kernel command line arguments
3838
#[clap(long, conflicts_with = "config")]
3939
kargs: Option<String>,
40+
/// KVM -cpu arguments
41+
#[clap(long, conflicts_with = "config", default_value = "host")]
42+
kvm_cpu_args: Option<String>,
4043
/// Location of rootfs, default to host's /
4144
#[clap(short, long, conflicts_with = "config", default_value = Target::default_rootfs().into_os_string())]
4245
rootfs: PathBuf,
@@ -122,6 +125,7 @@ fn config(args: &Args) -> Result<Vmtest> {
122125
rootfs: args.rootfs.clone(),
123126
arch: args.arch.clone(),
124127
kernel_args: args.kargs.clone(),
128+
kvm_cpu_args: args.kvm_cpu_args.clone(),
125129
qemu_command: args.qemu_command.clone(),
126130
command: args.command.join(" "),
127131
vm: VMConfig::default(),

src/qemu.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,17 @@ fn guest_agent_args(sock: &Path) -> Vec<OsString> {
251251
}
252252

253253
/// Generate arguments for full KVM virtualization if host supports it
254-
fn kvm_args(arch: &str) -> Vec<&'static str> {
254+
fn kvm_args<'a>(arch: &str, kvm_cpu_args: &'a str) -> Vec<&'a str> {
255255
let mut args = Vec::new();
256256

257257
if host_supports_kvm(arch) {
258258
args.push("-enable-kvm");
259259
args.push("-cpu");
260-
args.push("host");
260+
if kvm_cpu_args.is_empty() {
261+
args.push("host");
262+
} else {
263+
args.push(kvm_cpu_args);
264+
}
261265
} else {
262266
args.push("-cpu");
263267
match arch {
@@ -365,7 +369,7 @@ fn kernel_args(
365369
kernel: &Path,
366370
arch: &str,
367371
init: &Path,
368-
additional_kargs: Option<&String>,
372+
additional_kargs: Option<&str>,
369373
) -> Vec<OsString> {
370374
let mut args = Vec::new();
371375

@@ -669,7 +673,10 @@ impl Qemu {
669673
.stderr(Stdio::piped())
670674
.arg("-serial")
671675
.arg("mon:stdio")
672-
.args(kvm_args(&target.arch))
676+
.args(kvm_args(
677+
&target.arch,
678+
target.kvm_cpu_args.as_deref().unwrap_or("host"),
679+
))
673680
.args(machine_args(&target.arch))
674681
.args(machine_protocol_args(&qmp_sock))
675682
.args(guest_agent_args(&qga_sock))
@@ -691,7 +698,7 @@ impl Qemu {
691698
kernel,
692699
&target.arch,
693700
guest_init.as_path(),
694-
target.kernel_args.as_ref(),
701+
target.kernel_args.as_deref(),
695702
));
696703
} else {
697704
panic!("Config validation should've enforced XOR");

0 commit comments

Comments
 (0)