cb8e2639e0aa3dc7d3184e4ac0fd3691c969b918
[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    $Id: md-os.c 7252 2007-01-29 21:09:01Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
34 #include <signal.h>
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/mips/md-abi.h"
40
41 #include "mm/gc-common.h"
42
43 #include "vm/exceptions.h"
44 #include "vm/stringlocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/stacktrace.h"
48
49
50 /* md_init *********************************************************************
51
52    Do some machine dependent initialization.
53
54 *******************************************************************************/
55
56 void md_init(void)
57 {
58         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
59         /* dummy allocation here to ensure that the GC is initialized.            */
60
61 #if defined(ENABLE_GC_BOEHM)
62         heap_allocate(1, 0, NULL);
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 /* md_signal_handler_sigsegv ***************************************************
79
80    NullPointerException signal handler for hardware null pointer
81    check.
82
83 *******************************************************************************/
84
85 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
86 {
87         ucontext_t  *_uc;
88         mcontext_t  *_mc;
89         greg_t      *_gregs;
90         u4           instr;
91         ptrint       addr;
92         u1          *pv;
93         u1          *sp;
94         u1          *ra;
95         u1          *xpc;
96
97         _uc    = (struct ucontext *) _p;
98         _mc    = &_uc->uc_mcontext;
99
100 #if defined(__UCLIBC__)
101         _gregs = _mc->gpregs;
102 #else   
103         _gregs = _mc->gregs;
104 #endif
105
106         /* In glibc's ucontext.h the registers are defined as long long,
107            even for MIPS32, so we cast them.  This is not the case for
108            uClibc. */
109
110         pv  = (u1 *) (ptrint) _gregs[REG_PV];
111         sp  = (u1 *) (ptrint) _gregs[REG_SP];
112         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
113
114 #if defined(__UCLIBC__)
115         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
116 #else
117         xpc = (u1 *) (ptrint) _mc->pc;
118 #endif
119
120         instr = *((u4 *) xpc);
121         addr  = _gregs[(instr >> 21) & 0x1f];
122
123         if (addr == 0) {
124                 _gregs[REG_ITMP1_XPTR] =
125                         (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
126
127                 _gregs[REG_ITMP2_XPC] = (ptrint) xpc;
128
129 #if defined(__UCLIBC__)
130                 _gregs[CTX_EPC] = (ptrint) asm_handle_exception;
131 #else
132                 _mc->pc         = (ptrint) asm_handle_exception;
133 #endif
134         }
135         else {
136                 codegen_get_pv_from_pc(xpc);
137
138                 /* this should not happen */
139
140                 assert(0);
141         }
142 }
143
144
145 /* md_signal_handler_sigusr2 ***************************************************
146
147    DOCUMENT ME
148
149 *******************************************************************************/
150
151 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
152 {
153 }
154
155
156 #if defined(ENABLE_THREADS)
157 void thread_restartcriticalsection(ucontext_t *_uc)
158 {
159         mcontext_t *_mc;
160         u1         *pc;
161         u1         *npc;
162
163         _mc = &_uc->uc_mcontext;
164
165 #if defined(__UCLIBC__)
166         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
167 #else
168         pc = (u1 *) (ptrint) _mc->pc;
169 #endif
170
171         npc = critical_find_restart_point(pc);
172
173         if (npc != NULL) {
174 #if defined(__UCLIBC__)
175                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
176 #else
177                 _mc->pc              = (ptrint) npc;
178 #endif
179         }
180 }
181 #endif
182
183
184 /*
185  * These are local overrides for various environment variables in Emacs.
186  * Please do not remove this and leave it at the end of the file, where
187  * Emacs will automagically detect them.
188  * ---------------------------------------------------------------------
189  * Local variables:
190  * mode: c
191  * indent-tabs-mode: t
192  * c-basic-offset: 4
193  * tab-width: 4
194  * End:
195  * vim:noexpandtab:sw=4:ts=4:
196  */