forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubs.cpp
3733 lines (3061 loc) · 136 KB
/
stubs.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// File: stubs.cpp
//
// This file contains stub functions for unimplemented features need to
// run on the ARM platform.
#include "common.h"
#include "jitinterface.h"
#include "comdelegate.h"
#include "invokeutil.h"
#include "excep.h"
#include "class.h"
#include "field.h"
#include "dllimportcallback.h"
#include "dllimport.h"
#include "eeconfig.h"
#include "cgensys.h"
#include "asmconstants.h"
#include "virtualcallstub.h"
#include "gcdump.h"
#include "rtlfunctions.h"
#include "codeman.h"
#include "tls.h"
#include "ecall.h"
#include "threadsuspend.h"
// target write barriers
EXTERN_C void JIT_WriteBarrier(Object **dst, Object *ref);
EXTERN_C void JIT_WriteBarrier_End();
EXTERN_C void JIT_CheckedWriteBarrier(Object **dst, Object *ref);
EXTERN_C void JIT_CheckedWriteBarrier_End();
EXTERN_C void JIT_ByRefWriteBarrier_End();
EXTERN_C void JIT_ByRefWriteBarrier_SP(Object **dst, Object *ref);
// source write barriers
EXTERN_C void JIT_WriteBarrier_SP_Pre(Object **dst, Object *ref);
EXTERN_C void JIT_WriteBarrier_SP_Pre_End();
EXTERN_C void JIT_WriteBarrier_SP_Post(Object **dst, Object *ref);
EXTERN_C void JIT_WriteBarrier_SP_Post_End();
EXTERN_C void JIT_WriteBarrier_MP_Pre(Object **dst, Object *ref);
EXTERN_C void JIT_WriteBarrier_MP_Pre_End();
EXTERN_C void JIT_WriteBarrier_MP_Post(Object **dst, Object *ref);
EXTERN_C void JIT_WriteBarrier_MP_Post_End();
EXTERN_C void JIT_CheckedWriteBarrier_SP_Pre(Object **dst, Object *ref);
EXTERN_C void JIT_CheckedWriteBarrier_SP_Pre_End();
EXTERN_C void JIT_CheckedWriteBarrier_SP_Post(Object **dst, Object *ref);
EXTERN_C void JIT_CheckedWriteBarrier_SP_Post_End();
EXTERN_C void JIT_CheckedWriteBarrier_MP_Pre(Object **dst, Object *ref);
EXTERN_C void JIT_CheckedWriteBarrier_MP_Pre_End();
EXTERN_C void JIT_CheckedWriteBarrier_MP_Post(Object **dst, Object *ref);
EXTERN_C void JIT_CheckedWriteBarrier_MP_Post_End();
EXTERN_C void JIT_ByRefWriteBarrier_SP_Pre();
EXTERN_C void JIT_ByRefWriteBarrier_SP_Pre_End();
EXTERN_C void JIT_ByRefWriteBarrier_SP_Post();
EXTERN_C void JIT_ByRefWriteBarrier_SP_Post_End();
EXTERN_C void JIT_ByRefWriteBarrier_MP_Pre();
EXTERN_C void JIT_ByRefWriteBarrier_MP_Pre_End();
EXTERN_C void JIT_ByRefWriteBarrier_MP_Post(Object **dst, Object *ref);
EXTERN_C void JIT_ByRefWriteBarrier_MP_Post_End();
EXTERN_C void JIT_PatchedWriteBarrierStart();
EXTERN_C void JIT_PatchedWriteBarrierLast();
#ifndef DACCESS_COMPILE
//-----------------------------------------------------------------------
// InstructionFormat for conditional jump.
//-----------------------------------------------------------------------
class ThumbCondJump : public InstructionFormat
{
public:
ThumbCondJump() : InstructionFormat(InstructionFormat::k16)
{
LIMITED_METHOD_CONTRACT;
}
virtual UINT GetSizeOfInstruction(UINT refsize, UINT variationCode)
{
LIMITED_METHOD_CONTRACT
_ASSERTE(refsize == InstructionFormat::k16);
return 2;
}
virtual UINT GetHotSpotOffset(UINT refsize, UINT variationCode)
{
LIMITED_METHOD_CONTRACT
_ASSERTE(refsize == InstructionFormat::k16);
return 4;
}
//CB{N}Z Rn, <Label>
//Encoding 1|0|1|1|op|0|i|1|imm5|Rn
//op = Bit3(variation)
//Rn = Bits2-0(variation)
virtual VOID EmitInstruction(UINT refsize, __int64 fixedUpReference, BYTE *pOutBuffer, UINT variationCode, BYTE *pDataBuffer)
{
LIMITED_METHOD_CONTRACT
_ASSERTE(refsize == InstructionFormat::k16);
if(fixedUpReference <0 || fixedUpReference > 126)
COMPlusThrow(kNotSupportedException);
_ASSERTE((fixedUpReference & 0x1) == 0);
pOutBuffer[0] = static_cast<BYTE>(((0x3e & fixedUpReference) << 2) | (0x7 & variationCode));
pOutBuffer[1] = static_cast<BYTE>(0xb1 | (0x8 & variationCode)| ((0x40 & fixedUpReference)>>5));
}
};
//-----------------------------------------------------------------------
// InstructionFormat for near Jump and short Jump
//-----------------------------------------------------------------------
class ThumbNearJump : public InstructionFormat
{
public:
ThumbNearJump() : InstructionFormat(InstructionFormat::k16|InstructionFormat::k32)
{
LIMITED_METHOD_CONTRACT;
}
virtual UINT GetSizeOfInstruction(UINT refsize, UINT variationCode)
{
LIMITED_METHOD_CONTRACT
if(refsize == InstructionFormat::k16)
return 2;
else if(refsize == InstructionFormat::k32)
return 4;
else
_ASSERTE(!"Unknown refsize");
return 0;
}
virtual VOID EmitInstruction(UINT refsize, __int64 fixedUpReference, BYTE *pOutBuffer, UINT cond, BYTE *pDataBuffer)
{
LIMITED_METHOD_CONTRACT
_ASSERTE(cond <15);
//offsets must be in multiples of 2
_ASSERTE((fixedUpReference & 0x1) == 0);
if(cond == 0xe) //Always execute
{
if(fixedUpReference >= -2048 && fixedUpReference <= 2046)
{
if(refsize != InstructionFormat::k16)
_ASSERTE(!"Expected refSize to be 2");
//Emit T2 encoding of B<c> <label> instruction
pOutBuffer[0] = static_cast<BYTE>((fixedUpReference & 0x1fe)>>1);
pOutBuffer[1] = static_cast<BYTE>(0xe0 | ((fixedUpReference & 0xe00)>>9));
}
else if(fixedUpReference >= -16777216 && fixedUpReference <= 16777214)
{
if(refsize != InstructionFormat::k32)
_ASSERTE(!"Expected refSize to be 4");
//Emit T4 encoding of B<c> <label> instruction
int s = (fixedUpReference & 0x1000000) >> 24;
int i1 = (fixedUpReference & 0x800000) >> 23;
int i2 = (fixedUpReference & 0x400000) >> 22;
pOutBuffer[0] = static_cast<BYTE>((fixedUpReference & 0xff000) >> 12);
pOutBuffer[1] = static_cast<BYTE>(0xf0 | (s << 2) |( (fixedUpReference & 0x300000) >>20));
pOutBuffer[2] = static_cast<BYTE>((fixedUpReference & 0x1fe) >> 1);
pOutBuffer[3] = static_cast<BYTE>(0x90 | (~(i1^s)) << 5 | (~(i2^s)) << 3 | (fixedUpReference & 0xe00) >> 9);
}
else
{
COMPlusThrow(kNotSupportedException);
}
}
else // conditional branch based on flags
{
if(fixedUpReference >= -256 && fixedUpReference <= 254)
{
if(refsize != InstructionFormat::k16)
_ASSERTE(!"Expected refSize to be 2");
//Emit T1 encoding of B<c> <label> instruction
pOutBuffer[0] = static_cast<BYTE>((fixedUpReference & 0x1fe)>>1);
pOutBuffer[1] = static_cast<BYTE>(0xd0 | (cond & 0xf));
}
else if(fixedUpReference >= -1048576 && fixedUpReference <= 1048574)
{
if(refsize != InstructionFormat::k32)
_ASSERTE(!"Expected refSize to be 4");
//Emit T3 encoding of B<c> <label> instruction
pOutBuffer[0] = static_cast<BYTE>(((cond & 0x3) << 6) | ((fixedUpReference & 0x3f000) >>12));
pOutBuffer[1] = static_cast<BYTE>(0xf0 | ((fixedUpReference & 0x100000) >>18) | ((cond & 0xc) >> 2));
pOutBuffer[2] = static_cast<BYTE>((fixedUpReference & 0x1fe) >> 1);
pOutBuffer[3] = static_cast<BYTE>(0x80 | ((fixedUpReference & 0x40000) >> 13) | ((fixedUpReference & 0x80000) >> 16) | ((fixedUpReference & 0xe00) >> 9));
}
else
{
COMPlusThrow(kNotSupportedException);
}
}
}
virtual BOOL CanReach(UINT refsize, UINT variationCode, BOOL fExternal, INT_PTR offset)
{
LIMITED_METHOD_CONTRACT
if (fExternal)
{
_ASSERTE(0);
return FALSE;
}
else
{
switch (refsize)
{
case InstructionFormat::k16:
if(variationCode == 0xe)
return (offset >= -2048 && offset <= 2046 && (offset & 0x1) == 0);
else
return (offset >= -256 && offset <= 254 && (offset & 0x1) == 0);
case InstructionFormat::k32:
if(variationCode == 0xe)
return ((offset >= -16777216) && (offset <= 16777214) && ((offset & 0x1) == 0));
else
return ((offset >= -1048576) && (offset <= 1048574) && ((offset & 0x1) == 0));
default:
_ASSERTE(!"Unknown refsize");
return FALSE;
}
}
}
virtual UINT GetHotSpotOffset(UINT refsize, UINT variationCode)
{
LIMITED_METHOD_CONTRACT
_ASSERTE(refsize == InstructionFormat::k16 || refsize == InstructionFormat::k32);
return 4;
}
};
//static conditional jump instruction format object
static BYTE gThumbCondJump[sizeof(ThumbCondJump)];
//static near jump instruction format object
static BYTE gThumbNearJump[sizeof(ThumbNearJump)];
void StubLinkerCPU::Init(void)
{
//Initialize the object
new (gThumbCondJump) ThumbCondJump();
new (gThumbNearJump) ThumbNearJump();
}
#ifndef CROSSGEN_COMPILE
// GC write barrier support.
//
// To optimize our write barriers we code the values of several GC globals (e.g. g_lowest_address) directly
// into the barrier function itself, thus avoiding a double memory indirection. Every time the GC modifies one
// of these globals we need to update all of the write barriers accordingly.
//
// In order to keep this process non-brittle we don't hard code the offsets of the instructions that need to
// be changed. Instead the code used to create these barriers is implemented using special macros that record
// the necessary offsets in a descriptor table. Search for "GC write barrier support" in vm\arm\asmhelpers.asm
// for more details.
// Structure describing the layout of a single write barrier descriptor. This must be kept in sync with the
// code in vm\arm\asmhelpers.asm in the WRITE_BARRIER_END macro. Each offset recorded is for one of the
// supported GC globals (an offset of 0xffff is encoded if that global is not used by the particular barrier
// function). We currently only support one usage of each global by any single barrier function. The offset is
// the byte offset from the start of the function at which a movw,movt instruction pair is used to load the
// value of the global into a register.
struct WriteBarrierDescriptor
{
BYTE * m_pFuncStart; // Pointer to the start of the barrier function
BYTE * m_pFuncEnd; // Pointer to the end of the barrier function
DWORD m_dw_g_lowest_address_offset; // Offset of the instruction reading g_lowest_address
DWORD m_dw_g_highest_address_offset; // Offset of the instruction reading g_highest_address
DWORD m_dw_g_ephemeral_low_offset; // Offset of the instruction reading g_ephemeral_low
DWORD m_dw_g_ephemeral_high_offset; // Offset of the instruction reading g_ephemeral_high
DWORD m_dw_g_card_table_offset; // Offset of the instruction reading g_card_table
};
// Infrastructure used for mapping of the source and destination of current WB patching
struct WriteBarrierMapping
{
PBYTE to; // Pointer to the write-barrier where it was copied over
PBYTE from; // Pointer to write-barrier from which it was copied
};
const int WriteBarrierIndex = 0;
const int CheckedWriteBarrierIndex = 1;
const int ByRefWriteBarrierIndex = 2;
const int MaxWriteBarrierIndex = 3;
WriteBarrierMapping wbMapping[MaxWriteBarrierIndex] =
{
{(PBYTE)JIT_WriteBarrier, NULL},
{(PBYTE)JIT_CheckedWriteBarrier, NULL},
{(PBYTE)JIT_ByRefWriteBarrier, NULL}
};
PBYTE FindWBMapping(PBYTE from)
{
for(int i = 0; i < MaxWriteBarrierIndex; ++i)
{
if(wbMapping[i].from == from)
return wbMapping[i].to;
}
return NULL;
}
// Pointer to the start of the descriptor table. The end of the table is marked by a sentinel entry
// (m_pFuncStart is NULL).
EXTERN_C WriteBarrierDescriptor g_rgWriteBarrierDescriptors;
// Determine the range of memory containing all the write barrier implementations (these are clustered
// together and should fit in a page or maybe two).
void ComputeWriteBarrierRange(BYTE ** ppbStart, DWORD * pcbLength)
{
DWORD size = (PBYTE)JIT_PatchedWriteBarrierLast - (PBYTE)JIT_PatchedWriteBarrierStart;
*ppbStart = (PBYTE)JIT_PatchedWriteBarrierStart;
*pcbLength = size;
}
void CopyWriteBarrier(PCODE dstCode, PCODE srcCode, PCODE endCode)
{
TADDR dst = PCODEToPINSTR(dstCode);
TADDR src = PCODEToPINSTR(srcCode);
TADDR end = PCODEToPINSTR(endCode);
size_t size = (PBYTE)end - (PBYTE)src;
memcpy((PVOID)dst, (PVOID)src, size);
}
#if _DEBUG
void ValidateWriteBarriers()
{
// Post-grow WB are bigger than pre-grow so validating that target WB has space to accomodate those
_ASSERTE( ((PBYTE)JIT_WriteBarrier_End - (PBYTE)JIT_WriteBarrier) >= ((PBYTE)JIT_WriteBarrier_MP_Post_End - (PBYTE)JIT_WriteBarrier_MP_Post));
_ASSERTE( ((PBYTE)JIT_WriteBarrier_End - (PBYTE)JIT_WriteBarrier) >= ((PBYTE)JIT_WriteBarrier_SP_Post_End - (PBYTE)JIT_WriteBarrier_SP_Post));
_ASSERTE( ((PBYTE)JIT_CheckedWriteBarrier_End - (PBYTE)JIT_CheckedWriteBarrier) >= ((PBYTE)JIT_CheckedWriteBarrier_MP_Post_End - (PBYTE)JIT_CheckedWriteBarrier_MP_Post));
_ASSERTE( ((PBYTE)JIT_CheckedWriteBarrier_End - (PBYTE)JIT_CheckedWriteBarrier) >= ((PBYTE)JIT_CheckedWriteBarrier_SP_Post_End - (PBYTE)JIT_CheckedWriteBarrier_SP_Post));
_ASSERTE( ((PBYTE)JIT_ByRefWriteBarrier_End - (PBYTE)JIT_ByRefWriteBarrier) >= ((PBYTE)JIT_ByRefWriteBarrier_MP_Post_End - (PBYTE)JIT_ByRefWriteBarrier_MP_Post));
_ASSERTE( ((PBYTE)JIT_ByRefWriteBarrier_End - (PBYTE)JIT_ByRefWriteBarrier) >= ((PBYTE)JIT_ByRefWriteBarrier_SP_Post_End - (PBYTE)JIT_ByRefWriteBarrier_SP_Post));
}
#endif // _DEBUG
#define UPDATE_WB(_proc,_grow) \
CopyWriteBarrier((PCODE)JIT_WriteBarrier, (PCODE)JIT_WriteBarrier_ ## _proc ## _ ## _grow , (PCODE)JIT_WriteBarrier_ ## _proc ## _ ## _grow ## _End); \
wbMapping[WriteBarrierIndex].from = (PBYTE)JIT_WriteBarrier_ ## _proc ## _ ## _grow ; \
\
CopyWriteBarrier((PCODE)JIT_CheckedWriteBarrier, (PCODE)JIT_CheckedWriteBarrier_ ## _proc ## _ ## _grow , (PCODE)JIT_CheckedWriteBarrier_ ## _proc ## _ ## _grow ## _End); \
wbMapping[CheckedWriteBarrierIndex].from = (PBYTE)JIT_CheckedWriteBarrier_ ## _proc ## _ ## _grow ; \
\
CopyWriteBarrier((PCODE)JIT_ByRefWriteBarrier, (PCODE)JIT_ByRefWriteBarrier_ ## _proc ## _ ## _grow , (PCODE)JIT_ByRefWriteBarrier_ ## _proc ## _ ## _grow ## _End); \
wbMapping[ByRefWriteBarrierIndex].from = (PBYTE)JIT_ByRefWriteBarrier_ ## _proc ## _ ## _grow ; \
// Update the instructions in our various write barrier implementations that refer directly to the values
// of GC globals such as g_lowest_address and g_card_table. We don't particularly care which values have
// changed on each of these callbacks, it's pretty cheap to refresh them all.
void UpdateGCWriteBarriers(bool postGrow = false)
{
// Define a helper macro that abstracts the minutia of patching the instructions to access the value of a
// particular GC global.
#if _DEBUG
ValidateWriteBarriers();
#endif // _DEBUG
static bool wbCopyRequired = true; // We begin with a wb copy
static bool wbIsPostGrow = false; // We begin with pre-Grow write barrier
if(postGrow && !wbIsPostGrow)
{
wbIsPostGrow = true;
wbCopyRequired = true;
}
if(wbCopyRequired)
{
BOOL mp = g_SystemInfo.dwNumberOfProcessors > 1;
if(mp)
{
if(wbIsPostGrow)
{
UPDATE_WB(MP,Post);
}
else
{
UPDATE_WB(MP,Pre);
}
}
else
{
if(wbIsPostGrow)
{
UPDATE_WB(SP,Post);
}
else
{
UPDATE_WB(SP,Pre);
}
}
wbCopyRequired = false;
}
#define GWB_PATCH_OFFSET(_global) \
if (pDesc->m_dw_##_global##_offset != 0xffff) \
PutThumb2Mov32((UINT16*)(to + pDesc->m_dw_##_global##_offset - 1), (UINT32)(dac_cast<TADDR>(_global)));
// Iterate through the write barrier patch table created in the .clrwb section
// (see write barrier asm code)
WriteBarrierDescriptor * pDesc = &g_rgWriteBarrierDescriptors;
while (pDesc->m_pFuncStart)
{
// If the write barrier is being currently used (as in copied over to the patchable site)
// then read the patch location from the table and use the offset to patch the target asm code
PBYTE to = FindWBMapping(pDesc->m_pFuncStart);
if(to)
{
GWB_PATCH_OFFSET(g_lowest_address);
GWB_PATCH_OFFSET(g_highest_address);
GWB_PATCH_OFFSET(g_ephemeral_low);
GWB_PATCH_OFFSET(g_ephemeral_high);
GWB_PATCH_OFFSET(g_card_table);
}
pDesc++;
}
}
int StompWriteBarrierResize(bool isRuntimeSuspended, bool bReqUpperBoundsCheck)
{
// The runtime is not always suspended when this is called (unlike StompWriteBarrierEphemeral) but we have
// no way to update the barrier code atomically on ARM since each 32-bit value we change is loaded over
// two instructions. So we have to suspend the EE (which forces code out of the barrier functions) before
// proceeding. Luckily the case where the runtime is not already suspended is relatively rare (allocation
// of a new large object heap segment). Skip the suspend for the case where we're called during runtime
// startup.
// suspend/resuming the EE under GC stress will trigger a GC and if we're holding the
// GC lock due to allocating a LOH segment it will cause a deadlock so disable it here.
GCStressPolicy::InhibitHolder iholder;
int stompWBCompleteActions = SWB_ICACHE_FLUSH;
if (!isRuntimeSuspended)
{
ThreadSuspend::SuspendEE(ThreadSuspend::SUSPEND_OTHER);
stompWBCompleteActions |= SWB_EE_RESTART;
}
UpdateGCWriteBarriers(bReqUpperBoundsCheck);
return stompWBCompleteActions;
}
int StompWriteBarrierEphemeral(bool isRuntimeSuspended)
{
UNREFERENCED_PARAMETER(isRuntimeSuspended);
_ASSERTE(isRuntimeSuspended);
UpdateGCWriteBarriers();
return SWB_ICACHE_FLUSH;
}
void FlushWriteBarrierInstructionCache()
{
// We've changed code so we must flush the instruction cache.
BYTE *pbAlteredRange;
DWORD cbAlteredRange;
ComputeWriteBarrierRange(&pbAlteredRange, &cbAlteredRange);
FlushInstructionCache(GetCurrentProcess(), pbAlteredRange, cbAlteredRange);
}
#endif // CROSSGEN_COMPILE
#endif // !DACCESS_COMPILE
#ifndef CROSSGEN_COMPILE
void LazyMachState::unwindLazyState(LazyMachState* baseState,
MachState* unwoundstate,
DWORD threadId,
int funCallDepth,
HostCallPreference hostCallPreference)
{
T_CONTEXT ctx;
T_KNONVOLATILE_CONTEXT_POINTERS nonVolRegPtrs;
ctx.Pc = baseState->captureIp;
ctx.Sp = baseState->captureSp;
ctx.R4 = unwoundstate->captureR4_R11[0] = baseState->captureR4_R11[0];
ctx.R5 = unwoundstate->captureR4_R11[1] = baseState->captureR4_R11[1];
ctx.R6 = unwoundstate->captureR4_R11[2] = baseState->captureR4_R11[2];
ctx.R7 = unwoundstate->captureR4_R11[3] = baseState->captureR4_R11[3];
ctx.R8 = unwoundstate->captureR4_R11[4] = baseState->captureR4_R11[4];
ctx.R9 = unwoundstate->captureR4_R11[5] = baseState->captureR4_R11[5];
ctx.R10 = unwoundstate->captureR4_R11[6] = baseState->captureR4_R11[6];
ctx.R11 = unwoundstate->captureR4_R11[7] = baseState->captureR4_R11[7];
#if !defined(DACCESS_COMPILE)
// For DAC, if we get here, it means that the LazyMachState is uninitialized and we have to unwind it.
// The API we use to unwind in DAC is StackWalk64(), which does not support the context pointers.
//
// Restore the integer registers to KNONVOLATILE_CONTEXT_POINTERS to be used for unwinding.
nonVolRegPtrs.R4 = &unwoundstate->captureR4_R11[0];
nonVolRegPtrs.R5 = &unwoundstate->captureR4_R11[1];
nonVolRegPtrs.R6 = &unwoundstate->captureR4_R11[2];
nonVolRegPtrs.R7 = &unwoundstate->captureR4_R11[3];
nonVolRegPtrs.R8 = &unwoundstate->captureR4_R11[4];
nonVolRegPtrs.R9 = &unwoundstate->captureR4_R11[5];
nonVolRegPtrs.R10 = &unwoundstate->captureR4_R11[6];
nonVolRegPtrs.R11 = &unwoundstate->captureR4_R11[7];
#endif // DACCESS_COMPILE
LOG((LF_GCROOTS, LL_INFO100000, "STACKWALK LazyMachState::unwindLazyState(ip:%p,sp:%p)\n", baseState->captureIp, baseState->captureSp));
PCODE pvControlPc;
do
{
#ifndef FEATURE_PAL
pvControlPc = Thread::VirtualUnwindCallFrame(&ctx, &nonVolRegPtrs);
#else // !FEATURE_PAL
#ifdef DACCESS_COMPILE
HRESULT hr = DacVirtualUnwind(threadId, &ctx, &nonVolRegPtrs);
if (FAILED(hr))
{
DacError(hr);
}
#else // DACCESS_COMPILE
BOOL success = PAL_VirtualUnwind(&ctx, &nonVolRegPtrs);
if (!success)
{
_ASSERTE(!"unwindLazyState: Unwinding failed");
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
}
#endif // DACCESS_COMPILE
pvControlPc = GetIP(&ctx);
#endif // !FEATURE_PAL
if (funCallDepth > 0)
{
--funCallDepth;
if (funCallDepth == 0)
break;
}
else
{
// Determine whether given IP resides in JITted code. (It returns nonzero in that case.)
// Use it now to see if we've unwound to managed code yet.
BOOL fFailedReaderLock = FALSE;
BOOL fIsManagedCode = ExecutionManager::IsManagedCode(pvControlPc, hostCallPreference, &fFailedReaderLock);
if (fFailedReaderLock)
{
// We don't know if we would have been able to find a JIT
// manager, because we couldn't enter the reader lock without
// yielding (and our caller doesn't want us to yield). So abort
// now.
// Invalidate the lazyState we're returning, so the caller knows
// we aborted before we could fully unwind
unwoundstate->_isValid = false;
return;
}
if (fIsManagedCode)
break;
}
}
while(TRUE);
//
// Update unwoundState so that HelperMethodFrameRestoreState knows which
// registers have been potentially modified.
//
unwoundstate->_pc = ctx.Pc;
unwoundstate->_sp = ctx.Sp;
#ifdef DACCESS_COMPILE
// For DAC builds, we update the registers directly since we dont have context pointers
unwoundstate->captureR4_R11[0] = ctx.R4;
unwoundstate->captureR4_R11[1] = ctx.R5;
unwoundstate->captureR4_R11[2] = ctx.R6;
unwoundstate->captureR4_R11[3] = ctx.R7;
unwoundstate->captureR4_R11[4] = ctx.R8;
unwoundstate->captureR4_R11[5] = ctx.R9;
unwoundstate->captureR4_R11[6] = ctx.R10;
unwoundstate->captureR4_R11[7] = ctx.R11;
#else // !DACCESS_COMPILE
// For non-DAC builds, update the register state from context pointers
unwoundstate->_R4_R11[0] = (PDWORD)nonVolRegPtrs.R4;
unwoundstate->_R4_R11[1] = (PDWORD)nonVolRegPtrs.R5;
unwoundstate->_R4_R11[2] = (PDWORD)nonVolRegPtrs.R6;
unwoundstate->_R4_R11[3] = (PDWORD)nonVolRegPtrs.R7;
unwoundstate->_R4_R11[4] = (PDWORD)nonVolRegPtrs.R8;
unwoundstate->_R4_R11[5] = (PDWORD)nonVolRegPtrs.R9;
unwoundstate->_R4_R11[6] = (PDWORD)nonVolRegPtrs.R10;
unwoundstate->_R4_R11[7] = (PDWORD)nonVolRegPtrs.R11;
#endif // DACCESS_COMPILE
unwoundstate->_isValid = true;
}
void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
SUPPORTS_DAC;
}
CONTRACTL_END;
pRD->IsCallerContextValid = FALSE;
pRD->IsCallerSPValid = FALSE; // Don't add usage of this field. This is only temporary.
//
// Copy the saved state from the frame to the current context.
//
LOG((LF_GCROOTS, LL_INFO100000, "STACKWALK HelperMethodFrame::UpdateRegDisplay cached ip:%p, sp:%p\n", m_MachState._pc, m_MachState._sp));
#if defined(DACCESS_COMPILE)
// For DAC, we may get here when the HMF is still uninitialized.
// So we may need to unwind here.
if (!m_MachState.isValid())
{
// This allocation throws on OOM.
MachState* pUnwoundState = (MachState*)DacAllocHostOnlyInstance(sizeof(*pUnwoundState), true);
InsureInit(false, pUnwoundState);
pRD->pCurrentContext->Pc = pRD->ControlPC = pUnwoundState->_pc;
pRD->pCurrentContext->Sp = pRD->SP = pUnwoundState->_sp;
pRD->pCurrentContext->R4 = (DWORD)(pUnwoundState->captureR4_R11[0]);
pRD->pCurrentContext->R5 = (DWORD)(pUnwoundState->captureR4_R11[1]);
pRD->pCurrentContext->R6 = (DWORD)(pUnwoundState->captureR4_R11[2]);
pRD->pCurrentContext->R7 = (DWORD)(pUnwoundState->captureR4_R11[3]);
pRD->pCurrentContext->R8 = (DWORD)(pUnwoundState->captureR4_R11[4]);
pRD->pCurrentContext->R9 = (DWORD)(pUnwoundState->captureR4_R11[5]);
pRD->pCurrentContext->R10 = (DWORD)(pUnwoundState->captureR4_R11[6]);
pRD->pCurrentContext->R11 = (DWORD)(pUnwoundState->captureR4_R11[7]);
return;
}
#endif // DACCESS_COMPILE
// reset pContext; it's only valid for active (top-most) frame
pRD->pContext = NULL;
pRD->ControlPC = GetReturnAddress();
pRD->SP = (DWORD)(size_t)m_MachState._sp;
pRD->pCurrentContext->Pc = pRD->ControlPC;
pRD->pCurrentContext->Sp = pRD->SP;
pRD->pCurrentContext->R4 = *m_MachState._R4_R11[0];
pRD->pCurrentContext->R5 = *m_MachState._R4_R11[1];
pRD->pCurrentContext->R6 = *m_MachState._R4_R11[2];
pRD->pCurrentContext->R7 = *m_MachState._R4_R11[3];
pRD->pCurrentContext->R8 = *m_MachState._R4_R11[4];
pRD->pCurrentContext->R9 = *m_MachState._R4_R11[5];
pRD->pCurrentContext->R10 = *m_MachState._R4_R11[6];
pRD->pCurrentContext->R11 = *m_MachState._R4_R11[7];
pRD->pCurrentContextPointers->R4 = m_MachState._R4_R11[0];
pRD->pCurrentContextPointers->R5 = m_MachState._R4_R11[1];
pRD->pCurrentContextPointers->R6 = m_MachState._R4_R11[2];
pRD->pCurrentContextPointers->R7 = m_MachState._R4_R11[3];
pRD->pCurrentContextPointers->R8 = m_MachState._R4_R11[4];
pRD->pCurrentContextPointers->R9 = m_MachState._R4_R11[5];
pRD->pCurrentContextPointers->R10 = m_MachState._R4_R11[6];
pRD->pCurrentContextPointers->R11 = m_MachState._R4_R11[7];
pRD->pCurrentContextPointers->Lr = NULL;
}
#endif // !CROSSGEN_COMPILE
TADDR FixupPrecode::GetMethodDesc()
{
LIMITED_METHOD_DAC_CONTRACT;
// This lookup is also manually inlined in PrecodeFixupThunk assembly code
TADDR base = *PTR_TADDR(GetBase());
if (base == NULL)
return NULL;
return base + (m_MethodDescChunkIndex * MethodDesc::ALIGNMENT);
}
#ifdef DACCESS_COMPILE
void FixupPrecode::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
DacEnumMemoryRegion(dac_cast<TADDR>(this), sizeof(FixupPrecode));
DacEnumMemoryRegion(GetBase(), sizeof(TADDR));
}
#endif // DACCESS_COMPILE
#ifndef DACCESS_COMPILE
void StubPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator)
{
WRAPPER_NO_CONTRACT;
int n = 0;
m_rgCode[n++] = 0xf8df; // ldr r12, [pc, #8]
m_rgCode[n++] = 0xc008;
m_rgCode[n++] = 0xf8df; // ldr pc, [pc, #0]
m_rgCode[n++] = 0xf000;
_ASSERTE(n == _countof(m_rgCode));
m_pTarget = GetPreStubEntryPoint();
m_pMethodDesc = (TADDR)pMD;
}
#ifdef FEATURE_NATIVE_IMAGE_GENERATION
void StubPrecode::Fixup(DataImage *image)
{
WRAPPER_NO_CONTRACT;
image->FixupFieldToNode(this, offsetof(StubPrecode, m_pTarget),
image->GetHelperThunk(CORINFO_HELP_EE_PRESTUB),
0,
IMAGE_REL_BASED_PTR);
image->FixupField(this, offsetof(StubPrecode, m_pMethodDesc),
(void*)GetMethodDesc(),
0,
IMAGE_REL_BASED_PTR);
}
#endif // FEATURE_NATIVE_IMAGE_GENERATION
void NDirectImportPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator)
{
WRAPPER_NO_CONTRACT;
int n = 0;
m_rgCode[n++] = 0xf8df; // ldr r12, [pc, #4]
m_rgCode[n++] = 0xc004;
m_rgCode[n++] = 0xf8df; // ldr pc, [pc, #4]
m_rgCode[n++] = 0xf004;
_ASSERTE(n == _countof(m_rgCode));
m_pMethodDesc = (TADDR)pMD;
m_pTarget = GetEEFuncEntryPoint(NDirectImportThunk);
}
#ifdef FEATURE_NATIVE_IMAGE_GENERATION
void NDirectImportPrecode::Fixup(DataImage *image)
{
WRAPPER_NO_CONTRACT;
image->FixupField(this, offsetof(NDirectImportPrecode, m_pMethodDesc),
(void*)GetMethodDesc(),
0,
IMAGE_REL_BASED_PTR);
image->FixupFieldToNode(this, offsetof(NDirectImportPrecode, m_pTarget),
image->GetHelperThunk(CORINFO_HELP_EE_PINVOKE_FIXUP),
0,
IMAGE_REL_BASED_PTR);
}
#endif
void FixupPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator, int iMethodDescChunkIndex /*=0*/, int iPrecodeChunkIndex /*=0*/)
{
WRAPPER_NO_CONTRACT;
m_rgCode[0] = 0x46fc; // mov r12, pc
m_rgCode[1] = 0xf8df; // ldr pc, [pc, #4]
m_rgCode[2] = 0xf004;
// Initialize chunk indices only if they are not initialized yet. This is necessary to make MethodDesc::Reset work.
if (m_PrecodeChunkIndex == 0)
{
_ASSERTE(FitsInU1(iPrecodeChunkIndex));
m_PrecodeChunkIndex = static_cast<BYTE>(iPrecodeChunkIndex);
}
if (iMethodDescChunkIndex != -1)
{
if (m_MethodDescChunkIndex == 0)
{
_ASSERTE(FitsInU1(iMethodDescChunkIndex));
m_MethodDescChunkIndex = static_cast<BYTE>(iMethodDescChunkIndex);
}
if (*(void**)GetBase() == NULL)
*(void**)GetBase() = (BYTE*)pMD - (iMethodDescChunkIndex * MethodDesc::ALIGNMENT);
}
_ASSERTE(GetMethodDesc() == (TADDR)pMD);
if (pLoaderAllocator != NULL)
{
m_pTarget = GetEEFuncEntryPoint(PrecodeFixupThunk);
}
}
#ifdef FEATURE_NATIVE_IMAGE_GENERATION
// Partial initialization. Used to save regrouped chunks.
void FixupPrecode::InitForSave(int iPrecodeChunkIndex)
{
STANDARD_VM_CONTRACT;
m_rgCode[0] = 0x46fc; // mov r12, pc
m_rgCode[1] = 0xf8df; // ldr pc, [pc, #4]
m_rgCode[2] = 0xf004;
_ASSERTE(FitsInU1(iPrecodeChunkIndex));
m_PrecodeChunkIndex = static_cast<BYTE>(iPrecodeChunkIndex);
// The rest is initialized in code:FixupPrecode::Fixup
}
void FixupPrecode::Fixup(DataImage *image, MethodDesc * pMD)
{
STANDARD_VM_CONTRACT;
// Note that GetMethodDesc() does not return the correct value because of
// regrouping of MethodDescs into hot and cold blocks. That's why the caller
// has to supply the actual MethodDesc
SSIZE_T mdChunkOffset;
ZapNode * pMDChunkNode = image->GetNodeForStructure(pMD, &mdChunkOffset);
ZapNode * pHelperThunk = image->GetHelperThunk(CORINFO_HELP_EE_PRECODE_FIXUP);
image->FixupFieldToNode(this, offsetof(FixupPrecode, m_pTarget), pHelperThunk);
// Set the actual chunk index
FixupPrecode * pNewPrecode = (FixupPrecode *)image->GetImagePointer(this);
size_t mdOffset = mdChunkOffset - sizeof(MethodDescChunk);
size_t chunkIndex = mdOffset / MethodDesc::ALIGNMENT;
_ASSERTE(FitsInU1(chunkIndex));
pNewPrecode->m_MethodDescChunkIndex = (BYTE) chunkIndex;
// Fixup the base of MethodDescChunk
if (m_PrecodeChunkIndex == 0)
{
image->FixupFieldToNode(this, (BYTE *)GetBase() - (BYTE *)this,
pMDChunkNode, sizeof(MethodDescChunk));
}
}
#endif // FEATURE_NATIVE_IMAGE_GENERATION
void ThisPtrRetBufPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator)
{
WRAPPER_NO_CONTRACT;
int n = 0;
m_rgCode[n++] = 0x4684; // mov r12, r0
m_rgCode[n++] = 0x4608; // mov r0, r1
m_rgCode[n++] = 0xea4f; // mov r1, r12
m_rgCode[n++] = 0x010c;
m_rgCode[n++] = 0xf8df; // ldr pc, [pc, #0]
m_rgCode[n++] = 0xf000;
_ASSERTE(n == _countof(m_rgCode));
m_pTarget = GetPreStubEntryPoint();
m_pMethodDesc = (TADDR)pMD;
}
#ifdef HAS_REMOTING_PRECODE
void RemotingPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator)
{
WRAPPER_NO_CONTRACT;
int n = 0;
m_rgCode[n++] = 0xb502; // push {r1,lr}
m_rgCode[n++] = 0x4904; // ldr r1, [pc, #16] ; =m_pPrecodeRemotingThunk
m_rgCode[n++] = 0x4788; // blx r1
m_rgCode[n++] = 0xe8bd; // pop {r1,lr}
m_rgCode[n++] = 0x4002;
m_rgCode[n++] = 0xf8df; // ldr pc, [pc, #12] ; =m_pLocalTarget
m_rgCode[n++] = 0xf00c;
m_rgCode[n++] = 0xbf00; // nop ; padding for alignment
_ASSERTE(n == _countof(m_rgCode));
m_pMethodDesc = (TADDR)pMD;
m_pPrecodeRemotingThunk = GetEEFuncEntryPoint(PrecodeRemotingThunk);
m_pLocalTarget = GetPreStubEntryPoint();
}
#ifdef FEATURE_NATIVE_IMAGE_GENERATION
void RemotingPrecode::Fixup(DataImage *image, ZapNode *pCodeNode)
{
WRAPPER_NO_CONTRACT;
if (pCodeNode)
image->FixupFieldToNode(this, offsetof(RemotingPrecode, m_pLocalTarget),
pCodeNode,
THUMB_CODE,
IMAGE_REL_BASED_PTR);
else
image->FixupFieldToNode(this, offsetof(RemotingPrecode, m_pLocalTarget),
image->GetHelperThunk(CORINFO_HELP_EE_PRESTUB),
0,
IMAGE_REL_BASED_PTR);
image->FixupFieldToNode(this, offsetof(RemotingPrecode, m_pPrecodeRemotingThunk),
image->GetHelperThunk(CORINFO_HELP_EE_REMOTING_THUNK),
0,
IMAGE_REL_BASED_PTR);
image->FixupField(this, offsetof(RemotingPrecode, m_pMethodDesc),
(void*)GetMethodDesc(),
0,
IMAGE_REL_BASED_PTR);
}
#endif // FEATURE_NATIVE_IMAGE_GENERATION
void CTPMethodTable::ActivatePrecodeRemotingThunk()
{
// Nothing to do for ARM version of remoting precode (we don't burn the TP MethodTable pointer into
// PrecodeRemotingThunk directly).
}
#endif // HAS_REMOTING_PRECODE
#ifndef CROSSGEN_COMPILE
/*
Rough pseudo-code of interface dispatching:
// jitted code sets r0, r4:
r0 = object;
r4 = indirectionCell;
// jitted code calls *indirectionCell
switch (*indirectionCell)
{
case LookupHolder._stub:
// ResolveWorkerAsmStub:
*indirectionCell = DispatchHolder._stub;
call ResolveWorkerStatic, jump to target method;
case DispatchHolder._stub:
if (r0.methodTable == expectedMethodTable) jump to target method;
// ResolveHolder._stub._failEntryPoint:
jump to case ResolveHolder._stub._resolveEntryPoint;
case ResolveHolder._stub._resolveEntryPoint:
if (r0.methodTable in hashTable) jump to target method;
// ResolveHolder._stub._slowEntryPoint:
// ResolveWorkerChainLookupAsmStub:
// ResolveWorkerAsmStub:
if (_failEntryPoint called too many times) *indirectionCell = ResolveHolder._stub._resolveEntryPoint;
call ResolveWorkerStatic, jump to target method;
}
Note that ResolveWorkerChainLookupAsmStub currently points directly
to ResolveWorkerAsmStub; in the future, this could be separate.
*/
void LookupHolder::InitializeStatic()
{
// Nothing to initialize
}
void LookupHolder::Initialize(PCODE resolveWorkerTarget, size_t dispatchToken)
{
// Called directly by JITTED code
// See ResolveWorkerAsmStub
// ldr r12, [pc + 8] ; #_token
_stub._entryPoint[0] = 0xf8df;
_stub._entryPoint[1] = 0xc008;
// ldr pc, [pc] ; #_resolveWorkerTarget
_stub._entryPoint[2] = 0xf8df;
_stub._entryPoint[3] = 0xf000;
_stub._resolveWorkerTarget = resolveWorkerTarget;
_stub._token = dispatchToken;
_ASSERTE(4 == LookupStub::entryPointLen);
}