x86_64: reset msrs when using bios

Our BIOS doesn't set the TSC MSR to 0 on boot, but our hypervisors need
this to be set so they can correctly sync their TSC offsets. This
introduces a generic "reset_msrs" function where we currently reset the
TSC msr, but where we could also reset if we need to in the future.

Cherrypick from downstream branch.
Actual author: Colin Downs-Razouk <colindr@google.com>.

BUG=191981229
TEST=tested downstream

Change-Id: I17b5709411394ae48fb5d7310636636295ebfaad
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3690370
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Noah Gold <nkgold@google.com>
This commit is contained in:
Noah Gold 2022-06-03 17:37:53 -07:00 committed by Chromeos LUCI
parent 8839e1c368
commit 157bd6f04d
2 changed files with 17 additions and 1 deletions

View file

@ -795,6 +795,7 @@ impl arch::LinuxArch for X8664arch {
if has_bios {
regs::set_reset_vector(vcpu).map_err(Error::SetupRegs)?;
regs::reset_msrs(vcpu).map_err(Error::SetupMsrs)?;
return Ok(());
}

View file

@ -223,7 +223,7 @@ pub fn setup_fpu(vcpu: &dyn VcpuX86_64) -> Result<()> {
///
/// # Arguments
///
/// * `vcpu` - Structure for the vcpu that holds the vcpu fd.
/// * `vcpu` - Structure for the vcpu that holds the vcpu descriptor.
/// * `boot_ip` - Starting instruction pointer.
/// * `boot_sp` - Starting stack pointer.
/// * `boot_si` - Must point to zero page address per Linux ABI.
@ -377,6 +377,21 @@ pub fn set_reset_vector(vcpu: &dyn VcpuX86_64) -> Result<()> {
Ok(())
}
/// Configures a CPU so its MSRs are reset to their default value.
///
/// Currently only sets IA32_TSC to 0.
///
/// # Arguments
/// * `vcpu` - the VCPU to configure.
pub fn reset_msrs(vcpu: &dyn VcpuX86_64) -> Result<()> {
let msrs = vec![Register {
id: crate::msr_index::MSR_IA32_TSC,
value: 0x0,
}];
vcpu.set_msrs(&msrs).map_err(Error::MsrIoctlFailed)
}
#[cfg(test)]
mod tests {
use super::*;