[PATCH 12/20] KVM/MIPS32: Routines to handle specific traps/exceptions while executing the guest.

2012-10-31 Thread Sanjay Lal
Signed-off-by: Sanjay Lal sanj...@kymasys.com
---
 arch/mips/kvm/kvm_cb.c|  16 ++
 arch/mips/kvm/kvm_trap_emul.c | 446 ++
 2 files changed, 462 insertions(+)
 create mode 100644 arch/mips/kvm/kvm_cb.c
 create mode 100644 arch/mips/kvm/kvm_trap_emul.c

diff --git a/arch/mips/kvm/kvm_cb.c b/arch/mips/kvm/kvm_cb.c
new file mode 100644
index 000..768198e
--- /dev/null
+++ b/arch/mips/kvm/kvm_cb.c
@@ -0,0 +1,16 @@
+/*
+* This file is subject to the terms and conditions of the GNU General Public
+* License.  See the file COPYING in the main directory of this archive
+* for more details.
+*
+* PUT YOUR TITLE AND/OR INFORMATION FOR THE FILE HERE
+*
+* Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
+* Authors: Yann Le Du l...@kymasys.com
+*/
+
+#include linux/export.h
+#include linux/kvm_host.h
+
+struct kvm_mips_callbacks *kvm_mips_callbacks = NULL;
+EXPORT_SYMBOL(kvm_mips_callbacks);
diff --git a/arch/mips/kvm/kvm_trap_emul.c b/arch/mips/kvm/kvm_trap_emul.c
new file mode 100644
index 000..68983bc
--- /dev/null
+++ b/arch/mips/kvm/kvm_trap_emul.c
@@ -0,0 +1,446 @@
+/*
+* This file is subject to the terms and conditions of the GNU General Public
+* License.  See the file COPYING in the main directory of this archive
+* for more details.
+*
+* KVM/MIPS: Deliver/Emulate exceptions to the guest kernel 
+*
+* Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
+* Authors: Sanjay Lal sanj...@kymasys.com
+*/
+
+
+#include linux/errno.h
+#include linux/err.h
+#include linux/module.h
+#include linux/vmalloc.h
+
+#include linux/kvm_host.h
+
+#include kvm_mips_opcode.h
+#include kvm_mips_stats.h
+#include kvm_mips_int.h
+
+static gpa_t
+kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
+{
+gpa_t gpa;
+uint32_t kseg = KSEGX(gva);
+
+if ((kseg == CKSEG0) || (kseg == CKSEG1))
+gpa = CPHYSADDR(gva);
+else {
+printk(%s: cannot find GPA for GVA: %#lx\n, __func__, gva);
+kvm_mips_dump_host_tlbs();
+gpa = KVM_INVALID_ADDR;
+}
+
+#ifdef DEBUG
+kvm_debug(%s: gva %#lx, gpa: %#llx\n, __func__, gva, gpa);
+#endif
+
+return gpa;
+}
+
+
+static int
+kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
+{
+struct kvm_run *run = vcpu-run;
+uint32_t __user *opc = (uint32_t __user *) vcpu-arch.pc;
+ulong cause = vcpu-arch.host_cp0_cause;
+enum emulation_result er = EMULATE_DONE;
+int ret = RESUME_GUEST;
+
+if (((cause  CAUSEF_CE)  CAUSEB_CE) == 1) {
+er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
+}
+else
+er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
+
+switch (er) {
+case EMULATE_DONE:
+ret = RESUME_GUEST;
+break;
+
+case EMULATE_FAIL:
+run-exit_reason = KVM_EXIT_INTERNAL_ERROR;
+ret = RESUME_HOST;
+break;
+
+case EMULATE_WAIT:
+run-exit_reason = KVM_EXIT_INTR;
+ret = RESUME_HOST;
+break;
+
+default:
+BUG();
+}
+return ret;
+}
+
+static int
+kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
+{
+struct kvm_run *run = vcpu-run;
+uint32_t __user *opc = (uint32_t __user *) vcpu-arch.pc;
+ulong cause = vcpu-arch.host_cp0_cause;
+ulong badvaddr = vcpu-arch.host_cp0_badvaddr;
+enum emulation_result er = EMULATE_DONE;
+int ret = RESUME_GUEST;
+
+if (KVM_GUEST_KSEGX(badvaddr)  KVM_GUEST_KSEG0
+|| KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
+#ifdef DEBUG
+kvm_debug(USER/KSEG23 ADDR TLB MOD fault: cause %#lx, PC: %p, 
BadVaddr: %#lx\n,
+ cause, opc, badvaddr);
+#endif
+er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu);
+
+if (er == EMULATE_DONE)
+ret = RESUME_GUEST;
+else {
+run-exit_reason = KVM_EXIT_INTERNAL_ERROR;
+ret = RESUME_HOST;
+}
+}
+else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
+/* XXXKYMA: The guest kernel does not expect to get this fault when we 
are not
+ * using HIGHMEM. Need to address this in a HIGHMEM kernel
+ */
+printk (TLB MOD fault not handled, cause %#lx, PC: %p, BadVaddr: 
%#lx\n,
+ cause, opc, badvaddr);
+kvm_mips_dump_host_tlbs();
+kvm_arch_vcpu_dump_regs(vcpu);
+run-exit_reason = KVM_EXIT_INTERNAL_ERROR;
+ret = RESUME_HOST;
+}
+else {
+printk
+(Illegal TLB Mod fault address , cause %#lx, PC: %p, BadVaddr: 
%#lx\n,
+ cause, opc, badvaddr);
+kvm_mips_dump_host_tlbs();
+kvm_arch_vcpu_dump_regs(vcpu);
+run-exit_reason = KVM_EXIT_INTERNAL_ERROR;
+ret = RESUME_HOST;
+}
+return ret;
+}
+
+static int
+kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
+{
+struct kvm_run *run = vcpu-run;
+uint32_t __user *opc = (uint32_t __user *) vcpu-arch.pc;
+ulong cause = vcpu-arch.host_cp0_cause;
+ulong badvaddr = 

Re: [PATCH 12/20] KVM/MIPS32: Routines to handle specific traps/exceptions while executing the guest.

2012-10-31 Thread David Daney

On 10/31/2012 08:20 AM, Sanjay Lal wrote:

Signed-off-by: Sanjay Lal sanj...@kymasys.com
---
  arch/mips/kvm/kvm_cb.c|  16 ++
  arch/mips/kvm/kvm_trap_emul.c | 446 ++
  2 files changed, 462 insertions(+)
  create mode 100644 arch/mips/kvm/kvm_cb.c
  create mode 100644 arch/mips/kvm/kvm_trap_emul.c



At a minimum you should probably follow Documentation/CodingStyle

Running checkpatch.pl on all the patches and fixing obvious problems is 
always a good idea.




diff --git a/arch/mips/kvm/kvm_cb.c b/arch/mips/kvm/kvm_cb.c
new file mode 100644
index 000..768198e
--- /dev/null
+++ b/arch/mips/kvm/kvm_cb.c
@@ -0,0 +1,16 @@
+/*
+* This file is subject to the terms and conditions of the GNU General Public
+* License.  See the file COPYING in the main directory of this archive
+* for more details.
+*
+* PUT YOUR TITLE AND/OR INFORMATION FOR THE FILE HERE


That line can probably be safely removed.



+*
+* Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
+* Authors: Yann Le Du l...@kymasys.com
+*/

.
.
.

David Daney
--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html