hw_breakpoint.c 26 KB

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