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