hw_breakpoint.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. *
  15. * Copyright (C) 2009, 2010 ARM Limited
  16. *
  17. * Author: Will Deacon <will.deacon@arm.com>
  18. */
  19. /*
  20. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  21. * using the CPU's debug registers.
  22. */
  23. #define pr_fmt(fmt) "hw-breakpoint: " fmt
  24. #include <linux/errno.h>
  25. #include <linux/perf_event.h>
  26. #include <linux/hw_breakpoint.h>
  27. #include <linux/smp.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/cputype.h>
  30. #include <asm/current.h>
  31. #include <asm/hw_breakpoint.h>
  32. #include <asm/kdebug.h>
  33. #include <asm/system.h>
  34. #include <asm/traps.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. /* Number of BRP/WRP registers on this CPU. */
  40. static int core_num_brps;
  41. static int core_num_wrps;
  42. /* Debug architecture version. */
  43. static u8 debug_arch;
  44. /* Maximum supported watchpoint length. */
  45. static u8 max_watchpoint_len;
  46. /* Determine number of BRP registers available. */
  47. static int get_num_brps(void)
  48. {
  49. u32 didr;
  50. ARM_DBG_READ(c0, 0, didr);
  51. return ((didr >> 24) & 0xf) + 1;
  52. }
  53. /* Determine number of WRP registers available. */
  54. static int get_num_wrps(void)
  55. {
  56. /*
  57. * FIXME: When a watchpoint fires, the only way to work out which
  58. * watchpoint it was is by disassembling the faulting instruction
  59. * and working out the address of the memory access.
  60. *
  61. * Furthermore, we can only do this if the watchpoint was precise
  62. * since imprecise watchpoints prevent us from calculating register
  63. * based addresses.
  64. *
  65. * For the time being, we only report 1 watchpoint register so we
  66. * always know which watchpoint fired. In the future we can either
  67. * add a disassembler and address generation emulator, or we can
  68. * insert a check to see if the DFAR is set on watchpoint exception
  69. * entry [the ARM ARM states that the DFAR is UNKNOWN, but
  70. * experience shows that it is set on some implementations].
  71. */
  72. #if 0
  73. u32 didr, wrps;
  74. ARM_DBG_READ(c0, 0, didr);
  75. return ((didr >> 28) & 0xf) + 1;
  76. #endif
  77. return 1;
  78. }
  79. int hw_breakpoint_slots(int type)
  80. {
  81. /*
  82. * We can be called early, so don't rely on
  83. * our static variables being initialised.
  84. */
  85. switch (type) {
  86. case TYPE_INST:
  87. return get_num_brps();
  88. case TYPE_DATA:
  89. return get_num_wrps();
  90. default:
  91. pr_warning("unknown slot type: %d\n", type);
  92. return 0;
  93. }
  94. }
  95. /* Determine debug architecture. */
  96. static u8 get_debug_arch(void)
  97. {
  98. u32 didr;
  99. /* Do we implement the extended CPUID interface? */
  100. if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
  101. pr_warning("CPUID feature registers not supported. "
  102. "Assuming v6 debug is present.\n");
  103. return ARM_DEBUG_ARCH_V6;
  104. }
  105. ARM_DBG_READ(c0, 0, didr);
  106. return (didr >> 16) & 0xf;
  107. }
  108. /* Does this core support mismatch breakpoints? */
  109. static int core_has_mismatch_bps(void)
  110. {
  111. return debug_arch >= ARM_DEBUG_ARCH_V7_ECP14 && core_num_brps > 1;
  112. }
  113. u8 arch_get_debug_arch(void)
  114. {
  115. return debug_arch;
  116. }
  117. #define READ_WB_REG_CASE(OP2, M, VAL) \
  118. case ((OP2 << 4) + M): \
  119. ARM_DBG_READ(c ## M, OP2, VAL); \
  120. break
  121. #define WRITE_WB_REG_CASE(OP2, M, VAL) \
  122. case ((OP2 << 4) + M): \
  123. ARM_DBG_WRITE(c ## M, OP2, VAL);\
  124. break
  125. #define GEN_READ_WB_REG_CASES(OP2, VAL) \
  126. READ_WB_REG_CASE(OP2, 0, VAL); \
  127. READ_WB_REG_CASE(OP2, 1, VAL); \
  128. READ_WB_REG_CASE(OP2, 2, VAL); \
  129. READ_WB_REG_CASE(OP2, 3, VAL); \
  130. READ_WB_REG_CASE(OP2, 4, VAL); \
  131. READ_WB_REG_CASE(OP2, 5, VAL); \
  132. READ_WB_REG_CASE(OP2, 6, VAL); \
  133. READ_WB_REG_CASE(OP2, 7, VAL); \
  134. READ_WB_REG_CASE(OP2, 8, VAL); \
  135. READ_WB_REG_CASE(OP2, 9, VAL); \
  136. READ_WB_REG_CASE(OP2, 10, VAL); \
  137. READ_WB_REG_CASE(OP2, 11, VAL); \
  138. READ_WB_REG_CASE(OP2, 12, VAL); \
  139. READ_WB_REG_CASE(OP2, 13, VAL); \
  140. READ_WB_REG_CASE(OP2, 14, VAL); \
  141. READ_WB_REG_CASE(OP2, 15, VAL)
  142. #define GEN_WRITE_WB_REG_CASES(OP2, VAL) \
  143. WRITE_WB_REG_CASE(OP2, 0, VAL); \
  144. WRITE_WB_REG_CASE(OP2, 1, VAL); \
  145. WRITE_WB_REG_CASE(OP2, 2, VAL); \
  146. WRITE_WB_REG_CASE(OP2, 3, VAL); \
  147. WRITE_WB_REG_CASE(OP2, 4, VAL); \
  148. WRITE_WB_REG_CASE(OP2, 5, VAL); \
  149. WRITE_WB_REG_CASE(OP2, 6, VAL); \
  150. WRITE_WB_REG_CASE(OP2, 7, VAL); \
  151. WRITE_WB_REG_CASE(OP2, 8, VAL); \
  152. WRITE_WB_REG_CASE(OP2, 9, VAL); \
  153. WRITE_WB_REG_CASE(OP2, 10, VAL); \
  154. WRITE_WB_REG_CASE(OP2, 11, VAL); \
  155. WRITE_WB_REG_CASE(OP2, 12, VAL); \
  156. WRITE_WB_REG_CASE(OP2, 13, VAL); \
  157. WRITE_WB_REG_CASE(OP2, 14, VAL); \
  158. WRITE_WB_REG_CASE(OP2, 15, VAL)
  159. static u32 read_wb_reg(int n)
  160. {
  161. u32 val = 0;
  162. switch (n) {
  163. GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
  164. GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
  165. GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
  166. GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
  167. default:
  168. pr_warning("attempt to read from unknown breakpoint "
  169. "register %d\n", n);
  170. }
  171. return val;
  172. }
  173. static void write_wb_reg(int n, u32 val)
  174. {
  175. switch (n) {
  176. GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
  177. GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
  178. GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
  179. GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
  180. default:
  181. pr_warning("attempt to write to unknown breakpoint "
  182. "register %d\n", n);
  183. }
  184. isb();
  185. }
  186. /*
  187. * In order to access the breakpoint/watchpoint control registers,
  188. * we must be running in debug monitor mode. Unfortunately, we can
  189. * be put into halting debug mode at any time by an external debugger
  190. * but there is nothing we can do to prevent that.
  191. */
  192. static int enable_monitor_mode(void)
  193. {
  194. u32 dscr;
  195. int ret = 0;
  196. ARM_DBG_READ(c1, 0, dscr);
  197. /* Ensure that halting mode is disabled. */
  198. if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN, "halting debug mode enabled."
  199. "Unable to access hardware resources.")) {
  200. ret = -EPERM;
  201. goto out;
  202. }
  203. /* Write to the corresponding DSCR. */
  204. switch (debug_arch) {
  205. case ARM_DEBUG_ARCH_V6:
  206. case ARM_DEBUG_ARCH_V6_1:
  207. ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
  208. break;
  209. case ARM_DEBUG_ARCH_V7_ECP14:
  210. ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
  211. break;
  212. default:
  213. ret = -ENODEV;
  214. goto out;
  215. }
  216. /* Check that the write made it through. */
  217. ARM_DBG_READ(c1, 0, dscr);
  218. if (WARN_ONCE(!(dscr & ARM_DSCR_MDBGEN),
  219. "failed to enable monitor mode.")) {
  220. ret = -EPERM;
  221. }
  222. out:
  223. return ret;
  224. }
  225. /*
  226. * Check if 8-bit byte-address select is available.
  227. * This clobbers WRP 0.
  228. */
  229. static u8 get_max_wp_len(void)
  230. {
  231. u32 ctrl_reg;
  232. struct arch_hw_breakpoint_ctrl ctrl;
  233. u8 size = 4;
  234. if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
  235. goto out;
  236. if (enable_monitor_mode())
  237. goto out;
  238. memset(&ctrl, 0, sizeof(ctrl));
  239. ctrl.len = ARM_BREAKPOINT_LEN_8;
  240. ctrl_reg = encode_ctrl_reg(ctrl);
  241. write_wb_reg(ARM_BASE_WVR, 0);
  242. write_wb_reg(ARM_BASE_WCR, ctrl_reg);
  243. if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
  244. size = 8;
  245. out:
  246. return size;
  247. }
  248. u8 arch_get_max_wp_len(void)
  249. {
  250. return max_watchpoint_len;
  251. }
  252. /*
  253. * Handler for reactivating a suspended watchpoint when the single
  254. * step `mismatch' breakpoint is triggered.
  255. */
  256. static void wp_single_step_handler(struct perf_event *bp, int unused,
  257. struct perf_sample_data *data,
  258. struct pt_regs *regs)
  259. {
  260. perf_event_enable(counter_arch_bp(bp)->suspended_wp);
  261. unregister_hw_breakpoint(bp);
  262. }
  263. static int bp_is_single_step(struct perf_event *bp)
  264. {
  265. return bp->overflow_handler == wp_single_step_handler;
  266. }
  267. /*
  268. * Install a perf counter breakpoint.
  269. */
  270. int arch_install_hw_breakpoint(struct perf_event *bp)
  271. {
  272. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  273. struct perf_event **slot, **slots;
  274. int i, max_slots, ctrl_base, val_base, ret = 0;
  275. /* Ensure that we are in monitor mode and halting mode is disabled. */
  276. ret = enable_monitor_mode();
  277. if (ret)
  278. goto out;
  279. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  280. /* Breakpoint */
  281. ctrl_base = ARM_BASE_BCR;
  282. val_base = ARM_BASE_BVR;
  283. slots = __get_cpu_var(bp_on_reg);
  284. max_slots = core_num_brps - 1;
  285. if (bp_is_single_step(bp)) {
  286. info->ctrl.mismatch = 1;
  287. i = max_slots;
  288. slots[i] = bp;
  289. goto setup;
  290. }
  291. } else {
  292. /* Watchpoint */
  293. ctrl_base = ARM_BASE_WCR;
  294. val_base = ARM_BASE_WVR;
  295. slots = __get_cpu_var(wp_on_reg);
  296. max_slots = core_num_wrps;
  297. }
  298. for (i = 0; i < max_slots; ++i) {
  299. slot = &slots[i];
  300. if (!*slot) {
  301. *slot = bp;
  302. break;
  303. }
  304. }
  305. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot")) {
  306. ret = -EBUSY;
  307. goto out;
  308. }
  309. setup:
  310. /* Setup the address register. */
  311. write_wb_reg(val_base + i, info->address);
  312. /* Setup the control register. */
  313. write_wb_reg(ctrl_base + i, encode_ctrl_reg(info->ctrl) | 0x1);
  314. out:
  315. return ret;
  316. }
  317. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  318. {
  319. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  320. struct perf_event **slot, **slots;
  321. int i, max_slots, base;
  322. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  323. /* Breakpoint */
  324. base = ARM_BASE_BCR;
  325. slots = __get_cpu_var(bp_on_reg);
  326. max_slots = core_num_brps - 1;
  327. if (bp_is_single_step(bp)) {
  328. i = max_slots;
  329. slots[i] = NULL;
  330. goto reset;
  331. }
  332. } else {
  333. /* Watchpoint */
  334. base = ARM_BASE_WCR;
  335. slots = __get_cpu_var(wp_on_reg);
  336. max_slots = core_num_wrps;
  337. }
  338. /* Remove the breakpoint. */
  339. for (i = 0; i < max_slots; ++i) {
  340. slot = &slots[i];
  341. if (*slot == bp) {
  342. *slot = NULL;
  343. break;
  344. }
  345. }
  346. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
  347. return;
  348. reset:
  349. /* Reset the control register. */
  350. write_wb_reg(base + i, 0);
  351. }
  352. static int get_hbp_len(u8 hbp_len)
  353. {
  354. unsigned int len_in_bytes = 0;
  355. switch (hbp_len) {
  356. case ARM_BREAKPOINT_LEN_1:
  357. len_in_bytes = 1;
  358. break;
  359. case ARM_BREAKPOINT_LEN_2:
  360. len_in_bytes = 2;
  361. break;
  362. case ARM_BREAKPOINT_LEN_4:
  363. len_in_bytes = 4;
  364. break;
  365. case ARM_BREAKPOINT_LEN_8:
  366. len_in_bytes = 8;
  367. break;
  368. }
  369. return len_in_bytes;
  370. }
  371. /*
  372. * Check whether bp virtual address is in kernel space.
  373. */
  374. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  375. {
  376. unsigned int len;
  377. unsigned long va;
  378. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  379. va = info->address;
  380. len = get_hbp_len(info->ctrl.len);
  381. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  382. }
  383. /*
  384. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  385. * Hopefully this will disappear when ptrace can bypass the conversion
  386. * to generic breakpoint descriptions.
  387. */
  388. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  389. int *gen_len, int *gen_type)
  390. {
  391. /* Type */
  392. switch (ctrl.type) {
  393. case ARM_BREAKPOINT_EXECUTE:
  394. *gen_type = HW_BREAKPOINT_X;
  395. break;
  396. case ARM_BREAKPOINT_LOAD:
  397. *gen_type = HW_BREAKPOINT_R;
  398. break;
  399. case ARM_BREAKPOINT_STORE:
  400. *gen_type = HW_BREAKPOINT_W;
  401. break;
  402. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  403. *gen_type = HW_BREAKPOINT_RW;
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. /* Len */
  409. switch (ctrl.len) {
  410. case ARM_BREAKPOINT_LEN_1:
  411. *gen_len = HW_BREAKPOINT_LEN_1;
  412. break;
  413. case ARM_BREAKPOINT_LEN_2:
  414. *gen_len = HW_BREAKPOINT_LEN_2;
  415. break;
  416. case ARM_BREAKPOINT_LEN_4:
  417. *gen_len = HW_BREAKPOINT_LEN_4;
  418. break;
  419. case ARM_BREAKPOINT_LEN_8:
  420. *gen_len = HW_BREAKPOINT_LEN_8;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. return 0;
  426. }
  427. /*
  428. * Construct an arch_hw_breakpoint from a perf_event.
  429. */
  430. static int arch_build_bp_info(struct perf_event *bp)
  431. {
  432. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  433. /* Type */
  434. switch (bp->attr.bp_type) {
  435. case HW_BREAKPOINT_X:
  436. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  437. break;
  438. case HW_BREAKPOINT_R:
  439. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  440. break;
  441. case HW_BREAKPOINT_W:
  442. info->ctrl.type = ARM_BREAKPOINT_STORE;
  443. break;
  444. case HW_BREAKPOINT_RW:
  445. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  446. break;
  447. default:
  448. return -EINVAL;
  449. }
  450. /* Len */
  451. switch (bp->attr.bp_len) {
  452. case HW_BREAKPOINT_LEN_1:
  453. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  454. break;
  455. case HW_BREAKPOINT_LEN_2:
  456. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  457. break;
  458. case HW_BREAKPOINT_LEN_4:
  459. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  460. break;
  461. case HW_BREAKPOINT_LEN_8:
  462. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  463. if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
  464. && max_watchpoint_len >= 8)
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. /* Address */
  470. info->address = bp->attr.bp_addr;
  471. /* Privilege */
  472. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  473. if (arch_check_bp_in_kernelspace(bp) && !bp_is_single_step(bp))
  474. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  475. /* Enabled? */
  476. info->ctrl.enabled = !bp->attr.disabled;
  477. /* Mismatch */
  478. info->ctrl.mismatch = 0;
  479. return 0;
  480. }
  481. /*
  482. * Validate the arch-specific HW Breakpoint register settings.
  483. */
  484. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  485. {
  486. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  487. int ret = 0;
  488. u32 bytelen, max_len, offset, alignment_mask = 0x3;
  489. /* Build the arch_hw_breakpoint. */
  490. ret = arch_build_bp_info(bp);
  491. if (ret)
  492. goto out;
  493. /* Check address alignment. */
  494. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  495. alignment_mask = 0x7;
  496. if (info->address & alignment_mask) {
  497. /*
  498. * Try to fix the alignment. This may result in a length
  499. * that is too large, so we must check for that.
  500. */
  501. bytelen = get_hbp_len(info->ctrl.len);
  502. max_len = info->ctrl.type == ARM_BREAKPOINT_EXECUTE ? 4 :
  503. max_watchpoint_len;
  504. if (max_len >= 8)
  505. offset = info->address & 0x7;
  506. else
  507. offset = info->address & 0x3;
  508. if (bytelen > (1 << ((max_len - (offset + 1)) >> 1))) {
  509. ret = -EFBIG;
  510. goto out;
  511. }
  512. info->ctrl.len <<= offset;
  513. info->address &= ~offset;
  514. pr_debug("breakpoint alignment fixup: length = 0x%x, "
  515. "address = 0x%x\n", info->ctrl.len, info->address);
  516. }
  517. /*
  518. * Currently we rely on an overflow handler to take
  519. * care of single-stepping the breakpoint when it fires.
  520. * In the case of userspace breakpoints on a core with V7 debug,
  521. * we can use the mismatch feature as a poor-man's hardware single-step.
  522. */
  523. if (WARN_ONCE(!bp->overflow_handler &&
  524. (arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_bps()),
  525. "overflow handler required but none found")) {
  526. ret = -EINVAL;
  527. goto out;
  528. }
  529. out:
  530. return ret;
  531. }
  532. static void update_mismatch_flag(int idx, int flag)
  533. {
  534. struct perf_event *bp = __get_cpu_var(bp_on_reg[idx]);
  535. struct arch_hw_breakpoint *info;
  536. if (bp == NULL)
  537. return;
  538. info = counter_arch_bp(bp);
  539. /* Update the mismatch field to enter/exit `single-step' mode */
  540. if (!bp->overflow_handler && info->ctrl.mismatch != flag) {
  541. info->ctrl.mismatch = flag;
  542. write_wb_reg(ARM_BASE_BCR + idx, encode_ctrl_reg(info->ctrl) | 0x1);
  543. }
  544. }
  545. static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
  546. {
  547. int i;
  548. struct perf_event *bp, **slots = __get_cpu_var(wp_on_reg);
  549. struct arch_hw_breakpoint *info;
  550. struct perf_event_attr attr;
  551. /* Without a disassembler, we can only handle 1 watchpoint. */
  552. BUG_ON(core_num_wrps > 1);
  553. hw_breakpoint_init(&attr);
  554. attr.bp_addr = regs->ARM_pc & ~0x3;
  555. attr.bp_len = HW_BREAKPOINT_LEN_4;
  556. attr.bp_type = HW_BREAKPOINT_X;
  557. for (i = 0; i < core_num_wrps; ++i) {
  558. rcu_read_lock();
  559. if (slots[i] == NULL) {
  560. rcu_read_unlock();
  561. continue;
  562. }
  563. /*
  564. * The DFAR is an unknown value. Since we only allow a
  565. * single watchpoint, we can set the trigger to the lowest
  566. * possible faulting address.
  567. */
  568. info = counter_arch_bp(slots[i]);
  569. info->trigger = slots[i]->attr.bp_addr;
  570. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  571. perf_bp_event(slots[i], regs);
  572. /*
  573. * If no overflow handler is present, insert a temporary
  574. * mismatch breakpoint so we can single-step over the
  575. * watchpoint trigger.
  576. */
  577. if (!slots[i]->overflow_handler) {
  578. bp = register_user_hw_breakpoint(&attr,
  579. wp_single_step_handler,
  580. current);
  581. counter_arch_bp(bp)->suspended_wp = slots[i];
  582. perf_event_disable(slots[i]);
  583. }
  584. rcu_read_unlock();
  585. }
  586. }
  587. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  588. {
  589. int i;
  590. int mismatch;
  591. u32 ctrl_reg, val, addr;
  592. struct perf_event *bp, **slots = __get_cpu_var(bp_on_reg);
  593. struct arch_hw_breakpoint *info;
  594. struct arch_hw_breakpoint_ctrl ctrl;
  595. /* The exception entry code places the amended lr in the PC. */
  596. addr = regs->ARM_pc;
  597. for (i = 0; i < core_num_brps; ++i) {
  598. rcu_read_lock();
  599. bp = slots[i];
  600. if (bp == NULL) {
  601. rcu_read_unlock();
  602. continue;
  603. }
  604. mismatch = 0;
  605. /* Check if the breakpoint value matches. */
  606. val = read_wb_reg(ARM_BASE_BVR + i);
  607. if (val != (addr & ~0x3))
  608. goto unlock;
  609. /* Possible match, check the byte address select to confirm. */
  610. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  611. decode_ctrl_reg(ctrl_reg, &ctrl);
  612. if ((1 << (addr & 0x3)) & ctrl.len) {
  613. mismatch = 1;
  614. info = counter_arch_bp(bp);
  615. info->trigger = addr;
  616. }
  617. unlock:
  618. if ((mismatch && !info->ctrl.mismatch) || bp_is_single_step(bp)) {
  619. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  620. perf_bp_event(bp, regs);
  621. }
  622. update_mismatch_flag(i, mismatch);
  623. rcu_read_unlock();
  624. }
  625. }
  626. /*
  627. * Called from either the Data Abort Handler [watchpoint] or the
  628. * Prefetch Abort Handler [breakpoint].
  629. */
  630. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  631. struct pt_regs *regs)
  632. {
  633. int ret = 1; /* Unhandled fault. */
  634. u32 dscr;
  635. /* We only handle watchpoints and hardware breakpoints. */
  636. ARM_DBG_READ(c1, 0, dscr);
  637. /* Perform perf callbacks. */
  638. switch (ARM_DSCR_MOE(dscr)) {
  639. case ARM_ENTRY_BREAKPOINT:
  640. breakpoint_handler(addr, regs);
  641. break;
  642. case ARM_ENTRY_ASYNC_WATCHPOINT:
  643. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  644. case ARM_ENTRY_SYNC_WATCHPOINT:
  645. watchpoint_handler(addr, regs);
  646. break;
  647. default:
  648. goto out;
  649. }
  650. ret = 0;
  651. out:
  652. return ret;
  653. }
  654. /*
  655. * One-time initialisation.
  656. */
  657. static void __init reset_ctrl_regs(void *unused)
  658. {
  659. int i;
  660. if (enable_monitor_mode())
  661. return;
  662. for (i = 0; i < core_num_brps; ++i) {
  663. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  664. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  665. }
  666. for (i = 0; i < core_num_wrps; ++i) {
  667. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  668. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  669. }
  670. }
  671. static int __init arch_hw_breakpoint_init(void)
  672. {
  673. int ret = 0;
  674. u32 dscr;
  675. debug_arch = get_debug_arch();
  676. if (debug_arch > ARM_DEBUG_ARCH_V7_ECP14) {
  677. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  678. ret = -ENODEV;
  679. goto out;
  680. }
  681. /* Determine how many BRPs/WRPs are available. */
  682. core_num_brps = get_num_brps();
  683. core_num_wrps = get_num_wrps();
  684. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  685. core_num_brps, core_num_wrps);
  686. if (core_has_mismatch_bps())
  687. pr_info("1 breakpoint reserved for watchpoint single-step.\n");
  688. ARM_DBG_READ(c1, 0, dscr);
  689. if (dscr & ARM_DSCR_HDBGEN) {
  690. pr_warning("halting debug mode enabled. Assuming maximum "
  691. "watchpoint size of 4 bytes.");
  692. } else {
  693. /* Work out the maximum supported watchpoint length. */
  694. max_watchpoint_len = get_max_wp_len();
  695. pr_info("maximum watchpoint size is %u bytes.\n",
  696. max_watchpoint_len);
  697. /*
  698. * Reset the breakpoint resources. We assume that a halting
  699. * debugger will leave the world in a nice state for us.
  700. */
  701. smp_call_function(reset_ctrl_regs, NULL, 1);
  702. reset_ctrl_regs(NULL);
  703. }
  704. /* Register debug fault handler. */
  705. hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  706. "watchpoint debug exception");
  707. hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  708. "breakpoint debug exception");
  709. out:
  710. return ret;
  711. }
  712. arch_initcall(arch_hw_breakpoint_init);
  713. void hw_breakpoint_pmu_read(struct perf_event *bp)
  714. {
  715. }
  716. /*
  717. * Dummy function to register with die_notifier.
  718. */
  719. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  720. unsigned long val, void *data)
  721. {
  722. return NOTIFY_DONE;
  723. }