Skip to content

Commit ca97144

Browse files
committed
[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 ca97144

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ 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+
pub kvm_cpu_args: Option<String>,
103107
/// Path to rootfs to test against.
104108
///
105109
/// * The path is relative to `vmtest.toml`.
@@ -140,6 +144,7 @@ impl Default for Target {
140144
uefi: false,
141145
kernel: None,
142146
kernel_args: None,
147+
kvm_cpu_args: None,
143148
rootfs: Self::default_rootfs(),
144149
arch: Self::default_arch(),
145150
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ 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+
args.push(kvm_cpu_args);
261261
} else {
262262
args.push("-cpu");
263263
match arch {
@@ -365,7 +365,7 @@ fn kernel_args(
365365
kernel: &Path,
366366
arch: &str,
367367
init: &Path,
368-
additional_kargs: Option<&String>,
368+
additional_kargs: Option<&str>,
369369
) -> Vec<OsString> {
370370
let mut args = Vec::new();
371371

@@ -669,7 +669,10 @@ impl Qemu {
669669
.stderr(Stdio::piped())
670670
.arg("-serial")
671671
.arg("mon:stdio")
672-
.args(kvm_args(&target.arch))
672+
.args(kvm_args(
673+
&target.arch,
674+
target.kvm_cpu_args.as_deref().unwrap_or("host"),
675+
))
673676
.args(machine_args(&target.arch))
674677
.args(machine_protocol_args(&qmp_sock))
675678
.args(guest_agent_args(&qga_sock))
@@ -691,7 +694,7 @@ impl Qemu {
691694
kernel,
692695
&target.arch,
693696
guest_init.as_path(),
694-
target.kernel_args.as_ref(),
697+
target.kernel_args.as_deref(),
695698
));
696699
} else {
697700
panic!("Config validation should've enforced XOR");

src/vmtest.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ fn validate_config(config: &Config) -> Result<()> {
5656
);
5757
}
5858

59+
if target.kvm_cpu_args.is_none() || target.kvm_cpu_args.as_ref().unwrap().is_empty() {
60+
bail!(
61+
"Target '{}' must specify non-empty 'kvm_cpu_args'",
62+
target.name
63+
);
64+
}
65+
5966
if let Some(image) = &target.image {
6067
if image.as_os_str().is_empty() {
6168
bail!("Target '{}' has empty image path", target.name);

0 commit comments

Comments
 (0)