uprobes.c 10 KB

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