From: Alexander Graf <agraf@suse.de>
To: kvm-ppc@vger.kernel.org
Cc: kvm@vger.kernel.org
Subject: [PATCH 08/33] KVM: PPC: Introduce emulation for unprivileged instructions
Date: Sun, 22 Jun 2014 21:23:12 +0000 [thread overview]
Message-ID: <1403472217-22263-9-git-send-email-agraf@suse.de> (raw)
In-Reply-To: <1403472217-22263-1-git-send-email-agraf@suse.de>
We want to be able to emulate instructions during phases where we can't
run the guest in guest context, such as the "critical section" phase
in our magic page helpers.
Add an emulation helper for that case that allows us to emulate both
privileged and unprivileged instructions.
Signed-off-by: Alexander Graf <agraf@suse.de>
---
arch/powerpc/include/asm/kvm_ppc.h | 1 +
arch/powerpc/kvm/emulate.c | 55 +++++++++++++++++++++++++++++++++++---
2 files changed, 52 insertions(+), 4 deletions(-)
diff --git a/arch/powerpc/include/asm/kvm_ppc.h b/arch/powerpc/include/asm/kvm_ppc.h
index ad8a1d7..b141eae 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -78,6 +78,7 @@ extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
bool data);
extern int kvmppc_emulate_instruction(struct kvm_run *run,
struct kvm_vcpu *vcpu);
+extern int kvmppc_emulate_any_instruction(struct kvm_vcpu *vcpu);
extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
diff --git a/arch/powerpc/kvm/emulate.c b/arch/powerpc/kvm/emulate.c
index c92476e..7bf247e0 100644
--- a/arch/powerpc/kvm/emulate.c
+++ b/arch/powerpc/kvm/emulate.c
@@ -209,14 +209,13 @@ static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
/* XXX Should probably auto-generate instruction decoding for a particular core
* from opcode tables in the future. */
-int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
+static int kvmppc_emulate_priv_instruction(struct kvm_vcpu *vcpu, int *advance)
{
u32 inst = kvmppc_get_last_inst(vcpu);
int rs = get_rs(inst);
int rt = get_rt(inst);
int sprn = get_sprn(inst);
enum emulation_result emulated = EMULATE_DONE;
- int advance = 1;
/* this default type might be overwritten by subcategories */
kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
@@ -232,7 +231,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
kvmppc_core_queue_program(vcpu,
vcpu->arch.shared->esr | ESR_PTR);
#endif
- advance = 0;
+ *advance = 0;
break;
case 31:
@@ -248,7 +247,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
kvmppc_core_queue_program(vcpu,
vcpu->arch.shared->esr | ESR_PTR);
#endif
- advance = 0;
+ *advance = 0;
break;
case OP_31_XOP_MFSPR:
@@ -272,6 +271,17 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
emulated = EMULATE_FAIL;
}
+ return emulated;
+}
+
+/* Emulates privileged instructions only */
+int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
+{
+ u32 inst = kvmppc_get_last_inst(vcpu);
+ enum emulation_result emulated;
+ int advance = 1;
+
+ emulated = kvmppc_emulate_priv_instruction(vcpu, &advance);
if (emulated = EMULATE_FAIL) {
emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
&advance);
@@ -294,3 +304,40 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
return emulated;
}
EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);
+
+/* Emulates privileged and non-privileged instructions */
+int kvmppc_emulate_any_instruction(struct kvm_vcpu *vcpu)
+{
+ u32 inst = kvmppc_get_last_inst(vcpu);
+ enum emulation_result emulated = EMULATE_DONE;
+ int advance = 1;
+
+ kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
+
+ /* Try non-privileged instructions */
+ switch (get_op(inst)) {
+ default:
+ emulated = EMULATE_FAIL;
+ }
+
+ /* Try privileged instructions */
+ if (emulated = EMULATE_FAIL)
+ emulated = kvmppc_emulate_priv_instruction(vcpu, &advance);
+
+ if (emulated = EMULATE_AGAIN) {
+ advance = 0;
+ } else if (emulated = EMULATE_FAIL) {
+ advance = 0;
+ printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
+ "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
+ kvmppc_core_queue_program(vcpu, 0);
+ }
+
+ trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
+
+ /* Advance past emulated instruction. */
+ if (advance)
+ kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
+
+ return emulated;
+}
--
1.8.1.4
next prev parent reply other threads:[~2014-06-22 21:23 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-22 21:23 [PATCH 00/33] KVM: PPC: Fix IRQ race in magic page code Alexander Graf
2014-06-22 21:23 ` [PATCH 01/33] KVM: PPC: Implement kvmppc_xlate for all targets Alexander Graf
2014-06-22 21:23 ` [PATCH 02/33] KVM: PPC: Move kvmppc_ld/st to common code Alexander Graf
2014-06-22 21:23 ` [PATCH 03/33] KVM: PPC: Remove kvmppc_bad_hva() Alexander Graf
2014-06-22 21:23 ` [PATCH 04/33] KVM: PPC: Propagate kvmppc_xlate errors properly Alexander Graf
2014-06-22 21:23 ` [PATCH 05/33] KVM: PPC: Use kvm_read_guest in kvmppc_ld Alexander Graf
2014-06-22 21:23 ` [PATCH 06/33] KVM: PPC: Handle magic page in kvmppc_ld/st Alexander Graf
2014-06-22 21:23 ` [PATCH 07/33] KVM: PPC: Separate loadstore emulation from priv emulation Alexander Graf
2014-06-22 21:23 ` Alexander Graf [this message]
2014-06-22 21:23 ` [PATCH 09/33] KVM: PPC: Move critical section detection to common code Alexander Graf
2014-06-22 21:23 ` [PATCH 10/33] KVM: PPC: Make critical section detection conditional Alexander Graf
2014-06-22 21:23 ` [PATCH 11/33] KVM: PPC: BookE: Use common critical section helper Alexander Graf
2014-06-22 21:23 ` [PATCH 12/33] KVM: PPC: Emulate critical sections when we hit them Alexander Graf
2014-06-22 21:23 ` [PATCH 13/33] KVM: PPC: Expose helper functions for data/inst faults Alexander Graf
2014-06-22 21:23 ` [PATCH 14/33] KVM: PPC: Add std instruction emulation Alexander Graf
2014-06-22 21:23 ` [PATCH 15/33] KVM: PPC: Add stw " Alexander Graf
2014-06-22 21:23 ` [PATCH 16/33] KVM: PPC: Add ld " Alexander Graf
2014-06-22 21:23 ` [PATCH 17/33] KVM: PPC: Add lwz " Alexander Graf
2014-06-22 21:23 ` [PATCH 18/33] KVM: PPC: Add mfcr " Alexander Graf
2014-06-22 21:23 ` [PATCH 19/33] KVM: PPC: Add addis " Alexander Graf
2014-06-22 21:23 ` [PATCH 20/33] KVM: PPC: Add ori " Alexander Graf
2014-06-22 21:23 ` [PATCH 21/33] KVM: PPC: Add and " Alexander Graf
2014-06-22 21:23 ` [PATCH 22/33] KVM: PPC: Add andi. " Alexander Graf
2014-06-22 21:23 ` [PATCH 23/33] KVM: PPC: Add or " Alexander Graf
2014-06-22 21:23 ` [PATCH 24/33] KVM: PPC: Add cmpwi/cmpdi " Alexander Graf
2014-06-22 21:23 ` [PATCH 25/33] KVM: PPC: Add bc " Alexander Graf
2014-06-22 21:23 ` [PATCH 26/33] KVM: PPC: Add mtcrf " Alexander Graf
2014-06-22 21:23 ` [PATCH 27/33] KVM: PPC: Add xor " Alexander Graf
2014-06-22 21:23 ` [PATCH 28/33] KVM: PPC: Add oris " Alexander Graf
2014-06-22 21:23 ` [PATCH 29/33] KVM: PPC: Add rldicr/rldicl/rldic " Alexander Graf
2014-06-22 21:23 ` [PATCH 30/33] KVM: PPC: Add rlwimi " Alexander Graf
2014-06-22 21:23 ` [PATCH 31/33] KVM: PPC: Add rlwinm " Alexander Graf
2014-06-22 21:23 ` [PATCH 32/33] KVM: PPC: Handle NV registers in emulated critical sections Alexander Graf
2014-06-22 21:23 ` [PATCH 33/33] KVM: PPC: Enable critical section emulation Alexander Graf
2014-06-24 18:53 ` [PATCH 00/33] KVM: PPC: Fix IRQ race in magic page code Scott Wood
2014-06-24 22:41 ` Alexander Graf
2014-06-24 23:15 ` Scott Wood
2014-06-24 23:40 ` Alexander Graf
2014-06-25 0:21 ` Scott Wood
2014-07-28 14:10 ` Alexander Graf
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=1403472217-22263-9-git-send-email-agraf@suse.de \
--to=agraf@suse.de \
--cc=kvm-ppc@vger.kernel.org \
--cc=kvm@vger.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