02c978b795ae6dd54c22735d4b5d5efa9ecc24f6
[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;
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;
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         vm_abort("md_executionstate_read: PLEASE REVISE ME!");
133
134         _uc = (ucontext_t*) context;
135         _mc = &_uc->uc_mcontext;
136
137 #if defined(__UCLIBC__)
138         _gregs = _mc->gpregs;
139 #else   
140         _gregs = _mc->gregs;
141 #endif
142
143         /* Read special registers. */
144
145         /* In glibc's ucontext.h the registers are defined as long long,
146            even for MIPS32, so we cast them.  This is not the case for
147            uClibc. */
148
149 #if defined(__UCLIBC__)
150         es->pc = _gregs[CTX_EPC];
151 #else
152         es->pc = (void*) (uintptr_t) _mc->pc;
153 #endif
154
155         es->sp = (void*) (uintptr_t) _gregs[REG_SP];
156         es->pv = (void*) (uintptr_t) _gregs[REG_PV];
157         es->ra = (void*) (uintptr_t) _gregs[REG_RA];
158
159         /* Read integer registers. */
160
161         for (i = 0; i < INT_REG_CNT; i++)
162                 es->intregs[i] = _gregs[i];
163
164         /* Read float registers. */
165
166         /* Do not use the assignment operator '=', as the type of the
167            _mc->fpregs[i] can cause invalid conversions. */
168
169         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
170         os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
171 }
172
173
174 /**
175  * Write the given executionstate back to the context.
176  *
177  * @param es      execution state
178  * @param context machine context
179  */
180 void md_executionstate_write(executionstate_t* es, void* context)
181 {
182         ucontext_t* _uc;
183         mcontext_t* _mc;
184         greg_t*     _gregs;
185         int         i;
186
187         vm_abort("md_executionstate_write: PLEASE REVISE ME!");
188
189         _uc = (ucontext_t *) context;
190         _mc = &_uc->uc_mcontext;
191
192         /* Write integer registers. */
193
194         for (i = 0; i < INT_REG_CNT; i++)
195                 _gregs[i] = es->intregs[i];
196
197         /* Write float registers. */
198
199         /* Do not use the assignment operator '=', as the type of the
200            _mc->fpregs[i] can cause invalid conversions. */
201
202         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
203         os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
204
205         /* Write special registers. */
206
207 #if defined(__UCLIBC__)
208         _gregs[CTX_EPC] = es->pc;
209 #else
210         _mc->pc         = (uintptr_t) es->pc;
211 #endif
212
213         _gregs[REG_SP]  = (uintptr_t) es->sp;
214         _gregs[REG_PV]  = (uintptr_t) es->pv;
215         _gregs[REG_RA]  = (uintptr_t) es->ra;
216 }
217
218
219 /*
220  * These are local overrides for various environment variables in Emacs.
221  * Please do not remove this and leave it at the end of the file, where
222  * Emacs will automagically detect them.
223  * ---------------------------------------------------------------------
224  * Local variables:
225  * mode: c
226  * indent-tabs-mode: t
227  * c-basic-offset: 4
228  * tab-width: 4
229  * End:
230  * vim:noexpandtab:sw=4:ts=4:
231  */