uprobes.c 10 KB

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