546da55cea320deec6a50faec3443fc3b84193c8
[cacao.git] / src / vm / jit / x86_64 / md.c
1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 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.c 8247 2007-07-31 12:06:44Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdlib.h>
34
35 #include "vm/jit/x86_64/md-abi.h"
36
37 #if defined(ENABLE_THREADS)
38 # include "threads/native/threads.h"
39 #endif
40
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/codegen-common.h"
43 #include "vm/jit/stacktrace.h"
44
45 #if defined(ENABLE_REPLACEMENT)
46 # include "vm/exceptions.h"
47 #endif
48
49 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
50 #include "vmcore/options.h" /* XXX debug */
51 #include "vm/jit/disass.h" /* XXX debug */
52 #endif
53
54
55 /* md_init *********************************************************************
56
57    Do some machine dependent initialization.
58
59 *******************************************************************************/
60
61 void md_init(void)
62 {
63         /* nothing to do */
64 }
65
66
67 /* md_stacktrace_get_returnaddress *********************************************
68
69    Returns the return address of the current stackframe, specified by
70    the passed stack pointer and the stack frame size.
71
72 *******************************************************************************/
73
74 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
75 {
76         u1 *ra;
77
78         /* on x86_64 the return address is above the current stack frame */
79
80         ra = *((u1 **) (sp + framesize));
81
82         return ra;
83 }
84
85
86 /* md_get_method_patch_address *************************************************
87
88    Gets the patch address of the currently compiled method. The offset
89    is extracted from the load instruction(s) before the jump and added
90    to the right base address (PV or REG_METHODPTR).
91
92    INVOKESTATIC/SPECIAL:
93
94    4d 8b 15 e2 fe ff ff             mov    -286(%rip),%r10
95    49 ff d2                         rex64Z callq  *%r10
96
97    INVOKEVIRTUAL:
98
99    4c 8b 17                         mov    (%rdi),%r10
100    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
101    48 ff d3                         rex64 callq  *%rax
102
103    INVOKEINTERFACE:
104
105    4c 8b 17                         mov    (%rdi),%r10
106    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
107    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
108    48 ff d3                         rex64 callq  *%r11
109
110 *******************************************************************************/
111
112 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
113 {
114         u1  mcode;
115         s4  offset;
116         u1 *pa;                             /* patch address                      */
117
118         /* go back to the actual call instruction (3-bytes) */
119
120         ra = ra - 3;
121
122         /* get the last byte of the call */
123
124         mcode = ra[2];
125
126         /* check for the different calls */
127
128         if (mcode == 0xd2) {
129                 /* INVOKESTATIC/SPECIAL */
130
131                 /* Get the offset from the instruction (the offset address is
132                    4-bytes before the call instruction). */
133
134                 offset = *((s4 *) (ra - 4));
135
136                 /* add the offset to the return address (IP-relative addressing) */
137
138                 pa = ra + offset;
139         }
140         else if (mcode == 0xd3) {
141                 /* INVOKEVIRTUAL/INTERFACE */
142
143                 /* return NULL if no mptr was specified (used for replacement) */
144
145                 if (mptr == NULL)
146                         return NULL;
147
148                 /* Get the offset from the instruction (the offset address is
149                    4-bytes before the call instruction). */
150
151                 offset = *((s4 *) (ra - 4));
152
153                 /* add the offset to the method pointer */
154
155                 pa = mptr + offset;
156         }
157         else {
158                 /* catch any problems */
159
160                 vm_abort("md_get_method_patch_address: unknown instruction %x", mcode);
161
162                 /* keep compiler happy */
163
164                 pa = NULL;
165         }
166
167         return pa;
168 }
169
170
171 /* md_codegen_get_pv_from_pc ***************************************************
172
173    On this architecture just a wrapper function to
174    codegen_get_pv_from_pc.
175
176 *******************************************************************************/
177
178 u1 *md_codegen_get_pv_from_pc(u1 *ra)
179 {
180         u1 *pv;
181
182         /* Get the start address of the function which contains this
183        address from the method table. */
184
185         pv = codegen_get_pv_from_pc(ra);
186
187         return pv;
188 }
189
190
191 /* md_cacheflush ***************************************************************
192
193    Calls the system's function to flush the instruction and data
194    cache.
195
196 *******************************************************************************/
197
198 void md_cacheflush(u1 *addr, s4 nbytes)
199 {
200         /* do nothing */
201 }
202
203
204 /* md_icacheflush **************************************************************
205
206    Calls the system's function to flush the instruction cache.
207
208 *******************************************************************************/
209
210 void md_icacheflush(u1 *addr, s4 nbytes)
211 {
212         /* do nothing */
213 }
214
215
216 /* md_dcacheflush **************************************************************
217
218    Calls the system's function to flush the data cache.
219
220 *******************************************************************************/
221
222 void md_dcacheflush(u1 *addr, s4 nbytes)
223 {
224         /* do nothing */
225 }
226
227
228 /* md_patch_replacement_point **************************************************
229
230    Patch the given replacement point.
231
232 *******************************************************************************/
233
234 #if defined(ENABLE_REPLACEMENT)
235 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
236 {
237         u8 mcode;
238
239         /* XXX this is probably unsafe! */
240
241         if (index < 0) {
242                 /* write spinning instruction */
243                 *(u2*)(rp->pc) = 0xebfe;
244
245                 /* write 5th byte */
246                 rp->pc[4] = savedmcode[4];
247
248                 /* write first word */
249                 *(u4*)(rp->pc) = *(u4*)(savedmcode);
250         }
251         else {
252                 /* save the current machine code */
253                 *(u4*)(savedmcode) = *(u4*)(rp->pc);
254                 savedmcode[4] = rp->pc[4];
255
256                 /* build the machine code for the patch */
257                 assert(0); /* XXX build trap instruction below */
258                 mcode = 0;
259
260                 /* write spinning instruction */
261                 *(u2*)(rp->pc) = 0xebfe;
262
263                 /* write 5th byte */
264                 rp->pc[4] = (mcode >> 32);
265
266                 /* write first word */
267                 *(u4*)(rp->pc) = (u4) mcode;
268         }
269
270 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
271         {
272                 u1* u1ptr = rp->pc;
273                 DISASSINSTR(u1ptr);
274                 fflush(stdout);
275         }
276 #endif
277                         
278     /* XXX if required asm_cacheflush(rp->pc,8); */
279 }
280 #endif /* defined(ENABLE_REPLACEMENT) */
281
282 /*
283  * These are local overrides for various environment variables in Emacs.
284  * Please do not remove this and leave it at the end of the file, where
285  * Emacs will automagically detect them.
286  * ---------------------------------------------------------------------
287  * Local variables:
288  * mode: c
289  * indent-tabs-mode: t
290  * c-basic-offset: 4
291  * tab-width: 4
292  * End:
293  * vim:noexpandtab:sw=4:ts=4:
294  */