1fecb0a8355eb936529188f0b3f1149a45780dfa
[cacao.git] / src / vm / jit / arm / md.c
1 /* src/vm/jit/arm/md.c - machine dependent ARM functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2009 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31
32 #include "vm/jit/arm/codegen.h"
33 #include "vm/jit/arm/md.h"
34 #include "vm/jit/arm/md-abi.h"
35
36 #include "vm/jit/executionstate.h"
37 #include "vm/jit/trap.hpp"
38
39
40 /* md_init *********************************************************************
41
42    Do some machine dependent initialization.
43
44 *******************************************************************************/
45
46 void md_init(void)
47 {
48         /* do nothing here */
49 }
50
51
52 /* md_jit_method_patch_address *************************************************
53
54    Gets the patch address of the currently compiled method. The offset
55    is extracted from the load instruction(s) before the jump and added
56    to the right base address (PV or REG_METHODPTR).
57
58    Machine code:
59
60    e51cc040    ldr   ip, [ip, #-64]
61    e1a0e00f    mov   lr, pc
62    e1a0f00c    mov   pc, ip
63
64    or
65
66    e590b000    ldr   fp, [r0]
67    e59bc004    ldr   ip, [fp, #4]
68    e1a0e00f    mov   lr, pc
69    e1a0f00c    mov   pc, ip
70
71    or
72
73    e590b000    ldr      fp, [r0]
74    e28bca01    add      ip, fp, #4096   ; 0x1000
75    e59cc004    ldr      ip, [ip, #4]
76    e1a0e00f    mov      lr, pc
77    e1a0f00c    mov      pc, ip
78
79    How we find out the patching address to store new method pointer:
80     - loaded IP with LDR IP,[METHODPTR]?
81         yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
82     - loaded IP from data segment
83         yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
84         recompute pointer to data segment, maybe larger offset 
85
86 *******************************************************************************/
87
88 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
89 {
90         uint32_t *pc;
91         uint32_t  mcode;
92         int32_t   disp;
93         void     *pa;                       /* patch address                      */
94
95         /* Go back to the actual load instruction. */
96
97         pc = ((uint32_t *) ra) - 3;
98
99         /* Get first instruction word on current PC. */
100
101         mcode = pc[0];
102
103         /* Sanity check: Are we inside jit code? */
104
105         assert(pc[1] == 0xe1a0e00f /*MOV LR,PC*/);
106         assert(pc[2] == 0xe1a0f00c /*MOV PC,IP*/);
107
108         /* Sanity check: We unconditionally loaded a word into REG_PV? */
109
110         assert ((mcode & 0xff70f000) == 0xe510c000);
111
112         /* Get load displacement. */
113
114         disp = (int32_t) (mcode & 0x0fff);
115
116         /* Case: We loaded from base REG_PV with negative displacement. */
117
118         if (M_MEM_GET_Rbase(mcode) == REG_PV && (mcode & 0x00800000) == 0) {
119                 /* We loaded from data segment, displacement can be larger. */
120
121                 mcode = pc[-1];
122
123                 /* check for "SUB IP, IP, #??, ROTL 12" */
124
125                 if ((mcode & 0xffffff00) == 0xe24cca00)
126                         disp += (int32_t) ((mcode & 0x00ff) << 12);
127
128                 /* and get the final data segment address */
129
130                 pa = ((uint8_t *) pv) - disp;
131         }
132
133         /* Case: We loaded from base REG_METHODPTR with positive displacement. */
134
135         else if (M_MEM_GET_Rbase(mcode) == REG_METHODPTR && (mcode & 0x00800000) == 0x00800000) {
136                 /* return NULL if no mptr was specified (used for replacement) */
137
138                 if (mptr == NULL)
139                         return NULL;
140
141                 /* we loaded from REG_METHODPTR */
142
143                 pa = ((uint8_t *) mptr) + disp;
144         }
145
146         /* Case: We loaded from base REG_PV with positive offset. */
147
148         else if (M_MEM_GET_Rbase(mcode) == REG_PV && (mcode & 0x00800000) == 0x00800000) {
149                 /* We loaded with a larger displacement. Normally this means we loaded
150                    from REG_METHODPTR. However there is a corner case if we loaded
151                    from the data segment at an address aligned to 12 bit, which leads to a
152                    zero (positive) displacement for the last instruction. */
153
154                 mcode = pc[-1];
155
156                 /* check for "ADD IP, FP, #??, ROTL 12" */
157
158                 if ((mcode & 0xffffff00) == 0xe28bca00) {
159                         /* We loaded from REG_METHODPTR with a larger displacement. */
160
161                         assert(mptr != NULL);
162                         disp += (int32_t) ((mcode & 0x00ff) << 12);
163                         pa = ((uint8_t *) mptr) + disp;
164                 }
165
166                 /* check for "SUB IP, IP, #??, ROTL 12" (corner case) */
167
168                 else if ((mcode & 0xffffff00) == 0xe24cca00 && disp == 0) {
169                         /* We loaded from data segment with a larger displacement aligned to 12 bit. */
170
171                         disp += (int32_t) ((mcode & 0x00ff) << 12);
172                         pa = ((uint8_t *) pv) - disp;
173                 }
174
175                 /* Case is not covered, something is severely wrong. */
176
177                 else {
178                         vm_abort_disassemble(pc - 1, 4, "md_jit_method_patch_address: unknown instruction %x", mcode);
179
180                         /* Keep compiler happy. */
181
182                         pa = NULL;
183                 }
184         }
185
186         /* Case is not covered, something is severely wrong. */
187
188         else {
189                 vm_abort_disassemble(pc, 3, "md_jit_method_patch_address: unknown instruction %x", mcode);
190
191                 /* Keep compiler happy. */
192
193                 pa = NULL;
194         }
195
196         return pa;
197 }
198
199
200 /**
201  * Decode the trap instruction at the given PC.
202  *
203  * @param trp information about trap to be filled
204  * @param sig signal number
205  * @param xpc exception PC
206  * @param es execution state of the machine
207  * @return true if trap was decoded successfully, false otherwise.
208  */
209 bool md_trap_decode(trapinfo_t* trp, int sig, void* xpc, executionstate_t* es)
210 {
211         // Get the exception-throwing instruction.
212         uint32_t mcode = *((uint32_t*) xpc);
213
214         switch (sig) {
215         case TRAP_SIGILL:
216                 // Check for valid trap instruction.
217                 if (patcher_is_valid_trap_instruction_at(xpc)) {
218                         trp->type  = (mcode >> 8) & 0x0fff;
219                         trp->value = es->intregs[mcode & 0x0f];
220                         return true;
221                 }
222                 return false;
223
224         case TRAP_SIGSEGV:
225         {
226                 // Sanity check for load/store instruction.
227 #warning Implement this!
228
229                 // Retrieve base address of load/store instruction.
230                 uintptr_t addr = es->intregs[(mcode >> 16) & 0x0f];
231
232                 // Check for implicit NullPointerException.
233                 if (addr == 0) {
234                         trp->type  = TRAP_NullPointerException;
235                         trp->value = 0;
236                         return true;
237                 }
238         }
239
240         default:
241                 return false;
242         }
243 }
244
245
246 /**
247  * Patch the given replacement point.
248  */
249 #if defined(ENABLE_REPLACEMENT)
250 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
251 {
252         vm_abort("md_patch_replacement_point: IMPLEMENT ME!");
253 }
254 #endif
255
256
257 /*
258  * These are local overrides for various environment variables in Emacs.
259  * Please do not remove this and leave it at the end of the file, where
260  * Emacs will automagically detect them.
261  * ---------------------------------------------------------------------
262  * Local variables:
263  * mode: c
264  * indent-tabs-mode: t
265  * c-basic-offset: 4
266  * tab-width: 4
267  * End:
268  * vim:noexpandtab:sw=4:ts=4:
269  */