hw_breakpoint.c 24 KB

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