* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / vm / jit / mips / irix / md-os.c
1 /* src/vm/jit/mips/irix/md-os.c - machine dependent MIPS IRIX 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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <signal.h>
32 #include <stdint.h>
33 #include <sys/fpu.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/mips/codegen.h"
38 #include "vm/jit/mips/md-abi.h"
39
40 #include "mm/gc-common.h"
41
42 #include "vm/global.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/codegen-common.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 /* md_init *********************************************************************
52
53    Do some machine dependent initialization.
54
55 *******************************************************************************/
56
57 void md_init(void)
58 {
59         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
60         /* dummy allocation here to ensure that the GC is initialized.            */
61
62 #if defined(ENABLE_GC_BOEHM)
63         (void) GCNEW(u1);
64 #endif
65
66
67         /* Turn off flush-to-zero */
68
69         {
70                 union fpc_csr n;
71                 n.fc_word = get_fpc_csr();
72                 n.fc_struct.flush = 0;
73                 set_fpc_csr(n.fc_word);
74         }
75 }
76
77
78 /* md_signal_handler_sigsegv ***************************************************
79
80    Signal handler for hardware-exceptions.
81
82 *******************************************************************************/
83
84 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
85 {
86         stackframeinfo  sfi;
87         ucontext_t     *_uc;
88         mcontext_t     *_mc;
89         u1             *pv;
90         u1             *sp;
91         u1             *ra;
92         u1             *xpc;
93         u4              mcode;
94         int             d;
95         int             s1;
96         int16_t         disp;
97         intptr_t        val;
98         intptr_t        addr;
99         int              type;
100         void           *p;
101
102         _uc = (struct ucontext *) _p;
103         _mc = &_uc->uc_mcontext;
104
105         pv  = (u1 *) _mc->gregs[REG_PV];
106         sp  = (u1 *) _mc->gregs[REG_SP];
107         ra  = (u1 *) _mc->gregs[REG_RA];             /* this is correct for leafs */
108         xpc = (u1 *) _mc->gregs[CTX_EPC];
109
110         /* get exception-throwing instruction */
111
112         mcode = *((u4 *) xpc);
113
114         d    = M_ITYPE_GET_RT(mcode);
115         s1   = M_ITYPE_GET_RS(mcode);
116         disp = M_ITYPE_GET_IMM(mcode);
117
118         /* check for special-load */
119
120         if (s1 == REG_ZERO) {
121                 /* we use the exception type as load displacement */
122
123                 type = disp;
124                 val  = _mc->gregs[d];
125         }
126         else {
127                 /* This is a normal NPE: addr must be NULL and the NPE-type
128                    define is 0. */
129
130                 addr = _mc->gregs[s1];
131                 type = (int) addr;
132                 val  = 0;
133         }
134
135         /* create stackframeinfo */
136
137         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
138
139         /* Handle the type. */
140
141         p = signal_handle(xpc, type, val);
142
143         /* remove stackframeinfo */
144
145         stacktrace_remove_stackframeinfo(&sfi);
146
147         /* set registers (only if exception object ready) */
148
149         if (p != NULL) {
150                 _mc->gregs[REG_ITMP1_XPTR] = (intptr_t) p;
151                 _mc->gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
152                 _mc->gregs[CTX_EPC]        = (intptr_t) asm_handle_exception;
153         }
154 }
155
156
157 /* md_critical_section_restart *************************************************
158
159    Search the critical sections tree for a matching section and set
160    the PC to the restart point, if necessary.
161
162 *******************************************************************************/
163
164 #if defined(ENABLE_THREADS)
165 void md_critical_section_restart(ucontext_t *_uc)
166 {
167         mcontext_t *_mc;
168         u1         *pc;
169         u1         *npc;
170
171         _mc = &_uc->uc_mcontext;
172
173         pc = (u1 *) _mc->gregs[CTX_EPC];
174
175         npc = critical_find_restart_point(pc);
176
177         if (npc != NULL) {
178                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
179                 _mc->gregs[CTX_EPC] = (ptrint) npc;
180         }
181 }
182 #endif
183
184
185 /*
186  * These are local overrides for various environment variables in Emacs.
187  * Please do not remove this and leave it at the end of the file, where
188  * Emacs will automagically detect them.
189  * ---------------------------------------------------------------------
190  * Local variables:
191  * mode: c
192  * indent-tabs-mode: t
193  * c-basic-offset: 4
194  * tab-width: 4
195  * End:
196  */