* Implemented stacktraces
[cacao.git] / src / vm / jit / mips / md.c
1 /* src/vm/jit/mips/md.c - machine dependent MIPS functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Andreas Krall
28             Reinhard Grafl
29
30    Changes: Christian Thalinger
31
32    $Id: md.c 3023 2005-07-12 23:49:49Z twisti $
33
34 */
35
36
37 #include <assert.h>
38 #include <signal.h>
39 #include <sys/fpu.h>
40 #include <sys/mman.h>
41 #include <unistd.h>
42
43 #include "config.h"
44
45 #include "vm/jit/mips/md-abi.h"
46 #include "vm/jit/mips/types.h"
47
48 #include "vm/exceptions.h"
49 #include "vm/stringlocal.h"
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53
54 /* md_init *********************************************************************
55
56    Do some machine dependent initialization.
57
58 *******************************************************************************/
59
60 void md_init(void)
61 {
62         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
63         /* dummy allocation here to ensure that the GC is initialized.            */
64
65         heap_allocate(1, 0, NULL);
66
67
68         /* Turn off flush-to-zero */
69
70         {
71                 union fpc_csr n;
72                 n.fc_word = get_fpc_csr();
73                 n.fc_struct.flush = 0;
74                 set_fpc_csr(n.fc_word);
75         }
76 }
77
78
79 /* signal_handler_sigsegv ******************************************************
80
81    NullPointerException signal handler for hardware null pointer check.
82
83 *******************************************************************************/
84
85 void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
86 {
87         ucontext_t  *_uc;
88         mcontext_t  *_mc;
89         u4           instr;
90         ptrint       addr;
91         u1          *pv;
92         u1          *sp;
93         functionptr  ra;
94         functionptr  xpc;
95
96         _uc = (struct ucontext *) _p;
97         _mc = &_uc->uc_mcontext;
98
99         instr = *((u4 *) (_mc->gregs[CTX_EPC]));
100         addr = _mc->gregs[(instr >> 21) & 0x1f];
101
102         if (addr == 0) {
103                 pv  = (u1 *) _mc->gregs[REG_PV];
104                 sp  = (u1 *) _mc->gregs[REG_SP];
105                 ra  = (functionptr) _mc->gregs[REG_RA]; /* this is correct for leafs*/
106                 xpc = (functionptr) _mc->gregs[CTX_EPC];
107
108                 _mc->gregs[REG_ITMP1_XPTR] =
109                         (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
110
111                 _mc->gregs[REG_ITMP2_XPC] = (ptrint) xpc;
112                 _mc->gregs[CTX_EPC] = (ptrint) asm_handle_exception;
113
114         } else {
115         addr += (long) ((instr << 16) >> 16);
116
117                 throw_cacao_exception_exit(string_java_lang_InternalError,
118                                                                    "faulting address: 0x%lx at 0x%lx\n",
119                                                                    addr, _mc->gregs[CTX_EPC]);
120         }
121 }
122
123
124 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
125 void thread_restartcriticalsection(ucontext_t *uc)
126 {
127         void *critical;
128
129         critical = thread_checkcritical((void*) uc->uc_mcontext.gregs[CTX_EPC]);
130
131         if (critical)
132                 uc->uc_mcontext.gregs[CTX_EPC] = (ptrint) critical;
133 }
134 #endif
135
136
137 void docacheflush(u1 *p, long bytelen)
138 {
139         u1 *e = p + bytelen;
140         long psize = sysconf(_SC_PAGESIZE);
141         p -= (long) p & (psize - 1);
142         e += psize - ((((long) e - 1) & (psize - 1)) + 1);
143         bytelen = e-p;
144         mprotect(p, bytelen, PROT_READ | PROT_WRITE | PROT_EXEC);
145 }
146
147
148 /* md_stacktrace_get_returnaddress *********************************************
149
150    Returns the return address of the current stackframe, specified by
151    the passed stack pointer and the stack frame size.
152
153 *******************************************************************************/
154
155 functionptr md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
156 {
157         functionptr ra;
158
159         /* on MIPS the return address is located on the top of the stackframe */
160
161         ra = (functionptr) *((u1 **) (sp + framesize - SIZEOF_VOID_P));
162
163         return ra;
164 }
165
166
167 /* codegen_findmethod **********************************************************
168
169    Machine code:
170
171    6b5b4000    jsr     (pv)
172    237affe8    lda     pv,-24(ra)
173
174 *******************************************************************************/
175
176 functionptr codegen_findmethod(functionptr pc)
177 {
178         u1 *ra;
179         u1 *pv;
180         u4  mcode;
181         s2  offset;
182
183         ra = (u1 *) pc;
184         pv = ra;
185
186         /* get offset of first instruction (lda) */
187
188         mcode = *((u4 *) ra);
189
190         if ((mcode >> 16) != 0x67fe) {
191                 log_text("No `daddiu s8,ra,x' instruction found on return address!");
192                 assert(0);
193         }
194
195         offset = (s2) (mcode & 0x0000ffff);
196         pv += offset;
197
198 #if 0
199         /* XXX TWISTI: implement this! */
200
201         /* check for second instruction (ldah) */
202
203         mcode = *((u4 *) (ra + 1 * 4));
204
205         if ((mcode >> 16) == 0x177b) {
206                 offset = (s2) (mcode << 16);
207                 pv += offset;
208         }
209 #endif
210
211         return (functionptr) pv;
212 }
213
214
215 /*
216  * These are local overrides for various environment variables in Emacs.
217  * Please do not remove this and leave it at the end of the file, where
218  * Emacs will automagically detect them.
219  * ---------------------------------------------------------------------
220  * Local variables:
221  * mode: c
222  * indent-tabs-mode: t
223  * c-basic-offset: 4
224  * tab-width: 4
225  * End:
226  */