* Implemented idiv/irem, ldiv/lrem, aastore, arraycheckcast inline
[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 3002 2005-07-12 16:02:45Z 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
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         heap_allocate(1, 0, NULL);
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 /* signal_handler_sigsegv ******************************************************
79
80    NullPointerException signal handler for hardware null pointer check.
81
82 *******************************************************************************/
83
84 void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
85 {
86         ucontext_t *_uc;
87         mcontext_t *_mc;
88         u4          instr;
89         ptrint      addr;
90
91         _uc = (struct ucontext *) _p;
92         _mc = &_uc->uc_mcontext;
93
94         instr = *((u4 *) (_mc->gregs[CTX_EPC]));
95         addr = _mc->gregs[(instr >> 21) & 0x1f];
96
97         if (addr == 0) {
98                 _mc->gregs[REG_ITMP1_XPTR] = (ptrint) new_nullpointerexception();
99                 _mc->gregs[REG_ITMP2_XPC] = _mc->gregs[CTX_EPC];
100                 _mc->gregs[CTX_EPC] = (ptrint) asm_handle_exception;
101
102         } else {
103         addr += (long) ((instr << 16) >> 16);
104
105                 throw_cacao_exception_exit(string_java_lang_InternalError,
106                                                                    "faulting address: 0x%lx at 0x%lx\n",
107                                                                    addr, _mc->gregs[CTX_EPC]);
108         }
109 }
110
111
112 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
113 void thread_restartcriticalsection(ucontext_t *uc)
114 {
115         void *critical;
116
117         critical = thread_checkcritical((void*) uc->uc_mcontext.gregs[CTX_EPC]);
118
119         if (critical)
120                 uc->uc_mcontext.gregs[CTX_EPC] = (ptrint) critical;
121 }
122 #endif
123
124
125 void docacheflush(u1 *p, long bytelen)
126 {
127         u1 *e = p + bytelen;
128         long psize = sysconf(_SC_PAGESIZE);
129         p -= (long) p & (psize - 1);
130         e += psize - ((((long) e - 1) & (psize - 1)) + 1);
131         bytelen = e-p;
132         mprotect(p, bytelen, PROT_READ | PROT_WRITE | PROT_EXEC);
133 }
134
135
136 /* md_stacktrace_get_returnaddress *********************************************
137
138    Returns the return address of the current stackframe, specified by
139    the passed stack pointer and the stack frame size.
140
141 *******************************************************************************/
142
143 functionptr md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
144 {
145         functionptr ra;
146
147         /* on MIPS the return address is located on the top of the stackframe */
148
149         ra = (functionptr) *((u1 **) (sp + framesize - SIZEOF_VOID_P));
150
151         return ra;
152 }
153
154
155 /* codegen_findmethod **********************************************************
156
157    Machine code:
158
159    6b5b4000    jsr     (pv)
160    237affe8    lda     pv,-24(ra)
161
162 *******************************************************************************/
163
164 functionptr codegen_findmethod(functionptr pc)
165 {
166         u1 *ra;
167         u1 *pv;
168         u4  mcode;
169         s2  offset;
170
171         ra = (u1 *) pc;
172         pv = ra;
173
174         /* get offset of first instruction (lda) */
175
176         mcode = *((u4 *) ra);
177
178         if ((mcode >> 16) != 0x237a) {
179                 log_text("No `lda pv,x(ra)' instruction found on return address!");
180                 assert(0);
181         }
182
183         offset = (s2) (mcode & 0x0000ffff);
184         pv += offset;
185
186         /* check for second instruction (ldah) */
187
188         mcode = *((u4 *) (ra + 1 * 4));
189
190         if ((mcode >> 16) == 0x177b) {
191                 offset = (s2) (mcode << 16);
192                 pv += offset;
193         }
194
195         return (functionptr) pv;
196 }
197
198
199 /*
200  * These are local overrides for various environment variables in Emacs.
201  * Please do not remove this and leave it at the end of the file, where
202  * Emacs will automagically detect them.
203  * ---------------------------------------------------------------------
204  * Local variables:
205  * mode: c
206  * indent-tabs-mode: t
207  * c-basic-offset: 4
208  * tab-width: 4
209  * End:
210  */