hw_breakpoint.c 26 KB

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