hw_breakpoint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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/system.h>
  35. #include <asm/traps.h>
  36. /* Breakpoint currently in use for each BRP. */
  37. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
  38. /* Watchpoint currently in use for each WRP. */
  39. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
  40. /* Number of BRP/WRP registers on this CPU. */
  41. static int core_num_brps;
  42. static int core_num_reserved_brps;
  43. static int core_num_wrps;
  44. /* Debug architecture version. */
  45. static u8 debug_arch;
  46. /* Maximum supported watchpoint length. */
  47. static u8 max_watchpoint_len;
  48. #define READ_WB_REG_CASE(OP2, M, VAL) \
  49. case ((OP2 << 4) + M): \
  50. ARM_DBG_READ(c ## M, OP2, VAL); \
  51. break
  52. #define WRITE_WB_REG_CASE(OP2, M, VAL) \
  53. case ((OP2 << 4) + M): \
  54. ARM_DBG_WRITE(c ## M, OP2, VAL);\
  55. break
  56. #define GEN_READ_WB_REG_CASES(OP2, VAL) \
  57. READ_WB_REG_CASE(OP2, 0, VAL); \
  58. READ_WB_REG_CASE(OP2, 1, VAL); \
  59. READ_WB_REG_CASE(OP2, 2, VAL); \
  60. READ_WB_REG_CASE(OP2, 3, VAL); \
  61. READ_WB_REG_CASE(OP2, 4, VAL); \
  62. READ_WB_REG_CASE(OP2, 5, VAL); \
  63. READ_WB_REG_CASE(OP2, 6, VAL); \
  64. READ_WB_REG_CASE(OP2, 7, VAL); \
  65. READ_WB_REG_CASE(OP2, 8, VAL); \
  66. READ_WB_REG_CASE(OP2, 9, VAL); \
  67. READ_WB_REG_CASE(OP2, 10, VAL); \
  68. READ_WB_REG_CASE(OP2, 11, VAL); \
  69. READ_WB_REG_CASE(OP2, 12, VAL); \
  70. READ_WB_REG_CASE(OP2, 13, VAL); \
  71. READ_WB_REG_CASE(OP2, 14, VAL); \
  72. READ_WB_REG_CASE(OP2, 15, VAL)
  73. #define GEN_WRITE_WB_REG_CASES(OP2, VAL) \
  74. WRITE_WB_REG_CASE(OP2, 0, VAL); \
  75. WRITE_WB_REG_CASE(OP2, 1, VAL); \
  76. WRITE_WB_REG_CASE(OP2, 2, VAL); \
  77. WRITE_WB_REG_CASE(OP2, 3, VAL); \
  78. WRITE_WB_REG_CASE(OP2, 4, VAL); \
  79. WRITE_WB_REG_CASE(OP2, 5, VAL); \
  80. WRITE_WB_REG_CASE(OP2, 6, VAL); \
  81. WRITE_WB_REG_CASE(OP2, 7, VAL); \
  82. WRITE_WB_REG_CASE(OP2, 8, VAL); \
  83. WRITE_WB_REG_CASE(OP2, 9, VAL); \
  84. WRITE_WB_REG_CASE(OP2, 10, VAL); \
  85. WRITE_WB_REG_CASE(OP2, 11, VAL); \
  86. WRITE_WB_REG_CASE(OP2, 12, VAL); \
  87. WRITE_WB_REG_CASE(OP2, 13, VAL); \
  88. WRITE_WB_REG_CASE(OP2, 14, VAL); \
  89. WRITE_WB_REG_CASE(OP2, 15, VAL)
  90. static u32 read_wb_reg(int n)
  91. {
  92. u32 val = 0;
  93. switch (n) {
  94. GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
  95. GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
  96. GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
  97. GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
  98. default:
  99. pr_warning("attempt to read from unknown breakpoint "
  100. "register %d\n", n);
  101. }
  102. return val;
  103. }
  104. static void write_wb_reg(int n, u32 val)
  105. {
  106. switch (n) {
  107. GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
  108. GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
  109. GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
  110. GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
  111. default:
  112. pr_warning("attempt to write to unknown breakpoint "
  113. "register %d\n", n);
  114. }
  115. isb();
  116. }
  117. /* Determine debug architecture. */
  118. static u8 get_debug_arch(void)
  119. {
  120. u32 didr;
  121. /* Do we implement the extended CPUID interface? */
  122. if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
  123. pr_warning("CPUID feature registers not supported. "
  124. "Assuming v6 debug is present.\n");
  125. return ARM_DEBUG_ARCH_V6;
  126. }
  127. ARM_DBG_READ(c0, 0, didr);
  128. return (didr >> 16) & 0xf;
  129. }
  130. u8 arch_get_debug_arch(void)
  131. {
  132. return debug_arch;
  133. }
  134. /* Determine number of BRP register available. */
  135. static int get_num_brp_resources(void)
  136. {
  137. u32 didr;
  138. ARM_DBG_READ(c0, 0, didr);
  139. return ((didr >> 24) & 0xf) + 1;
  140. }
  141. /* Does this core support mismatch breakpoints? */
  142. static int core_has_mismatch_brps(void)
  143. {
  144. return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
  145. get_num_brp_resources() > 1);
  146. }
  147. /* Determine number of usable WRPs available. */
  148. static int get_num_wrps(void)
  149. {
  150. /*
  151. * FIXME: When a watchpoint fires, the only way to work out which
  152. * watchpoint it was is by disassembling the faulting instruction
  153. * and working out the address of the memory access.
  154. *
  155. * Furthermore, we can only do this if the watchpoint was precise
  156. * since imprecise watchpoints prevent us from calculating register
  157. * based addresses.
  158. *
  159. * Providing we have more than 1 breakpoint register, we only report
  160. * a single watchpoint register for the time being. This way, we always
  161. * know which watchpoint fired. In the future we can either add a
  162. * disassembler and address generation emulator, or we can insert a
  163. * check to see if the DFAR is set on watchpoint exception entry
  164. * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
  165. * that it is set on some implementations].
  166. */
  167. #if 0
  168. int wrps;
  169. u32 didr;
  170. ARM_DBG_READ(c0, 0, didr);
  171. wrps = ((didr >> 28) & 0xf) + 1;
  172. #endif
  173. int wrps = 1;
  174. if (core_has_mismatch_brps() && wrps >= get_num_brp_resources())
  175. wrps = get_num_brp_resources() - 1;
  176. return wrps;
  177. }
  178. /* We reserve one breakpoint for each watchpoint. */
  179. static int get_num_reserved_brps(void)
  180. {
  181. if (core_has_mismatch_brps())
  182. return get_num_wrps();
  183. return 0;
  184. }
  185. /* Determine number of usable BRPs available. */
  186. static int get_num_brps(void)
  187. {
  188. int brps = get_num_brp_resources();
  189. if (core_has_mismatch_brps())
  190. brps -= get_num_reserved_brps();
  191. return brps;
  192. }
  193. int hw_breakpoint_slots(int type)
  194. {
  195. /*
  196. * We can be called early, so don't rely on
  197. * our static variables being initialised.
  198. */
  199. switch (type) {
  200. case TYPE_INST:
  201. return get_num_brps();
  202. case TYPE_DATA:
  203. return get_num_wrps();
  204. default:
  205. pr_warning("unknown slot type: %d\n", type);
  206. return 0;
  207. }
  208. }
  209. /*
  210. * In order to access the breakpoint/watchpoint control registers,
  211. * we must be running in debug monitor mode. Unfortunately, we can
  212. * be put into halting debug mode at any time by an external debugger
  213. * but there is nothing we can do to prevent that.
  214. */
  215. static int enable_monitor_mode(void)
  216. {
  217. u32 dscr;
  218. int ret = 0;
  219. ARM_DBG_READ(c1, 0, dscr);
  220. /* Ensure that halting mode is disabled. */
  221. if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN, "halting debug mode enabled."
  222. "Unable to access hardware resources.")) {
  223. ret = -EPERM;
  224. goto out;
  225. }
  226. /* Write to the corresponding DSCR. */
  227. switch (debug_arch) {
  228. case ARM_DEBUG_ARCH_V6:
  229. case ARM_DEBUG_ARCH_V6_1:
  230. ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
  231. break;
  232. case ARM_DEBUG_ARCH_V7_ECP14:
  233. ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
  234. break;
  235. default:
  236. ret = -ENODEV;
  237. goto out;
  238. }
  239. /* Check that the write made it through. */
  240. ARM_DBG_READ(c1, 0, dscr);
  241. if (WARN_ONCE(!(dscr & ARM_DSCR_MDBGEN),
  242. "failed to enable monitor mode.")) {
  243. ret = -EPERM;
  244. }
  245. out:
  246. return ret;
  247. }
  248. /*
  249. * Check if 8-bit byte-address select is available.
  250. * This clobbers WRP 0.
  251. */
  252. static u8 get_max_wp_len(void)
  253. {
  254. u32 ctrl_reg;
  255. struct arch_hw_breakpoint_ctrl ctrl;
  256. u8 size = 4;
  257. if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
  258. goto out;
  259. if (enable_monitor_mode())
  260. goto out;
  261. memset(&ctrl, 0, sizeof(ctrl));
  262. ctrl.len = ARM_BREAKPOINT_LEN_8;
  263. ctrl_reg = encode_ctrl_reg(ctrl);
  264. write_wb_reg(ARM_BASE_WVR, 0);
  265. write_wb_reg(ARM_BASE_WCR, ctrl_reg);
  266. if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
  267. size = 8;
  268. out:
  269. return size;
  270. }
  271. u8 arch_get_max_wp_len(void)
  272. {
  273. return max_watchpoint_len;
  274. }
  275. /*
  276. * Install a perf counter breakpoint.
  277. */
  278. int arch_install_hw_breakpoint(struct perf_event *bp)
  279. {
  280. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  281. struct perf_event **slot, **slots;
  282. int i, max_slots, ctrl_base, val_base, ret = 0;
  283. u32 addr, ctrl;
  284. /* Ensure that we are in monitor mode and halting mode is disabled. */
  285. ret = enable_monitor_mode();
  286. if (ret)
  287. goto out;
  288. addr = info->address;
  289. ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
  290. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  291. /* Breakpoint */
  292. ctrl_base = ARM_BASE_BCR;
  293. val_base = ARM_BASE_BVR;
  294. slots = __get_cpu_var(bp_on_reg);
  295. max_slots = core_num_brps;
  296. if (info->step_ctrl.enabled) {
  297. /* Override the breakpoint data with the step data. */
  298. addr = info->trigger & ~0x3;
  299. ctrl = encode_ctrl_reg(info->step_ctrl);
  300. }
  301. } else {
  302. /* Watchpoint */
  303. if (info->step_ctrl.enabled) {
  304. /* Install into the reserved breakpoint region. */
  305. ctrl_base = ARM_BASE_BCR + core_num_brps;
  306. val_base = ARM_BASE_BVR + core_num_brps;
  307. /* Override the watchpoint data with the step data. */
  308. addr = info->trigger & ~0x3;
  309. ctrl = encode_ctrl_reg(info->step_ctrl);
  310. } else {
  311. ctrl_base = ARM_BASE_WCR;
  312. val_base = ARM_BASE_WVR;
  313. }
  314. slots = __get_cpu_var(wp_on_reg);
  315. max_slots = core_num_wrps;
  316. }
  317. for (i = 0; i < max_slots; ++i) {
  318. slot = &slots[i];
  319. if (!*slot) {
  320. *slot = bp;
  321. break;
  322. }
  323. }
  324. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot")) {
  325. ret = -EBUSY;
  326. goto out;
  327. }
  328. /* Setup the address register. */
  329. write_wb_reg(val_base + i, addr);
  330. /* Setup the control register. */
  331. write_wb_reg(ctrl_base + i, ctrl);
  332. out:
  333. return ret;
  334. }
  335. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  336. {
  337. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  338. struct perf_event **slot, **slots;
  339. int i, max_slots, base;
  340. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  341. /* Breakpoint */
  342. base = ARM_BASE_BCR;
  343. slots = __get_cpu_var(bp_on_reg);
  344. max_slots = core_num_brps;
  345. } else {
  346. /* Watchpoint */
  347. if (info->step_ctrl.enabled)
  348. base = ARM_BASE_BCR + core_num_brps;
  349. else
  350. base = ARM_BASE_WCR;
  351. slots = __get_cpu_var(wp_on_reg);
  352. max_slots = core_num_wrps;
  353. }
  354. /* Remove the breakpoint. */
  355. for (i = 0; i < max_slots; ++i) {
  356. slot = &slots[i];
  357. if (*slot == bp) {
  358. *slot = NULL;
  359. break;
  360. }
  361. }
  362. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot"))
  363. return;
  364. /* Reset the control register. */
  365. write_wb_reg(base + i, 0);
  366. }
  367. static int get_hbp_len(u8 hbp_len)
  368. {
  369. unsigned int len_in_bytes = 0;
  370. switch (hbp_len) {
  371. case ARM_BREAKPOINT_LEN_1:
  372. len_in_bytes = 1;
  373. break;
  374. case ARM_BREAKPOINT_LEN_2:
  375. len_in_bytes = 2;
  376. break;
  377. case ARM_BREAKPOINT_LEN_4:
  378. len_in_bytes = 4;
  379. break;
  380. case ARM_BREAKPOINT_LEN_8:
  381. len_in_bytes = 8;
  382. break;
  383. }
  384. return len_in_bytes;
  385. }
  386. /*
  387. * Check whether bp virtual address is in kernel space.
  388. */
  389. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  390. {
  391. unsigned int len;
  392. unsigned long va;
  393. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  394. va = info->address;
  395. len = get_hbp_len(info->ctrl.len);
  396. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  397. }
  398. /*
  399. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  400. * Hopefully this will disappear when ptrace can bypass the conversion
  401. * to generic breakpoint descriptions.
  402. */
  403. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  404. int *gen_len, int *gen_type)
  405. {
  406. /* Type */
  407. switch (ctrl.type) {
  408. case ARM_BREAKPOINT_EXECUTE:
  409. *gen_type = HW_BREAKPOINT_X;
  410. break;
  411. case ARM_BREAKPOINT_LOAD:
  412. *gen_type = HW_BREAKPOINT_R;
  413. break;
  414. case ARM_BREAKPOINT_STORE:
  415. *gen_type = HW_BREAKPOINT_W;
  416. break;
  417. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  418. *gen_type = HW_BREAKPOINT_RW;
  419. break;
  420. default:
  421. return -EINVAL;
  422. }
  423. /* Len */
  424. switch (ctrl.len) {
  425. case ARM_BREAKPOINT_LEN_1:
  426. *gen_len = HW_BREAKPOINT_LEN_1;
  427. break;
  428. case ARM_BREAKPOINT_LEN_2:
  429. *gen_len = HW_BREAKPOINT_LEN_2;
  430. break;
  431. case ARM_BREAKPOINT_LEN_4:
  432. *gen_len = HW_BREAKPOINT_LEN_4;
  433. break;
  434. case ARM_BREAKPOINT_LEN_8:
  435. *gen_len = HW_BREAKPOINT_LEN_8;
  436. break;
  437. default:
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. /*
  443. * Construct an arch_hw_breakpoint from a perf_event.
  444. */
  445. static int arch_build_bp_info(struct perf_event *bp)
  446. {
  447. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  448. /* Type */
  449. switch (bp->attr.bp_type) {
  450. case HW_BREAKPOINT_X:
  451. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  452. break;
  453. case HW_BREAKPOINT_R:
  454. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  455. break;
  456. case HW_BREAKPOINT_W:
  457. info->ctrl.type = ARM_BREAKPOINT_STORE;
  458. break;
  459. case HW_BREAKPOINT_RW:
  460. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. /* Len */
  466. switch (bp->attr.bp_len) {
  467. case HW_BREAKPOINT_LEN_1:
  468. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  469. break;
  470. case HW_BREAKPOINT_LEN_2:
  471. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  472. break;
  473. case HW_BREAKPOINT_LEN_4:
  474. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  475. break;
  476. case HW_BREAKPOINT_LEN_8:
  477. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  478. if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
  479. && max_watchpoint_len >= 8)
  480. break;
  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 (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  491. info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  492. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  493. return -EINVAL;
  494. /* Address */
  495. info->address = bp->attr.bp_addr;
  496. /* Privilege */
  497. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  498. if (arch_check_bp_in_kernelspace(bp))
  499. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  500. /* Enabled? */
  501. info->ctrl.enabled = !bp->attr.disabled;
  502. /* Mismatch */
  503. info->ctrl.mismatch = 0;
  504. return 0;
  505. }
  506. /*
  507. * Validate the arch-specific HW Breakpoint register settings.
  508. */
  509. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  510. {
  511. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  512. int ret = 0;
  513. u32 offset, alignment_mask = 0x3;
  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. /* Allow single byte watchpoint. */
  528. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  529. break;
  530. case 2:
  531. /* Allow halfword watchpoints and breakpoints. */
  532. if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  533. break;
  534. default:
  535. ret = -EINVAL;
  536. goto out;
  537. }
  538. info->address &= ~alignment_mask;
  539. info->ctrl.len <<= offset;
  540. /*
  541. * Currently we rely on an overflow handler to take
  542. * care of single-stepping the breakpoint when it fires.
  543. * In the case of userspace breakpoints on a core with V7 debug,
  544. * we can use the mismatch feature as a poor-man's hardware single-step.
  545. */
  546. if (WARN_ONCE(!bp->overflow_handler &&
  547. (arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_brps()),
  548. "overflow handler required but none found")) {
  549. ret = -EINVAL;
  550. }
  551. out:
  552. return ret;
  553. }
  554. /*
  555. * Enable/disable single-stepping over the breakpoint bp at address addr.
  556. */
  557. static void enable_single_step(struct perf_event *bp, u32 addr)
  558. {
  559. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  560. arch_uninstall_hw_breakpoint(bp);
  561. info->step_ctrl.mismatch = 1;
  562. info->step_ctrl.len = ARM_BREAKPOINT_LEN_4;
  563. info->step_ctrl.type = ARM_BREAKPOINT_EXECUTE;
  564. info->step_ctrl.privilege = info->ctrl.privilege;
  565. info->step_ctrl.enabled = 1;
  566. info->trigger = addr;
  567. arch_install_hw_breakpoint(bp);
  568. }
  569. static void disable_single_step(struct perf_event *bp)
  570. {
  571. arch_uninstall_hw_breakpoint(bp);
  572. counter_arch_bp(bp)->step_ctrl.enabled = 0;
  573. arch_install_hw_breakpoint(bp);
  574. }
  575. static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
  576. {
  577. int i;
  578. struct perf_event *wp, **slots = __get_cpu_var(wp_on_reg);
  579. struct arch_hw_breakpoint *info;
  580. /* Without a disassembler, we can only handle 1 watchpoint. */
  581. BUG_ON(core_num_wrps > 1);
  582. for (i = 0; i < core_num_wrps; ++i) {
  583. rcu_read_lock();
  584. wp = slots[i];
  585. if (wp == NULL) {
  586. rcu_read_unlock();
  587. continue;
  588. }
  589. /*
  590. * The DFAR is an unknown value. Since we only allow a
  591. * single watchpoint, we can set the trigger to the lowest
  592. * possible faulting address.
  593. */
  594. info = counter_arch_bp(wp);
  595. info->trigger = wp->attr.bp_addr;
  596. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  597. perf_bp_event(wp, regs);
  598. /*
  599. * If no overflow handler is present, insert a temporary
  600. * mismatch breakpoint so we can single-step over the
  601. * watchpoint trigger.
  602. */
  603. if (!wp->overflow_handler)
  604. enable_single_step(wp, instruction_pointer(regs));
  605. rcu_read_unlock();
  606. }
  607. }
  608. static void watchpoint_single_step_handler(unsigned long pc)
  609. {
  610. int i;
  611. struct perf_event *wp, **slots = __get_cpu_var(wp_on_reg);
  612. struct arch_hw_breakpoint *info;
  613. for (i = 0; i < core_num_reserved_brps; ++i) {
  614. rcu_read_lock();
  615. wp = slots[i];
  616. if (wp == NULL)
  617. goto unlock;
  618. info = counter_arch_bp(wp);
  619. if (!info->step_ctrl.enabled)
  620. goto unlock;
  621. /*
  622. * Restore the original watchpoint if we've completed the
  623. * single-step.
  624. */
  625. if (info->trigger != pc)
  626. disable_single_step(wp);
  627. unlock:
  628. rcu_read_unlock();
  629. }
  630. }
  631. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  632. {
  633. int i;
  634. u32 ctrl_reg, val, addr;
  635. struct perf_event *bp, **slots = __get_cpu_var(bp_on_reg);
  636. struct arch_hw_breakpoint *info;
  637. struct arch_hw_breakpoint_ctrl ctrl;
  638. /* The exception entry code places the amended lr in the PC. */
  639. addr = regs->ARM_pc;
  640. /* Check the currently installed breakpoints first. */
  641. for (i = 0; i < core_num_brps; ++i) {
  642. rcu_read_lock();
  643. bp = slots[i];
  644. if (bp == NULL)
  645. goto unlock;
  646. info = counter_arch_bp(bp);
  647. /* Check if the breakpoint value matches. */
  648. val = read_wb_reg(ARM_BASE_BVR + i);
  649. if (val != (addr & ~0x3))
  650. goto mismatch;
  651. /* Possible match, check the byte address select to confirm. */
  652. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  653. decode_ctrl_reg(ctrl_reg, &ctrl);
  654. if ((1 << (addr & 0x3)) & ctrl.len) {
  655. info->trigger = addr;
  656. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  657. perf_bp_event(bp, regs);
  658. if (!bp->overflow_handler)
  659. enable_single_step(bp, addr);
  660. goto unlock;
  661. }
  662. mismatch:
  663. /* If we're stepping a breakpoint, it can now be restored. */
  664. if (info->step_ctrl.enabled)
  665. disable_single_step(bp);
  666. unlock:
  667. rcu_read_unlock();
  668. }
  669. /* Handle any pending watchpoint single-step breakpoints. */
  670. watchpoint_single_step_handler(addr);
  671. }
  672. /*
  673. * Called from either the Data Abort Handler [watchpoint] or the
  674. * Prefetch Abort Handler [breakpoint] with preemption disabled.
  675. */
  676. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  677. struct pt_regs *regs)
  678. {
  679. int ret = 0;
  680. u32 dscr;
  681. /* We must be called with preemption disabled. */
  682. WARN_ON(preemptible());
  683. /* We only handle watchpoints and hardware breakpoints. */
  684. ARM_DBG_READ(c1, 0, dscr);
  685. /* Perform perf callbacks. */
  686. switch (ARM_DSCR_MOE(dscr)) {
  687. case ARM_ENTRY_BREAKPOINT:
  688. breakpoint_handler(addr, regs);
  689. break;
  690. case ARM_ENTRY_ASYNC_WATCHPOINT:
  691. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  692. case ARM_ENTRY_SYNC_WATCHPOINT:
  693. watchpoint_handler(addr, regs);
  694. break;
  695. default:
  696. ret = 1; /* Unhandled fault. */
  697. }
  698. /*
  699. * Re-enable preemption after it was disabled in the
  700. * low-level exception handling code.
  701. */
  702. preempt_enable();
  703. return ret;
  704. }
  705. /*
  706. * One-time initialisation.
  707. */
  708. static void reset_ctrl_regs(void *unused)
  709. {
  710. int i;
  711. /*
  712. * v7 debug contains save and restore registers so that debug state
  713. * can be maintained across low-power modes without leaving
  714. * the debug logic powered up. It is IMPLEMENTATION DEFINED whether
  715. * we can write to the debug registers out of reset, so we must
  716. * unlock the OS Lock Access Register to avoid taking undefined
  717. * instruction exceptions later on.
  718. */
  719. if (debug_arch >= ARM_DEBUG_ARCH_V7_ECP14) {
  720. /*
  721. * Unconditionally clear the lock by writing a value
  722. * other than 0xC5ACCE55 to the access register.
  723. */
  724. asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
  725. isb();
  726. }
  727. if (enable_monitor_mode())
  728. return;
  729. /* We must also reset any reserved registers. */
  730. for (i = 0; i < core_num_brps + core_num_reserved_brps; ++i) {
  731. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  732. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  733. }
  734. for (i = 0; i < core_num_wrps; ++i) {
  735. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  736. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  737. }
  738. }
  739. static int __cpuinit dbg_reset_notify(struct notifier_block *self,
  740. unsigned long action, void *cpu)
  741. {
  742. if (action == CPU_ONLINE)
  743. smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
  744. return NOTIFY_OK;
  745. }
  746. static struct notifier_block __cpuinitdata dbg_reset_nb = {
  747. .notifier_call = dbg_reset_notify,
  748. };
  749. static int __init arch_hw_breakpoint_init(void)
  750. {
  751. int ret = 0;
  752. u32 dscr;
  753. debug_arch = get_debug_arch();
  754. if (debug_arch > ARM_DEBUG_ARCH_V7_ECP14) {
  755. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  756. ret = -ENODEV;
  757. goto out;
  758. }
  759. /* Determine how many BRPs/WRPs are available. */
  760. core_num_brps = get_num_brps();
  761. core_num_reserved_brps = get_num_reserved_brps();
  762. core_num_wrps = get_num_wrps();
  763. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  764. core_num_brps + core_num_reserved_brps, core_num_wrps);
  765. if (core_num_reserved_brps)
  766. pr_info("%d breakpoint(s) reserved for watchpoint "
  767. "single-step.\n", core_num_reserved_brps);
  768. ARM_DBG_READ(c1, 0, dscr);
  769. if (dscr & ARM_DSCR_HDBGEN) {
  770. pr_warning("halting debug mode enabled. Assuming maximum "
  771. "watchpoint size of 4 bytes.");
  772. } else {
  773. /*
  774. * Reset the breakpoint resources. We assume that a halting
  775. * debugger will leave the world in a nice state for us.
  776. */
  777. smp_call_function(reset_ctrl_regs, NULL, 1);
  778. reset_ctrl_regs(NULL);
  779. /* Work out the maximum supported watchpoint length. */
  780. max_watchpoint_len = get_max_wp_len();
  781. pr_info("maximum watchpoint size is %u bytes.\n",
  782. max_watchpoint_len);
  783. }
  784. /* Register debug fault handler. */
  785. hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  786. "watchpoint debug exception");
  787. hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  788. "breakpoint debug exception");
  789. /* Register hotplug notifier. */
  790. register_cpu_notifier(&dbg_reset_nb);
  791. out:
  792. return ret;
  793. }
  794. arch_initcall(arch_hw_breakpoint_init);
  795. void hw_breakpoint_pmu_read(struct perf_event *bp)
  796. {
  797. }
  798. /*
  799. * Dummy function to register with die_notifier.
  800. */
  801. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  802. unsigned long val, void *data)
  803. {
  804. return NOTIFY_DONE;
  805. }