hw_breakpoint.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  4. * using the CPU's debug registers.
  5. *
  6. * Copyright (C) 2012 ARM Limited
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. */
  9. #define pr_fmt(fmt) "hw-breakpoint: " fmt
  10. #include <linux/compat.h>
  11. #include <linux/cpu_pm.h>
  12. #include <linux/errno.h>
  13. #include <linux/hw_breakpoint.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/smp.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/current.h>
  20. #include <asm/debug-monitors.h>
  21. #include <asm/esr.h>
  22. #include <asm/hw_breakpoint.h>
  23. #include <asm/traps.h>
  24. #include <asm/cputype.h>
  25. #include <asm/system_misc.h>
  26. /* Breakpoint currently in use for each BRP. */
  27. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
  28. /* Watchpoint currently in use for each WRP. */
  29. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
  30. /* Currently stepping a per-CPU kernel breakpoint. */
  31. static DEFINE_PER_CPU(int, stepping_kernel_bp);
  32. /* Number of BRP/WRP registers on this CPU. */
  33. static int core_num_brps;
  34. static int core_num_wrps;
  35. int hw_breakpoint_slots(int type)
  36. {
  37. /*
  38. * We can be called early, so don't rely on
  39. * our static variables being initialised.
  40. */
  41. switch (type) {
  42. case TYPE_INST:
  43. return get_num_brps();
  44. case TYPE_DATA:
  45. return get_num_wrps();
  46. default:
  47. pr_warn("unknown slot type: %d\n", type);
  48. return 0;
  49. }
  50. }
  51. #define READ_WB_REG_CASE(OFF, N, REG, VAL) \
  52. case (OFF + N): \
  53. AARCH64_DBG_READ(N, REG, VAL); \
  54. break
  55. #define WRITE_WB_REG_CASE(OFF, N, REG, VAL) \
  56. case (OFF + N): \
  57. AARCH64_DBG_WRITE(N, REG, VAL); \
  58. break
  59. #define GEN_READ_WB_REG_CASES(OFF, REG, VAL) \
  60. READ_WB_REG_CASE(OFF, 0, REG, VAL); \
  61. READ_WB_REG_CASE(OFF, 1, REG, VAL); \
  62. READ_WB_REG_CASE(OFF, 2, REG, VAL); \
  63. READ_WB_REG_CASE(OFF, 3, REG, VAL); \
  64. READ_WB_REG_CASE(OFF, 4, REG, VAL); \
  65. READ_WB_REG_CASE(OFF, 5, REG, VAL); \
  66. READ_WB_REG_CASE(OFF, 6, REG, VAL); \
  67. READ_WB_REG_CASE(OFF, 7, REG, VAL); \
  68. READ_WB_REG_CASE(OFF, 8, REG, VAL); \
  69. READ_WB_REG_CASE(OFF, 9, REG, VAL); \
  70. READ_WB_REG_CASE(OFF, 10, REG, VAL); \
  71. READ_WB_REG_CASE(OFF, 11, REG, VAL); \
  72. READ_WB_REG_CASE(OFF, 12, REG, VAL); \
  73. READ_WB_REG_CASE(OFF, 13, REG, VAL); \
  74. READ_WB_REG_CASE(OFF, 14, REG, VAL); \
  75. READ_WB_REG_CASE(OFF, 15, REG, VAL)
  76. #define GEN_WRITE_WB_REG_CASES(OFF, REG, VAL) \
  77. WRITE_WB_REG_CASE(OFF, 0, REG, VAL); \
  78. WRITE_WB_REG_CASE(OFF, 1, REG, VAL); \
  79. WRITE_WB_REG_CASE(OFF, 2, REG, VAL); \
  80. WRITE_WB_REG_CASE(OFF, 3, REG, VAL); \
  81. WRITE_WB_REG_CASE(OFF, 4, REG, VAL); \
  82. WRITE_WB_REG_CASE(OFF, 5, REG, VAL); \
  83. WRITE_WB_REG_CASE(OFF, 6, REG, VAL); \
  84. WRITE_WB_REG_CASE(OFF, 7, REG, VAL); \
  85. WRITE_WB_REG_CASE(OFF, 8, REG, VAL); \
  86. WRITE_WB_REG_CASE(OFF, 9, REG, VAL); \
  87. WRITE_WB_REG_CASE(OFF, 10, REG, VAL); \
  88. WRITE_WB_REG_CASE(OFF, 11, REG, VAL); \
  89. WRITE_WB_REG_CASE(OFF, 12, REG, VAL); \
  90. WRITE_WB_REG_CASE(OFF, 13, REG, VAL); \
  91. WRITE_WB_REG_CASE(OFF, 14, REG, VAL); \
  92. WRITE_WB_REG_CASE(OFF, 15, REG, VAL)
  93. static u64 read_wb_reg(int reg, int n)
  94. {
  95. u64 val = 0;
  96. switch (reg + n) {
  97. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val);
  98. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val);
  99. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val);
  100. GEN_READ_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val);
  101. default:
  102. pr_warn("attempt to read from unknown breakpoint register %d\n", n);
  103. }
  104. return val;
  105. }
  106. NOKPROBE_SYMBOL(read_wb_reg);
  107. static void write_wb_reg(int reg, int n, u64 val)
  108. {
  109. switch (reg + n) {
  110. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BVR, AARCH64_DBG_REG_NAME_BVR, val);
  111. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_BCR, AARCH64_DBG_REG_NAME_BCR, val);
  112. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WVR, AARCH64_DBG_REG_NAME_WVR, val);
  113. GEN_WRITE_WB_REG_CASES(AARCH64_DBG_REG_WCR, AARCH64_DBG_REG_NAME_WCR, val);
  114. default:
  115. pr_warn("attempt to write to unknown breakpoint register %d\n", n);
  116. }
  117. isb();
  118. }
  119. NOKPROBE_SYMBOL(write_wb_reg);
  120. /*
  121. * Convert a breakpoint privilege level to the corresponding exception
  122. * level.
  123. */
  124. static enum dbg_active_el debug_exception_level(int privilege)
  125. {
  126. switch (privilege) {
  127. case AARCH64_BREAKPOINT_EL0:
  128. return DBG_ACTIVE_EL0;
  129. case AARCH64_BREAKPOINT_EL1:
  130. return DBG_ACTIVE_EL1;
  131. default:
  132. pr_warn("invalid breakpoint privilege level %d\n", privilege);
  133. return -EINVAL;
  134. }
  135. }
  136. NOKPROBE_SYMBOL(debug_exception_level);
  137. enum hw_breakpoint_ops {
  138. HW_BREAKPOINT_INSTALL,
  139. HW_BREAKPOINT_UNINSTALL,
  140. HW_BREAKPOINT_RESTORE
  141. };
  142. static int is_compat_bp(struct perf_event *bp)
  143. {
  144. struct task_struct *tsk = bp->hw.target;
  145. /*
  146. * tsk can be NULL for per-cpu (non-ptrace) breakpoints.
  147. * In this case, use the native interface, since we don't have
  148. * the notion of a "compat CPU" and could end up relying on
  149. * deprecated behaviour if we use unaligned watchpoints in
  150. * AArch64 state.
  151. */
  152. return tsk && is_compat_thread(task_thread_info(tsk));
  153. }
  154. /**
  155. * hw_breakpoint_slot_setup - Find and setup a perf slot according to
  156. * operations
  157. *
  158. * @slots: pointer to array of slots
  159. * @max_slots: max number of slots
  160. * @bp: perf_event to setup
  161. * @ops: operation to be carried out on the slot
  162. *
  163. * Return:
  164. * slot index on success
  165. * -ENOSPC if no slot is available/matches
  166. * -EINVAL on wrong operations parameter
  167. */
  168. static int hw_breakpoint_slot_setup(struct perf_event **slots, int max_slots,
  169. struct perf_event *bp,
  170. enum hw_breakpoint_ops ops)
  171. {
  172. int i;
  173. struct perf_event **slot;
  174. for (i = 0; i < max_slots; ++i) {
  175. slot = &slots[i];
  176. switch (ops) {
  177. case HW_BREAKPOINT_INSTALL:
  178. if (!*slot) {
  179. *slot = bp;
  180. return i;
  181. }
  182. break;
  183. case HW_BREAKPOINT_UNINSTALL:
  184. if (*slot == bp) {
  185. *slot = NULL;
  186. return i;
  187. }
  188. break;
  189. case HW_BREAKPOINT_RESTORE:
  190. if (*slot == bp)
  191. return i;
  192. break;
  193. default:
  194. pr_warn_once("Unhandled hw breakpoint ops %d\n", ops);
  195. return -EINVAL;
  196. }
  197. }
  198. return -ENOSPC;
  199. }
  200. static int hw_breakpoint_control(struct perf_event *bp,
  201. enum hw_breakpoint_ops ops)
  202. {
  203. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  204. struct perf_event **slots;
  205. struct debug_info *debug_info = &current->thread.debug;
  206. int i, max_slots, ctrl_reg, val_reg, reg_enable;
  207. enum dbg_active_el dbg_el = debug_exception_level(info->ctrl.privilege);
  208. u32 ctrl;
  209. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  210. /* Breakpoint */
  211. ctrl_reg = AARCH64_DBG_REG_BCR;
  212. val_reg = AARCH64_DBG_REG_BVR;
  213. slots = this_cpu_ptr(bp_on_reg);
  214. max_slots = core_num_brps;
  215. reg_enable = !debug_info->bps_disabled;
  216. } else {
  217. /* Watchpoint */
  218. ctrl_reg = AARCH64_DBG_REG_WCR;
  219. val_reg = AARCH64_DBG_REG_WVR;
  220. slots = this_cpu_ptr(wp_on_reg);
  221. max_slots = core_num_wrps;
  222. reg_enable = !debug_info->wps_disabled;
  223. }
  224. i = hw_breakpoint_slot_setup(slots, max_slots, bp, ops);
  225. if (WARN_ONCE(i < 0, "Can't find any breakpoint slot"))
  226. return i;
  227. switch (ops) {
  228. case HW_BREAKPOINT_INSTALL:
  229. /*
  230. * Ensure debug monitors are enabled at the correct exception
  231. * level.
  232. */
  233. enable_debug_monitors(dbg_el);
  234. fallthrough;
  235. case HW_BREAKPOINT_RESTORE:
  236. /* Setup the address register. */
  237. write_wb_reg(val_reg, i, info->address);
  238. /* Setup the control register. */
  239. ctrl = encode_ctrl_reg(info->ctrl);
  240. write_wb_reg(ctrl_reg, i,
  241. reg_enable ? ctrl | 0x1 : ctrl & ~0x1);
  242. break;
  243. case HW_BREAKPOINT_UNINSTALL:
  244. /* Reset the control register. */
  245. write_wb_reg(ctrl_reg, i, 0);
  246. /*
  247. * Release the debug monitors for the correct exception
  248. * level.
  249. */
  250. disable_debug_monitors(dbg_el);
  251. break;
  252. }
  253. return 0;
  254. }
  255. /*
  256. * Install a perf counter breakpoint.
  257. */
  258. int arch_install_hw_breakpoint(struct perf_event *bp)
  259. {
  260. return hw_breakpoint_control(bp, HW_BREAKPOINT_INSTALL);
  261. }
  262. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  263. {
  264. hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL);
  265. }
  266. static int get_hbp_len(u8 hbp_len)
  267. {
  268. unsigned int len_in_bytes = 0;
  269. switch (hbp_len) {
  270. case ARM_BREAKPOINT_LEN_1:
  271. len_in_bytes = 1;
  272. break;
  273. case ARM_BREAKPOINT_LEN_2:
  274. len_in_bytes = 2;
  275. break;
  276. case ARM_BREAKPOINT_LEN_3:
  277. len_in_bytes = 3;
  278. break;
  279. case ARM_BREAKPOINT_LEN_4:
  280. len_in_bytes = 4;
  281. break;
  282. case ARM_BREAKPOINT_LEN_5:
  283. len_in_bytes = 5;
  284. break;
  285. case ARM_BREAKPOINT_LEN_6:
  286. len_in_bytes = 6;
  287. break;
  288. case ARM_BREAKPOINT_LEN_7:
  289. len_in_bytes = 7;
  290. break;
  291. case ARM_BREAKPOINT_LEN_8:
  292. len_in_bytes = 8;
  293. break;
  294. }
  295. return len_in_bytes;
  296. }
  297. /*
  298. * Check whether bp virtual address is in kernel space.
  299. */
  300. int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
  301. {
  302. unsigned int len;
  303. unsigned long va;
  304. va = hw->address;
  305. len = get_hbp_len(hw->ctrl.len);
  306. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  307. }
  308. /*
  309. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  310. * Hopefully this will disappear when ptrace can bypass the conversion
  311. * to generic breakpoint descriptions.
  312. */
  313. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  314. int *gen_len, int *gen_type, int *offset)
  315. {
  316. /* Type */
  317. switch (ctrl.type) {
  318. case ARM_BREAKPOINT_EXECUTE:
  319. *gen_type = HW_BREAKPOINT_X;
  320. break;
  321. case ARM_BREAKPOINT_LOAD:
  322. *gen_type = HW_BREAKPOINT_R;
  323. break;
  324. case ARM_BREAKPOINT_STORE:
  325. *gen_type = HW_BREAKPOINT_W;
  326. break;
  327. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  328. *gen_type = HW_BREAKPOINT_RW;
  329. break;
  330. default:
  331. return -EINVAL;
  332. }
  333. if (!ctrl.len)
  334. return -EINVAL;
  335. *offset = __ffs(ctrl.len);
  336. /* Len */
  337. switch (ctrl.len >> *offset) {
  338. case ARM_BREAKPOINT_LEN_1:
  339. *gen_len = HW_BREAKPOINT_LEN_1;
  340. break;
  341. case ARM_BREAKPOINT_LEN_2:
  342. *gen_len = HW_BREAKPOINT_LEN_2;
  343. break;
  344. case ARM_BREAKPOINT_LEN_3:
  345. *gen_len = HW_BREAKPOINT_LEN_3;
  346. break;
  347. case ARM_BREAKPOINT_LEN_4:
  348. *gen_len = HW_BREAKPOINT_LEN_4;
  349. break;
  350. case ARM_BREAKPOINT_LEN_5:
  351. *gen_len = HW_BREAKPOINT_LEN_5;
  352. break;
  353. case ARM_BREAKPOINT_LEN_6:
  354. *gen_len = HW_BREAKPOINT_LEN_6;
  355. break;
  356. case ARM_BREAKPOINT_LEN_7:
  357. *gen_len = HW_BREAKPOINT_LEN_7;
  358. break;
  359. case ARM_BREAKPOINT_LEN_8:
  360. *gen_len = HW_BREAKPOINT_LEN_8;
  361. break;
  362. default:
  363. return -EINVAL;
  364. }
  365. return 0;
  366. }
  367. /*
  368. * Construct an arch_hw_breakpoint from a perf_event.
  369. */
  370. static int arch_build_bp_info(struct perf_event *bp,
  371. const struct perf_event_attr *attr,
  372. struct arch_hw_breakpoint *hw)
  373. {
  374. /* Type */
  375. switch (attr->bp_type) {
  376. case HW_BREAKPOINT_X:
  377. hw->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  378. break;
  379. case HW_BREAKPOINT_R:
  380. hw->ctrl.type = ARM_BREAKPOINT_LOAD;
  381. break;
  382. case HW_BREAKPOINT_W:
  383. hw->ctrl.type = ARM_BREAKPOINT_STORE;
  384. break;
  385. case HW_BREAKPOINT_RW:
  386. hw->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. /* Len */
  392. switch (attr->bp_len) {
  393. case HW_BREAKPOINT_LEN_1:
  394. hw->ctrl.len = ARM_BREAKPOINT_LEN_1;
  395. break;
  396. case HW_BREAKPOINT_LEN_2:
  397. hw->ctrl.len = ARM_BREAKPOINT_LEN_2;
  398. break;
  399. case HW_BREAKPOINT_LEN_3:
  400. hw->ctrl.len = ARM_BREAKPOINT_LEN_3;
  401. break;
  402. case HW_BREAKPOINT_LEN_4:
  403. hw->ctrl.len = ARM_BREAKPOINT_LEN_4;
  404. break;
  405. case HW_BREAKPOINT_LEN_5:
  406. hw->ctrl.len = ARM_BREAKPOINT_LEN_5;
  407. break;
  408. case HW_BREAKPOINT_LEN_6:
  409. hw->ctrl.len = ARM_BREAKPOINT_LEN_6;
  410. break;
  411. case HW_BREAKPOINT_LEN_7:
  412. hw->ctrl.len = ARM_BREAKPOINT_LEN_7;
  413. break;
  414. case HW_BREAKPOINT_LEN_8:
  415. hw->ctrl.len = ARM_BREAKPOINT_LEN_8;
  416. break;
  417. default:
  418. return -EINVAL;
  419. }
  420. /*
  421. * On AArch64, we only permit breakpoints of length 4, whereas
  422. * AArch32 also requires breakpoints of length 2 for Thumb.
  423. * Watchpoints can be of length 1, 2, 4 or 8 bytes.
  424. */
  425. if (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  426. if (is_compat_bp(bp)) {
  427. if (hw->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  428. hw->ctrl.len != ARM_BREAKPOINT_LEN_4)
  429. return -EINVAL;
  430. } else if (hw->ctrl.len != ARM_BREAKPOINT_LEN_4) {
  431. /*
  432. * FIXME: Some tools (I'm looking at you perf) assume
  433. * that breakpoints should be sizeof(long). This
  434. * is nonsense. For now, we fix up the parameter
  435. * but we should probably return -EINVAL instead.
  436. */
  437. hw->ctrl.len = ARM_BREAKPOINT_LEN_4;
  438. }
  439. }
  440. /* Address */
  441. hw->address = attr->bp_addr;
  442. /*
  443. * Privilege
  444. * Note that we disallow combined EL0/EL1 breakpoints because
  445. * that would complicate the stepping code.
  446. */
  447. if (arch_check_bp_in_kernelspace(hw))
  448. hw->ctrl.privilege = AARCH64_BREAKPOINT_EL1;
  449. else
  450. hw->ctrl.privilege = AARCH64_BREAKPOINT_EL0;
  451. /* Enabled? */
  452. hw->ctrl.enabled = !attr->disabled;
  453. return 0;
  454. }
  455. /*
  456. * Validate the arch-specific HW Breakpoint register settings.
  457. */
  458. int hw_breakpoint_arch_parse(struct perf_event *bp,
  459. const struct perf_event_attr *attr,
  460. struct arch_hw_breakpoint *hw)
  461. {
  462. int ret;
  463. u64 alignment_mask, offset;
  464. /* Build the arch_hw_breakpoint. */
  465. ret = arch_build_bp_info(bp, attr, hw);
  466. if (ret)
  467. return ret;
  468. /*
  469. * Check address alignment.
  470. * We don't do any clever alignment correction for watchpoints
  471. * because using 64-bit unaligned addresses is deprecated for
  472. * AArch64.
  473. *
  474. * AArch32 tasks expect some simple alignment fixups, so emulate
  475. * that here.
  476. */
  477. if (is_compat_bp(bp)) {
  478. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_8)
  479. alignment_mask = 0x7;
  480. else
  481. alignment_mask = 0x3;
  482. offset = hw->address & alignment_mask;
  483. switch (offset) {
  484. case 0:
  485. /* Aligned */
  486. break;
  487. case 1:
  488. case 2:
  489. /* Allow halfword watchpoints and breakpoints. */
  490. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_2)
  491. break;
  492. fallthrough;
  493. case 3:
  494. /* Allow single byte watchpoint. */
  495. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_1)
  496. break;
  497. fallthrough;
  498. default:
  499. return -EINVAL;
  500. }
  501. } else {
  502. if (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE)
  503. alignment_mask = 0x3;
  504. else
  505. alignment_mask = 0x7;
  506. offset = hw->address & alignment_mask;
  507. }
  508. hw->address &= ~alignment_mask;
  509. hw->ctrl.len <<= offset;
  510. /*
  511. * Disallow per-task kernel breakpoints since these would
  512. * complicate the stepping code.
  513. */
  514. if (hw->ctrl.privilege == AARCH64_BREAKPOINT_EL1 && bp->hw.target)
  515. return -EINVAL;
  516. return 0;
  517. }
  518. /*
  519. * Enable/disable all of the breakpoints active at the specified
  520. * exception level at the register level.
  521. * This is used when single-stepping after a breakpoint exception.
  522. */
  523. static void toggle_bp_registers(int reg, enum dbg_active_el el, int enable)
  524. {
  525. int i, max_slots, privilege;
  526. u32 ctrl;
  527. struct perf_event **slots;
  528. switch (reg) {
  529. case AARCH64_DBG_REG_BCR:
  530. slots = this_cpu_ptr(bp_on_reg);
  531. max_slots = core_num_brps;
  532. break;
  533. case AARCH64_DBG_REG_WCR:
  534. slots = this_cpu_ptr(wp_on_reg);
  535. max_slots = core_num_wrps;
  536. break;
  537. default:
  538. return;
  539. }
  540. for (i = 0; i < max_slots; ++i) {
  541. if (!slots[i])
  542. continue;
  543. privilege = counter_arch_bp(slots[i])->ctrl.privilege;
  544. if (debug_exception_level(privilege) != el)
  545. continue;
  546. ctrl = read_wb_reg(reg, i);
  547. if (enable)
  548. ctrl |= 0x1;
  549. else
  550. ctrl &= ~0x1;
  551. write_wb_reg(reg, i, ctrl);
  552. }
  553. }
  554. NOKPROBE_SYMBOL(toggle_bp_registers);
  555. /*
  556. * Debug exception handlers.
  557. */
  558. static int breakpoint_handler(unsigned long unused, unsigned long esr,
  559. struct pt_regs *regs)
  560. {
  561. int i, step = 0, *kernel_step;
  562. u32 ctrl_reg;
  563. u64 addr, val;
  564. struct perf_event *bp, **slots;
  565. struct debug_info *debug_info;
  566. struct arch_hw_breakpoint_ctrl ctrl;
  567. slots = this_cpu_ptr(bp_on_reg);
  568. addr = instruction_pointer(regs);
  569. debug_info = &current->thread.debug;
  570. for (i = 0; i < core_num_brps; ++i) {
  571. rcu_read_lock();
  572. bp = slots[i];
  573. if (bp == NULL)
  574. goto unlock;
  575. /* Check if the breakpoint value matches. */
  576. val = read_wb_reg(AARCH64_DBG_REG_BVR, i);
  577. if (val != (addr & ~0x3))
  578. goto unlock;
  579. /* Possible match, check the byte address select to confirm. */
  580. ctrl_reg = read_wb_reg(AARCH64_DBG_REG_BCR, i);
  581. decode_ctrl_reg(ctrl_reg, &ctrl);
  582. if (!((1 << (addr & 0x3)) & ctrl.len))
  583. goto unlock;
  584. counter_arch_bp(bp)->trigger = addr;
  585. perf_bp_event(bp, regs);
  586. /* Do we need to handle the stepping? */
  587. if (is_default_overflow_handler(bp))
  588. step = 1;
  589. unlock:
  590. rcu_read_unlock();
  591. }
  592. if (!step)
  593. return 0;
  594. if (user_mode(regs)) {
  595. debug_info->bps_disabled = 1;
  596. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 0);
  597. /* If we're already stepping a watchpoint, just return. */
  598. if (debug_info->wps_disabled)
  599. return 0;
  600. if (test_thread_flag(TIF_SINGLESTEP))
  601. debug_info->suspended_step = 1;
  602. else
  603. user_enable_single_step(current);
  604. } else {
  605. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 0);
  606. kernel_step = this_cpu_ptr(&stepping_kernel_bp);
  607. if (*kernel_step != ARM_KERNEL_STEP_NONE)
  608. return 0;
  609. if (kernel_active_single_step()) {
  610. *kernel_step = ARM_KERNEL_STEP_SUSPEND;
  611. } else {
  612. *kernel_step = ARM_KERNEL_STEP_ACTIVE;
  613. kernel_enable_single_step(regs);
  614. }
  615. }
  616. return 0;
  617. }
  618. NOKPROBE_SYMBOL(breakpoint_handler);
  619. /*
  620. * Arm64 hardware does not always report a watchpoint hit address that matches
  621. * one of the watchpoints set. It can also report an address "near" the
  622. * watchpoint if a single instruction access both watched and unwatched
  623. * addresses. There is no straight-forward way, short of disassembling the
  624. * offending instruction, to map that address back to the watchpoint. This
  625. * function computes the distance of the memory access from the watchpoint as a
  626. * heuristic for the likelihood that a given access triggered the watchpoint.
  627. *
  628. * See Section D2.10.5 "Determining the memory location that caused a Watchpoint
  629. * exception" of ARMv8 Architecture Reference Manual for details.
  630. *
  631. * The function returns the distance of the address from the bytes watched by
  632. * the watchpoint. In case of an exact match, it returns 0.
  633. */
  634. static u64 get_distance_from_watchpoint(unsigned long addr, u64 val,
  635. struct arch_hw_breakpoint_ctrl *ctrl)
  636. {
  637. u64 wp_low, wp_high;
  638. u32 lens, lene;
  639. addr = untagged_addr(addr);
  640. lens = __ffs(ctrl->len);
  641. lene = __fls(ctrl->len);
  642. wp_low = val + lens;
  643. wp_high = val + lene;
  644. if (addr < wp_low)
  645. return wp_low - addr;
  646. else if (addr > wp_high)
  647. return addr - wp_high;
  648. else
  649. return 0;
  650. }
  651. static int watchpoint_report(struct perf_event *wp, unsigned long addr,
  652. struct pt_regs *regs)
  653. {
  654. int step = is_default_overflow_handler(wp);
  655. struct arch_hw_breakpoint *info = counter_arch_bp(wp);
  656. info->trigger = addr;
  657. /*
  658. * If we triggered a user watchpoint from a uaccess routine, then
  659. * handle the stepping ourselves since userspace really can't help
  660. * us with this.
  661. */
  662. if (!user_mode(regs) && info->ctrl.privilege == AARCH64_BREAKPOINT_EL0)
  663. step = 1;
  664. else
  665. perf_bp_event(wp, regs);
  666. return step;
  667. }
  668. static int watchpoint_handler(unsigned long addr, unsigned long esr,
  669. struct pt_regs *regs)
  670. {
  671. int i, step = 0, *kernel_step, access, closest_match = 0;
  672. u64 min_dist = -1, dist;
  673. u32 ctrl_reg;
  674. u64 val;
  675. struct perf_event *wp, **slots;
  676. struct debug_info *debug_info;
  677. struct arch_hw_breakpoint_ctrl ctrl;
  678. slots = this_cpu_ptr(wp_on_reg);
  679. debug_info = &current->thread.debug;
  680. /*
  681. * Find all watchpoints that match the reported address. If no exact
  682. * match is found. Attribute the hit to the closest watchpoint.
  683. */
  684. rcu_read_lock();
  685. for (i = 0; i < core_num_wrps; ++i) {
  686. wp = slots[i];
  687. if (wp == NULL)
  688. continue;
  689. /*
  690. * Check that the access type matches.
  691. * 0 => load, otherwise => store
  692. */
  693. access = (esr & ESR_ELx_WNR) ? HW_BREAKPOINT_W :
  694. HW_BREAKPOINT_R;
  695. if (!(access & hw_breakpoint_type(wp)))
  696. continue;
  697. /* Check if the watchpoint value and byte select match. */
  698. val = read_wb_reg(AARCH64_DBG_REG_WVR, i);
  699. ctrl_reg = read_wb_reg(AARCH64_DBG_REG_WCR, i);
  700. decode_ctrl_reg(ctrl_reg, &ctrl);
  701. dist = get_distance_from_watchpoint(addr, val, &ctrl);
  702. if (dist < min_dist) {
  703. min_dist = dist;
  704. closest_match = i;
  705. }
  706. /* Is this an exact match? */
  707. if (dist != 0)
  708. continue;
  709. step = watchpoint_report(wp, addr, regs);
  710. }
  711. /* No exact match found? */
  712. if (min_dist > 0 && min_dist != -1)
  713. step = watchpoint_report(slots[closest_match], addr, regs);
  714. rcu_read_unlock();
  715. if (!step)
  716. return 0;
  717. /*
  718. * We always disable EL0 watchpoints because the kernel can
  719. * cause these to fire via an unprivileged access.
  720. */
  721. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 0);
  722. if (user_mode(regs)) {
  723. debug_info->wps_disabled = 1;
  724. /* If we're already stepping a breakpoint, just return. */
  725. if (debug_info->bps_disabled)
  726. return 0;
  727. if (test_thread_flag(TIF_SINGLESTEP))
  728. debug_info->suspended_step = 1;
  729. else
  730. user_enable_single_step(current);
  731. } else {
  732. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 0);
  733. kernel_step = this_cpu_ptr(&stepping_kernel_bp);
  734. if (*kernel_step != ARM_KERNEL_STEP_NONE)
  735. return 0;
  736. if (kernel_active_single_step()) {
  737. *kernel_step = ARM_KERNEL_STEP_SUSPEND;
  738. } else {
  739. *kernel_step = ARM_KERNEL_STEP_ACTIVE;
  740. kernel_enable_single_step(regs);
  741. }
  742. }
  743. return 0;
  744. }
  745. NOKPROBE_SYMBOL(watchpoint_handler);
  746. /*
  747. * Handle single-step exception.
  748. */
  749. int reinstall_suspended_bps(struct pt_regs *regs)
  750. {
  751. struct debug_info *debug_info = &current->thread.debug;
  752. int handled_exception = 0, *kernel_step;
  753. kernel_step = this_cpu_ptr(&stepping_kernel_bp);
  754. /*
  755. * Called from single-step exception handler.
  756. * Return 0 if execution can resume, 1 if a SIGTRAP should be
  757. * reported.
  758. */
  759. if (user_mode(regs)) {
  760. if (debug_info->bps_disabled) {
  761. debug_info->bps_disabled = 0;
  762. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL0, 1);
  763. handled_exception = 1;
  764. }
  765. if (debug_info->wps_disabled) {
  766. debug_info->wps_disabled = 0;
  767. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1);
  768. handled_exception = 1;
  769. }
  770. if (handled_exception) {
  771. if (debug_info->suspended_step) {
  772. debug_info->suspended_step = 0;
  773. /* Allow exception handling to fall-through. */
  774. handled_exception = 0;
  775. } else {
  776. user_disable_single_step(current);
  777. }
  778. }
  779. } else if (*kernel_step != ARM_KERNEL_STEP_NONE) {
  780. toggle_bp_registers(AARCH64_DBG_REG_BCR, DBG_ACTIVE_EL1, 1);
  781. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL1, 1);
  782. if (!debug_info->wps_disabled)
  783. toggle_bp_registers(AARCH64_DBG_REG_WCR, DBG_ACTIVE_EL0, 1);
  784. if (*kernel_step != ARM_KERNEL_STEP_SUSPEND) {
  785. kernel_disable_single_step();
  786. handled_exception = 1;
  787. } else {
  788. handled_exception = 0;
  789. }
  790. *kernel_step = ARM_KERNEL_STEP_NONE;
  791. }
  792. return !handled_exception;
  793. }
  794. NOKPROBE_SYMBOL(reinstall_suspended_bps);
  795. /*
  796. * Context-switcher for restoring suspended breakpoints.
  797. */
  798. void hw_breakpoint_thread_switch(struct task_struct *next)
  799. {
  800. /*
  801. * current next
  802. * disabled: 0 0 => The usual case, NOTIFY_DONE
  803. * 0 1 => Disable the registers
  804. * 1 0 => Enable the registers
  805. * 1 1 => NOTIFY_DONE. per-task bps will
  806. * get taken care of by perf.
  807. */
  808. struct debug_info *current_debug_info, *next_debug_info;
  809. current_debug_info = &current->thread.debug;
  810. next_debug_info = &next->thread.debug;
  811. /* Update breakpoints. */
  812. if (current_debug_info->bps_disabled != next_debug_info->bps_disabled)
  813. toggle_bp_registers(AARCH64_DBG_REG_BCR,
  814. DBG_ACTIVE_EL0,
  815. !next_debug_info->bps_disabled);
  816. /* Update watchpoints. */
  817. if (current_debug_info->wps_disabled != next_debug_info->wps_disabled)
  818. toggle_bp_registers(AARCH64_DBG_REG_WCR,
  819. DBG_ACTIVE_EL0,
  820. !next_debug_info->wps_disabled);
  821. }
  822. /*
  823. * CPU initialisation.
  824. */
  825. static int hw_breakpoint_reset(unsigned int cpu)
  826. {
  827. int i;
  828. struct perf_event **slots;
  829. /*
  830. * When a CPU goes through cold-boot, it does not have any installed
  831. * slot, so it is safe to share the same function for restoring and
  832. * resetting breakpoints; when a CPU is hotplugged in, it goes
  833. * through the slots, which are all empty, hence it just resets control
  834. * and value for debug registers.
  835. * When this function is triggered on warm-boot through a CPU PM
  836. * notifier some slots might be initialized; if so they are
  837. * reprogrammed according to the debug slots content.
  838. */
  839. for (slots = this_cpu_ptr(bp_on_reg), i = 0; i < core_num_brps; ++i) {
  840. if (slots[i]) {
  841. hw_breakpoint_control(slots[i], HW_BREAKPOINT_RESTORE);
  842. } else {
  843. write_wb_reg(AARCH64_DBG_REG_BCR, i, 0UL);
  844. write_wb_reg(AARCH64_DBG_REG_BVR, i, 0UL);
  845. }
  846. }
  847. for (slots = this_cpu_ptr(wp_on_reg), i = 0; i < core_num_wrps; ++i) {
  848. if (slots[i]) {
  849. hw_breakpoint_control(slots[i], HW_BREAKPOINT_RESTORE);
  850. } else {
  851. write_wb_reg(AARCH64_DBG_REG_WCR, i, 0UL);
  852. write_wb_reg(AARCH64_DBG_REG_WVR, i, 0UL);
  853. }
  854. }
  855. return 0;
  856. }
  857. /*
  858. * One-time initialisation.
  859. */
  860. static int __init arch_hw_breakpoint_init(void)
  861. {
  862. int ret;
  863. core_num_brps = get_num_brps();
  864. core_num_wrps = get_num_wrps();
  865. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  866. core_num_brps, core_num_wrps);
  867. /* Register debug fault handlers. */
  868. hook_debug_fault_code(DBG_ESR_EVT_HWBP, breakpoint_handler, SIGTRAP,
  869. TRAP_HWBKPT, "hw-breakpoint handler");
  870. hook_debug_fault_code(DBG_ESR_EVT_HWWP, watchpoint_handler, SIGTRAP,
  871. TRAP_HWBKPT, "hw-watchpoint handler");
  872. /*
  873. * Reset the breakpoint resources. We assume that a halting
  874. * debugger will leave the world in a nice state for us.
  875. */
  876. ret = cpuhp_setup_state(CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING,
  877. "perf/arm64/hw_breakpoint:starting",
  878. hw_breakpoint_reset, NULL);
  879. if (ret)
  880. pr_err("failed to register CPU hotplug notifier: %d\n", ret);
  881. /* Register cpu_suspend hw breakpoint restore hook */
  882. cpu_suspend_set_dbg_restorer(hw_breakpoint_reset);
  883. return ret;
  884. }
  885. arch_initcall(arch_hw_breakpoint_init);
  886. void hw_breakpoint_pmu_read(struct perf_event *bp)
  887. {
  888. }
  889. /*
  890. * Dummy function to register with die_notifier.
  891. */
  892. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  893. unsigned long val, void *data)
  894. {
  895. return NOTIFY_DONE;
  896. }