hw_breakpoint.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 __ro_after_init;
  42. static int core_num_wrps __ro_after_init;
  43. /* Debug architecture version. */
  44. static u8 debug_arch __ro_after_init;
  45. /* Does debug architecture support OS Save and Restore? */
  46. static bool has_ossr __ro_after_init;
  47. /* Maximum supported watchpoint length. */
  48. static u8 max_watchpoint_len __ro_after_init;
  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 arch_hw_breakpoint *hw)
  396. {
  397. unsigned int len;
  398. unsigned long va;
  399. va = hw->address;
  400. len = get_hbp_len(hw->ctrl.len);
  401. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  402. }
  403. /*
  404. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  405. * Hopefully this will disappear when ptrace can bypass the conversion
  406. * to generic breakpoint descriptions.
  407. */
  408. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  409. int *gen_len, int *gen_type)
  410. {
  411. /* Type */
  412. switch (ctrl.type) {
  413. case ARM_BREAKPOINT_EXECUTE:
  414. *gen_type = HW_BREAKPOINT_X;
  415. break;
  416. case ARM_BREAKPOINT_LOAD:
  417. *gen_type = HW_BREAKPOINT_R;
  418. break;
  419. case ARM_BREAKPOINT_STORE:
  420. *gen_type = HW_BREAKPOINT_W;
  421. break;
  422. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  423. *gen_type = HW_BREAKPOINT_RW;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. /* Len */
  429. switch (ctrl.len) {
  430. case ARM_BREAKPOINT_LEN_1:
  431. *gen_len = HW_BREAKPOINT_LEN_1;
  432. break;
  433. case ARM_BREAKPOINT_LEN_2:
  434. *gen_len = HW_BREAKPOINT_LEN_2;
  435. break;
  436. case ARM_BREAKPOINT_LEN_4:
  437. *gen_len = HW_BREAKPOINT_LEN_4;
  438. break;
  439. case ARM_BREAKPOINT_LEN_8:
  440. *gen_len = HW_BREAKPOINT_LEN_8;
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. return 0;
  446. }
  447. /*
  448. * Construct an arch_hw_breakpoint from a perf_event.
  449. */
  450. static int arch_build_bp_info(struct perf_event *bp,
  451. const struct perf_event_attr *attr,
  452. struct arch_hw_breakpoint *hw)
  453. {
  454. /* Type */
  455. switch (attr->bp_type) {
  456. case HW_BREAKPOINT_X:
  457. hw->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  458. break;
  459. case HW_BREAKPOINT_R:
  460. hw->ctrl.type = ARM_BREAKPOINT_LOAD;
  461. break;
  462. case HW_BREAKPOINT_W:
  463. hw->ctrl.type = ARM_BREAKPOINT_STORE;
  464. break;
  465. case HW_BREAKPOINT_RW:
  466. hw->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. /* Len */
  472. switch (attr->bp_len) {
  473. case HW_BREAKPOINT_LEN_1:
  474. hw->ctrl.len = ARM_BREAKPOINT_LEN_1;
  475. break;
  476. case HW_BREAKPOINT_LEN_2:
  477. hw->ctrl.len = ARM_BREAKPOINT_LEN_2;
  478. break;
  479. case HW_BREAKPOINT_LEN_4:
  480. hw->ctrl.len = ARM_BREAKPOINT_LEN_4;
  481. break;
  482. case HW_BREAKPOINT_LEN_8:
  483. hw->ctrl.len = ARM_BREAKPOINT_LEN_8;
  484. if ((hw->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 (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  497. hw->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  498. hw->ctrl.len != ARM_BREAKPOINT_LEN_4)
  499. return -EINVAL;
  500. /* Address */
  501. hw->address = attr->bp_addr;
  502. /* Privilege */
  503. hw->ctrl.privilege = ARM_BREAKPOINT_USER;
  504. if (arch_check_bp_in_kernelspace(hw))
  505. hw->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  506. /* Enabled? */
  507. hw->ctrl.enabled = !attr->disabled;
  508. /* Mismatch */
  509. hw->ctrl.mismatch = 0;
  510. return 0;
  511. }
  512. /*
  513. * Validate the arch-specific HW Breakpoint register settings.
  514. */
  515. int hw_breakpoint_arch_parse(struct perf_event *bp,
  516. const struct perf_event_attr *attr,
  517. struct arch_hw_breakpoint *hw)
  518. {
  519. int ret = 0;
  520. u32 offset, alignment_mask = 0x3;
  521. /* Ensure that we are in monitor debug mode. */
  522. if (!monitor_mode_enabled())
  523. return -ENODEV;
  524. /* Build the arch_hw_breakpoint. */
  525. ret = arch_build_bp_info(bp, attr, hw);
  526. if (ret)
  527. goto out;
  528. /* Check address alignment. */
  529. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_8)
  530. alignment_mask = 0x7;
  531. offset = hw->address & alignment_mask;
  532. switch (offset) {
  533. case 0:
  534. /* Aligned */
  535. break;
  536. case 1:
  537. case 2:
  538. /* Allow halfword watchpoints and breakpoints. */
  539. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_2)
  540. break;
  541. case 3:
  542. /* Allow single byte watchpoint. */
  543. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_1)
  544. break;
  545. default:
  546. ret = -EINVAL;
  547. goto out;
  548. }
  549. hw->address &= ~alignment_mask;
  550. hw->ctrl.len <<= offset;
  551. if (is_default_overflow_handler(bp)) {
  552. /*
  553. * Mismatch breakpoints are required for single-stepping
  554. * breakpoints.
  555. */
  556. if (!core_has_mismatch_brps())
  557. return -EINVAL;
  558. /* We don't allow mismatch breakpoints in kernel space. */
  559. if (arch_check_bp_in_kernelspace(hw))
  560. return -EPERM;
  561. /*
  562. * Per-cpu breakpoints are not supported by our stepping
  563. * mechanism.
  564. */
  565. if (!bp->hw.target)
  566. return -EINVAL;
  567. /*
  568. * We only support specific access types if the fsr
  569. * reports them.
  570. */
  571. if (!debug_exception_updates_fsr() &&
  572. (hw->ctrl.type == ARM_BREAKPOINT_LOAD ||
  573. hw->ctrl.type == ARM_BREAKPOINT_STORE))
  574. return -EINVAL;
  575. }
  576. out:
  577. return ret;
  578. }
  579. /*
  580. * Enable/disable single-stepping over the breakpoint bp at address addr.
  581. */
  582. static void enable_single_step(struct perf_event *bp, u32 addr)
  583. {
  584. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  585. arch_uninstall_hw_breakpoint(bp);
  586. info->step_ctrl.mismatch = 1;
  587. info->step_ctrl.len = ARM_BREAKPOINT_LEN_4;
  588. info->step_ctrl.type = ARM_BREAKPOINT_EXECUTE;
  589. info->step_ctrl.privilege = info->ctrl.privilege;
  590. info->step_ctrl.enabled = 1;
  591. info->trigger = addr;
  592. arch_install_hw_breakpoint(bp);
  593. }
  594. static void disable_single_step(struct perf_event *bp)
  595. {
  596. arch_uninstall_hw_breakpoint(bp);
  597. counter_arch_bp(bp)->step_ctrl.enabled = 0;
  598. arch_install_hw_breakpoint(bp);
  599. }
  600. static void watchpoint_handler(unsigned long addr, unsigned int fsr,
  601. struct pt_regs *regs)
  602. {
  603. int i, access;
  604. u32 val, ctrl_reg, alignment_mask;
  605. struct perf_event *wp, **slots;
  606. struct arch_hw_breakpoint *info;
  607. struct arch_hw_breakpoint_ctrl ctrl;
  608. slots = this_cpu_ptr(wp_on_reg);
  609. for (i = 0; i < core_num_wrps; ++i) {
  610. rcu_read_lock();
  611. wp = slots[i];
  612. if (wp == NULL)
  613. goto unlock;
  614. info = counter_arch_bp(wp);
  615. /*
  616. * The DFAR is an unknown value on debug architectures prior
  617. * to 7.1. Since we only allow a single watchpoint on these
  618. * older CPUs, we can set the trigger to the lowest possible
  619. * faulting address.
  620. */
  621. if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
  622. BUG_ON(i > 0);
  623. info->trigger = wp->attr.bp_addr;
  624. } else {
  625. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  626. alignment_mask = 0x7;
  627. else
  628. alignment_mask = 0x3;
  629. /* Check if the watchpoint value matches. */
  630. val = read_wb_reg(ARM_BASE_WVR + i);
  631. if (val != (addr & ~alignment_mask))
  632. goto unlock;
  633. /* Possible match, check the byte address select. */
  634. ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
  635. decode_ctrl_reg(ctrl_reg, &ctrl);
  636. if (!((1 << (addr & alignment_mask)) & ctrl.len))
  637. goto unlock;
  638. /* Check that the access type matches. */
  639. if (debug_exception_updates_fsr()) {
  640. access = (fsr & ARM_FSR_ACCESS_MASK) ?
  641. HW_BREAKPOINT_W : HW_BREAKPOINT_R;
  642. if (!(access & hw_breakpoint_type(wp)))
  643. goto unlock;
  644. }
  645. /* We have a winner. */
  646. info->trigger = addr;
  647. }
  648. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  649. perf_bp_event(wp, regs);
  650. /*
  651. * If no overflow handler is present, insert a temporary
  652. * mismatch breakpoint so we can single-step over the
  653. * watchpoint trigger.
  654. */
  655. if (is_default_overflow_handler(wp))
  656. enable_single_step(wp, instruction_pointer(regs));
  657. unlock:
  658. rcu_read_unlock();
  659. }
  660. }
  661. static void watchpoint_single_step_handler(unsigned long pc)
  662. {
  663. int i;
  664. struct perf_event *wp, **slots;
  665. struct arch_hw_breakpoint *info;
  666. slots = this_cpu_ptr(wp_on_reg);
  667. for (i = 0; i < core_num_wrps; ++i) {
  668. rcu_read_lock();
  669. wp = slots[i];
  670. if (wp == NULL)
  671. goto unlock;
  672. info = counter_arch_bp(wp);
  673. if (!info->step_ctrl.enabled)
  674. goto unlock;
  675. /*
  676. * Restore the original watchpoint if we've completed the
  677. * single-step.
  678. */
  679. if (info->trigger != pc)
  680. disable_single_step(wp);
  681. unlock:
  682. rcu_read_unlock();
  683. }
  684. }
  685. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  686. {
  687. int i;
  688. u32 ctrl_reg, val, addr;
  689. struct perf_event *bp, **slots;
  690. struct arch_hw_breakpoint *info;
  691. struct arch_hw_breakpoint_ctrl ctrl;
  692. slots = this_cpu_ptr(bp_on_reg);
  693. /* The exception entry code places the amended lr in the PC. */
  694. addr = regs->ARM_pc;
  695. /* Check the currently installed breakpoints first. */
  696. for (i = 0; i < core_num_brps; ++i) {
  697. rcu_read_lock();
  698. bp = slots[i];
  699. if (bp == NULL)
  700. goto unlock;
  701. info = counter_arch_bp(bp);
  702. /* Check if the breakpoint value matches. */
  703. val = read_wb_reg(ARM_BASE_BVR + i);
  704. if (val != (addr & ~0x3))
  705. goto mismatch;
  706. /* Possible match, check the byte address select to confirm. */
  707. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  708. decode_ctrl_reg(ctrl_reg, &ctrl);
  709. if ((1 << (addr & 0x3)) & ctrl.len) {
  710. info->trigger = addr;
  711. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  712. perf_bp_event(bp, regs);
  713. if (!bp->overflow_handler)
  714. enable_single_step(bp, addr);
  715. goto unlock;
  716. }
  717. mismatch:
  718. /* If we're stepping a breakpoint, it can now be restored. */
  719. if (info->step_ctrl.enabled)
  720. disable_single_step(bp);
  721. unlock:
  722. rcu_read_unlock();
  723. }
  724. /* Handle any pending watchpoint single-step breakpoints. */
  725. watchpoint_single_step_handler(addr);
  726. }
  727. /*
  728. * Called from either the Data Abort Handler [watchpoint] or the
  729. * Prefetch Abort Handler [breakpoint] with interrupts disabled.
  730. */
  731. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  732. struct pt_regs *regs)
  733. {
  734. int ret = 0;
  735. u32 dscr;
  736. preempt_disable();
  737. if (interrupts_enabled(regs))
  738. local_irq_enable();
  739. /* We only handle watchpoints and hardware breakpoints. */
  740. ARM_DBG_READ(c0, c1, 0, dscr);
  741. /* Perform perf callbacks. */
  742. switch (ARM_DSCR_MOE(dscr)) {
  743. case ARM_ENTRY_BREAKPOINT:
  744. breakpoint_handler(addr, regs);
  745. break;
  746. case ARM_ENTRY_ASYNC_WATCHPOINT:
  747. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  748. case ARM_ENTRY_SYNC_WATCHPOINT:
  749. watchpoint_handler(addr, fsr, regs);
  750. break;
  751. default:
  752. ret = 1; /* Unhandled fault. */
  753. }
  754. preempt_enable();
  755. return ret;
  756. }
  757. /*
  758. * One-time initialisation.
  759. */
  760. static cpumask_t debug_err_mask;
  761. static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
  762. {
  763. int cpu = smp_processor_id();
  764. pr_warn("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
  765. instr, cpu);
  766. /* Set the error flag for this CPU and skip the faulting instruction. */
  767. cpumask_set_cpu(cpu, &debug_err_mask);
  768. instruction_pointer(regs) += 4;
  769. return 0;
  770. }
  771. static struct undef_hook debug_reg_hook = {
  772. .instr_mask = 0x0fe80f10,
  773. .instr_val = 0x0e000e10,
  774. .fn = debug_reg_trap,
  775. };
  776. /* Does this core support OS Save and Restore? */
  777. static bool core_has_os_save_restore(void)
  778. {
  779. u32 oslsr;
  780. switch (get_debug_arch()) {
  781. case ARM_DEBUG_ARCH_V7_1:
  782. return true;
  783. case ARM_DEBUG_ARCH_V7_ECP14:
  784. ARM_DBG_READ(c1, c1, 4, oslsr);
  785. if (oslsr & ARM_OSLSR_OSLM0)
  786. return true;
  787. default:
  788. return false;
  789. }
  790. }
  791. static void reset_ctrl_regs(unsigned int cpu)
  792. {
  793. int i, raw_num_brps, err = 0;
  794. u32 val;
  795. /*
  796. * v7 debug contains save and restore registers so that debug state
  797. * can be maintained across low-power modes without leaving the debug
  798. * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
  799. * the debug registers out of reset, so we must unlock the OS Lock
  800. * Access Register to avoid taking undefined instruction exceptions
  801. * later on.
  802. */
  803. switch (debug_arch) {
  804. case ARM_DEBUG_ARCH_V6:
  805. case ARM_DEBUG_ARCH_V6_1:
  806. /* ARMv6 cores clear the registers out of reset. */
  807. goto out_mdbgen;
  808. case ARM_DEBUG_ARCH_V7_ECP14:
  809. /*
  810. * Ensure sticky power-down is clear (i.e. debug logic is
  811. * powered up).
  812. */
  813. ARM_DBG_READ(c1, c5, 4, val);
  814. if ((val & 0x1) == 0)
  815. err = -EPERM;
  816. if (!has_ossr)
  817. goto clear_vcr;
  818. break;
  819. case ARM_DEBUG_ARCH_V7_1:
  820. /*
  821. * Ensure the OS double lock is clear.
  822. */
  823. ARM_DBG_READ(c1, c3, 4, val);
  824. if ((val & 0x1) == 1)
  825. err = -EPERM;
  826. break;
  827. }
  828. if (err) {
  829. pr_warn_once("CPU %d debug is powered down!\n", cpu);
  830. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  831. return;
  832. }
  833. /*
  834. * Unconditionally clear the OS lock by writing a value
  835. * other than CS_LAR_KEY to the access register.
  836. */
  837. ARM_DBG_WRITE(c1, c0, 4, ~CORESIGHT_UNLOCK);
  838. isb();
  839. /*
  840. * Clear any configured vector-catch events before
  841. * enabling monitor mode.
  842. */
  843. clear_vcr:
  844. ARM_DBG_WRITE(c0, c7, 0, 0);
  845. isb();
  846. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  847. pr_warn_once("CPU %d failed to disable vector catch\n", cpu);
  848. return;
  849. }
  850. /*
  851. * The control/value register pairs are UNKNOWN out of reset so
  852. * clear them to avoid spurious debug events.
  853. */
  854. raw_num_brps = get_num_brp_resources();
  855. for (i = 0; i < raw_num_brps; ++i) {
  856. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  857. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  858. }
  859. for (i = 0; i < core_num_wrps; ++i) {
  860. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  861. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  862. }
  863. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  864. pr_warn_once("CPU %d failed to clear debug register pairs\n", cpu);
  865. return;
  866. }
  867. /*
  868. * Have a crack at enabling monitor mode. We don't actually need
  869. * it yet, but reporting an error early is useful if it fails.
  870. */
  871. out_mdbgen:
  872. if (enable_monitor_mode())
  873. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  874. }
  875. static int dbg_reset_online(unsigned int cpu)
  876. {
  877. local_irq_disable();
  878. reset_ctrl_regs(cpu);
  879. local_irq_enable();
  880. return 0;
  881. }
  882. #ifdef CONFIG_CPU_PM
  883. static int dbg_cpu_pm_notify(struct notifier_block *self, unsigned long action,
  884. void *v)
  885. {
  886. if (action == CPU_PM_EXIT)
  887. reset_ctrl_regs(smp_processor_id());
  888. return NOTIFY_OK;
  889. }
  890. static struct notifier_block dbg_cpu_pm_nb = {
  891. .notifier_call = dbg_cpu_pm_notify,
  892. };
  893. static void __init pm_init(void)
  894. {
  895. cpu_pm_register_notifier(&dbg_cpu_pm_nb);
  896. }
  897. #else
  898. static inline void pm_init(void)
  899. {
  900. }
  901. #endif
  902. static int __init arch_hw_breakpoint_init(void)
  903. {
  904. int ret;
  905. debug_arch = get_debug_arch();
  906. if (!debug_arch_supported()) {
  907. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  908. return 0;
  909. }
  910. /*
  911. * Scorpion CPUs (at least those in APQ8060) seem to set DBGPRSR.SPD
  912. * whenever a WFI is issued, even if the core is not powered down, in
  913. * violation of the architecture. When DBGPRSR.SPD is set, accesses to
  914. * breakpoint and watchpoint registers are treated as undefined, so
  915. * this results in boot time and runtime failures when these are
  916. * accessed and we unexpectedly take a trap.
  917. *
  918. * It's not clear if/how this can be worked around, so we blacklist
  919. * Scorpion CPUs to avoid these issues.
  920. */
  921. if (read_cpuid_part() == ARM_CPU_PART_SCORPION) {
  922. pr_info("Scorpion CPU detected. Hardware breakpoints and watchpoints disabled\n");
  923. return 0;
  924. }
  925. has_ossr = core_has_os_save_restore();
  926. /* Determine how many BRPs/WRPs are available. */
  927. core_num_brps = get_num_brps();
  928. core_num_wrps = get_num_wrps();
  929. /*
  930. * We need to tread carefully here because DBGSWENABLE may be
  931. * driven low on this core and there isn't an architected way to
  932. * determine that.
  933. */
  934. cpus_read_lock();
  935. register_undef_hook(&debug_reg_hook);
  936. /*
  937. * Register CPU notifier which resets the breakpoint resources. We
  938. * assume that a halting debugger will leave the world in a nice state
  939. * for us.
  940. */
  941. ret = cpuhp_setup_state_cpuslocked(CPUHP_AP_ONLINE_DYN,
  942. "arm/hw_breakpoint:online",
  943. dbg_reset_online, NULL);
  944. unregister_undef_hook(&debug_reg_hook);
  945. if (WARN_ON(ret < 0) || !cpumask_empty(&debug_err_mask)) {
  946. core_num_brps = 0;
  947. core_num_wrps = 0;
  948. if (ret > 0)
  949. cpuhp_remove_state_nocalls_cpuslocked(ret);
  950. cpus_read_unlock();
  951. return 0;
  952. }
  953. pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
  954. core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
  955. "", core_num_wrps);
  956. /* Work out the maximum supported watchpoint length. */
  957. max_watchpoint_len = get_max_wp_len();
  958. pr_info("maximum watchpoint size is %u bytes.\n",
  959. max_watchpoint_len);
  960. /* Register debug fault handler. */
  961. hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  962. TRAP_HWBKPT, "watchpoint debug exception");
  963. hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  964. TRAP_HWBKPT, "breakpoint debug exception");
  965. cpus_read_unlock();
  966. /* Register PM notifiers. */
  967. pm_init();
  968. return 0;
  969. }
  970. arch_initcall(arch_hw_breakpoint_init);
  971. void hw_breakpoint_pmu_read(struct perf_event *bp)
  972. {
  973. }
  974. /*
  975. * Dummy function to register with die_notifier.
  976. */
  977. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  978. unsigned long val, void *data)
  979. {
  980. return NOTIFY_DONE;
  981. }