hw_breakpoint.c 23 KB

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