hw_breakpoint.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. /*
  470. * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
  471. * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
  472. * by the hardware and must be aligned to the appropriate number of
  473. * bytes.
  474. */
  475. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  476. info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  477. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  478. return -EINVAL;
  479. /* Address */
  480. info->address = bp->attr.bp_addr;
  481. /* Privilege */
  482. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  483. if (arch_check_bp_in_kernelspace(bp) && !bp_is_single_step(bp))
  484. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  485. /* Enabled? */
  486. info->ctrl.enabled = !bp->attr.disabled;
  487. /* Mismatch */
  488. info->ctrl.mismatch = 0;
  489. return 0;
  490. }
  491. /*
  492. * Validate the arch-specific HW Breakpoint register settings.
  493. */
  494. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  495. {
  496. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  497. int ret = 0;
  498. u32 offset, alignment_mask = 0x3;
  499. /* Build the arch_hw_breakpoint. */
  500. ret = arch_build_bp_info(bp);
  501. if (ret)
  502. goto out;
  503. /* Check address alignment. */
  504. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  505. alignment_mask = 0x7;
  506. offset = info->address & alignment_mask;
  507. switch (offset) {
  508. case 0:
  509. /* Aligned */
  510. break;
  511. case 1:
  512. /* Allow single byte watchpoint. */
  513. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  514. break;
  515. case 2:
  516. /* Allow halfword watchpoints and breakpoints. */
  517. if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  518. break;
  519. default:
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. info->address &= ~alignment_mask;
  524. info->ctrl.len <<= offset;
  525. /*
  526. * Currently we rely on an overflow handler to take
  527. * care of single-stepping the breakpoint when it fires.
  528. * In the case of userspace breakpoints on a core with V7 debug,
  529. * we can use the mismatch feature as a poor-man's hardware single-step.
  530. */
  531. if (WARN_ONCE(!bp->overflow_handler &&
  532. (arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_bps()),
  533. "overflow handler required but none found")) {
  534. ret = -EINVAL;
  535. }
  536. out:
  537. return ret;
  538. }
  539. static void update_mismatch_flag(int idx, int flag)
  540. {
  541. struct perf_event *bp = __get_cpu_var(bp_on_reg[idx]);
  542. struct arch_hw_breakpoint *info;
  543. if (bp == NULL)
  544. return;
  545. info = counter_arch_bp(bp);
  546. /* Update the mismatch field to enter/exit `single-step' mode */
  547. if (!bp->overflow_handler && info->ctrl.mismatch != flag) {
  548. info->ctrl.mismatch = flag;
  549. write_wb_reg(ARM_BASE_BCR + idx, encode_ctrl_reg(info->ctrl) | 0x1);
  550. }
  551. }
  552. static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
  553. {
  554. int i;
  555. struct perf_event *bp, **slots = __get_cpu_var(wp_on_reg);
  556. struct arch_hw_breakpoint *info;
  557. struct perf_event_attr attr;
  558. /* Without a disassembler, we can only handle 1 watchpoint. */
  559. BUG_ON(core_num_wrps > 1);
  560. hw_breakpoint_init(&attr);
  561. attr.bp_addr = regs->ARM_pc & ~0x3;
  562. attr.bp_len = HW_BREAKPOINT_LEN_4;
  563. attr.bp_type = HW_BREAKPOINT_X;
  564. for (i = 0; i < core_num_wrps; ++i) {
  565. rcu_read_lock();
  566. if (slots[i] == NULL) {
  567. rcu_read_unlock();
  568. continue;
  569. }
  570. /*
  571. * The DFAR is an unknown value. Since we only allow a
  572. * single watchpoint, we can set the trigger to the lowest
  573. * possible faulting address.
  574. */
  575. info = counter_arch_bp(slots[i]);
  576. info->trigger = slots[i]->attr.bp_addr;
  577. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  578. perf_bp_event(slots[i], regs);
  579. /*
  580. * If no overflow handler is present, insert a temporary
  581. * mismatch breakpoint so we can single-step over the
  582. * watchpoint trigger.
  583. */
  584. if (!slots[i]->overflow_handler) {
  585. bp = register_user_hw_breakpoint(&attr,
  586. wp_single_step_handler,
  587. current);
  588. counter_arch_bp(bp)->suspended_wp = slots[i];
  589. perf_event_disable(slots[i]);
  590. }
  591. rcu_read_unlock();
  592. }
  593. }
  594. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  595. {
  596. int i;
  597. int mismatch;
  598. u32 ctrl_reg, val, addr;
  599. struct perf_event *bp, **slots = __get_cpu_var(bp_on_reg);
  600. struct arch_hw_breakpoint *info;
  601. struct arch_hw_breakpoint_ctrl ctrl;
  602. /* The exception entry code places the amended lr in the PC. */
  603. addr = regs->ARM_pc;
  604. for (i = 0; i < core_num_brps; ++i) {
  605. rcu_read_lock();
  606. bp = slots[i];
  607. if (bp == NULL) {
  608. rcu_read_unlock();
  609. continue;
  610. }
  611. mismatch = 0;
  612. /* Check if the breakpoint value matches. */
  613. val = read_wb_reg(ARM_BASE_BVR + i);
  614. if (val != (addr & ~0x3))
  615. goto unlock;
  616. /* Possible match, check the byte address select to confirm. */
  617. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  618. decode_ctrl_reg(ctrl_reg, &ctrl);
  619. if ((1 << (addr & 0x3)) & ctrl.len) {
  620. mismatch = 1;
  621. info = counter_arch_bp(bp);
  622. info->trigger = addr;
  623. }
  624. unlock:
  625. if ((mismatch && !info->ctrl.mismatch) || bp_is_single_step(bp)) {
  626. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  627. perf_bp_event(bp, regs);
  628. }
  629. update_mismatch_flag(i, mismatch);
  630. rcu_read_unlock();
  631. }
  632. }
  633. /*
  634. * Called from either the Data Abort Handler [watchpoint] or the
  635. * Prefetch Abort Handler [breakpoint].
  636. */
  637. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  638. struct pt_regs *regs)
  639. {
  640. int ret = 1; /* Unhandled fault. */
  641. u32 dscr;
  642. /* We only handle watchpoints and hardware breakpoints. */
  643. ARM_DBG_READ(c1, 0, dscr);
  644. /* Perform perf callbacks. */
  645. switch (ARM_DSCR_MOE(dscr)) {
  646. case ARM_ENTRY_BREAKPOINT:
  647. breakpoint_handler(addr, regs);
  648. break;
  649. case ARM_ENTRY_ASYNC_WATCHPOINT:
  650. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  651. case ARM_ENTRY_SYNC_WATCHPOINT:
  652. watchpoint_handler(addr, regs);
  653. break;
  654. default:
  655. goto out;
  656. }
  657. ret = 0;
  658. out:
  659. return ret;
  660. }
  661. /*
  662. * One-time initialisation.
  663. */
  664. static void reset_ctrl_regs(void *unused)
  665. {
  666. int i;
  667. /*
  668. * v7 debug contains save and restore registers so that debug state
  669. * can be maintained across low-power modes without leaving
  670. * the debug logic powered up. It is IMPLEMENTATION DEFINED whether
  671. * we can write to the debug registers out of reset, so we must
  672. * unlock the OS Lock Access Register to avoid taking undefined
  673. * instruction exceptions later on.
  674. */
  675. if (debug_arch >= ARM_DEBUG_ARCH_V7_ECP14) {
  676. /*
  677. * Unconditionally clear the lock by writing a value
  678. * other than 0xC5ACCE55 to the access register.
  679. */
  680. asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
  681. isb();
  682. }
  683. if (enable_monitor_mode())
  684. return;
  685. for (i = 0; i < core_num_brps; ++i) {
  686. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  687. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  688. }
  689. for (i = 0; i < core_num_wrps; ++i) {
  690. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  691. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  692. }
  693. }
  694. static int __cpuinit dbg_reset_notify(struct notifier_block *self,
  695. unsigned long action, void *cpu)
  696. {
  697. if (action == CPU_ONLINE)
  698. smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
  699. return NOTIFY_OK;
  700. }
  701. static struct notifier_block __cpuinitdata dbg_reset_nb = {
  702. .notifier_call = dbg_reset_notify,
  703. };
  704. static int __init arch_hw_breakpoint_init(void)
  705. {
  706. int ret = 0;
  707. u32 dscr;
  708. debug_arch = get_debug_arch();
  709. if (debug_arch > ARM_DEBUG_ARCH_V7_ECP14) {
  710. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  711. ret = -ENODEV;
  712. goto out;
  713. }
  714. /* Determine how many BRPs/WRPs are available. */
  715. core_num_brps = get_num_brps();
  716. core_num_wrps = get_num_wrps();
  717. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  718. core_num_brps, core_num_wrps);
  719. if (core_has_mismatch_bps())
  720. pr_info("1 breakpoint reserved for watchpoint single-step.\n");
  721. ARM_DBG_READ(c1, 0, dscr);
  722. if (dscr & ARM_DSCR_HDBGEN) {
  723. pr_warning("halting debug mode enabled. Assuming maximum "
  724. "watchpoint size of 4 bytes.");
  725. } else {
  726. /*
  727. * Reset the breakpoint resources. We assume that a halting
  728. * debugger will leave the world in a nice state for us.
  729. */
  730. smp_call_function(reset_ctrl_regs, NULL, 1);
  731. reset_ctrl_regs(NULL);
  732. /* Work out the maximum supported watchpoint length. */
  733. max_watchpoint_len = get_max_wp_len();
  734. pr_info("maximum watchpoint size is %u bytes.\n",
  735. max_watchpoint_len);
  736. }
  737. /* Register debug fault handler. */
  738. hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  739. "watchpoint debug exception");
  740. hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  741. "breakpoint debug exception");
  742. /* Register hotplug notifier. */
  743. register_cpu_notifier(&dbg_reset_nb);
  744. out:
  745. return ret;
  746. }
  747. arch_initcall(arch_hw_breakpoint_init);
  748. void hw_breakpoint_pmu_read(struct perf_event *bp)
  749. {
  750. }
  751. /*
  752. * Dummy function to register with die_notifier.
  753. */
  754. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  755. unsigned long val, void *data)
  756. {
  757. return NOTIFY_DONE;
  758. }