Merge from subtype.
[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    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/alpha/codegen.h"
36 #include "vm/jit/alpha/md.h"
37 #include "vm/jit/alpha/md-abi.h"
38
39 #include "threads/thread.hpp"
40
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.h"
43 #include "vm/os.hpp"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/disass.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/trap.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
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         trap_handle(type, val, pv, sp, ra, xpc, _p);
117 }
118
119
120 /* md_signal_handler_sigill ****************************************************
121
122    Illegal Instruction signal handler for hardware exception checks.
123
124 *******************************************************************************/
125
126 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
127 {
128         ucontext_t* _uc = (ucontext_t*) _p;
129         mcontext_t* _mc = &_uc->uc_mcontext;
130
131         void* pv  = (u1 *) _mc->sc_regs[REG_PV];
132         void* sp  = (u1 *) _mc->sc_regs[REG_SP];
133         void* ra  = (u1 *) _mc->sc_regs[REG_RA]; // RA is correct for leaf methods.
134         void* xpc = (u1 *) _mc->sc_pc;
135
136         // The PC points to the instruction after the illegal instruction.
137         xpc = (void*) (((uintptr_t) xpc) - 4);
138
139         // Get the exception-throwing instruction.
140         uint32_t mcode = *((uint32_t*) xpc);
141
142         int opcode = M_OP3_GET_Opcode(mcode);
143
144         // Check for undefined instruction we use.
145         // TODO Check the whole instruction.
146         if (opcode != 0x4) {
147                 log_println("md_signal_handler_sigill: Unknown illegal instruction %x at %p", mcode, xpc);
148 #if defined(ENABLE_DISASSEMBLER)
149                 (void) disassinstr(xpc);
150 #endif
151                 vm_abort("Aborting...");
152         }
153
154         // This signal is always a patcher.
155         int      type = TRAP_PATCHER;
156         intptr_t val  = 0;
157
158         // Handle the trap.
159         trap_handle(type, val, pv, sp, ra, xpc, _p);
160 }
161
162
163 /* md_signal_handler_sigusr1 ***************************************************
164
165    Signal handler for suspending threads.
166
167 *******************************************************************************/
168
169 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
170 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
171 {
172         ucontext_t *_uc;
173         mcontext_t *_mc;
174         u1         *pc;
175         u1         *sp;
176
177         _uc = (ucontext_t *) _p;
178         _mc = &_uc->uc_mcontext;
179
180         /* get the PC and SP for this thread */
181         pc = (u1 *) _mc->sc_pc;
182         sp = (u1 *) _mc->sc_regs[REG_SP];
183
184         /* now suspend the current thread */
185         threads_suspend_ack(pc, sp);
186 }
187 #endif
188
189
190 /* md_signal_handler_sigusr2 ***************************************************
191
192    Signal handler for profiling sampling.
193
194 *******************************************************************************/
195
196 #if defined(ENABLE_THREADS)
197 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
198 {
199         threadobject *tobj;
200         ucontext_t   *_uc;
201         mcontext_t   *_mc;
202         u1           *pc;
203
204         tobj = THREADOBJECT;
205
206         _uc = (ucontext_t *) _p;
207         _mc = &_uc->uc_mcontext;
208
209         pc = (u1 *) _mc->sc_pc;
210
211         tobj->pc = pc;
212 }
213 #endif
214
215
216 /* md_executionstate_read ******************************************************
217
218    Read the given context into an executionstate.
219
220 *******************************************************************************/
221
222 void md_executionstate_read(executionstate_t *es, void *context)
223 {
224         ucontext_t *_uc;
225         mcontext_t *_mc;
226         int         i;
227
228         _uc = (ucontext_t *) context;
229         _mc = &_uc->uc_mcontext;
230
231         /* read special registers */
232         es->pc = (u1 *) _mc->sc_pc;
233         es->sp = (u1 *) _mc->sc_regs[REG_SP];
234         es->pv = (u1 *) _mc->sc_regs[REG_PV];
235         es->ra = (u1 *) _mc->sc_regs[REG_RA];
236
237         /* read integer registers */
238         for (i = 0; i < INT_REG_CNT; i++)
239                 es->intregs[i] = _mc->sc_regs[i];
240
241         /* read float registers */
242         /* Do not use the assignment operator '=', as the type of
243          * the _mc->sc_fpregs[i] can cause invalid conversions. */
244
245         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
246         os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
247 }
248
249
250 /* md_executionstate_write *****************************************************
251
252    Write the given executionstate back to the context.
253
254 *******************************************************************************/
255
256 void md_executionstate_write(executionstate_t *es, void *context)
257 {
258         ucontext_t *_uc;
259         mcontext_t *_mc;
260         int         i;
261
262         _uc = (ucontext_t *) context;
263         _mc = &_uc->uc_mcontext;
264
265         /* write integer registers */
266         for (i = 0; i < INT_REG_CNT; i++)
267                 _mc->sc_regs[i] = es->intregs[i];
268
269         /* write float registers */
270         /* Do not use the assignment operator '=', as the type of
271          * the _mc->sc_fpregs[i] can cause invalid conversions. */
272
273         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
274         os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
275
276         /* write special registers */
277         _mc->sc_pc           = (ptrint) es->pc;
278         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
279         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
280         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
281 }
282
283
284 /*
285  * These are local overrides for various environment variables in Emacs.
286  * Please do not remove this and leave it at the end of the file, where
287  * Emacs will automagically detect them.
288  * ---------------------------------------------------------------------
289  * Local variables:
290  * mode: c
291  * indent-tabs-mode: t
292  * c-basic-offset: 4
293  * tab-width: 4
294  * End:
295  */