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