* Updated header: Added 2006. Changed address of FSF. Changed email
[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 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
30    Changes: Christian Thalinger
31
32    $Id: md-os.c 4357 2006-01-22 23:33:38Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include <assert.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/boehm.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         heap_allocate(1, 0, NULL);
67
68 #if 0
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 #endif
78 }
79
80
81 /* md_signal_handler_sigsegv ***************************************************
82
83    NullPointerException signal handler for hardware null pointer
84    check.
85
86 *******************************************************************************/
87
88 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
89 {
90         ucontext_t  *_uc;
91         mcontext_t  *_mc;
92         u4           instr;
93         ptrint       addr;
94         u1          *pv;
95         u1          *sp;
96         u1          *ra;
97         u1          *xpc;
98
99         _uc = (struct ucontext *) _p;
100         _mc = &_uc->uc_mcontext;
101
102         /* in ucontext.h the registers are defined as long long, even for
103            MIPS32, so we cast them */
104         
105         instr = *((u4 *) ((ptrint) _mc->pc));
106         addr = _mc->gregs[(instr >> 21) & 0x1f];
107
108         if (addr == 0) {
109                 pv  = (u1 *) (ptrint) _mc->gregs[REG_PV];
110                 sp  = (u1 *) (ptrint) _mc->gregs[REG_SP];
111                 ra  = (u1 *) (ptrint) _mc->gregs[REG_RA]; /* this is correct for leafs*/
112                 xpc = (u1 *) (ptrint) _mc->pc;
113
114                 _mc->gregs[REG_ITMP1_XPTR] =
115                         (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
116
117                 _mc->gregs[REG_ITMP2_XPC] = (ptrint) xpc;
118                 _mc->pc = (ptrint) asm_handle_exception;
119
120         } else {
121                 addr += (long) ((instr << 16) >> 16);
122
123                 throw_cacao_exception_exit(string_java_lang_InternalError,
124                                                                    "faulting address: 0x%lx at 0x%lx\n",
125                                                                    addr, _mc->pc);
126         }
127 }
128
129
130 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
131 void thread_restartcriticalsection(ucontext_t *_uc)
132 {
133         mcontext_t *_mc;
134         void *critical;
135
136         _mc = &_uc->uc_mcontext;
137
138         critical = thread_checkcritical((void *) (ptrint) _mc->pc);
139
140         if (critical)
141                 _mc->pc = (ptrint) critical;
142 }
143 #endif
144
145
146 /*
147  * These are local overrides for various environment variables in Emacs.
148  * Please do not remove this and leave it at the end of the file, where
149  * Emacs will automagically detect them.
150  * ---------------------------------------------------------------------
151  * Local variables:
152  * mode: c
153  * indent-tabs-mode: t
154  * c-basic-offset: 4
155  * tab-width: 4
156  * End:
157  */