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