Signed-off-by: Liu Yu <yu....@freescale.com>
---
 arch/powerpc/include/asm/kvm_ppc.h |    5 ++
 arch/powerpc/kvm/booke.c           |   97 ++++++++++++++++++++++++++++++++++--
 arch/powerpc/kvm/e500.c            |    8 ---
 arch/powerpc/kvm/powerpc.c         |    2 +-
 4 files changed, 99 insertions(+), 13 deletions(-)

diff --git a/arch/powerpc/include/asm/kvm_ppc.h 
b/arch/powerpc/include/asm/kvm_ppc.h
index 2c6ee34..4f3656b 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -89,6 +89,11 @@ extern int kvmppc_core_emulate_op(struct kvm_run *run, 
struct kvm_vcpu *vcpu,
 extern int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs);
 extern int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt);
 
+extern void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu);
+extern void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu);
+extern int kvmppc_core_set_guest_debug(struct kvm_vcpu *vcpu,
+                                       struct kvm_guest_debug *dbg);
+
 extern int kvmppc_booke_init(void);
 extern void kvmppc_booke_exit(void);
 
diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index e7bf4d0..4e7954f 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -16,6 +16,9 @@
  *
  * Authors: Hollis Blanchard <holl...@us.ibm.com>
  *          Christian Ehrhardt <ehrha...@linux.vnet.ibm.com>
+ *
+ * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
+ *          Yu Liu <yu....@freescale.com>
  */
 
 #include <linux/errno.h>
@@ -230,6 +233,16 @@ int kvmppc_handle_exit(struct kvm_run *run, struct 
kvm_vcpu *vcpu,
                        break;
                }
 
+               if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_ENABLE) &&
+                            (vcpu->arch.last_inst == KVM_INST_GUESTGDB)) {
+                       run->exit_reason = KVM_EXIT_DEBUG;
+                       run->debug.arch.pc = vcpu->arch.pc;
+                       run->debug.arch.exception = exit_nr;
+                       kvmppc_account_exit(vcpu, DEBUG_EXITS);
+                       r = RESUME_HOST;
+                       break;
+               }
+
                er = kvmppc_emulate_instruction(run, vcpu);
                switch (er) {
                case EMULATE_DONE:
@@ -256,6 +269,12 @@ int kvmppc_handle_exit(struct kvm_run *run, struct 
kvm_vcpu *vcpu,
                default:
                        BUG();
                }
+
+               if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_ENABLE) &&
+                   (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
+                       run->exit_reason = KVM_EXIT_DEBUG;
+                       r = RESUME_HOST;
+               }
                break;
 
        case BOOKE_INTERRUPT_FP_UNAVAIL:
@@ -386,12 +405,27 @@ int kvmppc_handle_exit(struct kvm_run *run, struct 
kvm_vcpu *vcpu,
                u32 dbsr;
 
                vcpu->arch.pc = mfspr(SPRN_CSRR0);
-
-               /* clear IAC events in DBSR register */
                dbsr = mfspr(SPRN_DBSR);
-               dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
-               mtspr(SPRN_DBSR, dbsr);
+               run->debug.arch.pc = vcpu->arch.pc;
+               run->debug.arch.status = 0;
+
+               if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
+                       run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
+               } else {
+                       if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
+                               run->debug.arch.status |= 
KVMPPC_DEBUG_WATCH_WRITE;
+                       else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
+                               run->debug.arch.status |= 
KVMPPC_DEBUG_WATCH_READ;
+                       if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
+                               run->debug.arch.pc = 
vcpu->arch.shadow_dbg_reg.dac1;
+                       else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
+                               run->debug.arch.pc = 
vcpu->arch.shadow_dbg_reg.dac2;
+               }
 
+               /* clear events in DBSR register */
+               mtspr(SPRN_DBSR, ~0);
+
+               run->debug.arch.exception = exit_nr;
                run->exit_reason = KVM_EXIT_DEBUG;
                kvmppc_account_exit(vcpu, DEBUG_EXITS);
                r = RESUME_HOST;
@@ -463,6 +497,7 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, 
struct kvm_regs *regs)
        for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
                regs->gpr[i] = vcpu->arch.gpr[i];
 
+
        return 0;
 }
 
@@ -520,6 +555,60 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
        return kvmppc_core_vcpu_translate(vcpu, tr);
 }
 
+int kvmppc_core_set_guest_debug(struct kvm_vcpu *vcpu,
+                                struct kvm_guest_debug *dbg)
+{
+       if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
+               vcpu->guest_debug = 0;
+               return 0;
+       }
+
+       vcpu->guest_debug = dbg->control;
+       vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
+
+       if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
+               vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
+
+       if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
+               struct kvmppc_debug_reg *gdbgr = &(vcpu->arch.shadow_dbg_reg);
+               int n, b = 0, w = 0;
+               const u32 bp_code[] = {
+                       DBCR0_IAC1 | DBCR0_IDM,
+                       DBCR0_IAC2 | DBCR0_IDM,
+                       DBCR0_IAC3 | DBCR0_IDM,
+                       DBCR0_IAC4 | DBCR0_IDM
+               };
+               const u32 wp_code[] = {
+                       DBCR0_DAC1W | DBCR0_IDM,
+                       DBCR0_DAC2W | DBCR0_IDM,
+                       DBCR0_DAC1R | DBCR0_IDM,
+                       DBCR0_DAC2R | DBCR0_IDM
+               };
+
+               for (n = 0; n < 6 && dbg->arch.bp[n].type; n++) {
+                       if (dbg->arch.bp[n].type & KVMPPC_DEBUG_BREAKPOINT)
+                               gdbgr->dbcr0 |= bp_code[b];
+                       if (dbg->arch.bp[n].type & KVMPPC_DEBUG_WATCH_READ)
+                               gdbgr->dbcr0 |= wp_code[w + 2];
+                       if (dbg->arch.bp[n].type & KVMPPC_DEBUG_WATCH_WRITE)
+                               gdbgr->dbcr0 |= wp_code[w];
+
+                       if (b < 4 && (gdbgr->dbcr0 & (DBCR0_IAC1 | DBCR0_IAC2 |
+                                       DBCR0_IAC3 | DBCR0_IAC4))) {
+                               gdbgr->iac[b] = dbg->arch.bp[n].addr;
+                               b++;
+                       }
+                       if (w < 2 && (gdbgr->dbcr0 & (DBCR0_DAC1W | DBCR0_DAC1R
+                                       | DBCR0_DAC2W | DBCR0_DAC2R))) {
+                               gdbgr->dac[w] = dbg->arch.bp[n].addr;
+                               w++;
+                       }
+               }
+       }
+
+       return 0;
+}
+
 int __init kvmppc_booke_init(void)
 {
        unsigned long ivor[16];
diff --git a/arch/powerpc/kvm/e500.c b/arch/powerpc/kvm/e500.c
index 64949ee..f7914b5 100644
--- a/arch/powerpc/kvm/e500.c
+++ b/arch/powerpc/kvm/e500.c
@@ -24,14 +24,6 @@
 #include "booke.h"
 #include "e500_tlb.h"
 
-void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
-{
-}
-
-void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
-{
-}
-
 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
        kvmppc_e500_tlb_load(vcpu, cpu);
diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index 2a4551f..c00e490 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -234,7 +234,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
                                         struct kvm_guest_debug *dbg)
 {
-       return -EINVAL;
+       return kvmppc_core_set_guest_debug(vcpu, dbg);
 }
 
 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
-- 
1.5.4

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

Reply via email to