Merged revisions 8187-8244 via svnmerge from
[cacao.git] / src / vm / jit / m68k / md.c
1 /*      src/vm/jit/m68k/md.c
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: arch.h 5330 2006-09-05 18:43:12Z edwin $
26
27 */
28 #include "config.h"
29
30 #include <assert.h>
31
32 #include "md-os.h"
33
34 #include "vm/types.h"
35 #include "vm/jit/codegen-common.h"
36 #include "vm/jit/md.h"
37
38 #include "offsets.h"
39 #include "vm/vm.h"
40 #include "vmcore/class.h"
41 #include "vmcore/linker.h"
42 #include "vmcore/method.h"
43 #include "mm/memory.h"
44 #include "vm/jit/asmpart.h"
45 /*
46  *      As a sanity measuremnt we assert the offset.h values in here as m68k gets
47  *      crosscompiled for sure and noone thinks of offset.h wen changing compile flags
48  *      and subtile bugs will result...
49  *
50  *      m68k uses the trap instruction for hardware exceptions, need to register
51  *      according signal handler
52  */
53 void md_init(void) 
54 {
55         assert(sizeof(vm_arg) == sizevmarg);
56         assert(OFFSET(vftbl_t, baseval) == offbaseval);
57         assert(OFFSET(vftbl_t, diffval) == offdiffval);
58         assert(OFFSET(vm_arg, type) == offvmargtype);
59         assert(OFFSET(vm_arg, data) == offvmargdata);
60         assert(OFFSET(castinfo, super_baseval) == offcast_super_baseval);
61         assert(OFFSET(castinfo, super_diffval) == offcast_super_diffval);
62         assert(OFFSET(castinfo, sub_baseval) == offcast_sub_baseval);
63
64 #if 0
65 #if defined(ENABLE_REPLACEMENT)
66         assert(sizeof(executionstate_t) = sizeexecutionstate);
67         assert(OFFSET(executionstate_t, pc) == offes_pc);
68         assert(OFFSET(executionstate_t, sp) == offes_sp);
69         assert(OFFSET(executionstate_t, pv) == offes_pv);
70         assert(OFFSET(executionstate_t, intregs) == offes_intregs);
71         assert(OFFSET(executionstate_t, fltregs) == offes_fltregs);
72 #endif
73 #endif
74
75 #ifdef __LINUX__
76         md_init_linux();
77 #endif
78 }
79
80 /* md_codegen_get_pv_from_pc ***************************************************
81
82    On this architecture just a wrapper function to
83    codegen_get_pv_from_pc.
84
85 *******************************************************************************/
86 u1* md_codegen_get_pv_from_pc(u1 *ra) 
87
88         u1 *pv;
89         pv = codegen_get_pv_from_pc(ra);
90
91         return pv;
92 }
93
94 /* md_get_method_patch_address *************************************************
95  
96    Gets the patch address of the currently compiled method. Has to be 
97    extracted from the load instructions which lead to the jump.
98
99 from asmpart.S (asm_vm_call_method):
100 84:   2879 0000 0000  moveal 0 <asm_vm_call_method-0x34>,%a4
101 8a:   4e94            jsr %a4@
102
103
104 from invokestatic / invokespecial
105 0x40290882:   247c 4029 03b4    moveal #1076429748,%a2
106 0x40290888:   4e92              jsr %a2@
107
108 from invokevirtual
109 0x40297eca:   266a 0000         moveal %a2@(0),%a3
110 0x40297ece:   246b 002c         moveal %a3@(44),%a2
111 0x40297ed2:   4e92              jsr %a2@
112
113
114
115 *******************************************************************************/
116
117 u1* md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr) 
118 {
119         u1 * pa;
120         s2   offset;
121
122         if (*((u2*)(ra - 2)) == 0x4e94) {               /* jsr %a4@ */
123                 if (*((u2*)(ra - 6)) == 0x286b) {
124                         /* found an invokevirtual */
125                         /* get offset of load instruction 246b XXXX */
126                         offset = *((s2*)(ra - 4));
127
128                         /* return NULL if no mptr was specified (used for replacement) */
129
130                         if (mptr == NULL)
131                                 return NULL;
132
133                         pa = mptr + offset;                     /* mptr contains the magic we want */
134                 } else  {
135                         /* we had a moveal XXX, %a3 which is a 3 word opcode */
136                         /* 2679 0000 0000 */
137                         assert(*(u2*)(ra - 8) == 0x2879);               /* moveal */
138                         pa = *((u4*)(ra - 6));                          /* another indirection ! */
139                 }
140         } else if (*((u2*)(ra - 2)) == 0x4e92)  {               /* jsr %a2@ */
141                 if (*(u2*)(ra - 8) == 0x247c)   {
142                         /* found a invokestatic/invokespecial */
143                         pa = ((u4*)(ra - 6));                   /* no indirection ! */
144                 } else {
145                         assert(0);
146                 }
147         } else {
148                 assert(0);
149         }
150
151         return pa;
152 }
153
154 /* XXX i can't find a definition of cacheflush in any installed header files but i can find the symbol in libc */
155 /* lets extract the signature from the assembler code*/
156 /*
157     000e7158 <cacheflush>:
158     e7158:       707b            moveq #123,%d0
159     e715a:       2f04            movel %d4,%sp@-
160     e715c:       282f 0014       movel %sp@(20),%d4                     arg 
161     e7160:       2243            moveal %d3,%a1
162     e7162:       262f 0010       movel %sp@(16),%d3                     arg 
163     e7166:       2042            moveal %d2,%a0
164     e7168:       242f 000c       movel %sp@(12),%d2                     arg 
165     e716c:       222f 0008       movel %sp@(8),%d1                      arg 
166     e7170:       4e40            trap #0                                traps into system i guess
167     e7172:       2408            movel %a0,%d2
168     e7174:       2609            movel %a1,%d3
169     e7176:       281f            movel %sp@+,%d4
170     e7178:       223c ffff f001  movel #-4095,%d1
171     e717e:       b081            cmpl %d1,%d0
172     e7180:       6402            bccs e7184 <cacheflush+0x2c>
173     e7182:       4e75            rts
174     e7184:       4480            negl %d0
175     e7186:       2f00            movel %d0,%sp@-
176     e7188:       61ff fff3 82e2  bsrl 1f46c <D_MAX_EXP+0x1ec6d>
177     e718e:       209f            movel %sp@+,%a0@
178     e7190:       70ff            moveq #-1,%d0
179     e7192:       2040            moveal %d0,%a0
180     e7194:       4e75            rts
181     e7196:       4e75            rts
182                                                                         */
183
184 /* seems to have 4 arguments */
185 /* best guess: it is this syscall */
186 /* asmlinkage int sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len) */
187 /* kernel 2.6.10 with freescale patches (the one I develop against) needs a patch of */
188 /* arch/m68k/kernel/sys_m68k.c(sys_cacheflush) */
189 /* evil hack: */
190 /*
191 void DcacheFlushInvalidateCacheBlock(void *start, unsigned long size);
192 void IcacheInvalidateCacheBlock(void *start, unsigned long size);
193
194 asmlinkage int
195 sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)
196 {
197         lock_kernel();
198         DcacheFlushInvalidateCacheBlock(addr, len);
199         IcacheInvalidateCacheBlock(addr, len);
200         unlock_kernel();
201         return 0;
202 }
203 */
204 extern int cacheflush(unsigned long addr, int scope, int cache, unsigned long len);
205
206 #include "asm/cachectl.h"       /* found more traces of the cacheflush function */
207 #include "errno.h"
208
209 void md_cacheflush(u1 *addr, s4 nbytes)  { cacheflush(addr, FLUSH_SCOPE_PAGE, FLUSH_CACHE_BOTH, nbytes); }
210 void md_dcacheflush(u1 *addr, s4 nbytes) { cacheflush(addr, FLUSH_SCOPE_PAGE, FLUSH_CACHE_DATA, nbytes); }
211 void md_icacheflush(u1* addr, s4 nbytes) { cacheflush(addr, FLUSH_SCOPE_LINE, FLUSH_CACHE_INSN, nbytes); }
212
213 /* md_stacktrace_get_returnaddress *********************************************
214
215    Returns the return address of the current stackframe, specified by
216    the passed stack pointer and the stack frame size.
217
218 *******************************************************************************/
219 u1* md_stacktrace_get_returnaddress(u1* sp, u4 framesize) 
220
221         /* return address is above stackpointer */
222         u1 *ra = *((u1**)(sp + framesize));
223         
224         /* XXX: This helps for now, but it's a ugly hack
225          * the problem _may_ be: the link instruction is used
226          * by some gcc generated code, and we get an additional word
227          * on the stack, the old framepointer. Its address is somewhere
228          * near sp, but that all depends the code generated by the compiler.
229          * I'm unsure about a clean solution.
230          */
231         #if 0
232         if (!(ra > 0x40000000 && ra < 0x80000000))      {
233                 ra = *((u1**)(sp + framesize + 4));
234         }
235         #endif
236         /* assert(ra > 0x40000000 && ra < 0x80000000);
237         printf("XXXXXX=%x\n", ra);
238          */
239         return ra;
240 }
241
242
243 void md_codegen_patch_branch(void) { assert(0); }
244
245
246 /*
247  * These are local overrides for various environment variables in Emacs.
248  * Please do not remove this and leave it at the end of the file, where
249  * Emacs will automagically detect them.
250  * ---------------------------------------------------------------------
251  * Local variables:
252  * mode: c
253  * indent-tabs-mode: t
254  * c-basic-offset: 4
255  * tab-width: 4
256  * End:
257  * vim:noexpandtab:sw=4:ts=4:
258  */