Skip to content

Commit

Permalink
Port arm64 INLINE_GETTHREAD from NativeAOT (#99648)
Browse files Browse the repository at this point in the history
* port INLINE_GETTHREAD from NativeAOT

* it is actually called `gCurrentThreadInfo`

* unnecessary EXTERN

* formatting
  • Loading branch information
VSadov committed Mar 13, 2024
1 parent 596a1f7 commit df50407
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/coreclr/vm/arm64/PInvokeStubs.asm
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ RarePath

LEAF_END

INLINE_GETTHREAD_CONSTANT_POOL

; ------------------------------------------------------------------
; VarargPInvokeStub & VarargPInvokeGenILStub
;
Expand Down
51 changes: 22 additions & 29 deletions src/coreclr/vm/arm64/asmmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,52 +313,45 @@ __RedirectionFuncName SETS "|?RedirectedHandledJITCaseFor":CC:"$reason":CC:"@Thr

MEND

;-----------------------------------------------------------------------------
; Macro to get a pointer to the Thread* object for the currently executing thread
;
__tls_array equ 0x58 ;; offsetof(TEB, ThreadLocalStoragePointer)
;; -----------------------------------------------------------------------------
;;
;; Macro to get a pointer to a threadlocal symbol for the currently executing thread
;;

EXTERN _tls_index

GBLS __SECTIONREL_gCurrentThreadInfo
__SECTIONREL_gCurrentThreadInfo SETS "SECTIONREL_gCurrentThreadInfo"
__tls_array equ 0x58 ;; offsetof(TEB, ThreadLocalStoragePointer)

MACRO
INLINE_GETTHREAD $destReg, $trashReg
INLINE_GET_TLS_VAR $destReg, $trashReg, $variable

EXTERN _tls_index

;; The following macro variables are just some assembler magic to get the name of the 32-bit version
;; of $trashReg. It does it by string manipulation. Replaces something like x3 with w3.
LCLS TrashRegister32Bit
TrashRegister32Bit SETS "$trashReg"
TrashRegister32Bit SETS "w":CC:("$TrashRegister32Bit":RIGHT:((:LEN:TrashRegister32Bit) - 1))

ldr $trashReg, =_tls_index
ldr $TrashRegister32Bit, [$trashReg]
adrp $destReg, _tls_index
ldr $TrashRegister32Bit, [$destReg, _tls_index]
ldr $destReg, [xpr, #__tls_array]
ldr $destReg, [$destReg, $trashReg lsl #3]
ldr $trashReg, =$__SECTIONREL_gCurrentThreadInfo
ldr $trashReg, [$trashReg]
ldr $destReg, [$destReg, $trashReg] ; return gCurrentThreadInfo.m_pThread
ldr $destReg, [$destReg, $TrashRegister32Bit uxtw #3]
add $destReg, $destReg, #0, lsl #0xC
RELOC 0xA, $variable ;; IMAGE_REL_ARM64_SECREL_HIGH12A
add $destReg, $destReg, #0, lsl #0
RELOC 0x9, $variable ;; IMAGE_REL_ARM64_SECREL_LOW12A
MEND

;-----------------------------------------------------------------------------
; INLINE_GETTHREAD_CONSTANT_POOL macro has to be used after the last function in the .asm file that used
; INLINE_GETTHREAD. Optionally, it can be also used after any function that used INLINE_GETTHREAD
; to improve density, or to reduce distance between the constant pool and its use.
;
;; -----------------------------------------------------------------------------
;;
;; Macro to get a pointer to the Thread* object for the currently executing thread
;;
SETALIAS gCurrentThreadInfo, ?gCurrentThreadInfo@@3UThreadLocalInfo@@A

MACRO
INLINE_GETTHREAD_CONSTANT_POOL
INLINE_GETTHREAD $destReg, $trashReg

EXTERN $gCurrentThreadInfo

;; Section relocs are 32 bits. Using an extra DCD initialized to zero for 8-byte alignment.
$__SECTIONREL_gCurrentThreadInfo
DCD $gCurrentThreadInfo
RELOC 8, $gCurrentThreadInfo ;; SECREL
DCD 0

__SECTIONREL_gCurrentThreadInfo SETS "$__SECTIONREL_gCurrentThreadInfo":CC:"_"

INLINE_GET_TLS_VAR $destReg, $trashReg, $gCurrentThreadInfo
ldr $destReg, [$destReg] ;; return gCurrentThreadInfo.m_pThread
MEND

0 comments on commit df50407

Please sign in to comment.