hw_breakpoint.c 27 KB

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