hw_breakpoint.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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/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. #define READ_WB_REG_CASE(OP2, M, VAL) \
  47. case ((OP2 << 4) + M): \
  48. ARM_DBG_READ(c ## M, OP2, VAL); \
  49. break
  50. #define WRITE_WB_REG_CASE(OP2, M, VAL) \
  51. case ((OP2 << 4) + M): \
  52. ARM_DBG_WRITE(c ## M, OP2, VAL);\
  53. break
  54. #define GEN_READ_WB_REG_CASES(OP2, VAL) \
  55. READ_WB_REG_CASE(OP2, 0, VAL); \
  56. READ_WB_REG_CASE(OP2, 1, VAL); \
  57. READ_WB_REG_CASE(OP2, 2, VAL); \
  58. READ_WB_REG_CASE(OP2, 3, VAL); \
  59. READ_WB_REG_CASE(OP2, 4, VAL); \
  60. READ_WB_REG_CASE(OP2, 5, VAL); \
  61. READ_WB_REG_CASE(OP2, 6, VAL); \
  62. READ_WB_REG_CASE(OP2, 7, VAL); \
  63. READ_WB_REG_CASE(OP2, 8, VAL); \
  64. READ_WB_REG_CASE(OP2, 9, VAL); \
  65. READ_WB_REG_CASE(OP2, 10, VAL); \
  66. READ_WB_REG_CASE(OP2, 11, VAL); \
  67. READ_WB_REG_CASE(OP2, 12, VAL); \
  68. READ_WB_REG_CASE(OP2, 13, VAL); \
  69. READ_WB_REG_CASE(OP2, 14, VAL); \
  70. READ_WB_REG_CASE(OP2, 15, VAL)
  71. #define GEN_WRITE_WB_REG_CASES(OP2, VAL) \
  72. WRITE_WB_REG_CASE(OP2, 0, VAL); \
  73. WRITE_WB_REG_CASE(OP2, 1, VAL); \
  74. WRITE_WB_REG_CASE(OP2, 2, VAL); \
  75. WRITE_WB_REG_CASE(OP2, 3, VAL); \
  76. WRITE_WB_REG_CASE(OP2, 4, VAL); \
  77. WRITE_WB_REG_CASE(OP2, 5, VAL); \
  78. WRITE_WB_REG_CASE(OP2, 6, VAL); \
  79. WRITE_WB_REG_CASE(OP2, 7, VAL); \
  80. WRITE_WB_REG_CASE(OP2, 8, VAL); \
  81. WRITE_WB_REG_CASE(OP2, 9, VAL); \
  82. WRITE_WB_REG_CASE(OP2, 10, VAL); \
  83. WRITE_WB_REG_CASE(OP2, 11, VAL); \
  84. WRITE_WB_REG_CASE(OP2, 12, VAL); \
  85. WRITE_WB_REG_CASE(OP2, 13, VAL); \
  86. WRITE_WB_REG_CASE(OP2, 14, VAL); \
  87. WRITE_WB_REG_CASE(OP2, 15, VAL)
  88. static u32 read_wb_reg(int n)
  89. {
  90. u32 val = 0;
  91. switch (n) {
  92. GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
  93. GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
  94. GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
  95. GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
  96. default:
  97. pr_warning("attempt to read from unknown breakpoint "
  98. "register %d\n", n);
  99. }
  100. return val;
  101. }
  102. static void write_wb_reg(int n, u32 val)
  103. {
  104. switch (n) {
  105. GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
  106. GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
  107. GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
  108. GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
  109. default:
  110. pr_warning("attempt to write to unknown breakpoint "
  111. "register %d\n", n);
  112. }
  113. isb();
  114. }
  115. /* Determine debug architecture. */
  116. static u8 get_debug_arch(void)
  117. {
  118. u32 didr;
  119. /* Do we implement the extended CPUID interface? */
  120. if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
  121. pr_warning("CPUID feature registers not supported. "
  122. "Assuming v6 debug is present.\n");
  123. return ARM_DEBUG_ARCH_V6;
  124. }
  125. ARM_DBG_READ(c0, 0, didr);
  126. return (didr >> 16) & 0xf;
  127. }
  128. u8 arch_get_debug_arch(void)
  129. {
  130. return debug_arch;
  131. }
  132. static int debug_arch_supported(void)
  133. {
  134. u8 arch = get_debug_arch();
  135. /* We don't support the memory-mapped interface. */
  136. return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
  137. arch >= ARM_DEBUG_ARCH_V7_1;
  138. }
  139. /* Can we determine the watchpoint access type from the fsr? */
  140. static int debug_exception_updates_fsr(void)
  141. {
  142. return 0;
  143. }
  144. /* Determine number of WRP registers available. */
  145. static int get_num_wrp_resources(void)
  146. {
  147. u32 didr;
  148. ARM_DBG_READ(c0, 0, didr);
  149. return ((didr >> 28) & 0xf) + 1;
  150. }
  151. /* Determine number of BRP registers available. */
  152. static int get_num_brp_resources(void)
  153. {
  154. u32 didr;
  155. ARM_DBG_READ(c0, 0, didr);
  156. return ((didr >> 24) & 0xf) + 1;
  157. }
  158. /* Does this core support mismatch breakpoints? */
  159. static int core_has_mismatch_brps(void)
  160. {
  161. return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
  162. get_num_brp_resources() > 1);
  163. }
  164. /* Determine number of usable WRPs available. */
  165. static int get_num_wrps(void)
  166. {
  167. /*
  168. * On debug architectures prior to 7.1, when a watchpoint fires, the
  169. * only way to work out which watchpoint it was is by disassembling
  170. * the faulting instruction and working out the address of the memory
  171. * access.
  172. *
  173. * Furthermore, we can only do this if the watchpoint was precise
  174. * since imprecise watchpoints prevent us from calculating register
  175. * based addresses.
  176. *
  177. * Providing we have more than 1 breakpoint register, we only report
  178. * a single watchpoint register for the time being. This way, we always
  179. * know which watchpoint fired. In the future we can either add a
  180. * disassembler and address generation emulator, or we can insert a
  181. * check to see if the DFAR is set on watchpoint exception entry
  182. * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
  183. * that it is set on some implementations].
  184. */
  185. if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
  186. return 1;
  187. return get_num_wrp_resources();
  188. }
  189. /* Determine number of usable BRPs available. */
  190. static int get_num_brps(void)
  191. {
  192. int brps = get_num_brp_resources();
  193. return core_has_mismatch_brps() ? brps - 1 : brps;
  194. }
  195. /*
  196. * In order to access the breakpoint/watchpoint control registers,
  197. * we must be running in debug monitor mode. Unfortunately, we can
  198. * be put into halting debug mode at any time by an external debugger
  199. * but there is nothing we can do to prevent that.
  200. */
  201. static int enable_monitor_mode(void)
  202. {
  203. u32 dscr;
  204. ARM_DBG_READ(c1, 0, dscr);
  205. /* If monitor mode is already enabled, just return. */
  206. if (dscr & ARM_DSCR_MDBGEN)
  207. goto out;
  208. /* Write to the corresponding DSCR. */
  209. switch (get_debug_arch()) {
  210. case ARM_DEBUG_ARCH_V6:
  211. case ARM_DEBUG_ARCH_V6_1:
  212. ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
  213. break;
  214. case ARM_DEBUG_ARCH_V7_ECP14:
  215. case ARM_DEBUG_ARCH_V7_1:
  216. ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
  217. isb();
  218. break;
  219. default:
  220. return -ENODEV;
  221. }
  222. /* Check that the write made it through. */
  223. ARM_DBG_READ(c1, 0, dscr);
  224. if (WARN_ONCE(!(dscr & ARM_DSCR_MDBGEN),
  225. "Failed to enable monitor mode on CPU %d.\n",
  226. smp_processor_id()))
  227. return -EPERM;
  228. out:
  229. return 0;
  230. }
  231. int hw_breakpoint_slots(int type)
  232. {
  233. if (!debug_arch_supported())
  234. return 0;
  235. /*
  236. * We can be called early, so don't rely on
  237. * our static variables being initialised.
  238. */
  239. switch (type) {
  240. case TYPE_INST:
  241. return get_num_brps();
  242. case TYPE_DATA:
  243. return get_num_wrps();
  244. default:
  245. pr_warning("unknown slot type: %d\n", type);
  246. return 0;
  247. }
  248. }
  249. /*
  250. * Check if 8-bit byte-address select is available.
  251. * This clobbers WRP 0.
  252. */
  253. static u8 get_max_wp_len(void)
  254. {
  255. u32 ctrl_reg;
  256. struct arch_hw_breakpoint_ctrl ctrl;
  257. u8 size = 4;
  258. if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
  259. goto out;
  260. memset(&ctrl, 0, sizeof(ctrl));
  261. ctrl.len = ARM_BREAKPOINT_LEN_8;
  262. ctrl_reg = encode_ctrl_reg(ctrl);
  263. write_wb_reg(ARM_BASE_WVR, 0);
  264. write_wb_reg(ARM_BASE_WCR, ctrl_reg);
  265. if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
  266. size = 8;
  267. out:
  268. return size;
  269. }
  270. u8 arch_get_max_wp_len(void)
  271. {
  272. return max_watchpoint_len;
  273. }
  274. /*
  275. * Install a perf counter breakpoint.
  276. */
  277. int arch_install_hw_breakpoint(struct perf_event *bp)
  278. {
  279. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  280. struct perf_event **slot, **slots;
  281. int i, max_slots, ctrl_base, val_base, ret = 0;
  282. u32 addr, ctrl;
  283. /* Ensure that we are in monitor mode and halting mode is disabled. */
  284. ret = enable_monitor_mode();
  285. if (ret)
  286. goto out;
  287. addr = info->address;
  288. ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
  289. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  290. /* Breakpoint */
  291. ctrl_base = ARM_BASE_BCR;
  292. val_base = ARM_BASE_BVR;
  293. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  294. max_slots = core_num_brps;
  295. } else {
  296. /* Watchpoint */
  297. ctrl_base = ARM_BASE_WCR;
  298. val_base = ARM_BASE_WVR;
  299. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  300. max_slots = core_num_wrps;
  301. }
  302. for (i = 0; i < max_slots; ++i) {
  303. slot = &slots[i];
  304. if (!*slot) {
  305. *slot = bp;
  306. break;
  307. }
  308. }
  309. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) {
  310. ret = -EBUSY;
  311. goto out;
  312. }
  313. /* Override the breakpoint data with the step data. */
  314. if (info->step_ctrl.enabled) {
  315. addr = info->trigger & ~0x3;
  316. ctrl = encode_ctrl_reg(info->step_ctrl);
  317. if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) {
  318. i = 0;
  319. ctrl_base = ARM_BASE_BCR + core_num_brps;
  320. val_base = ARM_BASE_BVR + core_num_brps;
  321. }
  322. }
  323. /* Setup the address register. */
  324. write_wb_reg(val_base + i, addr);
  325. /* Setup the control register. */
  326. write_wb_reg(ctrl_base + i, ctrl);
  327. out:
  328. return ret;
  329. }
  330. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  331. {
  332. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  333. struct perf_event **slot, **slots;
  334. int i, max_slots, base;
  335. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  336. /* Breakpoint */
  337. base = ARM_BASE_BCR;
  338. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  339. max_slots = core_num_brps;
  340. } else {
  341. /* Watchpoint */
  342. base = ARM_BASE_WCR;
  343. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  344. max_slots = core_num_wrps;
  345. }
  346. /* Remove the breakpoint. */
  347. for (i = 0; i < max_slots; ++i) {
  348. slot = &slots[i];
  349. if (*slot == bp) {
  350. *slot = NULL;
  351. break;
  352. }
  353. }
  354. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n"))
  355. return;
  356. /* Ensure that we disable the mismatch breakpoint. */
  357. if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE &&
  358. info->step_ctrl.enabled) {
  359. i = 0;
  360. base = ARM_BASE_BCR + core_num_brps;
  361. }
  362. /* Reset the control register. */
  363. write_wb_reg(base + i, 0);
  364. }
  365. static int get_hbp_len(u8 hbp_len)
  366. {
  367. unsigned int len_in_bytes = 0;
  368. switch (hbp_len) {
  369. case ARM_BREAKPOINT_LEN_1:
  370. len_in_bytes = 1;
  371. break;
  372. case ARM_BREAKPOINT_LEN_2:
  373. len_in_bytes = 2;
  374. break;
  375. case ARM_BREAKPOINT_LEN_4:
  376. len_in_bytes = 4;
  377. break;
  378. case ARM_BREAKPOINT_LEN_8:
  379. len_in_bytes = 8;
  380. break;
  381. }
  382. return len_in_bytes;
  383. }
  384. /*
  385. * Check whether bp virtual address is in kernel space.
  386. */
  387. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  388. {
  389. unsigned int len;
  390. unsigned long va;
  391. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  392. va = info->address;
  393. len = get_hbp_len(info->ctrl.len);
  394. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  395. }
  396. /*
  397. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  398. * Hopefully this will disappear when ptrace can bypass the conversion
  399. * to generic breakpoint descriptions.
  400. */
  401. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  402. int *gen_len, int *gen_type)
  403. {
  404. /* Type */
  405. switch (ctrl.type) {
  406. case ARM_BREAKPOINT_EXECUTE:
  407. *gen_type = HW_BREAKPOINT_X;
  408. break;
  409. case ARM_BREAKPOINT_LOAD:
  410. *gen_type = HW_BREAKPOINT_R;
  411. break;
  412. case ARM_BREAKPOINT_STORE:
  413. *gen_type = HW_BREAKPOINT_W;
  414. break;
  415. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  416. *gen_type = HW_BREAKPOINT_RW;
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. /* Len */
  422. switch (ctrl.len) {
  423. case ARM_BREAKPOINT_LEN_1:
  424. *gen_len = HW_BREAKPOINT_LEN_1;
  425. break;
  426. case ARM_BREAKPOINT_LEN_2:
  427. *gen_len = HW_BREAKPOINT_LEN_2;
  428. break;
  429. case ARM_BREAKPOINT_LEN_4:
  430. *gen_len = HW_BREAKPOINT_LEN_4;
  431. break;
  432. case ARM_BREAKPOINT_LEN_8:
  433. *gen_len = HW_BREAKPOINT_LEN_8;
  434. break;
  435. default:
  436. return -EINVAL;
  437. }
  438. return 0;
  439. }
  440. /*
  441. * Construct an arch_hw_breakpoint from a perf_event.
  442. */
  443. static int arch_build_bp_info(struct perf_event *bp)
  444. {
  445. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  446. /* Type */
  447. switch (bp->attr.bp_type) {
  448. case HW_BREAKPOINT_X:
  449. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  450. break;
  451. case HW_BREAKPOINT_R:
  452. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  453. break;
  454. case HW_BREAKPOINT_W:
  455. info->ctrl.type = ARM_BREAKPOINT_STORE;
  456. break;
  457. case HW_BREAKPOINT_RW:
  458. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  459. break;
  460. default:
  461. return -EINVAL;
  462. }
  463. /* Len */
  464. switch (bp->attr.bp_len) {
  465. case HW_BREAKPOINT_LEN_1:
  466. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  467. break;
  468. case HW_BREAKPOINT_LEN_2:
  469. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  470. break;
  471. case HW_BREAKPOINT_LEN_4:
  472. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  473. break;
  474. case HW_BREAKPOINT_LEN_8:
  475. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  476. if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
  477. && max_watchpoint_len >= 8)
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. /*
  483. * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
  484. * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
  485. * by the hardware and must be aligned to the appropriate number of
  486. * bytes.
  487. */
  488. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  489. info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  490. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  491. return -EINVAL;
  492. /* Address */
  493. info->address = bp->attr.bp_addr;
  494. /* Privilege */
  495. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  496. if (arch_check_bp_in_kernelspace(bp))
  497. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  498. /* Enabled? */
  499. info->ctrl.enabled = !bp->attr.disabled;
  500. /* Mismatch */
  501. info->ctrl.mismatch = 0;
  502. return 0;
  503. }
  504. /*
  505. * Validate the arch-specific HW Breakpoint register settings.
  506. */
  507. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  508. {
  509. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  510. int ret = 0;
  511. u32 offset, alignment_mask = 0x3;
  512. /* Build the arch_hw_breakpoint. */
  513. ret = arch_build_bp_info(bp);
  514. if (ret)
  515. goto out;
  516. /* Check address alignment. */
  517. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  518. alignment_mask = 0x7;
  519. offset = info->address & alignment_mask;
  520. switch (offset) {
  521. case 0:
  522. /* Aligned */
  523. break;
  524. case 1:
  525. case 2:
  526. /* Allow halfword watchpoints and breakpoints. */
  527. if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  528. break;
  529. case 3:
  530. /* Allow single byte watchpoint. */
  531. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  532. break;
  533. default:
  534. ret = -EINVAL;
  535. goto out;
  536. }
  537. info->address &= ~alignment_mask;
  538. info->ctrl.len <<= offset;
  539. if (!bp->overflow_handler) {
  540. /*
  541. * Mismatch breakpoints are required for single-stepping
  542. * breakpoints.
  543. */
  544. if (!core_has_mismatch_brps())
  545. return -EINVAL;
  546. /* We don't allow mismatch breakpoints in kernel space. */
  547. if (arch_check_bp_in_kernelspace(bp))
  548. return -EPERM;
  549. /*
  550. * Per-cpu breakpoints are not supported by our stepping
  551. * mechanism.
  552. */
  553. if (!bp->hw.bp_target)
  554. return -EINVAL;
  555. /*
  556. * We only support specific access types if the fsr
  557. * reports them.
  558. */
  559. if (!debug_exception_updates_fsr() &&
  560. (info->ctrl.type == ARM_BREAKPOINT_LOAD ||
  561. info->ctrl.type == ARM_BREAKPOINT_STORE))
  562. return -EINVAL;
  563. }
  564. out:
  565. return ret;
  566. }
  567. /*
  568. * Enable/disable single-stepping over the breakpoint bp at address addr.
  569. */
  570. static void enable_single_step(struct perf_event *bp, u32 addr)
  571. {
  572. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  573. arch_uninstall_hw_breakpoint(bp);
  574. info->step_ctrl.mismatch = 1;
  575. info->step_ctrl.len = ARM_BREAKPOINT_LEN_4;
  576. info->step_ctrl.type = ARM_BREAKPOINT_EXECUTE;
  577. info->step_ctrl.privilege = info->ctrl.privilege;
  578. info->step_ctrl.enabled = 1;
  579. info->trigger = addr;
  580. arch_install_hw_breakpoint(bp);
  581. }
  582. static void disable_single_step(struct perf_event *bp)
  583. {
  584. arch_uninstall_hw_breakpoint(bp);
  585. counter_arch_bp(bp)->step_ctrl.enabled = 0;
  586. arch_install_hw_breakpoint(bp);
  587. }
  588. static void watchpoint_handler(unsigned long addr, unsigned int fsr,
  589. struct pt_regs *regs)
  590. {
  591. int i, access;
  592. u32 val, ctrl_reg, alignment_mask;
  593. struct perf_event *wp, **slots;
  594. struct arch_hw_breakpoint *info;
  595. struct arch_hw_breakpoint_ctrl ctrl;
  596. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  597. for (i = 0; i < core_num_wrps; ++i) {
  598. rcu_read_lock();
  599. wp = slots[i];
  600. if (wp == NULL)
  601. goto unlock;
  602. info = counter_arch_bp(wp);
  603. /*
  604. * The DFAR is an unknown value on debug architectures prior
  605. * to 7.1. Since we only allow a single watchpoint on these
  606. * older CPUs, we can set the trigger to the lowest possible
  607. * faulting address.
  608. */
  609. if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
  610. BUG_ON(i > 0);
  611. info->trigger = wp->attr.bp_addr;
  612. } else {
  613. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  614. alignment_mask = 0x7;
  615. else
  616. alignment_mask = 0x3;
  617. /* Check if the watchpoint value matches. */
  618. val = read_wb_reg(ARM_BASE_WVR + i);
  619. if (val != (addr & ~alignment_mask))
  620. goto unlock;
  621. /* Possible match, check the byte address select. */
  622. ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
  623. decode_ctrl_reg(ctrl_reg, &ctrl);
  624. if (!((1 << (addr & alignment_mask)) & ctrl.len))
  625. goto unlock;
  626. /* Check that the access type matches. */
  627. if (debug_exception_updates_fsr()) {
  628. access = (fsr & ARM_FSR_ACCESS_MASK) ?
  629. HW_BREAKPOINT_W : HW_BREAKPOINT_R;
  630. if (!(access & hw_breakpoint_type(wp)))
  631. goto unlock;
  632. }
  633. /* We have a winner. */
  634. info->trigger = addr;
  635. }
  636. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  637. perf_bp_event(wp, regs);
  638. /*
  639. * If no overflow handler is present, insert a temporary
  640. * mismatch breakpoint so we can single-step over the
  641. * watchpoint trigger.
  642. */
  643. if (!wp->overflow_handler)
  644. enable_single_step(wp, instruction_pointer(regs));
  645. unlock:
  646. rcu_read_unlock();
  647. }
  648. }
  649. static void watchpoint_single_step_handler(unsigned long pc)
  650. {
  651. int i;
  652. struct perf_event *wp, **slots;
  653. struct arch_hw_breakpoint *info;
  654. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  655. for (i = 0; i < core_num_wrps; ++i) {
  656. rcu_read_lock();
  657. wp = slots[i];
  658. if (wp == NULL)
  659. goto unlock;
  660. info = counter_arch_bp(wp);
  661. if (!info->step_ctrl.enabled)
  662. goto unlock;
  663. /*
  664. * Restore the original watchpoint if we've completed the
  665. * single-step.
  666. */
  667. if (info->trigger != pc)
  668. disable_single_step(wp);
  669. unlock:
  670. rcu_read_unlock();
  671. }
  672. }
  673. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  674. {
  675. int i;
  676. u32 ctrl_reg, val, addr;
  677. struct perf_event *bp, **slots;
  678. struct arch_hw_breakpoint *info;
  679. struct arch_hw_breakpoint_ctrl ctrl;
  680. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  681. /* The exception entry code places the amended lr in the PC. */
  682. addr = regs->ARM_pc;
  683. /* Check the currently installed breakpoints first. */
  684. for (i = 0; i < core_num_brps; ++i) {
  685. rcu_read_lock();
  686. bp = slots[i];
  687. if (bp == NULL)
  688. goto unlock;
  689. info = counter_arch_bp(bp);
  690. /* Check if the breakpoint value matches. */
  691. val = read_wb_reg(ARM_BASE_BVR + i);
  692. if (val != (addr & ~0x3))
  693. goto mismatch;
  694. /* Possible match, check the byte address select to confirm. */
  695. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  696. decode_ctrl_reg(ctrl_reg, &ctrl);
  697. if ((1 << (addr & 0x3)) & ctrl.len) {
  698. info->trigger = addr;
  699. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  700. perf_bp_event(bp, regs);
  701. if (!bp->overflow_handler)
  702. enable_single_step(bp, addr);
  703. goto unlock;
  704. }
  705. mismatch:
  706. /* If we're stepping a breakpoint, it can now be restored. */
  707. if (info->step_ctrl.enabled)
  708. disable_single_step(bp);
  709. unlock:
  710. rcu_read_unlock();
  711. }
  712. /* Handle any pending watchpoint single-step breakpoints. */
  713. watchpoint_single_step_handler(addr);
  714. }
  715. /*
  716. * Called from either the Data Abort Handler [watchpoint] or the
  717. * Prefetch Abort Handler [breakpoint] with interrupts disabled.
  718. */
  719. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  720. struct pt_regs *regs)
  721. {
  722. int ret = 0;
  723. u32 dscr;
  724. preempt_disable();
  725. if (interrupts_enabled(regs))
  726. local_irq_enable();
  727. /* We only handle watchpoints and hardware breakpoints. */
  728. ARM_DBG_READ(c1, 0, dscr);
  729. /* Perform perf callbacks. */
  730. switch (ARM_DSCR_MOE(dscr)) {
  731. case ARM_ENTRY_BREAKPOINT:
  732. breakpoint_handler(addr, regs);
  733. break;
  734. case ARM_ENTRY_ASYNC_WATCHPOINT:
  735. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  736. case ARM_ENTRY_SYNC_WATCHPOINT:
  737. watchpoint_handler(addr, fsr, regs);
  738. break;
  739. default:
  740. ret = 1; /* Unhandled fault. */
  741. }
  742. preempt_enable();
  743. return ret;
  744. }
  745. /*
  746. * One-time initialisation.
  747. */
  748. static cpumask_t debug_err_mask;
  749. static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
  750. {
  751. int cpu = smp_processor_id();
  752. pr_warning("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
  753. instr, cpu);
  754. /* Set the error flag for this CPU and skip the faulting instruction. */
  755. cpumask_set_cpu(cpu, &debug_err_mask);
  756. instruction_pointer(regs) += 4;
  757. return 0;
  758. }
  759. static struct undef_hook debug_reg_hook = {
  760. .instr_mask = 0x0fe80f10,
  761. .instr_val = 0x0e000e10,
  762. .fn = debug_reg_trap,
  763. };
  764. static void reset_ctrl_regs(void *unused)
  765. {
  766. int i, raw_num_brps, err = 0, cpu = smp_processor_id();
  767. u32 val;
  768. /*
  769. * v7 debug contains save and restore registers so that debug state
  770. * can be maintained across low-power modes without leaving the debug
  771. * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
  772. * the debug registers out of reset, so we must unlock the OS Lock
  773. * Access Register to avoid taking undefined instruction exceptions
  774. * later on.
  775. */
  776. switch (debug_arch) {
  777. case ARM_DEBUG_ARCH_V6:
  778. case ARM_DEBUG_ARCH_V6_1:
  779. /* ARMv6 cores just need to reset the registers. */
  780. goto reset_regs;
  781. case ARM_DEBUG_ARCH_V7_ECP14:
  782. /*
  783. * Ensure sticky power-down is clear (i.e. debug logic is
  784. * powered up).
  785. */
  786. asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (val));
  787. if ((val & 0x1) == 0)
  788. err = -EPERM;
  789. /*
  790. * Check whether we implement OS save and restore.
  791. */
  792. asm volatile("mrc p14, 0, %0, c1, c1, 4" : "=r" (val));
  793. if ((val & 0x9) == 0)
  794. goto clear_vcr;
  795. break;
  796. case ARM_DEBUG_ARCH_V7_1:
  797. /*
  798. * Ensure the OS double lock is clear.
  799. */
  800. asm volatile("mrc p14, 0, %0, c1, c3, 4" : "=r" (val));
  801. if ((val & 0x1) == 1)
  802. err = -EPERM;
  803. break;
  804. }
  805. if (err) {
  806. pr_warning("CPU %d debug is powered down!\n", cpu);
  807. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  808. return;
  809. }
  810. /*
  811. * Unconditionally clear the OS lock by writing a value
  812. * other than 0xC5ACCE55 to the access register.
  813. */
  814. asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
  815. isb();
  816. /*
  817. * Clear any configured vector-catch events before
  818. * enabling monitor mode.
  819. */
  820. clear_vcr:
  821. asm volatile("mcr p14, 0, %0, c0, c7, 0" : : "r" (0));
  822. isb();
  823. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  824. pr_warning("CPU %d failed to disable vector catch\n", cpu);
  825. return;
  826. }
  827. reset_regs:
  828. /*
  829. * The control/value register pairs are UNKNOWN out of reset so
  830. * clear them to avoid spurious debug events.
  831. */
  832. raw_num_brps = get_num_brp_resources();
  833. for (i = 0; i < raw_num_brps; ++i) {
  834. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  835. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  836. }
  837. for (i = 0; i < core_num_wrps; ++i) {
  838. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  839. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  840. }
  841. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  842. pr_warning("CPU %d failed to clear debug register pairs\n", cpu);
  843. return;
  844. }
  845. /*
  846. * Have a crack at enabling monitor mode. We don't actually need
  847. * it yet, but reporting an error early is useful if it fails.
  848. */
  849. if (enable_monitor_mode())
  850. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  851. }
  852. static int __cpuinit dbg_reset_notify(struct notifier_block *self,
  853. unsigned long action, void *cpu)
  854. {
  855. if (action == CPU_ONLINE)
  856. smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
  857. return NOTIFY_OK;
  858. }
  859. static struct notifier_block __cpuinitdata dbg_reset_nb = {
  860. .notifier_call = dbg_reset_notify,
  861. };
  862. static int __init arch_hw_breakpoint_init(void)
  863. {
  864. debug_arch = get_debug_arch();
  865. if (!debug_arch_supported()) {
  866. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  867. return 0;
  868. }
  869. /* Determine how many BRPs/WRPs are available. */
  870. core_num_brps = get_num_brps();
  871. core_num_wrps = get_num_wrps();
  872. /*
  873. * We need to tread carefully here because DBGSWENABLE may be
  874. * driven low on this core and there isn't an architected way to
  875. * determine that.
  876. */
  877. register_undef_hook(&debug_reg_hook);
  878. /*
  879. * Reset the breakpoint resources. We assume that a halting
  880. * debugger will leave the world in a nice state for us.
  881. */
  882. on_each_cpu(reset_ctrl_regs, NULL, 1);
  883. unregister_undef_hook(&debug_reg_hook);
  884. if (!cpumask_empty(&debug_err_mask)) {
  885. core_num_brps = 0;
  886. core_num_wrps = 0;
  887. return 0;
  888. }
  889. pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
  890. core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
  891. "", core_num_wrps);
  892. /* Work out the maximum supported watchpoint length. */
  893. max_watchpoint_len = get_max_wp_len();
  894. pr_info("maximum watchpoint size is %u bytes.\n",
  895. max_watchpoint_len);
  896. /* Register debug fault handler. */
  897. hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  898. TRAP_HWBKPT, "watchpoint debug exception");
  899. hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  900. TRAP_HWBKPT, "breakpoint debug exception");
  901. /* Register hotplug notifier. */
  902. register_cpu_notifier(&dbg_reset_nb);
  903. return 0;
  904. }
  905. arch_initcall(arch_hw_breakpoint_init);
  906. void hw_breakpoint_pmu_read(struct perf_event *bp)
  907. {
  908. }
  909. /*
  910. * Dummy function to register with die_notifier.
  911. */
  912. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  913. unsigned long val, void *data)
  914. {
  915. return NOTIFY_DONE;
  916. }