hw_breakpoint.c 25 KB

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