hw_breakpoint.c 26 KB

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