be30afa3759f0711b57cb8b7fbf489d81480707f
[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 + 12);
329
330                 if (byte == 0x94) {
331                         *((uint32_t *) (ra + 14))      = fi->offset;
332                 }
333                 else {
334                         *((uint32_t *) (ra + 13))      = fi->offset;
335                 }
336         }
337         else {
338                 /* handle special case when the base register is %r12 */
339
340                 byte = *(ra + 2);
341
342                 if (byte == 0x84)
343                         *((uint32_t *) (ra + 4)) = fi->offset;
344                 else
345                         *((uint32_t *) (ra + 3)) = fi->offset;
346         }
347
348         return true;
349 }
350
351
352 /* patcher_invokestatic_special ************************************************
353
354    Machine code:
355
356    <patched call position>
357    49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
358    49 ff d2                         callq  *%r10
359
360 *******************************************************************************/
361
362 bool patcher_invokestatic_special(patchref_t *pr)
363 {
364         unresolved_method *um;
365         intptr_t          *datap;
366         methodinfo        *m;
367
368         /* get stuff from the stack */
369
370         um    = (unresolved_method *) pr->ref;
371         datap = (intptr_t *)          pr->datap;
372
373         /* get the fieldinfo */
374
375         if (!(m = resolve_method_eager(um)))
376                 return false;
377
378         PATCH_BACK_ORIGINAL_MCODE;
379
380         /* patch stubroutine */
381
382         *datap = (intptr_t) m->stubroutine;
383
384         return true;
385 }
386
387
388 /* patcher_invokevirtual *******************************************************
389
390    Machine code:
391
392    <patched call position>
393    4c 8b 17                         mov    (%rdi),%r10
394    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
395    48 ff d0                         callq  *%rax
396
397 *******************************************************************************/
398
399 bool patcher_invokevirtual(patchref_t *pr)
400 {
401         uint8_t           *ra;
402         unresolved_method *um;
403         methodinfo        *m;
404
405         ra = (uint8_t *)           pr->mpc;
406         um = (unresolved_method *) pr->ref;
407
408         /* get the methodinfo */
409
410         if (!(m = resolve_method_eager(um)))
411                 return false;
412
413         PATCH_BACK_ORIGINAL_MCODE;
414
415         /* if we show disassembly, we have to skip the nop's */
416
417         if (opt_shownops)
418                 ra = ra + PATCHER_CALL_SIZE;
419
420         /* patch vftbl index */
421
422         *((int32_t *) (ra + 3 + 3)) =
423                 (int32_t) (OFFSET(vftbl_t, table[0]) +
424                                    sizeof(methodptr) * m->vftblindex);
425
426         return true;
427 }
428
429
430 /* patcher_invokeinterface *****************************************************
431
432    Machine code:
433
434    <patched call position>
435    4c 8b 17                         mov    (%rdi),%r10
436    4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
437    49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
438    48 ff d0                         callq  *%rax
439
440 *******************************************************************************/
441
442 bool patcher_invokeinterface(patchref_t *pr)
443 {
444         uint8_t           *ra;
445         unresolved_method *um;
446         methodinfo        *m;
447
448         /* get stuff from the stack */
449
450         ra = (uint8_t *)           pr->mpc;
451         um = (unresolved_method *) pr->ref;
452
453         /* get the fieldinfo */
454
455         if (!(m = resolve_method_eager(um)))
456                 return false;
457
458         PATCH_BACK_ORIGINAL_MCODE;
459
460         /* if we show disassembly, we have to skip the nop's */
461
462         if (opt_shownops)
463                 ra = ra + PATCHER_CALL_SIZE;
464
465         /* patch interfacetable index */
466
467         *((int32_t *) (ra + 3 + 3)) =
468                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
469                                    sizeof(methodptr) * m->clazz->index);
470
471         /* patch method offset */
472
473         *((int32_t *) (ra + 3 + 7 + 3)) =
474                 (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
475
476         return true;
477 }
478
479
480 /* patcher_checkcast_interface *************************************************
481
482    Machine code:
483
484    <patched call position>
485    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
486    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
487    0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
488    48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
489    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
490
491 *******************************************************************************/
492
493 bool patcher_checkcast_interface(patchref_t *pr)
494 {
495         uint8_t           *ra;
496         constant_classref *cr;
497         classinfo         *c;
498
499         ra = (uint8_t *)           pr->mpc;
500         cr = (constant_classref *) pr->ref;
501
502         /* get the fieldinfo */
503
504         if (!(c = resolve_classref_eager(cr)))
505                 return false;
506
507         PATCH_BACK_ORIGINAL_MCODE;
508
509         /* if we show disassembly, we have to skip the nop's */
510
511         if (opt_shownops)
512                 ra = ra + PATCHER_CALL_SIZE;
513
514         /* patch super class index */
515
516         *((int32_t *) (ra + 7 + 3)) = c->index;
517
518         *((int32_t *) (ra + 7 + 7 + 6 + 8 + 3)) =
519                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
520                                    c->index * sizeof(methodptr*));
521
522         return true;
523 }
524
525
526 /* patcher_instanceof_interface ************************************************
527
528    Machine code:
529
530    <patched call position>
531    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
532    41 81 fb 00 00 00 00             cmp    $0x0,%r11d
533    0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
534    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
535
536 *******************************************************************************/
537
538 bool patcher_instanceof_interface(patchref_t *pr)
539 {
540         uint8_t           *ra;
541         constant_classref *cr;
542         classinfo         *c;
543
544         ra = (uint8_t *)           pr->mpc;
545         cr = (constant_classref *) pr->ref;
546
547         /* get the fieldinfo */
548
549         if (!(c = resolve_classref_eager(cr)))
550                 return false;
551
552         PATCH_BACK_ORIGINAL_MCODE;
553
554         /* if we show disassembly, we have to skip the nop's */
555
556         if (opt_shownops)
557                 ra = ra + PATCHER_CALL_SIZE;
558
559         /* patch super class index */
560
561         *((int32_t *) (ra + 7 + 3)) = c->index;
562
563         *((int32_t *) (ra + 7 + 7 + 6 + 3)) =
564                 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
565                                    c->index * sizeof(methodptr*));
566
567         return true;
568 }
569
570
571 /*
572  * These are local overrides for various environment variables in Emacs.
573  * Please do not remove this and leave it at the end of the file, where
574  * Emacs will automagically detect them.
575  * ---------------------------------------------------------------------
576  * Local variables:
577  * mode: c
578  * indent-tabs-mode: t
579  * c-basic-offset: 4
580  * tab-width: 4
581  * End:
582  * vim:noexpandtab:sw=4:ts=4:
583  */