hw_breakpoint.c 27 KB

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