1 // SPDX-License-Identifier: GPL-2.0
3 * User-space Probes (UProbes) for s390
5 * Copyright IBM Corp. 2014
6 * Author(s): Jan Willeke,
9 #include <linux/uaccess.h>
10 #include <linux/uprobes.h>
11 #include <linux/compat.h>
12 #include <linux/kdebug.h>
13 #include <linux/sched/task_stack.h>
15 #include <asm/facility.h>
16 #include <asm/kprobes.h>
20 #define UPROBE_TRAP_NR UINT_MAX
22 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
25 return probe_is_prohibited_opcode(auprobe->insn);
28 int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
30 if (psw_bits(regs->psw).eaba == PSW_BITS_AMODE_24BIT)
32 if (!is_compat_task() && psw_bits(regs->psw).eaba == PSW_BITS_AMODE_31BIT)
34 clear_thread_flag(TIF_PER_TRAP);
35 auprobe->saved_per = psw_bits(regs->psw).per;
36 auprobe->saved_int_code = regs->int_code;
37 regs->int_code = UPROBE_TRAP_NR;
38 regs->psw.addr = current->utask->xol_vaddr;
39 set_tsk_thread_flag(current, TIF_UPROBE_SINGLESTEP);
40 update_cr_regs(current);
44 bool arch_uprobe_xol_was_trapped(struct task_struct *tsk)
46 struct pt_regs *regs = task_pt_regs(tsk);
48 if (regs->int_code != UPROBE_TRAP_NR)
53 static int check_per_event(unsigned short cause, unsigned long control,
56 if (!(regs->psw.mask & PSW_MASK_PER))
58 /* user space single step */
61 /* over indication for storage alteration */
62 if ((control & 0x20200000) && (cause & 0x2000))
66 if ((control & 0x80800000) == 0x80000000)
68 /* branch into selected range */
69 if (((control & 0x80800000) == 0x80800000) &&
70 regs->psw.addr >= current->thread.per_user.start &&
71 regs->psw.addr <= current->thread.per_user.end)
77 int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
79 int fixup = probe_get_fixup_type(auprobe->insn);
80 struct uprobe_task *utask = current->utask;
82 clear_tsk_thread_flag(current, TIF_UPROBE_SINGLESTEP);
83 update_cr_regs(current);
84 psw_bits(regs->psw).per = auprobe->saved_per;
85 regs->int_code = auprobe->saved_int_code;
87 if (fixup & FIXUP_PSW_NORMAL)
88 regs->psw.addr += utask->vaddr - utask->xol_vaddr;
89 if (fixup & FIXUP_RETURN_REGISTER) {
90 int reg = (auprobe->insn[0] & 0xf0) >> 4;
92 regs->gprs[reg] += utask->vaddr - utask->xol_vaddr;
94 if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
95 int ilen = insn_length(auprobe->insn[0] >> 8);
97 if (regs->psw.addr - utask->xol_vaddr == ilen)
98 regs->psw.addr = utask->vaddr + ilen;
100 if (check_per_event(current->thread.per_event.cause,
101 current->thread.per_user.control, regs)) {
102 /* fix per address */
103 current->thread.per_event.address = utask->vaddr;
104 /* trigger per event */
105 set_thread_flag(TIF_PER_TRAP);
110 int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val,
113 struct die_args *args = data;
114 struct pt_regs *regs = args->regs;
116 if (!user_mode(regs))
118 if (regs->int_code & 0x200) /* Trap during transaction */
122 if (uprobe_pre_sstep_notifier(regs))
126 if (uprobe_post_sstep_notifier(regs))
135 void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
137 clear_thread_flag(TIF_UPROBE_SINGLESTEP);
138 regs->int_code = auprobe->saved_int_code;
139 regs->psw.addr = current->utask->vaddr;
140 current->thread.per_event.address = current->utask->vaddr;
143 unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline,
144 struct pt_regs *regs)
148 orig = regs->gprs[14];
149 regs->gprs[14] = trampoline;
153 bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
154 struct pt_regs *regs)
156 if (ctx == RP_CHECK_CHAIN_CALL)
157 return user_stack_pointer(regs) <= ret->stack;
159 return user_stack_pointer(regs) < ret->stack;
162 /* Instruction Emulation */
164 static void adjust_psw_addr(psw_t *psw, unsigned long len)
166 psw->addr = __rewind_psw(*psw, -len);
169 #define EMU_ILLEGAL_OP 1
170 #define EMU_SPECIFICATION 2
171 #define EMU_ADDRESSING 3
173 #define emu_load_ril(ptr, output) \
175 unsigned int mask = sizeof(*(ptr)) - 1; \
176 __typeof__(*(ptr)) input; \
179 if ((u64 __force)ptr & mask) \
180 __rc = EMU_SPECIFICATION; \
181 else if (get_user(input, ptr)) \
182 __rc = EMU_ADDRESSING; \
188 #define emu_store_ril(regs, ptr, input) \
190 unsigned int mask = sizeof(*(ptr)) - 1; \
191 __typeof__(ptr) __ptr = (ptr); \
194 if ((u64 __force)__ptr & mask) \
195 __rc = EMU_SPECIFICATION; \
196 else if (put_user(*(input), __ptr)) \
197 __rc = EMU_ADDRESSING; \
199 sim_stor_event(regs, \
200 (void __force *)__ptr, \
205 #define emu_cmp_ril(regs, ptr, cmp) \
207 unsigned int mask = sizeof(*(ptr)) - 1; \
208 __typeof__(*(ptr)) input; \
211 if ((u64 __force)ptr & mask) \
212 __rc = EMU_SPECIFICATION; \
213 else if (get_user(input, ptr)) \
214 __rc = EMU_ADDRESSING; \
215 else if (input > *(cmp)) \
216 psw_bits((regs)->psw).cc = 1; \
217 else if (input < *(cmp)) \
218 psw_bits((regs)->psw).cc = 2; \
220 psw_bits((regs)->psw).cc = 0; \
231 union split_register {
241 * If user per registers are setup to trace storage alterations and an
242 * emulated store took place on a fitting address a user trap is generated.
244 static void sim_stor_event(struct pt_regs *regs, void *addr, int len)
246 if (!(regs->psw.mask & PSW_MASK_PER))
248 if (!(current->thread.per_user.control & PER_EVENT_STORE))
250 if ((void *)current->thread.per_user.start > (addr + len))
252 if ((void *)current->thread.per_user.end < addr)
254 current->thread.per_event.address = regs->psw.addr;
255 current->thread.per_event.cause = PER_EVENT_STORE >> 16;
256 set_thread_flag(TIF_PER_TRAP);
260 * pc relative instructions are emulated, since parameters may not be
261 * accessible from the xol area due to range limitations.
263 static void handle_insn_ril(struct arch_uprobe *auprobe, struct pt_regs *regs)
265 union split_register *rx;
266 struct insn_ril *insn;
271 insn = (struct insn_ril *) &auprobe->insn;
272 rx = (union split_register *) ®s->gprs[insn->reg];
273 uptr = (void *)(regs->psw.addr + (insn->disp * 2));
274 ilen = insn_length(insn->opc0);
276 switch (insn->opc0) {
278 switch (insn->opc1) {
279 case 0x00: /* larl */
280 rx->u64 = (unsigned long)uptr;
285 switch (insn->opc1) {
286 case 0x02: /* llhrl */
287 rc = emu_load_ril((u16 __user *)uptr, &rx->u32[1]);
289 case 0x04: /* lghrl */
290 rc = emu_load_ril((s16 __user *)uptr, &rx->u64);
292 case 0x05: /* lhrl */
293 rc = emu_load_ril((s16 __user *)uptr, &rx->u32[1]);
295 case 0x06: /* llghrl */
296 rc = emu_load_ril((u16 __user *)uptr, &rx->u64);
298 case 0x08: /* lgrl */
299 rc = emu_load_ril((u64 __user *)uptr, &rx->u64);
301 case 0x0c: /* lgfrl */
302 rc = emu_load_ril((s32 __user *)uptr, &rx->u64);
305 rc = emu_load_ril((u32 __user *)uptr, &rx->u32[1]);
307 case 0x0e: /* llgfrl */
308 rc = emu_load_ril((u32 __user *)uptr, &rx->u64);
310 case 0x07: /* sthrl */
311 rc = emu_store_ril(regs, (u16 __user *)uptr, &rx->u16[3]);
313 case 0x0b: /* stgrl */
314 rc = emu_store_ril(regs, (u64 __user *)uptr, &rx->u64);
316 case 0x0f: /* strl */
317 rc = emu_store_ril(regs, (u32 __user *)uptr, &rx->u32[1]);
322 switch (insn->opc1) {
323 case 0x04: /* cghrl */
324 rc = emu_cmp_ril(regs, (s16 __user *)uptr, &rx->s64);
326 case 0x05: /* chrl */
327 rc = emu_cmp_ril(regs, (s16 __user *)uptr, &rx->s32[1]);
329 case 0x06: /* clghrl */
330 rc = emu_cmp_ril(regs, (u16 __user *)uptr, &rx->u64);
332 case 0x07: /* clhrl */
333 rc = emu_cmp_ril(regs, (u16 __user *)uptr, &rx->u32[1]);
335 case 0x08: /* cgrl */
336 rc = emu_cmp_ril(regs, (s64 __user *)uptr, &rx->s64);
338 case 0x0a: /* clgrl */
339 rc = emu_cmp_ril(regs, (u64 __user *)uptr, &rx->u64);
341 case 0x0c: /* cgfrl */
342 rc = emu_cmp_ril(regs, (s32 __user *)uptr, &rx->s64);
345 rc = emu_cmp_ril(regs, (s32 __user *)uptr, &rx->s32[1]);
347 case 0x0e: /* clgfrl */
348 rc = emu_cmp_ril(regs, (u32 __user *)uptr, &rx->u64);
350 case 0x0f: /* clrl */
351 rc = emu_cmp_ril(regs, (u32 __user *)uptr, &rx->u32[1]);
356 adjust_psw_addr(®s->psw, ilen);
359 regs->int_code = ilen << 16 | 0x0001;
360 do_report_trap(regs, SIGILL, ILL_ILLOPC, NULL);
362 case EMU_SPECIFICATION:
363 regs->int_code = ilen << 16 | 0x0006;
364 do_report_trap(regs, SIGILL, ILL_ILLOPC , NULL);
367 regs->int_code = ilen << 16 | 0x0005;
368 do_report_trap(regs, SIGSEGV, SEGV_MAPERR, NULL);
373 bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
375 if ((psw_bits(regs->psw).eaba == PSW_BITS_AMODE_24BIT) ||
376 ((psw_bits(regs->psw).eaba == PSW_BITS_AMODE_31BIT) &&
377 !is_compat_task())) {
378 regs->psw.addr = __rewind_psw(regs->psw, UPROBE_SWBP_INSN_SIZE);
379 do_report_trap(regs, SIGILL, ILL_ILLADR, NULL);
382 if (probe_is_insn_relative_long(auprobe->insn)) {
383 handle_insn_ril(auprobe, regs);