* src/vm/jit/trap.c (trap_handle) [__ALPHA__]: Use executionstate for trap
[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         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
160
161         // Set registers
162         if (p == NULL) {
163                 // We need to set the PC because we adjusted it above.
164                 _mc->sc_pc                   = (uintptr_t) xpc;
165         }
166 }
167
168
169 /* md_signal_handler_sigusr1 ***************************************************
170
171    Signal handler for suspending threads.
172
173 *******************************************************************************/
174
175 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
176 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
177 {
178         ucontext_t *_uc;
179         mcontext_t *_mc;
180         u1         *pc;
181         u1         *sp;
182
183         _uc = (ucontext_t *) _p;
184         _mc = &_uc->uc_mcontext;
185
186         /* get the PC and SP for this thread */
187         pc = (u1 *) _mc->sc_pc;
188         sp = (u1 *) _mc->sc_regs[REG_SP];
189
190         /* now suspend the current thread */
191         threads_suspend_ack(pc, sp);
192 }
193 #endif
194
195
196 /* md_signal_handler_sigusr2 ***************************************************
197
198    Signal handler for profiling sampling.
199
200 *******************************************************************************/
201
202 #if defined(ENABLE_THREADS)
203 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
204 {
205         threadobject *tobj;
206         ucontext_t   *_uc;
207         mcontext_t   *_mc;
208         u1           *pc;
209
210         tobj = THREADOBJECT;
211
212         _uc = (ucontext_t *) _p;
213         _mc = &_uc->uc_mcontext;
214
215         pc = (u1 *) _mc->sc_pc;
216
217         tobj->pc = pc;
218 }
219 #endif
220
221
222 /* md_executionstate_read ******************************************************
223
224    Read the given context into an executionstate.
225
226 *******************************************************************************/
227
228 void md_executionstate_read(executionstate_t *es, void *context)
229 {
230         ucontext_t *_uc;
231         mcontext_t *_mc;
232         int         i;
233
234         _uc = (ucontext_t *) context;
235         _mc = &_uc->uc_mcontext;
236
237         /* read special registers */
238         es->pc = (u1 *) _mc->sc_pc;
239         es->sp = (u1 *) _mc->sc_regs[REG_SP];
240         es->pv = (u1 *) _mc->sc_regs[REG_PV];
241         es->ra = (u1 *) _mc->sc_regs[REG_RA];
242
243         /* read integer registers */
244         for (i = 0; i < INT_REG_CNT; i++)
245                 es->intregs[i] = _mc->sc_regs[i];
246
247         /* read float registers */
248         /* Do not use the assignment operator '=', as the type of
249          * the _mc->sc_fpregs[i] can cause invalid conversions. */
250
251         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
252         os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
253 }
254
255
256 /* md_executionstate_write *****************************************************
257
258    Write the given executionstate back to the context.
259
260 *******************************************************************************/
261
262 void md_executionstate_write(executionstate_t *es, void *context)
263 {
264         ucontext_t *_uc;
265         mcontext_t *_mc;
266         int         i;
267
268         _uc = (ucontext_t *) context;
269         _mc = &_uc->uc_mcontext;
270
271         /* write integer registers */
272         for (i = 0; i < INT_REG_CNT; i++)
273                 _mc->sc_regs[i] = es->intregs[i];
274
275         /* write float registers */
276         /* Do not use the assignment operator '=', as the type of
277          * the _mc->sc_fpregs[i] can cause invalid conversions. */
278
279         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
280         os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
281
282         /* write special registers */
283         _mc->sc_pc           = (ptrint) es->pc;
284         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
285         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
286         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
287 }
288
289
290 /*
291  * These are local overrides for various environment variables in Emacs.
292  * Please do not remove this and leave it at the end of the file, where
293  * Emacs will automagically detect them.
294  * ---------------------------------------------------------------------
295  * Local variables:
296  * mode: c
297  * indent-tabs-mode: t
298  * c-basic-offset: 4
299  * tab-width: 4
300  * End:
301  */