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.