ac50fc839b2fea320dca2153ed7eee35da85662a
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / kernel / swap / kprobe / arch / x86 / swap-asm / swap_kprobes.c
1 /**
2  * arch/asm-x86/swap_kprobes.c
3  * @author Alexey Gerenkov <a.gerenkov@samsung.com> User-Space Probes initial implementation;
4  * Support x86/ARM/MIPS for both user and kernel spaces.
5  * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
6  * @author Stanislav Andreev <s.andreev@samsung.com>: added time debug profiling support; BUG() message fix
7  *
8  * @section LICENSE
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  * @section COPYRIGHT
25  *
26  * Copyright (C) IBM Corporation, 2002, 2004
27  *
28  * @section DESCRIPTION
29  *
30  * SWAP krpobes arch-dependend part for x86.
31  */
32
33 #include<linux/module.h>
34 #include <linux/kdebug.h>
35
36 #include "swap_kprobes.h"
37 #include <kprobe/swap_kprobes.h>
38
39 #include <kprobe/swap_kdebug.h>
40 #include <kprobe/swap_slots.h>
41 #include <kprobe/swap_kprobes_deps.h>
42 #define SUPRESS_BUG_MESSAGES                    /**< Debug-off definition. */
43
44
45 static int (*swap_fixup_exception)(struct pt_regs *regs);
46 static void *(*swap_text_poke)(void *addr, const void *opcode, size_t len);
47 static void (*swap_show_registers)(struct pt_regs *regs);
48
49
50 /** Stack address. */
51 #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
52
53
54 #define SWAP_SAVE_REGS_STRING                   \
55         /* Skip cs, ip, orig_ax and gs. */      \
56         "subl $16, %esp\n"                      \
57         "pushl %fs\n"                           \
58         "pushl %es\n"                           \
59         "pushl %ds\n"                           \
60         "pushl %eax\n"                          \
61         "pushl %ebp\n"                          \
62         "pushl %edi\n"                          \
63         "pushl %esi\n"                          \
64         "pushl %edx\n"                          \
65         "pushl %ecx\n"                          \
66         "pushl %ebx\n"
67 #define SWAP_RESTORE_REGS_STRING                \
68         "popl %ebx\n"                           \
69         "popl %ecx\n"                           \
70         "popl %edx\n"                           \
71         "popl %esi\n"                           \
72         "popl %edi\n"                           \
73         "popl %ebp\n"                           \
74         "popl %eax\n"                           \
75         /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
76         "addl $24, %esp\n"
77
78
79 /*
80  * Function return probe trampoline:
81  *      - init_kprobes() establishes a probepoint here
82  *      - When the probed function returns, this probe
83  *        causes the handlers to fire
84  */
85 __asm(
86         ".global swap_kretprobe_trampoline\n"
87         "swap_kretprobe_trampoline:\n"
88         "pushf\n"
89         SWAP_SAVE_REGS_STRING
90         "movl %esp, %eax\n"
91         "call trampoline_probe_handler_x86\n"
92         /* move eflags to cs */
93         "movl 56(%esp), %edx\n"
94         "movl %edx, 52(%esp)\n"
95         /* replace saved flags with true return address. */
96         "movl %eax, 56(%esp)\n"
97         SWAP_RESTORE_REGS_STRING
98         "popf\n"
99         "ret\n"
100 );
101
102 /* insert a jmp code */
103 static __always_inline void set_jmp_op(void *from, void *to)
104 {
105         struct __arch_jmp_op {
106                 char op;
107                 long raddr;
108         } __packed * jop;
109         jop = (struct __arch_jmp_op *) from;
110         jop->raddr = (long) (to) - ((long) (from) + 5);
111         jop->op = RELATIVEJUMP_INSTRUCTION;
112 }
113
114 /**
115  * @brief Check if opcode can be boosted.
116  *
117  * @param opcodes Opcode to check.
118  * @return Non-zero if opcode can be boosted.
119  */
120 int swap_can_boost(kprobe_opcode_t *opcodes)
121 {
122 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf) \
123         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
124           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
125           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
126           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
127          << (row % 32))
128         /*
129          * Undefined/reserved opcodes, conditional jump, Opcode Extension
130          * Groups, and some special opcodes can not be boost.
131          */
132         static const unsigned long twobyte_is_boostable[256 / 32] = {
133                 /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
134                 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) |
135                 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
136                 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
137                 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
138                 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) |
139                 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
140                 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) |
141                 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),
142                 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
143                 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
144                 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |
145                 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),
146                 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) |
147                 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1),
148                 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |
149                 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)
150                 /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
151
152         };
153 #undef W
154         kprobe_opcode_t opcode;
155         kprobe_opcode_t *orig_opcodes = opcodes;
156 retry:
157         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
158                 return 0;
159         opcode = *(opcodes++);
160
161         /* 2nd-byte opcode */
162         if (opcode == 0x0f) {
163                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
164                         return 0;
165                 return test_bit(*opcodes, twobyte_is_boostable);
166         }
167
168         switch (opcode & 0xf0) {
169         case 0x60:
170                 if (0x63 < opcode && opcode < 0x67)
171                         goto retry;     /* prefixes */
172                 /* can't boost Address-size override and bound */
173                 return (opcode != 0x62 && opcode != 0x67);
174         case 0x70:
175                 return 0;       /* can't boost conditional jump */
176         case 0xc0:
177                 /* can't boost software-interruptions */
178                 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
179         case 0xd0:
180                 /* can boost AA* and XLAT */
181                 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
182         case 0xe0:
183                 /* can boost in/out and absolute jmps */
184                 return ((opcode & 0x04) || opcode == 0xea);
185         case 0xf0:
186                 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
187                         goto retry;     /* lock/rep(ne) prefix */
188                 /* clear and set flags can be boost */
189                 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
190         default:
191                 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
192                         goto retry;     /* prefixes */
193                 /* can't boost CS override and call */
194                 return (opcode != 0x2e && opcode != 0x9a);
195         }
196 }
197 EXPORT_SYMBOL_GPL(swap_can_boost);
198
199 /*
200  * returns non-zero if opcode modifies the interrupt flag.
201  */
202 static int is_IF_modifier(kprobe_opcode_t opcode)
203 {
204         switch (opcode) {
205         case 0xfa:              /* cli */
206         case 0xfb:              /* sti */
207         case 0xcf:              /* iret/iretd */
208         case 0x9d:              /* popf/popfd */
209                 return 1;
210         }
211         return 0;
212 }
213
214 /**
215  * @brief Creates trampoline for kprobe.
216  *
217  * @param p Pointer to kprobe.
218  * @param sm Pointer to slot manager
219  * @return 0 on success, error code on error.
220  */
221 int swap_arch_prepare_kprobe(struct kprobe *p, struct slot_manager *sm)
222 {
223         /* insn: must be on special executable page on i386. */
224         p->ainsn.insn = swap_slot_alloc(sm);
225         if (p->ainsn.insn == NULL)
226                 return -ENOMEM;
227
228         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
229
230         p->opcode = *p->addr;
231         p->ainsn.boostable = swap_can_boost(p->addr) ? 0 : -1;
232
233         return 0;
234 }
235
236 /**
237  * @brief Prepares singlestep for current CPU.
238  *
239  * @param p Pointer to kprobe.
240  * @param regs Pointer to CPU registers data.
241  * @return Void.
242  */
243 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
244 {
245         int cpu = smp_processor_id();
246
247         if (p->ss_addr[cpu]) {
248                 regs->EREG(ip) = (unsigned long)p->ss_addr[cpu];
249                 p->ss_addr[cpu] = NULL;
250         } else {
251                 regs->EREG(flags) |= TF_MASK;
252                 regs->EREG(flags) &= ~IF_MASK;
253                 /* single step inline if the instruction is an int3 */
254                 if (p->opcode == BREAKPOINT_INSTRUCTION) {
255                         regs->EREG(ip) = (unsigned long) p->addr;
256                         /* printk(KERN_INFO "break_insn!!!\n"); */
257                 } else
258                         regs->EREG(ip) = (unsigned long) p->ainsn.insn;
259         }
260 }
261 EXPORT_SYMBOL_GPL(prepare_singlestep);
262
263 /**
264  * @brief Saves previous kprobe.
265  *
266  * @param kcb Pointer to kprobe_ctlblk struct whereto save current kprobe.
267  * @param p_run Pointer to kprobe.
268  * @return Void.
269  */
270 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *cur_p)
271 {
272         if (kcb->prev_kprobe.kp != NULL) {
273                 panic("no space to save new probe[]: "
274                       "task = %d/%s, prev %p, current %p, new %p,",
275                       current->pid, current->comm, kcb->prev_kprobe.kp->addr,
276                       swap_kprobe_running()->addr, cur_p->addr);
277         }
278
279
280         kcb->prev_kprobe.kp = swap_kprobe_running();
281         kcb->prev_kprobe.status = kcb->kprobe_status;
282
283 }
284
285 /**
286  * @brief Restores previous kprobe.
287  *
288  * @param kcb Pointer to kprobe_ctlblk which contains previous kprobe.
289  * @return Void.
290  */
291 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
292 {
293         __get_cpu_var(swap_current_kprobe) = kcb->prev_kprobe.kp;
294         kcb->kprobe_status = kcb->prev_kprobe.status;
295         kcb->prev_kprobe.kp = NULL;
296         kcb->prev_kprobe.status = 0;
297 }
298
299 /**
300  * @brief Sets currently running kprobe.
301  *
302  * @param p Pointer to currently running kprobe.
303  * @param regs Pointer to CPU registers data.
304  * @param kcb Pointer to kprobe_ctlblk.
305  * @return Void.
306  */
307 void set_current_kprobe(struct kprobe *p,
308                         struct pt_regs *regs,
309                         struct kprobe_ctlblk *kcb)
310 {
311         __get_cpu_var(swap_current_kprobe) = p;
312         DBPRINTF("set_current_kprobe[]: p=%p addr=%p\n", p, p->addr);
313         kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags =
314                 (regs->EREG(flags) & (TF_MASK | IF_MASK));
315         if (is_IF_modifier(p->opcode))
316                 kcb->kprobe_saved_eflags &= ~IF_MASK;
317 }
318
319 static int setup_singlestep(struct kprobe *p, struct pt_regs *regs,
320                             struct kprobe_ctlblk *kcb)
321 {
322 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
323         if (p->ainsn.boostable == 1 && !p->post_handler) {
324                 /* Boost up -- we can execute copied instructions directly */
325                 swap_reset_current_kprobe();
326                 regs->ip = (unsigned long)p->ainsn.insn;
327                 swap_preempt_enable_no_resched();
328
329                 return 1;
330         }
331 #endif /* !CONFIG_PREEMPT */
332
333         prepare_singlestep(p, regs);
334         kcb->kprobe_status = KPROBE_HIT_SS;
335
336         return 1;
337 }
338
339 static int __kprobe_handler(struct pt_regs *regs)
340 {
341         struct kprobe *p = 0;
342         int ret = 0, reenter = 0;
343         kprobe_opcode_t *addr = NULL;
344         struct kprobe_ctlblk *kcb;
345
346         addr = (kprobe_opcode_t *) (regs->EREG(ip) - sizeof(kprobe_opcode_t));
347
348         preempt_disable();
349
350         kcb = swap_get_kprobe_ctlblk();
351         p = swap_get_kprobe(addr);
352
353         /* Check we're not actually recursing */
354         if (swap_kprobe_running()) {
355                 if (p) {
356                         if (kcb->kprobe_status == KPROBE_HIT_SS &&
357                             *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
358                                 regs->EREG(flags) &= ~TF_MASK;
359                                 regs->EREG(flags) |= kcb->kprobe_saved_eflags;
360                                 goto no_kprobe;
361                         }
362
363
364                         /* We have reentered the kprobe_handler(), since
365                          * another probe was hit while within the handler.
366                          * We here save the original kprobes variables and
367                          * just single step on the instruction of the new probe
368                          * without calling any user handlers.
369                          */
370                         save_previous_kprobe(kcb, p);
371                         set_current_kprobe(p, regs, kcb);
372                         swap_kprobes_inc_nmissed_count(p);
373                         prepare_singlestep(p, regs);
374                         kcb->kprobe_status = KPROBE_REENTER;
375
376                         return 1;
377                 } else {
378                         if (*addr != BREAKPOINT_INSTRUCTION) {
379                                 /* The breakpoint instruction was removed by
380                                  * another cpu right after we hit, no further
381                                  * handling of this interrupt is appropriate
382                                  */
383                                 regs->EREG(ip) -= sizeof(kprobe_opcode_t);
384                                 ret = 1;
385                                 goto no_kprobe;
386                         }
387
388                         p = __get_cpu_var(swap_current_kprobe);
389                         if (p->break_handler && p->break_handler(p, regs))
390                                 goto ss_probe;
391
392                         goto no_kprobe;
393                 }
394         }
395
396         if (!p) {
397                 if (*addr != BREAKPOINT_INSTRUCTION) {
398                         /*
399                          * The breakpoint instruction was removed right
400                          * after we hit it.  Another cpu has removed
401                          * either a probepoint or a debugger breakpoint
402                          * at this address.  In either case, no further
403                          * handling of this interrupt is appropriate.
404                          * Back up over the (now missing) int3 and run
405                          * the original instruction.
406                          */
407                         regs->EREG(ip) -= sizeof(kprobe_opcode_t);
408                         ret = 1;
409                 }
410
411                 if (!p) {
412                         /* Not one of ours: let kernel handle it */
413                         DBPRINTF("no_kprobe");
414                         goto no_kprobe;
415                 }
416         }
417
418         set_current_kprobe(p, regs, kcb);
419
420         if (!reenter)
421                 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
422
423         if (p->pre_handler) {
424                 ret = p->pre_handler(p, regs);
425                 if (ret)
426                         return ret;
427         }
428
429 ss_probe:
430         setup_singlestep(p, regs, kcb);
431
432         return 1;
433
434 no_kprobe:
435         swap_preempt_enable_no_resched();
436
437         return ret;
438 }
439
440 static int kprobe_handler(struct pt_regs *regs)
441 {
442         int ret;
443 #ifdef SUPRESS_BUG_MESSAGES
444         int swap_oops_in_progress;
445         /*
446          * oops_in_progress used to avoid BUG() messages
447          * that slow down kprobe_handler() execution
448          */
449         swap_oops_in_progress = oops_in_progress;
450         oops_in_progress = 1;
451 #endif
452
453         ret = __kprobe_handler(regs);
454
455 #ifdef SUPRESS_BUG_MESSAGES
456         oops_in_progress = swap_oops_in_progress;
457 #endif
458
459         return ret;
460 }
461
462 /**
463  * @brief Probe pre handler.
464  *
465  * @param p Pointer to fired kprobe.
466  * @param regs Pointer to CPU registers data.
467  * @return 0.
468  */
469 int swap_setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
470 {
471         struct jprobe *jp = container_of(p, struct jprobe, kp);
472         kprobe_pre_entry_handler_t pre_entry;
473         entry_point_t entry;
474
475         unsigned long addr;
476         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
477
478         pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
479         entry = (entry_point_t) jp->entry;
480
481         kcb->jprobe_saved_regs = *regs;
482         kcb->jprobe_saved_esp = stack_addr(regs);
483         addr = (unsigned long)(kcb->jprobe_saved_esp);
484
485         /* TBD: As Linus pointed out, gcc assumes that the callee
486          * owns the argument space and could overwrite it, e.g.
487          * tailcall optimization. So, to be absolutely safe
488          * we also save and restore enough stack bytes to cover
489          * the argument area. */
490         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
491                MIN_STACK_SIZE(addr));
492         regs->EREG(flags) &= ~IF_MASK;
493         trace_hardirqs_off();
494         if (pre_entry)
495                 p->ss_addr[smp_processor_id()] = (kprobe_opcode_t *)
496                                                  pre_entry(jp->priv_arg, regs);
497
498         regs->EREG(ip) = (unsigned long)(jp->entry);
499
500         return 1;
501 }
502
503 /**
504  * @brief Jprobe return end.
505  *
506  * @return Void.
507  */
508 void swap_jprobe_return_end(void);
509
510 /**
511  * @brief Jprobe return code.
512  *
513  * @return Void.
514  */
515 void swap_jprobe_return(void)
516 {
517         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
518
519         asm volatile("       xchgl   %%ebx,%%esp\n"
520                      "       int3\n"
521                      "       .globl swap_jprobe_return_end\n"
522                      "       swap_jprobe_return_end:\n"
523                      "       nop\n"
524                      : : "b" (kcb->jprobe_saved_esp) : "memory");
525 }
526 EXPORT_SYMBOL_GPL(swap_jprobe_return);
527
528 void arch_ujprobe_return(void)
529 {
530 }
531
532 /*
533  * Called after single-stepping.  p->addr is the address of the
534  * instruction whose first byte has been replaced by the "int 3"
535  * instruction.  To avoid the SMP problems that can occur when we
536  * temporarily put back the original opcode to single-step, we
537  * single-stepped a copy of the instruction.  The address of this
538  * copy is p->ainsn.insn.
539  *
540  * This function prepares to return from the post-single-step
541  * interrupt.  We have to fix up the stack as follows:
542  *
543  * 0) Except in the case of absolute or indirect jump or call instructions,
544  * the new eip is relative to the copied instruction.  We need to make
545  * it relative to the original instruction.
546  *
547  * 1) If the single-stepped instruction was pushfl, then the TF and IF
548  * flags are set in the just-pushed eflags, and may need to be cleared.
549  *
550  * 2) If the single-stepped instruction was a call, the return address
551  * that is atop the stack is the address following the copied instruction.
552  * We need to make it the address following the original instruction.
553  *
554  * This function also checks instruction size for preparing direct execution.
555  */
556 static void resume_execution(struct kprobe *p,
557                              struct pt_regs *regs,
558                              struct kprobe_ctlblk *kcb)
559 {
560         unsigned long *tos;
561         unsigned long copy_eip = (unsigned long) p->ainsn.insn;
562         unsigned long orig_eip = (unsigned long) p->addr;
563         kprobe_opcode_t insns[2];
564
565         regs->EREG(flags) &= ~TF_MASK;
566
567         tos = stack_addr(regs);
568         insns[0] = p->ainsn.insn[0];
569         insns[1] = p->ainsn.insn[1];
570
571         switch (insns[0]) {
572         case 0x9c: /* pushfl */
573                 *tos &= ~(TF_MASK | IF_MASK);
574                 *tos |= kcb->kprobe_old_eflags;
575                 break;
576         case 0xc2: /* iret/ret/lret */
577         case 0xc3:
578         case 0xca:
579         case 0xcb:
580         case 0xcf:
581         case 0xea: /* jmp absolute -- eip is correct */
582                 /* eip is already adjusted, no more changes required */
583                 p->ainsn.boostable = 1;
584                 goto no_change;
585         case 0xe8: /* call relative - Fix return addr */
586                 *tos = orig_eip + (*tos - copy_eip);
587                 break;
588         case 0x9a: /* call absolute -- same as call absolute, indirect */
589                 *tos = orig_eip + (*tos - copy_eip);
590                 goto no_change;
591         case 0xff:
592                 if ((insns[1] & 0x30) == 0x10) {
593                         /*
594                          * call absolute, indirect
595                          * Fix return addr; eip is correct.
596                          * But this is not boostable
597                          */
598                         *tos = orig_eip + (*tos - copy_eip);
599                         goto no_change;
600                 } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute
601                                                            * indirect */
602                          ((insns[1] & 0x31) == 0x21)) {
603                         /* jmp far, absolute indirect */
604                         /* eip is correct. And this is boostable */
605                         p->ainsn.boostable = 1;
606                         goto no_change;
607                 }
608         default:
609                 break;
610         }
611
612         if (p->ainsn.boostable == 0) {
613                 if ((regs->EREG(ip) > copy_eip) &&
614                     (regs->EREG(ip) - copy_eip) + 5 < MAX_INSN_SIZE) {
615                         /*
616                          * These instructions can be executed directly if it
617                          * jumps back to correct address.
618                          */
619                         set_jmp_op((void *)regs->EREG(ip),
620                                    (void *)orig_eip +
621                                    (regs->EREG(ip) - copy_eip));
622                         p->ainsn.boostable = 1;
623                 } else {
624                         p->ainsn.boostable = -1;
625                 }
626         }
627
628         regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
629
630 no_change:
631         return;
632 }
633
634 /*
635  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
636  * remain disabled thoroughout this function.
637  */
638 static int post_kprobe_handler(struct pt_regs *regs)
639 {
640         struct kprobe *cur = swap_kprobe_running();
641         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
642
643         if (!cur)
644                 return 0;
645         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
646                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
647                 cur->post_handler(cur, regs, 0);
648         }
649
650         resume_execution(cur, regs, kcb);
651         regs->EREG(flags) |= kcb->kprobe_saved_eflags;
652 #ifndef CONFIG_X86
653         trace_hardirqs_fixup_flags(regs->EREG(flags));
654 #endif /* CONFIG_X86 */
655         /* Restore back the original saved kprobes variables and continue. */
656         if (kcb->kprobe_status == KPROBE_REENTER) {
657                 restore_previous_kprobe(kcb);
658                 goto out;
659         }
660         swap_reset_current_kprobe();
661 out:
662         swap_preempt_enable_no_resched();
663
664         /*
665          * if somebody else is singlestepping across a probe point, eflags
666          * will have TF set, in which case, continue the remaining processing
667          * of do_debug, as if this is not a probe hit.
668          */
669         if (regs->EREG(flags) & TF_MASK)
670                 return 0;
671
672         return 1;
673 }
674
675 static int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
676 {
677         struct kprobe *cur = swap_kprobe_running();
678         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
679
680         switch (kcb->kprobe_status) {
681         case KPROBE_HIT_SS:
682         case KPROBE_REENTER:
683                 /*
684                  * We are here because the instruction being single
685                  * stepped caused a page fault. We reset the current
686                  * kprobe and the eip points back to the probe address
687                  * and allow the page fault handler to continue as a
688                  * normal page fault.
689                  */
690                 regs->EREG(ip) = (unsigned long) cur->addr;
691                 regs->EREG(flags) |= kcb->kprobe_old_eflags;
692                 if (kcb->kprobe_status == KPROBE_REENTER)
693                         restore_previous_kprobe(kcb);
694                 else
695                         swap_reset_current_kprobe();
696                 swap_preempt_enable_no_resched();
697                 break;
698         case KPROBE_HIT_ACTIVE:
699         case KPROBE_HIT_SSDONE:
700                 /*
701                  * We increment the nmissed count for accounting,
702                  * we can also use npre/npostfault count for accouting
703                  * these specific fault cases.
704                  */
705                 swap_kprobes_inc_nmissed_count(cur);
706
707                 /*
708                  * We come here because instructions in the pre/post
709                  * handler caused the page_fault, this could happen
710                  * if handler tries to access user space by
711                  * copy_from_user(), get_user() etc. Let the
712                  * user-specified handler try to fix it first.
713                  */
714                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
715                         return 1;
716
717                 /*
718                  * In case the user-specified fault handler returned
719                  * zero, try to fix up.
720                  */
721                 if (swap_fixup_exception(regs))
722                         return 1;
723
724                 /*
725                  * fixup_exception() could not handle it,
726                  * Let do_page_fault() fix it.
727                  */
728                 break;
729         default:
730                 break;
731         }
732         return 0;
733 }
734
735 static int kprobe_exceptions_notify(struct notifier_block *self,
736                                     unsigned long val, void *data)
737 {
738         struct die_args *args = (struct die_args *) data;
739         int ret = NOTIFY_DONE;
740
741         DBPRINTF("val = %ld, data = 0x%X", val, (unsigned int) data);
742
743         if (args->regs == NULL || user_mode_vm(args->regs))
744                 return ret;
745
746         DBPRINTF("switch (val) %lu %d %d", val, DIE_INT3, DIE_TRAP);
747         switch (val) {
748 #ifdef CONFIG_KPROBES
749         case DIE_INT3:
750 #else
751         case DIE_TRAP:
752 #endif
753                 DBPRINTF("before kprobe_handler ret=%d %p",
754                          ret, args->regs);
755                 if (kprobe_handler (args->regs))
756                         ret = NOTIFY_STOP;
757                 DBPRINTF("after kprobe_handler ret=%d %p",
758                          ret, args->regs);
759                 break;
760         case DIE_DEBUG:
761                 if (post_kprobe_handler(args->regs))
762                         ret = NOTIFY_STOP;
763                 break;
764         case DIE_GPF:
765                 /* swap_kprobe_running() needs smp_processor_id() */
766                 preempt_disable();
767                 if (swap_kprobe_running() &&
768                     kprobe_fault_handler(args->regs, args->trapnr))
769                         ret = NOTIFY_STOP;
770                 preempt_enable();
771                 break;
772         default:
773                 break;
774         }
775         DBPRINTF("ret=%d", ret);
776         /* if(ret == NOTIFY_STOP) */
777         /*      handled_exceptions++; */
778
779         return ret;
780 }
781
782 static struct notifier_block kprobe_exceptions_nb = {
783         .notifier_call = kprobe_exceptions_notify,
784         .priority = INT_MAX
785 };
786
787 /**
788  * @brief Longjump break handler.
789  *
790  * @param p Pointer to fired kprobe.
791  * @param regs Pointer to CPU registers data.
792  * @return 0 on success.
793  */
794 int swap_longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
795 {
796         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
797         u8 *addr = (u8 *) (regs->EREG(ip) - 1);
798         unsigned long stack_addr = (unsigned long) (kcb->jprobe_saved_esp);
799         struct jprobe *jp = container_of(p, struct jprobe, kp);
800
801         DBPRINTF("p = %p\n", p);
802
803         if ((addr > (u8 *)swap_jprobe_return) &&
804             (addr < (u8 *)swap_jprobe_return_end)) {
805                 if (stack_addr(regs) != kcb->jprobe_saved_esp) {
806                         struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
807                         printk(KERN_INFO "current esp %p does not match saved esp %p\n",
808                                stack_addr(regs), kcb->jprobe_saved_esp);
809                         printk(KERN_INFO "Saved registers for jprobe %p\n", jp);
810                         swap_show_registers(saved_regs);
811                         printk(KERN_INFO "Current registers\n");
812                         swap_show_registers(regs);
813                         panic("BUG");
814                         /* BUG(); */
815                 }
816                 *regs = kcb->jprobe_saved_regs;
817                 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
818                        MIN_STACK_SIZE(stack_addr));
819                 swap_preempt_enable_no_resched();
820                 return 1;
821         }
822
823         return 0;
824 }
825
826 /**
827  * @brief Arms kprobe.
828  *
829  * @param p Pointer to target kprobe.
830  * @return Void.
831  */
832 void swap_arch_arm_kprobe(struct kprobe *p)
833 {
834         swap_text_poke(p->addr,
835                        ((unsigned char[]){BREAKPOINT_INSTRUCTION}), 1);
836 }
837
838 /**
839  * @brief Disarms kprobe.
840  *
841  * @param p Pointer to target kprobe.
842  * @return Void.
843  */
844 void swap_arch_disarm_kprobe(struct kprobe *p)
845 {
846         swap_text_poke(p->addr, &p->opcode, 1);
847 }
848
849 static __used void *trampoline_probe_handler_x86(struct pt_regs *regs)
850 {
851         return (void *)trampoline_probe_handler(NULL, regs);
852 }
853
854 /**
855  * @brief Prepares kretprobes, saves ret address, makes function return to
856  * trampoline.
857  *
858  * @param ri Pointer to kretprobe_instance.
859  * @param regs Pointer to CPU registers data.
860  * @return Void.
861  */
862 void swap_arch_prepare_kretprobe(struct kretprobe_instance *ri,
863                                  struct pt_regs *regs)
864 {
865         unsigned long *ptr_ret_addr = stack_addr(regs);
866
867         /* for __switch_to probe */
868         if ((unsigned long)ri->rp->kp.addr == sched_addr) {
869                 ri->sp = NULL;
870                 ri->task = (struct task_struct *)regs->dx;
871         } else {
872                 ri->sp = ptr_ret_addr;
873         }
874
875         /* Save the return address */
876         ri->ret_addr = (unsigned long *)*ptr_ret_addr;
877
878         /* Replace the return addr with trampoline addr */
879         *ptr_ret_addr = (unsigned long)&swap_kretprobe_trampoline;
880 }
881
882
883
884
885
886 /*
887  ******************************************************************************
888  *                                   kjumper                                  *
889  ******************************************************************************
890  */
891 struct kj_cb_data {
892         struct pt_regs regs;
893         struct kprobe *p;
894
895         jumper_cb_t cb;
896         char data[0];
897 };
898
899 static struct kj_cb_data * __used kjump_handler(struct kj_cb_data *data)
900 {
901         /* call callback */
902         data->cb(data->data);
903
904         return data;
905 }
906
907 void kjump_trampoline(void);
908 void kjump_trampoline_int3(void);
909 __asm(
910         "kjump_trampoline:\n"
911         "call   kjump_handler\n"
912         "kjump_trampoline_int3:\n"
913         "nop\n" /* for restore_regs_kp */
914 );
915
916 int set_kjump_cb(struct pt_regs *regs, jumper_cb_t cb, void *data, size_t size)
917 {
918         struct kj_cb_data *cb_data;
919
920         cb_data = kmalloc(sizeof(*cb_data) + size, GFP_ATOMIC);
921         if (cb_data == NULL)
922                 return -ENOMEM;
923
924         /* save regs */
925         cb_data->regs = *regs;
926
927         cb_data->p = swap_kprobe_running();
928         cb_data->cb = cb;
929
930         /* save data */
931         if (size)
932                 memcpy(cb_data->data, data, size);
933
934         /* save pointer cb_data at ax */
935         regs->ax = (long)cb_data;
936
937         /* jump to kjump_trampoline */
938         regs->ip = (unsigned long)&kjump_trampoline;
939
940         swap_reset_current_kprobe();
941         swap_preempt_enable_no_resched();
942
943         return 1;
944 }
945 EXPORT_SYMBOL_GPL(set_kjump_cb);
946
947 static int restore_regs_pre_handler(struct kprobe *p, struct pt_regs *regs)
948 {
949         struct kj_cb_data *data = (struct kj_cb_data *)regs->ax;
950         struct kprobe *kp = data->p;
951         struct kprobe_ctlblk *kcb = swap_get_kprobe_ctlblk();
952
953         /* restore regs */
954         *regs = data->regs;
955
956         /* FIXME: potential memory leak, when process kill */
957         kfree(data);
958
959         kcb = swap_get_kprobe_ctlblk();
960
961         set_current_kprobe(kp, regs, kcb);
962         setup_singlestep(kp, regs, kcb);
963
964         return 1;
965 }
966
967 static struct kprobe restore_regs_kp = {
968         .pre_handler = restore_regs_pre_handler,
969         .addr = (kprobe_opcode_t *)&kjump_trampoline_int3,      /* nop */
970 };
971
972 static int kjump_init(void)
973 {
974         int ret;
975
976         ret = swap_register_kprobe(&restore_regs_kp);
977         if (ret)
978                 printk(KERN_INFO "ERROR: kjump_init(), ret=%d\n", ret);
979
980         return ret;
981 }
982
983 static void kjump_exit(void)
984 {
985         swap_unregister_kprobe(&restore_regs_kp);
986 }
987
988
989
990
991
992 /*
993  ******************************************************************************
994  *                                   jumper                                   *
995  ******************************************************************************
996  */
997 struct cb_data {
998         unsigned long ret_addr;
999         unsigned long bx;
1000
1001         jumper_cb_t cb;
1002         char data[0];
1003 };
1004
1005 static unsigned long __used get_bx(struct cb_data *data)
1006 {
1007         return data->bx;
1008 }
1009
1010 static unsigned long __used jump_handler(struct cb_data *data)
1011 {
1012         unsigned long ret_addr = data->ret_addr;
1013
1014         /* call callback */
1015         data->cb(data->data);
1016
1017         /* FIXME: potential memory leak, when process kill */
1018         kfree(data);
1019
1020         return ret_addr;
1021 }
1022
1023 void jump_trampoline(void);
1024 __asm(
1025         "jump_trampoline:\n"
1026         "pushf\n"
1027         SWAP_SAVE_REGS_STRING
1028         "movl   %ebx, %eax\n"   /* data --> ax */
1029         "call   get_bx\n"
1030         "movl   %eax, (%esp)\n" /* restore bx */
1031         "movl   %ebx, %eax\n"   /* data --> ax */
1032         "call   jump_handler\n"
1033         /* move flags to cs */
1034         "movl 56(%esp), %edx\n"
1035         "movl %edx, 52(%esp)\n"
1036         /* replace saved flags with true return address. */
1037         "movl %eax, 56(%esp)\n"
1038         SWAP_RESTORE_REGS_STRING
1039         "popf\n"
1040         "ret\n"
1041 );
1042
1043 unsigned long get_jump_addr(void)
1044 {
1045         return (unsigned long)&jump_trampoline;
1046 }
1047 EXPORT_SYMBOL_GPL(get_jump_addr);
1048
1049 int set_jump_cb(unsigned long ret_addr, struct pt_regs *regs,
1050                 jumper_cb_t cb, void *data, size_t size)
1051 {
1052         struct cb_data *cb_data;
1053
1054         cb_data = kmalloc(sizeof(*cb_data) + size, GFP_ATOMIC);
1055         if (cb_data == NULL)
1056                 return -ENOMEM;
1057
1058         /* save data */
1059         if (size)
1060                 memcpy(cb_data->data, data, size);
1061
1062         /* save info for restore */
1063         cb_data->ret_addr = ret_addr;
1064         cb_data->cb = cb;
1065         cb_data->bx = regs->bx;
1066
1067         /* save cb_data to bx */
1068         regs->bx = (long)cb_data;
1069
1070         return 0;
1071 }
1072 EXPORT_SYMBOL_GPL(set_jump_cb);
1073
1074
1075
1076
1077
1078 /**
1079  * @brief Initializes x86 module deps.
1080  *
1081  * @return 0 on success, negative error code on error.
1082  */
1083 int arch_init_module_deps()
1084 {
1085         const char *sym;
1086
1087         sym = "fixup_exception";
1088         swap_fixup_exception = (void *)swap_ksyms(sym);
1089         if (swap_fixup_exception == NULL)
1090                 goto not_found;
1091
1092         sym = "text_poke";
1093         swap_text_poke = (void *)swap_ksyms(sym);
1094         if (swap_text_poke == NULL)
1095                 goto not_found;
1096
1097         sym = "show_regs";
1098         swap_show_registers = (void *)swap_ksyms(sym);
1099         if (swap_show_registers == NULL)
1100                 goto not_found;
1101
1102         return 0;
1103
1104 not_found:
1105         printk(KERN_INFO "ERROR: symbol %s(...) not found\n", sym);
1106         return -ESRCH;
1107 }
1108
1109 /**
1110  * @brief Initializes kprobes module for ARM arch.
1111  *
1112  * @return 0 on success, error code on error.
1113  */
1114 int swap_arch_init_kprobes(void)
1115 {
1116         int ret;
1117
1118         ret = register_die_notifier(&kprobe_exceptions_nb);
1119         if (ret)
1120                 return ret;
1121
1122         ret = kjump_init();
1123         if (ret)
1124                 unregister_die_notifier(&kprobe_exceptions_nb);
1125
1126         return ret;
1127 }
1128
1129 /**
1130  * @brief Uninitializes kprobe module.
1131  *
1132  * @return Void.
1133  */
1134 void swap_arch_exit_kprobes(void)
1135 {
1136         kjump_exit();
1137         unregister_die_notifier(&kprobe_exceptions_nb);
1138 }