uprobes.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * User-space Probes (UProbes) for s390
  3. *
  4. * Copyright IBM Corp. 2014
  5. * Author(s): Jan Willeke,
  6. */
  7. #include <linux/uaccess.h>
  8. #include <linux/uprobes.h>
  9. #include <linux/compat.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/sched/task_stack.h>
  12. #include <asm/switch_to.h>
  13. #include <asm/facility.h>
  14. #include <asm/kprobes.h>
  15. #include <asm/dis.h>
  16. #include "entry.h"
  17. #define UPROBE_TRAP_NR UINT_MAX
  18. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
  19. unsigned long addr)
  20. {
  21. return probe_is_prohibited_opcode(auprobe->insn);
  22. }
  23. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  24. {
  25. if (psw_bits(regs->psw).eaba == PSW_AMODE_24BIT)
  26. return -EINVAL;
  27. if (!is_compat_task() && psw_bits(regs->psw).eaba == PSW_AMODE_31BIT)
  28. return -EINVAL;
  29. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  30. auprobe->saved_per = psw_bits(regs->psw).r;
  31. auprobe->saved_int_code = regs->int_code;
  32. regs->int_code = UPROBE_TRAP_NR;
  33. regs->psw.addr = current->utask->xol_vaddr;
  34. set_tsk_thread_flag(current, TIF_UPROBE_SINGLESTEP);
  35. update_cr_regs(current);
  36. return 0;
  37. }
  38. bool arch_uprobe_xol_was_trapped(struct task_struct *tsk)
  39. {
  40. struct pt_regs *regs = task_pt_regs(tsk);
  41. if (regs->int_code != UPROBE_TRAP_NR)
  42. return true;
  43. return false;
  44. }
  45. static int check_per_event(unsigned short cause, unsigned long control,
  46. struct pt_regs *regs)
  47. {
  48. if (!(regs->psw.mask & PSW_MASK_PER))
  49. return 0;
  50. /* user space single step */
  51. if (control == 0)
  52. return 1;
  53. /* over indication for storage alteration */
  54. if ((control & 0x20200000) && (cause & 0x2000))
  55. return 1;
  56. if (cause & 0x8000) {
  57. /* all branches */
  58. if ((control & 0x80800000) == 0x80000000)
  59. return 1;
  60. /* branch into selected range */
  61. if (((control & 0x80800000) == 0x80800000) &&
  62. regs->psw.addr >= current->thread.per_user.start &&
  63. regs->psw.addr <= current->thread.per_user.end)
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  69. {
  70. int fixup = probe_get_fixup_type(auprobe->insn);
  71. struct uprobe_task *utask = current->utask;
  72. clear_tsk_thread_flag(current, TIF_UPROBE_SINGLESTEP);
  73. update_cr_regs(current);
  74. psw_bits(regs->psw).r = auprobe->saved_per;
  75. regs->int_code = auprobe->saved_int_code;
  76. if (fixup & FIXUP_PSW_NORMAL)
  77. regs->psw.addr += utask->vaddr - utask->xol_vaddr;
  78. if (fixup & FIXUP_RETURN_REGISTER) {
  79. int reg = (auprobe->insn[0] & 0xf0) >> 4;
  80. regs->gprs[reg] += utask->vaddr - utask->xol_vaddr;
  81. }
  82. if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
  83. int ilen = insn_length(auprobe->insn[0] >> 8);
  84. if (regs->psw.addr - utask->xol_vaddr == ilen)
  85. regs->psw.addr = utask->vaddr + ilen;
  86. }
  87. if (check_per_event(current->thread.per_event.cause,
  88. current->thread.per_user.control, regs)) {
  89. /* fix per address */
  90. current->thread.per_event.address = utask->vaddr;
  91. /* trigger per event */
  92. set_pt_regs_flag(regs, PIF_PER_TRAP);
  93. }
  94. return 0;
  95. }
  96. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val,
  97. void *data)
  98. {
  99. struct die_args *args = data;
  100. struct pt_regs *regs = args->regs;
  101. if (!user_mode(regs))
  102. return NOTIFY_DONE;
  103. if (regs->int_code & 0x200) /* Trap during transaction */
  104. return NOTIFY_DONE;
  105. switch (val) {
  106. case DIE_BPT:
  107. if (uprobe_pre_sstep_notifier(regs))
  108. return NOTIFY_STOP;
  109. break;
  110. case DIE_SSTEP:
  111. if (uprobe_post_sstep_notifier(regs))
  112. return NOTIFY_STOP;
  113. default:
  114. break;
  115. }
  116. return NOTIFY_DONE;
  117. }
  118. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  119. {
  120. clear_thread_flag(TIF_UPROBE_SINGLESTEP);
  121. regs->int_code = auprobe->saved_int_code;
  122. regs->psw.addr = current->utask->vaddr;
  123. current->thread.per_event.address = current->utask->vaddr;
  124. }
  125. unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline,
  126. struct pt_regs *regs)
  127. {
  128. unsigned long orig;
  129. orig = regs->gprs[14];
  130. regs->gprs[14] = trampoline;
  131. return orig;
  132. }
  133. /* Instruction Emulation */
  134. static void adjust_psw_addr(psw_t *psw, unsigned long len)
  135. {
  136. psw->addr = __rewind_psw(*psw, -len);
  137. }
  138. #define EMU_ILLEGAL_OP 1
  139. #define EMU_SPECIFICATION 2
  140. #define EMU_ADDRESSING 3
  141. #define emu_load_ril(ptr, output) \
  142. ({ \
  143. unsigned int mask = sizeof(*(ptr)) - 1; \
  144. __typeof__(*(ptr)) input; \
  145. int __rc = 0; \
  146. \
  147. if (!test_facility(34)) \
  148. __rc = EMU_ILLEGAL_OP; \
  149. else if ((u64 __force)ptr & mask) \
  150. __rc = EMU_SPECIFICATION; \
  151. else if (get_user(input, ptr)) \
  152. __rc = EMU_ADDRESSING; \
  153. else \
  154. *(output) = input; \
  155. __rc; \
  156. })
  157. #define emu_store_ril(regs, ptr, input) \
  158. ({ \
  159. unsigned int mask = sizeof(*(ptr)) - 1; \
  160. __typeof__(ptr) __ptr = (ptr); \
  161. int __rc = 0; \
  162. \
  163. if (!test_facility(34)) \
  164. __rc = EMU_ILLEGAL_OP; \
  165. else if ((u64 __force)__ptr & mask) \
  166. __rc = EMU_SPECIFICATION; \
  167. else if (put_user(*(input), __ptr)) \
  168. __rc = EMU_ADDRESSING; \
  169. if (__rc == 0) \
  170. sim_stor_event(regs, \
  171. (void __force *)__ptr, \
  172. mask + 1); \
  173. __rc; \
  174. })
  175. #define emu_cmp_ril(regs, ptr, cmp) \
  176. ({ \
  177. unsigned int mask = sizeof(*(ptr)) - 1; \
  178. __typeof__(*(ptr)) input; \
  179. int __rc = 0; \
  180. \
  181. if (!test_facility(34)) \
  182. __rc = EMU_ILLEGAL_OP; \
  183. else if ((u64 __force)ptr & mask) \
  184. __rc = EMU_SPECIFICATION; \
  185. else if (get_user(input, ptr)) \
  186. __rc = EMU_ADDRESSING; \
  187. else if (input > *(cmp)) \
  188. psw_bits((regs)->psw).cc = 1; \
  189. else if (input < *(cmp)) \
  190. psw_bits((regs)->psw).cc = 2; \
  191. else \
  192. psw_bits((regs)->psw).cc = 0; \
  193. __rc; \
  194. })
  195. struct insn_ril {
  196. u8 opc0;
  197. u8 reg : 4;
  198. u8 opc1 : 4;
  199. s32 disp;
  200. } __packed;
  201. union split_register {
  202. u64 u64;
  203. u32 u32[2];
  204. u16 u16[4];
  205. s64 s64;
  206. s32 s32[2];
  207. s16 s16[4];
  208. };
  209. /*
  210. * If user per registers are setup to trace storage alterations and an
  211. * emulated store took place on a fitting address a user trap is generated.
  212. */
  213. static void sim_stor_event(struct pt_regs *regs, void *addr, int len)
  214. {
  215. if (!(regs->psw.mask & PSW_MASK_PER))
  216. return;
  217. if (!(current->thread.per_user.control & PER_EVENT_STORE))
  218. return;
  219. if ((void *)current->thread.per_user.start > (addr + len))
  220. return;
  221. if ((void *)current->thread.per_user.end < addr)
  222. return;
  223. current->thread.per_event.address = regs->psw.addr;
  224. current->thread.per_event.cause = PER_EVENT_STORE >> 16;
  225. set_pt_regs_flag(regs, PIF_PER_TRAP);
  226. }
  227. /*
  228. * pc relative instructions are emulated, since parameters may not be
  229. * accessible from the xol area due to range limitations.
  230. */
  231. static void handle_insn_ril(struct arch_uprobe *auprobe, struct pt_regs *regs)
  232. {
  233. union split_register *rx;
  234. struct insn_ril *insn;
  235. unsigned int ilen;
  236. void *uptr;
  237. int rc = 0;
  238. insn = (struct insn_ril *) &auprobe->insn;
  239. rx = (union split_register *) &regs->gprs[insn->reg];
  240. uptr = (void *)(regs->psw.addr + (insn->disp * 2));
  241. ilen = insn_length(insn->opc0);
  242. switch (insn->opc0) {
  243. case 0xc0:
  244. switch (insn->opc1) {
  245. case 0x00: /* larl */
  246. rx->u64 = (unsigned long)uptr;
  247. break;
  248. }
  249. break;
  250. case 0xc4:
  251. switch (insn->opc1) {
  252. case 0x02: /* llhrl */
  253. rc = emu_load_ril((u16 __user *)uptr, &rx->u32[1]);
  254. break;
  255. case 0x04: /* lghrl */
  256. rc = emu_load_ril((s16 __user *)uptr, &rx->u64);
  257. break;
  258. case 0x05: /* lhrl */
  259. rc = emu_load_ril((s16 __user *)uptr, &rx->u32[1]);
  260. break;
  261. case 0x06: /* llghrl */
  262. rc = emu_load_ril((u16 __user *)uptr, &rx->u64);
  263. break;
  264. case 0x08: /* lgrl */
  265. rc = emu_load_ril((u64 __user *)uptr, &rx->u64);
  266. break;
  267. case 0x0c: /* lgfrl */
  268. rc = emu_load_ril((s32 __user *)uptr, &rx->u64);
  269. break;
  270. case 0x0d: /* lrl */
  271. rc = emu_load_ril((u32 __user *)uptr, &rx->u32[1]);
  272. break;
  273. case 0x0e: /* llgfrl */
  274. rc = emu_load_ril((u32 __user *)uptr, &rx->u64);
  275. break;
  276. case 0x07: /* sthrl */
  277. rc = emu_store_ril(regs, (u16 __user *)uptr, &rx->u16[3]);
  278. break;
  279. case 0x0b: /* stgrl */
  280. rc = emu_store_ril(regs, (u64 __user *)uptr, &rx->u64);
  281. break;
  282. case 0x0f: /* strl */
  283. rc = emu_store_ril(regs, (u32 __user *)uptr, &rx->u32[1]);
  284. break;
  285. }
  286. break;
  287. case 0xc6:
  288. switch (insn->opc1) {
  289. case 0x02: /* pfdrl */
  290. if (!test_facility(34))
  291. rc = EMU_ILLEGAL_OP;
  292. break;
  293. case 0x04: /* cghrl */
  294. rc = emu_cmp_ril(regs, (s16 __user *)uptr, &rx->s64);
  295. break;
  296. case 0x05: /* chrl */
  297. rc = emu_cmp_ril(regs, (s16 __user *)uptr, &rx->s32[1]);
  298. break;
  299. case 0x06: /* clghrl */
  300. rc = emu_cmp_ril(regs, (u16 __user *)uptr, &rx->u64);
  301. break;
  302. case 0x07: /* clhrl */
  303. rc = emu_cmp_ril(regs, (u16 __user *)uptr, &rx->u32[1]);
  304. break;
  305. case 0x08: /* cgrl */
  306. rc = emu_cmp_ril(regs, (s64 __user *)uptr, &rx->s64);
  307. break;
  308. case 0x0a: /* clgrl */
  309. rc = emu_cmp_ril(regs, (u64 __user *)uptr, &rx->u64);
  310. break;
  311. case 0x0c: /* cgfrl */
  312. rc = emu_cmp_ril(regs, (s32 __user *)uptr, &rx->s64);
  313. break;
  314. case 0x0d: /* crl */
  315. rc = emu_cmp_ril(regs, (s32 __user *)uptr, &rx->s32[1]);
  316. break;
  317. case 0x0e: /* clgfrl */
  318. rc = emu_cmp_ril(regs, (u32 __user *)uptr, &rx->u64);
  319. break;
  320. case 0x0f: /* clrl */
  321. rc = emu_cmp_ril(regs, (u32 __user *)uptr, &rx->u32[1]);
  322. break;
  323. }
  324. break;
  325. }
  326. adjust_psw_addr(&regs->psw, ilen);
  327. switch (rc) {
  328. case EMU_ILLEGAL_OP:
  329. regs->int_code = ilen << 16 | 0x0001;
  330. do_report_trap(regs, SIGILL, ILL_ILLOPC, NULL);
  331. break;
  332. case EMU_SPECIFICATION:
  333. regs->int_code = ilen << 16 | 0x0006;
  334. do_report_trap(regs, SIGILL, ILL_ILLOPC , NULL);
  335. break;
  336. case EMU_ADDRESSING:
  337. regs->int_code = ilen << 16 | 0x0005;
  338. do_report_trap(regs, SIGSEGV, SEGV_MAPERR, NULL);
  339. break;
  340. }
  341. }
  342. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  343. {
  344. if ((psw_bits(regs->psw).eaba == PSW_AMODE_24BIT) ||
  345. ((psw_bits(regs->psw).eaba == PSW_AMODE_31BIT) &&
  346. !is_compat_task())) {
  347. regs->psw.addr = __rewind_psw(regs->psw, UPROBE_SWBP_INSN_SIZE);
  348. do_report_trap(regs, SIGILL, ILL_ILLADR, NULL);
  349. return true;
  350. }
  351. if (probe_is_insn_relative_long(auprobe->insn)) {
  352. handle_insn_ril(auprobe, regs);
  353. return true;
  354. }
  355. return false;
  356. }