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