From: Arnabjyoti Kalita <akalita@cs.stonybrook.edu>
To: Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org
Subject: Re: Intercepting RDTSC instruction by causing a VMEXIT
Date: Tue, 27 Apr 2021 07:49:04 +0530 [thread overview]
Message-ID: <CAJGDS+EjtHmV7fdQmtHfiRG3uywd5=XbFb_VWr-GhCpmuN+=zA@mail.gmail.com> (raw)
In-Reply-To: <YIbkxXwHPTPhN20C@google.com>
Hello Sean,
Thank you very much again. I didn't know about the tracepointing
methodology. This is very helpful, indeed.
In addition to my requirement of recording tsc values, I am trying to
track when the VMEXITs happen in userspace(QEMU). This allows me to
correlate recorded TSC values with the VMEXIT sequence. So, causing a
VMEXIT in userspace is absolutely essential to me and I would love to
have some pointers on how to do it.
I run a server within the guest and I'm okay with performance dropping
down to within a factor of 2 compared to running the server on native
hardware. I run this experiment for about 20-30 seconds.
Best Regards,
Arnabjyoti Kalita
On Mon, Apr 26, 2021 at 9:35 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Mon, Apr 26, 2021, Arnabjyoti Kalita wrote:
> > Hello Sean,
> >
> > Thank you very much for your answer again. I could actually see that the
> > RDTSC handler gets called successfully. I added a "printk" statement to the
> > handler and I can see the messages being printed out to the kernel syslog.
> >
> > However, I am not sure if a VMEXIT is happening in userspace. I use QEMU
> > and I do not see any notification from QEMU that tells me that a VMEXIT
> > happened due to RDTSC being executed. Is there a mechanism to confirm this?
>
> Without further modification, KVM will _not_ exit to userspace in this case.
>
> > My actual requirement to record tsc values read out as a result of RDTSC
> > execution still stands.
>
> Your requirement didn't clarify that userspace needed to record the values ;-)
>
> Forcing an exit to userspace is very doable, but will tank guest performance and
> possibly interfere with whatever you're trying to do. I would try adding a
> tracepoint and using that to capture the TSC values. Adding guest RIP, etc...
> as needed is trivial.
>
> diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h
> index a61c015870e3..e962e813ba04 100644
> --- a/arch/x86/kvm/trace.h
> +++ b/arch/x86/kvm/trace.h
> @@ -821,6 +821,23 @@ TRACE_EVENT(
> __entry->gpa_match ? "GPA" : "GVA")
> );
>
> +TRACE_EVENT(kvm_emulate_rdtsc,
> + TP_PROTO(unsigned int vcpu_id, __u64 tsc),
> + TP_ARGS(vcpu_id, tsc),
> +
> + TP_STRUCT__entry(
> + __field( unsigned int, vcpu_id )
> + __field( __u64, tsc )
> + ),
> +
> + TP_fast_assign(
> + __entry->vcpu_id = vcpu_id;
> + __entry->tsc = tsc;
> + ),
> +
> + TP_printk("vcpu=%u tsc=%llu", __entry->vcpu_id, __entry->tsc)
> +);
> +
> TRACE_EVENT(kvm_write_tsc_offset,
> TP_PROTO(unsigned int vcpu_id, __u64 previous_tsc_offset,
> __u64 next_tsc_offset),
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 61bf0b86770b..1fbeef520349 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -5254,6 +5254,8 @@ static int handle_rdtsc(struct kvm_vcpu *vcpu)
> {
> u64 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
>
> + trace_kvm_emulate_rdtsc(vcpu->vcpu_id, tsc);
> +
> kvm_rax_write(vcpu, tsc & -1u);
> kvm_rdx_write(vcpu, (tsc >> 32) & -1u);
> return kvm_skip_emulated_instruction(vcpu);
next prev parent reply other threads:[~2021-04-27 2:19 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-17 4:54 Intercepting RDTSC instruction by causing a VMEXIT Arnabjyoti Kalita
2021-04-19 16:46 ` Sean Christopherson
2021-04-20 2:33 ` Arnabjyoti Kalita
[not found] ` <CAJGDS+GT1mKHz6K=qHQf54S_97ym=nRP12MfO6OSEOpLYGht=A@mail.gmail.com>
2021-04-26 1:51 ` Arnabjyoti Kalita
2021-04-26 16:05 ` Sean Christopherson
2021-04-27 2:19 ` Arnabjyoti Kalita [this message]
2021-04-30 17:16 ` Arnabjyoti Kalita
2024-01-01 22:06 ` obtain the timestamp counter of physical/host machine inside the VMs Tao Lyu
2024-01-02 5:11 ` Dongli Zhang
2024-01-02 10:10 ` Tao Lyu
2024-01-02 17:53 ` Sean Christopherson
2024-01-02 18:20 ` Tao Lyu
2024-01-02 19:36 ` Dongli Zhang
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='CAJGDS+EjtHmV7fdQmtHfiRG3uywd5=XbFb_VWr-GhCpmuN+=zA@mail.gmail.com' \
--to=akalita@cs.stonybrook.edu \
--cc=kvm@vger.kernel.org \
--cc=seanjc@google.com \
/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;
as well as URLs for NNTP newsgroup(s).