2fec3a5bf1b66c97d2a5236058f92b4eaedaf9fb
[cacao.git] / src / vm / jit / mips / linux / md-os.c
1 /* src/vm/jit/mips/linux/md-os.c - machine dependent MIPS Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
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 <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
30 #include <signal.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/mips/codegen.h"
37 #include "vm/jit/mips/md.h"
38 #include "vm/jit/mips/md-abi.h"
39
40 #include "mm/gc.hpp"
41 #include "mm/memory.hpp"
42
43 #include "vm/signallocal.hpp"
44
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/trap.hpp"
47
48
49 /* md_init *********************************************************************
50
51    Do some machine dependent initialization.
52
53 *******************************************************************************/
54
55 void md_init(void)
56 {
57         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
58            a dummy allocation here to ensure that the GC is
59            initialized. */
60
61 #if defined(ENABLE_GC_BOEHM)
62         (void) GCNEW(int);
63 #endif
64
65 #if 0
66         /* Turn off flush-to-zero */
67
68         {
69                 union fpc_csr n;
70                 n.fc_word = get_fpc_csr();
71                 n.fc_struct.flush = 0;
72                 set_fpc_csr(n.fc_word);
73         }
74 #endif
75 }
76
77
78 /**
79  * NullPointerException signal handler for hardware null pointer check.
80  */
81 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
82 {
83         ucontext_t* _uc = (struct ucontext *) _p;
84         mcontext_t* _mc = &_uc->uc_mcontext;
85
86         void* xpc = (void*) (_mc->pc - 4);
87
88         // Handle the trap.
89         trap_handle(TRAP_SIGSEGV, xpc, _p);
90 }
91
92
93 /**
94  * Illegal Instruction signal handler for hardware exception checks.
95  */
96 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
97 {
98         ucontext_t* _uc = (struct ucontext *) _p;
99         mcontext_t* _mc = &_uc->uc_mcontext;
100
101         void* xpc = (void*) (_mc->pc - 4);
102
103         // Handle the trap.
104         trap_handle(TRAP_SIGILL, xpc, _p);
105 }
106
107
108 /* md_signal_handler_sigusr2 ***************************************************
109
110    DOCUMENT ME
111
112 *******************************************************************************/
113
114 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
115 {
116 }
117
118
119 /**
120  * Read the given context into an executionstate.
121  *
122  * @param es      execution state
123  * @param context machine context
124  */
125 void md_executionstate_read(executionstate_t* es, void* context)
126 {
127         ucontext_t* _uc;
128         mcontext_t* _mc;
129         greg_t*     _gregs;
130         int         i;
131
132         _uc = (ucontext_t*) context;
133         _mc = &_uc->uc_mcontext;
134
135 #if defined(__UCLIBC__)
136         _gregs = _mc->gpregs;
137 #else   
138         _gregs = _mc->gregs;
139 #endif
140
141         /* Read special registers. */
142
143         /* In glibc's ucontext.h the registers are defined as long long,
144            even for MIPS32, so we cast them.  This is not the case for
145            uClibc. */
146
147 #if defined(__UCLIBC__)
148         es->pc = _gregs[CTX_EPC];
149 #else
150         es->pc = (void*) (uintptr_t) _mc->pc;
151 #endif
152
153         es->sp = (void*) (uintptr_t) _gregs[REG_SP];
154         es->pv = (void*) (uintptr_t) _gregs[REG_PV];
155         es->ra = (void*) (uintptr_t) _gregs[REG_RA];
156
157         /* Read integer registers. */
158
159         for (i = 0; i < INT_REG_CNT; i++)
160                 es->intregs[i] = _gregs[i];
161
162         /* Read float registers. */
163
164         /* Do not use the assignment operator '=', as the type of the
165            _mc->fpregs[i] can cause invalid conversions. */
166
167         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
168         os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
169 }
170
171
172 /**
173  * Write the given executionstate back to the context.
174  *
175  * @param es      execution state
176  * @param context machine context
177  */
178 void md_executionstate_write(executionstate_t* es, void* context)
179 {
180         ucontext_t* _uc;
181         mcontext_t* _mc;
182         greg_t*     _gregs;
183         int         i;
184
185         _uc = (ucontext_t *) context;
186         _mc = &_uc->uc_mcontext;
187
188 #if defined(__UCLIBC__)
189         _gregs = _mc->gpregs;
190 #else   
191         _gregs = _mc->gregs;
192 #endif
193
194         /* Write integer registers. */
195
196         for (i = 0; i < INT_REG_CNT; i++)
197                 _gregs[i] = es->intregs[i];
198
199         /* Write float registers. */
200
201         /* Do not use the assignment operator '=', as the type of the
202            _mc->fpregs[i] can cause invalid conversions. */
203
204         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
205         os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
206
207         /* Write special registers. */
208
209 #if defined(__UCLIBC__)
210         _gregs[CTX_EPC] = es->pc;
211 #else
212         _mc->pc         = (uintptr_t) es->pc;
213 #endif
214
215         _gregs[REG_SP]  = (uintptr_t) es->sp;
216         _gregs[REG_PV]  = (uintptr_t) es->pv;
217         _gregs[REG_RA]  = (uintptr_t) es->ra;
218 }
219
220
221 /*
222  * These are local overrides for various environment variables in Emacs.
223  * Please do not remove this and leave it at the end of the file, where
224  * Emacs will automagically detect them.
225  * ---------------------------------------------------------------------
226  * Local variables:
227  * mode: c
228  * indent-tabs-mode: t
229  * c-basic-offset: 4
230  * tab-width: 4
231  * End:
232  * vim:noexpandtab:sw=4:ts=4:
233  */