* src/vm/jit/codegen-common.cpp, src/vm/jit/x86_64/codegen.c: Generate
[cacao.git] / src / vm / jit / arm / linux / md-os.c
1 /* src/vm/jit/arm/linux/md-os.c - machine dependent ARM Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008, 2009 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <stdint.h>
30
31 #define ucontext broken_glibc_ucontext
32 #define ucontext_t broken_glibc_ucontext_t
33 #include <ucontext.h>
34 #undef ucontext
35 #undef ucontext_t
36
37 typedef struct ucontext {
38    unsigned long     uc_flags;
39    struct ucontext  *uc_link;
40    stack_t           uc_stack;
41    struct sigcontext uc_mcontext;
42    sigset_t          uc_sigmask;
43 } ucontext_t;
44
45 #define scontext_t struct sigcontext
46
47 #include "vm/types.h"
48
49 #include "vm/jit/arm/md.h"
50 #include "vm/jit/arm/md-abi.h"
51
52 #include "threads/thread.hpp"
53
54 #include "vm/signallocal.hpp"
55
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/executionstate.h"
58 #include "vm/jit/trap.hpp"
59
60
61 /**
62  * Signal handler for hardware exceptions.
63  */
64 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
65 {
66         ucontext_t* _uc = (ucontext_t*) _p;
67         scontext_t* _sc = &_uc->uc_mcontext;
68
69         /* ATTENTION: glibc included messed up kernel headers we needed a
70            workaround for the ucontext structure. */
71
72         void* xpc = (u1 *) _sc->arm_pc;
73
74         // Handle the trap.
75         trap_handle(TRAP_SIGSEGV, xpc, _p);
76 }
77
78
79 /**
80  * Illegal instruction signal handler for hardware exception checks.
81  */
82 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
83 {
84         ucontext_t* _uc = (ucontext_t*) _p;
85         scontext_t* _sc = &_uc->uc_mcontext;
86
87         /* ATTENTION: glibc included messed up kernel headers we needed a
88            workaround for the ucontext structure. */
89
90         void* xpc = (void*) _sc->arm_pc;
91
92         // Handle the trap.
93         trap_handle(TRAP_SIGILL, xpc, _p);
94 }
95
96
97 /* md_signal_handler_sigusr1 ***************************************************
98
99    Signal handler for suspending threads.
100
101 *******************************************************************************/
102
103 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
104 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
105 {
106         ucontext_t *_uc;
107         scontext_t *_sc;
108         u1         *pc;
109         u1         *sp;
110
111         _uc = (ucontext_t *) _p;
112         _sc = &_uc->uc_mcontext;
113
114         /* get the PC and SP for this thread */
115         pc = (u1 *) _sc->arm_pc;
116         sp = (u1 *) _sc->arm_sp;
117
118         /* now suspend the current thread */
119         threads_suspend_ack(pc, sp);
120 }
121 #endif
122
123
124 /* md_signal_handler_sigusr2 ***************************************************
125
126    Signal handler for profiling sampling.
127
128 *******************************************************************************/
129
130 #if defined(ENABLE_THREADS)
131 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
132 {
133         threadobject *thread;
134         ucontext_t   *_uc;
135         scontext_t   *_sc;
136         u1           *pc;
137
138         thread = THREADOBJECT;
139
140         _uc = (ucontext_t*) _p;
141         _sc = &_uc->uc_mcontext;
142
143         pc = (u1 *) _sc->arm_pc;
144
145         thread->pc = pc;
146 }
147 #endif
148
149
150 /**
151  * Read the given context into an executionstate.
152  *
153  * @param es      execution state
154  * @param context machine context
155  */
156 void md_executionstate_read(executionstate_t *es, void *context)
157 {
158         ucontext_t *_uc;
159         scontext_t *_sc;
160         int         i;
161
162         _uc = (ucontext_t *) context;
163         _sc = &_uc->uc_mcontext;
164
165         /* ATTENTION: glibc included messed up kernel headers we needed a
166            workaround for the ucontext structure. */
167
168         /* read special registers */
169
170         es->pc = (u1 *) _sc->arm_pc;
171         es->sp = (u1 *) _sc->arm_sp;
172         es->pv = (u1 *) _sc->arm_ip;
173         es->ra = (u1 *) _sc->arm_lr;
174
175         /* read integer registers */
176
177         for (i = 0; i < INT_REG_CNT; i++)
178                 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
179
180         /* read float registers */
181
182         for (i = 0; i < FLT_REG_CNT; i++)
183                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
184 }
185
186
187 /**
188  * Write the given executionstate back to the context.
189  *
190  * @param es      execution state
191  * @param context machine context
192  */
193 void md_executionstate_write(executionstate_t *es, void *context)
194 {
195         ucontext_t *_uc;
196         scontext_t *_sc;
197         int         i;
198
199         _uc = (ucontext_t *) context;
200         _sc = &_uc->uc_mcontext;
201
202         /* ATTENTION: glibc included messed up kernel headers we needed a
203            workaround for the ucontext structure. */
204
205         /* write integer registers */
206
207         for (i = 0; i < INT_REG_CNT; i++)
208                 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
209
210         /* write special registers */
211
212         _sc->arm_pc = (ptrint) es->pc;
213         _sc->arm_sp = (ptrint) es->sp;
214         _sc->arm_ip = (ptrint) es->pv;
215         _sc->arm_lr = (ptrint) es->ra;
216 }
217
218
219 /*
220  * These are local overrides for various environment variables in Emacs.
221  * Please do not remove this and leave it at the end of the file, where
222  * Emacs will automagically detect them.
223  * ---------------------------------------------------------------------
224  * Local variables:
225  * mode: c
226  * indent-tabs-mode: t
227  * c-basic-offset: 4
228  * tab-width: 4
229  * End:
230  */
231