aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390/kernel/kprobes.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/s390/kernel/kprobes.c')
-rw-r--r--arch/s390/kernel/kprobes.c523
1 files changed, 276 insertions, 247 deletions
diff --git a/arch/s390/kernel/kprobes.c b/arch/s390/kernel/kprobes.c
index 2a3d2bf6f083..1d05d669107c 100644
--- a/arch/s390/kernel/kprobes.c
+++ b/arch/s390/kernel/kprobes.c
@@ -30,35 +30,16 @@
30#include <asm/sections.h> 30#include <asm/sections.h>
31#include <linux/module.h> 31#include <linux/module.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/hardirq.h>
33 34
34DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; 35DEFINE_PER_CPU(struct kprobe *, current_kprobe);
35DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 36DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
36 37
37struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; 38struct kretprobe_blackpoint kretprobe_blacklist[] = { };
38 39
39int __kprobes arch_prepare_kprobe(struct kprobe *p) 40static int __kprobes is_prohibited_opcode(kprobe_opcode_t *insn)
40{ 41{
41 /* Make sure the probe isn't going on a difficult instruction */ 42 switch (insn[0] >> 8) {
42 if (is_prohibited_opcode((kprobe_opcode_t *) p->addr))
43 return -EINVAL;
44
45 if ((unsigned long)p->addr & 0x01)
46 return -EINVAL;
47
48 /* Use the get_insn_slot() facility for correctness */
49 if (!(p->ainsn.insn = get_insn_slot()))
50 return -ENOMEM;
51
52 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
53
54 get_instruction_type(&p->ainsn);
55 p->opcode = *p->addr;
56 return 0;
57}
58
59int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
60{
61 switch (*(__u8 *) instruction) {
62 case 0x0c: /* bassm */ 43 case 0x0c: /* bassm */
63 case 0x0b: /* bsm */ 44 case 0x0b: /* bsm */
64 case 0x83: /* diag */ 45 case 0x83: /* diag */
@@ -67,7 +48,7 @@ int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
67 case 0xad: /* stosm */ 48 case 0xad: /* stosm */
68 return -EINVAL; 49 return -EINVAL;
69 } 50 }
70 switch (*(__u16 *) instruction) { 51 switch (insn[0]) {
71 case 0x0101: /* pr */ 52 case 0x0101: /* pr */
72 case 0xb25a: /* bsa */ 53 case 0xb25a: /* bsa */
73 case 0xb240: /* bakr */ 54 case 0xb240: /* bakr */
@@ -80,93 +61,92 @@ int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
80 return 0; 61 return 0;
81} 62}
82 63
83void __kprobes get_instruction_type(struct arch_specific_insn *ainsn) 64static int __kprobes get_fixup_type(kprobe_opcode_t *insn)
84{ 65{
85 /* default fixup method */ 66 /* default fixup method */
86 ainsn->fixup = FIXUP_PSW_NORMAL; 67 int fixup = FIXUP_PSW_NORMAL;
87
88 /* save r1 operand */
89 ainsn->reg = (*ainsn->insn & 0xf0) >> 4;
90
91 /* save the instruction length (pop 5-5) in bytes */
92 switch (*(__u8 *) (ainsn->insn) >> 6) {
93 case 0:
94 ainsn->ilen = 2;
95 break;
96 case 1:
97 case 2:
98 ainsn->ilen = 4;
99 break;
100 case 3:
101 ainsn->ilen = 6;
102 break;
103 }
104 68
105 switch (*(__u8 *) ainsn->insn) { 69 switch (insn[0] >> 8) {
106 case 0x05: /* balr */ 70 case 0x05: /* balr */
107 case 0x0d: /* basr */ 71 case 0x0d: /* basr */
108 ainsn->fixup = FIXUP_RETURN_REGISTER; 72 fixup = FIXUP_RETURN_REGISTER;
109 /* if r2 = 0, no branch will be taken */ 73 /* if r2 = 0, no branch will be taken */
110 if ((*ainsn->insn & 0x0f) == 0) 74 if ((insn[0] & 0x0f) == 0)
111 ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN; 75 fixup |= FIXUP_BRANCH_NOT_TAKEN;
112 break; 76 break;
113 case 0x06: /* bctr */ 77 case 0x06: /* bctr */
114 case 0x07: /* bcr */ 78 case 0x07: /* bcr */
115 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; 79 fixup = FIXUP_BRANCH_NOT_TAKEN;
116 break; 80 break;
117 case 0x45: /* bal */ 81 case 0x45: /* bal */
118 case 0x4d: /* bas */ 82 case 0x4d: /* bas */
119 ainsn->fixup = FIXUP_RETURN_REGISTER; 83 fixup = FIXUP_RETURN_REGISTER;
120 break; 84 break;
121 case 0x47: /* bc */ 85 case 0x47: /* bc */
122 case 0x46: /* bct */ 86 case 0x46: /* bct */
123 case 0x86: /* bxh */ 87 case 0x86: /* bxh */
124 case 0x87: /* bxle */ 88 case 0x87: /* bxle */
125 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; 89 fixup = FIXUP_BRANCH_NOT_TAKEN;
126 break; 90 break;
127 case 0x82: /* lpsw */ 91 case 0x82: /* lpsw */
128 ainsn->fixup = FIXUP_NOT_REQUIRED; 92 fixup = FIXUP_NOT_REQUIRED;
129 break; 93 break;
130 case 0xb2: /* lpswe */ 94 case 0xb2: /* lpswe */
131 if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) { 95 if ((insn[0] & 0xff) == 0xb2)
132 ainsn->fixup = FIXUP_NOT_REQUIRED; 96 fixup = FIXUP_NOT_REQUIRED;
133 }
134 break; 97 break;
135 case 0xa7: /* bras */ 98 case 0xa7: /* bras */
136 if ((*ainsn->insn & 0x0f) == 0x05) { 99 if ((insn[0] & 0x0f) == 0x05)
137 ainsn->fixup |= FIXUP_RETURN_REGISTER; 100 fixup |= FIXUP_RETURN_REGISTER;
138 }
139 break; 101 break;
140 case 0xc0: 102 case 0xc0:
141 if ((*ainsn->insn & 0x0f) == 0x00 /* larl */ 103 if ((insn[0] & 0x0f) == 0x00 || /* larl */
142 || (*ainsn->insn & 0x0f) == 0x05) /* brasl */ 104 (insn[0] & 0x0f) == 0x05) /* brasl */
143 ainsn->fixup |= FIXUP_RETURN_REGISTER; 105 fixup |= FIXUP_RETURN_REGISTER;
144 break; 106 break;
145 case 0xeb: 107 case 0xeb:
146 if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 || /* bxhg */ 108 if ((insn[2] & 0xff) == 0x44 || /* bxhg */
147 *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */ 109 (insn[2] & 0xff) == 0x45) /* bxleg */
148 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; 110 fixup = FIXUP_BRANCH_NOT_TAKEN;
149 }
150 break; 111 break;
151 case 0xe3: /* bctg */ 112 case 0xe3: /* bctg */
152 if (*(((__u8 *) ainsn->insn) + 5) == 0x46) { 113 if ((insn[2] & 0xff) == 0x46)
153 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; 114 fixup = FIXUP_BRANCH_NOT_TAKEN;
154 }
155 break; 115 break;
156 } 116 }
117 return fixup;
157} 118}
158 119
120int __kprobes arch_prepare_kprobe(struct kprobe *p)
121{
122 if ((unsigned long) p->addr & 0x01)
123 return -EINVAL;
124
125 /* Make sure the probe isn't going on a difficult instruction */
126 if (is_prohibited_opcode(p->addr))
127 return -EINVAL;
128
129 p->opcode = *p->addr;
130 memcpy(p->ainsn.insn, p->addr, ((p->opcode >> 14) + 3) & -2);
131
132 return 0;
133}
134
135struct ins_replace_args {
136 kprobe_opcode_t *ptr;
137 kprobe_opcode_t opcode;
138};
139
159static int __kprobes swap_instruction(void *aref) 140static int __kprobes swap_instruction(void *aref)
160{ 141{
161 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 142 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
162 unsigned long status = kcb->kprobe_status; 143 unsigned long status = kcb->kprobe_status;
163 struct ins_replace_args *args = aref; 144 struct ins_replace_args *args = aref;
164 int rc;
165 145
166 kcb->kprobe_status = KPROBE_SWAP_INST; 146 kcb->kprobe_status = KPROBE_SWAP_INST;
167 rc = probe_kernel_write(args->ptr, &args->new, sizeof(args->new)); 147 probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode));
168 kcb->kprobe_status = status; 148 kcb->kprobe_status = status;
169 return rc; 149 return 0;
170} 150}
171 151
172void __kprobes arch_arm_kprobe(struct kprobe *p) 152void __kprobes arch_arm_kprobe(struct kprobe *p)
@@ -174,8 +154,7 @@ void __kprobes arch_arm_kprobe(struct kprobe *p)
174 struct ins_replace_args args; 154 struct ins_replace_args args;
175 155
176 args.ptr = p->addr; 156 args.ptr = p->addr;
177 args.old = p->opcode; 157 args.opcode = BREAKPOINT_INSTRUCTION;
178 args.new = BREAKPOINT_INSTRUCTION;
179 stop_machine(swap_instruction, &args, NULL); 158 stop_machine(swap_instruction, &args, NULL);
180} 159}
181 160
@@ -184,64 +163,69 @@ void __kprobes arch_disarm_kprobe(struct kprobe *p)
184 struct ins_replace_args args; 163 struct ins_replace_args args;
185 164
186 args.ptr = p->addr; 165 args.ptr = p->addr;
187 args.old = BREAKPOINT_INSTRUCTION; 166 args.opcode = p->opcode;
188 args.new = p->opcode;
189 stop_machine(swap_instruction, &args, NULL); 167 stop_machine(swap_instruction, &args, NULL);
190} 168}
191 169
192void __kprobes arch_remove_kprobe(struct kprobe *p) 170void __kprobes arch_remove_kprobe(struct kprobe *p)
193{ 171{
194 if (p->ainsn.insn) {
195 free_insn_slot(p->ainsn.insn, 0);
196 p->ainsn.insn = NULL;
197 }
198} 172}
199 173
200static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) 174static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb,
175 struct pt_regs *regs,
176 unsigned long ip)
201{ 177{
202 per_cr_bits kprobe_per_regs[1]; 178 struct per_regs per_kprobe;
203 179
204 memset(kprobe_per_regs, 0, sizeof(per_cr_bits)); 180 /* Set up the PER control registers %cr9-%cr11 */
205 regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE; 181 per_kprobe.control = PER_EVENT_IFETCH;
182 per_kprobe.start = ip;
183 per_kprobe.end = ip;
206 184
207 /* Set up the per control reg info, will pass to lctl */ 185 /* Save control regs and psw mask */
208 kprobe_per_regs[0].em_instruction_fetch = 1; 186 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
209 kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn; 187 kcb->kprobe_saved_imask = regs->psw.mask &
210 kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1; 188 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
211 189
212 /* Set the PER control regs, turns on single step for this address */ 190 /* Set PER control regs, turns on single step for the given address */
213 __ctl_load(kprobe_per_regs, 9, 11); 191 __ctl_load(per_kprobe, 9, 11);
214 regs->psw.mask |= PSW_MASK_PER; 192 regs->psw.mask |= PSW_MASK_PER;
215 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK); 193 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
194 regs->psw.addr = ip | PSW_ADDR_AMODE;
216} 195}
217 196
218static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) 197static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
198 struct pt_regs *regs,
199 unsigned long ip)
219{ 200{
220 kcb->prev_kprobe.kp = kprobe_running(); 201 /* Restore control regs and psw mask, set new psw address */
221 kcb->prev_kprobe.status = kcb->kprobe_status; 202 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
222 kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask; 203 regs->psw.mask &= ~PSW_MASK_PER;
223 memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl, 204 regs->psw.mask |= kcb->kprobe_saved_imask;
224 sizeof(kcb->kprobe_saved_ctl)); 205 regs->psw.addr = ip | PSW_ADDR_AMODE;
225} 206}
226 207
227static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) 208/*
209 * Activate a kprobe by storing its pointer to current_kprobe. The
210 * previous kprobe is stored in kcb->prev_kprobe. A stack of up to
211 * two kprobes can be active, see KPROBE_REENTER.
212 */
213static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
228{ 214{
229 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; 215 kcb->prev_kprobe.kp = __get_cpu_var(current_kprobe);
230 kcb->kprobe_status = kcb->prev_kprobe.status; 216 kcb->prev_kprobe.status = kcb->kprobe_status;
231 kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask; 217 __get_cpu_var(current_kprobe) = p;
232 memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl,
233 sizeof(kcb->kprobe_saved_ctl));
234} 218}
235 219
236static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, 220/*
237 struct kprobe_ctlblk *kcb) 221 * Deactivate a kprobe by backing up to the previous state. If the
222 * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL,
223 * for any other state prev_kprobe.kp will be NULL.
224 */
225static void __kprobes pop_kprobe(struct kprobe_ctlblk *kcb)
238{ 226{
239 __get_cpu_var(current_kprobe) = p; 227 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
240 /* Save the interrupt and per flags */ 228 kcb->kprobe_status = kcb->prev_kprobe.status;
241 kcb->kprobe_saved_imask = regs->psw.mask &
242 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
243 /* Save the control regs that govern PER */
244 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
245} 229}
246 230
247void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, 231void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
@@ -250,79 +234,104 @@ void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
250 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; 234 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
251 235
252 /* Replace the return addr with trampoline addr */ 236 /* Replace the return addr with trampoline addr */
253 regs->gprs[14] = (unsigned long)&kretprobe_trampoline; 237 regs->gprs[14] = (unsigned long) &kretprobe_trampoline;
238}
239
240static void __kprobes kprobe_reenter_check(struct kprobe_ctlblk *kcb,
241 struct kprobe *p)
242{
243 switch (kcb->kprobe_status) {
244 case KPROBE_HIT_SSDONE:
245 case KPROBE_HIT_ACTIVE:
246 kprobes_inc_nmissed_count(p);
247 break;
248 case KPROBE_HIT_SS:
249 case KPROBE_REENTER:
250 default:
251 /*
252 * A kprobe on the code path to single step an instruction
253 * is a BUG. The code path resides in the .kprobes.text
254 * section and is executed with interrupts disabled.
255 */
256 printk(KERN_EMERG "Invalid kprobe detected at %p.\n", p->addr);
257 dump_kprobe(p);
258 BUG();
259 }
254} 260}
255 261
256static int __kprobes kprobe_handler(struct pt_regs *regs) 262static int __kprobes kprobe_handler(struct pt_regs *regs)
257{ 263{
258 struct kprobe *p;
259 int ret = 0;
260 unsigned long *addr = (unsigned long *)
261 ((regs->psw.addr & PSW_ADDR_INSN) - 2);
262 struct kprobe_ctlblk *kcb; 264 struct kprobe_ctlblk *kcb;
265 struct kprobe *p;
263 266
264 /* 267 /*
265 * We don't want to be preempted for the entire 268 * We want to disable preemption for the entire duration of kprobe
266 * duration of kprobe processing 269 * processing. That includes the calls to the pre/post handlers
270 * and single stepping the kprobe instruction.
267 */ 271 */
268 preempt_disable(); 272 preempt_disable();
269 kcb = get_kprobe_ctlblk(); 273 kcb = get_kprobe_ctlblk();
274 p = get_kprobe((void *)((regs->psw.addr & PSW_ADDR_INSN) - 2));
270 275
271 /* Check we're not actually recursing */ 276 if (p) {
272 if (kprobe_running()) { 277 if (kprobe_running()) {
273 p = get_kprobe(addr); 278 /*
274 if (p) { 279 * We have hit a kprobe while another is still
275 if (kcb->kprobe_status == KPROBE_HIT_SS && 280 * active. This can happen in the pre and post
276 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { 281 * handler. Single step the instruction of the
277 regs->psw.mask &= ~PSW_MASK_PER; 282 * new probe but do not call any handler function
278 regs->psw.mask |= kcb->kprobe_saved_imask; 283 * of this secondary kprobe.
279 goto no_kprobe; 284 * push_kprobe and pop_kprobe saves and restores
280 } 285 * the currently active kprobe.
281 /* We have reentered the kprobe_handler(), since
282 * another probe was hit while within the handler.
283 * We here save the original kprobes variables and
284 * just single step on the instruction of the new probe
285 * without calling any user handlers.
286 */ 286 */
287 save_previous_kprobe(kcb); 287 kprobe_reenter_check(kcb, p);
288 set_current_kprobe(p, regs, kcb); 288 push_kprobe(kcb, p);
289 kprobes_inc_nmissed_count(p);
290 prepare_singlestep(p, regs);
291 kcb->kprobe_status = KPROBE_REENTER; 289 kcb->kprobe_status = KPROBE_REENTER;
292 return 1;
293 } else { 290 } else {
294 p = __get_cpu_var(current_kprobe); 291 /*
295 if (p->break_handler && p->break_handler(p, regs)) { 292 * If we have no pre-handler or it returned 0, we
296 goto ss_probe; 293 * continue with single stepping. If we have a
297 } 294 * pre-handler and it returned non-zero, it prepped
295 * for calling the break_handler below on re-entry
296 * for jprobe processing, so get out doing nothing
297 * more here.
298 */
299 push_kprobe(kcb, p);
300 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
301 if (p->pre_handler && p->pre_handler(p, regs))
302 return 1;
303 kcb->kprobe_status = KPROBE_HIT_SS;
298 } 304 }
299 goto no_kprobe; 305 enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
300 }
301
302 p = get_kprobe(addr);
303 if (!p)
304 /*
305 * No kprobe at this address. The fault has not been
306 * caused by a kprobe breakpoint. The race of breakpoint
307 * vs. kprobe remove does not exist because on s390 we
308 * use stop_machine to arm/disarm the breakpoints.
309 */
310 goto no_kprobe;
311
312 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
313 set_current_kprobe(p, regs, kcb);
314 if (p->pre_handler && p->pre_handler(p, regs))
315 /* handler has already set things up, so skip ss setup */
316 return 1; 306 return 1;
317 307 } else if (kprobe_running()) {
318ss_probe: 308 p = __get_cpu_var(current_kprobe);
319 prepare_singlestep(p, regs); 309 if (p->break_handler && p->break_handler(p, regs)) {
320 kcb->kprobe_status = KPROBE_HIT_SS; 310 /*
321 return 1; 311 * Continuation after the jprobe completed and
322 312 * caused the jprobe_return trap. The jprobe
323no_kprobe: 313 * break_handler "returns" to the original
314 * function that still has the kprobe breakpoint
315 * installed. We continue with single stepping.
316 */
317 kcb->kprobe_status = KPROBE_HIT_SS;
318 enable_singlestep(kcb, regs,
319 (unsigned long) p->ainsn.insn);
320 return 1;
321 } /* else:
322 * No kprobe at this address and the current kprobe
323 * has no break handler (no jprobe!). The kernel just
324 * exploded, let the standard trap handler pick up the
325 * pieces.
326 */
327 } /* else:
328 * No kprobe at this address and no active kprobe. The trap has
329 * not been caused by a kprobe breakpoint. The race of breakpoint
330 * vs. kprobe remove does not exist because on s390 as we use
331 * stop_machine to arm/disarm the breakpoints.
332 */
324 preempt_enable_no_resched(); 333 preempt_enable_no_resched();
325 return ret; 334 return 0;
326} 335}
327 336
328/* 337/*
@@ -343,11 +352,12 @@ static void __used kretprobe_trampoline_holder(void)
343static int __kprobes trampoline_probe_handler(struct kprobe *p, 352static int __kprobes trampoline_probe_handler(struct kprobe *p,
344 struct pt_regs *regs) 353 struct pt_regs *regs)
345{ 354{
346 struct kretprobe_instance *ri = NULL; 355 struct kretprobe_instance *ri;
347 struct hlist_head *head, empty_rp; 356 struct hlist_head *head, empty_rp;
348 struct hlist_node *node, *tmp; 357 struct hlist_node *node, *tmp;
349 unsigned long flags, orig_ret_address = 0; 358 unsigned long flags, orig_ret_address;
350 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; 359 unsigned long trampoline_address;
360 kprobe_opcode_t *correct_ret_addr;
351 361
352 INIT_HLIST_HEAD(&empty_rp); 362 INIT_HLIST_HEAD(&empty_rp);
353 kretprobe_hash_lock(current, &head, &flags); 363 kretprobe_hash_lock(current, &head, &flags);
@@ -365,30 +375,55 @@ static int __kprobes trampoline_probe_handler(struct kprobe *p,
365 * real return address, and all the rest will point to 375 * real return address, and all the rest will point to
366 * kretprobe_trampoline 376 * kretprobe_trampoline
367 */ 377 */
378 ri = NULL;
379 orig_ret_address = 0;
380 correct_ret_addr = NULL;
381 trampoline_address = (unsigned long) &kretprobe_trampoline;
368 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { 382 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
369 if (ri->task != current) 383 if (ri->task != current)
370 /* another task is sharing our hash bucket */ 384 /* another task is sharing our hash bucket */
371 continue; 385 continue;
372 386
373 if (ri->rp && ri->rp->handler) 387 orig_ret_address = (unsigned long) ri->ret_addr;
388
389 if (orig_ret_address != trampoline_address)
390 /*
391 * This is the real return address. Any other
392 * instances associated with this task are for
393 * other calls deeper on the call stack
394 */
395 break;
396 }
397
398 kretprobe_assert(ri, orig_ret_address, trampoline_address);
399
400 correct_ret_addr = ri->ret_addr;
401 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
402 if (ri->task != current)
403 /* another task is sharing our hash bucket */
404 continue;
405
406 orig_ret_address = (unsigned long) ri->ret_addr;
407
408 if (ri->rp && ri->rp->handler) {
409 ri->ret_addr = correct_ret_addr;
374 ri->rp->handler(ri, regs); 410 ri->rp->handler(ri, regs);
411 }
375 412
376 orig_ret_address = (unsigned long)ri->ret_addr;
377 recycle_rp_inst(ri, &empty_rp); 413 recycle_rp_inst(ri, &empty_rp);
378 414
379 if (orig_ret_address != trampoline_address) { 415 if (orig_ret_address != trampoline_address)
380 /* 416 /*
381 * This is the real return address. Any other 417 * This is the real return address. Any other
382 * instances associated with this task are for 418 * instances associated with this task are for
383 * other calls deeper on the call stack 419 * other calls deeper on the call stack
384 */ 420 */
385 break; 421 break;
386 }
387 } 422 }
388 kretprobe_assert(ri, orig_ret_address, trampoline_address); 423
389 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE; 424 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
390 425
391 reset_current_kprobe(); 426 pop_kprobe(get_kprobe_ctlblk());
392 kretprobe_hash_unlock(current, &flags); 427 kretprobe_hash_unlock(current, &flags);
393 preempt_enable_no_resched(); 428 preempt_enable_no_resched();
394 429
@@ -415,55 +450,42 @@ static int __kprobes trampoline_probe_handler(struct kprobe *p,
415static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) 450static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
416{ 451{
417 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 452 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
453 unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
454 int fixup = get_fixup_type(p->ainsn.insn);
418 455
419 regs->psw.addr &= PSW_ADDR_INSN; 456 if (fixup & FIXUP_PSW_NORMAL)
420 457 ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
421 if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
422 regs->psw.addr = (unsigned long)p->addr +
423 ((unsigned long)regs->psw.addr -
424 (unsigned long)p->ainsn.insn);
425 458
426 if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN) 459 if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
427 if ((unsigned long)regs->psw.addr - 460 int ilen = ((p->ainsn.insn[0] >> 14) + 3) & -2;
428 (unsigned long)p->ainsn.insn == p->ainsn.ilen) 461 if (ip - (unsigned long) p->ainsn.insn == ilen)
429 regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen; 462 ip = (unsigned long) p->addr + ilen;
463 }
430 464
431 if (p->ainsn.fixup & FIXUP_RETURN_REGISTER) 465 if (fixup & FIXUP_RETURN_REGISTER) {
432 regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr + 466 int reg = (p->ainsn.insn[0] & 0xf0) >> 4;
433 (regs->gprs[p->ainsn.reg] - 467 regs->gprs[reg] += (unsigned long) p->addr -
434 (unsigned long)p->ainsn.insn)) 468 (unsigned long) p->ainsn.insn;
435 | PSW_ADDR_AMODE; 469 }
436 470
437 regs->psw.addr |= PSW_ADDR_AMODE; 471 disable_singlestep(kcb, regs, ip);
438 /* turn off PER mode */
439 regs->psw.mask &= ~PSW_MASK_PER;
440 /* Restore the original per control regs */
441 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
442 regs->psw.mask |= kcb->kprobe_saved_imask;
443} 472}
444 473
445static int __kprobes post_kprobe_handler(struct pt_regs *regs) 474static int __kprobes post_kprobe_handler(struct pt_regs *regs)
446{ 475{
447 struct kprobe *cur = kprobe_running();
448 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 476 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
477 struct kprobe *p = kprobe_running();
449 478
450 if (!cur) 479 if (!p)
451 return 0; 480 return 0;
452 481
453 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { 482 if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) {
454 kcb->kprobe_status = KPROBE_HIT_SSDONE; 483 kcb->kprobe_status = KPROBE_HIT_SSDONE;
455 cur->post_handler(cur, regs, 0); 484 p->post_handler(p, regs, 0);
456 } 485 }
457 486
458 resume_execution(cur, regs); 487 resume_execution(p, regs);
459 488 pop_kprobe(kcb);
460 /*Restore back the original saved kprobes variables and continue. */
461 if (kcb->kprobe_status == KPROBE_REENTER) {
462 restore_previous_kprobe(kcb);
463 goto out;
464 }
465 reset_current_kprobe();
466out:
467 preempt_enable_no_resched(); 489 preempt_enable_no_resched();
468 490
469 /* 491 /*
@@ -471,17 +493,16 @@ out:
471 * will have PER set, in which case, continue the remaining processing 493 * will have PER set, in which case, continue the remaining processing
472 * of do_single_step, as if this is not a probe hit. 494 * of do_single_step, as if this is not a probe hit.
473 */ 495 */
474 if (regs->psw.mask & PSW_MASK_PER) { 496 if (regs->psw.mask & PSW_MASK_PER)
475 return 0; 497 return 0;
476 }
477 498
478 return 1; 499 return 1;
479} 500}
480 501
481int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) 502static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
482{ 503{
483 struct kprobe *cur = kprobe_running();
484 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 504 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
505 struct kprobe *p = kprobe_running();
485 const struct exception_table_entry *entry; 506 const struct exception_table_entry *entry;
486 507
487 switch(kcb->kprobe_status) { 508 switch(kcb->kprobe_status) {
@@ -497,13 +518,8 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
497 * and allow the page fault handler to continue as a 518 * and allow the page fault handler to continue as a
498 * normal page fault. 519 * normal page fault.
499 */ 520 */
500 regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE; 521 disable_singlestep(kcb, regs, (unsigned long) p->addr);
501 regs->psw.mask &= ~PSW_MASK_PER; 522 pop_kprobe(kcb);
502 regs->psw.mask |= kcb->kprobe_saved_imask;
503 if (kcb->kprobe_status == KPROBE_REENTER)
504 restore_previous_kprobe(kcb);
505 else
506 reset_current_kprobe();
507 preempt_enable_no_resched(); 523 preempt_enable_no_resched();
508 break; 524 break;
509 case KPROBE_HIT_ACTIVE: 525 case KPROBE_HIT_ACTIVE:
@@ -513,7 +529,7 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
513 * we can also use npre/npostfault count for accouting 529 * we can also use npre/npostfault count for accouting
514 * these specific fault cases. 530 * these specific fault cases.
515 */ 531 */
516 kprobes_inc_nmissed_count(cur); 532 kprobes_inc_nmissed_count(p);
517 533
518 /* 534 /*
519 * We come here because instructions in the pre/post 535 * We come here because instructions in the pre/post
@@ -522,7 +538,7 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
522 * copy_from_user(), get_user() etc. Let the 538 * copy_from_user(), get_user() etc. Let the
523 * user-specified handler try to fix it first. 539 * user-specified handler try to fix it first.
524 */ 540 */
525 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) 541 if (p->fault_handler && p->fault_handler(p, regs, trapnr))
526 return 1; 542 return 1;
527 543
528 /* 544 /*
@@ -546,57 +562,71 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
546 return 0; 562 return 0;
547} 563}
548 564
565int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
566{
567 int ret;
568
569 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
570 local_irq_disable();
571 ret = kprobe_trap_handler(regs, trapnr);
572 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
573 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
574 return ret;
575}
576
549/* 577/*
550 * Wrapper routine to for handling exceptions. 578 * Wrapper routine to for handling exceptions.
551 */ 579 */
552int __kprobes kprobe_exceptions_notify(struct notifier_block *self, 580int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
553 unsigned long val, void *data) 581 unsigned long val, void *data)
554{ 582{
555 struct die_args *args = (struct die_args *)data; 583 struct die_args *args = (struct die_args *) data;
584 struct pt_regs *regs = args->regs;
556 int ret = NOTIFY_DONE; 585 int ret = NOTIFY_DONE;
557 586
587 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
588 local_irq_disable();
589
558 switch (val) { 590 switch (val) {
559 case DIE_BPT: 591 case DIE_BPT:
560 if (kprobe_handler(args->regs)) 592 if (kprobe_handler(regs))
561 ret = NOTIFY_STOP; 593 ret = NOTIFY_STOP;
562 break; 594 break;
563 case DIE_SSTEP: 595 case DIE_SSTEP:
564 if (post_kprobe_handler(args->regs)) 596 if (post_kprobe_handler(regs))
565 ret = NOTIFY_STOP; 597 ret = NOTIFY_STOP;
566 break; 598 break;
567 case DIE_TRAP: 599 case DIE_TRAP:
568 /* kprobe_running() needs smp_processor_id() */ 600 if (!preemptible() && kprobe_running() &&
569 preempt_disable(); 601 kprobe_trap_handler(regs, args->trapnr))
570 if (kprobe_running() &&
571 kprobe_fault_handler(args->regs, args->trapnr))
572 ret = NOTIFY_STOP; 602 ret = NOTIFY_STOP;
573 preempt_enable();
574 break; 603 break;
575 default: 604 default:
576 break; 605 break;
577 } 606 }
607
608 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
609 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
610
578 return ret; 611 return ret;
579} 612}
580 613
581int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) 614int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
582{ 615{
583 struct jprobe *jp = container_of(p, struct jprobe, kp); 616 struct jprobe *jp = container_of(p, struct jprobe, kp);
584 unsigned long addr;
585 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 617 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
618 unsigned long stack;
586 619
587 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs)); 620 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
588 621
589 /* setup return addr to the jprobe handler routine */ 622 /* setup return addr to the jprobe handler routine */
590 regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE; 623 regs->psw.addr = (unsigned long) jp->entry | PSW_ADDR_AMODE;
624 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
591 625
592 /* r14 is the function return address */
593 kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
594 /* r15 is the stack pointer */ 626 /* r15 is the stack pointer */
595 kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15]; 627 stack = (unsigned long) regs->gprs[15];
596 addr = (unsigned long)kcb->jprobe_saved_r15;
597 628
598 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr, 629 memcpy(kcb->jprobes_stack, (void *) stack, MIN_STACK_SIZE(stack));
599 MIN_STACK_SIZE(addr));
600 return 1; 630 return 1;
601} 631}
602 632
@@ -613,30 +643,29 @@ void __kprobes jprobe_return_end(void)
613int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) 643int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
614{ 644{
615 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 645 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
616 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15); 646 unsigned long stack;
647
648 stack = (unsigned long) kcb->jprobe_saved_regs.gprs[15];
617 649
618 /* Put the regs back */ 650 /* Put the regs back */
619 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs)); 651 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
620 /* put the stack back */ 652 /* put the stack back */
621 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, 653 memcpy((void *) stack, kcb->jprobes_stack, MIN_STACK_SIZE(stack));
622 MIN_STACK_SIZE(stack_addr));
623 preempt_enable_no_resched(); 654 preempt_enable_no_resched();
624 return 1; 655 return 1;
625} 656}
626 657
627static struct kprobe trampoline_p = { 658static struct kprobe trampoline = {
628 .addr = (kprobe_opcode_t *) & kretprobe_trampoline, 659 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
629 .pre_handler = trampoline_probe_handler 660 .pre_handler = trampoline_probe_handler
630}; 661};
631 662
632int __init arch_init_kprobes(void) 663int __init arch_init_kprobes(void)
633{ 664{
634 return register_kprobe(&trampoline_p); 665 return register_kprobe(&trampoline);
635} 666}
636 667
637int __kprobes arch_trampoline_kprobe(struct kprobe *p) 668int __kprobes arch_trampoline_kprobe(struct kprobe *p)
638{ 669{
639 if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline) 670 return p->addr == (kprobe_opcode_t *) &kretprobe_trampoline;
640 return 1;
641 return 0;
642} 671}