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