hw_breakpoint.c 24 KB

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