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