Re: [PATCH 00/25] treewide: Use bool function return values of true/false not 1/0

2015-03-31 Thread Casey Schaufler
On 3/30/2015 4:45 PM, Joe Perches wrote:
 Joe Perches (25):
   arm: Use bool function return values of true/false not 1/0
   arm64: Use bool function return values of true/false not 1/0
   hexagon: Use bool function return values of true/false not 1/0
   ia64: Use bool function return values of true/false not 1/0
   mips: Use bool function return values of true/false not 1/0
   powerpc: Use bool function return values of true/false not 1/0
   s390: Use bool function return values of true/false not 1/0
   sparc: Use bool function return values of true/false not 1/0
   tile: Use bool function return values of true/false not 1/0
   unicore32: Use bool function return values of true/false not 1/0
   x86: Use bool function return values of true/false not 1/0
   virtio_console: Use bool function return values of true/false not 1/0
   csiostor: Use bool function return values of true/false not 1/0
   dcache: Use bool function return values of true/false not 1/0
   nfsd: nfs4state: Use bool function return values of true/false not 1/0
   include/linux: Use bool function return values of true/false not 1/0
   sound: Use bool function return values of true/false not 1/0
   rcu: tree_plugin: Use bool function return values of true/false not 1/0
   sched: Use bool function return values of true/false not 1/0
   ftrace: Use bool function return values of true/false not 1/0
   slub: Use bool function return values of true/false not 1/0
   bridge: Use bool function return values of true/false not 1/0
   netfilter: Use bool function return values of true/false not 1/0
   security: Use bool function return values of true/false not 1/0
   sound: wm5100-tables: Use bool function return values of true/false not 1/0

  arch/arm/include/asm/dma-mapping.h   |  8 ++--
  arch/arm/include/asm/kvm_emulate.h   |  2 +-
  arch/arm/mach-omap2/powerdomain.c| 14 +++---
  arch/arm64/include/asm/dma-mapping.h |  2 +-
  arch/hexagon/include/asm/dma-mapping.h   |  2 +-
  arch/ia64/include/asm/dma-mapping.h  |  2 +-
  arch/mips/include/asm/dma-mapping.h  |  2 +-
  arch/powerpc/include/asm/dcr-native.h|  2 +-
  arch/powerpc/include/asm/dma-mapping.h   |  4 +-
  arch/powerpc/include/asm/kvm_book3s_64.h |  4 +-
  arch/powerpc/sysdev/dcr.c|  2 +-
  arch/s390/include/asm/dma-mapping.h  |  2 +-
  arch/sparc/mm/init_64.c  |  8 ++--
  arch/tile/include/asm/dma-mapping.h  |  2 +-
  arch/unicore32/include/asm/dma-mapping.h |  2 +-
  arch/x86/include/asm/archrandom.h|  2 +-
  arch/x86/include/asm/dma-mapping.h   |  2 +-
  arch/x86/include/asm/kvm_para.h  |  2 +-
  arch/x86/kvm/cpuid.h |  2 +-
  arch/x86/kvm/vmx.c   | 72 
 ++--
  drivers/char/virtio_console.c|  2 +-
  drivers/scsi/csiostor/csio_scsi.c|  4 +-
  fs/dcache.c  | 12 ++---
  fs/nfsd/nfs4state.c  |  2 +-
  include/linux/blkdev.h   |  2 +-
  include/linux/ide.h  |  2 +-
  include/linux/kgdb.h |  2 +-
  include/linux/mfd/db8500-prcmu.h |  2 +-
  include/linux/mm.h   |  2 +-
  include/linux/power_supply.h |  8 ++--
  include/linux/ssb/ssb_driver_extif.h |  2 +-
  include/linux/ssb/ssb_driver_gige.h  | 16 +++
  include/sound/soc.h  |  4 +-
  kernel/rcu/tree_plugin.h |  4 +-
  kernel/sched/auto_group.h|  2 +-
  kernel/sched/completion.c| 16 ---
  kernel/trace/ftrace.c| 10 ++--
  mm/slub.c| 12 ++---
  net/bridge/br_private.h  |  2 +-
  net/ipv4/netfilter/ipt_ah.c  |  2 +-
  net/netfilter/ipset/ip_set_hash_ip.c |  8 ++--
  net/netfilter/ipset/ip_set_hash_ipmark.c |  8 ++--
  net/netfilter/ipset/ip_set_hash_ipport.c |  8 ++--
  net/netfilter/ipset/ip_set_hash_ipportip.c   |  8 ++--
  net/netfilter/ipset/ip_set_hash_ipportnet.c  |  8 ++--
  net/netfilter/ipset/ip_set_hash_net.c|  8 ++--
  net/netfilter/ipset/ip_set_hash_netiface.c   |  8 ++--
  net/netfilter/ipset/ip_set_hash_netport.c|  8 ++--
  net/netfilter/ipset/ip_set_hash_netportnet.c |  8 ++--
  net/netfilter/xt_connlimit.c |  2 +-
  net/netfilter/xt_hashlimit.c |  2 +-
  net/netfilter/xt_ipcomp.c|  2 +-
  security/apparmor/file.c |  8 ++--
  security/apparmor/policy.c   | 10 ++--
  sound/soc/codecs/wm5100-tables.c | 12 ++---

Why, and why these in particular?

  55 files changed, 178 insertions(+), 176 deletions(-)


___
kvmarm mailing list

[PATCH 01/25] arm: Use bool function return values of true/false not 1/0

2015-03-31 Thread Joe Perches
Use the normal return values for bool functions

Signed-off-by: Joe Perches j...@perches.com
---
 arch/arm/include/asm/dma-mapping.h |  8 
 arch/arm/include/asm/kvm_emulate.h |  2 +-
 arch/arm/mach-omap2/powerdomain.c  | 14 +++---
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/arch/arm/include/asm/dma-mapping.h 
b/arch/arm/include/asm/dma-mapping.h
index b52101d..166e1e1 100644
--- a/arch/arm/include/asm/dma-mapping.h
+++ b/arch/arm/include/asm/dma-mapping.h
@@ -151,18 +151,18 @@ static inline bool dma_capable(struct device *dev, 
dma_addr_t addr, size_t size)
u64 limit, mask;
 
if (!dev-dma_mask)
-   return 0;
+   return false;
 
mask = *dev-dma_mask;
 
limit = (mask + 1)  ~mask;
if (limit  size  limit)
-   return 0;
+   return false;
 
if ((addr | (addr + size - 1))  ~mask)
-   return 0;
+   return false;
 
-   return 1;
+   return true;
 }
 
 static inline void dma_mark_clean(void *addr, size_t size) { }
diff --git a/arch/arm/include/asm/kvm_emulate.h 
b/arch/arm/include/asm/kvm_emulate.h
index a9c80a2..ad200a0 100644
--- a/arch/arm/include/asm/kvm_emulate.h
+++ b/arch/arm/include/asm/kvm_emulate.h
@@ -51,7 +51,7 @@ static inline void vcpu_set_hcr(struct kvm_vcpu *vcpu, 
unsigned long hcr)
 
 static inline bool vcpu_mode_is_32bit(struct kvm_vcpu *vcpu)
 {
-   return 1;
+   return true;
 }
 
 static inline unsigned long *vcpu_pc(struct kvm_vcpu *vcpu)
diff --git a/arch/arm/mach-omap2/powerdomain.c 
b/arch/arm/mach-omap2/powerdomain.c
index 78af6d8..897f9fb 100644
--- a/arch/arm/mach-omap2/powerdomain.c
+++ b/arch/arm/mach-omap2/powerdomain.c
@@ -950,7 +950,7 @@ int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
  */
 bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
 {
-   return (pwrdm  pwrdm-flags  PWRDM_HAS_HDWR_SAR) ? 1 : 0;
+   return pwrdm  (pwrdm-flags  PWRDM_HAS_HDWR_SAR);
 }
 
 int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
@@ -1185,24 +1185,24 @@ bool pwrdm_can_ever_lose_context(struct powerdomain 
*pwrdm)
if (!pwrdm) {
pr_debug(powerdomain: %s: invalid powerdomain pointer\n,
 __func__);
-   return 1;
+   return true;
}
 
if (pwrdm-pwrsts  PWRSTS_OFF)
-   return 1;
+   return true;
 
if (pwrdm-pwrsts  PWRSTS_RET) {
if (pwrdm-pwrsts_logic_ret  PWRSTS_OFF)
-   return 1;
+   return true;
 
for (i = 0; i  pwrdm-banks; i++)
if (pwrdm-pwrsts_mem_ret[i]  PWRSTS_OFF)
-   return 1;
+   return true;
}
 
for (i = 0; i  pwrdm-banks; i++)
if (pwrdm-pwrsts_mem_on[i]  PWRSTS_OFF)
-   return 1;
+   return true;
 
-   return 0;
+   return false;
 }
-- 
2.1.2

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v2 00/10] KVM Guest Debug support for arm64

2015-03-31 Thread Alex Bennée
Hi,

Here is V2 of the KVM Guest Debug support for arm64. Although there
has been an increase in the total number of patches the implementation
is both simpler and more complete.

Gone are most of the changes that touch hyp.S replaced with C based
hooks kvm_arch_setup/clear_debug() that manipulate the VCPU context
before entering hyp.S. As a result I dropped the re-factoring patch
for simplicity.

The API has been simplified to pass the syndrome information directly
back to user-space and leaving it to figure out what each exception
is.

The ioctl and handle_exit code have been re-factored and simplified.
The handle_exit code in particular is all done with one handler which
aside from watchpoints is supplying pretty much the same information
for every exception.

The HW debugging support has been improved by handling guest access to
the debug register while debugging is happening.

As a result a bunch of the review comments are no longer relevant as
they applied to code that no longer exists. All the rest have been
addressed as of this patch series.

There are a few checkpatch violations for white space. Some in
existing code (asm-offsets) and a couple in the handle_exit code where
adding a whole extra tab seemed excessive.

GIT Repos:

The patches for this series are based off v4.0-rc6 and can be found
at:

https://git.linaro.org/people/alex.bennee/linux.git
branch: guest-debug/4.0-rc6-v2

You can find the QEMU code that goes with this patch series at:

https://github.com/stsquad/qemu
branch: kvm/guest-debug-v2

Patch breakdown:

The first 2 patches are simple clean-ups to rationalise some of the
commentary and #defines.

The next 2 introduce the API and implement the stub ioctl handler
which is built up in later patches.

The kvm_arch_setup/clear_debug() patch is a functional replacement for
the previous manipulations of mdcr_el2 in hyp.S but making the value
part of the VCPU context.

The next 3 patches implement the various guest debug features.

The penultimate patch could be merged with the one before but I kept
it split apart for ease of review.

The final patch may get dropped before up-streaming but it does
provide useful trace points for anyone who want to track what is
happening during guest debug.

Alex Bennée (10):
  KVM: add commentary for kvm_debug_exit_arch struct
  KVM: define common __KVM_GUESTDBG_USE_SW/HW_BP values
  KVM: arm: guest debug, define API headers
  KVM: arm: guest debug, add stub KVM_SET_GUEST_DEBUG ioctl
  KVM: arm: introduce kvm_arch_setup/clear_debug()
  KVM: arm64: guest debug, add SW break point support
  KVM: arm64: guest debug, add support for single-step
  KVM: arm64: guest debug, HW assisted debug support
  KVM: arm64: trap nested debug register access
  KVM: arm64: add trace points for guest_debug debug

 Documentation/virtual/kvm/api.txt  |  10 +-
 arch/arm/include/asm/kvm_host.h|   2 +
 arch/arm/kvm/arm.c |  51 ++-
 arch/arm64/include/asm/hw_breakpoint.h |  12 ++
 arch/arm64/include/asm/kvm_host.h  |  19 ++-
 arch/arm64/include/uapi/asm/kvm.h  |  22 +++
 arch/arm64/kernel/asm-offsets.c|   1 +
 arch/arm64/kernel/hw_breakpoint.c  |  12 --
 arch/arm64/kvm/Makefile|   2 +-
 arch/arm64/kvm/debug.c | 249 +
 arch/arm64/kvm/handle_exit.c   |  43 ++
 arch/arm64/kvm/hyp.S   |  13 +-
 arch/arm64/kvm/reset.c |   6 +
 arch/arm64/kvm/sys_regs.c  |  55 
 arch/arm64/kvm/trace.h |  66 +
 arch/powerpc/include/uapi/asm/kvm.h|   4 +-
 arch/x86/include/uapi/asm/kvm.h|   4 +-
 include/uapi/linux/kvm.h   |  17 ++-
 18 files changed, 553 insertions(+), 35 deletions(-)
 create mode 100644 arch/arm64/kvm/debug.c

-- 
2.3.4

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v2 07/10] KVM: arm64: guest debug, add support for single-step

2015-03-31 Thread Alex Bennée
This adds support for single-stepping the guest. As userspace can and
will manipulate guest registers before restarting any tweaking of the
registers has to occur just before control is passed back to the guest.
Furthermore while guest debugging is in effect we need to squash the
ability of the guest to single-step itself as we have no easy way of
re-entering the guest after the exception has been delivered to the
hypervisor.

Signed-off-by: Alex Bennée alex.ben...@linaro.org

---
v2
  - Move pstate/mdscr manipulation into C
  - don't export guest_debug to assembly
  - add accessor for saved_debug regs
  - tweak save/restore of mdscr_el1

diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index d3bc8dc..c1ed8cb 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -304,7 +304,21 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
kvm_arm_set_running_vcpu(NULL);
 }
 
-#define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE|KVM_GUESTDBG_USE_SW_BP)
+#define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE |\
+   KVM_GUESTDBG_USE_SW_BP | \
+   KVM_GUESTDBG_SINGLESTEP)
+
+/**
+ * kvm_arch_vcpu_ioctl_set_guest_debug - Setup guest debugging
+ * @kvm:   pointer to the KVM struct
+ * @kvm_guest_debug: the ioctl data buffer
+ *
+ * This sets up the VM for guest debugging. Care has to be taken when
+ * manipulating guest registers as these will be set/cleared by the
+ * hyper-visor controller, typically before each kvm_run event. As a
+ * result modification of the guest registers needs to take place
+ * after they have been restored in the hyp.S trampoline code.
+ */
 
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
struct kvm_guest_debug *dbg)
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 0631840..6a33647 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -121,6 +121,13 @@ struct kvm_vcpu_arch {
 * here.
 */
 
+   /* Registers pre any guest debug manipulations */
+   struct {
+   u32 pstate_ss_bit;
+   u32 mdscr_el1_bits;
+
+   } debug_saved_regs;
+
/* Don't run the guest */
bool pause;
 
@@ -143,6 +150,7 @@ struct kvm_vcpu_arch {
 
 #define vcpu_gp_regs(v)((v)-arch.ctxt.gp_regs)
 #define vcpu_sys_reg(v,r)  ((v)-arch.ctxt.sys_regs[(r)])
+#define vcpu_debug_saved_reg(v, r) ((v)-arch.debug_saved_regs.r)
 /*
  * CP14 and CP15 live in the same array, as they are backed by the
  * same system registers.
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index cff0475..b32362c 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -19,8 +19,16 @@
 
 #include linux/kvm_host.h
 
+#include asm/debug-monitors.h
+#include asm/kvm_asm.h
 #include asm/kvm_arm.h
 #include asm/kvm_host.h
+#include asm/kvm_emulate.h
+
+/* These are the bits of MDSCR_EL1 we may mess with */
+#define MDSCR_EL1_DEBUG_BITS   (DBG_MDSCR_SS | \
+   DBG_MDSCR_KDE | \
+   DBG_MDSCR_MDE)
 
 /**
  * kvm_arch_setup_debug - set-up debug related stuff
@@ -51,15 +59,46 @@ void kvm_arch_setup_debug(struct kvm_vcpu *vcpu)
else
vcpu-arch.mdcr_el2 = ~MDCR_EL2_TDA;
 
-   /* Trap breakpoints? */
-   if (vcpu-guest_debug  KVM_GUESTDBG_USE_SW_BP)
+   /* Is Guest debugging in effect? */
+   if (vcpu-guest_debug) {
vcpu-arch.mdcr_el2 |= MDCR_EL2_TDE;
-   else
-   vcpu-arch.mdcr_el2 = ~MDCR_EL2_TDE;
 
+   /* Save pstate/mdscr */
+   vcpu_debug_saved_reg(vcpu, pstate_ss_bit) =
+   *vcpu_cpsr(vcpu)  DBG_SPSR_SS;
+   vcpu_debug_saved_reg(vcpu, mdscr_el1_bits) =
+   vcpu_sys_reg(vcpu, MDSCR_EL1)  MDSCR_EL1_DEBUG_BITS;
+   /*
+* Single Step (ARM ARM D2.12.3 The software step state
+* machine)
+*
+* If we are doing Single Step we need to manipulate
+* MDSCR_EL1.SS and PSTATE.SS. If not we need to
+* suppress the guest from messing with it.
+*/
+   if (vcpu-guest_debug  KVM_GUESTDBG_SINGLESTEP) {
+   *vcpu_cpsr(vcpu) |=  DBG_SPSR_SS;
+   vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS;
+   } else {
+   *vcpu_cpsr(vcpu) = ~DBG_SPSR_SS;
+   vcpu_sys_reg(vcpu, MDSCR_EL1) = ~DBG_MDSCR_SS;
+   }
+
+   } else {
+   /* Debug operations can go straight to the guest */
+   vcpu-arch.mdcr_el2 = ~MDCR_EL2_TDE;
+   }
 }
 
 void kvm_arch_clear_debug(struct kvm_vcpu *vcpu)
 {
-   /* Nothing to do yet */
+   if (vcpu-guest_debug) {
+   /* Restore pstate/mdscr bits we may have messed with */
+ 

[PATCH v2 04/10] KVM: arm: guest debug, add stub KVM_SET_GUEST_DEBUG ioctl

2015-03-31 Thread Alex Bennée
This commit adds a stub function to support the KVM_SET_GUEST_DEBUG
ioctl. Currently any operation flag will return EINVAL. Actual
functionality will be added with further patches.

Signed-off-by: Alex Bennée alex.ben...@linaro.org.

---
v2
  - simplified form of the ioctl (stuff will go into setup_debug)

diff --git a/Documentation/virtual/kvm/api.txt 
b/Documentation/virtual/kvm/api.txt
index b112efc..06c5064 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -2604,7 +2604,7 @@ handled.
 4.87 KVM_SET_GUEST_DEBUG
 
 Capability: KVM_CAP_SET_GUEST_DEBUG
-Architectures: x86, s390, ppc
+Architectures: x86, s390, ppc, arm64
 Type: vcpu ioctl
 Parameters: struct kvm_guest_debug (in)
 Returns: 0 on success; -1 on error
diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index 5560f74..445933d 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -183,6 +183,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_ARM_PSCI:
case KVM_CAP_ARM_PSCI_0_2:
case KVM_CAP_READONLY_MEM:
+   case KVM_CAP_SET_GUEST_DEBUG:
r = 1;
break;
case KVM_CAP_COALESCED_MMIO:
@@ -303,10 +304,21 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
kvm_arm_set_running_vcpu(NULL);
 }
 
+#define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE)
+
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
struct kvm_guest_debug *dbg)
 {
-   return -EINVAL;
+   if (dbg-control  KVM_GUESTDBG_ENABLE) {
+   if (dbg-control  ~KVM_GUESTDBG_VALID)
+   return -EINVAL;
+
+   vcpu-guest_debug = dbg-control;
+   } else {
+   /* If not enabled clear all flags */
+   vcpu-guest_debug = 0;
+   }
+   return 0;
 }
 
 
-- 
2.3.4

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v2 08/10] KVM: arm64: guest debug, HW assisted debug support

2015-03-31 Thread Alex Bennée
This adds support for userspace to control the HW debug registers for
guest debug. We'll only copy the $ARCH defined number across as that is
all that hyp.S will use anyway. I've moved some helper functions into
the hw_breakpoint.h header for re-use.

As with single step we need to tweak the guest registers to enable the
exceptions so we need to save and restore those bits.

Two new capabilities have been added to the KVM_EXTENSION ioctl to allow
userspace to query the number of hardware break and watch points
available on the host hardware.

As QEMU tests for watchpoints based on the address and not the PC we
also need to export the value of far_el2 to userspace.

Signed-off-by: Alex Bennée alex.ben...@linaro.org

---
v2
   - switched to C setup
   - replace host debug registers directly into context
   - minor tweak to api docs
   - setup right register for debug
   - add FAR_EL2 to debug exit structure
   - add support fro trapping debug register access

diff --git a/Documentation/virtual/kvm/api.txt 
b/Documentation/virtual/kvm/api.txt
index 17d4f9c..ac34093 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -2627,7 +2627,7 @@ The top 16 bits of the control field are architecture 
specific control
 flags which can include the following:
 
   - KVM_GUESTDBG_USE_SW_BP: using software breakpoints [x86, arm64]
-  - KVM_GUESTDBG_USE_HW_BP: using hardware breakpoints [x86, s390]
+  - KVM_GUESTDBG_USE_HW_BP: using hardware breakpoints [x86, s390, arm64]
   - KVM_GUESTDBG_INJECT_DB: inject DB type exception [x86]
   - KVM_GUESTDBG_INJECT_BP: inject BP type exception [x86]
   - KVM_GUESTDBG_EXIT_PENDING:  trigger an immediate guest exit [s390]
@@ -2642,6 +2642,10 @@ updated to the correct (supplied) values.
 The second part of the structure is architecture specific and
 typically contains a set of debug registers.
 
+For arm64 the number of debug registers is implementation defined and
+can be determined by querying the KVM_CAP_GUEST_DEBUG_HW_BPS and
+KVM_CAP_GUEST_DEBUG_HW_WPS capabilities.
+
 When debug events exit the main run loop with the reason
 KVM_EXIT_DEBUG with the kvm_debug_exit_arch part of the kvm_run
 structure containing architecture specific debug information.
diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index c1ed8cb..a286026 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -306,6 +306,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 
 #define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE |\
KVM_GUESTDBG_USE_SW_BP | \
+   KVM_GUESTDBG_USE_HW_BP | \
KVM_GUESTDBG_SINGLESTEP)
 
 /**
@@ -328,6 +329,26 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu 
*vcpu,
return -EINVAL;
 
vcpu-guest_debug = dbg-control;
+
+   /* Hardware assisted Break and Watch points */
+   if (vcpu-guest_debug  KVM_GUESTDBG_USE_HW_BP) {
+   int nb = get_num_brps();
+   int nw = get_num_wrps();
+
+   /* Copy across up to IMPDEF debug registers to our
+* shadow copy in the vcpu structure. The debug code
+* will then set them up before we re-enter the guest.
+*/
+   memcpy(vcpu-arch.guest_debug_regs.dbg_bcr,
+   dbg-arch.dbg_bcr, sizeof(__u64)*nb);
+   memcpy(vcpu-arch.guest_debug_regs.dbg_bvr,
+   dbg-arch.dbg_bvr, sizeof(__u64)*nb);
+   memcpy(vcpu-arch.guest_debug_regs.dbg_wcr,
+   dbg-arch.dbg_wcr, sizeof(__u64)*nw);
+   memcpy(vcpu-arch.guest_debug_regs.dbg_wvr,
+   dbg-arch.dbg_wvr, sizeof(__u64)*nw);
+   }
+
} else {
/* If not enabled clear all flags */
vcpu-guest_debug = 0;
diff --git a/arch/arm64/include/asm/hw_breakpoint.h 
b/arch/arm64/include/asm/hw_breakpoint.h
index 52b484b..c450552 100644
--- a/arch/arm64/include/asm/hw_breakpoint.h
+++ b/arch/arm64/include/asm/hw_breakpoint.h
@@ -130,6 +130,18 @@ static inline void ptrace_hw_copy_thread(struct 
task_struct *task)
 }
 #endif
 
+/* Determine number of BRP registers available. */
+static inline int get_num_brps(void)
+{
+   return ((read_cpuid(ID_AA64DFR0_EL1)  12)  0xf) + 1;
+}
+
+/* Determine number of WRP registers available. */
+static inline int get_num_wrps(void)
+{
+   return ((read_cpuid(ID_AA64DFR0_EL1)  20)  0xf) + 1;
+}
+
 extern struct pmu perf_ops_bp;
 
 #endif /* __KERNEL__ */
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 6a33647..2c359c9 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -106,8 +106,9 @@ struct kvm_vcpu_arch {
/* Exception Information */
  

[PATCH v2 06/10] KVM: arm64: guest debug, add SW break point support

2015-03-31 Thread Alex Bennée
This adds support for SW breakpoints inserted by userspace.

We do this by trapping all BKPT exceptions in the
hypervisor (MDCR_EL2_TDE). The kvm_debug_exit_arch carries the address
of the exception. If user-space doesn't know of the breakpoint then we
have a guest inserted breakpoint and the hypervisor needs to start again
and deliver the exception to guest.

Signed-off-by: Alex Bennée alex.ben...@linaro.org

---
v2
  - update to use new exit struct
  - tweak for C setup
  - do our setup in debug_setup/clear code
  - fixed up comments

diff --git a/Documentation/virtual/kvm/api.txt 
b/Documentation/virtual/kvm/api.txt
index 06c5064..17d4f9c 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -2626,7 +2626,7 @@ when running. Common control bits are:
 The top 16 bits of the control field are architecture specific control
 flags which can include the following:
 
-  - KVM_GUESTDBG_USE_SW_BP: using software breakpoints [x86]
+  - KVM_GUESTDBG_USE_SW_BP: using software breakpoints [x86, arm64]
   - KVM_GUESTDBG_USE_HW_BP: using hardware breakpoints [x86, s390]
   - KVM_GUESTDBG_INJECT_DB: inject DB type exception [x86]
   - KVM_GUESTDBG_INJECT_BP: inject BP type exception [x86]
diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index 7ea8b0e..d3bc8dc 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -304,7 +304,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
kvm_arm_set_running_vcpu(NULL);
 }
 
-#define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE)
+#define KVM_GUESTDBG_VALID (KVM_GUESTDBG_ENABLE|KVM_GUESTDBG_USE_SW_BP)
 
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
struct kvm_guest_debug *dbg)
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index 8a29d0b..cff0475 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -45,11 +45,18 @@ void kvm_arch_setup_debug(struct kvm_vcpu *vcpu)
vcpu-arch.mdcr_el2 |= (MDCR_EL2_TPM | MDCR_EL2_TPMCR);
vcpu-arch.mdcr_el2 |= (MDCR_EL2_TDRA | MDCR_EL2_TDOSA);
 
+   /* Trap debug register access? */
if (!vcpu-arch.debug_flags  KVM_ARM64_DEBUG_DIRTY)
vcpu-arch.mdcr_el2 |= MDCR_EL2_TDA;
else
vcpu-arch.mdcr_el2 = ~MDCR_EL2_TDA;
 
+   /* Trap breakpoints? */
+   if (vcpu-guest_debug  KVM_GUESTDBG_USE_SW_BP)
+   vcpu-arch.mdcr_el2 |= MDCR_EL2_TDE;
+   else
+   vcpu-arch.mdcr_el2 = ~MDCR_EL2_TDE;
+
 }
 
 void kvm_arch_clear_debug(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 524fa25..ed1bbb4 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -82,6 +82,37 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct 
kvm_run *run)
return 1;
 }
 
+/**
+ * kvm_handle_debug_exception - handle a debug exception instruction
+ *
+ * @vcpu:  the vcpu pointer
+ * @run:   access to the kvm_run structure for results
+ *
+ * We route all debug exceptions through the same handler as we
+ * just need to report the PC and the HSR values to userspace.
+ * Userspace may decide to re-inject the exception and deliver it to
+ * the guest if it wasn't for the host to deal with.
+ */
+static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu, struct kvm_run *run)
+{
+   u32 hsr = kvm_vcpu_get_hsr(vcpu);
+
+   run-exit_reason = KVM_EXIT_DEBUG;
+   run-debug.arch.hsr = hsr;
+
+   switch (hsr  ESR_ELx_EC_SHIFT) {
+   case ESR_ELx_EC_BKPT32:
+   case ESR_ELx_EC_BRK64:
+   run-debug.arch.pc = *vcpu_pc(vcpu);
+   break;
+   default:
+   kvm_err(%s: un-handled case hsr: %#08x\n,
+   __func__, (unsigned int) hsr);
+   break;
+   }
+   return 0;
+}
+
 static exit_handle_fn arm_exit_handlers[] = {
[ESR_ELx_EC_WFx]= kvm_handle_wfx,
[ESR_ELx_EC_CP15_32]= kvm_handle_cp15_32,
@@ -96,6 +127,8 @@ static exit_handle_fn arm_exit_handlers[] = {
[ESR_ELx_EC_SYS64]  = kvm_handle_sys_reg,
[ESR_ELx_EC_IABT_LOW]   = kvm_handle_guest_abort,
[ESR_ELx_EC_DABT_LOW]   = kvm_handle_guest_abort,
+   [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug,
+   [ESR_ELx_EC_BRK64]  = kvm_handle_guest_debug,
 };
 
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
-- 
2.3.4

___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


[PATCH v2 05/10] KVM: arm: introduce kvm_arch_setup/clear_debug()

2015-03-31 Thread Alex Bennée
This is a precursor for later patches which will need to do more to
setup debug state before entering the hyp.S switch code. The existing
functionality for setting mdcr_el2 has been moved out of hyp.S and now
uses the value kept in vcpu-arch.mdcr_el2.

This also moves the conditional setting of the TDA bit from the hyp code
into the C code.

Signed-off-by: Alex Bennée alex.ben...@linaro.org

 create mode 100644 arch/arm64/kvm/debug.c

diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h
index 41008cd..8c01c97 100644
--- a/arch/arm/include/asm/kvm_host.h
+++ b/arch/arm/include/asm/kvm_host.h
@@ -242,5 +242,7 @@ static inline void kvm_arch_hardware_unsetup(void) {}
 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
+static inline void kvm_arch_setup_debug(struct kvm_vcpu *vcpu) {}
+static inline void kvm_arch_clear_debug(struct kvm_vcpu *vcpu) {}
 
 #endif /* __ARM_KVM_HOST_H__ */
diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
index 445933d..7ea8b0e 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -523,6 +523,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct 
kvm_run *run)
 
kvm_vgic_flush_hwstate(vcpu);
kvm_timer_flush_hwstate(vcpu);
+   kvm_arch_setup_debug(vcpu);
 
local_irq_disable();
 
@@ -569,6 +570,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct 
kvm_run *run)
 * Back from guest
 */
 
+   kvm_arch_clear_debug(vcpu);
kvm_timer_sync_hwstate(vcpu);
kvm_vgic_sync_hwstate(vcpu);
 
diff --git a/arch/arm64/include/asm/kvm_host.h 
b/arch/arm64/include/asm/kvm_host.h
index 8ac3c70..0631840 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -101,6 +101,7 @@ struct kvm_vcpu_arch {
 
/* HYP configuration */
u64 hcr_el2;
+   u32 mdcr_el2;
 
/* Exception Information */
struct kvm_vcpu_fault_info fault;
@@ -257,4 +258,7 @@ static inline void kvm_arch_sync_events(struct kvm *kvm) {}
 static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
 
+void kvm_arch_setup_debug(struct kvm_vcpu *vcpu);
+void kvm_arch_clear_debug(struct kvm_vcpu *vcpu);
+
 #endif /* __ARM64_KVM_HOST_H__ */
diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
index f7fa65d..cd06209 100644
--- a/arch/arm64/kernel/asm-offsets.c
+++ b/arch/arm64/kernel/asm-offsets.c
@@ -122,6 +122,7 @@ int main(void)
   DEFINE(VCPU_HPFAR_EL2,   offsetof(struct kvm_vcpu, 
arch.fault.hpfar_el2));
   DEFINE(VCPU_DEBUG_FLAGS, offsetof(struct kvm_vcpu, arch.debug_flags));
   DEFINE(VCPU_HCR_EL2, offsetof(struct kvm_vcpu, arch.hcr_el2));
+  DEFINE(VCPU_MDCR_EL2,offsetof(struct kvm_vcpu, arch.mdcr_el2));
   DEFINE(VCPU_IRQ_LINES,   offsetof(struct kvm_vcpu, arch.irq_lines));
   DEFINE(VCPU_HOST_CONTEXT,offsetof(struct kvm_vcpu, 
arch.host_cpu_context));
   DEFINE(VCPU_TIMER_CNTV_CTL,  offsetof(struct kvm_vcpu, 
arch.timer_cpu.cntv_ctl));
diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile
index 4e6e09e..6796d4a 100644
--- a/arch/arm64/kvm/Makefile
+++ b/arch/arm64/kvm/Makefile
@@ -17,7 +17,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o
 
 kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o
 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
-kvm-$(CONFIG_KVM_ARM_HOST) += guest.o reset.o sys_regs.o sys_regs_generic_v8.o
+kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o 
sys_regs_generic_v8.o
 
 kvm-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic.o
 kvm-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic-v2.o
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
new file mode 100644
index 000..8a29d0b
--- /dev/null
+++ b/arch/arm64/kvm/debug.c
@@ -0,0 +1,58 @@
+/*
+ * Debug and Guest Debug support
+ *
+ * Copyright (C) 2015 - Linaro Ltd
+ * Author: Alex Bennée alex.ben...@linaro.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/.
+ */
+
+#include linux/kvm_host.h
+
+#include asm/kvm_arm.h
+#include asm/kvm_host.h
+
+/**
+ * kvm_arch_setup_debug - set-up debug