hw_breakpoint.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. */
  5. #ifndef __ASM_HW_BREAKPOINT_H
  6. #define __ASM_HW_BREAKPOINT_H
  7. #include <asm/cputype.h>
  8. #include <asm/cpufeature.h>
  9. #include <asm/sysreg.h>
  10. #include <asm/virt.h>
  11. struct arch_hw_breakpoint_ctrl {
  12. u32 __reserved : 19,
  13. len : 8,
  14. type : 2,
  15. privilege : 2,
  16. enabled : 1;
  17. };
  18. struct arch_hw_breakpoint {
  19. u64 address;
  20. u64 trigger;
  21. struct arch_hw_breakpoint_ctrl ctrl;
  22. };
  23. /* Privilege Levels */
  24. #define AARCH64_BREAKPOINT_EL1 1
  25. #define AARCH64_BREAKPOINT_EL0 2
  26. #define DBG_HMC_HYP (1 << 13)
  27. static inline u32 encode_ctrl_reg(struct arch_hw_breakpoint_ctrl ctrl)
  28. {
  29. u32 val = (ctrl.len << 5) | (ctrl.type << 3) | (ctrl.privilege << 1) |
  30. ctrl.enabled;
  31. if (is_kernel_in_hyp_mode() && ctrl.privilege == AARCH64_BREAKPOINT_EL1)
  32. val |= DBG_HMC_HYP;
  33. return val;
  34. }
  35. static inline void decode_ctrl_reg(u32 reg,
  36. struct arch_hw_breakpoint_ctrl *ctrl)
  37. {
  38. ctrl->enabled = reg & 0x1;
  39. reg >>= 1;
  40. ctrl->privilege = reg & 0x3;
  41. reg >>= 2;
  42. ctrl->type = reg & 0x3;
  43. reg >>= 2;
  44. ctrl->len = reg & 0xff;
  45. }
  46. /* Breakpoint */
  47. #define ARM_BREAKPOINT_EXECUTE 0
  48. /* Watchpoints */
  49. #define ARM_BREAKPOINT_LOAD 1
  50. #define ARM_BREAKPOINT_STORE 2
  51. /* Lengths */
  52. #define ARM_BREAKPOINT_LEN_1 0x1
  53. #define ARM_BREAKPOINT_LEN_2 0x3
  54. #define ARM_BREAKPOINT_LEN_3 0x7
  55. #define ARM_BREAKPOINT_LEN_4 0xf
  56. #define ARM_BREAKPOINT_LEN_5 0x1f
  57. #define ARM_BREAKPOINT_LEN_6 0x3f
  58. #define ARM_BREAKPOINT_LEN_7 0x7f
  59. #define ARM_BREAKPOINT_LEN_8 0xff
  60. /* Kernel stepping */
  61. #define ARM_KERNEL_STEP_NONE 0
  62. #define ARM_KERNEL_STEP_ACTIVE 1
  63. #define ARM_KERNEL_STEP_SUSPEND 2
  64. /*
  65. * Limits.
  66. * Changing these will require modifications to the register accessors.
  67. */
  68. #define ARM_MAX_BRP 16
  69. #define ARM_MAX_WRP 16
  70. /* Virtual debug register bases. */
  71. #define AARCH64_DBG_REG_BVR 0
  72. #define AARCH64_DBG_REG_BCR (AARCH64_DBG_REG_BVR + ARM_MAX_BRP)
  73. #define AARCH64_DBG_REG_WVR (AARCH64_DBG_REG_BCR + ARM_MAX_BRP)
  74. #define AARCH64_DBG_REG_WCR (AARCH64_DBG_REG_WVR + ARM_MAX_WRP)
  75. /* Debug register names. */
  76. #define AARCH64_DBG_REG_NAME_BVR bvr
  77. #define AARCH64_DBG_REG_NAME_BCR bcr
  78. #define AARCH64_DBG_REG_NAME_WVR wvr
  79. #define AARCH64_DBG_REG_NAME_WCR wcr
  80. /* Accessor macros for the debug registers. */
  81. #define AARCH64_DBG_READ(N, REG, VAL) do {\
  82. VAL = read_sysreg(dbg##REG##N##_el1);\
  83. } while (0)
  84. #define AARCH64_DBG_WRITE(N, REG, VAL) do {\
  85. write_sysreg(VAL, dbg##REG##N##_el1);\
  86. } while (0)
  87. struct task_struct;
  88. struct notifier_block;
  89. struct perf_event_attr;
  90. struct perf_event;
  91. struct pmu;
  92. extern int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  93. int *gen_len, int *gen_type, int *offset);
  94. extern int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw);
  95. extern int hw_breakpoint_arch_parse(struct perf_event *bp,
  96. const struct perf_event_attr *attr,
  97. struct arch_hw_breakpoint *hw);
  98. extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  99. unsigned long val, void *data);
  100. extern int arch_install_hw_breakpoint(struct perf_event *bp);
  101. extern void arch_uninstall_hw_breakpoint(struct perf_event *bp);
  102. extern void hw_breakpoint_pmu_read(struct perf_event *bp);
  103. extern int hw_breakpoint_slots(int type);
  104. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  105. extern void hw_breakpoint_thread_switch(struct task_struct *next);
  106. extern void ptrace_hw_copy_thread(struct task_struct *task);
  107. #else
  108. static inline void hw_breakpoint_thread_switch(struct task_struct *next)
  109. {
  110. }
  111. static inline void ptrace_hw_copy_thread(struct task_struct *task)
  112. {
  113. }
  114. #endif
  115. /* Determine number of BRP registers available. */
  116. static inline int get_num_brps(void)
  117. {
  118. u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
  119. return 1 +
  120. cpuid_feature_extract_unsigned_field(dfr0,
  121. ID_AA64DFR0_EL1_BRPs_SHIFT);
  122. }
  123. /* Determine number of WRP registers available. */
  124. static inline int get_num_wrps(void)
  125. {
  126. u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
  127. return 1 +
  128. cpuid_feature_extract_unsigned_field(dfr0,
  129. ID_AA64DFR0_EL1_WRPs_SHIFT);
  130. }
  131. #ifdef CONFIG_CPU_PM
  132. extern void cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int));
  133. #else
  134. static inline void cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int))
  135. {
  136. }
  137. #endif
  138. #endif /* __ASM_BREAKPOINT_H */