hw_breakpoint.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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/kdebug.h>
  36. #include <asm/traps.h>
  37. /* Breakpoint currently in use for each BRP. */
  38. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
  39. /* Watchpoint currently in use for each WRP. */
  40. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
  41. /* Number of BRP/WRP registers on this CPU. */
  42. static int core_num_brps;
  43. static int core_num_wrps;
  44. /* Debug architecture version. */
  45. static u8 debug_arch;
  46. /* Does debug architecture support OS Save and Restore? */
  47. static bool has_ossr;
  48. /* Maximum supported watchpoint length. */
  49. static u8 max_watchpoint_len;
  50. #define READ_WB_REG_CASE(OP2, M, VAL) \
  51. case ((OP2 << 4) + M): \
  52. ARM_DBG_READ(c0, c ## M, OP2, VAL); \
  53. break
  54. #define WRITE_WB_REG_CASE(OP2, M, VAL) \
  55. case ((OP2 << 4) + M): \
  56. ARM_DBG_WRITE(c0, c ## M, OP2, VAL); \
  57. break
  58. #define GEN_READ_WB_REG_CASES(OP2, VAL) \
  59. READ_WB_REG_CASE(OP2, 0, VAL); \
  60. READ_WB_REG_CASE(OP2, 1, VAL); \
  61. READ_WB_REG_CASE(OP2, 2, VAL); \
  62. READ_WB_REG_CASE(OP2, 3, VAL); \
  63. READ_WB_REG_CASE(OP2, 4, VAL); \
  64. READ_WB_REG_CASE(OP2, 5, VAL); \
  65. READ_WB_REG_CASE(OP2, 6, VAL); \
  66. READ_WB_REG_CASE(OP2, 7, VAL); \
  67. READ_WB_REG_CASE(OP2, 8, VAL); \
  68. READ_WB_REG_CASE(OP2, 9, VAL); \
  69. READ_WB_REG_CASE(OP2, 10, VAL); \
  70. READ_WB_REG_CASE(OP2, 11, VAL); \
  71. READ_WB_REG_CASE(OP2, 12, VAL); \
  72. READ_WB_REG_CASE(OP2, 13, VAL); \
  73. READ_WB_REG_CASE(OP2, 14, VAL); \
  74. READ_WB_REG_CASE(OP2, 15, VAL)
  75. #define GEN_WRITE_WB_REG_CASES(OP2, VAL) \
  76. WRITE_WB_REG_CASE(OP2, 0, VAL); \
  77. WRITE_WB_REG_CASE(OP2, 1, VAL); \
  78. WRITE_WB_REG_CASE(OP2, 2, VAL); \
  79. WRITE_WB_REG_CASE(OP2, 3, VAL); \
  80. WRITE_WB_REG_CASE(OP2, 4, VAL); \
  81. WRITE_WB_REG_CASE(OP2, 5, VAL); \
  82. WRITE_WB_REG_CASE(OP2, 6, VAL); \
  83. WRITE_WB_REG_CASE(OP2, 7, VAL); \
  84. WRITE_WB_REG_CASE(OP2, 8, VAL); \
  85. WRITE_WB_REG_CASE(OP2, 9, VAL); \
  86. WRITE_WB_REG_CASE(OP2, 10, VAL); \
  87. WRITE_WB_REG_CASE(OP2, 11, VAL); \
  88. WRITE_WB_REG_CASE(OP2, 12, VAL); \
  89. WRITE_WB_REG_CASE(OP2, 13, VAL); \
  90. WRITE_WB_REG_CASE(OP2, 14, VAL); \
  91. WRITE_WB_REG_CASE(OP2, 15, VAL)
  92. static u32 read_wb_reg(int n)
  93. {
  94. u32 val = 0;
  95. switch (n) {
  96. GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
  97. GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
  98. GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
  99. GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
  100. default:
  101. pr_warn("attempt to read from unknown breakpoint register %d\n",
  102. n);
  103. }
  104. return val;
  105. }
  106. static void write_wb_reg(int n, u32 val)
  107. {
  108. switch (n) {
  109. GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
  110. GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
  111. GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
  112. GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
  113. default:
  114. pr_warn("attempt to write to unknown breakpoint register %d\n",
  115. n);
  116. }
  117. isb();
  118. }
  119. /* Determine debug architecture. */
  120. static u8 get_debug_arch(void)
  121. {
  122. u32 didr;
  123. /* Do we implement the extended CPUID interface? */
  124. if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
  125. pr_warn_once("CPUID feature registers not supported. "
  126. "Assuming v6 debug is present.\n");
  127. return ARM_DEBUG_ARCH_V6;
  128. }
  129. ARM_DBG_READ(c0, c0, 0, didr);
  130. return (didr >> 16) & 0xf;
  131. }
  132. u8 arch_get_debug_arch(void)
  133. {
  134. return debug_arch;
  135. }
  136. static int debug_arch_supported(void)
  137. {
  138. u8 arch = get_debug_arch();
  139. /* We don't support the memory-mapped interface. */
  140. return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
  141. arch >= ARM_DEBUG_ARCH_V7_1;
  142. }
  143. /* Can we determine the watchpoint access type from the fsr? */
  144. static int debug_exception_updates_fsr(void)
  145. {
  146. return get_debug_arch() >= ARM_DEBUG_ARCH_V8;
  147. }
  148. /* Determine number of WRP registers available. */
  149. static int get_num_wrp_resources(void)
  150. {
  151. u32 didr;
  152. ARM_DBG_READ(c0, c0, 0, didr);
  153. return ((didr >> 28) & 0xf) + 1;
  154. }
  155. /* Determine number of BRP registers available. */
  156. static int get_num_brp_resources(void)
  157. {
  158. u32 didr;
  159. ARM_DBG_READ(c0, c0, 0, didr);
  160. return ((didr >> 24) & 0xf) + 1;
  161. }
  162. /* Does this core support mismatch breakpoints? */
  163. static int core_has_mismatch_brps(void)
  164. {
  165. return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
  166. get_num_brp_resources() > 1);
  167. }
  168. /* Determine number of usable WRPs available. */
  169. static int get_num_wrps(void)
  170. {
  171. /*
  172. * On debug architectures prior to 7.1, when a watchpoint fires, the
  173. * only way to work out which watchpoint it was is by disassembling
  174. * the faulting instruction and working out the address of the memory
  175. * access.
  176. *
  177. * Furthermore, we can only do this if the watchpoint was precise
  178. * since imprecise watchpoints prevent us from calculating register
  179. * based addresses.
  180. *
  181. * Providing we have more than 1 breakpoint register, we only report
  182. * a single watchpoint register for the time being. This way, we always
  183. * know which watchpoint fired. In the future we can either add a
  184. * disassembler and address generation emulator, or we can insert a
  185. * check to see if the DFAR is set on watchpoint exception entry
  186. * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
  187. * that it is set on some implementations].
  188. */
  189. if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
  190. return 1;
  191. return get_num_wrp_resources();
  192. }
  193. /* Determine number of usable BRPs available. */
  194. static int get_num_brps(void)
  195. {
  196. int brps = get_num_brp_resources();
  197. return core_has_mismatch_brps() ? brps - 1 : brps;
  198. }
  199. /*
  200. * In order to access the breakpoint/watchpoint control registers,
  201. * we must be running in debug monitor mode. Unfortunately, we can
  202. * be put into halting debug mode at any time by an external debugger
  203. * but there is nothing we can do to prevent that.
  204. */
  205. static int monitor_mode_enabled(void)
  206. {
  207. u32 dscr;
  208. ARM_DBG_READ(c0, c1, 0, dscr);
  209. return !!(dscr & ARM_DSCR_MDBGEN);
  210. }
  211. static int enable_monitor_mode(void)
  212. {
  213. u32 dscr;
  214. ARM_DBG_READ(c0, c1, 0, dscr);
  215. /* If monitor mode is already enabled, just return. */
  216. if (dscr & ARM_DSCR_MDBGEN)
  217. goto out;
  218. /* Write to the corresponding DSCR. */
  219. switch (get_debug_arch()) {
  220. case ARM_DEBUG_ARCH_V6:
  221. case ARM_DEBUG_ARCH_V6_1:
  222. ARM_DBG_WRITE(c0, c1, 0, (dscr | ARM_DSCR_MDBGEN));
  223. break;
  224. case ARM_DEBUG_ARCH_V7_ECP14:
  225. case ARM_DEBUG_ARCH_V7_1:
  226. case ARM_DEBUG_ARCH_V8:
  227. ARM_DBG_WRITE(c0, c2, 2, (dscr | ARM_DSCR_MDBGEN));
  228. isb();
  229. break;
  230. default:
  231. return -ENODEV;
  232. }
  233. /* Check that the write made it through. */
  234. ARM_DBG_READ(c0, c1, 0, dscr);
  235. if (!(dscr & ARM_DSCR_MDBGEN)) {
  236. pr_warn_once("Failed to enable monitor mode on CPU %d.\n",
  237. smp_processor_id());
  238. return -EPERM;
  239. }
  240. out:
  241. return 0;
  242. }
  243. int hw_breakpoint_slots(int type)
  244. {
  245. if (!debug_arch_supported())
  246. return 0;
  247. /*
  248. * We can be called early, so don't rely on
  249. * our static variables being initialised.
  250. */
  251. switch (type) {
  252. case TYPE_INST:
  253. return get_num_brps();
  254. case TYPE_DATA:
  255. return get_num_wrps();
  256. default:
  257. pr_warn("unknown slot type: %d\n", type);
  258. return 0;
  259. }
  260. }
  261. /*
  262. * Check if 8-bit byte-address select is available.
  263. * This clobbers WRP 0.
  264. */
  265. static u8 get_max_wp_len(void)
  266. {
  267. u32 ctrl_reg;
  268. struct arch_hw_breakpoint_ctrl ctrl;
  269. u8 size = 4;
  270. if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
  271. goto out;
  272. memset(&ctrl, 0, sizeof(ctrl));
  273. ctrl.len = ARM_BREAKPOINT_LEN_8;
  274. ctrl_reg = encode_ctrl_reg(ctrl);
  275. write_wb_reg(ARM_BASE_WVR, 0);
  276. write_wb_reg(ARM_BASE_WCR, ctrl_reg);
  277. if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
  278. size = 8;
  279. out:
  280. return size;
  281. }
  282. u8 arch_get_max_wp_len(void)
  283. {
  284. return max_watchpoint_len;
  285. }
  286. /*
  287. * Install a perf counter breakpoint.
  288. */
  289. int arch_install_hw_breakpoint(struct perf_event *bp)
  290. {
  291. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  292. struct perf_event **slot, **slots;
  293. int i, max_slots, ctrl_base, val_base;
  294. u32 addr, ctrl;
  295. addr = info->address;
  296. ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
  297. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  298. /* Breakpoint */
  299. ctrl_base = ARM_BASE_BCR;
  300. val_base = ARM_BASE_BVR;
  301. slots = this_cpu_ptr(bp_on_reg);
  302. max_slots = core_num_brps;
  303. } else {
  304. /* Watchpoint */
  305. ctrl_base = ARM_BASE_WCR;
  306. val_base = ARM_BASE_WVR;
  307. slots = this_cpu_ptr(wp_on_reg);
  308. max_slots = core_num_wrps;
  309. }
  310. for (i = 0; i < max_slots; ++i) {
  311. slot = &slots[i];
  312. if (!*slot) {
  313. *slot = bp;
  314. break;
  315. }
  316. }
  317. if (i == max_slots) {
  318. pr_warn("Can't find any breakpoint slot\n");
  319. return -EBUSY;
  320. }
  321. /* Override the breakpoint data with the step data. */
  322. if (info->step_ctrl.enabled) {
  323. addr = info->trigger & ~0x3;
  324. ctrl = encode_ctrl_reg(info->step_ctrl);
  325. if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) {
  326. i = 0;
  327. ctrl_base = ARM_BASE_BCR + core_num_brps;
  328. val_base = ARM_BASE_BVR + core_num_brps;
  329. }
  330. }
  331. /* Setup the address register. */
  332. write_wb_reg(val_base + i, addr);
  333. /* Setup the control register. */
  334. write_wb_reg(ctrl_base + i, ctrl);
  335. return 0;
  336. }
  337. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  338. {
  339. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  340. struct perf_event **slot, **slots;
  341. int i, max_slots, base;
  342. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  343. /* Breakpoint */
  344. base = ARM_BASE_BCR;
  345. slots = this_cpu_ptr(bp_on_reg);
  346. max_slots = core_num_brps;
  347. } else {
  348. /* Watchpoint */
  349. base = ARM_BASE_WCR;
  350. slots = this_cpu_ptr(wp_on_reg);
  351. max_slots = core_num_wrps;
  352. }
  353. /* Remove the breakpoint. */
  354. for (i = 0; i < max_slots; ++i) {
  355. slot = &slots[i];
  356. if (*slot == bp) {
  357. *slot = NULL;
  358. break;
  359. }
  360. }
  361. if (i == max_slots) {
  362. pr_warn("Can't find any breakpoint slot\n");
  363. return;
  364. }
  365. /* Ensure that we disable the mismatch breakpoint. */
  366. if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE &&
  367. info->step_ctrl.enabled) {
  368. i = 0;
  369. base = ARM_BASE_BCR + core_num_brps;
  370. }
  371. /* Reset the control register. */
  372. write_wb_reg(base + i, 0);
  373. }
  374. static int get_hbp_len(u8 hbp_len)
  375. {
  376. unsigned int len_in_bytes = 0;
  377. switch (hbp_len) {
  378. case ARM_BREAKPOINT_LEN_1:
  379. len_in_bytes = 1;
  380. break;
  381. case ARM_BREAKPOINT_LEN_2:
  382. len_in_bytes = 2;
  383. break;
  384. case ARM_BREAKPOINT_LEN_4:
  385. len_in_bytes = 4;
  386. break;
  387. case ARM_BREAKPOINT_LEN_8:
  388. len_in_bytes = 8;
  389. break;
  390. }
  391. return len_in_bytes;
  392. }
  393. /*
  394. * Check whether bp virtual address is in kernel space.
  395. */
  396. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  397. {
  398. unsigned int len;
  399. unsigned long va;
  400. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  401. va = info->address;
  402. len = get_hbp_len(info->ctrl.len);
  403. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  404. }
  405. /*
  406. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  407. * Hopefully this will disappear when ptrace can bypass the conversion
  408. * to generic breakpoint descriptions.
  409. */
  410. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  411. int *gen_len, int *gen_type)
  412. {
  413. /* Type */
  414. switch (ctrl.type) {
  415. case ARM_BREAKPOINT_EXECUTE:
  416. *gen_type = HW_BREAKPOINT_X;
  417. break;
  418. case ARM_BREAKPOINT_LOAD:
  419. *gen_type = HW_BREAKPOINT_R;
  420. break;
  421. case ARM_BREAKPOINT_STORE:
  422. *gen_type = HW_BREAKPOINT_W;
  423. break;
  424. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  425. *gen_type = HW_BREAKPOINT_RW;
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. /* Len */
  431. switch (ctrl.len) {
  432. case ARM_BREAKPOINT_LEN_1:
  433. *gen_len = HW_BREAKPOINT_LEN_1;
  434. break;
  435. case ARM_BREAKPOINT_LEN_2:
  436. *gen_len = HW_BREAKPOINT_LEN_2;
  437. break;
  438. case ARM_BREAKPOINT_LEN_4:
  439. *gen_len = HW_BREAKPOINT_LEN_4;
  440. break;
  441. case ARM_BREAKPOINT_LEN_8:
  442. *gen_len = HW_BREAKPOINT_LEN_8;
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. return 0;
  448. }
  449. /*
  450. * Construct an arch_hw_breakpoint from a perf_event.
  451. */
  452. static int arch_build_bp_info(struct perf_event *bp)
  453. {
  454. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  455. /* Type */
  456. switch (bp->attr.bp_type) {
  457. case HW_BREAKPOINT_X:
  458. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  459. break;
  460. case HW_BREAKPOINT_R:
  461. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  462. break;
  463. case HW_BREAKPOINT_W:
  464. info->ctrl.type = ARM_BREAKPOINT_STORE;
  465. break;
  466. case HW_BREAKPOINT_RW:
  467. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  468. break;
  469. default:
  470. return -EINVAL;
  471. }
  472. /* Len */
  473. switch (bp->attr.bp_len) {
  474. case HW_BREAKPOINT_LEN_1:
  475. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  476. break;
  477. case HW_BREAKPOINT_LEN_2:
  478. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  479. break;
  480. case HW_BREAKPOINT_LEN_4:
  481. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  482. break;
  483. case HW_BREAKPOINT_LEN_8:
  484. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  485. if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
  486. && max_watchpoint_len >= 8)
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. /*
  492. * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
  493. * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
  494. * by the hardware and must be aligned to the appropriate number of
  495. * bytes.
  496. */
  497. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  498. info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  499. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  500. return -EINVAL;
  501. /* Address */
  502. info->address = bp->attr.bp_addr;
  503. /* Privilege */
  504. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  505. if (arch_check_bp_in_kernelspace(bp))
  506. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  507. /* Enabled? */
  508. info->ctrl.enabled = !bp->attr.disabled;
  509. /* Mismatch */
  510. info->ctrl.mismatch = 0;
  511. return 0;
  512. }
  513. /*
  514. * Validate the arch-specific HW Breakpoint register settings.
  515. */
  516. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  517. {
  518. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  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);
  526. if (ret)
  527. goto out;
  528. /* Check address alignment. */
  529. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  530. alignment_mask = 0x7;
  531. offset = info->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 (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  540. break;
  541. case 3:
  542. /* Allow single byte watchpoint. */
  543. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  544. break;
  545. default:
  546. ret = -EINVAL;
  547. goto out;
  548. }
  549. info->address &= ~alignment_mask;
  550. info->ctrl.len <<= offset;
  551. if (!bp->overflow_handler) {
  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(bp))
  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. (info->ctrl.type == ARM_BREAKPOINT_LOAD ||
  573. info->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 (!wp->overflow_handler)
  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(void *unused)
  792. {
  793. int i, raw_num_brps, err = 0, cpu = smp_processor_id();
  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_notify(struct notifier_block *self,
  876. unsigned long action, void *cpu)
  877. {
  878. if ((action & ~CPU_TASKS_FROZEN) == CPU_ONLINE)
  879. smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
  880. return NOTIFY_OK;
  881. }
  882. static struct notifier_block dbg_reset_nb = {
  883. .notifier_call = dbg_reset_notify,
  884. };
  885. #ifdef CONFIG_CPU_PM
  886. static int dbg_cpu_pm_notify(struct notifier_block *self, unsigned long action,
  887. void *v)
  888. {
  889. if (action == CPU_PM_EXIT)
  890. reset_ctrl_regs(NULL);
  891. return NOTIFY_OK;
  892. }
  893. static struct notifier_block dbg_cpu_pm_nb = {
  894. .notifier_call = dbg_cpu_pm_notify,
  895. };
  896. static void __init pm_init(void)
  897. {
  898. cpu_pm_register_notifier(&dbg_cpu_pm_nb);
  899. }
  900. #else
  901. static inline void pm_init(void)
  902. {
  903. }
  904. #endif
  905. static int __init arch_hw_breakpoint_init(void)
  906. {
  907. debug_arch = get_debug_arch();
  908. if (!debug_arch_supported()) {
  909. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  910. return 0;
  911. }
  912. has_ossr = core_has_os_save_restore();
  913. /* Determine how many BRPs/WRPs are available. */
  914. core_num_brps = get_num_brps();
  915. core_num_wrps = get_num_wrps();
  916. cpu_notifier_register_begin();
  917. /*
  918. * We need to tread carefully here because DBGSWENABLE may be
  919. * driven low on this core and there isn't an architected way to
  920. * determine that.
  921. */
  922. register_undef_hook(&debug_reg_hook);
  923. /*
  924. * Reset the breakpoint resources. We assume that a halting
  925. * debugger will leave the world in a nice state for us.
  926. */
  927. on_each_cpu(reset_ctrl_regs, NULL, 1);
  928. unregister_undef_hook(&debug_reg_hook);
  929. if (!cpumask_empty(&debug_err_mask)) {
  930. core_num_brps = 0;
  931. core_num_wrps = 0;
  932. cpu_notifier_register_done();
  933. return 0;
  934. }
  935. pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
  936. core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
  937. "", core_num_wrps);
  938. /* Work out the maximum supported watchpoint length. */
  939. max_watchpoint_len = get_max_wp_len();
  940. pr_info("maximum watchpoint size is %u bytes.\n",
  941. max_watchpoint_len);
  942. /* Register debug fault handler. */
  943. hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  944. TRAP_HWBKPT, "watchpoint debug exception");
  945. hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  946. TRAP_HWBKPT, "breakpoint debug exception");
  947. /* Register hotplug and PM notifiers. */
  948. __register_cpu_notifier(&dbg_reset_nb);
  949. cpu_notifier_register_done();
  950. pm_init();
  951. return 0;
  952. }
  953. arch_initcall(arch_hw_breakpoint_init);
  954. void hw_breakpoint_pmu_read(struct perf_event *bp)
  955. {
  956. }
  957. /*
  958. * Dummy function to register with die_notifier.
  959. */
  960. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  961. unsigned long val, void *data)
  962. {
  963. return NOTIFY_DONE;
  964. }