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