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