hw_breakpoint.c 21 KB

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