-
Notifications
You must be signed in to change notification settings - Fork 5k
[NativeAOT] Implement thunk page generation and mapping for iOS-like platforms #82317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
acf6f57
NativeAOT: Implement thunk page generation and mapping for iOS-like p…
filipnavara a1a6193
Use minipal_getexepath instead of libproc
filipnavara 49fb9f7
Reimplement PalAllocateThunksFromTemplate to work inside shared libra…
filipnavara 6c447e3
Specify correct rpath in tests for all Apple platforms
filipnavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
.intel_syntax noprefix | ||
#include <unixasmmacros.inc> | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA SECTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
#define THUNK_CODESIZE 0x10 // 3 instructions, 4 bytes each (and we also have 4 bytes of padding) | ||
#define THUNK_DATASIZE 0x10 // 2 qwords | ||
|
||
#define POINTER_SIZE 0x08 | ||
|
||
#define THUNKS_MAP_SIZE 0x8000 | ||
|
||
#define PAGE_SIZE 0x1000 | ||
#define PAGE_SIZE_LOG2 12 | ||
|
||
// THUNK_POOL_NUM_THUNKS_PER_PAGE = min(PAGE_SIZE / THUNK_CODESIZE, (PAGE_SIZE - POINTER_SIZE) / THUNK_DATASIZE) | ||
#define THUNK_POOL_NUM_THUNKS_PER_PAGE 0xff | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Thunk Pages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
.macro THUNKS_PAGE_BLOCK | ||
IN_PAGE_INDEX = 0 | ||
.rept THUNK_POOL_NUM_THUNKS_PER_PAGE | ||
|
||
.p2align 4 | ||
|
||
// Set r10 to the address of the current thunk's data block. | ||
lea r10, [rip + THUNKS_MAP_SIZE - 7] | ||
|
||
// jump to the location pointed at by the last qword in the data page | ||
jmp qword ptr[r10 + PAGE_SIZE - POINTER_SIZE - (THUNK_DATASIZE * IN_PAGE_INDEX)] | ||
|
||
IN_PAGE_INDEX = IN_PAGE_INDEX + 1 | ||
.endr | ||
.endm | ||
|
||
#ifdef TARGET_APPLE | ||
// Create two segments in the Mach-O file: | ||
// __THUNKS with executable permissions | ||
// __THUNKS_DATA with read/write permissions | ||
|
||
.section __THUNKS,__thunks,regular,pure_instructions | ||
.p2align PAGE_SIZE_LOG2 | ||
PATCH_LABEL ThunkPool | ||
.rept (THUNKS_MAP_SIZE / PAGE_SIZE) | ||
.p2align PAGE_SIZE_LOG2 | ||
THUNKS_PAGE_BLOCK | ||
.endr | ||
.p2align PAGE_SIZE_LOG2 | ||
.section __THUNKS_DATA,__thunks,regular | ||
.p2align PAGE_SIZE_LOG2 | ||
.space THUNKS_MAP_SIZE | ||
.p2align PAGE_SIZE_LOG2 | ||
#else | ||
#error Unsupported OS | ||
#endif | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; General Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
// | ||
// IntPtr RhpGetThunksBase() | ||
// | ||
LEAF_ENTRY RhpGetThunksBase | ||
// Return the address of the first thunk pool to the caller (this is really the base address) | ||
lea rax, [rip + C_FUNC(ThunkPool)] | ||
ret | ||
LEAF_END RhpGetThunksBase | ||
|
||
// | ||
// int RhpGetNumThunksPerBlock() | ||
// | ||
LEAF_ENTRY RhpGetNumThunksPerBlock | ||
mov rax, THUNK_POOL_NUM_THUNKS_PER_PAGE | ||
ret | ||
LEAF_END RhpGetNumThunksPerBlock | ||
|
||
// | ||
// int RhpGetThunkSize() | ||
// | ||
LEAF_ENTRY RhpGetThunkSize | ||
mov rax, THUNK_CODESIZE | ||
ret | ||
LEAF_END RhpGetThunkSize | ||
|
||
// | ||
// int RhpGetNumThunkBlocksPerMapping() | ||
// | ||
LEAF_ENTRY RhpGetNumThunkBlocksPerMapping | ||
mov rax, (THUNKS_MAP_SIZE / PAGE_SIZE) | ||
ret | ||
LEAF_END RhpGetNumThunkBlocksPerMapping | ||
|
||
// | ||
// int RhpGetThunkBlockSize | ||
// | ||
LEAF_ENTRY RhpGetThunkBlockSize | ||
mov rax, PAGE_SIZE | ||
ret | ||
LEAF_END RhpGetThunkBlockSize | ||
|
||
// | ||
// IntPtr RhpGetThunkDataBlockAddress(IntPtr thunkStubAddress) | ||
// | ||
LEAF_ENTRY RhpGetThunkDataBlockAddress | ||
mov rax, rdi | ||
mov rdi, PAGE_SIZE - 1 | ||
not rdi | ||
and rax, rdi | ||
add rax, THUNKS_MAP_SIZE | ||
ret | ||
LEAF_END RhpGetThunkDataBlockAddress | ||
|
||
// | ||
// IntPtr RhpGetThunkStubsBlockAddress(IntPtr thunkDataAddress) | ||
// | ||
LEAF_ENTRY RhpGetThunkStubsBlockAddress | ||
mov rax, rdi | ||
mov rdi, PAGE_SIZE - 1 | ||
not rdi | ||
and rax, rdi | ||
sub rax, THUNKS_MAP_SIZE | ||
ret | ||
LEAF_END RhpGetThunkStubsBlockAddress |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include <unixasmmacros.inc> | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA SECTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
#define THUNK_CODESIZE 0x10 // 3 instructions, 4 bytes each (and we also have 4 bytes of padding) | ||
#define THUNK_DATASIZE 0x10 // 2 qwords | ||
|
||
#define POINTER_SIZE 0x08 | ||
|
||
#define THUNKS_MAP_SIZE 0x8000 | ||
|
||
#ifdef TARGET_APPLE | ||
#define PAGE_SIZE 0x4000 | ||
#define PAGE_SIZE_LOG2 14 | ||
#else | ||
#error Unsupported OS | ||
#endif | ||
|
||
// THUNK_POOL_NUM_THUNKS_PER_PAGE = min(PAGE_SIZE / THUNK_CODESIZE, (PAGE_SIZE - POINTER_SIZE) / THUNK_DATASIZE) | ||
#define THUNK_POOL_NUM_THUNKS_PER_PAGE 0x3ff | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Thunk Pages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
.macro THUNKS_PAGE_BLOCK | ||
IN_PAGE_INDEX = 0 | ||
.rept THUNK_POOL_NUM_THUNKS_PER_PAGE | ||
|
||
// Set xip0 to the address of the current thunk's data block. | ||
adr xip0, THUNKS_MAP_SIZE | ||
|
||
// start : xip0 points to the current thunks first data cell in the data page | ||
// set xip0 to beginning of data page : xip0 <- xip0 - (THUNK_DATASIZE * current thunk's index) | ||
// fix offset to point to last QWROD in page : xip1 <- [xip0 + PAGE_SIZE - POINTER_SIZE] | ||
// tailcall to the location pointed at by the last qword in the data page | ||
ldr xip1, [xip0, #(PAGE_SIZE - POINTER_SIZE - (THUNK_DATASIZE * IN_PAGE_INDEX))] | ||
br xip1 | ||
|
||
brk 0xf000 // Stubs need to be 16-byte aligned for CFG table. Filling padding with a | ||
// deterministic brk instruction, instead of having it just filled with zeros. | ||
|
||
IN_PAGE_INDEX = IN_PAGE_INDEX + 1 | ||
.endr | ||
.endm | ||
|
||
#ifdef TARGET_APPLE | ||
// Create two segments in the Mach-O file: | ||
// __THUNKS with executable permissions | ||
// __THUNKS_DATA with read/write permissions | ||
|
||
.section __THUNKS,__thunks,regular,pure_instructions | ||
.p2align PAGE_SIZE_LOG2 | ||
PATCH_LABEL ThunkPool | ||
.rept (THUNKS_MAP_SIZE / PAGE_SIZE) | ||
.p2align PAGE_SIZE_LOG2 | ||
THUNKS_PAGE_BLOCK | ||
.endr | ||
.p2align PAGE_SIZE_LOG2 | ||
.section __THUNKS_DATA,__thunks,regular | ||
.p2align PAGE_SIZE_LOG2 | ||
.space THUNKS_MAP_SIZE | ||
.p2align PAGE_SIZE_LOG2 | ||
#else | ||
#error Unsupported OS | ||
#endif | ||
|
||
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; General Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
// | ||
// IntPtr RhpGetThunksBase() | ||
// | ||
LEAF_ENTRY RhpGetThunksBase | ||
// Return the address of the first thunk pool to the caller (this is really the base address) | ||
adrp x0, C_FUNC(ThunkPool)@PAGE | ||
add x0, x0, C_FUNC(ThunkPool)@PAGEOFF | ||
ret | ||
LEAF_END RhpGetThunksBase | ||
|
||
// | ||
// int RhpGetNumThunksPerBlock() | ||
// | ||
LEAF_ENTRY RhpGetNumThunksPerBlock | ||
mov x0, THUNK_POOL_NUM_THUNKS_PER_PAGE | ||
ret | ||
LEAF_END RhpGetNumThunksPerBlock | ||
|
||
// | ||
// int RhpGetThunkSize() | ||
// | ||
LEAF_ENTRY RhpGetThunkSize | ||
mov x0, THUNK_CODESIZE | ||
ret | ||
LEAF_END RhpGetThunkSize | ||
|
||
// | ||
// int RhpGetNumThunkBlocksPerMapping() | ||
// | ||
LEAF_ENTRY RhpGetNumThunkBlocksPerMapping | ||
mov x0, (THUNKS_MAP_SIZE / PAGE_SIZE) | ||
ret | ||
LEAF_END RhpGetNumThunkBlocksPerMapping | ||
|
||
// | ||
// int RhpGetThunkBlockSize | ||
// | ||
LEAF_ENTRY RhpGetThunkBlockSize | ||
mov x0, PAGE_SIZE | ||
ret | ||
LEAF_END RhpGetThunkBlockSize | ||
|
||
// | ||
// IntPtr RhpGetThunkDataBlockAddress(IntPtr thunkStubAddress) | ||
// | ||
LEAF_ENTRY RhpGetThunkDataBlockAddress | ||
mov x12, PAGE_SIZE - 1 | ||
bic x0, x0, x12 | ||
mov x12, THUNKS_MAP_SIZE | ||
add x0, x0, x12 | ||
ret | ||
LEAF_END RhpGetThunkDataBlockAddress | ||
|
||
// | ||
// IntPtr RhpGetThunkStubsBlockAddress(IntPtr thunkDataAddress) | ||
// | ||
LEAF_ENTRY RhpGetThunkStubsBlockAddress | ||
mov x12, PAGE_SIZE - 1 | ||
bic x0, x0, x12 | ||
mov x12, THUNKS_MAP_SIZE | ||
sub x0, x0, x12 | ||
ret | ||
LEAF_END RhpGetThunkStubsBlockAddress |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.