Merge from subtype branch.
[cacao.git] / src / vm / jit / x86_64 / patcher.c
1 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "vm/types.h"
31
32 #include "vm/jit/x86_64/codegen.h"
33 #include "vm/jit/x86_64/md.h"
34
35 #include "mm/memory.h"
36
37 #include "native/native.hpp"
38
39 #include "vm/jit/builtin.hpp"
40 #include "vm/class.hpp"
41 #include "vm/field.hpp"
42 #include "vm/initialize.hpp"
43 #include "vm/options.h"
44 #include "vm/references.h"
45 #include "vm/resolve.hpp"
46
47 #include "vm/jit/patcher-common.hpp"
48
49
50 /* patcher_patch_code **********************************************************
51
52    Just patches back the original machine code.
53
54 *******************************************************************************/
55
56 void patcher_patch_code(patchref_t *pr)
57 {
58         *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
59         md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
60 }
61
62
63 /* patcher_resolve_classref_to_classinfo ***************************************
64
65    ACONST:
66
67    <patched call position>
68    48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
69
70    MULTIANEWARRAY:
71
72    <patched call position>
73    48 be 30 40 b2 00 00 00 00 00    mov    $0xb24030,%rsi
74    48 89 e2                         mov    %rsp,%rdx
75    48 b8 7c 96 4b 00 00 00 00 00    mov    $0x4b967c,%rax
76    48 ff d0                         callq  *%rax
77
78    ARRAYCHECKCAST:
79
80    <patched call position>
81    48 be b8 3f b2 00 00 00 00 00    mov    $0xb23fb8,%rsi
82    48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
83    48 ff d0                         callq  *%rax
84
85 *******************************************************************************/
86
87 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
88 {
89         constant_classref* cr    = (constant_classref*) pr->ref;
90         uintptr_t*         datap = (uintptr_t*)         pr->datap;
91
92         // Resolve the class.
93         classinfo* c = resolve_classref_eager(cr);
94
95         if (c == NULL)
96                 return false;
97
98         // Patch the class.
99         *datap = (uintptr_t) c;
100
101         // Synchronize data cache.
102         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
103
104         // Patch back the original code.
105         patcher_patch_code(pr);
106
107         return true;
108 }
109
110
111 /* patcher_resolve_classref_to_vftbl *******************************************
112
113    CHECKCAST (class):
114    INSTANCEOF (class):
115
116    <patched call position>
117
118 *******************************************************************************/
119
120 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
121 {
122         constant_classref* cr    = (constant_classref*) pr->ref;
123         uintptr_t*         datap = (uintptr_t*)         pr->datap;
124
125         // Resolve the field.
126         classinfo* c = resolve_classref_eager(cr);
127
128         if (c == NULL)
129                 return false;
130
131         // Patch super class' vftbl.
132         *datap = (uintptr_t) c->vftbl;
133
134         // Synchronize data cache.
135         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
136
137         // Patch back the original code.
138         patcher_patch_code(pr);
139
140         return true;
141 }
142
143
144 /* patcher_resolve_classref_to_flags *******************************************
145
146    CHECKCAST/INSTANCEOF:
147
148    <patched call position>
149
150 *******************************************************************************/
151
152 bool patcher_resolve_classref_to_flags(patchref_t *pr)
153 {
154         constant_classref* cr    = (constant_classref*) pr->ref;
155 /*      int32_t*           datap = (int32_t*)           pr->datap; */
156         uint8_t*           ra    = (uint8_t*)           pr->mpc;
157
158         // Resolve the field.
159         classinfo* c = resolve_classref_eager(cr);
160
161         if (c == NULL)
162                 return false;
163
164         // Patch class flags.
165 /*      *datap = c->flags; */
166         *((int32_t*) (ra + 2)) = c->flags;
167
168         // Synchronize data cache.
169 /*      md_dcacheflush(datap, sizeof(int32_t)); */
170         md_icacheflush(ra + 2, sizeof(int32_t));
171
172         // Patch back the original code.
173         patcher_patch_code(pr);
174
175         return true;
176 }
177
178
179 /* patcher_get_putstatic *******************************************************
180
181    Machine code:
182
183    <patched call position>
184    4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
185    49 8b 32                         mov    (%r10),%rsi
186
187 *******************************************************************************/
188
189 bool patcher_get_putstatic(patchref_t *pr)
190 {
191         unresolved_field* uf    = (unresolved_field*) pr->ref;
192         uintptr_t*        datap = (uintptr_t*)        pr->datap;
193
194         // Resolve the field.
195         fieldinfo* fi = resolve_field_eager(uf);
196
197         if (fi == NULL)
198                 return false;
199
200         // Check if the field's class is initialized/
201         if (!(fi->clazz->state & CLASS_INITIALIZED))
202                 if (!initialize_class(fi->clazz))
203                         return false;
204
205         // Patch the field value's address.
206         *datap = (uintptr_t) fi->value;
207
208         // Synchronize data cache.
209         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
210
211         // Patch back the original code.
212         patcher_patch_code(pr);
213
214         return true;
215 }
216
217
218 /* patcher_get_putfield ********************************************************
219
220    Machine code:
221
222    <patched call position>
223    45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
224
225 *******************************************************************************/
226
227 bool patcher_get_putfield(patchref_t *pr)
228 {
229         uint8_t*          pc = (uint8_t*)          pr->mpc;
230         unresolved_field* uf = (unresolved_field*) pr->ref;
231
232         // Resolve the field.
233         fieldinfo* fi = resolve_field_eager(uf);
234
235         if (fi == NULL)
236                 return false;
237
238         // Patch the field's offset: we check for the field type, because
239         // the instructions have different lengths.
240         if (IS_INT_LNG_TYPE(fi->type)) {
241                 // Check for special case: %rsp or %r12 as base register.
242                 if (pc[3] == 0x24)
243                         *((int32_t*) (pc + 4)) = fi->offset;
244                 else
245                         *((int32_t*) (pc + 3)) = fi->offset;
246         }
247         else {
248                 // Check for special case: %rsp or %r12 as base register.
249                 if (pc[5] == 0x24)
250                         *((int32_t*) (pc + 6)) = fi->offset;
251                 else
252                         *((int32_t*) (pc + 5)) = fi->offset;
253         }
254
255         // Synchronize instruction cache.
256         md_icacheflush(pc, 6 + sizeof(int32_t));
257
258         // Patch back the original code.
259         patcher_patch_code(pr);
260
261         return true;
262 }
263
264
265 /* patcher_putfieldconst *******************************************************
266
267    Machine code:
268
269    <patched call position>
270    41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
271
272 *******************************************************************************/
273
274 bool patcher_putfieldconst(patchref_t *pr)
275 {
276         uint8_t*          pc = (uint8_t*)          pr->mpc;
277         unresolved_field* uf = (unresolved_field*) pr->ref;
278
279         // Resolve the field.
280         fieldinfo* fi = resolve_field_eager(uf);
281
282         if (fi == NULL)
283                 return false;
284
285         // Patch the field's offset.
286         if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
287                 // Handle special case when the base register is %r12.
288                 if (pc[12] == 0x94)
289                         *((uint32_t*) (pc + 14)) = fi->offset;
290                 else
291                         *((uint32_t*) (pc + 13)) = fi->offset;
292         }
293         else {
294                 // Handle special case when the base register is %r12.
295                 if (pc[2] == 0x84)
296                         *((uint32_t*) (pc + 4)) = fi->offset;
297                 else
298                         *((uint32_t*) (pc + 3)) = fi->offset;
299         }
300
301         // Synchronize instruction cache.
302         md_icacheflush(pc, 14 + sizeof(int32_t));
303
304         // Patch back the original code.
305         patcher_patch_code(pr);
306
307         return true;
308 }
309
310
311 /* patcher_invokestatic_special ************************************************
312
313    Machine code:
314
315    <patched call position>
316    49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
317    49 ff d2                         callq  *%r10
318
319 *******************************************************************************/
320
321 bool patcher_invokestatic_special(patchref_t *pr)
322 {
323         unresolved_method* um    = (unresolved_method*) pr->ref;
324         uintptr_t*         datap = (uintptr_t*)         pr->datap;
325
326         // Resolve the method.
327         methodinfo* m = resolve_method_eager(um);
328
329         if (m == NULL)
330                 return false;
331
332         // Patch stubroutine.
333         *datap = (uintptr_t) m->stubroutine;
334
335         // Synchronize data cache.
336         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
337
338         // Patch back the original code.
339         patcher_patch_code(pr);
340
341         return true;
342 }
343
344
345 /* patcher_invokevirtual *******************************************************
346
347    Machine code:
348
349    <patched call position>
350    4c 8b 17                         mov    (%rdi),%r10
351    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
352    48 ff d0                         callq  *%rax
353
354 *******************************************************************************/
355
356 bool patcher_invokevirtual(patchref_t *pr)
357 {
358         uint8_t*           pc = (uint8_t*)           pr->mpc;
359         unresolved_method* um = (unresolved_method*) pr->ref;
360
361         // Resovlve the method.
362         methodinfo* m = resolve_method_eager(um);
363
364         if (m == NULL)
365                 return false;
366
367         // Patch vftbl index.
368         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
369
370         // Synchronize instruction cache.
371         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
372
373         // Patch back the original code.
374         patcher_patch_code(pr);
375
376         return true;
377 }
378
379
380 /* patcher_invokeinterface *****************************************************
381
382    Machine code:
383
384    <patched call position>
385    4c 8b 17                         mov    (%rdi),%r10
386    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
387    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
388    48 ff d0                         callq  *%rax
389
390 *******************************************************************************/
391
392 bool patcher_invokeinterface(patchref_t *pr)
393 {
394         uint8_t*           pc = (uint8_t*)           pr->mpc;
395         unresolved_method* um = (unresolved_method*) pr->ref;
396
397         // Resolve the method.
398         methodinfo* m = resolve_method_eager(um);
399
400         if (m == NULL)
401                 return false;
402
403         // Patch interfacetable index.
404         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
405
406         // Patch method offset.
407         *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
408
409         // Synchronize instruction cache.
410         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
411
412         // Patch back the original code.
413         patcher_patch_code(pr);
414
415         return true;
416 }
417
418
419 /* patcher_checkcast_interface *************************************************
420
421    Machine code:
422
423    <patched call position>
424    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
425    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
426    0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
427    48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
428    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
429
430 *******************************************************************************/
431
432 bool patcher_checkcast_interface(patchref_t *pr)
433 {
434         uint8_t*           pc = (uint8_t*)           pr->mpc;
435         constant_classref* cr = (constant_classref*) pr->ref;
436
437         // Resolve the class.
438         classinfo* c = resolve_classref_eager(cr);
439
440         if (c == NULL)
441                 return false;
442
443         // Patch super class index.
444         *((int32_t*) (pc + 7 + 3)) = c->index;
445
446         *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
447
448         // Synchronize instruction cache.
449         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
450
451         // Patch back the original code.
452         patcher_patch_code(pr);
453
454         return true;
455 }
456
457
458 /* patcher_instanceof_interface ************************************************
459
460    Machine code:
461
462    <patched call position>
463    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
464    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
465    0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
466    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
467
468 *******************************************************************************/
469
470 bool patcher_instanceof_interface(patchref_t *pr)
471 {
472         uint8_t*           pc = (uint8_t*)           pr->mpc;
473         constant_classref* cr = (constant_classref*) pr->ref;
474
475         // Resolve the class.
476         classinfo* c = resolve_classref_eager(cr);
477
478         if (c == NULL)
479                 return false;
480
481         // Patch super class index.
482         *((int32_t*) (pc + 7 + 3)) = c->index;
483
484         *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
485
486         // Synchronize instruction cache.
487         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
488
489         // Patch back the original code.
490         patcher_patch_code(pr);
491
492         return true;
493 }
494
495
496 /*
497  * These are local overrides for various environment variables in Emacs.
498  * Please do not remove this and leave it at the end of the file, where
499  * Emacs will automagically detect them.
500  * ---------------------------------------------------------------------
501  * Local variables:
502  * mode: c
503  * indent-tabs-mode: t
504  * c-basic-offset: 4
505  * tab-width: 4
506  * End:
507  * vim:noexpandtab:sw=4:ts=4:
508  */