methodPool: compile methods on-demand
[mate.git] / ffi / trap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <asm/ucontext.h>
5
6 unsigned int getMethodEntry(void *, char *);
7 void *method_map = NULL;
8
9 void set_mmap(void *mmap)
10 {
11         printf("set_mmap: 0x%08x\n", (unsigned int) mmap);
12         method_map = mmap;
13 }
14
15 void *get_mmap()
16 {
17         printf("get_mmap: 0x%08x\n", (unsigned int) method_map);
18         return method_map;
19 }
20
21
22 void mainresult(unsigned int a)
23 {
24         printf("mainresult: 0x%08x\n", a);
25 }
26
27 void callertrap(int nSignal, siginfo_t *info, void *ctx)
28 {
29         struct ucontext *uctx = (struct ucontext *) ctx;
30         unsigned int patchme = getMethodEntry(method_map, "fib");
31
32         printf("callertrap(mctx)  by 0x%08x\n", (unsigned int) uctx->uc_mcontext.eip);
33         // printf("callertrap(addr)  by 0x%08x\n", info->si_addr);
34         // printf("callertrap(*esp)  by 0x%08x\n", * (unsigned int *) uctx->uc_mcontext.esp);
35
36         unsigned int *to_patch = (unsigned int *) (uctx->uc_mcontext.eip + 2);
37         unsigned char *insn = (unsigned char *) (uctx->uc_mcontext.eip);
38         *insn = 0x90; // nop
39         insn++;
40         *insn = 0xe8; // call
41         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
42         printf("*to_patch: 0x%08x\n", *to_patch);
43         if (*to_patch != 0x00000000) {
44                 printf("something is wrong here. abort\n");
45                 exit(0);
46         }
47         *to_patch = (unsigned int) patchme - ((unsigned int) insn + 5);
48         printf("*to_patch: 0x%08x\n", *to_patch);
49         uctx->uc_mcontext.eip = (unsigned long) insn;
50         // while (1) ;
51 }
52
53 void register_signal(void)
54 {
55         struct sigaction segvaction;
56         segvaction.sa_sigaction = callertrap;
57         sigemptyset(&segvaction.sa_mask);
58         segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
59         sigaction(SIGSEGV, &segvaction, NULL);
60 }
61
62 unsigned int getaddr(void)
63 {
64         return (unsigned int) mainresult;
65 }