hw_breakpoint.c 26 KB

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