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