b5ed97d497dd58d8a75b374e75dedd59c82d8a38
[cacao.git] / src / vm / jit / x86_64 / md.c
1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 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: Christian Thalinger
28
29    Changes: Edwin Steiner
30
31    $Id: md.c 4698 2006-03-28 14:31:53Z twisti $
32
33 */
34
35
36 #define _GNU_SOURCE
37
38 #include "config.h"
39
40 #include <stdlib.h>
41 #include <ucontext.h>
42
43 #include "vm/jit/x86_64/md-abi.h"
44
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
51 #include "vm/options.h" /* XXX debug */
52 #include "vm/jit/disass.h" /* XXX debug */
53 #endif
54
55
56 /* md_init *********************************************************************
57
58    Do some machine dependent initialization.
59
60 *******************************************************************************/
61
62 void md_init(void)
63 {
64         /* nothing to do */
65 }
66
67
68 /* md_signal_handler_sigsegv ***************************************************
69
70    NullPointerException signal handler for hardware null pointer
71    check.
72
73 *******************************************************************************/
74
75 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
76 {
77         ucontext_t  *_uc;
78         mcontext_t  *_mc;
79         u1          *sp;
80         u1          *ra;
81         u1          *xpc;
82
83         _uc = (ucontext_t *) _p;
84         _mc = &_uc->uc_mcontext;
85
86         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
87            different to the ones in <ucontext.h>. */
88
89         sp  = (u1 *) _mc->gregs[REG_RSP];
90         xpc = (u1 *) _mc->gregs[REG_RIP];
91         ra  = xpc;                          /* return address is equal to xpc     */
92
93         _mc->gregs[REG_RAX] =
94                 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
95
96         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
97         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
98 }
99
100
101 /* md_signal_handler_sigfpe ****************************************************
102
103    ArithmeticException signal handler for hardware divide by zero
104    check.
105
106 *******************************************************************************/
107
108 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
109 {
110         ucontext_t  *_uc;
111         mcontext_t  *_mc;
112         u1          *sp;
113         u1          *ra;
114         u1          *xpc;
115
116         _uc = (ucontext_t *) _p;
117         _mc = &_uc->uc_mcontext;
118
119         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
120            different to the ones in <ucontext.h>. */
121
122         sp  = (u1 *) _mc->gregs[REG_RSP];
123         xpc = (u1 *) _mc->gregs[REG_RIP];
124         ra  = xpc;                          /* return address is equal to xpc     */
125
126         _mc->gregs[REG_RAX] =
127                 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
128
129         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
130         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
131 }
132
133
134 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
135 void thread_restartcriticalsection(ucontext_t *uc)
136 {
137         void *critical;
138
139         critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
140
141         if (critical)
142                 uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
143 }
144 #endif
145
146
147 /* md_stacktrace_get_returnaddress *********************************************
148
149    Returns the return address of the current stackframe, specified by
150    the passed stack pointer and the stack frame size.
151
152 *******************************************************************************/
153
154 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
155 {
156         u1 *ra;
157
158         /* on x86_64 the return address is above the current stack frame */
159
160         ra = *((u1 **) (sp + framesize));
161
162         return ra;
163 }
164
165
166 /* md_codegen_findmethod *******************************************************
167
168    On this architecture just a wrapper function to codegen_findmethod.
169
170 *******************************************************************************/
171
172 u1 *md_codegen_findmethod(u1 *ra)
173 {
174         u1 *pv;
175
176         /* the the start address of the function which contains this
177        address from the method table */
178
179         pv = codegen_findmethod(ra);
180
181         return pv;
182 }
183
184
185 /* md_cacheflush ***************************************************************
186
187    Calls the system's function to flush the instruction and data
188    cache.
189
190 *******************************************************************************/
191
192 void md_cacheflush(u1 *addr, s4 nbytes)
193 {
194         /* do nothing */
195 }
196
197
198 /* md_patch_replacement_point **************************************************
199
200    Patch the given replacement point.
201
202 *******************************************************************************/
203
204 void md_patch_replacement_point(rplpoint *rp)
205 {
206     u8 mcode;
207
208         /* XXX this is probably unsafe! */
209
210         /* save the current machine code */
211         mcode = *(u8*)rp->pc;
212
213         /* write spinning instruction */
214         *(u2*)(rp->pc) = 0xebfe;
215
216         /* write 5th byte */
217         rp->pc[4] = (rp->mcode >> 32);
218
219         /* write first word */
220     *(u4*)(rp->pc) = (u4) rp->mcode;
221
222         /* store saved mcode */
223         rp->mcode = mcode;
224         
225 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
226         {
227                 u1* u1ptr = rp->pc;
228                 DISASSINSTR(u1ptr);
229                 fflush(stdout);
230         }
231 #endif
232                         
233     /* XXX if required asm_cacheflush(rp->pc,8); */
234 }
235
236 /*
237  * These are local overrides for various environment variables in Emacs.
238  * Please do not remove this and leave it at the end of the file, where
239  * Emacs will automagically detect them.
240  * ---------------------------------------------------------------------
241  * Local variables:
242  * mode: c
243  * indent-tabs-mode: t
244  * c-basic-offset: 4
245  * tab-width: 4
246  * End:
247  * vim:noexpandtab:sw=4:ts=4:
248  */