* src/vm/jit/i386/darwin/md-os.c (md_replace_executionstate_read):
[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 #if defined(ENABLE_THREADS)
39 # include "threads/native/threads.h"
40 #endif
41
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/stacktrace.h"
49
50 #include "vmcore/system.h"
51
52
53 /* md_signal_handler_sigsegv ***************************************************
54
55    NullPointerException signal handler for hardware null pointer
56    check.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         ucontext_t     *_uc;
63         mcontext_t     *_mc;
64         u1             *pv;
65         u1             *sp;
66         u1             *ra;
67         u1             *xpc;
68         u4              mcode;
69         s4              d;
70         s4              s1;
71         s4              disp;
72         intptr_t        val;
73         intptr_t        addr;
74         int             type;
75         void           *p;
76
77         _uc = (ucontext_t *) _p;
78         _mc = &_uc->uc_mcontext;
79
80         pv  = (u1 *) _mc->sc_regs[REG_PV];
81         sp  = (u1 *) _mc->sc_regs[REG_SP];
82         ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
83         xpc = (u1 *) _mc->sc_pc;
84
85         /* get exception-throwing instruction */
86
87         mcode = *((u4 *) xpc);
88
89         d    = M_MEM_GET_Ra(mcode);
90         s1   = M_MEM_GET_Rb(mcode);
91         disp = M_MEM_GET_Memory_disp(mcode);
92
93         val  = _mc->sc_regs[d];
94
95         /* check for special-load */
96
97         if (s1 == REG_ZERO) {
98                 /* we use the exception type as load displacement */
99
100                 type = disp;
101
102                 if (type == EXCEPTION_HARDWARE_COMPILER) {
103                         /* The XPC is the RA minus 1, because the RA points to the
104                            instruction after the call. */
105
106                         xpc = ra - 4;
107                 }
108         }
109         else {
110                 /* This is a normal NPE: addr must be NULL and the NPE-type
111                    define is 0. */
112
113                 addr = _mc->sc_regs[s1];
114                 type = (int) addr;
115         }
116
117         /* Handle the type. */
118
119         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
120
121         /* Set registers. */
122
123         switch (type) {
124         case EXCEPTION_HARDWARE_COMPILER:
125                 if (p != NULL) {
126                         _mc->sc_regs[REG_PV] = (uintptr_t) p;
127                         _mc->sc_pc           = (uintptr_t) p;
128                         break;
129                 }
130
131                 /* Get and set the PV from the parent Java method. */
132
133                 pv = md_codegen_get_pv_from_pc(ra);
134
135                 _mc->sc_regs[REG_PV] = (uintptr_t) pv;
136
137                 /* Get the exception object. */
138
139                 p = builtin_retrieve_exception();
140
141                 assert(p != NULL);
142
143                 /* fall-through */
144
145         case EXCEPTION_HARDWARE_PATCHER:
146                 if (p == NULL)
147                         break;
148
149                 /* fall-through */
150                 
151         default:
152                 _mc->sc_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
153                 _mc->sc_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
154                 _mc->sc_pc                   = (uintptr_t) asm_handle_exception;
155         }
156 }
157
158
159 /* md_signal_handler_sigusr1 ***************************************************
160
161    Signal handler for suspending threads.
162
163 *******************************************************************************/
164
165 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
166 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
167 {
168         ucontext_t *_uc;
169         mcontext_t *_mc;
170         u1         *pc;
171         u1         *sp;
172
173         _uc = (ucontext_t *) _p;
174         _mc = &_uc->uc_mcontext;
175
176         /* get the PC and SP for this thread */
177         pc = (u1 *) _mc->sc_pc;
178         sp = (u1 *) _mc->sc_regs[REG_SP];
179
180         /* now suspend the current thread */
181         threads_suspend_ack(pc, sp);
182 }
183 #endif
184
185
186 /* md_signal_handler_sigusr2 ***************************************************
187
188    Signal handler for profiling sampling.
189
190 *******************************************************************************/
191
192 #if defined(ENABLE_THREADS)
193 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
194 {
195         threadobject *tobj;
196         ucontext_t   *_uc;
197         mcontext_t   *_mc;
198         u1           *pc;
199
200         tobj = THREADOBJECT;
201
202         _uc = (ucontext_t *) _p;
203         _mc = &_uc->uc_mcontext;
204
205         pc = (u1 *) _mc->sc_pc;
206
207         tobj->pc = pc;
208 }
209 #endif
210
211
212 /* md_executionstate_read ******************************************************
213
214    Read the given context into an executionstate.
215
216 *******************************************************************************/
217
218 void md_executionstate_read(executionstate_t *es, void *context)
219 {
220         ucontext_t *_uc;
221         mcontext_t *_mc;
222         int         i;
223
224         _uc = (ucontext_t *) context;
225         _mc = &_uc->uc_mcontext;
226
227         /* read special registers */
228         es->pc = (u1 *) _mc->sc_pc;
229         es->sp = (u1 *) _mc->sc_regs[REG_SP];
230         es->pv = (u1 *) _mc->sc_regs[REG_PV];
231         es->ra = (u1 *) _mc->sc_regs[REG_RA];
232
233         /* read integer registers */
234         for (i = 0; i < INT_REG_CNT; i++)
235                 es->intregs[i] = _mc->sc_regs[i];
236
237         /* read float registers */
238         /* Do not use the assignment operator '=', as the type of
239          * the _mc->sc_fpregs[i] can cause invalid conversions. */
240
241         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
242         system_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
243 }
244
245
246 /* md_executionstate_write *****************************************************
247
248    Write the given executionstate back to the context.
249
250 *******************************************************************************/
251
252 void md_executionstate_write(executionstate_t *es, void *context)
253 {
254         ucontext_t *_uc;
255         mcontext_t *_mc;
256         int         i;
257
258         _uc = (ucontext_t *) context;
259         _mc = &_uc->uc_mcontext;
260
261         /* write integer registers */
262         for (i = 0; i < INT_REG_CNT; i++)
263                 _mc->sc_regs[i] = es->intregs[i];
264
265         /* write float registers */
266         /* Do not use the assignment operator '=', as the type of
267          * the _mc->sc_fpregs[i] can cause invalid conversions. */
268
269         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
270         system_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
271
272         /* write special registers */
273         _mc->sc_pc           = (ptrint) es->pc;
274         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
275         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
276         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
277 }
278
279
280 /* md_critical_section_restart *************************************************
281
282    Search the critical sections tree for a matching section and set
283    the PC to the restart point, if necessary.
284
285 *******************************************************************************/
286
287 #if defined(ENABLE_THREADS)
288 void md_critical_section_restart(ucontext_t *_uc)
289 {
290         mcontext_t *_mc;
291         u1         *pc;
292         u1         *npc;
293
294         _mc = &_uc->uc_mcontext;
295
296         pc = (u1 *) _mc->sc_pc;
297
298         npc = critical_find_restart_point(pc);
299
300         if (npc != NULL)
301                 _mc->sc_pc = (ptrint) npc;
302 }
303 #endif
304
305
306 /*
307  * These are local overrides for various environment variables in Emacs.
308  * Please do not remove this and leave it at the end of the file, where
309  * Emacs will automagically detect them.
310  * ---------------------------------------------------------------------
311  * Local variables:
312  * mode: c
313  * indent-tabs-mode: t
314  * c-basic-offset: 4
315  * tab-width: 4
316  * End:
317  */