hw_breakpoint.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 (WARN_ONCE((((read_cpuid_id() >> 16) & 0xf) != 0xf),
  123. "CPUID feature registers not supported. "
  124. "Assuming v6 debug is present.\n"))
  125. return ARM_DEBUG_ARCH_V6;
  126. ARM_DBG_READ(c0, 0, didr);
  127. return (didr >> 16) & 0xf;
  128. }
  129. u8 arch_get_debug_arch(void)
  130. {
  131. return debug_arch;
  132. }
  133. static int debug_arch_supported(void)
  134. {
  135. u8 arch = get_debug_arch();
  136. return arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14;
  137. }
  138. /* Determine number of BRP register available. */
  139. static int get_num_brp_resources(void)
  140. {
  141. u32 didr;
  142. ARM_DBG_READ(c0, 0, didr);
  143. return ((didr >> 24) & 0xf) + 1;
  144. }
  145. /* Does this core support mismatch breakpoints? */
  146. static int core_has_mismatch_brps(void)
  147. {
  148. return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
  149. get_num_brp_resources() > 1);
  150. }
  151. /* Determine number of usable WRPs available. */
  152. static int get_num_wrps(void)
  153. {
  154. /*
  155. * FIXME: When a watchpoint fires, the only way to work out which
  156. * watchpoint it was is by disassembling the faulting instruction
  157. * and working out the address of the memory access.
  158. *
  159. * Furthermore, we can only do this if the watchpoint was precise
  160. * since imprecise watchpoints prevent us from calculating register
  161. * based addresses.
  162. *
  163. * Providing we have more than 1 breakpoint register, we only report
  164. * a single watchpoint register for the time being. This way, we always
  165. * know which watchpoint fired. In the future we can either add a
  166. * disassembler and address generation emulator, or we can insert a
  167. * check to see if the DFAR is set on watchpoint exception entry
  168. * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
  169. * that it is set on some implementations].
  170. */
  171. #if 0
  172. int wrps;
  173. u32 didr;
  174. ARM_DBG_READ(c0, 0, didr);
  175. wrps = ((didr >> 28) & 0xf) + 1;
  176. #endif
  177. int wrps = 1;
  178. if (core_has_mismatch_brps() && wrps >= get_num_brp_resources())
  179. wrps = get_num_brp_resources() - 1;
  180. return wrps;
  181. }
  182. /* We reserve one breakpoint for each watchpoint. */
  183. static int get_num_reserved_brps(void)
  184. {
  185. if (core_has_mismatch_brps())
  186. return get_num_wrps();
  187. return 0;
  188. }
  189. /* Determine number of usable BRPs available. */
  190. static int get_num_brps(void)
  191. {
  192. int brps = get_num_brp_resources();
  193. if (core_has_mismatch_brps())
  194. brps -= get_num_reserved_brps();
  195. return brps;
  196. }
  197. /*
  198. * In order to access the breakpoint/watchpoint control registers,
  199. * we must be running in debug monitor mode. Unfortunately, we can
  200. * be put into halting debug mode at any time by an external debugger
  201. * but there is nothing we can do to prevent that.
  202. */
  203. static int enable_monitor_mode(void)
  204. {
  205. u32 dscr;
  206. int ret = 0;
  207. ARM_DBG_READ(c1, 0, dscr);
  208. /* Ensure that halting mode is disabled. */
  209. if (WARN_ONCE(dscr & ARM_DSCR_HDBGEN,
  210. "halting debug mode enabled. Unable to access hardware resources.\n")) {
  211. ret = -EPERM;
  212. goto out;
  213. }
  214. /* If monitor mode is already enabled, just return. */
  215. if (dscr & ARM_DSCR_MDBGEN)
  216. goto out;
  217. /* Write to the corresponding DSCR. */
  218. switch (get_debug_arch()) {
  219. case ARM_DEBUG_ARCH_V6:
  220. case ARM_DEBUG_ARCH_V6_1:
  221. ARM_DBG_WRITE(c1, 0, (dscr | ARM_DSCR_MDBGEN));
  222. break;
  223. case ARM_DEBUG_ARCH_V7_ECP14:
  224. ARM_DBG_WRITE(c2, 2, (dscr | ARM_DSCR_MDBGEN));
  225. break;
  226. default:
  227. ret = -ENODEV;
  228. goto out;
  229. }
  230. /* Check that the write made it through. */
  231. ARM_DBG_READ(c1, 0, dscr);
  232. if (!(dscr & ARM_DSCR_MDBGEN))
  233. ret = -EPERM;
  234. out:
  235. return ret;
  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, ret = 0;
  288. u32 addr, ctrl;
  289. /* Ensure that we are in monitor mode and halting mode is disabled. */
  290. ret = enable_monitor_mode();
  291. if (ret)
  292. goto out;
  293. addr = info->address;
  294. ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
  295. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  296. /* Breakpoint */
  297. ctrl_base = ARM_BASE_BCR;
  298. val_base = ARM_BASE_BVR;
  299. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  300. max_slots = core_num_brps;
  301. if (info->step_ctrl.enabled) {
  302. /* Override the breakpoint data with the step data. */
  303. addr = info->trigger & ~0x3;
  304. ctrl = encode_ctrl_reg(info->step_ctrl);
  305. }
  306. } else {
  307. /* Watchpoint */
  308. if (info->step_ctrl.enabled) {
  309. /* Install into the reserved breakpoint region. */
  310. ctrl_base = ARM_BASE_BCR + core_num_brps;
  311. val_base = ARM_BASE_BVR + core_num_brps;
  312. /* Override the watchpoint data with the step data. */
  313. addr = info->trigger & ~0x3;
  314. ctrl = encode_ctrl_reg(info->step_ctrl);
  315. } else {
  316. ctrl_base = ARM_BASE_WCR;
  317. val_base = ARM_BASE_WVR;
  318. }
  319. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  320. max_slots = core_num_wrps;
  321. }
  322. for (i = 0; i < max_slots; ++i) {
  323. slot = &slots[i];
  324. if (!*slot) {
  325. *slot = bp;
  326. break;
  327. }
  328. }
  329. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n")) {
  330. ret = -EBUSY;
  331. goto out;
  332. }
  333. /* Setup the address register. */
  334. write_wb_reg(val_base + i, addr);
  335. /* Setup the control register. */
  336. write_wb_reg(ctrl_base + i, ctrl);
  337. out:
  338. return ret;
  339. }
  340. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  341. {
  342. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  343. struct perf_event **slot, **slots;
  344. int i, max_slots, base;
  345. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
  346. /* Breakpoint */
  347. base = ARM_BASE_BCR;
  348. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  349. max_slots = core_num_brps;
  350. } else {
  351. /* Watchpoint */
  352. if (info->step_ctrl.enabled)
  353. base = ARM_BASE_BCR + core_num_brps;
  354. else
  355. base = ARM_BASE_WCR;
  356. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  357. max_slots = core_num_wrps;
  358. }
  359. /* Remove the breakpoint. */
  360. for (i = 0; i < max_slots; ++i) {
  361. slot = &slots[i];
  362. if (*slot == bp) {
  363. *slot = NULL;
  364. break;
  365. }
  366. }
  367. if (WARN_ONCE(i == max_slots, "Can't find any breakpoint slot\n"))
  368. return;
  369. /* Reset the control register. */
  370. write_wb_reg(base + i, 0);
  371. }
  372. static int get_hbp_len(u8 hbp_len)
  373. {
  374. unsigned int len_in_bytes = 0;
  375. switch (hbp_len) {
  376. case ARM_BREAKPOINT_LEN_1:
  377. len_in_bytes = 1;
  378. break;
  379. case ARM_BREAKPOINT_LEN_2:
  380. len_in_bytes = 2;
  381. break;
  382. case ARM_BREAKPOINT_LEN_4:
  383. len_in_bytes = 4;
  384. break;
  385. case ARM_BREAKPOINT_LEN_8:
  386. len_in_bytes = 8;
  387. break;
  388. }
  389. return len_in_bytes;
  390. }
  391. /*
  392. * Check whether bp virtual address is in kernel space.
  393. */
  394. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  395. {
  396. unsigned int len;
  397. unsigned long va;
  398. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  399. va = info->address;
  400. len = get_hbp_len(info->ctrl.len);
  401. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  402. }
  403. /*
  404. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  405. * Hopefully this will disappear when ptrace can bypass the conversion
  406. * to generic breakpoint descriptions.
  407. */
  408. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  409. int *gen_len, int *gen_type)
  410. {
  411. /* Type */
  412. switch (ctrl.type) {
  413. case ARM_BREAKPOINT_EXECUTE:
  414. *gen_type = HW_BREAKPOINT_X;
  415. break;
  416. case ARM_BREAKPOINT_LOAD:
  417. *gen_type = HW_BREAKPOINT_R;
  418. break;
  419. case ARM_BREAKPOINT_STORE:
  420. *gen_type = HW_BREAKPOINT_W;
  421. break;
  422. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  423. *gen_type = HW_BREAKPOINT_RW;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. /* Len */
  429. switch (ctrl.len) {
  430. case ARM_BREAKPOINT_LEN_1:
  431. *gen_len = HW_BREAKPOINT_LEN_1;
  432. break;
  433. case ARM_BREAKPOINT_LEN_2:
  434. *gen_len = HW_BREAKPOINT_LEN_2;
  435. break;
  436. case ARM_BREAKPOINT_LEN_4:
  437. *gen_len = HW_BREAKPOINT_LEN_4;
  438. break;
  439. case ARM_BREAKPOINT_LEN_8:
  440. *gen_len = HW_BREAKPOINT_LEN_8;
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. return 0;
  446. }
  447. /*
  448. * Construct an arch_hw_breakpoint from a perf_event.
  449. */
  450. static int arch_build_bp_info(struct perf_event *bp)
  451. {
  452. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  453. /* Type */
  454. switch (bp->attr.bp_type) {
  455. case HW_BREAKPOINT_X:
  456. info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  457. break;
  458. case HW_BREAKPOINT_R:
  459. info->ctrl.type = ARM_BREAKPOINT_LOAD;
  460. break;
  461. case HW_BREAKPOINT_W:
  462. info->ctrl.type = ARM_BREAKPOINT_STORE;
  463. break;
  464. case HW_BREAKPOINT_RW:
  465. info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  466. break;
  467. default:
  468. return -EINVAL;
  469. }
  470. /* Len */
  471. switch (bp->attr.bp_len) {
  472. case HW_BREAKPOINT_LEN_1:
  473. info->ctrl.len = ARM_BREAKPOINT_LEN_1;
  474. break;
  475. case HW_BREAKPOINT_LEN_2:
  476. info->ctrl.len = ARM_BREAKPOINT_LEN_2;
  477. break;
  478. case HW_BREAKPOINT_LEN_4:
  479. info->ctrl.len = ARM_BREAKPOINT_LEN_4;
  480. break;
  481. case HW_BREAKPOINT_LEN_8:
  482. info->ctrl.len = ARM_BREAKPOINT_LEN_8;
  483. if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
  484. && max_watchpoint_len >= 8)
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. /*
  490. * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
  491. * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
  492. * by the hardware and must be aligned to the appropriate number of
  493. * bytes.
  494. */
  495. if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  496. info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  497. info->ctrl.len != ARM_BREAKPOINT_LEN_4)
  498. return -EINVAL;
  499. /* Address */
  500. info->address = bp->attr.bp_addr;
  501. /* Privilege */
  502. info->ctrl.privilege = ARM_BREAKPOINT_USER;
  503. if (arch_check_bp_in_kernelspace(bp))
  504. info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  505. /* Enabled? */
  506. info->ctrl.enabled = !bp->attr.disabled;
  507. /* Mismatch */
  508. info->ctrl.mismatch = 0;
  509. return 0;
  510. }
  511. /*
  512. * Validate the arch-specific HW Breakpoint register settings.
  513. */
  514. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  515. {
  516. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  517. int ret = 0;
  518. u32 offset, alignment_mask = 0x3;
  519. /* Build the arch_hw_breakpoint. */
  520. ret = arch_build_bp_info(bp);
  521. if (ret)
  522. goto out;
  523. /* Check address alignment. */
  524. if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
  525. alignment_mask = 0x7;
  526. offset = info->address & alignment_mask;
  527. switch (offset) {
  528. case 0:
  529. /* Aligned */
  530. break;
  531. case 1:
  532. /* Allow single byte watchpoint. */
  533. if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
  534. break;
  535. case 2:
  536. /* Allow halfword watchpoints and breakpoints. */
  537. if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
  538. break;
  539. default:
  540. ret = -EINVAL;
  541. goto out;
  542. }
  543. info->address &= ~alignment_mask;
  544. info->ctrl.len <<= offset;
  545. /*
  546. * Currently we rely on an overflow handler to take
  547. * care of single-stepping the breakpoint when it fires.
  548. * In the case of userspace breakpoints on a core with V7 debug,
  549. * we can use the mismatch feature as a poor-man's hardware
  550. * single-step, but this only works for per-task breakpoints.
  551. */
  552. if (WARN_ONCE(!bp->overflow_handler &&
  553. (arch_check_bp_in_kernelspace(bp) || !core_has_mismatch_brps()
  554. || !bp->hw.bp_target),
  555. "overflow handler required but none found\n")) {
  556. ret = -EINVAL;
  557. }
  558. out:
  559. return ret;
  560. }
  561. /*
  562. * Enable/disable single-stepping over the breakpoint bp at address addr.
  563. */
  564. static void enable_single_step(struct perf_event *bp, u32 addr)
  565. {
  566. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  567. arch_uninstall_hw_breakpoint(bp);
  568. info->step_ctrl.mismatch = 1;
  569. info->step_ctrl.len = ARM_BREAKPOINT_LEN_4;
  570. info->step_ctrl.type = ARM_BREAKPOINT_EXECUTE;
  571. info->step_ctrl.privilege = info->ctrl.privilege;
  572. info->step_ctrl.enabled = 1;
  573. info->trigger = addr;
  574. arch_install_hw_breakpoint(bp);
  575. }
  576. static void disable_single_step(struct perf_event *bp)
  577. {
  578. arch_uninstall_hw_breakpoint(bp);
  579. counter_arch_bp(bp)->step_ctrl.enabled = 0;
  580. arch_install_hw_breakpoint(bp);
  581. }
  582. static void watchpoint_handler(unsigned long unknown, struct pt_regs *regs)
  583. {
  584. int i;
  585. struct perf_event *wp, **slots;
  586. struct arch_hw_breakpoint *info;
  587. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  588. /* Without a disassembler, we can only handle 1 watchpoint. */
  589. BUG_ON(core_num_wrps > 1);
  590. for (i = 0; i < core_num_wrps; ++i) {
  591. rcu_read_lock();
  592. wp = slots[i];
  593. if (wp == NULL) {
  594. rcu_read_unlock();
  595. continue;
  596. }
  597. /*
  598. * The DFAR is an unknown value. Since we only allow a
  599. * single watchpoint, we can set the trigger to the lowest
  600. * possible faulting address.
  601. */
  602. info = counter_arch_bp(wp);
  603. info->trigger = wp->attr.bp_addr;
  604. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  605. perf_bp_event(wp, regs);
  606. /*
  607. * If no overflow handler is present, insert a temporary
  608. * mismatch breakpoint so we can single-step over the
  609. * watchpoint trigger.
  610. */
  611. if (!wp->overflow_handler)
  612. enable_single_step(wp, instruction_pointer(regs));
  613. rcu_read_unlock();
  614. }
  615. }
  616. static void watchpoint_single_step_handler(unsigned long pc)
  617. {
  618. int i;
  619. struct perf_event *wp, **slots;
  620. struct arch_hw_breakpoint *info;
  621. slots = (struct perf_event **)__get_cpu_var(wp_on_reg);
  622. for (i = 0; i < core_num_reserved_brps; ++i) {
  623. rcu_read_lock();
  624. wp = slots[i];
  625. if (wp == NULL)
  626. goto unlock;
  627. info = counter_arch_bp(wp);
  628. if (!info->step_ctrl.enabled)
  629. goto unlock;
  630. /*
  631. * Restore the original watchpoint if we've completed the
  632. * single-step.
  633. */
  634. if (info->trigger != pc)
  635. disable_single_step(wp);
  636. unlock:
  637. rcu_read_unlock();
  638. }
  639. }
  640. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  641. {
  642. int i;
  643. u32 ctrl_reg, val, addr;
  644. struct perf_event *bp, **slots;
  645. struct arch_hw_breakpoint *info;
  646. struct arch_hw_breakpoint_ctrl ctrl;
  647. slots = (struct perf_event **)__get_cpu_var(bp_on_reg);
  648. /* The exception entry code places the amended lr in the PC. */
  649. addr = regs->ARM_pc;
  650. /* Check the currently installed breakpoints first. */
  651. for (i = 0; i < core_num_brps; ++i) {
  652. rcu_read_lock();
  653. bp = slots[i];
  654. if (bp == NULL)
  655. goto unlock;
  656. info = counter_arch_bp(bp);
  657. /* Check if the breakpoint value matches. */
  658. val = read_wb_reg(ARM_BASE_BVR + i);
  659. if (val != (addr & ~0x3))
  660. goto mismatch;
  661. /* Possible match, check the byte address select to confirm. */
  662. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  663. decode_ctrl_reg(ctrl_reg, &ctrl);
  664. if ((1 << (addr & 0x3)) & ctrl.len) {
  665. info->trigger = addr;
  666. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  667. perf_bp_event(bp, regs);
  668. if (!bp->overflow_handler)
  669. enable_single_step(bp, addr);
  670. goto unlock;
  671. }
  672. mismatch:
  673. /* If we're stepping a breakpoint, it can now be restored. */
  674. if (info->step_ctrl.enabled)
  675. disable_single_step(bp);
  676. unlock:
  677. rcu_read_unlock();
  678. }
  679. /* Handle any pending watchpoint single-step breakpoints. */
  680. watchpoint_single_step_handler(addr);
  681. }
  682. /*
  683. * Called from either the Data Abort Handler [watchpoint] or the
  684. * Prefetch Abort Handler [breakpoint] with preemption disabled.
  685. */
  686. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  687. struct pt_regs *regs)
  688. {
  689. int ret = 0;
  690. u32 dscr;
  691. /* We must be called with preemption disabled. */
  692. WARN_ON(preemptible());
  693. /* We only handle watchpoints and hardware breakpoints. */
  694. ARM_DBG_READ(c1, 0, dscr);
  695. /* Perform perf callbacks. */
  696. switch (ARM_DSCR_MOE(dscr)) {
  697. case ARM_ENTRY_BREAKPOINT:
  698. breakpoint_handler(addr, regs);
  699. break;
  700. case ARM_ENTRY_ASYNC_WATCHPOINT:
  701. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  702. case ARM_ENTRY_SYNC_WATCHPOINT:
  703. watchpoint_handler(addr, regs);
  704. break;
  705. default:
  706. ret = 1; /* Unhandled fault. */
  707. }
  708. /*
  709. * Re-enable preemption after it was disabled in the
  710. * low-level exception handling code.
  711. */
  712. preempt_enable();
  713. return ret;
  714. }
  715. /*
  716. * One-time initialisation.
  717. */
  718. static void reset_ctrl_regs(void *info)
  719. {
  720. int i, cpu = smp_processor_id();
  721. u32 dbg_power;
  722. cpumask_t *cpumask = info;
  723. /*
  724. * v7 debug contains save and restore registers so that debug state
  725. * can be maintained across low-power modes without leaving the debug
  726. * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
  727. * the debug registers out of reset, so we must unlock the OS Lock
  728. * Access Register to avoid taking undefined instruction exceptions
  729. * later on.
  730. */
  731. if (debug_arch >= ARM_DEBUG_ARCH_V7_ECP14) {
  732. /*
  733. * Ensure sticky power-down is clear (i.e. debug logic is
  734. * powered up).
  735. */
  736. asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (dbg_power));
  737. if ((dbg_power & 0x1) == 0) {
  738. pr_warning("CPU %d debug is powered down!\n", cpu);
  739. cpumask_or(cpumask, cpumask, cpumask_of(cpu));
  740. return;
  741. }
  742. /*
  743. * Unconditionally clear the lock by writing a value
  744. * other than 0xC5ACCE55 to the access register.
  745. */
  746. asm volatile("mcr p14, 0, %0, c1, c0, 4" : : "r" (0));
  747. isb();
  748. /*
  749. * Clear any configured vector-catch events before
  750. * enabling monitor mode.
  751. */
  752. asm volatile("mcr p14, 0, %0, c0, c7, 0" : : "r" (0));
  753. isb();
  754. }
  755. if (enable_monitor_mode())
  756. return;
  757. /* We must also reset any reserved registers. */
  758. for (i = 0; i < core_num_brps + core_num_reserved_brps; ++i) {
  759. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  760. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  761. }
  762. for (i = 0; i < core_num_wrps; ++i) {
  763. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  764. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  765. }
  766. }
  767. static int __cpuinit dbg_reset_notify(struct notifier_block *self,
  768. unsigned long action, void *cpu)
  769. {
  770. if (action == CPU_ONLINE)
  771. smp_call_function_single((int)cpu, reset_ctrl_regs, NULL, 1);
  772. return NOTIFY_OK;
  773. }
  774. static struct notifier_block __cpuinitdata dbg_reset_nb = {
  775. .notifier_call = dbg_reset_notify,
  776. };
  777. static int __init arch_hw_breakpoint_init(void)
  778. {
  779. u32 dscr;
  780. cpumask_t cpumask = { CPU_BITS_NONE };
  781. debug_arch = get_debug_arch();
  782. if (!debug_arch_supported()) {
  783. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  784. return 0;
  785. }
  786. /* Determine how many BRPs/WRPs are available. */
  787. core_num_brps = get_num_brps();
  788. core_num_reserved_brps = get_num_reserved_brps();
  789. core_num_wrps = get_num_wrps();
  790. pr_info("found %d breakpoint and %d watchpoint registers.\n",
  791. core_num_brps + core_num_reserved_brps, core_num_wrps);
  792. if (core_num_reserved_brps)
  793. pr_info("%d breakpoint(s) reserved for watchpoint "
  794. "single-step.\n", core_num_reserved_brps);
  795. /*
  796. * Reset the breakpoint resources. We assume that a halting
  797. * debugger will leave the world in a nice state for us.
  798. */
  799. on_each_cpu(reset_ctrl_regs, &cpumask, 1);
  800. if (!cpumask_empty(&cpumask)) {
  801. core_num_brps = 0;
  802. core_num_reserved_brps = 0;
  803. core_num_wrps = 0;
  804. return 0;
  805. }
  806. ARM_DBG_READ(c1, 0, dscr);
  807. if (dscr & ARM_DSCR_HDBGEN) {
  808. max_watchpoint_len = 4;
  809. pr_warning("halting debug mode enabled. Assuming maximum watchpoint size of %u bytes.\n",
  810. max_watchpoint_len);
  811. } else {
  812. /* Work out the maximum supported watchpoint length. */
  813. max_watchpoint_len = get_max_wp_len();
  814. pr_info("maximum watchpoint size is %u bytes.\n",
  815. max_watchpoint_len);
  816. }
  817. /* Register debug fault handler. */
  818. hook_fault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  819. "watchpoint debug exception");
  820. hook_ifault_code(2, hw_breakpoint_pending, SIGTRAP, TRAP_HWBKPT,
  821. "breakpoint debug exception");
  822. /* Register hotplug notifier. */
  823. register_cpu_notifier(&dbg_reset_nb);
  824. return 0;
  825. }
  826. arch_initcall(arch_hw_breakpoint_init);
  827. void hw_breakpoint_pmu_read(struct perf_event *bp)
  828. {
  829. }
  830. /*
  831. * Dummy function to register with die_notifier.
  832. */
  833. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  834. unsigned long val, void *data)
  835. {
  836. return NOTIFY_DONE;
  837. }