hw_breakpoint.c 23 KB

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