public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Brijesh Singh <brijesh.singh@amd.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: <brijesh.singh@amd.com>, <kvm@vger.kernel.org>,
	thomas lendacky <thomas.lendacky@amd.com>, <rkrcmar@redhat.com>,
	<joro@8bytes.org>, <x86@kernel.org>,
	<linux-kernel@vger.kernel.org>, <mingo@redhat.com>,
	<hpa@zytor.com>, <tglx@linutronix.de>, <bp@suse.de>
Subject: Re: [PATCH v2 3/3] kvm: svm: Use the hardware provided GPA instead of page walk
Date: Wed, 14 Dec 2016 11:07:58 -0600	[thread overview]
Message-ID: <43e626f1-e9a6-cf5d-4771-c6f7ca07ec8e@amd.com> (raw)
In-Reply-To: <057d8a2e-0a3a-38d3-d9bf-9301e3eb8238@redhat.com>

Hi Paolo,

On 12/13/2016 11:09 AM, Paolo Bonzini wrote:
>
>
> On 12/12/2016 18:51, Brijesh Singh wrote:
>> As per the AMD BKDG [1] Section 2.7.1, we should not be using any of
>> these instruction for MMIO access, the behavior is undefined.
>>
>> The question is, do we really need to add logic to detect the cross-page
>> MMIO accesses and push/pop mem operations so that we pass the
>> kvm-unit-test or we should update the unit test? Like you said
>> cross-page MMIO access detection is going to be a bit tricky.
>
> Actually there is a nice trick you can do to support cross-page
> MMIO access detection:
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 37cd31645d45..754d251dc611 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4549,6 +4549,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,
>  	 */
>  	if (vcpu->arch.gpa_available &&
>  	    !emulator_is_string_op(ctxt) &&
> +	    (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK) &&
>  	    vcpu_is_mmio_gpa(vcpu, addr, exception->address, write)) {
>  		gpa = exception->address;
>  		goto mmio;
>
>
> It fixes the testcase for push/pop with two memory ops too,
> but it's not reliable, so your change for TwoMemOp is still
> necessary.  Feel free to include it in your patch!
>
> Regarding the replacement of emulator_is_string_op with
> emulator_is_two_memory_op, what about REP prefixes?  In that
> case I think that you do need to reject string ops.  So the
> function would have to reject all TwoMemOps, and REP-prefixed
> String operations.
>

Since now we are going to perform multiple conditional checks before 
concluding that its safe to use HW provided GPA. How about if we add two 
functions "emulator_is_rep_string_op" and "emulator_is_two_mem_op" into 
emulator.c and  use these functions inside the x86.c to determine if its 
safe to use HW provided gpa?

Please let me know if you are okay with this approach.

diff --git a/arch/x86/include/asm/kvm_emulate.h 
b/arch/x86/include/asm/kvm_emulate.h
index 777eea2..29e44cb 100644
--- a/arch/x86/include/asm/kvm_emulate.h
+++ b/arch/x86/include/asm/kvm_emulate.h
@@ -441,6 +441,7 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq);
  void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt);
  void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt);
-bool emulator_is_string_op(struct x86_emulate_ctxt *ctxt);
+bool emulator_is_rep_string_op(struct x86_emulate_ctxt *ctxt);
+bool emulator_is_two_mem_op(struct x86_emulate_ctxt *ctxt);

  #endif /* _ASM_X86_KVM_X86_EMULATE_H */
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 8e7d09f..16149ad 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -5485,10 +5485,12 @@ void emulator_writeback_register_cache(struct 
x86_emulate_ctxt *ctxt)
         writeback_registers(ctxt);
  }

-bool emulator_is_string_op(struct x86_emulate_ctxt *ctxt)
+bool emulator_is_rep_string_op(struct x86_emulate_ctxt *ctxt)
  {
-       if (ctxt->d & String)
-               return true;
+       return ctxt->rep_prefix && (ctxt->d & String) ? true: false;
+}

-       return false;
+bool emulator_is_two_mem_op(struct x86_emulate_ctxt *ctxt)
+{
+       return ctxt->d & TwoMemOp ? true : false;
  }
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 640527b..0bc814a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4548,6 +4548,12 @@ static const struct read_write_emulator_ops 
write_emultor = {
         .write = true,
  };

+static bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
+{
+       return emulator_is_rep_string_op(ctxt) &&
+               emulator_is_two_mem_op(ctxt) ? true : false;
+}
+
  static int emulator_read_write_onepage(unsigned long addr, void *val,
                                        unsigned int bytes,
                                        struct x86_exception *exception,
@@ -4568,7 +4574,7 @@ static int emulator_read_write_onepage(unsigned 
long addr, void *val,
          * occurred.
          */
         if (vcpu->arch.gpa_available &&
-           !emulator_is_string_op(ctxt) &&
+           !emulator_can_use_gpa(ctxt) &&
             vcpu_is_mmio_gpa(vcpu, addr, exception->address, write) &&
             (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK)) {

  reply	other threads:[~2016-12-14 17:07 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-23 17:01 [PATCH v2 0/3] x86: SVM: add additional SVM NPF error and use HW GPA Brijesh Singh
2016-11-23 17:01 ` [PATCH v2 1/3] kvm: svm: Add support for additional SVM NPF error codes Brijesh Singh
2017-07-27 16:27   ` Paolo Bonzini
2017-07-31 13:30     ` Brijesh Singh
2017-07-31 15:44       ` Paolo Bonzini
2017-07-31 16:54         ` Brijesh Singh
2017-07-31 20:05           ` Paolo Bonzini
2017-08-01 13:36             ` Brijesh Singh
2017-08-02 10:42               ` Paolo Bonzini
2017-08-04  0:30                 ` Brijesh Singh
2017-08-04 14:05                   ` Paolo Bonzini
2017-08-04 14:23                     ` Brijesh Singh
2016-11-23 17:01 ` [PATCH v2 2/3] kvm: svm: Add kvm_fast_pio_in support Brijesh Singh
2016-11-23 17:02 ` [PATCH v2 3/3] kvm: svm: Use the hardware provided GPA instead of page walk Brijesh Singh
2016-11-23 21:53   ` Paolo Bonzini
2016-12-08 14:52   ` Paolo Bonzini
2016-12-08 15:39     ` Brijesh Singh
2016-12-08 19:00       ` Brijesh Singh
2016-12-09 15:41         ` Paolo Bonzini
2016-12-12 17:51           ` Brijesh Singh
2016-12-13 17:09             ` Paolo Bonzini
2016-12-14 17:07               ` Brijesh Singh [this message]
2016-12-14 17:23                 ` Paolo Bonzini
2016-12-14 18:39                   ` Brijesh Singh
2016-12-14 18:47                     ` Paolo Bonzini
2016-11-24 20:51 ` [PATCH v2 0/3] x86: SVM: add additional SVM NPF error and use HW GPA Radim Krčmář

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=43e626f1-e9a6-cf5d-4771-c6f7ca07ec8e@amd.com \
    --to=brijesh.singh@amd.com \
    --cc=bp@suse.de \
    --cc=hpa@zytor.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /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