* src/vm/jit/patcher-common.h: Added __X86_64__ ifdef.
[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 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 <stdint.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/x86_64/codegen.h"
35
36 #include "mm/memory.h"
37
38 #include "native/native.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/initialize.h"
43
44 #include "vm/jit/patcher-common.h"
45 #include "vm/jit/stacktrace.h"
46
47 #include "vmcore/class.h"
48 #include "vmcore/field.h"
49 #include "vmcore/options.h"
50 #include "vmcore/references.h"
51 #include "vm/resolve.h"
52
53
54 #define PATCH_BACK_ORIGINAL_MCODE \
55     do { \
56         *((uint16_t *) pr->mpc) = (uint16_t) pr->mcode; \
57     } while (0)
58
59
60 /* patcher_patch_code **********************************************************
61
62    Just patches back the original machine code.
63
64 *******************************************************************************/
65
66 void patcher_patch_code(patchref_t *pr)
67 {
68         PATCH_BACK_ORIGINAL_MCODE;
69 }
70
71
72 /* patcher_resolve_classref_to_classinfo ***************************************
73
74    ACONST:
75
76    <patched call position>
77    48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
78
79    MULTIANEWARRAY:
80
81    <patched call position>
82    48 be 30 40 b2 00 00 00 00 00    mov    $0xb24030,%rsi
83    48 89 e2                         mov    %rsp,%rdx
84    48 b8 7c 96 4b 00 00 00 00 00    mov    $0x4b967c,%rax
85    48 ff d0                         callq  *%rax
86
87    ARRAYCHECKCAST:
88
89    <patched call position>
90    48 be b8 3f b2 00 00 00 00 00    mov    $0xb23fb8,%rsi
91    48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
92    48 ff d0                         callq  *%rax
93
94 *******************************************************************************/
95
96 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
97 {
98         constant_classref *cr;
99         intptr_t          *datap;
100         classinfo         *c;
101
102         cr    = (constant_classref *) pr->ref;
103         datap = (intptr_t *)          pr->datap;
104
105         /* get the classinfo */
106
107         if (!(c = resolve_classref_eager(cr)))
108                 return false;
109
110         PATCH_BACK_ORIGINAL_MCODE;
111
112         /* patch the classinfo pointer */
113
114         *datap = (intptr_t) c;
115
116         return true;
117 }
118
119
120 /* patcher_resolve_classref_to_vftbl *******************************************
121
122    CHECKCAST (class):
123    INSTANCEOF (class):
124
125    <patched call position>
126
127 *******************************************************************************/
128
129 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
130 {
131         constant_classref *cr;
132         intptr_t          *datap;
133         classinfo         *c;
134
135         /* get stuff from the stack */
136
137         cr    = (constant_classref *) pr->ref;
138         datap = (intptr_t *)          pr->datap;
139
140         /* get the fieldinfo */
141
142         if (!(c = resolve_classref_eager(cr)))
143                 return false;
144
145         PATCH_BACK_ORIGINAL_MCODE;
146
147         /* patch super class' vftbl */
148
149         *datap = (intptr_t) c->vftbl;
150
151         return true;
152 }
153
154
155 /* patcher_resolve_classref_to_flags *******************************************
156
157    CHECKCAST/INSTANCEOF:
158
159    <patched call position>
160
161 *******************************************************************************/
162
163 bool patcher_resolve_classref_to_flags(patchref_t *pr)
164 {
165         constant_classref *cr;
166         int32_t           *datap;
167         classinfo         *c;
168         uint8_t           *ra;
169
170         cr    = (constant_classref *) pr->ref;
171         datap = (int32_t *)           pr->datap;
172         ra    = (uint8_t *)           pr->mpc;
173
174         /* get the fieldinfo */
175
176         if (!(c = resolve_classref_eager(cr)))
177                 return false;
178
179         PATCH_BACK_ORIGINAL_MCODE;
180
181         /* if we show disassembly, we have to skip the nop's */
182
183         if (opt_shownops)
184                 ra = ra + PATCHER_CALL_SIZE;
185
186         /* patch class flags */
187
188 /*      *datap = c->flags; */
189         *((int32_t *) (ra + 2)) = c->flags;
190
191         return true;
192 }
193
194
195 /* patcher_resolve_native_function *********************************************
196
197    XXX
198
199 *******************************************************************************/
200
201 bool patcher_resolve_native_function(patchref_t *pr)
202 {
203         methodinfo  *m;
204         intptr_t    *datap;
205         functionptr  f;
206
207         m     = (methodinfo *) pr->ref;
208         datap = (intptr_t *)   pr->datap;
209
210         /* resolve native function */
211
212         if (!(f = native_resolve_function(m)))
213                 return false;
214
215         PATCH_BACK_ORIGINAL_MCODE;
216
217         /* patch native function pointer */
218
219         *datap = (intptr_t) f;
220
221         return true;
222 }
223
224
225 /* patcher_get_putstatic *******************************************************
226
227    Machine code:
228
229    <patched call position>
230    4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
231    49 8b 32                         mov    (%r10),%rsi
232
233 *******************************************************************************/
234
235 bool patcher_get_putstatic(patchref_t *pr)
236 {
237         unresolved_field *uf;
238         intptr_t         *datap;
239         fieldinfo        *fi;
240
241         uf    = (unresolved_field *) pr->ref;
242         datap = (intptr_t *)         pr->datap;
243
244         /* get the fieldinfo */
245
246         if (!(fi = resolve_field_eager(uf)))
247                 return false;
248
249         /* check if the field's class is initialized */
250
251         if (!(fi->class->state & CLASS_INITIALIZED))
252                 if (!initialize_class(fi->class))
253                         return false;
254
255         PATCH_BACK_ORIGINAL_MCODE;
256
257         /* patch the field value's address */
258
259         *datap = (intptr_t) fi->value;
260
261         return true;
262 }
263
264
265 /* patcher_get_putfield ********************************************************
266
267    Machine code:
268
269    <patched call position>
270    45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
271
272 *******************************************************************************/
273
274 bool patcher_get_putfield(patchref_t *pr)
275 {
276         uint8_t          *ra;
277         unresolved_field *uf;
278         fieldinfo        *fi;
279         uint8_t           byte;
280
281         ra = (uint8_t *)          pr->mpc;
282         uf = (unresolved_field *) pr->ref;
283
284         /* get the fieldinfo */
285
286         if (!(fi = resolve_field_eager(uf)))
287                 return false;
288
289         PATCH_BACK_ORIGINAL_MCODE;
290
291         /* if we show disassembly, we have to skip the nop's */
292
293         if (opt_shownops)
294                 ra = ra + PATCHER_CALL_SIZE;
295
296         /* Patch the field's offset: we check for the field type, because
297            the instructions have different lengths. */
298
299         if (IS_INT_LNG_TYPE(fi->type)) {
300                 /* Check for special case: %rsp or %r12 as base register. */
301
302                 byte = *(ra + 3);
303
304                 if (byte == 0x24)
305                         *((int32_t *) (ra + 4)) = fi->offset;
306                 else
307                         *((int32_t *) (ra + 3)) = fi->offset;
308         }
309         else {
310                 /* Check for special case: %rsp or %r12 as base register. */
311
312                 byte = *(ra + 5);
313
314                 if (byte == 0x24)
315                         *((int32_t *) (ra + 6)) = fi->offset;
316                 else
317                         *((int32_t *) (ra + 5)) = fi->offset;
318         }
319
320         return true;
321 }
322
323
324 /* patcher_putfieldconst *******************************************************
325
326    Machine code:
327
328    <patched call position>
329    41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
330
331 *******************************************************************************/
332
333 bool patcher_putfieldconst(patchref_t *pr)
334 {
335         uint8_t          *ra;
336         unresolved_field *uf;
337         fieldinfo        *fi;
338         uint8_t           byte;
339
340         ra = (uint8_t *)          pr->mpc;
341         uf = (unresolved_field *) pr->ref;
342
343         /* get the fieldinfo */
344
345         if (!(fi = resolve_field_eager(uf)))
346                 return false;
347
348         PATCH_BACK_ORIGINAL_MCODE;
349
350         /* if we show disassembly, we have to skip the nop's */
351
352         if (opt_shownops)
353                 ra = ra + PATCHER_CALL_SIZE;
354
355         /* patch the field's offset */
356
357         if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
358                 /* handle special case when the base register is %r12 */
359
360                 byte = *(ra + 2);
361
362                 if (byte == 0x84) {
363                         *((uint32_t *) (ra + 4))      = fi->offset;
364                         *((uint32_t *) (ra + 12 + 4)) = fi->offset + 4;
365                 }
366                 else {
367                         *((uint32_t *) (ra + 3))      = fi->offset;
368                         *((uint32_t *) (ra + 11 + 3)) = fi->offset + 4;
369                 }
370         }
371         else {
372                 /* handle special case when the base register is %r12 */
373
374                 byte = *(ra + 2);
375
376                 if (byte == 0x84)
377                         *((uint32_t *) (ra + 4)) = fi->offset;
378                 else
379                         *((uint32_t *) (ra + 3)) = fi->offset;
380         }
381
382         return true;
383 }
384
385
386 /* patcher_invokestatic_special ************************************************
387
388    Machine code:
389
390    <patched call position>
391    49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
392    49 ff d2                         callq  *%r10
393
394 *******************************************************************************/
395
396 bool patcher_invokestatic_special(patchref_t *pr)
397 {
398         unresolved_method *um;
399         intptr_t          *datap;
400         methodinfo        *m;
401
402         /* get stuff from the stack */
403
404         um    = (unresolved_method *) pr->ref;
405         datap = (intptr_t *)          pr->datap;
406
407         /* get the fieldinfo */
408
409         if (!(m = resolve_method_eager(um)))
410                 return false;
411
412         PATCH_BACK_ORIGINAL_MCODE;
413
414         /* patch stubroutine */
415
416         *datap = (intptr_t) m->stubroutine;
417
418         return true;
419 }
420
421
422 /* patcher_invokevirtual *******************************************************
423
424    Machine code:
425
426    <patched call position>
427    4c 8b 17                         mov    (%rdi),%r10
428    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
429    48 ff d0                         callq  *%rax
430
431 *******************************************************************************/
432
433 bool patcher_invokevirtual(patchref_t *pr)
434 {
435         uint8_t           *ra;
436         unresolved_method *um;
437         methodinfo        *m;
438
439         ra = (uint8_t *)           pr->mpc;
440         um = (unresolved_method *) pr->ref;
441
442         /* get the methodinfo */
443
444         if (!(m = resolve_method_eager(um)))
445                 return false;
446
447         PATCH_BACK_ORIGINAL_MCODE;
448
449         /* if we show disassembly, we have to skip the nop's */
450
451         if (opt_shownops)
452                 ra = ra + PATCHER_CALL_SIZE;
453
454         /* patch vftbl index */
455
456         *((int32_t *) (ra + 3 + 3)) =
457                 (int32_t) (OFFSET(vftbl_t, table[0]) +
458                                    sizeof(methodptr) * m->vftblindex);
459
460         return true;
461 }
462
463
464 /* patcher_invokeinterface *****************************************************
465
466    Machine code:
467
468    <patched call position>
469    4c 8b 17                         mov    (%rdi),%r10
470    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
471    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
472    48 ff d0                         callq  *%rax
473
474 *******************************************************************************/
475
476 bool patcher_invokeinterface(patchref_t *pr)
477 {
478         uint8_t           *ra;
479         unresolved_method *um;
480         methodinfo        *m;
481
482         /* get stuff from the stack */
483
484         ra = (uint8_t *)           pr->mpc;
485         um = (unresolved_method *) pr->ref;
486
487         /* get the fieldinfo */
488
489         if (!(m = resolve_method_eager(um)))
490                 return false;
491
492         PATCH_BACK_ORIGINAL_MCODE;
493
494         /* if we show disassembly, we have to skip the nop's */
495
496         if (opt_shownops)
497                 ra = ra + PATCHER_CALL_SIZE;
498
499         /* patch interfacetable index */
500
501         *((int32_t *) (ra + 3 + 3)) =
502                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
503                                    sizeof(methodptr) * m->class->index);
504
505         /* patch method offset */
506
507         *((int32_t *) (ra + 3 + 7 + 3)) =
508                 (int32_t) (sizeof(methodptr) * (m - m->class->methods));
509
510         return true;
511 }
512
513
514 /* patcher_checkcast_interface *************************************************
515
516    Machine code:
517
518    <patched call position>
519    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
520    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
521    0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
522    48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
523    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
524
525 *******************************************************************************/
526
527 bool patcher_checkcast_interface(patchref_t *pr)
528 {
529         uint8_t           *ra;
530         constant_classref *cr;
531         classinfo         *c;
532
533         ra = (uint8_t *)           pr->mpc;
534         cr = (constant_classref *) pr->ref;
535
536         /* get the fieldinfo */
537
538         if (!(c = resolve_classref_eager(cr)))
539                 return false;
540
541         PATCH_BACK_ORIGINAL_MCODE;
542
543         /* if we show disassembly, we have to skip the nop's */
544
545         if (opt_shownops)
546                 ra = ra + PATCHER_CALL_SIZE;
547
548         /* patch super class index */
549
550         *((int32_t *) (ra + 7 + 3)) = c->index;
551
552         *((int32_t *) (ra + 7 + 7 + 6 + 8 + 3)) =
553                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
554                                    c->index * sizeof(methodptr*));
555
556         return true;
557 }
558
559
560 /* patcher_instanceof_interface ************************************************
561
562    Machine code:
563
564    <patched call position>
565    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
566    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
567    0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
568    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
569
570 *******************************************************************************/
571
572 bool patcher_instanceof_interface(patchref_t *pr)
573 {
574         uint8_t           *ra;
575         constant_classref *cr;
576         classinfo         *c;
577
578         ra = (uint8_t *)           pr->mpc;
579         cr = (constant_classref *) pr->ref;
580
581         /* get the fieldinfo */
582
583         if (!(c = resolve_classref_eager(cr)))
584                 return false;
585
586         PATCH_BACK_ORIGINAL_MCODE;
587
588         /* if we show disassembly, we have to skip the nop's */
589
590         if (opt_shownops)
591                 ra = ra + PATCHER_CALL_SIZE;
592
593         /* patch super class index */
594
595         *((int32_t *) (ra + 7 + 3)) = c->index;
596
597         *((int32_t *) (ra + 7 + 7 + 6 + 3)) =
598                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
599                                    c->index * sizeof(methodptr*));
600
601         return true;
602 }
603
604
605 /*
606  * These are local overrides for various environment variables in Emacs.
607  * Please do not remove this and leave it at the end of the file, where
608  * Emacs will automagically detect them.
609  * ---------------------------------------------------------------------
610  * Local variables:
611  * mode: c
612  * indent-tabs-mode: t
613  * c-basic-offset: 4
614  * tab-width: 4
615  * End:
616  * vim:noexpandtab:sw=4:ts=4:
617  */