public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Yifei Ma <yifei@clockwork.io>
Cc: kvm@vger.kernel.org
Subject: Re: Questions about TSC virtualization in KVM
Date: Tue, 24 Oct 2023 10:33:12 -0700	[thread overview]
Message-ID: <ZTf_2MN3uyHFtWqa@google.com> (raw)
In-Reply-To: <594A322A-8100-429A-A3E8-64362E3ED5A2@clockwork.io>

On Tue, Oct 24, 2023, Yifei Ma wrote:
> Hi KVM community,
> 
>    I am trying to figure out how TSC is virtualized in KVM-VMX world.
>    According to the kernel documentation, reading TSC register through MSR
>    can be trapped into KVM and VMX. I am trying to figure out the KVM code
>    handing this trap.

Key word "can".  KVM chooses not to intercept RDMSR to MSR_IA32_TSC because
hardware handles the necessary offset and scaling.  KVM does still emulate reads
in kvm_get_msr_common(), e.g. if KVM is forced to emulate a RDMSR, but that's a
very, very uncommon path.

Ditto for the RDTSC instruction, which isn't subject to MSR intercpetion bitmaps
and has a dedicated control.  KVM will emulate RDTSC if KVM is already emulating,
but otherwise the guest can execute RDTSC without triggering a VM-Exit.

Modern CPUs provide both a offset and a scaling factor for VMX guests, i.e. the
CPU itself virtualizes guest TSC.  See the RDMSR and RDTSC bullet points in the
"CHANGES TO INSTRUCTION BEHAVIOR IN VMX NON-ROOT OPERATION" section of the SDM
for details.

>    In order to understand it, I have run a kernel traced by GDB, and added
>    break points to the code I thought they may handle the MSR trap, e.g.,
>    kvm_get_msr, vmx_exec_control, etc. Then ran rdtsc from guest application,
>    however, it  didn’t trigger these breakpoints. I am a little lost in how
>    TSC is virtualized.
>
>    Two questions:
>    - does the TSC MRS instructions are emulated and trapped into KVM?

Nope, see above.

>    - if TSC is trapped, which code handles it?

Also see above :-)

> Any background about TSC virtualization and suggestions on tracing its
> virtualization are appreciated.

  reply	other threads:[~2023-10-24 17:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-24 17:15 Questions about TSC virtualization in KVM Yifei Ma
2023-10-24 17:33 ` Sean Christopherson [this message]
2023-10-25 23:11   ` Yifei Ma
2023-10-26 13:35     ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZTf_2MN3uyHFtWqa@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=yifei@clockwork.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox