hw_breakpoint.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  4. * using the CPU's debug registers. Derived from
  5. * "arch/x86/kernel/hw_breakpoint.c"
  6. *
  7. * Copyright 2010 IBM Corporation
  8. * Author: K.Prasad <prasad@linux.vnet.ibm.com>
  9. */
  10. #include <linux/hw_breakpoint.h>
  11. #include <linux/notifier.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/percpu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/smp.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/init.h>
  20. #include <asm/hw_breakpoint.h>
  21. #include <asm/processor.h>
  22. #include <asm/sstep.h>
  23. #include <asm/debug.h>
  24. #include <asm/hvcall.h>
  25. #include <asm/inst.h>
  26. #include <linux/uaccess.h>
  27. /*
  28. * Stores the breakpoints currently in use on each breakpoint address
  29. * register for every cpu
  30. */
  31. static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM_MAX]);
  32. /*
  33. * Returns total number of data or instruction breakpoints available.
  34. */
  35. int hw_breakpoint_slots(int type)
  36. {
  37. if (type == TYPE_DATA)
  38. return nr_wp_slots();
  39. return 0; /* no instruction breakpoints available */
  40. }
  41. /*
  42. * Install a perf counter breakpoint.
  43. *
  44. * We seek a free debug address register and use it for this
  45. * breakpoint.
  46. *
  47. * Atomic: we hold the counter->ctx->lock and we only handle variables
  48. * and registers local to this cpu.
  49. */
  50. int arch_install_hw_breakpoint(struct perf_event *bp)
  51. {
  52. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  53. struct perf_event **slot;
  54. int i;
  55. for (i = 0; i < nr_wp_slots(); i++) {
  56. slot = this_cpu_ptr(&bp_per_reg[i]);
  57. if (!*slot) {
  58. *slot = bp;
  59. break;
  60. }
  61. }
  62. if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
  63. return -EBUSY;
  64. /*
  65. * Do not install DABR values if the instruction must be single-stepped.
  66. * If so, DABR will be populated in single_step_dabr_instruction().
  67. */
  68. if (!info->perf_single_step)
  69. __set_breakpoint(i, info);
  70. return 0;
  71. }
  72. /*
  73. * Uninstall the breakpoint contained in the given counter.
  74. *
  75. * First we search the debug address register it uses and then we disable
  76. * it.
  77. *
  78. * Atomic: we hold the counter->ctx->lock and we only handle variables
  79. * and registers local to this cpu.
  80. */
  81. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  82. {
  83. struct arch_hw_breakpoint null_brk = {0};
  84. struct perf_event **slot;
  85. int i;
  86. for (i = 0; i < nr_wp_slots(); i++) {
  87. slot = this_cpu_ptr(&bp_per_reg[i]);
  88. if (*slot == bp) {
  89. *slot = NULL;
  90. break;
  91. }
  92. }
  93. if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
  94. return;
  95. __set_breakpoint(i, &null_brk);
  96. }
  97. static bool is_ptrace_bp(struct perf_event *bp)
  98. {
  99. return bp->overflow_handler == ptrace_triggered;
  100. }
  101. /*
  102. * Check for virtual address in kernel space.
  103. */
  104. int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
  105. {
  106. return is_kernel_addr(hw->address);
  107. }
  108. int arch_bp_generic_fields(int type, int *gen_bp_type)
  109. {
  110. *gen_bp_type = 0;
  111. if (type & HW_BRK_TYPE_READ)
  112. *gen_bp_type |= HW_BREAKPOINT_R;
  113. if (type & HW_BRK_TYPE_WRITE)
  114. *gen_bp_type |= HW_BREAKPOINT_W;
  115. if (*gen_bp_type == 0)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. /*
  120. * Watchpoint match range is always doubleword(8 bytes) aligned on
  121. * powerpc. If the given range is crossing doubleword boundary, we
  122. * need to increase the length such that next doubleword also get
  123. * covered. Ex,
  124. *
  125. * address len = 6 bytes
  126. * |=========.
  127. * |------------v--|------v--------|
  128. * | | | | | | | | | | | | | | | | |
  129. * |---------------|---------------|
  130. * <---8 bytes--->
  131. *
  132. * In this case, we should configure hw as:
  133. * start_addr = address & ~(HW_BREAKPOINT_SIZE - 1)
  134. * len = 16 bytes
  135. *
  136. * @start_addr is inclusive but @end_addr is exclusive.
  137. */
  138. static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
  139. {
  140. u16 max_len = DABR_MAX_LEN;
  141. u16 hw_len;
  142. unsigned long start_addr, end_addr;
  143. start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE);
  144. end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE);
  145. hw_len = end_addr - start_addr;
  146. if (dawr_enabled()) {
  147. max_len = DAWR_MAX_LEN;
  148. /* DAWR region can't cross 512 bytes boundary on p10 predecessors */
  149. if (!cpu_has_feature(CPU_FTR_ARCH_31) &&
  150. (ALIGN_DOWN(start_addr, SZ_512) != ALIGN_DOWN(end_addr - 1, SZ_512)))
  151. return -EINVAL;
  152. } else if (IS_ENABLED(CONFIG_PPC_8xx)) {
  153. /* 8xx can setup a range without limitation */
  154. max_len = U16_MAX;
  155. }
  156. if (hw_len > max_len)
  157. return -EINVAL;
  158. hw->hw_len = hw_len;
  159. return 0;
  160. }
  161. /*
  162. * Validate the arch-specific HW Breakpoint register settings
  163. */
  164. int hw_breakpoint_arch_parse(struct perf_event *bp,
  165. const struct perf_event_attr *attr,
  166. struct arch_hw_breakpoint *hw)
  167. {
  168. int ret = -EINVAL;
  169. if (!bp || !attr->bp_len)
  170. return ret;
  171. hw->type = HW_BRK_TYPE_TRANSLATE;
  172. if (attr->bp_type & HW_BREAKPOINT_R)
  173. hw->type |= HW_BRK_TYPE_READ;
  174. if (attr->bp_type & HW_BREAKPOINT_W)
  175. hw->type |= HW_BRK_TYPE_WRITE;
  176. if (hw->type == HW_BRK_TYPE_TRANSLATE)
  177. /* must set alteast read or write */
  178. return ret;
  179. if (!attr->exclude_user)
  180. hw->type |= HW_BRK_TYPE_USER;
  181. if (!attr->exclude_kernel)
  182. hw->type |= HW_BRK_TYPE_KERNEL;
  183. if (!attr->exclude_hv)
  184. hw->type |= HW_BRK_TYPE_HYP;
  185. hw->address = attr->bp_addr;
  186. hw->len = attr->bp_len;
  187. if (!ppc_breakpoint_available())
  188. return -ENODEV;
  189. return hw_breakpoint_validate_len(hw);
  190. }
  191. /*
  192. * Restores the breakpoint on the debug registers.
  193. * Invoke this function if it is known that the execution context is
  194. * about to change to cause loss of MSR_SE settings.
  195. *
  196. * The perf watchpoint will simply re-trigger once the thread is started again,
  197. * and the watchpoint handler will set up MSR_SE and perf_single_step as
  198. * needed.
  199. */
  200. void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
  201. {
  202. struct arch_hw_breakpoint *info;
  203. int i;
  204. preempt_disable();
  205. for (i = 0; i < nr_wp_slots(); i++) {
  206. struct perf_event *bp = __this_cpu_read(bp_per_reg[i]);
  207. if (unlikely(bp && counter_arch_bp(bp)->perf_single_step))
  208. goto reset;
  209. }
  210. goto out;
  211. reset:
  212. regs_set_return_msr(regs, regs->msr & ~MSR_SE);
  213. for (i = 0; i < nr_wp_slots(); i++) {
  214. info = counter_arch_bp(__this_cpu_read(bp_per_reg[i]));
  215. __set_breakpoint(i, info);
  216. info->perf_single_step = false;
  217. }
  218. out:
  219. preempt_enable();
  220. }
  221. static bool is_larx_stcx_instr(int type)
  222. {
  223. return type == LARX || type == STCX;
  224. }
  225. static bool is_octword_vsx_instr(int type, int size)
  226. {
  227. return ((type == LOAD_VSX || type == STORE_VSX) && size == 32);
  228. }
  229. /*
  230. * We've failed in reliably handling the hw-breakpoint. Unregister
  231. * it and throw a warning message to let the user know about it.
  232. */
  233. static void handler_error(struct perf_event *bp)
  234. {
  235. WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.",
  236. counter_arch_bp(bp)->address);
  237. perf_event_disable_inatomic(bp);
  238. }
  239. static void larx_stcx_err(struct perf_event *bp)
  240. {
  241. printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n",
  242. counter_arch_bp(bp)->address);
  243. perf_event_disable_inatomic(bp);
  244. }
  245. static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp,
  246. int *hit, ppc_inst_t instr)
  247. {
  248. int i;
  249. int stepped;
  250. /* Do not emulate user-space instructions, instead single-step them */
  251. if (user_mode(regs)) {
  252. for (i = 0; i < nr_wp_slots(); i++) {
  253. if (!hit[i])
  254. continue;
  255. counter_arch_bp(bp[i])->perf_single_step = true;
  256. bp[i] = NULL;
  257. }
  258. regs_set_return_msr(regs, regs->msr | MSR_SE);
  259. return false;
  260. }
  261. stepped = emulate_step(regs, instr);
  262. if (!stepped) {
  263. for (i = 0; i < nr_wp_slots(); i++) {
  264. if (!hit[i])
  265. continue;
  266. handler_error(bp[i]);
  267. bp[i] = NULL;
  268. }
  269. return false;
  270. }
  271. return true;
  272. }
  273. static void handle_p10dd1_spurious_exception(struct perf_event **bp,
  274. int *hit, unsigned long ea)
  275. {
  276. int i;
  277. unsigned long hw_end_addr;
  278. /*
  279. * Handle spurious exception only when any bp_per_reg is set.
  280. * Otherwise this might be created by xmon and not actually a
  281. * spurious exception.
  282. */
  283. for (i = 0; i < nr_wp_slots(); i++) {
  284. struct arch_hw_breakpoint *info;
  285. if (!bp[i])
  286. continue;
  287. info = counter_arch_bp(bp[i]);
  288. hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
  289. /*
  290. * Ending address of DAWR range is less than starting
  291. * address of op.
  292. */
  293. if ((hw_end_addr - 1) >= ea)
  294. continue;
  295. /*
  296. * Those addresses need to be in the same or in two
  297. * consecutive 512B blocks;
  298. */
  299. if (((hw_end_addr - 1) >> 10) != (ea >> 10))
  300. continue;
  301. /*
  302. * 'op address + 64B' generates an address that has a
  303. * carry into bit 52 (crosses 2K boundary).
  304. */
  305. if ((ea & 0x800) == ((ea + 64) & 0x800))
  306. continue;
  307. break;
  308. }
  309. if (i == nr_wp_slots())
  310. return;
  311. for (i = 0; i < nr_wp_slots(); i++) {
  312. if (bp[i]) {
  313. hit[i] = 1;
  314. counter_arch_bp(bp[i])->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
  315. }
  316. }
  317. }
  318. /*
  319. * Handle a DABR or DAWR exception.
  320. *
  321. * Called in atomic context.
  322. */
  323. int hw_breakpoint_handler(struct die_args *args)
  324. {
  325. bool err = false;
  326. int rc = NOTIFY_STOP;
  327. struct perf_event *bp[HBP_NUM_MAX] = { NULL };
  328. struct pt_regs *regs = args->regs;
  329. int i;
  330. int hit[HBP_NUM_MAX] = {0};
  331. int nr_hit = 0;
  332. bool ptrace_bp = false;
  333. ppc_inst_t instr = ppc_inst(0);
  334. int type = 0;
  335. int size = 0;
  336. unsigned long ea = 0;
  337. /* Disable breakpoints during exception handling */
  338. hw_breakpoint_disable();
  339. /*
  340. * The counter may be concurrently released but that can only
  341. * occur from a call_rcu() path. We can then safely fetch
  342. * the breakpoint, use its callback, touch its counter
  343. * while we are in an rcu_read_lock() path.
  344. */
  345. rcu_read_lock();
  346. if (!IS_ENABLED(CONFIG_PPC_8xx))
  347. wp_get_instr_detail(regs, &instr, &type, &size, &ea);
  348. for (i = 0; i < nr_wp_slots(); i++) {
  349. struct arch_hw_breakpoint *info;
  350. bp[i] = __this_cpu_read(bp_per_reg[i]);
  351. if (!bp[i])
  352. continue;
  353. info = counter_arch_bp(bp[i]);
  354. info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
  355. if (wp_check_constraints(regs, instr, ea, type, size, info)) {
  356. if (!IS_ENABLED(CONFIG_PPC_8xx) &&
  357. ppc_inst_equal(instr, ppc_inst(0))) {
  358. handler_error(bp[i]);
  359. bp[i] = NULL;
  360. err = 1;
  361. continue;
  362. }
  363. if (is_ptrace_bp(bp[i]))
  364. ptrace_bp = true;
  365. hit[i] = 1;
  366. nr_hit++;
  367. }
  368. }
  369. if (err)
  370. goto reset;
  371. if (!nr_hit) {
  372. /* Workaround for Power10 DD1 */
  373. if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 &&
  374. is_octword_vsx_instr(type, size)) {
  375. handle_p10dd1_spurious_exception(bp, hit, ea);
  376. } else {
  377. rc = NOTIFY_DONE;
  378. goto out;
  379. }
  380. }
  381. /*
  382. * Return early after invoking user-callback function without restoring
  383. * DABR if the breakpoint is from ptrace which always operates in
  384. * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
  385. * generated in do_dabr().
  386. */
  387. if (ptrace_bp) {
  388. for (i = 0; i < nr_wp_slots(); i++) {
  389. if (!hit[i] || !is_ptrace_bp(bp[i]))
  390. continue;
  391. perf_bp_event(bp[i], regs);
  392. bp[i] = NULL;
  393. }
  394. rc = NOTIFY_DONE;
  395. goto reset;
  396. }
  397. if (!IS_ENABLED(CONFIG_PPC_8xx)) {
  398. if (is_larx_stcx_instr(type)) {
  399. for (i = 0; i < nr_wp_slots(); i++) {
  400. if (!hit[i])
  401. continue;
  402. larx_stcx_err(bp[i]);
  403. bp[i] = NULL;
  404. }
  405. goto reset;
  406. }
  407. if (!stepping_handler(regs, bp, hit, instr))
  408. goto reset;
  409. }
  410. /*
  411. * As a policy, the callback is invoked in a 'trigger-after-execute'
  412. * fashion
  413. */
  414. for (i = 0; i < nr_wp_slots(); i++) {
  415. if (!hit[i])
  416. continue;
  417. if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
  418. perf_bp_event(bp[i], regs);
  419. }
  420. reset:
  421. for (i = 0; i < nr_wp_slots(); i++) {
  422. if (!bp[i])
  423. continue;
  424. __set_breakpoint(i, counter_arch_bp(bp[i]));
  425. }
  426. out:
  427. rcu_read_unlock();
  428. return rc;
  429. }
  430. NOKPROBE_SYMBOL(hw_breakpoint_handler);
  431. /*
  432. * Handle single-step exceptions following a DABR hit.
  433. *
  434. * Called in atomic context.
  435. */
  436. static int single_step_dabr_instruction(struct die_args *args)
  437. {
  438. struct pt_regs *regs = args->regs;
  439. bool found = false;
  440. /*
  441. * Check if we are single-stepping as a result of a
  442. * previous HW Breakpoint exception
  443. */
  444. for (int i = 0; i < nr_wp_slots(); i++) {
  445. struct perf_event *bp;
  446. struct arch_hw_breakpoint *info;
  447. bp = __this_cpu_read(bp_per_reg[i]);
  448. if (!bp)
  449. continue;
  450. info = counter_arch_bp(bp);
  451. if (!info->perf_single_step)
  452. continue;
  453. found = true;
  454. /*
  455. * We shall invoke the user-defined callback function in the
  456. * single stepping handler to confirm to 'trigger-after-execute'
  457. * semantics
  458. */
  459. if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
  460. perf_bp_event(bp, regs);
  461. info->perf_single_step = false;
  462. __set_breakpoint(i, counter_arch_bp(bp));
  463. }
  464. /*
  465. * If the process was being single-stepped by ptrace, let the
  466. * other single-step actions occur (e.g. generate SIGTRAP).
  467. */
  468. if (!found || test_thread_flag(TIF_SINGLESTEP))
  469. return NOTIFY_DONE;
  470. return NOTIFY_STOP;
  471. }
  472. NOKPROBE_SYMBOL(single_step_dabr_instruction);
  473. /*
  474. * Handle debug exception notifications.
  475. *
  476. * Called in atomic context.
  477. */
  478. int hw_breakpoint_exceptions_notify(
  479. struct notifier_block *unused, unsigned long val, void *data)
  480. {
  481. int ret = NOTIFY_DONE;
  482. switch (val) {
  483. case DIE_DABR_MATCH:
  484. ret = hw_breakpoint_handler(data);
  485. break;
  486. case DIE_SSTEP:
  487. ret = single_step_dabr_instruction(data);
  488. break;
  489. }
  490. return ret;
  491. }
  492. NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
  493. /*
  494. * Release the user breakpoints used by ptrace
  495. */
  496. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  497. {
  498. int i;
  499. struct thread_struct *t = &tsk->thread;
  500. for (i = 0; i < nr_wp_slots(); i++) {
  501. unregister_hw_breakpoint(t->ptrace_bps[i]);
  502. t->ptrace_bps[i] = NULL;
  503. }
  504. }
  505. void hw_breakpoint_pmu_read(struct perf_event *bp)
  506. {
  507. /* TODO */
  508. }
  509. void ptrace_triggered(struct perf_event *bp,
  510. struct perf_sample_data *data, struct pt_regs *regs)
  511. {
  512. struct perf_event_attr attr;
  513. /*
  514. * Disable the breakpoint request here since ptrace has defined a
  515. * one-shot behaviour for breakpoint exceptions in PPC64.
  516. * The SIGTRAP signal is generated automatically for us in do_dabr().
  517. * We don't have to do anything about that here
  518. */
  519. attr = bp->attr;
  520. attr.disabled = true;
  521. modify_user_hw_breakpoint(bp, &attr);
  522. }