hw_breakpoint.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 <linux/coresight.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/cputype.h>
  33. #include <asm/current.h>
  34. #include <asm/hw_breakpoint.h>
  35. #include <asm/traps.h>
  36. /* Breakpoint currently in use for each BRP. */
  37. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
  38. /* Watchpoint currently in use for each WRP. */
  39. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
  40. /* Number of BRP/WRP registers on this CPU. */
  41. static int core_num_brps __ro_after_init;
  42. static int core_num_wrps __ro_after_init;
  43. /* Debug architecture version. */
  44. static u8 debug_arch __ro_after_init;
  45. /* Does debug architecture support OS Save and Restore? */
  46. static bool has_ossr __ro_after_init;
  47. /* Maximum supported watchpoint length. */
  48. static u8 max_watchpoint_len __ro_after_init;
  49. #define READ_WB_REG_CASE(OP2, M, VAL) \
  50. case ((OP2 << 4) + M): \
  51. ARM_DBG_READ(c0, c ## M, OP2, VAL); \
  52. break
  53. #define WRITE_WB_REG_CASE(OP2, M, VAL) \
  54. case ((OP2 << 4) + M): \
  55. ARM_DBG_WRITE(c0, c ## M, OP2, VAL); \
  56. break
  57. #define GEN_READ_WB_REG_CASES(OP2, VAL) \
  58. READ_WB_REG_CASE(OP2, 0, VAL); \
  59. READ_WB_REG_CASE(OP2, 1, VAL); \
  60. READ_WB_REG_CASE(OP2, 2, VAL); \
  61. READ_WB_REG_CASE(OP2, 3, VAL); \
  62. READ_WB_REG_CASE(OP2, 4, VAL); \
  63. READ_WB_REG_CASE(OP2, 5, VAL); \
  64. READ_WB_REG_CASE(OP2, 6, VAL); \
  65. READ_WB_REG_CASE(OP2, 7, VAL); \
  66. READ_WB_REG_CASE(OP2, 8, VAL); \
  67. READ_WB_REG_CASE(OP2, 9, VAL); \
  68. READ_WB_REG_CASE(OP2, 10, VAL); \
  69. READ_WB_REG_CASE(OP2, 11, VAL); \
  70. READ_WB_REG_CASE(OP2, 12, VAL); \
  71. READ_WB_REG_CASE(OP2, 13, VAL); \
  72. READ_WB_REG_CASE(OP2, 14, VAL); \
  73. READ_WB_REG_CASE(OP2, 15, VAL)
  74. #define GEN_WRITE_WB_REG_CASES(OP2, VAL) \
  75. WRITE_WB_REG_CASE(OP2, 0, VAL); \
  76. WRITE_WB_REG_CASE(OP2, 1, VAL); \
  77. WRITE_WB_REG_CASE(OP2, 2, VAL); \
  78. WRITE_WB_REG_CASE(OP2, 3, VAL); \
  79. WRITE_WB_REG_CASE(OP2, 4, VAL); \
  80. WRITE_WB_REG_CASE(OP2, 5, VAL); \
  81. WRITE_WB_REG_CASE(OP2, 6, VAL); \
  82. WRITE_WB_REG_CASE(OP2, 7, VAL); \
  83. WRITE_WB_REG_CASE(OP2, 8, VAL); \
  84. WRITE_WB_REG_CASE(OP2, 9, VAL); \
  85. WRITE_WB_REG_CASE(OP2, 10, VAL); \
  86. WRITE_WB_REG_CASE(OP2, 11, VAL); \
  87. WRITE_WB_REG_CASE(OP2, 12, VAL); \
  88. WRITE_WB_REG_CASE(OP2, 13, VAL); \
  89. WRITE_WB_REG_CASE(OP2, 14, VAL); \
  90. WRITE_WB_REG_CASE(OP2, 15, VAL)
  91. static u32 read_wb_reg(int n)
  92. {
  93. u32 val = 0;
  94. switch (n) {
  95. GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
  96. GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
  97. GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
  98. GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
  99. default:
  100. pr_warn("attempt to read from unknown breakpoint register %d\n",
  101. n);
  102. }
  103. return val;
  104. }
  105. static void write_wb_reg(int n, u32 val)
  106. {
  107. switch (n) {
  108. GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
  109. GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
  110. GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
  111. GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
  112. default:
  113. pr_warn("attempt to write to unknown breakpoint register %d\n",
  114. n);
  115. }
  116. isb();
  117. }
  118. /* Determine debug architecture. */
  119. static u8 get_debug_arch(void)
  120. {
  121. u32 didr;
  122. /* Do we implement the extended CPUID interface? */
  123. if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
  124. pr_warn_once("CPUID feature registers not supported. "
  125. "Assuming v6 debug is present.\n");
  126. return ARM_DEBUG_ARCH_V6;
  127. }
  128. ARM_DBG_READ(c0, c0, 0, didr);
  129. return (didr >> 16) & 0xf;
  130. }
  131. u8 arch_get_debug_arch(void)
  132. {
  133. return debug_arch;
  134. }
  135. static int debug_arch_supported(void)
  136. {
  137. u8 arch = get_debug_arch();
  138. /* We don't support the memory-mapped interface. */
  139. return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
  140. arch >= ARM_DEBUG_ARCH_V7_1;
  141. }
  142. /* Can we determine the watchpoint access type from the fsr? */
  143. static int debug_exception_updates_fsr(void)
  144. {
  145. return get_debug_arch() >= ARM_DEBUG_ARCH_V8;
  146. }
  147. /* Determine number of WRP registers available. */
  148. static int get_num_wrp_resources(void)
  149. {
  150. u32 didr;
  151. ARM_DBG_READ(c0, c0, 0, didr);
  152. return ((didr >> 28) & 0xf) + 1;
  153. }
  154. /* Determine number of BRP registers available. */
  155. static int get_num_brp_resources(void)
  156. {
  157. u32 didr;
  158. ARM_DBG_READ(c0, c0, 0, didr);
  159. return ((didr >> 24) & 0xf) + 1;
  160. }
  161. /* Does this core support mismatch breakpoints? */
  162. static int core_has_mismatch_brps(void)
  163. {
  164. return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
  165. get_num_brp_resources() > 1);
  166. }
  167. /* Determine number of usable WRPs available. */
  168. static int get_num_wrps(void)
  169. {
  170. /*
  171. * On debug architectures prior to 7.1, when a watchpoint fires, the
  172. * only way to work out which watchpoint it was is by disassembling
  173. * the faulting instruction and working out the address of the memory
  174. * access.
  175. *
  176. * Furthermore, we can only do this if the watchpoint was precise
  177. * since imprecise watchpoints prevent us from calculating register
  178. * based addresses.
  179. *
  180. * Providing we have more than 1 breakpoint register, we only report
  181. * a single watchpoint register for the time being. This way, we always
  182. * know which watchpoint fired. In the future we can either add a
  183. * disassembler and address generation emulator, or we can insert a
  184. * check to see if the DFAR is set on watchpoint exception entry
  185. * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
  186. * that it is set on some implementations].
  187. */
  188. if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
  189. return 1;
  190. return get_num_wrp_resources();
  191. }
  192. /* Determine number of usable BRPs available. */
  193. static int get_num_brps(void)
  194. {
  195. int brps = get_num_brp_resources();
  196. return core_has_mismatch_brps() ? brps - 1 : brps;
  197. }
  198. /*
  199. * In order to access the breakpoint/watchpoint control registers,
  200. * we must be running in debug monitor mode. Unfortunately, we can
  201. * be put into halting debug mode at any time by an external debugger
  202. * but there is nothing we can do to prevent that.
  203. */
  204. static int monitor_mode_enabled(void)
  205. {
  206. u32 dscr;
  207. ARM_DBG_READ(c0, c1, 0, dscr);
  208. return !!(dscr & ARM_DSCR_MDBGEN);
  209. }
  210. static int enable_monitor_mode(void)
  211. {
  212. u32 dscr;
  213. ARM_DBG_READ(c0, c1, 0, dscr);
  214. /* If monitor mode is already enabled, just return. */
  215. if (dscr & ARM_DSCR_MDBGEN)
  216. goto out;
  217. /* Write to the corresponding DSCR. */
  218. switch (get_debug_arch()) {
  219. case ARM_DEBUG_ARCH_V6:
  220. case ARM_DEBUG_ARCH_V6_1:
  221. ARM_DBG_WRITE(c0, c1, 0, (dscr | ARM_DSCR_MDBGEN));
  222. break;
  223. case ARM_DEBUG_ARCH_V7_ECP14:
  224. case ARM_DEBUG_ARCH_V7_1:
  225. case ARM_DEBUG_ARCH_V8:
  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_warn("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 = this_cpu_ptr(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 = this_cpu_ptr(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_warn("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 = this_cpu_ptr(bp_on_reg);
  345. max_slots = core_num_brps;
  346. } else {
  347. /* Watchpoint */
  348. base = ARM_BASE_WCR;
  349. slots = this_cpu_ptr(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_warn("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 arch_hw_breakpoint *hw)
  396. {
  397. unsigned int len;
  398. unsigned long va;
  399. va = hw->address;
  400. len = get_hbp_len(hw->ctrl.len);
  401. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  402. }
  403. /*
  404. * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
  405. * Hopefully this will disappear when ptrace can bypass the conversion
  406. * to generic breakpoint descriptions.
  407. */
  408. int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
  409. int *gen_len, int *gen_type)
  410. {
  411. /* Type */
  412. switch (ctrl.type) {
  413. case ARM_BREAKPOINT_EXECUTE:
  414. *gen_type = HW_BREAKPOINT_X;
  415. break;
  416. case ARM_BREAKPOINT_LOAD:
  417. *gen_type = HW_BREAKPOINT_R;
  418. break;
  419. case ARM_BREAKPOINT_STORE:
  420. *gen_type = HW_BREAKPOINT_W;
  421. break;
  422. case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
  423. *gen_type = HW_BREAKPOINT_RW;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. /* Len */
  429. switch (ctrl.len) {
  430. case ARM_BREAKPOINT_LEN_1:
  431. *gen_len = HW_BREAKPOINT_LEN_1;
  432. break;
  433. case ARM_BREAKPOINT_LEN_2:
  434. *gen_len = HW_BREAKPOINT_LEN_2;
  435. break;
  436. case ARM_BREAKPOINT_LEN_4:
  437. *gen_len = HW_BREAKPOINT_LEN_4;
  438. break;
  439. case ARM_BREAKPOINT_LEN_8:
  440. *gen_len = HW_BREAKPOINT_LEN_8;
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. return 0;
  446. }
  447. /*
  448. * Construct an arch_hw_breakpoint from a perf_event.
  449. */
  450. static int arch_build_bp_info(struct perf_event *bp,
  451. const struct perf_event_attr *attr,
  452. struct arch_hw_breakpoint *hw)
  453. {
  454. /* Type */
  455. switch (attr->bp_type) {
  456. case HW_BREAKPOINT_X:
  457. hw->ctrl.type = ARM_BREAKPOINT_EXECUTE;
  458. break;
  459. case HW_BREAKPOINT_R:
  460. hw->ctrl.type = ARM_BREAKPOINT_LOAD;
  461. break;
  462. case HW_BREAKPOINT_W:
  463. hw->ctrl.type = ARM_BREAKPOINT_STORE;
  464. break;
  465. case HW_BREAKPOINT_RW:
  466. hw->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. /* Len */
  472. switch (attr->bp_len) {
  473. case HW_BREAKPOINT_LEN_1:
  474. hw->ctrl.len = ARM_BREAKPOINT_LEN_1;
  475. break;
  476. case HW_BREAKPOINT_LEN_2:
  477. hw->ctrl.len = ARM_BREAKPOINT_LEN_2;
  478. break;
  479. case HW_BREAKPOINT_LEN_4:
  480. hw->ctrl.len = ARM_BREAKPOINT_LEN_4;
  481. break;
  482. case HW_BREAKPOINT_LEN_8:
  483. hw->ctrl.len = ARM_BREAKPOINT_LEN_8;
  484. if ((hw->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 (hw->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
  497. hw->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
  498. hw->ctrl.len != ARM_BREAKPOINT_LEN_4)
  499. return -EINVAL;
  500. /* Address */
  501. hw->address = attr->bp_addr;
  502. /* Privilege */
  503. hw->ctrl.privilege = ARM_BREAKPOINT_USER;
  504. if (arch_check_bp_in_kernelspace(hw))
  505. hw->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
  506. /* Enabled? */
  507. hw->ctrl.enabled = !attr->disabled;
  508. /* Mismatch */
  509. hw->ctrl.mismatch = 0;
  510. return 0;
  511. }
  512. /*
  513. * Validate the arch-specific HW Breakpoint register settings.
  514. */
  515. int hw_breakpoint_arch_parse(struct perf_event *bp,
  516. const struct perf_event_attr *attr,
  517. struct arch_hw_breakpoint *hw)
  518. {
  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, attr, hw);
  526. if (ret)
  527. goto out;
  528. /* Check address alignment. */
  529. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_8)
  530. alignment_mask = 0x7;
  531. offset = hw->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 (hw->ctrl.len == ARM_BREAKPOINT_LEN_2)
  540. break;
  541. case 3:
  542. /* Allow single byte watchpoint. */
  543. if (hw->ctrl.len == ARM_BREAKPOINT_LEN_1)
  544. break;
  545. default:
  546. ret = -EINVAL;
  547. goto out;
  548. }
  549. hw->address &= ~alignment_mask;
  550. hw->ctrl.len <<= offset;
  551. if (is_default_overflow_handler(bp)) {
  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(hw))
  560. return -EPERM;
  561. /*
  562. * Per-cpu breakpoints are not supported by our stepping
  563. * mechanism.
  564. */
  565. if (!bp->hw.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. (hw->ctrl.type == ARM_BREAKPOINT_LOAD ||
  573. hw->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. /*
  601. * Arm32 hardware does not always report a watchpoint hit address that matches
  602. * one of the watchpoints set. It can also report an address "near" the
  603. * watchpoint if a single instruction access both watched and unwatched
  604. * addresses. There is no straight-forward way, short of disassembling the
  605. * offending instruction, to map that address back to the watchpoint. This
  606. * function computes the distance of the memory access from the watchpoint as a
  607. * heuristic for the likelyhood that a given access triggered the watchpoint.
  608. *
  609. * See this same function in the arm64 platform code, which has the same
  610. * problem.
  611. *
  612. * The function returns the distance of the address from the bytes watched by
  613. * the watchpoint. In case of an exact match, it returns 0.
  614. */
  615. static u32 get_distance_from_watchpoint(unsigned long addr, u32 val,
  616. struct arch_hw_breakpoint_ctrl *ctrl)
  617. {
  618. u32 wp_low, wp_high;
  619. u32 lens, lene;
  620. lens = __ffs(ctrl->len);
  621. lene = __fls(ctrl->len);
  622. wp_low = val + lens;
  623. wp_high = val + lene;
  624. if (addr < wp_low)
  625. return wp_low - addr;
  626. else if (addr > wp_high)
  627. return addr - wp_high;
  628. else
  629. return 0;
  630. }
  631. static int watchpoint_fault_on_uaccess(struct pt_regs *regs,
  632. struct arch_hw_breakpoint *info)
  633. {
  634. return !user_mode(regs) && info->ctrl.privilege == ARM_BREAKPOINT_USER;
  635. }
  636. static void watchpoint_handler(unsigned long addr, unsigned int fsr,
  637. struct pt_regs *regs)
  638. {
  639. int i, access, closest_match = 0;
  640. u32 min_dist = -1, dist;
  641. u32 val, ctrl_reg;
  642. struct perf_event *wp, **slots;
  643. struct arch_hw_breakpoint *info;
  644. struct arch_hw_breakpoint_ctrl ctrl;
  645. slots = this_cpu_ptr(wp_on_reg);
  646. /*
  647. * Find all watchpoints that match the reported address. If no exact
  648. * match is found. Attribute the hit to the closest watchpoint.
  649. */
  650. rcu_read_lock();
  651. for (i = 0; i < core_num_wrps; ++i) {
  652. wp = slots[i];
  653. if (wp == NULL)
  654. continue;
  655. /*
  656. * The DFAR is an unknown value on debug architectures prior
  657. * to 7.1. Since we only allow a single watchpoint on these
  658. * older CPUs, we can set the trigger to the lowest possible
  659. * faulting address.
  660. */
  661. if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
  662. BUG_ON(i > 0);
  663. info = counter_arch_bp(wp);
  664. info->trigger = wp->attr.bp_addr;
  665. } else {
  666. /* Check that the access type matches. */
  667. if (debug_exception_updates_fsr()) {
  668. access = (fsr & ARM_FSR_ACCESS_MASK) ?
  669. HW_BREAKPOINT_W : HW_BREAKPOINT_R;
  670. if (!(access & hw_breakpoint_type(wp)))
  671. continue;
  672. }
  673. val = read_wb_reg(ARM_BASE_WVR + i);
  674. ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
  675. decode_ctrl_reg(ctrl_reg, &ctrl);
  676. dist = get_distance_from_watchpoint(addr, val, &ctrl);
  677. if (dist < min_dist) {
  678. min_dist = dist;
  679. closest_match = i;
  680. }
  681. /* Is this an exact match? */
  682. if (dist != 0)
  683. continue;
  684. /* We have a winner. */
  685. info = counter_arch_bp(wp);
  686. info->trigger = addr;
  687. }
  688. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  689. /*
  690. * If we triggered a user watchpoint from a uaccess routine,
  691. * then handle the stepping ourselves since userspace really
  692. * can't help us with this.
  693. */
  694. if (watchpoint_fault_on_uaccess(regs, info))
  695. goto step;
  696. perf_bp_event(wp, regs);
  697. /*
  698. * Defer stepping to the overflow handler if one is installed.
  699. * Otherwise, insert a temporary mismatch breakpoint so that
  700. * we can single-step over the watchpoint trigger.
  701. */
  702. if (!is_default_overflow_handler(wp))
  703. continue;
  704. step:
  705. enable_single_step(wp, instruction_pointer(regs));
  706. }
  707. if (min_dist > 0 && min_dist != -1) {
  708. /* No exact match found. */
  709. wp = slots[closest_match];
  710. info = counter_arch_bp(wp);
  711. info->trigger = addr;
  712. pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
  713. perf_bp_event(wp, regs);
  714. if (is_default_overflow_handler(wp))
  715. enable_single_step(wp, instruction_pointer(regs));
  716. }
  717. rcu_read_unlock();
  718. }
  719. static void watchpoint_single_step_handler(unsigned long pc)
  720. {
  721. int i;
  722. struct perf_event *wp, **slots;
  723. struct arch_hw_breakpoint *info;
  724. slots = this_cpu_ptr(wp_on_reg);
  725. for (i = 0; i < core_num_wrps; ++i) {
  726. rcu_read_lock();
  727. wp = slots[i];
  728. if (wp == NULL)
  729. goto unlock;
  730. info = counter_arch_bp(wp);
  731. if (!info->step_ctrl.enabled)
  732. goto unlock;
  733. /*
  734. * Restore the original watchpoint if we've completed the
  735. * single-step.
  736. */
  737. if (info->trigger != pc)
  738. disable_single_step(wp);
  739. unlock:
  740. rcu_read_unlock();
  741. }
  742. }
  743. static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
  744. {
  745. int i;
  746. u32 ctrl_reg, val, addr;
  747. struct perf_event *bp, **slots;
  748. struct arch_hw_breakpoint *info;
  749. struct arch_hw_breakpoint_ctrl ctrl;
  750. slots = this_cpu_ptr(bp_on_reg);
  751. /* The exception entry code places the amended lr in the PC. */
  752. addr = regs->ARM_pc;
  753. /* Check the currently installed breakpoints first. */
  754. for (i = 0; i < core_num_brps; ++i) {
  755. rcu_read_lock();
  756. bp = slots[i];
  757. if (bp == NULL)
  758. goto unlock;
  759. info = counter_arch_bp(bp);
  760. /* Check if the breakpoint value matches. */
  761. val = read_wb_reg(ARM_BASE_BVR + i);
  762. if (val != (addr & ~0x3))
  763. goto mismatch;
  764. /* Possible match, check the byte address select to confirm. */
  765. ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
  766. decode_ctrl_reg(ctrl_reg, &ctrl);
  767. if ((1 << (addr & 0x3)) & ctrl.len) {
  768. info->trigger = addr;
  769. pr_debug("breakpoint fired: address = 0x%x\n", addr);
  770. perf_bp_event(bp, regs);
  771. if (is_default_overflow_handler(bp))
  772. enable_single_step(bp, addr);
  773. goto unlock;
  774. }
  775. mismatch:
  776. /* If we're stepping a breakpoint, it can now be restored. */
  777. if (info->step_ctrl.enabled)
  778. disable_single_step(bp);
  779. unlock:
  780. rcu_read_unlock();
  781. }
  782. /* Handle any pending watchpoint single-step breakpoints. */
  783. watchpoint_single_step_handler(addr);
  784. }
  785. /*
  786. * Called from either the Data Abort Handler [watchpoint] or the
  787. * Prefetch Abort Handler [breakpoint] with interrupts disabled.
  788. */
  789. static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
  790. struct pt_regs *regs)
  791. {
  792. int ret = 0;
  793. u32 dscr;
  794. preempt_disable();
  795. if (interrupts_enabled(regs))
  796. local_irq_enable();
  797. /* We only handle watchpoints and hardware breakpoints. */
  798. ARM_DBG_READ(c0, c1, 0, dscr);
  799. /* Perform perf callbacks. */
  800. switch (ARM_DSCR_MOE(dscr)) {
  801. case ARM_ENTRY_BREAKPOINT:
  802. breakpoint_handler(addr, regs);
  803. break;
  804. case ARM_ENTRY_ASYNC_WATCHPOINT:
  805. WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
  806. case ARM_ENTRY_SYNC_WATCHPOINT:
  807. watchpoint_handler(addr, fsr, regs);
  808. break;
  809. default:
  810. ret = 1; /* Unhandled fault. */
  811. }
  812. preempt_enable();
  813. return ret;
  814. }
  815. /*
  816. * One-time initialisation.
  817. */
  818. static cpumask_t debug_err_mask;
  819. static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
  820. {
  821. int cpu = smp_processor_id();
  822. pr_warn("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
  823. instr, cpu);
  824. /* Set the error flag for this CPU and skip the faulting instruction. */
  825. cpumask_set_cpu(cpu, &debug_err_mask);
  826. instruction_pointer(regs) += 4;
  827. return 0;
  828. }
  829. static struct undef_hook debug_reg_hook = {
  830. .instr_mask = 0x0fe80f10,
  831. .instr_val = 0x0e000e10,
  832. .fn = debug_reg_trap,
  833. };
  834. /* Does this core support OS Save and Restore? */
  835. static bool core_has_os_save_restore(void)
  836. {
  837. u32 oslsr;
  838. switch (get_debug_arch()) {
  839. case ARM_DEBUG_ARCH_V7_1:
  840. return true;
  841. case ARM_DEBUG_ARCH_V7_ECP14:
  842. ARM_DBG_READ(c1, c1, 4, oslsr);
  843. if (oslsr & ARM_OSLSR_OSLM0)
  844. return true;
  845. default:
  846. return false;
  847. }
  848. }
  849. static void reset_ctrl_regs(unsigned int cpu)
  850. {
  851. int i, raw_num_brps, err = 0;
  852. u32 val;
  853. /*
  854. * v7 debug contains save and restore registers so that debug state
  855. * can be maintained across low-power modes without leaving the debug
  856. * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
  857. * the debug registers out of reset, so we must unlock the OS Lock
  858. * Access Register to avoid taking undefined instruction exceptions
  859. * later on.
  860. */
  861. switch (debug_arch) {
  862. case ARM_DEBUG_ARCH_V6:
  863. case ARM_DEBUG_ARCH_V6_1:
  864. /* ARMv6 cores clear the registers out of reset. */
  865. goto out_mdbgen;
  866. case ARM_DEBUG_ARCH_V7_ECP14:
  867. /*
  868. * Ensure sticky power-down is clear (i.e. debug logic is
  869. * powered up).
  870. */
  871. ARM_DBG_READ(c1, c5, 4, val);
  872. if ((val & 0x1) == 0)
  873. err = -EPERM;
  874. if (!has_ossr)
  875. goto clear_vcr;
  876. break;
  877. case ARM_DEBUG_ARCH_V7_1:
  878. /*
  879. * Ensure the OS double lock is clear.
  880. */
  881. ARM_DBG_READ(c1, c3, 4, val);
  882. if ((val & 0x1) == 1)
  883. err = -EPERM;
  884. break;
  885. }
  886. if (err) {
  887. pr_warn_once("CPU %d debug is powered down!\n", cpu);
  888. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  889. return;
  890. }
  891. /*
  892. * Unconditionally clear the OS lock by writing a value
  893. * other than CS_LAR_KEY to the access register.
  894. */
  895. ARM_DBG_WRITE(c1, c0, 4, ~CORESIGHT_UNLOCK);
  896. isb();
  897. /*
  898. * Clear any configured vector-catch events before
  899. * enabling monitor mode.
  900. */
  901. clear_vcr:
  902. ARM_DBG_WRITE(c0, c7, 0, 0);
  903. isb();
  904. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  905. pr_warn_once("CPU %d failed to disable vector catch\n", cpu);
  906. return;
  907. }
  908. /*
  909. * The control/value register pairs are UNKNOWN out of reset so
  910. * clear them to avoid spurious debug events.
  911. */
  912. raw_num_brps = get_num_brp_resources();
  913. for (i = 0; i < raw_num_brps; ++i) {
  914. write_wb_reg(ARM_BASE_BCR + i, 0UL);
  915. write_wb_reg(ARM_BASE_BVR + i, 0UL);
  916. }
  917. for (i = 0; i < core_num_wrps; ++i) {
  918. write_wb_reg(ARM_BASE_WCR + i, 0UL);
  919. write_wb_reg(ARM_BASE_WVR + i, 0UL);
  920. }
  921. if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
  922. pr_warn_once("CPU %d failed to clear debug register pairs\n", cpu);
  923. return;
  924. }
  925. /*
  926. * Have a crack at enabling monitor mode. We don't actually need
  927. * it yet, but reporting an error early is useful if it fails.
  928. */
  929. out_mdbgen:
  930. if (enable_monitor_mode())
  931. cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
  932. }
  933. static int dbg_reset_online(unsigned int cpu)
  934. {
  935. local_irq_disable();
  936. reset_ctrl_regs(cpu);
  937. local_irq_enable();
  938. return 0;
  939. }
  940. #ifdef CONFIG_CPU_PM
  941. static int dbg_cpu_pm_notify(struct notifier_block *self, unsigned long action,
  942. void *v)
  943. {
  944. if (action == CPU_PM_EXIT)
  945. reset_ctrl_regs(smp_processor_id());
  946. return NOTIFY_OK;
  947. }
  948. static struct notifier_block dbg_cpu_pm_nb = {
  949. .notifier_call = dbg_cpu_pm_notify,
  950. };
  951. static void __init pm_init(void)
  952. {
  953. cpu_pm_register_notifier(&dbg_cpu_pm_nb);
  954. }
  955. #else
  956. static inline void pm_init(void)
  957. {
  958. }
  959. #endif
  960. static int __init arch_hw_breakpoint_init(void)
  961. {
  962. int ret;
  963. debug_arch = get_debug_arch();
  964. if (!debug_arch_supported()) {
  965. pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
  966. return 0;
  967. }
  968. /*
  969. * Scorpion CPUs (at least those in APQ8060) seem to set DBGPRSR.SPD
  970. * whenever a WFI is issued, even if the core is not powered down, in
  971. * violation of the architecture. When DBGPRSR.SPD is set, accesses to
  972. * breakpoint and watchpoint registers are treated as undefined, so
  973. * this results in boot time and runtime failures when these are
  974. * accessed and we unexpectedly take a trap.
  975. *
  976. * It's not clear if/how this can be worked around, so we blacklist
  977. * Scorpion CPUs to avoid these issues.
  978. */
  979. if (read_cpuid_part() == ARM_CPU_PART_SCORPION) {
  980. pr_info("Scorpion CPU detected. Hardware breakpoints and watchpoints disabled\n");
  981. return 0;
  982. }
  983. has_ossr = core_has_os_save_restore();
  984. /* Determine how many BRPs/WRPs are available. */
  985. core_num_brps = get_num_brps();
  986. core_num_wrps = get_num_wrps();
  987. /*
  988. * We need to tread carefully here because DBGSWENABLE may be
  989. * driven low on this core and there isn't an architected way to
  990. * determine that.
  991. */
  992. cpus_read_lock();
  993. register_undef_hook(&debug_reg_hook);
  994. /*
  995. * Register CPU notifier which resets the breakpoint resources. We
  996. * assume that a halting debugger will leave the world in a nice state
  997. * for us.
  998. */
  999. ret = cpuhp_setup_state_cpuslocked(CPUHP_AP_ONLINE_DYN,
  1000. "arm/hw_breakpoint:online",
  1001. dbg_reset_online, NULL);
  1002. unregister_undef_hook(&debug_reg_hook);
  1003. if (WARN_ON(ret < 0) || !cpumask_empty(&debug_err_mask)) {
  1004. core_num_brps = 0;
  1005. core_num_wrps = 0;
  1006. if (ret > 0)
  1007. cpuhp_remove_state_nocalls_cpuslocked(ret);
  1008. cpus_read_unlock();
  1009. return 0;
  1010. }
  1011. pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
  1012. core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
  1013. "", core_num_wrps);
  1014. /* Work out the maximum supported watchpoint length. */
  1015. max_watchpoint_len = get_max_wp_len();
  1016. pr_info("maximum watchpoint size is %u bytes.\n",
  1017. max_watchpoint_len);
  1018. /* Register debug fault handler. */
  1019. hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  1020. TRAP_HWBKPT, "watchpoint debug exception");
  1021. hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
  1022. TRAP_HWBKPT, "breakpoint debug exception");
  1023. cpus_read_unlock();
  1024. /* Register PM notifiers. */
  1025. pm_init();
  1026. return 0;
  1027. }
  1028. arch_initcall(arch_hw_breakpoint_init);
  1029. void hw_breakpoint_pmu_read(struct perf_event *bp)
  1030. {
  1031. }
  1032. /*
  1033. * Dummy function to register with die_notifier.
  1034. */
  1035. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  1036. unsigned long val, void *data)
  1037. {
  1038. return NOTIFY_DONE;
  1039. }