[PATCH 4/6] KVM: PPC: BOOKE : Emulate rfdi instruction

2014-07-11 Thread Bharat Bhushan
This patch adds "rfdi" instruction emulation which is required for
guest debug hander on BOOKE-HV

Signed-off-by: Bharat Bhushan 
---
 arch/powerpc/include/asm/kvm_host.h |  1 +
 arch/powerpc/kvm/booke_emulate.c| 17 +
 2 files changed, 18 insertions(+)

diff --git a/arch/powerpc/include/asm/kvm_host.h 
b/arch/powerpc/include/asm/kvm_host.h
index 855ba4d..372b977 100644
--- a/arch/powerpc/include/asm/kvm_host.h
+++ b/arch/powerpc/include/asm/kvm_host.h
@@ -149,6 +149,7 @@ enum kvm_exit_types {
EMULATED_TLBWE_EXITS,
EMULATED_RFI_EXITS,
EMULATED_RFCI_EXITS,
+   EMULATED_RFDI_EXITS,
DEC_EXITS,
EXT_INTR_EXITS,
HALT_WAKEUP,
diff --git a/arch/powerpc/kvm/booke_emulate.c b/arch/powerpc/kvm/booke_emulate.c
index 80c51a2..2a20194 100644
--- a/arch/powerpc/kvm/booke_emulate.c
+++ b/arch/powerpc/kvm/booke_emulate.c
@@ -25,6 +25,7 @@
 
 #define OP_19_XOP_RFI 50
 #define OP_19_XOP_RFCI51
+#define OP_19_XOP_RFDI39
 
 #define OP_31_XOP_MFMSR   83
 #define OP_31_XOP_WRTEE   131
@@ -37,6 +38,16 @@ static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
 }
 
+static void kvmppc_emul_rfdi(struct kvm_vcpu *vcpu)
+{
+   vcpu->arch.pc = vcpu->arch.dsrr0;
+   /* Force MSR_DE when guest does not own debug facilities */
+   if (vcpu->guest_debug)
+   kvmppc_set_msr(vcpu, vcpu->arch.dsrr1 | MSR_DE);
+   else
+   kvmppc_set_msr(vcpu, vcpu->arch.dsrr1);
+}
+
 static void kvmppc_emul_rfci(struct kvm_vcpu *vcpu)
 {
vcpu->arch.pc = vcpu->arch.csrr0;
@@ -69,6 +80,12 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct 
kvm_vcpu *vcpu,
*advance = 0;
break;
 
+   case OP_19_XOP_RFDI:
+   kvmppc_emul_rfdi(vcpu);
+   kvmppc_set_exit_type(vcpu, EMULATED_RFDI_EXITS);
+   *advance = 0;
+   break;
+
default:
emulated = EMULATE_FAIL;
break;
-- 
1.9.3

--
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


[PATCH 0/6] Guest debug emulation

2014-07-11 Thread Bharat Bhushan
This patchset adds debug register and interrupt emulation support
for guest, which enables running gdb/kgdb etc in guest.
This also have couple of bux fixes

Bharat Bhushan (6):
  KVM: PPC: BOOKE: No need to set DBCR0_EDM in guest visible register
  KVM: PPC: BOOKE: Force MSR_DE in rfci if guest is under debug
  KVM: PPC: BOOKE: allow debug interrupt at "debug level"
  KVM: PPC: BOOKE : Emulate rfdi instruction
  KVM: PPC: BOOKE: Allow guest to change MSR_DE
  KVM: PPC: BOOKE: Emulate debug registers and exception

 arch/powerpc/include/asm/kvm_host.h |   1 +
 arch/powerpc/include/asm/kvm_ppc.h  |   3 +
 arch/powerpc/kvm/booke.c|  35 ++-
 arch/powerpc/kvm/booke_emulate.c| 180 +++-
 arch/powerpc/kvm/e500mc.c   |   2 +-
 5 files changed, 216 insertions(+), 5 deletions(-)

-- 
1.9.3

--
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


[PATCH 5/6] KVM: PPC: BOOKE: Allow guest to change MSR_DE

2014-07-11 Thread Bharat Bhushan
When userspace is debugging guest then MSR_DE is always set and
MSRP_DEP is set so that guest cannot change MSR_DE.
Guest debug resources are not yet emulated, So there seems no reason
we should stop guest controlling MSR_DE.
Also a followup patch will enable debug emulation and that requires
guest to control MSR_DE.

Signed-off-by: Bharat Bhushan 
---
 arch/powerpc/kvm/e500mc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/e500mc.c b/arch/powerpc/kvm/e500mc.c
index 690499d..bd0a2bd 100644
--- a/arch/powerpc/kvm/e500mc.c
+++ b/arch/powerpc/kvm/e500mc.c
@@ -194,7 +194,7 @@ int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
 #ifdef CONFIG_64BIT
vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
 #endif
-   vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_DEP | MSRP_PMMP;
+   vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP;
vcpu->arch.eplc = EPC_EGS | (vcpu->kvm->arch.lpid << EPC_ELPID_SHIFT);
vcpu->arch.epsc = vcpu->arch.eplc;
 
-- 
1.9.3

--
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


[PATCH 3/6] KVM: PPC: BOOKE: allow debug interrupt at "debug level"

2014-07-11 Thread Bharat Bhushan
Debug interrupt can be either "critical level" or "debug level".
There are separate set of save/restore registers used for different level.
Example: DSRR0/DSRR1 are used for "debug level" and CSRR0/CSRR1
are used for critical level debug interrupt.

Using CPU_FTR_DEBUG_LVL_EXC to decide which interrupt level to be used.

Signed-off-by: Bharat Bhushan 
---
 arch/powerpc/kvm/booke.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index a5ee42c..fadfe76 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -424,7 +424,11 @@ static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu 
*vcpu,
allowed = vcpu->arch.shared->msr & MSR_DE;
allowed = allowed && !crit;
msr_mask = MSR_ME;
-   int_class = INT_CLASS_CRIT;
+   if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
+   int_class = INT_CLASS_DBG;
+   else
+   int_class = INT_CLASS_CRIT;
+
break;
}
 
-- 
1.9.3

--
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


[PATCH 6/6] KVM: PPC: BOOKE: Emulate debug registers and exception

2014-07-11 Thread Bharat Bhushan
This patch emulates debug registers and debug exception
to support guest using debug resource. This enables running
gdb/kgdb etc in guest.

On BOOKE architecture we cannot share debug resources between QEMU and
guest because:
When QEMU is using debug resources then debug exception must
be always enabled. To achieve this we set MSR_DE and also set
MSRP_DEP so guest cannot change MSR_DE.

When emulating debug resource for guest we want guest
to control MSR_DE (enable/disable debug interrupt on need).

So above mentioned two configuration cannot be supported
at the same time. So the result is that we cannot share
debug resources between QEMU and Guest on BOOKE architecture.

In the current design QEMU gets priority over guest, this means that if
QEMU is using debug resources then guest cannot use them and if guest is
using debug resource then QEMU can overwrite them.

Signed-off-by: Bharat Bhushan 
---
Hi Alex,

I thought of having some print in register emulation if QEMU
is using debug resource, Also when QEMU overwrites guest written
values but that looks excessive. If I uses some variable which
get set when guest starts using debug registers and check in
debug set ioctl then that look ugly. Looking for suggestions

 arch/powerpc/include/asm/kvm_ppc.h |   3 +
 arch/powerpc/kvm/booke.c   |  27 +++
 arch/powerpc/kvm/booke_emulate.c   | 157 +
 3 files changed, 187 insertions(+)

diff --git a/arch/powerpc/include/asm/kvm_ppc.h 
b/arch/powerpc/include/asm/kvm_ppc.h
index e2fd5a1..f3f7611 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -173,6 +173,9 @@ extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, 
u32 *server,
 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
 
+void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
+void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
+
 union kvmppc_one_reg {
u32 wval;
u64 dval;
diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index fadfe76..c2471ed 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -264,6 +264,16 @@ static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu 
*vcpu)
clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
 }
 
+void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
+{
+   kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
+}
+
+void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
+{
+   clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
+}
+
 static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
 {
 #ifdef CONFIG_KVM_BOOKE_HV
@@ -783,6 +793,23 @@ static int kvmppc_handle_debug(struct kvm_run *run, struct 
kvm_vcpu *vcpu)
struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
u32 dbsr = vcpu->arch.dbsr;
 
+   if (vcpu->guest_debug == 0) {
+   /* Debug resources belong to Guest */
+   if (dbsr && (vcpu->arch.shared->msr & MSR_DE))
+   kvmppc_core_queue_debug(vcpu);
+
+   /* Inject a program interrupt if trap debug is not allowed */
+   if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
+   kvmppc_core_queue_program(vcpu, ESR_PTR);
+
+   return RESUME_GUEST;
+   }
+
+   /*
+* Prepare for userspace exit as debug resources
+* are owned by userspace.
+*/
+   vcpu->arch.dbsr = 0;
run->debug.arch.status = 0;
run->debug.arch.address = vcpu->arch.pc;
 
diff --git a/arch/powerpc/kvm/booke_emulate.c b/arch/powerpc/kvm/booke_emulate.c
index 2a20194..3d143fe 100644
--- a/arch/powerpc/kvm/booke_emulate.c
+++ b/arch/powerpc/kvm/booke_emulate.c
@@ -139,6 +139,7 @@ int kvmppc_booke_emulate_op(struct kvm_run *run, struct 
kvm_vcpu *vcpu,
 int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
 {
int emulated = EMULATE_DONE;
+   bool debug_inst = false;
 
switch (sprn) {
case SPRN_DEAR:
@@ -153,14 +154,137 @@ int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, 
int sprn, ulong spr_val)
case SPRN_CSRR1:
vcpu->arch.csrr1 = spr_val;
break;
+   case SPRN_DSRR0:
+   vcpu->arch.dsrr0 = spr_val;
+   break;
+   case SPRN_DSRR1:
+   vcpu->arch.dsrr1 = spr_val;
+   break;
+   case SPRN_IAC1:
+   /*
+* If userspace is debugging guest then guest
+* can not access debug registers.
+*/
+   if (vcpu->guest_debug)
+   break;
+
+   debug_inst = true;
+   vcpu->arch.dbg_reg.iac1 = spr_val;
+   vcpu->arch.shadow_dbg_reg.iac1 = spr_val;
+   break;
+   case SPRN_IAC2:
+   /*
+

[PATCH 1/6] KVM: PPC: BOOKE: No need to set DBCR0_EDM in guest visible register

2014-07-11 Thread Bharat Bhushan
This is not used and  even I do not remember why this was added
in first place.

Signed-off-by: Bharat Bhushan 
---
 arch/powerpc/kvm/booke.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index ab62109..a5ee42c 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -1804,8 +1804,6 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu 
*vcpu,
kvm_guest_protect_msr(vcpu, MSR_DE, true);
vcpu->guest_debug = dbg->control;
vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
-   /* Set DBCR0_EDM in guest visible DBCR0 register. */
-   vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
 
if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
-- 
1.9.3

--
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


[PATCH 2/6] KVM: PPC: BOOKE: Force MSR_DE in rfci if guest is under debug

2014-07-11 Thread Bharat Bhushan
When userspace (QEMU) is using the debug resource to debug guest
then we want MSR_DE to be always set. This patch adds missing
MSR_DE setting in "rfci" instruction.

Signed-off-by: Bharat Bhushan 
---
 arch/powerpc/kvm/booke_emulate.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/booke_emulate.c b/arch/powerpc/kvm/booke_emulate.c
index 27a4b28..80c51a2 100644
--- a/arch/powerpc/kvm/booke_emulate.c
+++ b/arch/powerpc/kvm/booke_emulate.c
@@ -40,7 +40,11 @@ static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
 static void kvmppc_emul_rfci(struct kvm_vcpu *vcpu)
 {
vcpu->arch.pc = vcpu->arch.csrr0;
-   kvmppc_set_msr(vcpu, vcpu->arch.csrr1);
+   /* Force MSR_DE when guest does not own debug facilities */
+   if (vcpu->guest_debug)
+   kvmppc_set_msr(vcpu, vcpu->arch.csrr1 | MSR_DE);
+   else
+   kvmppc_set_msr(vcpu, vcpu->arch.csrr1);
 }
 
 int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
-- 
1.9.3

--
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