hw_breakpoint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  3. * using the CPU's debug registers.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. * Author: Will Deacon <will.deacon@arm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define pr_fmt(fmt) "hw-breakpoint: " fmt
  21. #include <linux/errno.h>
  22. #include <linux/hw_breakpoint.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/smp.h>
  26. #include <asm/compat.h>
  27. #include <asm/current.h>
  28. #include <asm/debug-monitors.h>
  29. #include <asm/hw_breakpoint.h>
  30. #include <asm/kdebug.h>
  31. #include <asm/traps.h>
  32. #include <asm/cputype.h>
  33. #include <asm/system_misc.h>
  34. /* Breakpoint currently in use for each BRP. */
  35. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
  36. /* Watchpoint currently in use for each WRP. */
  37. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
  38. /* Currently stepping a per-CPU kernel breakpoint. */
  39. static DEFINE_PER_CPU(int, stepping_kernel_bp);
  40. /* Number of BRP/WRP registers on this CPU. */
  41. static int core_num_brps;
  42. static int core_num_wrps;
  43. /* Determine number of BRP registers available. */
  44. static int get_num_brps(void)
  45. {
  46. return ((read_cpuid(ID_AA64DFR0_EL1) >> 12) & 0xf) + 1;
  47. }
  48. /* Determine number of WRP registers available. */
  49. static int get_num_wrps(void)
  50. {
  51. return ((read_cpuid(ID_AA64DFR0_EL1) >> 20) & 0xf) + 1;
  52. }
  53. int hw_breakpoint_slots(int type)
  54. {
  55. /*
  56. * We can be called early, so don't rely on
  57. * our static variables being initialised.
  58. */
  59. switch (type) {
  60. case TYPE_INST:
  61. return get_num_brps();
  62. case TYPE_DATA:
  63. return get_num_wrps();
  64. default:
  65. pr_warning("unknown slot type: %d\n", type);
  66. return 0;
  67. }
  68. }
  69. #define READ_WB_REG_CASE(OFF, N, REG, VAL) \
  70. case (OFF + N): \
  71. AARCH64_DBG_READ(N, REG, VAL); \
  72. break
  73. #define WRITE_WB_REG_CASE(OFF, N, REG, VAL) \
  74. case (OFF + N): \
  75. AARCH64_DBG_WRITE(N, REG, VAL); \
  76. break
  77. #define GEN_READ_WB_REG_CASES(OFF, REG, VAL) \
  78. READ_WB_REG_CASE(OFF, 0, REG, VAL); \
  79. READ_WB_REG_CASE(OFF, 1, REG, VAL); \
  80. READ_WB_REG_CASE(OFF, 2, REG, VAL); \
  81. READ_WB_REG_CASE(OFF, 3, REG, VAL); \
  82. READ_WB_REG_CASE(OFF, 4, REG, VAL); \
  83. READ_WB_REG_CASE(OFF, 5, REG, VAL); \
  84. READ_WB_REG_CASE(OFF, 6, REG, VAL); \
  85. READ_WB_REG_CASE(OFF, 7, REG, VAL); \
  86. READ_WB_REG_CASE(OFF, 8, REG, VAL); \
  87. READ_WB_REG_CASE(OFF, 9, REG, VAL); \
  88. READ_WB_REG_CASE(OFF, 10, REG, VAL); \
  89. READ_WB_REG_CASE(OFF, 11, REG, VAL); \
  90. READ_WB_REG_CASE(OFF, 12, REG, VAL); \
  91. READ_WB_REG_CASE(OFF, 13, REG, VAL); \
  92. READ_WB_REG_CASE(OFF, 14, REG, VAL); \
  93. READ_WB_REG_CASE(OFF, 15, REG, VAL)
  94. #define GEN_WRITE_WB_REG_CASES(OFF, REG, VAL) \
  95. WRITE_WB_REG_CASE(OFF, 0, REG, VAL); \
  96. WRITE_WB_REG_CASE(OFF, 1, REG, VAL); \
  97. WRITE_WB_REG_CASE(OFF, 2, REG, VAL); \
  98. WRITE_WB_REG_CASE(OFF, 3, REG, VAL); \
  99. WRITE_WB_REG_CASE(OFF, 4, REG, VAL); \
  100. WRITE_WB_REG_CASE(OFF, 5, REG, VAL); \
  101. WRITE_WB_REG_CASE(OFF, 6, REG, VAL); \
  102. WRITE_WB_REG_CASE(OFF, 7, REG, VAL); \
  103. WRITE_WB_REG_CASE(OFF, 8, REG, VAL); \
  104. WRITE_WB_REG_CASE(OFF, 9, REG, VAL); \
  105. WRITE_WB_REG_CASE(OFF, 10, REG, VAL); \
  106. WRITE_WB_REG_CASE(OFF, 11, REG, VAL); \
  107. WRITE_WB_REG_CASE(OFF, 12, REG, VAL); \
  108. WRITE_WB_REG_CASE(OFF, 13, REG, VAL); \
  109. WRITE_WB_REG_CASE(OFF, 14, REG, VAL); \
  110. WRITE_WB_REG_CASE(OFF, 15, REG, VAL)
  111. static u64 read_wb_reg(int reg, int n)
  112. {
  113. u64 val = 0;
  114. switch (reg + n) {
  115. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val);
  116. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val);
  117. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val);
  118. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val);
  119. default:
  120. pr_warning("attempt to read from unknown breakpoint register %d\n", n);
  121. }
  122. return val;
  123. }
  124. static void write_wb_reg(int reg, int n, u64 val)
  125. {
  126. switch (reg + n) {
  127. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val);
  128. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val);
  129. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val);
  130. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val);
  131. default:
  132. pr_warning("attempt to write to unknown breakpoint register %d\n", n);
  133. }
  134. isb();
  135. }
  136. /*
  137. * Convert a breakpoint privilege level to the corresponding exception
  138. * level.
  139. */
  140. static enum debug_el debug_exception_level(int privilege)
  141. {
  142. switch (privilege) {
  143. case AARCH64_BREAKPOINT_EL0:
  144. return DBG_ACTIVE_EL0;
  145. case AARCH64_BREAKPOINT_EL1:
  146. return DBG_ACTIVE_EL1;
  147. default:
  148. pr_warning("invalid breakpoint privilege level %d\n", privilege);
  149. return -EINVAL;
  150. }
  151. }
  152. /*
  153. * Install a perf counter breakpoint.
  154. */
  155. int arch_install_hw_breakpoint(struct perf_event *bp)
  156. {
  157. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  158. struct perf_event **slot, **slots;
  159. struct debug_info *debug_info = &current->thread.debug;
  160. int i, max_slots, ctrl_reg, val_reg, reg_enable;
  161. u32 ctrl;
  162. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  163. /* Breakpoint */
  164. ctrl_reg = AARCH64_DBG_REG_BCR;
  165. val_reg = AARCH64_DBG_REG_BVR;
  166. slots = __get_cpu_var(bp_on_reg);
  167. max_slots = core_num_brps;
  168. reg_enable = !debug_info->bps_disabled;
  169. } else {
  170. /* Watchpoint */
  171. ctrl_reg = AARCH64_DBG_REG_WCR;
  172. val_reg = AARCH64_DBG_REG_WVR;
  173. slots = __get_cpu_var(wp_on_reg);
  174. max_slots = core_num_wrps;
  175. reg_enable = !debug_info->wps_disabled;
  176. }
  177. for (i = 0; i < max_slots; ++i) {
  178. slot = &slots[i];
  179. if (!*slot) {
  180. *slot = bp;
  181. break;
  182. }
  183. }
  184. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
  185. return -ENOSPC;
  186. /* Ensure debug monitors are enabled at the correct exception level. */
  187. enable_debug_monitors(debug_exception_level(info->ctrl.privilege));
  188. /* Setup the address register. */
  189. write_wb_reg(val_reg, i, info->address);
  190. /* Setup the control register. */
  191. ctrl = encode_ctrl_reg(info->ctrl);
  192. write_wb_reg(ctrl_reg, i, reg_enable ? ctrl | 0x1 : ctrl & ~0x1);
  193. return 0;
  194. }
  195. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  196. {
  197. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  198. struct perf_event **slot, **slots;
  199. int i, max_slots, base;
  200. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  201. /* Breakpoint */
  202. base = AARCH64_DBG_REG_BCR;
  203. slots = __get_cpu_var(bp_on_reg);
  204. max_slots = core_num_brps;
  205. } else {
  206. /* Watchpoint */
  207. base = AARCH64_DBG_REG_WCR;
  208. slots = __get_cpu_var(wp_on_reg);
  209. max_slots = core_num_wrps;
  210. }
  211. /* Remove the breakpoint. */
  212. for (i = 0; i < max_slots; ++i) {
  213. slot = &slots[i];
  214. if (*slot == bp) {
  215. *slot = NULL;
  216. break;
  217. }
  218. }
  219. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
  220. return;
  221. /* Reset the control register. */
  222. write_wb_reg(base, i, 0);
  223. /* Release the debug monitors for the correct exception level. */
  224. disable_debug_monitors(debug_exception_level(info->ctrl.privilege));
  225. }
  226. static int get_hbp_len(u8 hbp_len)
  227. {
  228. unsigned int len_in_bytes = 0;
  229. switch (hbp_len) {
  230. case ARM_BREAKPOINT_LEN_1:
  231. len_in_bytes = 1;
  232. break;
  233. case ARM_BREAKPOINT_LEN_2:
  234. len_in_bytes = 2;
  235. break;
  236. case ARM_BREAKPOINT_LEN_4:
  237. len_in_bytes = 4;
  238. break;
  239. case ARM_BREAKPOINT_LEN_8:
  240. len_in_bytes = 8;
  241. break;
  242. }
  243. return len_in_bytes;
  244. }
  245. /*
  246. * Check whether bp virtual address is in kernel space.
  247. */
  248. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  249. {
  250. unsigned int len;
  251. unsigned long va;
  252. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  253. va = info->address;
  254. len = get_hbp_len(info->ctrl.len);
  255. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  256. }
  257. /*
  258. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  259. * Hopefully this will disappear when ptrace can bypass the conversion
  260. * to generic breakpoint descriptions.
  261. */
  262. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  263. int *gen_len, int *gen_type)
  264. {
  265. /* Type */
  266. switch (ctrl.type) {
  267. case ARM_BREAKPOINT_EXECUTE:
  268. *gen_type = HW_BREAKPOINT_X;
  269. break;
  270. case ARM_BREAKPOINT_LOAD:
  271. *gen_type = HW_BREAKPOINT_R;
  272. break;
  273. case ARM_BREAKPOINT_STORE:
  274. *gen_type = HW_BREAKPOINT_W;
  275. break;
  276. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  277. *gen_type = HW_BREAKPOINT_RW;
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. /* Len */
  283. switch (ctrl.len) {
  284. case ARM_BREAKPOINT_LEN_1:
  285. *gen_len = HW_BREAKPOINT_LEN_1;
  286. break;
  287. case ARM_BREAKPOINT_LEN_2:
  288. *gen_len = HW_BREAKPOINT_LEN_2;
  289. break;
  290. case ARM_BREAKPOINT_LEN_4:
  291. *gen_len = HW_BREAKPOINT_LEN_4;
  292. break;
  293. case ARM_BREAKPOINT_LEN_8:
  294. *gen_len = HW_BREAKPOINT_LEN_8;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Construct an arch_hw_breakpoint from a perf_event.
  303. */
  304. static int arch_build_bp_info(struct perf_event *bp)
  305. {
  306. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  307. /* Type */
  308. switch (bp->attr.bp_type) {
  309. case HW_BREAKPOINT_X:
  310. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  311. break;
  312. case HW_BREAKPOINT_R:
  313. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  314. break;
  315. case HW_BREAKPOINT_W:
  316. info->ctrl.type = ARM_BREAKPOINT_STORE;
  317. break;
  318. case HW_BREAKPOINT_RW:
  319. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. /* Len */
  325. switch (bp->attr.bp_len) {
  326. case HW_BREAKPOINT_LEN_1:
  327. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  328. break;
  329. case HW_BREAKPOINT_LEN_2:
  330. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  331. break;
  332. case HW_BREAKPOINT_LEN_4:
  333. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  334. break;
  335. case HW_BREAKPOINT_LEN_8:
  336. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  337. break;
  338. default:
  339. return -EINVAL;
  340. }
  341. /*
  342. * On AArch64, we only permit breakpoints of length 4, whereas
  343. * AArch32 also requires breakpoints of length 2 for Thumb.
  344. * Watchpoints can be of length 1, 2, 4 or 8 bytes.
  345. */
  346. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  347. if (is_compat_task()) {
  348. if (info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  349. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  350. return -EINVAL;
  351. } else if (info->ctrl.len != ARM_BREAKPOINT_LEN_4) {
  352. /*
  353. * FIXME: Some tools (I'm looking at you perf) assume
  354. * that breakpoints should be sizeof(long). This
  355. * is nonsense. For now, we fix up the parameter
  356. * but we should probably return -EINVAL instead.
  357. */
  358. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  359. }
  360. }
  361. /* Address */
  362. info->address = bp->attr.bp_addr;
  363. /*
  364. * Privilege
  365. * Note that we disallow combined EL0/EL1 breakpoints because
  366. * that would complicate the stepping code.
  367. */
  368. if (arch_check_bp_in_kernelspace(bp))
  369. info->ctrl.privilege = AARCH64_BREAKPOINT_EL1;
  370. else
  371. info->ctrl.privilege = AARCH64_BREAKPOINT_EL0;
  372. /* Enabled? */
  373. info->ctrl.enabled = !bp->attr.disabled;
  374. return 0;
  375. }
  376. /*
  377. * Validate the arch-specific HW Breakpoint register settings.
  378. */
  379. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  380. {
  381. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  382. int ret;
  383. u64 alignment_mask, offset;
  384. /* Build the arch_hw_breakpoint. */
  385. ret = arch_build_bp_info(bp);
  386. if (ret)
  387. return ret;
  388. /*
  389. * Check address alignment.
  390. * We don't do any clever alignment correction for watchpoints
  391. * because using 64-bit unaligned addresses is deprecated for
  392. * AArch64.
  393. *
  394. * AArch32 tasks expect some simple alignment fixups, so emulate
  395. * that here.
  396. */
  397. if (is_compat_task()) {
  398. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  399. alignment_mask = 0x7;
  400. else
  401. alignment_mask = 0x3;
  402. offset = info->address & alignment_mask;
  403. switch (offset) {
  404. case 0:
  405. /* Aligned */
  406. break;
  407. case 1:
  408. /* Allow single byte watchpoint. */
  409. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  410. break;
  411. case 2:
  412. /* Allow halfword watchpoints and breakpoints. */
  413. if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. info->address &= ~alignment_mask;
  419. info->ctrl.len <<= offset;
  420. } else {
  421. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE)
  422. alignment_mask = 0x3;
  423. else
  424. alignment_mask = 0x7;
  425. if (info->address & alignment_mask)
  426. return -EINVAL;
  427. }
  428. /*
  429. * Disallow per-task kernel breakpoints since these would
  430. * complicate the stepping code.
  431. */
  432. if (info->ctrl.privilege == AARCH64_BREAKPOINT_EL1 && bp->hw.bp_target)
  433. return -EINVAL;
  434. return 0;
  435. }
  436. /*
  437. * Enable/disable all of the breakpoints active at the specified
  438. * exception level at the register level.
  439. * This is used when single-stepping after a breakpoint exception.
  440. */
  441. static void toggle_bp_registers(int reg, enum debug_el el, int enable)
  442. {
  443. int i, max_slots, privilege;
  444. u32 ctrl;
  445. struct perf_event **slots;
  446. switch (reg) {
  447. case AARCH64_DBG_REG_BCR:
  448. slots = __get_cpu_var(bp_on_reg);
  449. max_slots = core_num_brps;
  450. break;
  451. case AARCH64_DBG_REG_WCR:
  452. slots = __get_cpu_var(wp_on_reg);
  453. max_slots = core_num_wrps;
  454. break;
  455. default:
  456. return;
  457. }
  458. for (i = 0; i < max_slots; ++i) {
  459. if (!slots[i])
  460. continue;
  461. privilege = counter_arch_bp(slots[i])->ctrl.privilege;
  462. if (debug_exception_level(privilege) != el)
  463. continue;
  464. ctrl = read_wb_reg(reg, i);
  465. if (enable)
  466. ctrl |= 0x1;
  467. else
  468. ctrl &= ~0x1;
  469. write_wb_reg(reg, i, ctrl);
  470. }
  471. }
  472. /*
  473. * Debug exception handlers.
  474. */
  475. static int breakpoint_handler(unsigned long unused, unsigned int esr,
  476. struct pt_regs *regs)
  477. {
  478. int i, step = 0, *kernel_step;
  479. u32 ctrl_reg;
  480. u64 addr, val;
  481. struct perf_event *bp, **slots;
  482. struct debug_info *debug_info;
  483. struct arch_hw_breakpoint_ctrl ctrl;
  484. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  485. addr = instruction_pointer(regs);
  486. debug_info = &current->thread.debug;
  487. for (i = 0; i < core_num_brps; ++i) {
  488. rcu_read_lock();
  489. bp = slots[i];
  490. if (bp == NULL)
  491. goto unlock;
  492. /* Check if the breakpoint value matches. */
  493. val = read_wb_reg(AARCH64_DBG_REG_BVR, i);
  494. if (val != (addr & ~0x3))
  495. goto unlock;
  496. /* Possible match, check the byte address select to confirm. */
  497. ctrl_reg = read_wb_reg(AARCH64_DBG_REG_BCR, i);
  498. decode_ctrl_reg(ctrl_reg, &ctrl);
  499. if (!((1 << (addr & 0x3)) & ctrl.len))
  500. goto unlock;
  501. counter_arch_bp(bp)->trigger = addr;
  502. perf_bp_event(bp, regs);
  503. /* Do we need to handle the stepping? */
  504. if (!bp->overflow_handler)
  505. step = 1;
  506. unlock:
  507. rcu_read_unlock();
  508. }
  509. if (!step)
  510. return 0;
  511. if (user_mode(regs)) {
  512. debug_info->bps_disabled = 1;
  513. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 0);
  514. /* If we're already stepping a watchpoint, just return. */
  515. if (debug_info->wps_disabled)
  516. return 0;
  517. if (test_thread_flag(TIF_SINGLESTEP))
  518. debug_info->suspended_step = 1;
  519. else
  520. user_enable_single_step(current);
  521. } else {
  522. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 0);
  523. kernel_step = &__get_cpu_var(stepping_kernel_bp);
  524. if (*kernel_step != ARM_KERNEL_STEP_NONE)
  525. return 0;
  526. if (kernel_active_single_step()) {
  527. *kernel_step = ARM_KERNEL_STEP_SUSPEND;
  528. } else {
  529. *kernel_step = ARM_KERNEL_STEP_ACTIVE;
  530. kernel_enable_single_step(regs);
  531. }
  532. }
  533. return 0;
  534. }
  535. static int watchpoint_handler(unsigned long addr, unsigned int esr,
  536. struct pt_regs *regs)
  537. {
  538. int i, step = 0, *kernel_step, access;
  539. u32 ctrl_reg;
  540. u64 val, alignment_mask;
  541. struct perf_event *wp, **slots;
  542. struct debug_info *debug_info;
  543. struct arch_hw_breakpoint *info;
  544. struct arch_hw_breakpoint_ctrl ctrl;
  545. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  546. debug_info = &current->thread.debug;
  547. for (i = 0; i < core_num_wrps; ++i) {
  548. rcu_read_lock();
  549. wp = slots[i];
  550. if (wp == NULL)
  551. goto unlock;
  552. info = counter_arch_bp(wp);
  553. /* AArch32 watchpoints are either 4 or 8 bytes aligned. */
  554. if (is_compat_task()) {
  555. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  556. alignment_mask = 0x7;
  557. else
  558. alignment_mask = 0x3;
  559. } else {
  560. alignment_mask = 0x7;
  561. }
  562. /* Check if the watchpoint value matches. */
  563. val = read_wb_reg(AARCH64_DBG_REG_WVR, i);
  564. if (val != (addr & ~alignment_mask))
  565. goto unlock;
  566. /* Possible match, check the byte address select to confirm. */
  567. ctrl_reg = read_wb_reg(AARCH64_DBG_REG_WCR, i);
  568. decode_ctrl_reg(ctrl_reg, &ctrl);
  569. if (!((1 << (addr & alignment_mask)) & ctrl.len))
  570. goto unlock;
  571. /*
  572. * Check that the access type matches.
  573. * 0 => load, otherwise => store
  574. */
  575. access = (esr & AARCH64_ESR_ACCESS_MASK) ? HW_BREAKPOINT_W :
  576. HW_BREAKPOINT_R;
  577. if (!(access & hw_breakpoint_type(wp)))
  578. goto unlock;
  579. info->trigger = addr;
  580. perf_bp_event(wp, regs);
  581. /* Do we need to handle the stepping? */
  582. if (!wp->overflow_handler)
  583. step = 1;
  584. unlock:
  585. rcu_read_unlock();
  586. }
  587. if (!step)
  588. return 0;
  589. /*
  590. * We always disable EL0 watchpoints because the kernel can
  591. * cause these to fire via an unprivileged access.
  592. */
  593. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 0);
  594. if (user_mode(regs)) {
  595. debug_info->wps_disabled = 1;
  596. /* If we're already stepping a breakpoint, just return. */
  597. if (debug_info->bps_disabled)
  598. return 0;
  599. if (test_thread_flag(TIF_SINGLESTEP))
  600. debug_info->suspended_step = 1;
  601. else
  602. user_enable_single_step(current);
  603. } else {
  604. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 0);
  605. kernel_step = &__get_cpu_var(stepping_kernel_bp);
  606. if (*kernel_step != ARM_KERNEL_STEP_NONE)
  607. return 0;
  608. if (kernel_active_single_step()) {
  609. *kernel_step = ARM_KERNEL_STEP_SUSPEND;
  610. } else {
  611. *kernel_step = ARM_KERNEL_STEP_ACTIVE;
  612. kernel_enable_single_step(regs);
  613. }
  614. }
  615. return 0;
  616. }
  617. /*
  618. * Handle single-step exception.
  619. */
  620. int reinstall_suspended_bps(struct pt_regs *regs)
  621. {
  622. struct debug_info *debug_info = &current->thread.debug;
  623. int handled_exception = 0, *kernel_step;
  624. kernel_step = &__get_cpu_var(stepping_kernel_bp);
  625. /*
  626. * Called from single-step exception handler.
  627. * Return 0 if execution can resume, 1 if a SIGTRAP should be
  628. * reported.
  629. */
  630. if (user_mode(regs)) {
  631. if (debug_info->bps_disabled) {
  632. debug_info->bps_disabled = 0;
  633. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 1);
  634. handled_exception = 1;
  635. }
  636. if (debug_info->wps_disabled) {
  637. debug_info->wps_disabled = 0;
  638. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1);
  639. handled_exception = 1;
  640. }
  641. if (handled_exception) {
  642. if (debug_info->suspended_step) {
  643. debug_info->suspended_step = 0;
  644. /* Allow exception handling to fall-through. */
  645. handled_exception = 0;
  646. } else {
  647. user_disable_single_step(current);
  648. }
  649. }
  650. } else if (*kernel_step != ARM_KERNEL_STEP_NONE) {
  651. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 1);
  652. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 1);
  653. if (!debug_info->wps_disabled)
  654. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1);
  655. if (*kernel_step != ARM_KERNEL_STEP_SUSPEND) {
  656. kernel_disable_single_step();
  657. handled_exception = 1;
  658. } else {
  659. handled_exception = 0;
  660. }
  661. *kernel_step = ARM_KERNEL_STEP_NONE;
  662. }
  663. return !handled_exception;
  664. }
  665. /*
  666. * Context-switcher for restoring suspended breakpoints.
  667. */
  668. void hw_breakpoint_thread_switch(struct task_struct *next)
  669. {
  670. /*
  671. * current next
  672. * disabled: 0 0 => The usual case, NOTIFY_DONE
  673. * 0 1 => Disable the registers
  674. * 1 0 => Enable the registers
  675. * 1 1 => NOTIFY_DONE. per-task bps will
  676. * get taken care of by perf.
  677. */
  678. struct debug_info *current_debug_info, *next_debug_info;
  679. current_debug_info = &current->thread.debug;
  680. next_debug_info = &next->thread.debug;
  681. /* Update breakpoints. */
  682. if (current_debug_info->bps_disabled != next_debug_info->bps_disabled)
  683. toggle_bp_registers(AARCH64_DBG_REG_BCR,
  684. DBG_ACTIVE_EL0,
  685. !next_debug_info->bps_disabled);
  686. /* Update watchpoints. */
  687. if (current_debug_info->wps_disabled != next_debug_info->wps_disabled)
  688. toggle_bp_registers(AARCH64_DBG_REG_WCR,
  689. DBG_ACTIVE_EL0,
  690. !next_debug_info->wps_disabled);
  691. }
  692. /*
  693. * CPU initialisation.
  694. */
  695. static void reset_ctrl_regs(void *unused)
  696. {
  697. int i;
  698. for (i = 0; i < core_num_brps; ++i) {
  699. write_wb_reg(AARCH64_DBG_REG_BCR, i, 0UL);
  700. write_wb_reg(AARCH64_DBG_REG_BVR, i, 0UL);
  701. }
  702. for (i = 0; i < core_num_wrps; ++i) {
  703. write_wb_reg(AARCH64_DBG_REG_WCR, i, 0UL);
  704. write_wb_reg(AARCH64_DBG_REG_WVR, i, 0UL);
  705. }
  706. }
  707. static int __cpuinit hw_breakpoint_reset_notify(struct notifier_block *self,
  708. unsigned long action,
  709. void *hcpu)
  710. {
  711. int cpu = (long)hcpu;
  712. if (action == CPU_ONLINE)
  713. smp_call_function_single(cpu, reset_ctrl_regs, NULL, 1);
  714. return NOTIFY_OK;
  715. }
  716. static struct notifier_block __cpuinitdata hw_breakpoint_reset_nb = {
  717. .notifier_call = hw_breakpoint_reset_notify,
  718. };
  719. /*
  720. * One-time initialisation.
  721. */
  722. static int __init arch_hw_breakpoint_init(void)
  723. {
  724. core_num_brps = get_num_brps();
  725. core_num_wrps = get_num_wrps();
  726. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  727. core_num_brps, core_num_wrps);
  728. /*
  729. * Reset the breakpoint resources. We assume that a halting
  730. * debugger will leave the world in a nice state for us.
  731. */
  732. smp_call_function(reset_ctrl_regs, NULL, 1);
  733. reset_ctrl_regs(NULL);
  734. /* Register debug fault handlers. */
  735. hook_debug_fault_code(DBG_ESR_EVT_HWBP, breakpoint_handler, SIGTRAP,
  736. TRAP_HWBKPT, "hw-breakpoint handler");
  737. hook_debug_fault_code(DBG_ESR_EVT_HWWP, watchpoint_handler, SIGTRAP,
  738. TRAP_HWBKPT, "hw-watchpoint handler");
  739. /* Register hotplug notifier. */
  740. register_cpu_notifier(&hw_breakpoint_reset_nb);
  741. return 0;
  742. }
  743. arch_initcall(arch_hw_breakpoint_init);
  744. void hw_breakpoint_pmu_read(struct perf_event *bp)
  745. {
  746. }
  747. /*
  748. * Dummy function to register with die_notifier.
  749. */
  750. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  751. unsigned long val, void *data)
  752. {
  753. return NOTIFY_DONE;
  754. }