hw_breakpoint.c 29 KB

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