hw_breakpoint.c 25 KB

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