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