hw_breakpoint.c 26 KB

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