kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Question on rdtsc instruction in guest
@ 2020-01-16 21:39 Spoorti Doddamani
  2020-01-16 23:44 ` Sean Christopherson
  0 siblings, 1 reply; 2+ messages in thread
From: Spoorti Doddamani @ 2020-01-16 21:39 UTC (permalink / raw)
  To: kvm

Hi,

I would like to know how rdtsc instruction works when executed in
guest. The guest uses kvm-clock for timekeeping. The tsc_timestamp
field in the shared page between the hypervisor and guest is updated
constantly by the hypervisor. Does rdtsc instruction, when executed in
guest, read the value from this shared page? Or does it read the
hardware TSC MSR register? If it reads from the hardware TSC MSR
register why do I observe different values of TSC when executed in
host and guest? Or is the instruction emulated by the hypervisor?



Thanks

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Question on rdtsc instruction in guest
  2020-01-16 21:39 Question on rdtsc instruction in guest Spoorti Doddamani
@ 2020-01-16 23:44 ` Sean Christopherson
  0 siblings, 0 replies; 2+ messages in thread
From: Sean Christopherson @ 2020-01-16 23:44 UTC (permalink / raw)
  To: Spoorti Doddamani; +Cc: kvm

On Thu, Jan 16, 2020 at 04:39:47PM -0500, Spoorti Doddamani wrote:
> Hi,
> 
> I would like to know how rdtsc instruction works when executed in
> guest. The guest uses kvm-clock for timekeeping. The tsc_timestamp
> field in the shared page between the hypervisor and guest is updated
> constantly by the hypervisor. Does rdtsc instruction, when executed in
> guest, read the value from this shared page? Or does it read the
> hardware TSC MSR register? If it reads from the hardware TSC MSR
> register why do I observe different values of TSC when executed in
> host and guest? Or is the instruction emulated by the hypervisor?

On KVM, RDTSC accesses hardware directly.  A VMM *can* configure RDTSC to
VM-Exit, e.g. to emulate it, but KVM does not do so (unless you get into
nested virtualization scenarios).

As to why the guest sees a different value, hardware supports a virtual
TSC via a TSC offset mechanism, i.e. RDTSC returns the "real" TSC plus an
arbitrary value (the offset) controlled by the VMM (KVM).  This allows KVM
to virtualize the TSC and expose RDTSC to the guest (as opposed to
emulating RDTSC).

Recent CPUs also support TSC scaling, which essentially allows the virtual
TSC to count at a different frequency than the real TSC.  This is used to
migrate VMs between physical systems with different TSC frequencies.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-01-16 23:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-16 21:39 Question on rdtsc instruction in guest Spoorti Doddamani
2020-01-16 23:44 ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).