hw_breakpoint.c 27 KB

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