* src/vm/jit/mips/md-abi.h: Merged MIPS32 code.
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Andreas Krall
28             Reinhard Grafl
29             Christian Thalinger
30
31    $Id: md-os.c 7206 2007-01-11 22:39:52Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
40 #include <signal.h>
41 #include <ucontext.h>
42
43 #include "vm/types.h"
44
45 #include "vm/jit/mips/md-abi.h"
46
47 #include "mm/gc-common.h"
48 #include "vm/exceptions.h"
49 #include "vm/signallocal.h"
50 #include "vm/stringlocal.h"
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/stacktrace.h"
53
54
55 /* md_init *********************************************************************
56
57    Do some machine dependent initialization.
58
59 *******************************************************************************/
60
61 void md_init(void)
62 {
63         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
64         /* dummy allocation here to ensure that the GC is initialized.            */
65
66 #if defined(ENABLE_GC_BOEHM)
67         heap_allocate(1, 0, NULL);
68 #endif
69
70 #if 0
71         /* Turn off flush-to-zero */
72
73         {
74                 union fpc_csr n;
75                 n.fc_word = get_fpc_csr();
76                 n.fc_struct.flush = 0;
77                 set_fpc_csr(n.fc_word);
78         }
79 #endif
80 }
81
82
83 /* md_signal_handler_sigsegv ***************************************************
84
85    NullPointerException signal handler for hardware null pointer
86    check.
87
88 *******************************************************************************/
89
90 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
91 {
92         ucontext_t  *_uc;
93         mcontext_t  *_mc;
94         greg_t      *_gregs;
95         u4           instr;
96         ptrint       addr;
97         u1          *pv;
98         u1          *sp;
99         u1          *ra;
100         u1          *xpc;
101
102         _uc    = (struct ucontext *) _p;
103         _mc    = &_uc->uc_mcontext;
104
105 #if defined(__UCLIBC__)
106         _gregs = _mc->gpregs;
107 #else   
108         _gregs = _mc->gregs;
109 #endif
110
111         /* In glibc's ucontext.h the registers are defined as long long,
112            even for MIPS32, so we cast them.  This is not the case for
113            uClibc. */
114
115         pv  = (u1 *) (ptrint) _gregs[REG_PV];
116         sp  = (u1 *) (ptrint) _gregs[REG_SP];
117         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
118
119 #if defined(__UCLIBC__)
120         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
121 #else
122         xpc = (u1 *) (ptrint) _mc->pc;
123 #endif
124
125         instr = *((u4 *) xpc);
126         addr  = _gregs[(instr >> 21) & 0x1f];
127
128         if (addr == 0) {
129                 _gregs[REG_ITMP1_XPTR] =
130                         (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
131
132                 _gregs[REG_ITMP2_XPC] = (ptrint) xpc;
133
134 #if defined(__UCLIBC__)
135                 _gregs[CTX_EPC] = (ptrint) asm_handle_exception;
136 #else
137                 _mc->pc         = (ptrint) asm_handle_exception;
138 #endif
139         }
140         else {
141                 addr += (long) ((instr << 16) >> 16);
142
143                 throw_cacao_exception_exit(string_java_lang_InternalError,
144                                                                    "faulting address: 0x%lx at 0x%lx\n",
145                                                                    addr, xpc);
146         }
147 }
148
149
150 /* md_signal_handler_sigusr2 ***************************************************
151
152    DOCUMENT ME
153
154 *******************************************************************************/
155
156 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
157 {
158 }
159
160
161 #if defined(ENABLE_THREADS)
162 void thread_restartcriticalsection(ucontext_t *_uc)
163 {
164         mcontext_t *_mc;
165         u1         *pc;
166         u1         *npc;
167
168         _mc = &_uc->uc_mcontext;
169
170 #if defined(__UCLIBC__)
171         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
172 #else
173         pc = (u1 *) (ptrint) _mc->pc;
174 #endif
175
176         npc = critical_find_restart_point(pc);
177
178         if (npc != NULL) {
179 #if defined(__UCLIBC__)
180                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
181 #else
182                 _mc->pc              = (ptrint) npc;
183 #endif
184         }
185 }
186 #endif
187
188
189 /*
190  * These are local overrides for various environment variables in Emacs.
191  * Please do not remove this and leave it at the end of the file, where
192  * Emacs will automagically detect them.
193  * ---------------------------------------------------------------------
194  * Local variables:
195  * mode: c
196  * indent-tabs-mode: t
197  * c-basic-offset: 4
198  * tab-width: 4
199  * End:
200  * vim:noexpandtab:sw=4:ts=4:
201  */