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