* src/vm/jit/patcher-common.cpp: Conditionally restored NOP-insertion at
[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, 2009
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         ra += PATCHER_CALL_SIZE;
180
181         // Patch class flags.
182 /*      *datap = c->flags; */
183         *((int32_t*) (ra + 2)) = c->flags;
184
185         // Synchronize data cache.
186 /*      md_dcacheflush(datap, sizeof(int32_t)); */
187         md_icacheflush(ra + 2, sizeof(int32_t));
188
189         // Patch back the original code.
190         patcher_patch_code(pr);
191
192         return true;
193 }
194
195
196 /* patcher_get_putstatic *******************************************************
197
198    Machine code:
199
200    <patched call position>
201    4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
202    49 8b 32                         mov    (%r10),%rsi
203
204 *******************************************************************************/
205
206 bool patcher_get_putstatic(patchref_t *pr)
207 {
208         unresolved_field* uf    = (unresolved_field*) pr->ref;
209         uintptr_t*        datap = (uintptr_t*)        pr->datap;
210         uint8_t*          ra    = (uint8_t*)          pr->mpc;
211
212         // Resolve the field.
213         fieldinfo* fi = resolve_field_eager(uf);
214
215         if (fi == NULL)
216                 return false;
217
218         ra += PATCHER_CALL_SIZE;
219
220         // Check if the field's class is initialized/
221         if (!(fi->clazz->state & CLASS_INITIALIZED))
222                 if (!initialize_class(fi->clazz))
223                         return false;
224
225         // Patch the field value's address.
226         *datap = (uintptr_t) fi->value;
227
228         // Synchronize data cache.
229         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
230
231         // Patch back the original code.
232         patcher_patch_code(pr);
233
234         return true;
235 }
236
237
238 /* patcher_get_putfield ********************************************************
239
240    Machine code:
241
242    <patched call position>
243    45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
244
245 *******************************************************************************/
246
247 bool patcher_get_putfield(patchref_t *pr)
248 {
249         uint8_t*          pc = (uint8_t*)          pr->mpc;
250         unresolved_field* uf = (unresolved_field*) pr->ref;
251
252         // Resolve the field.
253         fieldinfo* fi = resolve_field_eager(uf);
254
255         if (fi == NULL)
256                 return false;
257
258         pc += PATCHER_CALL_SIZE;
259
260         // Patch the field's offset: we check for the field type, because
261         // the instructions have different lengths.
262         if (IS_INT_LNG_TYPE(fi->type)) {
263                 // Check for special case: %rsp or %r12 as base register.
264                 if (pc[3] == 0x24)
265                         *((int32_t*) (pc + 4)) = fi->offset;
266                 else
267                         *((int32_t*) (pc + 3)) = fi->offset;
268         }
269         else {
270                 // Check for special case: %rsp or %r12 as base register.
271                 if (pc[5] == 0x24)
272                         *((int32_t*) (pc + 6)) = fi->offset;
273                 else
274                         *((int32_t*) (pc + 5)) = fi->offset;
275         }
276
277         // Synchronize instruction cache.
278         md_icacheflush(pc, 6 + sizeof(int32_t));
279
280         // Patch back the original code.
281         patcher_patch_code(pr);
282
283         return true;
284 }
285
286
287 /* patcher_putfieldconst *******************************************************
288
289    Machine code:
290
291    <patched call position>
292    41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
293
294 *******************************************************************************/
295
296 bool patcher_putfieldconst(patchref_t *pr)
297 {
298         uint8_t*          pc = (uint8_t*)          pr->mpc;
299         unresolved_field* uf = (unresolved_field*) pr->ref;
300
301         // Resolve the field.
302         fieldinfo* fi = resolve_field_eager(uf);
303
304         if (fi == NULL)
305                 return false;
306
307         pc += PATCHER_CALL_SIZE;
308
309         // Patch the field's offset.
310         if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
311                 // Handle special case when the base register is %r12.
312                 if (pc[12] == 0x94)
313                         *((uint32_t*) (pc + 14)) = fi->offset;
314                 else
315                         *((uint32_t*) (pc + 13)) = fi->offset;
316         }
317         else {
318                 // Handle special case when the base register is %r12.
319                 if (pc[2] == 0x84)
320                         *((uint32_t*) (pc + 4)) = fi->offset;
321                 else
322                         *((uint32_t*) (pc + 3)) = fi->offset;
323         }
324
325         // Synchronize instruction cache.
326         md_icacheflush(pc, 14 + sizeof(int32_t));
327
328         // Patch back the original code.
329         patcher_patch_code(pr);
330
331         return true;
332 }
333
334
335 /* patcher_invokestatic_special ************************************************
336
337    Machine code:
338
339    <patched call position>
340    49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
341    49 ff d2                         callq  *%r10
342
343 *******************************************************************************/
344
345 bool patcher_invokestatic_special(patchref_t *pr)
346 {
347         unresolved_method* um    = (unresolved_method*) pr->ref;
348         uintptr_t*         datap = (uintptr_t*)         pr->datap;
349
350         // Resolve the method.
351         methodinfo* m = resolve_method_eager(um);
352
353         if (m == NULL)
354                 return false;
355
356         // Patch stubroutine.
357         *datap = (uintptr_t) m->stubroutine;
358
359         // Synchronize data cache.
360         md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
361
362         // Patch back the original code.
363         patcher_patch_code(pr);
364
365         return true;
366 }
367
368
369 /* patcher_invokevirtual *******************************************************
370
371    Machine code:
372
373    <patched call position>
374    4c 8b 17                         mov    (%rdi),%r10
375    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
376    48 ff d0                         callq  *%rax
377
378 *******************************************************************************/
379
380 bool patcher_invokevirtual(patchref_t *pr)
381 {
382         uint8_t*           pc = (uint8_t*)           pr->mpc;
383         unresolved_method* um = (unresolved_method*) pr->ref;
384
385         // Resovlve the method.
386         methodinfo* m = resolve_method_eager(um);
387
388         if (m == NULL)
389                 return false;
390
391         pc += PATCHER_CALL_SIZE;
392
393         // Patch vftbl index.
394         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
395
396         // Synchronize instruction cache.
397         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
398
399         // Patch back the original code.
400         patcher_patch_code(pr);
401
402         return true;
403 }
404
405
406 /* patcher_invokeinterface *****************************************************
407
408    Machine code:
409
410    <patched call position>
411    4c 8b 17                         mov    (%rdi),%r10
412    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
413    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
414    48 ff d0                         callq  *%rax
415
416 *******************************************************************************/
417
418 bool patcher_invokeinterface(patchref_t *pr)
419 {
420         uint8_t*           pc = (uint8_t*)           pr->mpc;
421         unresolved_method* um = (unresolved_method*) pr->ref;
422
423         // Resolve the method.
424         methodinfo* m = resolve_method_eager(um);
425
426         if (m == NULL)
427                 return false;
428
429         pc += PATCHER_CALL_SIZE;
430
431         // Patch interfacetable index.
432         *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
433
434         // Patch method offset.
435         *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
436
437         // Synchronize instruction cache.
438         md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
439
440         // Patch back the original code.
441         patcher_patch_code(pr);
442
443         return true;
444 }
445
446
447 /* patcher_checkcast_interface *************************************************
448
449    Machine code:
450
451    <patched call position>
452    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
453    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
454    0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
455    48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
456    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
457
458 *******************************************************************************/
459
460 bool patcher_checkcast_interface(patchref_t *pr)
461 {
462         uint8_t*           pc = (uint8_t*)           pr->mpc;
463         constant_classref* cr = (constant_classref*) pr->ref;
464
465         // Resolve the class.
466         classinfo* c = resolve_classref_eager(cr);
467
468         if (c == NULL)
469                 return false;
470
471         pc += PATCHER_CALL_SIZE;
472
473         // Patch super class index.
474         *((int32_t*) (pc + 7 + 3)) = c->index;
475
476         *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
477
478         // Synchronize instruction cache.
479         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
480
481         // Patch back the original code.
482         patcher_patch_code(pr);
483
484         return true;
485 }
486
487
488 /* patcher_instanceof_interface ************************************************
489
490    Machine code:
491
492    <patched call position>
493    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
494    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
495    0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
496    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
497
498 *******************************************************************************/
499
500 bool patcher_instanceof_interface(patchref_t *pr)
501 {
502         uint8_t*           pc = (uint8_t*)           pr->mpc;
503         constant_classref* cr = (constant_classref*) pr->ref;
504
505         // Resolve the class.
506         classinfo* c = resolve_classref_eager(cr);
507
508         if (c == NULL)
509                 return false;
510
511         pc += PATCHER_CALL_SIZE;
512
513         // Patch super class index.
514         *((int32_t*) (pc + 7 + 3)) = c->index;
515
516         *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
517
518         // Synchronize instruction cache.
519         md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
520
521         // Patch back the original code.
522         patcher_patch_code(pr);
523
524         return true;
525 }
526
527
528 /*
529  * These are local overrides for various environment variables in Emacs.
530  * Please do not remove this and leave it at the end of the file, where
531  * Emacs will automagically detect them.
532  * ---------------------------------------------------------------------
533  * Local variables:
534  * mode: c
535  * indent-tabs-mode: t
536  * c-basic-offset: 4
537  * tab-width: 4
538  * End:
539  * vim:noexpandtab:sw=4:ts=4:
540  */