2 * User-space Probes (UProbes) for s390
4 * Copyright IBM Corp. 2014
5 * Author(s): Jan Willeke,
8 #include <linux/uaccess.h>
9 #include <linux/uprobes.h>
10 #include <linux/compat.h>
11 #include <linux/kdebug.h>
12 #include <asm/switch_to.h>
13 #include <asm/facility.h>
14 #include <asm/kprobes.h>
18 #define UPROBE_TRAP_NR UINT_MAX
20 int arch_uprobe_analyze_insn(struct arch_uprobe
*auprobe
, struct mm_struct
*mm
,
23 return probe_is_prohibited_opcode(auprobe
->insn
);
26 int arch_uprobe_pre_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
28 if (psw_bits(regs
->psw
).eaba
== PSW_AMODE_24BIT
)
30 if (!is_compat_task() && psw_bits(regs
->psw
).eaba
== PSW_AMODE_31BIT
)
32 clear_pt_regs_flag(regs
, PIF_PER_TRAP
);
33 auprobe
->saved_per
= psw_bits(regs
->psw
).r
;
34 auprobe
->saved_int_code
= regs
->int_code
;
35 regs
->int_code
= UPROBE_TRAP_NR
;
36 regs
->psw
.addr
= current
->utask
->xol_vaddr
;
37 set_tsk_thread_flag(current
, TIF_UPROBE_SINGLESTEP
);
38 update_cr_regs(current
);
42 bool arch_uprobe_xol_was_trapped(struct task_struct
*tsk
)
44 struct pt_regs
*regs
= task_pt_regs(tsk
);
46 if (regs
->int_code
!= UPROBE_TRAP_NR
)
51 int arch_uprobe_post_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
53 int fixup
= probe_get_fixup_type(auprobe
->insn
);
54 struct uprobe_task
*utask
= current
->utask
;
56 clear_tsk_thread_flag(current
, TIF_UPROBE_SINGLESTEP
);
57 update_cr_regs(current
);
58 psw_bits(regs
->psw
).r
= auprobe
->saved_per
;
59 regs
->int_code
= auprobe
->saved_int_code
;
61 if (fixup
& FIXUP_PSW_NORMAL
)
62 regs
->psw
.addr
+= utask
->vaddr
- utask
->xol_vaddr
;
63 if (fixup
& FIXUP_RETURN_REGISTER
) {
64 int reg
= (auprobe
->insn
[0] & 0xf0) >> 4;
66 regs
->gprs
[reg
] += utask
->vaddr
- utask
->xol_vaddr
;
68 if (fixup
& FIXUP_BRANCH_NOT_TAKEN
) {
69 int ilen
= insn_length(auprobe
->insn
[0] >> 8);
71 if (regs
->psw
.addr
- utask
->xol_vaddr
== ilen
)
72 regs
->psw
.addr
= utask
->vaddr
+ ilen
;
74 /* If per tracing was active generate trap */
75 if (regs
->psw
.mask
& PSW_MASK_PER
)
80 int arch_uprobe_exception_notify(struct notifier_block
*self
, unsigned long val
,
83 struct die_args
*args
= data
;
84 struct pt_regs
*regs
= args
->regs
;
88 if (regs
->int_code
& 0x200) /* Trap during transaction */
92 if (uprobe_pre_sstep_notifier(regs
))
96 if (uprobe_post_sstep_notifier(regs
))
104 void arch_uprobe_abort_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
106 clear_thread_flag(TIF_UPROBE_SINGLESTEP
);
107 regs
->int_code
= auprobe
->saved_int_code
;
108 regs
->psw
.addr
= current
->utask
->vaddr
;
111 unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline
,
112 struct pt_regs
*regs
)
116 orig
= regs
->gprs
[14];
117 regs
->gprs
[14] = trampoline
;
121 /* Instruction Emulation */
123 static void adjust_psw_addr(psw_t
*psw
, unsigned long len
)
125 psw
->addr
= __rewind_psw(*psw
, -len
);
128 #define EMU_ILLEGAL_OP 1
129 #define EMU_SPECIFICATION 2
130 #define EMU_ADDRESSING 3
132 #define emu_load_ril(ptr, output) \
134 unsigned int mask = sizeof(*(ptr)) - 1; \
135 __typeof__(*(ptr)) input; \
138 if (!test_facility(34)) \
139 __rc = EMU_ILLEGAL_OP; \
140 else if ((u64 __force)ptr & mask) \
141 __rc = EMU_SPECIFICATION; \
142 else if (get_user(input, ptr)) \
143 __rc = EMU_ADDRESSING; \
149 #define emu_store_ril(ptr, input) \
151 unsigned int mask = sizeof(*(ptr)) - 1; \
154 if (!test_facility(34)) \
155 __rc = EMU_ILLEGAL_OP; \
156 else if ((u64 __force)ptr & mask) \
157 __rc = EMU_SPECIFICATION; \
158 else if (put_user(*(input), ptr)) \
159 __rc = EMU_ADDRESSING; \
163 #define emu_cmp_ril(regs, ptr, cmp) \
165 unsigned int mask = sizeof(*(ptr)) - 1; \
166 __typeof__(*(ptr)) input; \
169 if (!test_facility(34)) \
170 __rc = EMU_ILLEGAL_OP; \
171 else if ((u64 __force)ptr & mask) \
172 __rc = EMU_SPECIFICATION; \
173 else if (get_user(input, ptr)) \
174 __rc = EMU_ADDRESSING; \
175 else if (input > *(cmp)) \
176 psw_bits((regs)->psw).cc = 1; \
177 else if (input < *(cmp)) \
178 psw_bits((regs)->psw).cc = 2; \
180 psw_bits((regs)->psw).cc = 0; \
191 union split_register
{
201 * pc relative instructions are emulated, since parameters may not be
202 * accessible from the xol area due to range limitations.
204 static void handle_insn_ril(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
206 union split_register
*rx
;
207 struct insn_ril
*insn
;
212 insn
= (struct insn_ril
*) &auprobe
->insn
;
213 rx
= (union split_register
*) ®s
->gprs
[insn
->reg
];
214 uptr
= (void *)(regs
->psw
.addr
+ (insn
->disp
* 2));
215 ilen
= insn_length(insn
->opc0
);
217 switch (insn
->opc0
) {
219 switch (insn
->opc1
) {
220 case 0x00: /* larl */
221 rx
->u64
= (unsigned long)uptr
;
226 switch (insn
->opc1
) {
227 case 0x02: /* llhrl */
228 rc
= emu_load_ril((u16 __user
*)uptr
, &rx
->u32
[1]);
230 case 0x04: /* lghrl */
231 rc
= emu_load_ril((s16 __user
*)uptr
, &rx
->u64
);
233 case 0x05: /* lhrl */
234 rc
= emu_load_ril((s16 __user
*)uptr
, &rx
->u32
[1]);
236 case 0x06: /* llghrl */
237 rc
= emu_load_ril((u16 __user
*)uptr
, &rx
->u64
);
239 case 0x08: /* lgrl */
240 rc
= emu_load_ril((u64 __user
*)uptr
, &rx
->u64
);
242 case 0x0c: /* lgfrl */
243 rc
= emu_load_ril((s32 __user
*)uptr
, &rx
->u64
);
246 rc
= emu_load_ril((u32 __user
*)uptr
, &rx
->u32
[1]);
248 case 0x0e: /* llgfrl */
249 rc
= emu_load_ril((u32 __user
*)uptr
, &rx
->u64
);
251 case 0x07: /* sthrl */
252 rc
= emu_store_ril((u16 __user
*)uptr
, &rx
->u16
[3]);
254 case 0x0b: /* stgrl */
255 rc
= emu_store_ril((u64 __user
*)uptr
, &rx
->u64
);
257 case 0x0f: /* strl */
258 rc
= emu_store_ril((u32 __user
*)uptr
, &rx
->u32
[1]);
263 switch (insn
->opc1
) {
264 case 0x02: /* pfdrl */
265 if (!test_facility(34))
268 case 0x04: /* cghrl */
269 rc
= emu_cmp_ril(regs
, (s16 __user
*)uptr
, &rx
->s64
);
271 case 0x05: /* chrl */
272 rc
= emu_cmp_ril(regs
, (s16 __user
*)uptr
, &rx
->s32
[1]);
274 case 0x06: /* clghrl */
275 rc
= emu_cmp_ril(regs
, (u16 __user
*)uptr
, &rx
->u64
);
277 case 0x07: /* clhrl */
278 rc
= emu_cmp_ril(regs
, (u16 __user
*)uptr
, &rx
->u32
[1]);
280 case 0x08: /* cgrl */
281 rc
= emu_cmp_ril(regs
, (s64 __user
*)uptr
, &rx
->s64
);
283 case 0x0a: /* clgrl */
284 rc
= emu_cmp_ril(regs
, (u64 __user
*)uptr
, &rx
->u64
);
286 case 0x0c: /* cgfrl */
287 rc
= emu_cmp_ril(regs
, (s32 __user
*)uptr
, &rx
->s64
);
290 rc
= emu_cmp_ril(regs
, (s32 __user
*)uptr
, &rx
->s32
[1]);
292 case 0x0e: /* clgfrl */
293 rc
= emu_cmp_ril(regs
, (u32 __user
*)uptr
, &rx
->u64
);
295 case 0x0f: /* clrl */
296 rc
= emu_cmp_ril(regs
, (u32 __user
*)uptr
, &rx
->u32
[1]);
301 adjust_psw_addr(®s
->psw
, ilen
);
304 regs
->int_code
= ilen
<< 16 | 0x0001;
305 do_report_trap(regs
, SIGILL
, ILL_ILLOPC
, NULL
);
307 case EMU_SPECIFICATION
:
308 regs
->int_code
= ilen
<< 16 | 0x0006;
309 do_report_trap(regs
, SIGILL
, ILL_ILLOPC
, NULL
);
312 regs
->int_code
= ilen
<< 16 | 0x0005;
313 do_report_trap(regs
, SIGSEGV
, SEGV_MAPERR
, NULL
);
318 bool arch_uprobe_skip_sstep(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
320 if ((psw_bits(regs
->psw
).eaba
== PSW_AMODE_24BIT
) ||
321 ((psw_bits(regs
->psw
).eaba
== PSW_AMODE_31BIT
) &&
322 !is_compat_task())) {
323 regs
->psw
.addr
= __rewind_psw(regs
->psw
, UPROBE_SWBP_INSN_SIZE
);
324 do_report_trap(regs
, SIGILL
, ILL_ILLADR
, NULL
);
327 if (probe_is_insn_relative_long(auprobe
->insn
)) {
328 handle_insn_ril(auprobe
, regs
);