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
/
Copy pathJitHelpers_Slow.asm
1793 lines (1349 loc) · 56.7 KB
/
JitHelpers_Slow.asm
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: JitHelpers_Slow.asm, see history in jithelp.asm
;
; Notes: These are ASM routinues which we believe to be cold in normal
; AMD64 scenarios, mainly because they have other versions which
; have some more performant nature which will be used in the best
; cases.
; ***********************************************************************
include AsmMacros.inc
include asmconstants.inc
; Min amount of stack space that a nested function should allocate.
MIN_SIZE equ 28h
EXTERN g_ephemeral_low:QWORD
EXTERN g_ephemeral_high:QWORD
EXTERN g_lowest_address:QWORD
EXTERN g_highest_address:QWORD
EXTERN g_card_table:QWORD
ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
EXTERN g_sw_ww_table:QWORD
EXTERN g_sw_ww_enabled_for_gc_heap:BYTE
endif
ifdef WRITE_BARRIER_CHECK
; Those global variables are always defined, but should be 0 for Server GC
g_GCShadow TEXTEQU <?g_GCShadow@@3PEAEEA>
g_GCShadowEnd TEXTEQU <?g_GCShadowEnd@@3PEAEEA>
EXTERN g_GCShadow:QWORD
EXTERN g_GCShadowEnd:QWORD
endif
JIT_NEW equ ?JIT_New@@YAPEAVObject@@PEAUCORINFO_CLASS_STRUCT_@@@Z
Object__DEBUG_SetAppDomain equ ?DEBUG_SetAppDomain@Object@@QEAAXPEAVAppDomain@@@Z
CopyValueClassUnchecked equ ?CopyValueClassUnchecked@@YAXPEAX0PEAVMethodTable@@@Z
JIT_Box equ ?JIT_Box@@YAPEAVObject@@PEAUCORINFO_CLASS_STRUCT_@@PEAX@Z
g_pStringClass equ ?g_pStringClass@@3PEAVMethodTable@@EA
FramedAllocateString equ ?FramedAllocateString@@YAPEAVStringObject@@K@Z
JIT_NewArr1 equ ?JIT_NewArr1@@YAPEAVObject@@PEAUCORINFO_CLASS_STRUCT_@@_J@Z
INVALIDGCVALUE equ 0CCCCCCCDh
extern JIT_NEW:proc
extern CopyValueClassUnchecked:proc
extern JIT_Box:proc
extern g_pStringClass:QWORD
extern FramedAllocateString:proc
extern JIT_NewArr1:proc
extern JIT_GetSharedNonGCStaticBase_Helper:proc
extern JIT_GetSharedGCStaticBase_Helper:proc
extern JIT_InternalThrow:proc
ifdef _DEBUG
; Version for when we're sure to be in the GC, checks whether or not the card
; needs to be updated
;
; void JIT_WriteBarrier_Debug(Object** dst, Object* src)
LEAF_ENTRY JIT_WriteBarrier_Debug, _TEXT
ifdef WRITE_BARRIER_CHECK
; **ALSO update the shadow GC heap if that is enabled**
; Do not perform the work if g_GCShadow is 0
cmp g_GCShadow, 0
je NoShadow
; If we end up outside of the heap don't corrupt random memory
mov r10, rcx
sub r10, [g_lowest_address]
jb NoShadow
; Check that our adjusted destination is somewhere in the shadow gc
add r10, [g_GCShadow]
cmp r10, [g_GCShadowEnd]
ja NoShadow
; Write ref into real GC; see comment below about possibility of AV
mov [rcx], rdx
; Write ref into shadow GC
mov [r10], rdx
; Ensure that the write to the shadow heap occurs before the read from
; the GC heap so that race conditions are caught by INVALIDGCVALUE
mfence
; Check that GC/ShadowGC values match
mov r11, [rcx]
mov rax, [r10]
cmp rax, r11
je DoneShadow
mov r11, INVALIDGCVALUE
mov [r10], r11
jmp DoneShadow
; If we don't have a shadow GC we won't have done the write yet
NoShadow:
endif
mov rax, rdx
; Do the move. It is correct to possibly take an AV here, the EH code
; figures out that this came from a WriteBarrier and correctly maps it back
; to the managed method which called the WriteBarrier (see setup in
; InitializeExceptionHandling, vm\exceptionhandling.cpp).
mov [rcx], rax
ifdef WRITE_BARRIER_CHECK
; If we had a shadow GC then we already wrote to the real GC at the same time
; as the shadow GC so we want to jump over the real write immediately above
DoneShadow:
endif
ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
; Update the write watch table if necessary
cmp byte ptr [g_sw_ww_enabled_for_gc_heap], 0h
je CheckCardTable
mov r10, rcx
shr r10, 0Ch ; SoftwareWriteWatch::AddressToTableByteIndexShift
add r10, qword ptr [g_sw_ww_table]
cmp byte ptr [r10], 0h
jne CheckCardTable
mov byte ptr [r10], 0FFh
endif
CheckCardTable:
; See if we can just quick out
cmp rax, [g_ephemeral_low]
jb Exit
cmp rax, [g_ephemeral_high]
jnb Exit
; Check if we need to update the card table
; Calc pCardByte
shr rcx, 0Bh
add rcx, [g_card_table]
; Check if this card is dirty
cmp byte ptr [rcx], 0FFh
jne UpdateCardTable
REPRET
UpdateCardTable:
mov byte ptr [rcx], 0FFh
ret
align 16
Exit:
REPRET
LEAF_END_MARKED JIT_WriteBarrier_Debug, _TEXT
endif
NESTED_ENTRY JIT_TrialAllocSFastMP, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
CALL_GETTHREAD
mov r11, rax
mov r8d, [rcx + OFFSET__MethodTable__m_BaseSize]
; m_BaseSize is guaranteed to be a multiple of 8.
mov r10, [r11 + OFFSET__Thread__m_alloc_context__alloc_limit]
mov rax, [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr]
add r8, rax
cmp r8, r10
ja AllocFailed
mov [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr], r8
mov [rax], rcx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain
endif ; _DEBUG
; epilog
add rsp, MIN_SIZE
ret
AllocFailed:
add rsp, MIN_SIZE
jmp JIT_NEW
NESTED_END JIT_TrialAllocSFastMP, _TEXT
; HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData)
NESTED_ENTRY JIT_BoxFastMP, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
mov rax, [rcx + OFFSETOF__MethodTable__m_pWriteableData]
; Check whether the class has not been initialized
test dword ptr [rax + OFFSETOF__MethodTableWriteableData__m_dwFlags], MethodTableWriteableData__enum_flag_Unrestored
jnz ClassNotInited
CALL_GETTHREAD
mov r11, rax
mov r8d, [rcx + OFFSET__MethodTable__m_BaseSize]
; m_BaseSize is guaranteed to be a multiple of 8.
mov r10, [r11 + OFFSET__Thread__m_alloc_context__alloc_limit]
mov rax, [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr]
add r8, rax
cmp r8, r10
ja AllocFailed
mov [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr], r8
mov [rax], rcx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain
endif ; _DEBUG
; Check whether the object contains pointers
test dword ptr [rcx + OFFSETOF__MethodTable__m_dwFlags], MethodTable__enum_flag_ContainsPointers
jnz ContainsPointers
; We have no pointers - emit a simple inline copy loop
mov ecx, [rcx + OFFSET__MethodTable__m_BaseSize]
sub ecx, 18h ; sizeof(ObjHeader) + sizeof(Object) + last slot
CopyLoop:
mov r8, [rdx+rcx]
mov [rax+rcx+8], r8
sub ecx, 8
jge CopyLoop
add rsp, MIN_SIZE
ret
ContainsPointers:
; Do call to CopyValueClassUnchecked(object, data, pMT)
mov [rsp+20h], rax
mov r8, rcx
lea rcx, [rax + 8]
call CopyValueClassUnchecked
mov rax, [rsp+20h]
add rsp, MIN_SIZE
ret
ClassNotInited:
AllocFailed:
add rsp, MIN_SIZE
jmp JIT_Box
NESTED_END JIT_BoxFastMP, _TEXT
NESTED_ENTRY AllocateStringFastMP, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; Instead of doing elaborate overflow checks, we just limit the number of elements
; to (LARGE_OBJECT_SIZE - 256)/sizeof(WCHAR) or less.
; This will avoid all overflow problems, as well as making sure
; big string objects are correctly allocated in the big object heap.
cmp ecx, (ASM_LARGE_OBJECT_SIZE - 256)/2
jae OversizedString
CALL_GETTHREAD
mov r11, rax
mov rdx, [g_pStringClass]
mov r8d, [rdx + OFFSET__MethodTable__m_BaseSize]
; Calculate the final size to allocate.
; We need to calculate baseSize + cnt*2, then round that up by adding 7 and anding ~7.
lea r8d, [r8d + ecx*2 + 7]
and r8d, -8
mov r10, [r11 + OFFSET__Thread__m_alloc_context__alloc_limit]
mov rax, [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr]
add r8, rax
cmp r8, r10
ja AllocFailed
mov [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr], r8
mov [rax], rdx
mov [rax + OFFSETOF__StringObject__m_StringLength], ecx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain
endif ; _DEBUG
add rsp, MIN_SIZE
ret
OversizedString:
AllocFailed:
add rsp, MIN_SIZE
jmp FramedAllocateString
NESTED_END AllocateStringFastMP, _TEXT
; HCIMPL2(Object*, JIT_NewArr1VC_MP, CORINFO_CLASS_HANDLE arrayMT, INT_PTR size)
NESTED_ENTRY JIT_NewArr1VC_MP, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; We were passed a (shared) method table in RCX, which contains the element type.
; The element count is in RDX
; NOTE: if this code is ported for CORINFO_HELP_NEWSFAST_ALIGN8, it will need
; to emulate the double-specific behavior of JIT_TrialAlloc::GenAllocArray.
; Do a conservative check here. This is to avoid overflow while doing the calculations. We don't
; have to worry about "large" objects, since the allocation quantum is never big enough for
; LARGE_OBJECT_SIZE.
; For Value Classes, this needs to be 2^16 - slack (2^32 / max component size),
; The slack includes the size for the array header and round-up ; for alignment. Use 256 for the
; slack value out of laziness.
; In both cases we do a final overflow check after adding to the alloc_ptr.
CALL_GETTHREAD
mov r11, rax
cmp rdx, (65535 - 256)
jae OversizedArray
movzx r8d, word ptr [rcx + OFFSETOF__MethodTable__m_dwFlags] ; component size is low 16 bits
imul r8d, edx ; signed mul, but won't overflow due to length restriction above
add r8d, dword ptr [rcx + OFFSET__MethodTable__m_BaseSize]
; round the size to a multiple of 8
add r8d, 7
and r8d, -8
mov r10, [r11 + OFFSET__Thread__m_alloc_context__alloc_limit]
mov rax, [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr]
add r8, rax
jc AllocFailed
cmp r8, r10
ja AllocFailed
mov [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr], r8
mov [rax], rcx
mov dword ptr [rax + OFFSETOF__ArrayBase__m_NumComponents], edx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain
endif ; _DEBUG
add rsp, MIN_SIZE
ret
OversizedArray:
AllocFailed:
add rsp, MIN_SIZE
jmp JIT_NewArr1
NESTED_END JIT_NewArr1VC_MP, _TEXT
; HCIMPL2(Object*, JIT_NewArr1OBJ_MP, CORINFO_CLASS_HANDLE arrayMT, INT_PTR size)
NESTED_ENTRY JIT_NewArr1OBJ_MP, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; We were passed a (shared) method table in RCX, which contains the element type.
; The element count is in RDX
; NOTE: if this code is ported for CORINFO_HELP_NEWSFAST_ALIGN8, it will need
; to emulate the double-specific behavior of JIT_TrialAlloc::GenAllocArray.
; Verifies that LARGE_OBJECT_SIZE fits in 32-bit. This allows us to do array size
; arithmetic using 32-bit registers.
.erre ASM_LARGE_OBJECT_SIZE lt 100000000h
cmp rdx, (ASM_LARGE_OBJECT_SIZE - 256)/8
jae OversizedArray
CALL_GETTHREAD
mov r11, rax
; In this case we know the element size is sizeof(void *), or 8 for x64
; This helps us in two ways - we can shift instead of multiplying, and
; there's no need to align the size either
mov r8d, dword ptr [rcx + OFFSET__MethodTable__m_BaseSize]
lea r8d, [r8d + edx * 8]
; No need for rounding in this case - element size is 8, and m_BaseSize is guaranteed
; to be a multiple of 8.
mov r10, [r11 + OFFSET__Thread__m_alloc_context__alloc_limit]
mov rax, [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr]
add r8, rax
cmp r8, r10
ja AllocFailed
mov [r11 + OFFSET__Thread__m_alloc_context__alloc_ptr], r8
mov [rax], rcx
mov dword ptr [rax + OFFSETOF__ArrayBase__m_NumComponents], edx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain
endif ; _DEBUG
add rsp, MIN_SIZE
ret
OversizedArray:
AllocFailed:
add rsp, MIN_SIZE
jmp JIT_NewArr1
NESTED_END JIT_NewArr1OBJ_MP, _TEXT
extern g_global_alloc_lock:dword
extern g_global_alloc_context:qword
LEAF_ENTRY JIT_TrialAllocSFastSP, _TEXT
mov r8d, [rcx + OFFSET__MethodTable__m_BaseSize]
; m_BaseSize is guaranteed to be a multiple of 8.
inc [g_global_alloc_lock]
jnz JIT_NEW
mov rax, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr] ; alloc_ptr
mov r10, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_limit] ; limit_ptr
add r8, rax
cmp r8, r10
ja AllocFailed
mov qword ptr [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr], r8 ; update the alloc ptr
mov [rax], rcx
mov [g_global_alloc_lock], -1
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain_NoScratchArea
endif ; _DEBUG
ret
AllocFailed:
mov [g_global_alloc_lock], -1
jmp JIT_NEW
LEAF_END JIT_TrialAllocSFastSP, _TEXT
; HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData)
NESTED_ENTRY JIT_BoxFastUP, _TEXT
mov rax, [rcx + OFFSETOF__MethodTable__m_pWriteableData]
; Check whether the class has not been initialized
test dword ptr [rax + OFFSETOF__MethodTableWriteableData__m_dwFlags], MethodTableWriteableData__enum_flag_Unrestored
jnz JIT_Box
mov r8d, [rcx + OFFSET__MethodTable__m_BaseSize]
; m_BaseSize is guaranteed to be a multiple of 8.
inc [g_global_alloc_lock]
jnz JIT_Box
mov rax, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr] ; alloc_ptr
mov r10, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_limit] ; limit_ptr
add r8, rax
cmp r8, r10
ja NoAlloc
mov qword ptr [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr], r8 ; update the alloc ptr
mov [rax], rcx
mov [g_global_alloc_lock], -1
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain_NoScratchArea
endif ; _DEBUG
; Check whether the object contains pointers
test dword ptr [rcx + OFFSETOF__MethodTable__m_dwFlags], MethodTable__enum_flag_ContainsPointers
jnz ContainsPointers
; We have no pointers - emit a simple inline copy loop
mov ecx, [rcx + OFFSET__MethodTable__m_BaseSize]
sub ecx, 18h ; sizeof(ObjHeader) + sizeof(Object) + last slot
CopyLoop:
mov r8, [rdx+rcx]
mov [rax+rcx+8], r8
sub ecx, 8
jge CopyLoop
REPRET
ContainsPointers:
; Do call to CopyValueClassUnchecked(object, data, pMT)
push_vol_reg rax
alloc_stack 20h
END_PROLOGUE
mov r8, rcx
lea rcx, [rax + 8]
call CopyValueClassUnchecked
add rsp, 20h
pop rax
ret
NoAlloc:
mov [g_global_alloc_lock], -1
jmp JIT_Box
NESTED_END JIT_BoxFastUP, _TEXT
LEAF_ENTRY AllocateStringFastUP, _TEXT
; We were passed the number of characters in ECX
; we need to load the method table for string from the global
mov r11, [g_pStringClass]
; Instead of doing elaborate overflow checks, we just limit the number of elements
; to (LARGE_OBJECT_SIZE - 256)/sizeof(WCHAR) or less.
; This will avoid all overflow problems, as well as making sure
; big string objects are correctly allocated in the big object heap.
cmp ecx, (ASM_LARGE_OBJECT_SIZE - 256)/2
jae FramedAllocateString
mov r8d, [r11 + OFFSET__MethodTable__m_BaseSize]
; Calculate the final size to allocate.
; We need to calculate baseSize + cnt*2, then round that up by adding 7 and anding ~7.
lea r8d, [r8d + ecx*2 + 7]
and r8d, -8
inc [g_global_alloc_lock]
jnz FramedAllocateString
mov rax, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr] ; alloc_ptr
mov r10, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_limit] ; limit_ptr
add r8, rax
cmp r8, r10
ja AllocFailed
mov qword ptr [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr], r8 ; update the alloc ptr
mov [rax], r11
mov [g_global_alloc_lock], -1
mov [rax + OFFSETOF__StringObject__m_StringLength], ecx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain_NoScratchArea
endif ; _DEBUG
ret
AllocFailed:
mov [g_global_alloc_lock], -1
jmp FramedAllocateString
LEAF_END AllocateStringFastUP, _TEXT
; HCIMPL2(Object*, JIT_NewArr1VC_UP, CORINFO_CLASS_HANDLE arrayMT, INT_PTR size)
LEAF_ENTRY JIT_NewArr1VC_UP, _TEXT
; We were passed a (shared) method table in RCX, which contains the element type.
; The element count is in RDX
; NOTE: if this code is ported for CORINFO_HELP_NEWSFAST_ALIGN8, it will need
; to emulate the double-specific behavior of JIT_TrialAlloc::GenAllocArray.
; Do a conservative check here. This is to avoid overflow while doing the calculations. We don't
; have to worry about "large" objects, since the allocation quantum is never big enough for
; LARGE_OBJECT_SIZE.
; For Value Classes, this needs to be 2^16 - slack (2^32 / max component size),
; The slack includes the size for the array header and round-up ; for alignment. Use 256 for the
; slack value out of laziness.
; In both cases we do a final overflow check after adding to the alloc_ptr.
cmp rdx, (65535 - 256)
jae JIT_NewArr1
movzx r8d, word ptr [rcx + OFFSETOF__MethodTable__m_dwFlags] ; component size is low 16 bits
imul r8d, edx ; signed mul, but won't overflow due to length restriction above
add r8d, dword ptr [rcx + OFFSET__MethodTable__m_BaseSize]
; round the size to a multiple of 8
add r8d, 7
and r8d, -8
inc [g_global_alloc_lock]
jnz JIT_NewArr1
mov rax, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr] ; alloc_ptr
mov r10, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_limit] ; limit_ptr
add r8, rax
jc AllocFailed
cmp r8, r10
ja AllocFailed
mov qword ptr [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr], r8 ; update the alloc ptr
mov [rax], rcx
mov [g_global_alloc_lock], -1
mov dword ptr [rax + OFFSETOF__ArrayBase__m_NumComponents], edx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain_NoScratchArea
endif ; _DEBUG
ret
AllocFailed:
mov [g_global_alloc_lock], -1
jmp JIT_NewArr1
LEAF_END JIT_NewArr1VC_UP, _TEXT
; HCIMPL2(Object*, JIT_NewArr1OBJ_UP, CORINFO_CLASS_HANDLE arrayMT, INT_PTR size)
LEAF_ENTRY JIT_NewArr1OBJ_UP, _TEXT
; We were passed a (shared) method table in RCX, which contains the element type.
; The element count is in RDX
; NOTE: if this code is ported for CORINFO_HELP_NEWSFAST_ALIGN8, it will need
; to emulate the double-specific behavior of JIT_TrialAlloc::GenAllocArray.
; Verifies that LARGE_OBJECT_SIZE fits in 32-bit. This allows us to do array size
; arithmetic using 32-bit registers.
.erre ASM_LARGE_OBJECT_SIZE lt 100000000h
cmp rdx, (ASM_LARGE_OBJECT_SIZE - 256)/8 ; sizeof(void*)
jae OversizedArray
; In this case we know the element size is sizeof(void *), or 8 for x64
; This helps us in two ways - we can shift instead of multiplying, and
; there's no need to align the size either
mov r8d, dword ptr [rcx + OFFSET__MethodTable__m_BaseSize]
lea r8d, [r8d + edx * 8]
; No need for rounding in this case - element size is 8, and m_BaseSize is guaranteed
; to be a multiple of 8.
inc [g_global_alloc_lock]
jnz JIT_NewArr1
mov rax, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr] ; alloc_ptr
mov r10, [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_limit] ; limit_ptr
add r8, rax
cmp r8, r10
ja AllocFailed
mov qword ptr [g_global_alloc_context + OFFSETOF__gc_alloc_context__alloc_ptr], r8 ; update the alloc ptr
mov [rax], rcx
mov [g_global_alloc_lock], -1
mov dword ptr [rax + OFFSETOF__ArrayBase__m_NumComponents], edx
ifdef _DEBUG
call DEBUG_TrialAllocSetAppDomain_NoScratchArea
endif ; _DEBUG
ret
AllocFailed:
mov [g_global_alloc_lock], -1
OversizedArray:
jmp JIT_NewArr1
LEAF_END JIT_NewArr1OBJ_UP, _TEXT
NESTED_ENTRY JIT_GetSharedNonGCStaticBase_Slow, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; Check if rcx (moduleDomainID) is not a moduleID
test rcx, 1
jz HaveLocalModule
CALL_GETAPPDOMAIN
; Get the LocalModule
mov rax, [rax + OFFSETOF__AppDomain__m_sDomainLocalBlock + OFFSETOF__DomainLocalBlock__m_pModuleSlots]
; rcx will always be odd, so: rcx * 4 - 4 <=> (rcx >> 1) * 8
mov rcx, [rax + rcx * 4 - 4]
HaveLocalModule:
; If class is not initialized, bail to C++ helper
test [rcx + OFFSETOF__DomainLocalModule__m_pDataBlob + rdx], 1
jz CallHelper
mov rax, rcx
add rsp, MIN_SIZE
ret
align 16
CallHelper:
; Tail call Jit_GetSharedNonGCStaticBase_Helper
add rsp, MIN_SIZE
jmp JIT_GetSharedNonGCStaticBase_Helper
NESTED_END JIT_GetSharedNonGCStaticBase_Slow, _TEXT
NESTED_ENTRY JIT_GetSharedNonGCStaticBaseNoCtor_Slow, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; Check if rcx (moduleDomainID) is not a moduleID
test rcx, 1
jz HaveLocalModule
CALL_GETAPPDOMAIN
; Get the LocalModule
mov rax, [rax + OFFSETOF__AppDomain__m_sDomainLocalBlock + OFFSETOF__DomainLocalBlock__m_pModuleSlots]
; rcx will always be odd, so: rcx * 4 - 4 <=> (rcx >> 1) * 8
mov rax, [rax + rcx * 4 - 4]
add rsp, MIN_SIZE
ret
align 16
HaveLocalModule:
mov rax, rcx
add rsp, MIN_SIZE
ret
NESTED_END JIT_GetSharedNonGCStaticBaseNoCtor_Slow, _TEXT
NESTED_ENTRY JIT_GetSharedGCStaticBase_Slow, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; Check if rcx (moduleDomainID) is not a moduleID
test rcx, 1
jz HaveLocalModule
CALL_GETAPPDOMAIN
; Get the LocalModule
mov rax, [rax + OFFSETOF__AppDomain__m_sDomainLocalBlock + OFFSETOF__DomainLocalBlock__m_pModuleSlots]
; rcx will always be odd, so: rcx * 4 - 4 <=> (rcx >> 1) * 8
mov rcx, [rax + rcx * 4 - 4]
HaveLocalModule:
; If class is not initialized, bail to C++ helper
test [rcx + OFFSETOF__DomainLocalModule__m_pDataBlob + rdx], 1
jz CallHelper
mov rax, [rcx + OFFSETOF__DomainLocalModule__m_pGCStatics]
add rsp, MIN_SIZE
ret
align 16
CallHelper:
; Tail call Jit_GetSharedGCStaticBase_Helper
add rsp, MIN_SIZE
jmp JIT_GetSharedGCStaticBase_Helper
NESTED_END JIT_GetSharedGCStaticBase_Slow, _TEXT
NESTED_ENTRY JIT_GetSharedGCStaticBaseNoCtor_Slow, _TEXT
alloc_stack MIN_SIZE
END_PROLOGUE
; Check if rcx (moduleDomainID) is not a moduleID
test rcx, 1
jz HaveLocalModule
CALL_GETAPPDOMAIN
; Get the LocalModule
mov rax, [rax + OFFSETOF__AppDomain__m_sDomainLocalBlock + OFFSETOF__DomainLocalBlock__m_pModuleSlots]
; rcx will always be odd, so: rcx * 4 - 4 <=> (rcx >> 1) * 8
mov rcx, [rax + rcx * 4 - 4]
HaveLocalModule:
mov rax, [rcx + OFFSETOF__DomainLocalModule__m_pGCStatics]
add rsp, MIN_SIZE
ret
NESTED_END JIT_GetSharedGCStaticBaseNoCtor_Slow, _TEXT
MON_ENTER_STACK_SIZE equ 00000020h
MON_EXIT_STACK_SIZE equ 00000068h
ifdef MON_DEBUG
ifdef TRACK_SYNC
MON_ENTER_STACK_SIZE_INLINEGETTHREAD equ 00000020h
MON_EXIT_STACK_SIZE_INLINEGETTHREAD equ 00000068h
endif
endif
BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX equ 08000000h ; syncblk.h
BIT_SBLK_IS_HASHCODE equ 04000000h ; syncblk.h
BIT_SBLK_SPIN_LOCK equ 10000000h ; syncblk.h
SBLK_MASK_LOCK_THREADID equ 000003FFh ; syncblk.h
SBLK_LOCK_RECLEVEL_INC equ 00000400h ; syncblk.h
SBLK_MASK_LOCK_RECLEVEL equ 0000FC00h ; syncblk.h
MASK_SYNCBLOCKINDEX equ 03FFFFFFh ; syncblk.h
STATE_CHECK equ 0FFFFFFFEh
MT_CTX_PROXY_FLAG equ 10000000h
g_pSyncTable equ ?g_pSyncTable@@3PEAVSyncTableEntry@@EA
g_SystemInfo equ ?g_SystemInfo@@3U_SYSTEM_INFO@@A
g_SpinConstants equ ?g_SpinConstants@@3USpinConstants@@A
extern g_pSyncTable:QWORD
extern g_SystemInfo:QWORD
extern g_SpinConstants:QWORD
; JITutil_MonEnterWorker(Object* obj, BYTE* pbLockTaken)
extern JITutil_MonEnterWorker:proc
; JITutil_MonTryEnter(Object* obj, INT32 timeout, BYTE* pbLockTaken)
extern JITutil_MonTryEnter:proc
; JITutil_MonExitWorker(Object* obj, BYTE* pbLockTaken)
extern JITutil_MonExitWorker:proc
; JITutil_MonSignal(AwareLock* lock, BYTE* pbLockTaken)
extern JITutil_MonSignal:proc
; JITutil_MonContention(AwareLock* lock, BYTE* pbLockTaken)
extern JITutil_MonContention:proc
ifdef _DEBUG
MON_DEBUG equ 1
endif
ifdef MON_DEBUG
ifdef TRACK_SYNC
extern EnterSyncHelper:proc
extern LeaveSyncHelper:proc
endif
endif
; This is a frameless helper for entering a monitor on a object.
; The object is in ARGUMENT_REG1. This tries the normal case (no
; blocking or object allocation) in line and calls a framed helper
; for the other cases.
;
; EXTERN_C void JIT_MonEnterWorker_Slow(Object* obj, /*OUT*/ BYTE* pbLockTaken)
NESTED_ENTRY JIT_MonEnterWorker_Slow, _TEXT
push_nonvol_reg rsi
alloc_stack MON_ENTER_STACK_SIZE
save_reg_postrsp rcx, MON_ENTER_STACK_SIZE + 10h + 0h
save_reg_postrsp rdx, MON_ENTER_STACK_SIZE + 10h + 8h
save_reg_postrsp r8, MON_ENTER_STACK_SIZE + 10h + 10h
save_reg_postrsp r9, MON_ENTER_STACK_SIZE + 10h + 18h
END_PROLOGUE
; Check if the instance is NULL
test rcx, rcx
jz FramedLockHelper
; Put pbLockTaken in rsi, this can be null
mov rsi, rdx
; We store the thread object in r11
CALL_GETTHREAD
mov r11, rax
; Initialize delay value for retry with exponential backoff
mov r10d, dword ptr [g_SpinConstants + OFFSETOF__g_SpinConstants__dwInitialDuration]
; Check if we can abort here
mov eax, dword ptr [r11 + OFFSETOF__Thread__m_State]
and eax, THREAD_CATCHATSAFEPOINT_BITS
; Go through the slow code path to initiate ThreadAbort
jnz FramedLockHelper
; r8 will hold the syncblockindex address
lea r8, [rcx - OFFSETOF__ObjHeader__SyncBlkIndex]
RetryThinLock:
; Fetch the syncblock dword
mov eax, dword ptr [r8]
; Check whether we have the "thin lock" layout, the lock is free and the spin lock bit is not set
test eax, BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX + BIT_SBLK_SPIN_LOCK + SBLK_MASK_LOCK_THREADID + SBLK_MASK_LOCK_RECLEVEL
jnz NeedMoreTests
; Everything is fine - get the thread id to store in the lock
mov edx, dword ptr [r11 + OFFSETOF__Thread__m_ThreadId]
; If the thread id is too large, we need a syncblock for sure
cmp edx, SBLK_MASK_LOCK_THREADID
ja FramedLockHelper
; We want to store a new value with the current thread id set in the low 10 bits
or edx, eax
lock cmpxchg dword ptr [r8], edx
jnz PrepareToWaitThinLock
; Everything went fine and we're done
add dword ptr [r11 + OFFSETOF__Thread__m_dwLockCount], 1
; Done, leave and set pbLockTaken if we have it
jmp LockTaken
NeedMoreTests:
; OK, not the simple case, find out which case it is
test eax, BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX
jnz HaveHashOrSyncBlockIndex
; The header is transitioning or the lock, treat this as if the lock was taken
test eax, BIT_SBLK_SPIN_LOCK
jnz PrepareToWaitThinLock
; Here we know we have the "thin lock" layout, but the lock is not free.
; It could still be the recursion case, compare the thread id to check
mov edx, eax
and edx, SBLK_MASK_LOCK_THREADID
cmp edx, dword ptr [r11 + OFFSETOF__Thread__m_ThreadId]
jne PrepareToWaitThinLock
; Ok, the thread id matches, it's the recursion case.
; Bump up the recursion level and check for overflow
lea edx, [eax + SBLK_LOCK_RECLEVEL_INC]
test edx, SBLK_MASK_LOCK_RECLEVEL
jz FramedLockHelper
; Try to put the new recursion level back. If the header was changed in the meantime
; we need a full retry, because the layout could have changed
lock cmpxchg dword ptr [r8], edx
jnz RetryHelperThinLock
; Done, leave and set pbLockTaken if we have it
jmp LockTaken
PrepareToWaitThinLock:
; If we are on an MP system, we try spinning for a certain number of iterations
cmp dword ptr [g_SystemInfo + OFFSETOF__g_SystemInfo__dwNumberOfProcessors], 1
jle FramedLockHelper
; Exponential backoff; delay by approximately 2*r10 clock cycles
mov eax, r10d
delayLoopThinLock:
pause ; indicate to the CPU that we are spin waiting
sub eax, 1
jnz delayLoopThinLock