* Merged executionstate branch.
[cacao.git] / src / vm / jit / alpha / linux / md-os.c
1 /* src/vm/jit/alpha/linux/md-os.c - machine dependent Alpha Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <ucontext.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/alpha/codegen.h"
35 #include "vm/jit/alpha/md.h"
36 #include "vm/jit/alpha/md-abi.h"
37
38 #include "threads/thread.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.h"
47
48 #include "vmcore/system.h"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    NullPointerException signal handler for hardware null pointer
54    check.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         ucontext_t     *_uc;
61         mcontext_t     *_mc;
62         u1             *pv;
63         u1             *sp;
64         u1             *ra;
65         u1             *xpc;
66         u4              mcode;
67         s4              d;
68         s4              s1;
69         s4              disp;
70         intptr_t        val;
71         intptr_t        addr;
72         int             type;
73         void           *p;
74
75         _uc = (ucontext_t *) _p;
76         _mc = &_uc->uc_mcontext;
77
78         pv  = (u1 *) _mc->sc_regs[REG_PV];
79         sp  = (u1 *) _mc->sc_regs[REG_SP];
80         ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
81         xpc = (u1 *) _mc->sc_pc;
82
83         /* get exception-throwing instruction */
84
85         mcode = *((u4 *) xpc);
86
87         d    = M_MEM_GET_Ra(mcode);
88         s1   = M_MEM_GET_Rb(mcode);
89         disp = M_MEM_GET_Memory_disp(mcode);
90
91         val  = _mc->sc_regs[d];
92
93         /* check for special-load */
94
95         if (s1 == REG_ZERO) {
96                 /* we use the exception type as load displacement */
97
98                 type = disp;
99
100                 if (type == EXCEPTION_HARDWARE_COMPILER) {
101                         /* The XPC is the RA minus 1, because the RA points to the
102                            instruction after the call. */
103
104                         xpc = ra - 4;
105                 }
106         }
107         else {
108                 /* This is a normal NPE: addr must be NULL and the NPE-type
109                    define is 0. */
110
111                 addr = _mc->sc_regs[s1];
112                 type = (int) addr;
113         }
114
115         /* Handle the type. */
116
117         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
118
119         /* Set registers. */
120
121         switch (type) {
122         case EXCEPTION_HARDWARE_COMPILER:
123                 if (p != NULL) {
124                         _mc->sc_regs[REG_PV] = (uintptr_t) p;
125                         _mc->sc_pc           = (uintptr_t) p;
126                         break;
127                 }
128
129                 /* Get and set the PV from the parent Java method. */
130
131                 pv = md_codegen_get_pv_from_pc(ra);
132
133                 _mc->sc_regs[REG_PV] = (uintptr_t) pv;
134
135                 /* Get the exception object. */
136
137                 p = builtin_retrieve_exception();
138
139                 assert(p != NULL);
140
141                 /* fall-through */
142
143         case EXCEPTION_HARDWARE_PATCHER:
144                 if (p == NULL)
145                         break;
146
147                 /* fall-through */
148                 
149         default:
150                 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
151                 _mc->sc_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
152                 _mc->sc_pc                   = (uintptr_t) asm_handle_exception;
153         }
154 }
155
156
157 /* md_signal_handler_sigusr1 ***************************************************
158
159    Signal handler for suspending threads.
160
161 *******************************************************************************/
162
163 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
164 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
165 {
166         ucontext_t *_uc;
167         mcontext_t *_mc;
168         u1         *pc;
169         u1         *sp;
170
171         _uc = (ucontext_t *) _p;
172         _mc = &_uc->uc_mcontext;
173
174         /* get the PC and SP for this thread */
175         pc = (u1 *) _mc->sc_pc;
176         sp = (u1 *) _mc->sc_regs[REG_SP];
177
178         /* now suspend the current thread */
179         threads_suspend_ack(pc, sp);
180 }
181 #endif
182
183
184 /* md_signal_handler_sigusr2 ***************************************************
185
186    Signal handler for profiling sampling.
187
188 *******************************************************************************/
189
190 #if defined(ENABLE_THREADS)
191 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
192 {
193         threadobject *tobj;
194         ucontext_t   *_uc;
195         mcontext_t   *_mc;
196         u1           *pc;
197
198         tobj = THREADOBJECT;
199
200         _uc = (ucontext_t *) _p;
201         _mc = &_uc->uc_mcontext;
202
203         pc = (u1 *) _mc->sc_pc;
204
205         tobj->pc = pc;
206 }
207 #endif
208
209
210 /* md_executionstate_read ******************************************************
211
212    Read the given context into an executionstate.
213
214 *******************************************************************************/
215
216 void md_executionstate_read(executionstate_t *es, void *context)
217 {
218         ucontext_t *_uc;
219         mcontext_t *_mc;
220         int         i;
221
222         _uc = (ucontext_t *) context;
223         _mc = &_uc->uc_mcontext;
224
225         /* read special registers */
226         es->pc = (u1 *) _mc->sc_pc;
227         es->sp = (u1 *) _mc->sc_regs[REG_SP];
228         es->pv = (u1 *) _mc->sc_regs[REG_PV];
229         es->ra = (u1 *) _mc->sc_regs[REG_RA];
230
231         /* read integer registers */
232         for (i = 0; i < INT_REG_CNT; i++)
233                 es->intregs[i] = _mc->sc_regs[i];
234
235         /* read float registers */
236         /* Do not use the assignment operator '=', as the type of
237          * the _mc->sc_fpregs[i] can cause invalid conversions. */
238
239         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
240         system_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
241 }
242
243
244 /* md_executionstate_write *****************************************************
245
246    Write the given executionstate back to the context.
247
248 *******************************************************************************/
249
250 void md_executionstate_write(executionstate_t *es, void *context)
251 {
252         ucontext_t *_uc;
253         mcontext_t *_mc;
254         int         i;
255
256         _uc = (ucontext_t *) context;
257         _mc = &_uc->uc_mcontext;
258
259         /* write integer registers */
260         for (i = 0; i < INT_REG_CNT; i++)
261                 _mc->sc_regs[i] = es->intregs[i];
262
263         /* write float registers */
264         /* Do not use the assignment operator '=', as the type of
265          * the _mc->sc_fpregs[i] can cause invalid conversions. */
266
267         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
268         system_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
269
270         /* write special registers */
271         _mc->sc_pc           = (ptrint) es->pc;
272         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
273         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
274         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
275 }
276
277
278 /* md_critical_section_restart *************************************************
279
280    Search the critical sections tree for a matching section and set
281    the PC to the restart point, if necessary.
282
283 *******************************************************************************/
284
285 #if defined(ENABLE_THREADS)
286 void md_critical_section_restart(ucontext_t *_uc)
287 {
288         mcontext_t *_mc;
289         u1         *pc;
290         u1         *npc;
291
292         _mc = &_uc->uc_mcontext;
293
294         pc = (u1 *) _mc->sc_pc;
295
296         npc = critical_find_restart_point(pc);
297
298         if (npc != NULL)
299                 _mc->sc_pc = (ptrint) npc;
300 }
301 #endif
302
303
304 /*
305  * These are local overrides for various environment variables in Emacs.
306  * Please do not remove this and leave it at the end of the file, where
307  * Emacs will automagically detect them.
308  * ---------------------------------------------------------------------
309  * Local variables:
310  * mode: c
311  * indent-tabs-mode: t
312  * c-basic-offset: 4
313  * tab-width: 4
314  * End:
315  */