This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
asmhelpers.S
1378 lines (1086 loc) · 44.2 KB
/
asmhelpers.S
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.
#include "asmconstants.h"
#include "unixasmmacros.inc"
// LPVOID __stdcall GetCurrentIP(void)//
LEAF_ENTRY GetCurrentIP, _TEXT
mov x0, lr
ret lr
LEAF_END GetCurrentIP, _TEXT
// LPVOID __stdcall GetCurrentSP(void)//
LEAF_ENTRY GetCurrentSP, _TEXT
mov x0, sp
ret lr
LEAF_END GetCurrentSP, _TEXT
//-----------------------------------------------------------------------------
// This routine captures the machine state. It is used by helper method frame
//-----------------------------------------------------------------------------
//void LazyMachStateCaptureState(struct LazyMachState *pState)//
LEAF_ENTRY LazyMachStateCaptureState, _TEXT
// marks that this is not yet valid
mov w1, #0
str w1, [x0, #MachState__isValid]
str lr, [x0, #LazyMachState_captureIp]
// str instruction does not save sp register directly so move to temp register
mov x1, sp
str x1, [x0, #LazyMachState_captureSp]
// save non-volatile registers that can contain object references
add x1, x0, #LazyMachState_captureX19_X29
stp x19, x20, [x1, #(16*0)]
stp x21, x22, [x1, #(16*1)]
stp x23, x24, [x1, #(16*2)]
stp x25, x26, [x1, #(16*3)]
stp x27, x28, [x1, #(16*4)]
str x29, [x1, #(16*5)]
ret lr
LEAF_END LazyMachStateCaptureState, _TEXT
//
// If a preserved register were pushed onto the stack between
// the managed caller and the H_M_F, ptrX19_X29 will point to its
// location on the stack and it would have been updated on the
// stack by the GC already and it will be popped back into the
// appropriate register when the appropriate epilog is run.
//
// Otherwise, the register is preserved across all the code
// in this HCALL or FCALL, so we need to update those registers
// here because the GC will have updated our copies in the
// frame.
//
// So, if ptrX19_X29 points into the MachState, we need to update
// the register here. That's what this macro does.
//
.macro RestoreRegMS regIndex, reg
// Incoming:
//
// x0 = address of MachState
//
// $regIndex: Index of the register (x19-x28). For x19, index is 19.
//For x20, index is 20, and so on.
//
// $reg: Register name (e.g. x19, x20, etc)
//
// Get the address of the specified captured register from machine state
add x2, x0, #(MachState__captureX19_X29 + ((\regIndex-19)*8))
// Get the content of specified preserved register pointer from machine state
ldr x3, [x0, #(MachState__ptrX19_X29 + ((\regIndex-19)*8))]
cmp x2, x3
bne LOCAL_LABEL(NoRestore_\reg)
ldr \reg, [x2]
LOCAL_LABEL(NoRestore_\reg):
.endmacro
// EXTERN_C int __fastcall HelperMethodFrameRestoreState(
// INDEBUG_COMMA(HelperMethodFrame *pFrame)
// MachState *pState
// )
LEAF_ENTRY HelperMethodFrameRestoreState, _TEXT
#ifdef _DEBUG
mov x0, x1
#endif
// If machine state is invalid, then simply exit
ldr w1, [x0, #MachState__isValid]
cmp w1, #0
beq LOCAL_LABEL(Done)
RestoreRegMS 19, X19
RestoreRegMS 20, X20
RestoreRegMS 21, X21
RestoreRegMS 22, X22
RestoreRegMS 23, X23
RestoreRegMS 24, X24
RestoreRegMS 25, X25
RestoreRegMS 26, X26
RestoreRegMS 27, X27
RestoreRegMS 28, X28
RestoreRegMS 29, X29
LOCAL_LABEL(Done):
// Its imperative that the return value of HelperMethodFrameRestoreState is zero
// as it is used in the state machine to loop until it becomes zero.
// Refer to HELPER_METHOD_FRAME_END macro for details.
mov x0,#0
ret lr
LEAF_END HelperMethodFrameRestoreState, _TEXT
// ------------------------------------------------------------------
// The call in ndirect import precode points to this function.
NESTED_ENTRY NDirectImportThunk, _TEXT, NoHandler
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -224
SAVE_ARGUMENT_REGISTERS sp, 16
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 96
mov x0, x12
bl NDirectImportWorker
mov x12, x0
// pop the stack and restore original register state
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 96
RESTORE_ARGUMENT_REGISTERS sp, 16
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 224
// If we got back from NDirectImportWorker, the MD has been successfully
// linked. Proceed to execute the original DLL call.
EPILOG_BRANCH_REG x12
NESTED_END NDirectImportThunk, _TEXT
// ------------------------------------------------------------------
// The call in fixup precode initally points to this function.
// The pupose of this function is to load the MethodDesc and forward the call to prestub.
NESTED_ENTRY PrecodeFixupThunk, _TEXT, NoHandler
// x12 = FixupPrecode *
// On Exit
// x12 = MethodDesc*
// x13, x14 Trashed
// Inline computation done by FixupPrecode::GetMethodDesc()
ldrb w13, [x12, #Offset_PrecodeChunkIndex] //m_PrecodeChunkIndex
ldrb w14, [x12, #Offset_MethodDescChunkIndex] // m_MethodDescChunkIndex
add x12, x12, w13, uxtw #FixupPrecode_ALIGNMENT_SHIFT_1
add x13, x12, w13, uxtw #FixupPrecode_ALIGNMENT_SHIFT_2
ldr x13, [x13, #SIZEOF__FixupPrecode]
add x12, x13, w14, uxtw #MethodDesc_ALIGNMENT_SHIFT
b ThePreStub
NESTED_END PrecodeFixupThunk, _TEXT
// ------------------------------------------------------------------
NESTED_ENTRY ThePreStub, _TEXT, NoHandler
PROLOG_WITH_TRANSITION_BLOCK
add x0, sp, #__PWTB_TransitionBlock // pTransitionBlock
mov x1, METHODDESC_REGISTER // pMethodDesc
bl PreStubWorker
mov x9, x0
EPILOG_WITH_TRANSITION_BLOCK_TAILCALL
EPILOG_BRANCH_REG x9
NESTED_END ThePreStub, _TEXT
// ------------------------------------------------------------------
// ThePreStubPatch()
LEAF_ENTRY ThePreStubPatch, _TEXT
nop
.globl C_FUNC(ThePreStubPatchLabel)
C_FUNC(ThePreStubPatchLabel):
ret lr
LEAF_END ThePreStubPatch, _TEXT
//-----------------------------------------------------------------------------
// The following Macros help in WRITE_BARRIER Implemetations
// WRITE_BARRIER_ENTRY
//
// Declare the start of a write barrier function. Use similarly to NESTED_ENTRY. This is the only legal way
// to declare a write barrier function.
//
.macro WRITE_BARRIER_ENTRY name
LEAF_ENTRY \name, _TEXT
.endmacro
// WRITE_BARRIER_END
//
// The partner to WRITE_BARRIER_ENTRY, used like NESTED_END.
//
.macro WRITE_BARRIER_END name
LEAF_END_MARKED \name, _TEXT
.endmacro
// ------------------------------------------------------------------
// Start of the writeable code region
LEAF_ENTRY JIT_PatchedCodeStart, _TEXT
ret lr
LEAF_END JIT_PatchedCodeStart, _TEXT
// void JIT_UpdateWriteBarrierState(bool skipEphemeralCheck)
//
// Update shadow copies of the various state info required for barrier
//
// State info is contained in a literal pool at the end of the function
// Placed in text section so that it is close enough to use ldr literal and still
// be relocatable. Eliminates need for PREPARE_EXTERNAL_VAR in hot code.
//
// Align and group state info together so it fits in a single cache line
// and each entry can be written atomically
//
WRITE_BARRIER_ENTRY JIT_UpdateWriteBarrierState
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -16
// x0-x7 will contain intended new state
// x8 will preserve skipEphemeralCheck
// x12 will be used for pointers
mov x8, x0
PREPARE_EXTERNAL_VAR g_card_table, x12
ldr x0, [x12]
#ifdef FEATURE_MANUALLY_MANAGED_CARD_BUNDLES
PREPARE_EXTERNAL_VAR g_card_bundle_table, x12
ldr x1, [x12]
#endif
#ifdef WRITE_BARRIER_CHECK
PREPARE_EXTERNAL_VAR g_GCShadow, x12
ldr x2, [x12]
#endif
#ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
PREPARE_EXTERNAL_VAR g_sw_ww_table, x12
ldr x3, [x12]
#endif
PREPARE_EXTERNAL_VAR g_ephemeral_low, x12
ldr x4, [x12]
PREPARE_EXTERNAL_VAR g_ephemeral_high, x12
ldr x5, [x12]
cbz x8, LOCAL_LABEL(EphemeralCheckEnabled)
movz x4, #0
movn x5, #0
LOCAL_LABEL(EphemeralCheckEnabled):
PREPARE_EXTERNAL_VAR g_lowest_address, x12
ldr x6, [x12]
PREPARE_EXTERNAL_VAR g_highest_address, x12
ldr x7, [x12]
// Update wbs state
adr x12, LOCAL_LABEL(wbs_begin)
stp x0, x1, [x12], 16
stp x2, x3, [x12], 16
stp x4, x5, [x12], 16
stp x6, x7, [x12], 16
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 16
EPILOG_RETURN
// Begin patchable literal pool
.balign 64 // Align to power of two at least as big as patchable literal pool so that it fits optimally in cache line
LOCAL_LABEL(wbs_begin):
LOCAL_LABEL(wbs_card_table):
.quad 0
LOCAL_LABEL(wbs_card_bundle_table):
.quad 0
LOCAL_LABEL(wbs_GCShadow):
.quad 0
LOCAL_LABEL(wbs_sw_ww_table):
.quad 0
LOCAL_LABEL(wbs_ephemeral_low):
.quad 0
LOCAL_LABEL(wbs_ephemeral_high):
.quad 0
LOCAL_LABEL(wbs_lowest_address):
.quad 0
LOCAL_LABEL(wbs_highest_address):
.quad 0
WRITE_BARRIER_END JIT_UpdateWriteBarrierState
// ------------------------------------------------------------------
// End of the writeable code region
LEAF_ENTRY JIT_PatchedCodeLast, _TEXT
ret lr
LEAF_END JIT_PatchedCodeLast, _TEXT
// void JIT_ByRefWriteBarrier
// On entry:
// x13 : the source address (points to object reference to write)
// x14 : the destination address (object reference written here)
//
// On exit:
// x12 : trashed
// x13 : incremented by 8
// x14 : incremented by 8
// x15 : trashed
// x17 : trashed (ip1) if FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
//
WRITE_BARRIER_ENTRY JIT_ByRefWriteBarrier
ldr x15, [x13], 8
b C_FUNC(JIT_CheckedWriteBarrier)
WRITE_BARRIER_END JIT_ByRefWriteBarrier
//-----------------------------------------------------------------------------
// Simple WriteBarriers
// void JIT_CheckedWriteBarrier(Object** dst, Object* src)
// On entry:
// x14 : the destination address (LHS of the assignment)
// x15 : the object reference (RHS of the assignment)
//
// On exit:
// x12 : trashed
// x14 : trashed (incremented by 8 to implement JIT_ByRefWriteBarrier contract)
// x15 : trashed
// x17 : trashed (ip1) if FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
//
WRITE_BARRIER_ENTRY JIT_CheckedWriteBarrier
ldr x12, LOCAL_LABEL(wbs_lowest_address)
cmp x14, x12
ldr x12, LOCAL_LABEL(wbs_highest_address)
// Compare against the upper bound if the previous comparison indicated
// that the destination address is greater than or equal to the lower
// bound. Otherwise, set the C flag (specified by the 0x2) so that the
// branch below is not taken.
ccmp x14, x12, #0x2, hs
blo C_FUNC(JIT_WriteBarrier)
LOCAL_LABEL(NotInHeap):
str x15, [x14], 8
ret lr
WRITE_BARRIER_END JIT_CheckedWriteBarrier
// void JIT_WriteBarrier(Object** dst, Object* src)
// On entry:
// x14 : the destination address (LHS of the assignment)
// x15 : the object reference (RHS of the assignment)
//
// On exit:
// x12 : trashed
// x14 : trashed (incremented by 8 to implement JIT_ByRefWriteBarrier contract)
// x15 : trashed
// x17 : trashed (ip1) if FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
//
WRITE_BARRIER_ENTRY JIT_WriteBarrier
stlr x15, [x14]
#ifdef WRITE_BARRIER_CHECK
// Update GC Shadow Heap
// Do not perform the work if g_GCShadow is 0
ldr x12, LOCAL_LABEL(wbs_GCShadow)
cbz x12, LOCAL_LABEL(ShadowUpdateDisabled)
// need temporary register. Save before using.
str x13, [sp, #-16]!
// Compute address of shadow heap location:
// pShadow = g_GCShadow + (x14 - g_lowest_address)
ldr x13, LOCAL_LABEL(wbs_lowest_address)
sub x13, x14, x13
add x12, x13, x12
// if (pShadow >= g_GCShadowEnd) goto end
PREPARE_EXTERNAL_VAR g_GCShadowEnd, x13
ldr x13, [x13]
cmp x12, x13
bhs LOCAL_LABEL(ShadowUpdateEnd)
// *pShadow = x15
str x15, [x12]
// Ensure that the write to the shadow heap occurs before the read from the GC heap so that race
// conditions are caught by INVALIDGCVALUE.
dmb ish
// if ([x14] == x15) goto end
ldr x13, [x14]
cmp x13, x15
beq LOCAL_LABEL(ShadowUpdateEnd)
// *pShadow = INVALIDGCVALUE (0xcccccccd)
movz x13, #0xcccd
movk x13, #0xcccc, LSL #16
str x13, [x12]
LOCAL_LABEL(ShadowUpdateEnd):
ldr x13, [sp], #16
LOCAL_LABEL(ShadowUpdateDisabled):
#endif
#ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
// Update the write watch table if necessary
ldr x12, LOCAL_LABEL(wbs_sw_ww_table)
cbz x12, LOCAL_LABEL(CheckCardTable)
add x12, x12, x14, lsr #0xc // SoftwareWriteWatch::AddressToTableByteIndexShift
ldrb w17, [x12]
cbnz x17, LOCAL_LABEL(CheckCardTable)
mov w17, #0xFF
strb w17, [x12]
#endif
LOCAL_LABEL(CheckCardTable):
// Branch to Exit if the reference is not in the Gen0 heap
//
ldr x12, LOCAL_LABEL(wbs_ephemeral_low)
cbz x12, LOCAL_LABEL(SkipEphemeralCheck)
cmp x15, x12
ldr x12, LOCAL_LABEL(wbs_ephemeral_high)
// Compare against the upper bound if the previous comparison indicated
// that the destination address is greater than or equal to the lower
// bound. Otherwise, set the C flag (specified by the 0x2) so that the
// branch to exit is taken.
ccmp x15, x12, #0x2, hs
bhi LOCAL_LABEL(Exit)
LOCAL_LABEL(SkipEphemeralCheck):
// Check if we need to update the card table
ldr x12, LOCAL_LABEL(wbs_card_table)
add x15, x12, x14, lsr #11
ldrb w12, [x15]
cmp x12, 0xFF
beq LOCAL_LABEL(Exit)
LOCAL_LABEL(UpdateCardTable):
mov x12, 0xFF
strb w12, [x15]
#ifdef FEATURE_MANUALLY_MANAGED_CARD_BUNDLES
// Check if we need to update the card bundle table
ldr x12, LOCAL_LABEL(wbs_card_bundle_table)
add x15, x12, x14, lsr #21
ldrb w12, [x15]
cmp x12, 0xFF
beq LOCAL_LABEL(Exit)
LOCAL_LABEL(UpdateCardBundle):
mov x12, 0xFF
strb w12, [x15]
#endif
LOCAL_LABEL(Exit):
add x14, x14, 8
ret lr
WRITE_BARRIER_END JIT_WriteBarrier
//------------------------------------------------
// VirtualMethodFixupStub
//
// In NGEN images, virtual slots inherited from cross-module dependencies
// point to a jump thunk that calls into the following function that will
// call into a VM helper. The VM helper is responsible for patching up
// thunk, upon executing the precode, so that all subsequent calls go directly
// to the actual method body.
//
// This is done lazily for performance reasons.
//
// On entry:
//
// x0 = "this" pointer
// x12 = Address of thunk
NESTED_ENTRY VirtualMethodFixupStub, _TEXT, NoHandler
// Save arguments and return address
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -224
SAVE_ARGUMENT_REGISTERS sp, 16
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 96
// Refer to ZapImportVirtualThunk::Save
// for details on this.
//
// Move the thunk start address in x1
mov x1, x12
// Call the helper in the VM to perform the actual fixup
// and tell us where to tail call. x0 already contains
// the this pointer.
bl C_FUNC(VirtualMethodFixupWorker)
// On return, x0 contains the target to tailcall to
mov x12, x0
// pop the stack and restore original register state
RESTORE_ARGUMENT_REGISTERS sp, 16
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 96
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 224
PATCH_LABEL VirtualMethodFixupPatchLabel
// and tailcall to the actual method
EPILOG_BRANCH_REG x12
NESTED_END VirtualMEthodFixupStub, _TEXT
//------------------------------------------------
// ExternalMethodFixupStub
//
// In NGEN images, calls to cross-module external methods initially
// point to a jump thunk that calls into the following function that will
// call into a VM helper. The VM helper is responsible for patching up the
// thunk, upon executing the precode, so that all subsequent calls go directly
// to the actual method body.
//
// This is done lazily for performance reasons.
//
// On entry:
//
// x12 = Address of thunk
NESTED_ENTRY ExternalMethodFixupStub, _TEXT, NoHandler
PROLOG_WITH_TRANSITION_BLOCK
add x0, sp, #__PWTB_TransitionBlock // pTransitionBlock
mov x1, x12 // pThunk
bl C_FUNC(ExternalMethodFixupWorker)
// mov the address we patched to in x12 so that we can tail call to it
mov x12, x0
EPILOG_WITH_TRANSITION_BLOCK_TAILCALL
PATCH_LABEL ExternalMethodFixupPatchLabel
EPILOG_BRANCH_REG x12
NESTED_END ExternalMethodFixupStub, _TEXT
// void SinglecastDelegateInvokeStub(Delegate *pThis)
LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT
cmp x0, #0
beq LOCAL_LABEL(LNullThis)
ldr x16, [x0, #DelegateObject___methodPtr]
ldr x0, [x0, #DelegateObject___target]
br x16
LOCAL_LABEL(LNullThis):
mov x0, #CORINFO_NullReferenceException_ASM
b C_FUNC(JIT_InternalThrow)
LEAF_END SinglecastDelegateInvokeStub, _TEXT
#ifdef FEATURE_COMINTEROP
#define ComCallPreStub_FrameSize (SIZEOF__GSCookie + SIZEOF__ComMethodFrame)
#define ComCallPreStub_FirstStackAdjust (8 + SIZEOF__ArgumentRegisters + 2 * 8) // x8, reg args , fp & lr already pushed
#define ComCallPreStub_StackAlloc0 (ComCallPreStub_FrameSize - ComCallPreStub_FirstStackAdjust)
#define ComCallPreStub_StackAlloc1 (ComCallPreStub_StackAlloc0 + SIZEOF__FloatArgumentRegisters + 8)// 8 for ErrorReturn
#define ComCallPreStub_StackAlloc (ComCallPreStub_StackAlloc1 + (ComCallPreStub_StackAlloc1 & 8))
#define ComCallPreStub_FrameOffset (ComCallPreStub_StackAlloc - (SIZEOF__ComMethodFrame - ComCallPreStub_FirstStackAdjust))
#define ComCallPreStub_ErrorReturnOffset0 SIZEOF__FloatArgumentRegisters
#define ComCallPreStub_FirstStackAdjust (ComCallPreStub_ErrorReturnOffset0 + (ComCallPreStub_ErrorReturnOffset0 & 8))
// ------------------------------------------------------------------
// COM to CLR stub called the first time a particular method is invoked.//
//
// On entry:
// x12 : ComCallMethodDesc* provided by prepad thunk
// plus user arguments in registers and on the stack
//
// On exit:
// tail calls to real method
//
NESTED_ENTRY ComCallPreStub, _TEXT, NoHandler
// Save arguments and return address
PROLOG_SAVE_REG_PAIR fp, lr, -ComCallPreStub_FirstStackAdjust!
PROLOG_STACK_ALLOC ComCallPreStub_StackAlloc
SAVE_ARGUMENT_REGISTERS sp, (16+ComCallPreStub_StackAlloc)
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 0
str x12, [sp, #(ComCallPreStub_FrameOffset + UnmanagedToManagedFrame__m_pvDatum)]
add x0, sp, #(ComCallPreStub_FrameOffset)
add x1, sp, #(ComCallPreStub_ErrorReturnOffset)
bl ComPreStubWorker
cbz x0, ComCallPreStub_ErrorExit
mov x12, x0
// pop the stack and restore original register state
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 0
RESTORE_ARGUMENT_REGISTERS sp, (16+ComCallPreStub_StackAlloc)
EPILOG_STACK_FREE ComCallPreStub_StackAlloc
EPILOG_RESTORE_REG_PAIR fp, lr, ComCallPreStub_FirstStackAdjust!
// and tailcall to the actual method
EPILOG_BRANCH_REG x12
ComCallPreStub_ErrorExit
ldr x0, [sp, #(ComCallPreStub_ErrorReturnOffset)] // ErrorReturn
// pop the stack
EPILOG_STACK_FREE ComCallPreStub_StackAlloc
EPILOG_RESTORE_REG_PAIR fp, lr, ComCallPreStub_FirstStackAdjust!
EPILOG_RETURN
NESTED_END ComCallPreStub, _TEXT
// ------------------------------------------------------------------
// COM to CLR stub which sets up a ComMethodFrame and calls COMToCLRWorker.
//
// On entry:
// x12 : ComCallMethodDesc* provided by prepad thunk
// plus user arguments in registers and on the stack
//
// On exit:
// Result in x0/d0 as per the real method being called
//
NESTED_ENTRY GenericComCallStub, _TEXT, NoHandler
// Save arguments and return address
PROLOG_SAVE_REG_PAIR fp, lr, -GenericComCallStub_FirstStackAdjust!
PROLOG_STACK_ALLOC GenericComCallStub_StackAlloc
SAVE_ARGUMENT_REGISTERS sp, (16+GenericComCallStub_StackAlloc)
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 0
str x12, [sp, #(GenericComCallStub_FrameOffset + UnmanagedToManagedFrame__m_pvDatum)]
add x1, sp, #GenericComCallStub_FrameOffset
bl COMToCLRWorker
// pop the stack
EPILOG_STACK_FREE GenericComCallStub_StackAlloc
EPILOG_RESTORE_REG_PAIR fp, lr, GenericComCallStub_FirstStackAdjust!
EPILOG_RETURN
NESTED_END GenericComCallStub, _TEXT
// ------------------------------------------------------------------
// COM to CLR stub called from COMToCLRWorker that actually dispatches to the real managed method.
//
// On entry:
// x0 : dwStackSlots, count of argument stack slots to copy
// x1 : pFrame, ComMethodFrame pushed by GenericComCallStub above
// x2 : pTarget, address of code to call
// x3 : pSecretArg, hidden argument passed to target above in x12
// x4 : pDangerousThis, managed 'this' reference
//
// On exit:
// Result in x0/d0 as per the real method being called
//
NESTED_ENTRY COMToCLRDispatchHelper, _TEXT,CallDescrWorkerUnwindFrameChainHandler
PROLOG_SAVE_REG_PAIR fp, lr, -16!
cbz x0, COMToCLRDispatchHelper_RegSetup
add x9, x1, #SIZEOF__ComMethodFrame
add x9, x9, x0, LSL #3
COMToCLRDispatchHelper_StackLoop
ldr x8, [x9, #-8]!
str x8, [sp, #-8]!
sub x0, x0, #1
cbnz x0, COMToCLRDispatchHelper_StackLoop
COMToCLRDispatchHelper_RegSetup
RESTORE_FLOAT_ARGUMENT_REGISTERS x1, -1 * GenericComCallStub_FrameOffset
mov lr, x2
mov x12, x3
mov x0, x4
ldp x2, x3, [x1, #(SIZEOF__ComMethodFrame - SIZEOF__ArgumentRegisters + 16)]
ldp x4, x5, [x1, #(SIZEOF__ComMethodFrame - SIZEOF__ArgumentRegisters + 32)]
ldp x6, x7, [x1, #(SIZEOF__ComMethodFrame - SIZEOF__ArgumentRegisters + 48)]
ldr x8, [x1, #(SIZEOF__ComMethodFrame - SIZEOF__ArgumentRegisters - 8)]
ldr x1, [x1, #(SIZEOF__ComMethodFrame - SIZEOF__ArgumentRegisters + 8)]
blr lr
EPILOG_STACK_RESTORE
EPILOG_RESTORE_REG_PAIR fp, lr, 16!
EPILOG_RETURN
NESTED_END COMToCLRDispatchHelper, _TEXT
#endif // FEATURE_COMINTEROP
//
// x12 = UMEntryThunk*
//
NESTED_ENTRY TheUMEntryPrestub, _TEXT, UnhandledExceptionHandlerUnix
// Save arguments and return address
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -224
SAVE_ARGUMENT_REGISTERS sp, 16
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 96
mov x0, x12
bl C_FUNC(TheUMEntryPrestubWorker)
// save real target address in x12.
mov x12, x0
// pop the stack and restore original register state
RESTORE_ARGUMENT_REGISTERS sp, 16
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 96
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 224
// and tailcall to the actual method
EPILOG_BRANCH_REG x12
NESTED_END TheUMEntryPrestub, _TEXT
//
// x12 = UMEntryThunk*
//
NESTED_ENTRY UMThunkStub, _TEXT, UnhandledExceptionHandlerUnix
// Save arguments and return address
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -112 // 72 for regArgs, 8 for x19 & 8 for x12
// save callee saved reg x19. x19 is used in the method to store thread*
PROLOG_SAVE_REG x19, 96
SAVE_ARGUMENT_REGISTERS sp, 16
#define UMThunkStub_HiddenArg 88 // offset of saved UMEntryThunk *
#define UMThunkStub_StackArgs 112 // offset of original stack args (total size of UMThunkStub frame)
// save UMEntryThunk*
str x12, [sp, #UMThunkStub_HiddenArg]
// assuming GetThread does not clobber FP Args
bl C_FUNC(GetThread)
cbz x0, LOCAL_LABEL(UMThunkStub_DoThreadSetup)
LOCAL_LABEL(UMThunkStub_HaveThread):
mov x19, x0 // x19 = Thread *
mov x9, 1
// m_fPreemptiveGCDisabled is 4 byte field so using 32-bit variant
str w9, [x19, #Thread__m_fPreemptiveGCDisabled]
PREPARE_EXTERNAL_VAR g_TrapReturningThreads, x2
ldr x3, [x2]
// assuming x0 contains Thread* before jumping to UMThunkStub_DoTrapReturningThreads
cbnz x3, LOCAL_LABEL(UMThunkStub_DoTrapReturningThreads)
LOCAL_LABEL(UMThunkStub_InCooperativeMode):
ldr x12, [fp, #UMThunkStub_HiddenArg] // x12 = UMEntryThunk*
ldr x3, [x12, #UMEntryThunk__m_pUMThunkMarshInfo] // x3 = m_pUMThunkMarshInfo
// m_cbActualArgSize is UINT32 and hence occupies 4 bytes
ldr w2, [x3, #UMThunkMarshInfo__m_cbActualArgSize] // w2 = Stack arg bytes
cbz w2, LOCAL_LABEL(UMThunkStub_RegArgumentsSetup)
// extend to 64-bits
uxtw x2, w2
// Source pointer
add x0, fp, #UMThunkStub_StackArgs
// move source pointer to end of Stack Args
add x0, x0, x2
// Count of stack slot pairs to copy (divide by 16)
lsr x1, x2, #4
// Is there an extra stack slot (can happen when stack arg bytes not multiple of 16)
and x2, x2, #8
// If yes then start source pointer from 16 byte aligned stack slot
add x0, x0, x2
// increment stack slot pair count by 1 if x2 is not zero
add x1, x1, x2, LSR #3
LOCAL_LABEL(UMThunkStub_StackLoop):
ldp x4, x5, [x0, #-16]! // pre-Index
stp x4, x5, [sp, #-16]! // pre-Index
subs x1, x1, #1
bne LOCAL_LABEL(UMThunkStub_StackLoop)
LOCAL_LABEL(UMThunkStub_RegArgumentsSetup):
ldr x16, [x3, #UMThunkMarshInfo__m_pILStub]
RESTORE_ARGUMENT_REGISTERS fp, 16
blr x16
LOCAL_LABEL(UMThunkStub_PostCall):
mov x4, 0
// m_fPreemptiveGCDisabled is 4 byte field so using 32-bit variant
str w4, [x19, #Thread__m_fPreemptiveGCDisabled]
EPILOG_STACK_RESTORE
EPILOG_RESTORE_REG x19, 96
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 112
EPILOG_RETURN
LOCAL_LABEL(UMThunkStub_DoThreadSetup):
sub sp, sp, #SIZEOF__FloatArgumentRegisters
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 0
bl C_FUNC(CreateThreadBlockThrow)
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 0
add sp, sp, #SIZEOF__FloatArgumentRegisters
b LOCAL_LABEL(UMThunkStub_HaveThread)
LOCAL_LABEL(UMThunkStub_DoTrapReturningThreads):
sub sp, sp, #SIZEOF__FloatArgumentRegisters
SAVE_FLOAT_ARGUMENT_REGISTERS sp, 0
// x0 already contains Thread* pThread
// UMEntryThunk* pUMEntry
ldr x1, [fp, #UMThunkStub_HiddenArg]
bl C_FUNC(UMThunkStubRareDisableWorker)
RESTORE_FLOAT_ARGUMENT_REGISTERS sp, 0
add sp, sp, #SIZEOF__FloatArgumentRegisters
b LOCAL_LABEL(UMThunkStub_InCooperativeMode)
NESTED_END UMThunkStub, _TEXT
#ifdef FEATURE_HIJACK
// ------------------------------------------------------------------
// Hijack function for functions which return a scalar type or a struct (value type)
NESTED_ENTRY OnHijackTripThread, _TEXT, NoHandler
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -144
// Spill callee saved registers
PROLOG_SAVE_REG_PAIR x19, x20, 16
PROLOG_SAVE_REG_PAIR x21, x22, 32
PROLOG_SAVE_REG_PAIR x23, x24, 48
PROLOG_SAVE_REG_PAIR x25, x26, 64
PROLOG_SAVE_REG_PAIR x27, x28, 80
// save any integral return value(s)
stp x0, x1, [sp, #96]
// save any FP/HFA return value(s)
stp d0, d1, [sp, #112]
stp d2, d3, [sp, #128]
mov x0, sp
bl OnHijackWorker
// restore any integral return value(s)
ldp x0, x1, [sp, #96]
// restore any FP/HFA return value(s)
ldp d0, d1, [sp, #112]
ldp d2, d3, [sp, #128]
EPILOG_RESTORE_REG_PAIR x19, x20, 16
EPILOG_RESTORE_REG_PAIR x21, x22, 32
EPILOG_RESTORE_REG_PAIR x23, x24, 48
EPILOG_RESTORE_REG_PAIR x25, x26, 64
EPILOG_RESTORE_REG_PAIR x27, x28, 80
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 144
EPILOG_RETURN
NESTED_END OnHijackTripThread, _TEXT
#endif // FEATURE_HIJACK
// ------------------------------------------------------------------
// Redirection Stub for GC in fully interruptible method
//GenerateRedirectedHandledJITCaseStub GCThreadControl
// ------------------------------------------------------------------
//GenerateRedirectedHandledJITCaseStub DbgThreadControl
// ------------------------------------------------------------------
//GenerateRedirectedHandledJITCaseStub UserSuspend
#ifdef _DEBUG
// ------------------------------------------------------------------
// Redirection Stub for GC Stress
GenerateRedirectedHandledJITCaseStub GCStress
#endif
// ------------------------------------------------------------------
// This helper enables us to call into a funclet after restoring Fp register
NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler
// On entry:
//
// X0 = throwable
// X1 = PC to invoke
// X2 = address of X19 register in CONTEXT record// used to restore the non-volatile registers of CrawlFrame
// X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved.
//
// Using below prolog instead of PROLOG_SAVE_REG_PAIR_INDEXED fp,lr, -96
// is intentional. Above statement would also emit instruction to save
// sp in fp. If sp is saved in fp in prolog then it is not expected that fp can change in the body
// of method. However, this method needs to be able to change fp before calling funclet.
// This is required to access locals in funclet.
PROLOG_SAVE_REG_PAIR_INDEXED x29, lr, -96
// Spill callee saved registers
PROLOG_SAVE_REG_PAIR x19, x20, 16
PROLOG_SAVE_REG_PAIR x21, x22, 32
PROLOG_SAVE_REG_PAIR x23, x24, 48
PROLOG_SAVE_REG_PAIR x25, x26, 64
PROLOG_SAVE_REG_PAIR x27, x28, 80
// Save the SP of this function
mov x4, sp
str x4, [x3]
ldp x19, x20, [x2, #0]
ldp x21, x22, [x2, #16]
ldp x23, x24, [x2, #32]
ldp x25, x26, [x2, #48]
ldp x27, x28, [x2, #64]
ldr fp, [x2, #80] // offset of fp in CONTEXT relative to X19
// Invoke the funclet
blr x1
nop
EPILOG_RESTORE_REG_PAIR x19, x20, 16
EPILOG_RESTORE_REG_PAIR x21, x22, 32
EPILOG_RESTORE_REG_PAIR x23, x24, 48
EPILOG_RESTORE_REG_PAIR x25, x26, 64
EPILOG_RESTORE_REG_PAIR x27, x28, 80
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 96
EPILOG_RETURN
NESTED_END CallEHFunclet, _TEXT
// This helper enables us to call into a filter funclet by passing it the CallerSP to lookup the
// frame pointer for accessing the locals in the parent method.
NESTED_ENTRY CallEHFilterFunclet, _TEXT, NoHandler
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -16
// On entry:
//
// X0 = throwable
// X1 = SP of the caller of the method/funclet containing the filter
// X2 = PC to invoke
// X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved.
//
// Save the SP of this function
str fp, [x3]
// Invoke the filter funclet
blr x2
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 16
EPILOG_RETURN
NESTED_END CallEHFilterFunclet, _TEXT
#define FaultingExceptionFrame_StackAlloc (SIZEOF__GSCookie + SIZEOF__FaultingExceptionFrame)
#define FaultingExceptionFrame_FrameOffset SIZEOF__GSCookie
.macro GenerateRedirectedStubWithFrame stub, target
//
// This is the primary function to which execution will be redirected to.
//
NESTED_ENTRY \stub, _TEXT, NoHandler
//
// IN: lr: original IP before redirect
//
PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -16
PROLOG_STACK_ALLOC FaultingExceptionFrame_StackAlloc
// At this point, the stack maybe misaligned if the thread abort was asynchronously