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