All of lore.kernel.org
 help / color / mirror / Atom feed
From: Uros Bizjak <ubizjak@gmail.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Uros Bizjak <ubizjak@gmail.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>
Subject: [PATCH] KVM: x86: Improve exception safe wrappers in emulate.c
Date: Fri, 17 Sep 2021 15:51:52 +0200	[thread overview]
Message-ID: <20210917135152.5111-1-ubizjak@gmail.com> (raw)

Improve exception safe wrappers in emulate.c by converting them to
ASM GOTO (and ASM GOTO OUTPUT when supported) statements.  Also, convert
wrappers to inline functions to avoid statement as expression
GNU extension and to remove weird requirement where user must know
where the asm argument is being expanded.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson  <seanjc@google.com>
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
---
 arch/x86/kvm/emulate.c | 80 ++++++++++++++++++++++++++++++------------
 1 file changed, 57 insertions(+), 23 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 2837110e66ed..2197a3ecc55b 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -464,25 +464,59 @@ FOP_FUNC(salc)
 FOP_RET(salc)
 FOP_END;
 
-/*
- * XXX: inoutclob user must know where the argument is being expanded.
- *      Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
- */
-#define asm_safe(insn, inoutclob...) \
-({ \
-	int _fault = 0; \
- \
-	asm volatile("1:" insn "\n" \
-	             "2:\n" \
-	             ".pushsection .fixup, \"ax\"\n" \
-	             "3: movl $1, %[_fault]\n" \
-	             "   jmp  2b\n" \
-	             ".popsection\n" \
-	             _ASM_EXTABLE(1b, 3b) \
-	             : [_fault] "+qm"(_fault) inoutclob ); \
- \
-	_fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
-})
+static __always_inline int safe_fwait(void)
+{
+	asm_volatile_goto("1: fwait\n\t"
+			  _ASM_EXTABLE(1b, %l[fault])
+			  : : : : fault);
+	return X86EMUL_CONTINUE;
+ fault:
+	return X86EMUL_UNHANDLEABLE;
+}
+
+static __always_inline int safe_fxrstor(struct fxregs_state *fx_state)
+{
+	asm_volatile_goto("1: fxrstor %0\n\t"
+			  _ASM_EXTABLE(1b, %l[fault])
+			  : : "m" (*fx_state) : : fault);
+	return X86EMUL_CONTINUE;
+ fault:
+	return X86EMUL_UNHANDLEABLE;
+}
+
+#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
+
+static __always_inline int safe_fxsave(struct fxregs_state *fx_state)
+{
+	asm_volatile_goto("1: fxsave %0\n\t"
+			  _ASM_EXTABLE(1b, %l[fault])
+			  : "=m" (*fx_state) : : : fault);
+	return X86EMUL_CONTINUE;
+ fault:
+	return X86EMUL_UNHANDLEABLE;
+}
+
+#else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
+
+static __always_inline int safe_fxsave(struct fxregs_state *fx_state)
+{
+	int rc;
+
+	asm volatile("1: fxsave %0\n\t"
+		     "movl %2, %1\n\t"
+		     "2:\n\t"
+	             ".pushsection .fixup, \"ax\"\n\t"
+	             "3: movl %3, %1\n\t"
+	             "jmp 2b\n\t"
+	             ".popsection\n\t"
+	             _ASM_EXTABLE(1b, 3b)
+	             : "=m" (*fx_state), "=rm" (rc)
+		     : "i" (X86EMUL_CONTINUE),
+		       "i" (X86EMUL_UNHANDLEABLE));
+	return rc;
+}
+
+#endif // CONFIG_CC_ASM_GOTO_OUTPUT
 
 static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 				    enum x86_intercept intercept,
@@ -4030,7 +4064,7 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt)
 
 	kvm_fpu_get();
 
-	rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
+	rc = safe_fxsave (&fx_state);
 
 	kvm_fpu_put();
 
@@ -4054,7 +4088,7 @@ static noinline int fxregs_fixup(struct fxregs_state *fx_state,
 	struct fxregs_state fx_tmp;
 	int rc;
 
-	rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
+	rc = safe_fxsave (&fx_tmp);
 	memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
 	       __fxstate_size(16) - used_size);
 
@@ -4090,7 +4124,7 @@ static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
 	}
 
 	if (rc == X86EMUL_CONTINUE)
-		rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
+		rc = safe_fxrstor (&fx_state);
 
 out:
 	kvm_fpu_put();
@@ -5342,7 +5376,7 @@ static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
 	int rc;
 
 	kvm_fpu_get();
-	rc = asm_safe("fwait");
+	rc = safe_fwait();
 	kvm_fpu_put();
 
 	if (unlikely(rc != X86EMUL_CONTINUE))
-- 
2.31.1


             reply	other threads:[~2021-09-17 13:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-17 13:51 Uros Bizjak [this message]
2021-10-13 19:24 ` [PATCH] KVM: x86: Improve exception safe wrappers in emulate.c Sean Christopherson
2021-11-01  9:59   ` Uros Bizjak

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=20210917135152.5111-1-ubizjak@gmail.com \
    --to=ubizjak@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.