hw_breakpoint.c 30 KB

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