Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFadi Hanna <fadim@microsoft.com>2016-10-19 02:39:06 +0300
committerFadi Hanna <fadim@microsoft.com>2016-10-19 02:39:06 +0300
commitdd535f433564a5734bc6c931a4c7a8bb834987d2 (patch)
tree5d572860134ce7975e8400a202b31d396037e787 /src/Native/Runtime/i386
parent135400013170daf61928f1028a6e2250a208670f (diff)
Refactoring for the ThunkPool APIs:
1) Removing the ThunkPool APIs from the System.Private.Corelib layer and moving it to mrt100_app.dll. Reasons: a) A more correct layering b) Thunks are also used by mrt100_app, and today it has to call into the S.P.Corelib layer to get thunks c) The binder (which today takes care of laying out the thunks section) is going away with the ProjectX work, so the refactoring is needed 2) Addition of a new API: CreateThunksHeap. Thunks are allocated and deallocated from heaps, and each caller can create and managed their own heaps (TODO: heap deallocation feature) Note: The use of a simple heap removes the need for a the hashtable that we previously had in the old implementation, and simplifies the implementation a bit. 4) Optimization (use of simple thunks linked list with better perf characteristics) 5) Added ThunkPool APIs into the internal RuntimeAugments assembly, to enable writing of tests that directly target the thunks APIs 6) Tests for the feature (including a multithreaded test). Tested on all 3 architectures. [tfs-changeset: 1634032]
Diffstat (limited to 'src/Native/Runtime/i386')
-rw-r--r--src/Native/Runtime/i386/ThunkPoolThunks.asm268
1 files changed, 268 insertions, 0 deletions
diff --git a/src/Native/Runtime/i386/ThunkPoolThunks.asm b/src/Native/Runtime/i386/ThunkPoolThunks.asm
new file mode 100644
index 000000000..a3f180146
--- /dev/null
+++ b/src/Native/Runtime/i386/ThunkPoolThunks.asm
@@ -0,0 +1,268 @@
+;; ==++==
+;;
+;; Copyright (c) Microsoft Corporation. All rights reserved.
+;;
+;; ==--==
+
+.586
+.model flat
+option casemap:none
+.code
+
+;; -----------------------------------------------------------------------------------------------------------
+;; standard macros
+;; -----------------------------------------------------------------------------------------------------------
+LEAF_ENTRY macro Name, Section
+ Section segment para 'CODE'
+ public Name
+ Name proc
+endm
+
+NAMED_LEAF_ENTRY macro Name, Section, SectionAlias
+ Section segment para alias(SectionAlias) 'CODE'
+ public Name
+ Name proc
+endm
+
+LEAF_END macro Name, Section
+ Name endp
+ Section ends
+endm
+
+NAMED_READONLY_DATA_SECTION macro Section, SectionAlias
+ Section segment para alias(SectionAlias) read 'DATA'
+ DD 0
+ Section ends
+endm
+
+NAMED_READWRITE_DATA_SECTION macro Section, SectionAlias
+ Section segment para alias(SectionAlias) read write 'DATA'
+ DD 0
+ Section ends
+endm
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; STUBS & DATA SECTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+THUNK_CODESIZE equ 14h ;; 5-byte call, 1 byte pop, 6-byte lea, 6-byte jmp, 2 bytes of nop
+THUNK_DATASIZE equ 08h ;; 2 dwords
+
+THUNK_POOL_NUM_THUNKS_PER_PAGE equ 0C8h ;; 200 thunks per page
+
+PAGE_SIZE equ 01000h ;; 4K
+POINTER_SIZE equ 04h
+
+
+GET_CURRENT_IP macro
+ call @F
+ @@: pop eax
+endm
+
+LOAD_DATA_ADDRESS macro groupIndex, index
+ ;; start : eax points to current instruction of the current thunk
+ ;; set eax to begining of data page : eax <- [eax - (size of the call instruction + (THUNK_CODESIZE * current thunk's index)) + PAGE_SIZE]
+ ;; fix offset of the data : eax <- eax + (THUNK_DATASIZE * current thunk's index)
+ lea eax,[eax - (5 + groupIndex * THUNK_CODESIZE * 10 + THUNK_CODESIZE * index) + PAGE_SIZE + (groupIndex * THUNK_DATASIZE * 10 + THUNK_DATASIZE * index)]
+endm
+
+JUMP_TO_COMMON macro groupIndex, index
+ ;; start : eax points to current thunk's data block
+ ;; re-point eax to begining of data page : eax <- [eax - (THUNK_DATASIZE * current thunk's index)]
+ ;; jump to the location pointed at by the last dword in the data page : jump [eax + PAGE_SIZE - POINTER_SIZE]
+ jmp dword ptr[eax - (groupIndex * THUNK_DATASIZE * 10 + THUNK_DATASIZE * index) + PAGE_SIZE - POINTER_SIZE]
+ ;; pad out to 4 byte aligned
+ nop
+ nop
+endm
+
+TenThunks macro groupIndex
+ ;; Each thunk will load the address of its corresponding data (from the page that immediately follows)
+ ;; and call a common stub. The address of the common stub is setup by the caller (last dword
+ ;; in the thunks data section) depending on the 'kind' of thunks needed (interop, fat function pointers, etc...)
+
+ ;; Each data block used by a thunk consists of two dword values:
+ ;; - Context: some value given to the thunk as context (passed in eax). Example for fat-fptrs: context = generic dictionary
+ ;; - Target : target code that the thunk eventually jumps to.
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,0
+ JUMP_TO_COMMON groupIndex,0
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,1
+ JUMP_TO_COMMON groupIndex,1
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,2
+ JUMP_TO_COMMON groupIndex,2
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,3
+ JUMP_TO_COMMON groupIndex,3
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,4
+ JUMP_TO_COMMON groupIndex,4
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,5
+ JUMP_TO_COMMON groupIndex,5
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,6
+ JUMP_TO_COMMON groupIndex,6
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,7
+ JUMP_TO_COMMON groupIndex,7
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,8
+ JUMP_TO_COMMON groupIndex,8
+
+ GET_CURRENT_IP
+ LOAD_DATA_ADDRESS groupIndex,9
+ JUMP_TO_COMMON groupIndex,9
+endm
+
+THUNKS_PAGE_BLOCK macro
+ TenThunks 0
+ TenThunks 1
+ TenThunks 2
+ TenThunks 3
+ TenThunks 4
+ TenThunks 5
+ TenThunks 6
+ TenThunks 7
+ TenThunks 8
+ TenThunks 9
+ TenThunks 10
+ TenThunks 11
+ TenThunks 12
+ TenThunks 13
+ TenThunks 14
+ TenThunks 15
+ TenThunks 16
+ TenThunks 17
+ TenThunks 18
+ TenThunks 19
+endm
+
+;;
+;; The first thunks section should be 64K aligned because it can get
+;; mapped multiple times in memory, and mapping works on allocation
+;; granularity boundaries (we don't want to map more than what we need)
+;;
+;; The easiest way to do so is by having the thunks section at the
+;; first 64K aligned virtual address in the binary. We provide a section
+;; layout file to the linker to tell it how to layout the thunks sections
+;; that we care about. (ndp\rh\src\runtime\DLLs\app\mrt100_app_sectionlayout.txt)
+;;
+;; The PE spec says images cannot have gaps between sections (other
+;; than what is required by the section alignment value in the header),
+;; therefore we need a couple of padding data sections (otherwise the
+;; OS will not load the image).
+;;
+
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment0, ".pad0"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment1, ".pad1"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment2, ".pad2"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment3, ".pad3"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment4, ".pad4"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment5, ".pad5"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment6, ".pad6"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment7, ".pad7"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment8, ".pad8"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment9, ".pad9"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment10, ".pad10"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment11, ".pad11"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment12, ".pad12"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment13, ".pad13"
+NAMED_READONLY_DATA_SECTION PaddingFor64KAlignment14, ".pad14"
+
+;;
+;; Thunk Stubs
+;; NOTE: Keep number of blocks in sync with macro/constant named 'NUM_THUNK_BLOCKS' in:
+;; - ndp\FxCore\src\System.Private.CoreLib\System\Runtime\InteropServices\ThunkPool.cs
+;; - ndp\rh\src\tools\rhbind\zapimage.h
+;;
+NAMED_LEAF_ENTRY ThunkPool, TKS0, ".tks0"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool, TKS0
+
+NAMED_READWRITE_DATA_SECTION ThunkData0, ".tkd0"
+
+NAMED_LEAF_ENTRY ThunkPool1, TKS1, ".tks1"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool1, TKS1
+
+NAMED_READWRITE_DATA_SECTION ThunkData1, ".tkd1"
+
+NAMED_LEAF_ENTRY ThunkPool2, TKS2, ".tks2"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool2, TKS2
+
+NAMED_READWRITE_DATA_SECTION ThunkData2, ".tkd2"
+
+NAMED_LEAF_ENTRY ThunkPool3, TKS3, ".tks3"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool3, TKS3
+
+NAMED_READWRITE_DATA_SECTION ThunkData3, ".tkd3"
+
+NAMED_LEAF_ENTRY ThunkPool4, TKS4, ".tks4"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool4, TKS4
+
+NAMED_READWRITE_DATA_SECTION ThunkData4, ".tkd4"
+
+NAMED_LEAF_ENTRY ThunkPool5, TKS5, ".tks5"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool5, TKS5
+
+NAMED_READWRITE_DATA_SECTION ThunkData5, ".tkd5"
+
+NAMED_LEAF_ENTRY ThunkPool6, TKS6, ".tks6"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool6, TKS6
+
+NAMED_READWRITE_DATA_SECTION ThunkData6, ".tkd6"
+
+NAMED_LEAF_ENTRY ThunkPool7, TKS7, ".tks7"
+ THUNKS_PAGE_BLOCK
+LEAF_END ThunkPool7, TKS7
+
+NAMED_READWRITE_DATA_SECTION ThunkData7, ".tkd7"
+
+
+;;
+;; IntPtr _RhpGetThunksBase()
+;;
+LEAF_ENTRY _RhpGetThunksBase, _TEXT
+ ;; Return the address of the first thunk pool to the caller (this is really the base address)
+ lea eax, [ThunkPool]
+ ret
+LEAF_END _RhpGetThunksBase, _TEXT
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; General Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;
+;; int _RhpGetNumThunksPerBlock()
+;;
+LEAF_ENTRY _RhpGetNumThunksPerBlock, _TEXT
+ mov eax, THUNK_POOL_NUM_THUNKS_PER_PAGE
+ ret
+LEAF_END _RhpGetNumThunksPerBlock, _TEXT
+
+;;
+;; int _RhpGetThunkSize()
+;;
+LEAF_ENTRY _RhpGetThunkSize, _TEXT
+ mov eax, THUNK_CODESIZE
+ ret
+LEAF_END _RhpGetThunkSize, _TEXT
+
+
+end