* changed src/vm/jit/m68k/asmpart.S: Removed unused method
[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-abi.h"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/threads.h"
41 #endif
42
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.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_replace_executionstate_read **********************************************
211
212    Read the given context into an executionstate for Replacement.
213
214 *******************************************************************************/
215
216 #if defined(ENABLE_REPLACEMENT)
217 void md_replace_executionstate_read(executionstate_t *es, void *context)
218 {
219         ucontext_t *_uc;
220         mcontext_t *_mc;
221         s4          i;
222
223         _uc = (ucontext_t *) context;
224         _mc = &_uc->uc_mcontext;
225
226         /* read special registers */
227         es->pc = (u1 *) _mc->sc_pc;
228         es->sp = (u1 *) _mc->sc_regs[REG_SP];
229         es->pv = (u1 *) _mc->sc_regs[REG_PV];
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         for (i = 0; i < FLT_REG_CNT; i++)
237                 es->fltregs[i] = _mc->sc_fpregs[i];
238 }
239 #endif
240
241
242 /* md_replace_executionstate_write *********************************************
243
244    Write the given executionstate back to the context for Replacement.
245
246 *******************************************************************************/
247
248 #if defined(ENABLE_REPLACEMENT)
249 void md_replace_executionstate_write(executionstate_t *es, void *context)
250 {
251         ucontext_t *_uc;
252         mcontext_t *_mc;
253         s4          i;
254
255         _uc = (ucontext_t *) context;
256         _mc = &_uc->uc_mcontext;
257
258         /* write integer registers */
259         for (i = 0; i < INT_REG_CNT; i++)
260                 _mc->sc_regs[i] = es->intregs[i];
261
262         /* write float registers */
263         for (i = 0; i < FLT_REG_CNT; i++)
264                 _mc->sc_fpregs[i] = es->fltregs[i];
265
266         /* write special registers */
267         _mc->sc_pc           = (ptrint) es->pc;
268         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
269         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
270 }
271 #endif
272
273
274 /* md_critical_section_restart *************************************************
275
276    Search the critical sections tree for a matching section and set
277    the PC to the restart point, if necessary.
278
279 *******************************************************************************/
280
281 #if defined(ENABLE_THREADS)
282 void md_critical_section_restart(ucontext_t *_uc)
283 {
284         mcontext_t *_mc;
285         u1         *pc;
286         u1         *npc;
287
288         _mc = &_uc->uc_mcontext;
289
290         pc = (u1 *) _mc->sc_pc;
291
292         npc = critical_find_restart_point(pc);
293
294         if (npc != NULL)
295                 _mc->sc_pc = (ptrint) npc;
296 }
297 #endif
298
299
300 /*
301  * These are local overrides for various environment variables in Emacs.
302  * Please do not remove this and leave it at the end of the file, where
303  * Emacs will automagically detect them.
304  * ---------------------------------------------------------------------
305  * Local variables:
306  * mode: c
307  * indent-tabs-mode: t
308  * c-basic-offset: 4
309  * tab-width: 4
310  * End:
311  */