/
tramp-arm.c
1080 lines (907 loc) · 29.5 KB
/
tramp-arm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tramp-arm.c: JIT trampoline code for ARM
*
* Authors:
* Paolo Molaro (lupus@ximian.com)
*
* (C) 2001-2003 Ximian, Inc.
* Copyright 2003-2011 Novell Inc
* Copyright 2011 Xamarin Inc
*/
#include <config.h>
#include <glib.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/tabledefs.h>
#include <mono/arch/arm/arm-codegen.h>
#include "mini.h"
#include "mini-arm.h"
#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
static guint8* nullified_class_init_trampoline;
#ifdef USE_JUMP_TABLES
static guint16
decode_imm16 (guint32 insn)
{
return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
}
#define INSN_MASK 0xff00000
#define MOVW_MASK ((3 << 24) | (0 << 20))
#define MOVT_MASK ((3 << 24) | (4 << 20))
gpointer*
mono_arch_jumptable_entry_from_code (guint8 *code)
{
guint32 insn1 = ((guint32*)code) [0];
guint32 insn2 = ((guint32*)code) [1];
if (((insn1 & INSN_MASK) == MOVW_MASK) &&
((insn2 & INSN_MASK) == MOVT_MASK) ) {
guint32 imm_lo = decode_imm16 (insn1);
guint32 imm_hi = decode_imm16 (insn2);
return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
} else {
g_assert_not_reached ();
return NULL;
}
}
#undef INSN_MASK
#undef MOVW_MASK
#undef MOVT_MASK
void
mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
{
gpointer *jte;
/*
* code_ptr is 4 instructions after MOVW/MOVT used to address
* jumptable entry.
*/
jte = mono_jumptable_get_entry (code_ptr - 16);
g_assert ( jte != NULL);
*jte = addr;
}
#else
void
mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
{
guint32 *code = (guint32*)code_ptr;
/* This is the 'bl' or the 'mov pc' instruction */
--code;
/*
* Note that methods are called also with the bl opcode.
*/
if ((((*code) >> 25) & 7) == 5) {
/*g_print ("direct patching\n");*/
arm_patch ((guint8*)code, addr);
mono_arch_flush_icache ((guint8*)code, 4);
return;
}
if ((((*code) >> 20) & 0xFF) == 0x12) {
/*g_print ("patching bx\n");*/
arm_patch ((guint8*)code, addr);
mono_arch_flush_icache ((guint8*)(code - 2), 4);
return;
}
g_assert_not_reached ();
}
#endif
void
mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
{
guint8 *jump_entry;
/* Patch the jump table entry used by the plt entry */
if (*(guint32*)code == 0xe59fc000) {
/* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
guint32 offset = ((guint32*)code)[2];
jump_entry = code + offset + 12;
} else if (*(guint16*)(code - 4) == 0xf8df) {
/*
* Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
* mono_arm_get_thumb_plt_entry ().
*/
guint32 offset;
code -= 4;
offset = *(guint32*)(code + 12);
jump_entry = code + offset + 8;
} else {
g_assert_not_reached ();
}
*(guint8**)jump_entry = addr;
}
void
mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
{
mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
}
void
mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
{
if (mono_aot_only && !nullified_class_init_trampoline)
nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
}
#ifndef DISABLE_JIT
#define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
#ifndef USE_JUMP_TABLES
/*
* Return the instruction to jump from code to target, 0 if not
* reachable with a single instruction
*/
static guint32
branch_for_target_reachable (guint8 *branch, guint8 *target)
{
gint diff = target - branch - 8;
g_assert ((diff & 3) == 0);
if (diff >= 0) {
if (diff <= 33554431)
return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
} else {
/* diff between 0 and -33554432 */
if (diff >= -33554432)
return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
}
return 0;
}
#endif
static inline guint8*
emit_bx (guint8* code, int reg)
{
if (mono_arm_thumb_supported ())
ARM_BX (code, reg);
else
ARM_MOV_REG_REG (code, ARMREG_PC, reg);
return code;
}
/* Stack size for trampoline function
*/
#define STACK ALIGN_TO (sizeof (MonoLMF), 8)
/* Method-specific trampoline code fragment size */
#define METHOD_TRAMPOLINE_SIZE 64
/* Jump-specific trampoline code fragment size */
#define JUMP_TRAMPOLINE_SIZE 64
guchar*
mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
{
char *tramp_name;
guint8 *buf, *code = NULL;
#ifdef USE_JUMP_TABLES
gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
#else
guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
gpointer *constants;
#endif
int cfa_offset, lmf_offset, regsave_size, lr_offset;
GSList *unwind_ops = NULL;
MonoJumpInfo *ji = NULL;
int buf_len;
#ifdef USE_JUMP_TABLES
g_assert (!aot);
#endif
/* Now we'll create in 'buf' the ARM trampoline code. This
is the trampoline code common to all methods */
buf_len = 212;
code = buf = mono_global_codeman_reserve (buf_len);
/*
* At this point lr points to the specific arg and sp points to the saved
* regs on the stack (all but PC and SP). The original LR value has been
* saved as sp + LR_OFFSET by the push in the specific trampoline
*/
/* The offset of lmf inside the stack frame */
lmf_offset = STACK - sizeof (MonoLMF);
/* The size of the area already allocated by the push in the specific trampoline */
regsave_size = 14 * sizeof (mgreg_t);
/* The offset where lr was saved inside the regsave area */
lr_offset = 13 * sizeof (mgreg_t);
// FIXME: Finish the unwind info, the current info allows us to unwind
// when the trampoline is not in the epilog
// CFA = SP + (num registers pushed) * 4
cfa_offset = 14 * sizeof (mgreg_t);
mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
// PC saved at sp+LR_OFFSET
mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
/*
* For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
* The trampoline contains a pc-relative offset to the got slot
* preceeding the got slot where the value is stored. The offset can be
* found at [lr + 0].
*/
if (aot == 2) {
ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
} else {
ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
}
} else {
if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
}
else
ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
}
ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
/* ok, now we can continue with the MonoLMF setup, mostly untouched
* from emit_prolog in mini-arm.c
* This is a synthetized call to mono_get_lmf_addr ()
*/
if (aot) {
ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = NULL;
code += 4;
ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
} else {
#ifdef USE_JUMP_TABLES
load_get_lmf_addr = mono_jumptable_add_entry ();
code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
#else
load_get_lmf_addr = code;
code += 4;
#endif
}
ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
code = emit_bx (code, ARMREG_R0);
/* we build the MonoLMF structure on the stack - see mini-arm.h
* The pointer to the struct is put in r1.
* the iregs array is already allocated on the stack by push.
*/
ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
cfa_offset += STACK - regsave_size;
mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
/* V1 == lmf */
ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
/*
* The stack now looks like:
* <saved regs>
* v1 -> <rest of LMF>
* sp -> <alignment>
*/
/* r0 is the result from mono_get_lmf_addr () */
ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
/* new_lmf->previous_lmf = *lmf_addr */
ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
/* *(lmf_addr) = r1 */
ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
/* save method info (it's in v2) */
if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
else {
ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
}
/* save caller SP */
ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
/* save caller FP */
ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
/* save the IP (caller ip) */
if (tramp_type == MONO_TRAMPOLINE_JUMP) {
ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
} else {
ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
}
ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
/*
* Now we're ready to call xxx_trampoline ().
*/
/* Arg 1: the saved registers */
ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
/* Arg 2: code (next address to the instruction that called us) */
if (tramp_type == MONO_TRAMPOLINE_JUMP) {
ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
} else {
ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
}
/* Arg 3: the specific argument, stored in v2
*/
ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
if (aot) {
char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = NULL;
code += 4;
ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
} else {
#ifdef USE_JUMP_TABLES
load_trampoline = mono_jumptable_add_entry ();
code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
#else
load_trampoline = code;
code += 4;
#endif
}
ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
code = emit_bx (code, ARMREG_IP);
/* OK, code address is now on r0. Move it to the place on the stack
* where IP was saved (it is now no more useful to us and it can be
* clobbered). This way we can just restore all the regs in one inst
* and branch to IP.
*/
ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
/* Check for thread interruption */
/* This is not perf critical code so no need to check the interrupt flag */
/*
* Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
*/
if (aot) {
ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = NULL;
code += 4;
ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
} else {
#ifdef USE_JUMP_TABLES
gpointer *jte = mono_jumptable_add_entry ();
code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
jte [0] = mono_thread_force_interruption_checkpoint;
#else
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = mono_thread_force_interruption_checkpoint;
code += 4;
#endif
}
ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
code = emit_bx (code, ARMREG_IP);
/*
* Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
* and the rest of the registers, so the method called will see
* the same state as before we executed.
*/
/* ip = previous_lmf */
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
/* lr = lmf_addr */
ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
/* *(lmf_addr) = previous_lmf */
ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
/* Non-standard function epilogue. Instead of doing a proper
* return, we just jump to the compiled code.
*/
/* Restore the registers and jump to the code:
* Note that IP has been conveniently set to the method addr.
*/
ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
ARM_POP_NWB (code, 0x5fff);
if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
code = emit_bx (code, ARMREG_LR);
else
code = emit_bx (code, ARMREG_IP);
#ifdef USE_JUMP_TABLES
load_get_lmf_addr [0] = mono_get_lmf_addr;
load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
#else
constants = (gpointer*)code;
constants [0] = mono_get_lmf_addr;
constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
if (!aot) {
/* backpatch by emitting the missing instructions skipped above */
ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
}
code += 8;
#endif
/* Flush instruction cache, since we've generated code */
mono_arch_flush_icache (buf, code - buf);
/* Sanity check */
g_assert ((code - buf) <= buf_len);
if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
/* Initialize the nullified class init trampoline used in the AOT case */
nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
if (info) {
tramp_name = mono_get_generic_trampoline_name (tramp_type);
*info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
g_free (tramp_name);
}
return buf;
}
gpointer
mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
{
guint8 *buf, *code;
code = buf = mono_global_codeman_reserve (16);
code = emit_bx (code, ARMREG_LR);
mono_arch_flush_icache (buf, code - buf);
if (info)
*info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
return buf;
}
#define SPEC_TRAMP_SIZE 24
gpointer
mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
{
guint8 *code, *buf, *tramp;
gpointer *constants;
#ifndef USE_JUMP_TABLES
guint32 short_branch;
#endif
guint32 size = SPEC_TRAMP_SIZE;
tramp = mono_get_trampoline_code (tramp_type);
mono_domain_lock (domain);
#ifdef USE_JUMP_TABLES
code = buf = mono_domain_code_reserve_align (domain, size, 4);
#else
code = buf = mono_domain_code_reserve_align (domain, size, 4);
if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
size = 12;
mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
}
#endif
mono_domain_unlock (domain);
#ifdef USE_JUMP_TABLES
/* For jumptables case we always generate the same code for trampolines,
* namely
* push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
* movw lr, lo(jte)
* movt lr, hi(jte)
* ldr r1, [lr + 4]
* bx r1
*/
ARM_PUSH (code, 0x5fff);
constants = mono_jumptable_add_entries (2);
code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
code = emit_bx (code, ARMREG_R1);
constants [0] = arg1;
constants [1] = tramp;
#else
/* we could reduce this to 12 bytes if tramp is within reach:
* ARM_PUSH ()
* ARM_BL ()
* method-literal
* The called code can access method using the lr register
* A 20 byte sequence could be:
* ARM_PUSH ()
* ARM_MOV_REG_REG (lr, pc)
* ARM_LDR_IMM (pc, pc, 0)
* method-literal
* tramp-literal
*/
/* We save all the registers, except PC and SP */
ARM_PUSH (code, 0x5fff);
if (short_branch) {
constants = (gpointer*)code;
constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
constants [1] = arg1;
code += 8;
} else {
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
code = emit_bx (code, ARMREG_R1);
constants = (gpointer*)code;
constants [0] = arg1;
constants [1] = tramp;
code += 8;
}
#endif
/* Flush instruction cache, since we've generated code */
mono_arch_flush_icache (buf, code - buf);
g_assert ((code - buf) <= size);
if (code_len)
*code_len = code - buf;
return buf;
}
/*
* mono_arch_get_unbox_trampoline:
* @m: method pointer
* @addr: pointer to native code for @m
*
* when value type methods are called through the vtable we need to unbox the
* this argument. This method returns a pointer to a trampoline which does
* unboxing before calling the method
*/
gpointer
mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
{
guint8 *code, *start;
MonoDomain *domain = mono_domain_get ();
#ifdef USE_JUMP_TABLES
gpointer *jte;
guint32 size = 20;
#else
guint32 size = 16;
#endif
start = code = mono_domain_code_reserve (domain, size);
#ifdef USE_JUMP_TABLES
jte = mono_jumptable_add_entry ();
code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
code = emit_bx (code, ARMREG_IP);
jte [0] = addr;
#else
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
code = emit_bx (code, ARMREG_IP);
*(guint32*)code = (guint32)addr;
code += 4;
#endif
mono_arch_flush_icache (start, code - start);
g_assert ((code - start) <= size);
/*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
g_print ("unbox code is at %p for method at %p\n", start, addr);*/
return start;
}
gpointer
mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
{
guint8 *code, *start;
#ifdef USE_JUMP_TABLES
int buf_len = 20;
gpointer *jte;
#else
int buf_len = 16;
#endif
MonoDomain *domain = mono_domain_get ();
start = code = mono_domain_code_reserve (domain, buf_len);
#ifdef USE_JUMP_TABLES
jte = mono_jumptable_add_entries (2);
code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
ARM_BX (code, ARMREG_IP);
jte [0] = mrgctx;
jte [1] = addr;
#else
ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
*(guint32*)code = (guint32)mrgctx;
code += 4;
*(guint32*)code = (guint32)addr;
code += 4;
#endif
g_assert ((code - start) <= buf_len);
mono_arch_flush_icache (start, code - start);
return start;
}
gpointer
mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
{
guint8 *tramp;
guint8 *code, *buf;
int tramp_size;
guint32 code_len;
guint8 **rgctx_null_jumps;
int depth, index;
int i, njumps;
gboolean mrgctx;
MonoJumpInfo *ji = NULL;
GSList *unwind_ops = NULL;
#ifdef USE_JUMP_TABLES
gpointer *jte;
#endif
mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
index = MONO_RGCTX_SLOT_INDEX (slot);
if (mrgctx)
index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
for (depth = 0; ; ++depth) {
int size = mono_class_rgctx_get_array_size (depth, mrgctx);
if (index < size - 1)
break;
index -= size - 1;
}
tramp_size = 64 + 16 * depth;
code = buf = mono_global_codeman_reserve (tramp_size);
mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
njumps = 0;
/* The vtable/mrgctx is in R0 */
g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
if (mrgctx) {
/* get mrgctx ptr */
ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
} else {
/* load rgctx ptr from vtable */
g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
/* is the rgctx ptr null? */
ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
/* if yes, jump to actual trampoline */
rgctx_null_jumps [njumps ++] = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
}
for (i = 0; i < depth; ++i) {
/* load ptr to next array */
if (mrgctx && i == 0) {
g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
} else {
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
}
/* is the ptr null? */
ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
/* if yes, jump to actual trampoline */
rgctx_null_jumps [njumps ++] = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
}
/* fetch slot */
code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
/* is the slot null? */
ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
/* if yes, jump to actual trampoline */
rgctx_null_jumps [njumps ++] = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
/* otherwise return, result is in R1 */
ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
code = emit_bx (code, ARMREG_LR);
g_assert (njumps <= depth + 2);
for (i = 0; i < njumps; ++i)
arm_patch (rgctx_null_jumps [i], code);
g_free (rgctx_null_jumps);
/* Slowpath */
/* The vtable/mrgctx is still in R0 */
if (aot) {
ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = NULL;
code += 4;
ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
} else {
tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
/* Jump to the actual trampoline */
#ifdef USE_JUMP_TABLES
jte = mono_jumptable_add_entry ();
jte [0] = tramp;
code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
code = emit_bx (code, ARMREG_R1);
#else
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
code = emit_bx (code, ARMREG_R1);
*(gpointer*)code = tramp;
code += 4;
#endif
}
mono_arch_flush_icache (buf, code - buf);
g_assert (code - buf <= tramp_size);
if (info) {
char *name = mono_get_rgctx_fetch_trampoline_name (slot);
*info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
g_free (name);
}
return buf;
}
gpointer
mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
{
guint8 *code, *buf;
int tramp_size;
MonoJumpInfo *ji = NULL;
GSList *unwind_ops = NULL;
g_assert (aot);
tramp_size = 32;
code = buf = mono_global_codeman_reserve (tramp_size);
mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
// FIXME: Currently, we always go to the slow path.
/* Load trampoline addr */
ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
/* The vtable/mrgctx is in R0 */
g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
code = emit_bx (code, ARMREG_R1);
mono_arch_flush_icache (buf, code - buf);
g_assert (code - buf <= tramp_size);
if (info)
*info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
return buf;
}
#define arm_is_imm8(v) ((v) > -256 && (v) < 256)
gpointer
mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
{
guint8 *tramp;
guint8 *code, *buf;
static int byte_offset = -1;
static guint8 bitmask;
guint8 *jump;
int tramp_size;
guint32 code_len, imm8;
gint rot_amount;
GSList *unwind_ops = NULL;
MonoJumpInfo *ji = NULL;
tramp_size = 64;
code = buf = mono_global_codeman_reserve (tramp_size);
if (byte_offset < 0)
mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
g_assert (arm_is_imm8 (byte_offset));
ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
g_assert (imm8 >= 0);
ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
jump = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
/* Initialized case */
ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
/* Uninitialized case */
arm_patch (jump, code);
if (aot) {
ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
ARM_B (code, 0);
*(gpointer*)code = NULL;
code += 4;
ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
} else {
#ifdef USE_JUMP_TABLES
gpointer *jte = mono_jumptable_add_entry ();
#endif
tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
/* Jump to the actual trampoline */
#ifdef USE_JUMP_TABLES
code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
jte [0] = tramp;
code = emit_bx (code, ARMREG_R1);
#else
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
code = emit_bx (code, ARMREG_R1);
*(gpointer*)code = tramp;
code += 4;
#endif
}
mono_arch_flush_icache (buf, code - buf);
g_assert (code - buf <= tramp_size);
if (info)
*info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
return buf;
}
#else
guchar*
mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
{
g_assert_not_reached ();
return NULL;
}
gpointer
mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
{
g_assert_not_reached ();
return NULL;
}
gpointer
mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
{
g_assert_not_reached ();
return NULL;
}
gpointer
mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
{
g_assert_not_reached ();
return NULL;
}
gpointer
mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
{
g_assert_not_reached ();
return NULL;
}
gpointer
mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
{
g_assert_not_reached ();
return NULL;
}
#endif /* DISABLE_JIT */
guint8*
mono_arch_get_call_target (guint8 *code)
{
guint32 ins = ((guint32*)(gpointer)code) [-1];
#if MONOTOUCH
/* Should be a 'bl' or a 'b' */
if (((ins >> 25) & 0x7) == 0x5) {
#else
/* Should be a 'bl' */
if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
#endif
gint32 disp = ((gint32)ins) & 0xffffff;
guint8 *target = code - 4 + 8 + (disp * 4);
return target;
} else {
return NULL;
}
}
guint32
mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
{
/* The offset is stored as the 4th word of the plt entry */
return ((guint32*)plt_entry) [3];
}
/*
* Return the address of the PLT entry called by the thumb code CODE.
*/
guint8*
mono_arm_get_thumb_plt_entry (guint8 *code)
{
int s, j1, j2, imm10, imm11, i1, i2, imm32;
guint8 *bl, *base;
guint16 t1, t2;
guint8 *target;
/* code should be right after a BL */
code = (guint8*)((mgreg_t)code & ~1);
base = (guint8*)((mgreg_t)code & ~3);
bl = code - 4;
t1 = ((guint16*)bl) [0];
t2 = ((guint16*)bl) [1];
g_assert ((t1 >> 11) == 0x1e);
s = (t1 >> 10) & 0x1;
imm10 = (t1 >> 0) & 0x3ff;
j1 = (t2 >> 13) & 0x1;
j2 = (t2 >> 11) & 0x1;
imm11 = t2 & 0x7ff;
i1 = (s ^ j1) ? 0 : 1;
i2 = (s ^ j2) ? 0 : 1;
imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
// FIXME:
g_assert (s == 0);
target = code + imm32;
/* target now points to the thumb plt entry */
/* ldr.w r12, [pc, #8] */
g_assert (((guint16*)target) [0] == 0xf8df);
g_assert (((guint16*)target) [1] == 0xc008);