ae4b7aa2131eb14c9949dca08ab808ba38f185ab
[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.hpp"
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  * Check if the trap instruction at the given PC is valid.
64  *
65  * @param pc Program counter.
66  *
67  * @return true if valid, false otherwise.
68  */
69 bool patcher_is_valid_trap_instruction_at(void* pc)
70 {
71         uint16_t mcode = *((uint16_t*) pc);
72
73         // Check for the undefined instruction we use.
74         return (mcode == 0x0b0f);
75 }
76
77
78 /* patcher_resolve_classref_to_classinfo ***************************************
79
80    ACONST:
81
82    <patched call position>
83    48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
84
85    MULTIANEWARRAY:
86
87    <patched call position>
88    48 be 30 40 b2 00 00 00 00 00    mov    $0xb24030,%rsi
89    48 89 e2                         mov    %rsp,%rdx
90    48 b8 7c 96 4b 00 00 00 00 00    mov    $0x4b967c,%rax
91    48 ff d0                         callq  *%rax
92
93    ARRAYCHECKCAST:
94
95    <patched call position>
96    48 be b8 3f b2 00 00 00 00 00    mov    $0xb23fb8,%rsi
97    48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
98    48 ff d0                         callq  *%rax
99
100 *******************************************************************************/
101
102 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
103 {
104         constant_classref* cr    = (constant_classref*) pr->ref;
105         uintptr_t*         datap = (uintptr_t*)         pr->datap;
106
107         // Resolve the class.
108         classinfo* c = resolve_classref_eager(cr);
109
110         if (c == NULL)
111                 return false;
112
113         // Patch the class.
114         *datap = (uintptr_t) c;
115
116         // Synchronize data cache.
117         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
118
119         // Patch back the original code.
120         patcher_patch_code(pr);
121
122         return true;
123 }
124
125
126 /* patcher_resolve_classref_to_vftbl *******************************************
127
128    CHECKCAST (class):
129    INSTANCEOF (class):
130
131    <patched call position>
132
133 *******************************************************************************/
134
135 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
136 {
137         constant_classref* cr    = (constant_classref*) pr->ref;
138         uintptr_t*         datap = (uintptr_t*)         pr->datap;
139
140         // Resolve the field.
141         classinfo* c = resolve_classref_eager(cr);
142
143         if (c == NULL)
144                 return false;
145
146         // Patch super class' vftbl.
147         *datap = (uintptr_t) c->vftbl;
148
149         // Synchronize data cache.
150         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
151
152         // Patch back the original code.
153         patcher_patch_code(pr);
154
155         return true;
156 }
157
158
159 /* patcher_resolve_classref_to_flags *******************************************
160
161    CHECKCAST/INSTANCEOF:
162
163    <patched call position>
164
165 *******************************************************************************/
166
167 bool patcher_resolve_classref_to_flags(patchref_t *pr)
168 {
169         constant_classref* cr    = (constant_classref*) pr->ref;
170 /*      int32_t*           datap = (int32_t*)           pr->datap; */
171         uint8_t*           ra    = (uint8_t*)           pr->mpc;
172
173         // Resolve the field.
174         classinfo* c = resolve_classref_eager(cr);
175
176         if (c == NULL)
177                 return false;
178
179         // Patch class flags.
180 /*      *datap = c->flags; */
181         *((int32_t*) (ra + 2)) = c->flags;
182
183         // Synchronize data cache.
184 /*      md_dcacheflush(datap, sizeof(int32_t)); */
185         md_icacheflush(ra + 2, sizeof(int32_t));
186
187         // Patch back the original code.
188         patcher_patch_code(pr);
189
190         return true;
191 }
192
193
194 /* patcher_get_putstatic *******************************************************
195
196    Machine code:
197
198    <patched call position>
199    4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
200    49 8b 32                         mov    (%r10),%rsi
201
202 *******************************************************************************/
203
204 bool patcher_get_putstatic(patchref_t *pr)
205 {
206         unresolved_field* uf    = (unresolved_field*) pr->ref;
207         uintptr_t*        datap = (uintptr_t*)        pr->datap;
208
209         // Resolve the field.
210         fieldinfo* fi = resolve_field_eager(uf);
211
212         if (fi == NULL)
213                 return false;
214
215         // Check if the field's class is initialized/
216         if (!(fi->clazz->state & CLASS_INITIALIZED))
217                 if (!initialize_class(fi->clazz))
218                         return false;
219
220         // Patch the field value's address.
221         *datap = (uintptr_t) fi->value;
222
223         // Synchronize data cache.
224         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
225
226         // Patch back the original code.
227         patcher_patch_code(pr);
228
229         return true;
230 }
231
232
233 /* patcher_get_putfield ********************************************************
234
235    Machine code:
236
237    <patched call position>
238    45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
239
240 *******************************************************************************/
241
242 bool patcher_get_putfield(patchref_t *pr)
243 {
244         uint8_t*          pc = (uint8_t*)          pr->mpc;
245         unresolved_field* uf = (unresolved_field*) pr->ref;
246
247         // Resolve the field.
248         fieldinfo* fi = resolve_field_eager(uf);
249
250         if (fi == NULL)
251                 return false;
252
253         // Patch the field's offset: we check for the field type, because
254         // the instructions have different lengths.
255         if (IS_INT_LNG_TYPE(fi->type)) {
256                 // Check for special case: %rsp or %r12 as base register.
257                 if (pc[3] == 0x24)
258                         *((int32_t*) (pc + 4)) = fi->offset;
259                 else
260                         *((int32_t*) (pc + 3)) = fi->offset;
261         }
262         else {
263                 // Check for special case: %rsp or %r12 as base register.
264                 if (pc[5] == 0x24)
265                         *((int32_t*) (pc + 6)) = fi->offset;
266                 else
267                         *((int32_t*) (pc + 5)) = fi->offset;
268         }
269
270         // Synchronize instruction cache.
271         md_icacheflush(pc, 6 + sizeof(int32_t));
272
273         // Patch back the original code.
274         patcher_patch_code(pr);
275
276         return true;
277 }
278
279
280 /* patcher_putfieldconst *******************************************************
281
282    Machine code:
283
284    <patched call position>
285    41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
286
287 *******************************************************************************/
288
289 bool patcher_putfieldconst(patchref_t *pr)
290 {
291         uint8_t*          pc = (uint8_t*)          pr->mpc;
292         unresolved_field* uf = (unresolved_field*) pr->ref;
293
294         // Resolve the field.
295         fieldinfo* fi = resolve_field_eager(uf);
296
297         if (fi == NULL)
298                 return false;
299
300         // Patch the field's offset.
301         if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
302                 // Handle special case when the base register is %r12.
303                 if (pc[12] == 0x94)
304                         *((uint32_t*) (pc + 14)) = fi->offset;
305                 else
306                         *((uint32_t*) (pc + 13)) = fi->offset;
307         }
308         else {
309                 // Handle special case when the base register is %r12.
310                 if (pc[2] == 0x84)
311                         *((uint32_t*) (pc + 4)) = fi->offset;
312                 else
313                         *((uint32_t*) (pc + 3)) = fi->offset;
314         }
315
316         // Synchronize instruction cache.
317         md_icacheflush(pc, 14 + sizeof(int32_t));
318
319         // Patch back the original code.
320         patcher_patch_code(pr);
321
322         return true;
323 }
324
325
326 /* patcher_invokestatic_special ************************************************
327
328    Machine code:
329
330    <patched call position>
331    49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
332    49 ff d2                         callq  *%r10
333
334 *******************************************************************************/
335
336 bool patcher_invokestatic_special(patchref_t *pr)
337 {
338         unresolved_method* um    = (unresolved_method*) pr->ref;
339         uintptr_t*         datap = (uintptr_t*)         pr->datap;
340
341         // Resolve the method.
342         methodinfo* m = resolve_method_eager(um);
343
344         if (m == NULL)
345                 return false;
346
347         // Patch stubroutine.
348         *datap = (uintptr_t) m->stubroutine;
349
350         // Synchronize data cache.
351         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
352
353         // Patch back the original code.
354         patcher_patch_code(pr);
355
356         return true;
357 }
358
359
360 /* patcher_invokevirtual *******************************************************
361
362    Machine code:
363
364    <patched call position>
365    4c 8b 17                         mov    (%rdi),%r10
366    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
367    48 ff d0                         callq  *%rax
368
369 *******************************************************************************/
370
371 bool patcher_invokevirtual(patchref_t *pr)
372 {
373         uint8_t*           pc = (uint8_t*)           pr->mpc;
374         unresolved_method* um = (unresolved_method*) pr->ref;
375
376         // Resovlve the method.
377         methodinfo* m = resolve_method_eager(um);
378
379         if (m == NULL)
380                 return false;
381
382         // Patch vftbl index.
383         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
384
385         // Synchronize instruction cache.
386         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
387
388         // Patch back the original code.
389         patcher_patch_code(pr);
390
391         return true;
392 }
393
394
395 /* patcher_invokeinterface *****************************************************
396
397    Machine code:
398
399    <patched call position>
400    4c 8b 17                         mov    (%rdi),%r10
401    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
402    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
403    48 ff d0                         callq  *%rax
404
405 *******************************************************************************/
406
407 bool patcher_invokeinterface(patchref_t *pr)
408 {
409         uint8_t*           pc = (uint8_t*)           pr->mpc;
410         unresolved_method* um = (unresolved_method*) pr->ref;
411
412         // Resolve the method.
413         methodinfo* m = resolve_method_eager(um);
414
415         if (m == NULL)
416                 return false;
417
418         // Patch interfacetable index.
419         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
420
421         // Patch method offset.
422         *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
423
424         // Synchronize instruction cache.
425         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
426
427         // Patch back the original code.
428         patcher_patch_code(pr);
429
430         return true;
431 }
432
433
434 /* patcher_checkcast_interface *************************************************
435
436    Machine code:
437
438    <patched call position>
439    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
440    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
441    0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
442    48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
443    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
444
445 *******************************************************************************/
446
447 bool patcher_checkcast_interface(patchref_t *pr)
448 {
449         uint8_t*           pc = (uint8_t*)           pr->mpc;
450         constant_classref* cr = (constant_classref*) pr->ref;
451
452         // Resolve the class.
453         classinfo* c = resolve_classref_eager(cr);
454
455         if (c == NULL)
456                 return false;
457
458         // Patch super class index.
459         *((int32_t*) (pc + 7 + 3)) = c->index;
460
461         *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
462
463         // Synchronize instruction cache.
464         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
465
466         // Patch back the original code.
467         patcher_patch_code(pr);
468
469         return true;
470 }
471
472
473 /* patcher_instanceof_interface ************************************************
474
475    Machine code:
476
477    <patched call position>
478    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
479    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
480    0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
481    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
482
483 *******************************************************************************/
484
485 bool patcher_instanceof_interface(patchref_t *pr)
486 {
487         uint8_t*           pc = (uint8_t*)           pr->mpc;
488         constant_classref* cr = (constant_classref*) pr->ref;
489
490         // Resolve the class.
491         classinfo* c = resolve_classref_eager(cr);
492
493         if (c == NULL)
494                 return false;
495
496         // Patch super class index.
497         *((int32_t*) (pc + 7 + 3)) = c->index;
498
499         *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
500
501         // Synchronize instruction cache.
502         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
503
504         // Patch back the original code.
505         patcher_patch_code(pr);
506
507         return true;
508 }
509
510
511 /*
512  * These are local overrides for various environment variables in Emacs.
513  * Please do not remove this and leave it at the end of the file, where
514  * Emacs will automagically detect them.
515  * ---------------------------------------------------------------------
516  * Local variables:
517  * mode: c
518  * indent-tabs-mode: t
519  * c-basic-offset: 4
520  * tab-width: 4
521  * End:
522  * vim:noexpandtab:sw=4:ts=4:
523  */