|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include <unixasmmacros.inc> |
| 5 | + |
| 6 | +//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA SECTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 7 | + |
| 8 | +#define THUNK_CODESIZE 0x10 // 3 instructions, 4 bytes each (and we also have 4 bytes of padding) |
| 9 | +#define THUNK_DATASIZE 0x10 // 2 qwords |
| 10 | + |
| 11 | +#define POINTER_SIZE 0x08 |
| 12 | + |
| 13 | +#define THUNKS_MAP_SIZE 0x8000 |
| 14 | + |
| 15 | +#ifdef TARGET_APPLE |
| 16 | +#define PAGE_SIZE 0x4000 |
| 17 | +#define PAGE_SIZE_LOG2 14 |
| 18 | +#else |
| 19 | +#error Unsupported OS |
| 20 | +#endif |
| 21 | + |
| 22 | +// THUNK_POOL_NUM_THUNKS_PER_PAGE = min(PAGE_SIZE / THUNK_CODESIZE, (PAGE_SIZE - POINTER_SIZE) / THUNK_DATASIZE) |
| 23 | +#define THUNK_POOL_NUM_THUNKS_PER_PAGE 0x3ff |
| 24 | + |
| 25 | +//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Thunk Pages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 26 | + |
| 27 | +.macro THUNKS_PAGE_BLOCK |
| 28 | + IN_PAGE_INDEX = 0 |
| 29 | + .rept THUNK_POOL_NUM_THUNKS_PER_PAGE |
| 30 | + |
| 31 | + // Set xip0 to the address of the current thunk's data block. |
| 32 | + adr xip0, THUNKS_MAP_SIZE |
| 33 | + |
| 34 | + // start : xip0 points to the current thunks first data cell in the data page |
| 35 | + // set xip0 to beginning of data page : xip0 <- xip0 - (THUNK_DATASIZE * current thunk's index) |
| 36 | + // fix offset to point to last QWROD in page : xip1 <- [xip0 + PAGE_SIZE - POINTER_SIZE] |
| 37 | + // tailcall to the location pointed at by the last qword in the data page |
| 38 | + ldr xip1, [xip0, #(PAGE_SIZE - POINTER_SIZE - (THUNK_DATASIZE * IN_PAGE_INDEX))] |
| 39 | + br xip1 |
| 40 | + |
| 41 | + brk 0xf000 // Stubs need to be 16-byte aligned for CFG table. Filling padding with a |
| 42 | + // deterministic brk instruction, instead of having it just filled with zeros. |
| 43 | + |
| 44 | + IN_PAGE_INDEX = IN_PAGE_INDEX + 1 |
| 45 | + .endr |
| 46 | +.endm |
| 47 | + |
| 48 | +#ifdef TARGET_APPLE |
| 49 | + // Create two segments in the Mach-O file: |
| 50 | + // __THUNKS with executable permissions |
| 51 | + // __THUNKS_DATA with read/write permissions |
| 52 | + |
| 53 | + .section __THUNKS,__thunks,regular,pure_instructions |
| 54 | + .p2align PAGE_SIZE_LOG2 |
| 55 | +PATCH_LABEL ThunkPool |
| 56 | + .rept (THUNKS_MAP_SIZE / PAGE_SIZE) |
| 57 | + .p2align PAGE_SIZE_LOG2 |
| 58 | + THUNKS_PAGE_BLOCK |
| 59 | + .endr |
| 60 | + .p2align PAGE_SIZE_LOG2 |
| 61 | + .section __THUNKS_DATA,__thunks,regular |
| 62 | + .p2align PAGE_SIZE_LOG2 |
| 63 | + .space THUNKS_MAP_SIZE |
| 64 | + .p2align PAGE_SIZE_LOG2 |
| 65 | +#else |
| 66 | +#error Unsupported OS |
| 67 | +#endif |
| 68 | + |
| 69 | +//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; General Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 70 | + |
| 71 | +// |
| 72 | +// IntPtr RhpGetThunksBase() |
| 73 | +// |
| 74 | +LEAF_ENTRY RhpGetThunksBase |
| 75 | + // Return the address of the first thunk pool to the caller (this is really the base address) |
| 76 | + adrp x0, C_FUNC(ThunkPool)@PAGE |
| 77 | + add x0, x0, C_FUNC(ThunkPool)@PAGEOFF |
| 78 | + ret |
| 79 | +LEAF_END RhpGetThunksBase |
| 80 | + |
| 81 | +// |
| 82 | +// int RhpGetNumThunksPerBlock() |
| 83 | +// |
| 84 | +LEAF_ENTRY RhpGetNumThunksPerBlock |
| 85 | + mov x0, THUNK_POOL_NUM_THUNKS_PER_PAGE |
| 86 | + ret |
| 87 | +LEAF_END RhpGetNumThunksPerBlock |
| 88 | + |
| 89 | +// |
| 90 | +// int RhpGetThunkSize() |
| 91 | +// |
| 92 | +LEAF_ENTRY RhpGetThunkSize |
| 93 | + mov x0, THUNK_CODESIZE |
| 94 | + ret |
| 95 | +LEAF_END RhpGetThunkSize |
| 96 | + |
| 97 | +// |
| 98 | +// int RhpGetNumThunkBlocksPerMapping() |
| 99 | +// |
| 100 | +LEAF_ENTRY RhpGetNumThunkBlocksPerMapping |
| 101 | + mov x0, (THUNKS_MAP_SIZE / PAGE_SIZE) |
| 102 | + ret |
| 103 | +LEAF_END RhpGetNumThunkBlocksPerMapping |
| 104 | + |
| 105 | +// |
| 106 | +// int RhpGetThunkBlockSize |
| 107 | +// |
| 108 | +LEAF_ENTRY RhpGetThunkBlockSize |
| 109 | + mov x0, PAGE_SIZE |
| 110 | + ret |
| 111 | +LEAF_END RhpGetThunkBlockSize |
| 112 | + |
| 113 | +// |
| 114 | +// IntPtr RhpGetThunkDataBlockAddress(IntPtr thunkStubAddress) |
| 115 | +// |
| 116 | +LEAF_ENTRY RhpGetThunkDataBlockAddress |
| 117 | + mov x12, PAGE_SIZE - 1 |
| 118 | + bic x0, x0, x12 |
| 119 | + mov x12, THUNKS_MAP_SIZE |
| 120 | + add x0, x0, x12 |
| 121 | + ret |
| 122 | +LEAF_END RhpGetThunkDataBlockAddress |
| 123 | + |
| 124 | +// |
| 125 | +// IntPtr RhpGetThunkStubsBlockAddress(IntPtr thunkDataAddress) |
| 126 | +// |
| 127 | +LEAF_ENTRY RhpGetThunkStubsBlockAddress |
| 128 | + mov x12, PAGE_SIZE - 1 |
| 129 | + bic x0, x0, x12 |
| 130 | + mov x12, THUNKS_MAP_SIZE |
| 131 | + sub x0, x0, x12 |
| 132 | + ret |
| 133 | +LEAF_END RhpGetThunkStubsBlockAddress |
0 commit comments