forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThunksMapping.cpp
427 lines (334 loc) · 14.4 KB
/
ThunksMapping.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "common.h"
#include "CommonTypes.h"
#include "CommonMacros.h"
#include "daccess.h"
#include "PalRedhawkCommon.h"
#include "CommonMacros.inl"
#include "volatile.h"
#include "PalRedhawk.h"
#include "rhassert.h"
#ifdef FEATURE_RX_THUNKS
#ifdef TARGET_AMD64
#define THUNK_SIZE 20
#elif TARGET_X86
#define THUNK_SIZE 12
#elif TARGET_ARM
#define THUNK_SIZE 20
#elif TARGET_ARM64
#define THUNK_SIZE 16
#elif TARGET_LOONGARCH64
#define THUNK_SIZE 16
#elif TARGET_RISCV64
#define THUNK_SIZE 20
#else
#define THUNK_SIZE (2 * OS_PAGE_SIZE) // This will cause RhpGetNumThunksPerBlock to return 0
#endif
static_assert((THUNK_SIZE % 4) == 0, "Thunk stubs size not aligned correctly. This will cause runtime failures.");
// 32 K or OS page
#define THUNKS_MAP_SIZE (max(0x8000, OS_PAGE_SIZE))
#ifdef TARGET_ARM
//*****************************************************************************
// Encode a 16-bit immediate mov/movt in ARM Thumb2 Instruction (format T2_N)
//*****************************************************************************
void EncodeThumb2Mov16(uint16_t * pCode, uint16_t value, uint8_t rDestination, bool topWord)
{
pCode[0] = ((topWord ? 0xf2c0 : 0xf240) |
((value >> 12) & 0x000f) |
((value >> 1) & 0x0400));
pCode[1] = (((value << 4) & 0x7000) |
(value & 0x00ff) |
(rDestination << 8));
}
//*****************************************************************************
// Encode a 32-bit immediate mov in ARM Thumb2 Instruction (format T2_N)
//*****************************************************************************
void EncodeThumb2Mov32(uint16_t * pCode, uint32_t value, uint8_t rDestination)
{
EncodeThumb2Mov16(pCode, (uint16_t)(value & 0x0000ffff), rDestination, false);
EncodeThumb2Mov16(pCode + 2, (uint16_t)(value >> 16), rDestination, true);
}
#endif
FCIMPL0(int, RhpGetNumThunkBlocksPerMapping)
{
ASSERT_MSG((THUNKS_MAP_SIZE % OS_PAGE_SIZE) == 0, "Thunks map size should be in multiples of pages");
return THUNKS_MAP_SIZE / OS_PAGE_SIZE;
}
FCIMPLEND
FCIMPL0(int, RhpGetNumThunksPerBlock)
{
return min(
OS_PAGE_SIZE / THUNK_SIZE, // Number of thunks that can fit in a page
(OS_PAGE_SIZE - POINTER_SIZE) / (POINTER_SIZE * 2) // Number of pointer pairs, minus the jump stub cell, that can fit in a page
);
}
FCIMPLEND
FCIMPL0(int, RhpGetThunkSize)
{
return THUNK_SIZE;
}
FCIMPLEND
FCIMPL1(void*, RhpGetThunkDataBlockAddress, void* pThunkStubAddress)
{
return (void*)(((uintptr_t)pThunkStubAddress & ~(OS_PAGE_SIZE - 1)) + THUNKS_MAP_SIZE);
}
FCIMPLEND
FCIMPL1(void*, RhpGetThunkStubsBlockAddress, void* pThunkDataAddress)
{
return (void*)(((uintptr_t)pThunkDataAddress & ~(OS_PAGE_SIZE - 1)) - THUNKS_MAP_SIZE);
}
FCIMPLEND
FCIMPL0(int, RhpGetThunkBlockSize)
{
return OS_PAGE_SIZE;
}
FCIMPLEND
EXTERN_C void* QCALLTYPE RhAllocateThunksMapping()
{
#ifdef WIN32
void * pNewMapping = PalVirtualAlloc(THUNKS_MAP_SIZE * 2, PAGE_READWRITE);
if (pNewMapping == NULL)
return NULL;
void * pThunksSection = pNewMapping;
void * pDataSection = (uint8_t*)pNewMapping + THUNKS_MAP_SIZE;
#else
// Note: On secure linux systems, we can't add execute permissions to a mapped virtual memory if it was not created
// with execute permissions in the first place. This is why we create the virtual section with RX permissions, then
// reduce it to RW for the data section. For the stubs section we need to increase to RWX to generate the stubs
// instructions. After this we go back to RX for the stubs section before the stubs are used and should not be
// changed anymore.
void * pNewMapping = PalVirtualAlloc(THUNKS_MAP_SIZE * 2, PAGE_EXECUTE_READ);
if (pNewMapping == NULL)
return NULL;
void * pThunksSection = pNewMapping;
void * pDataSection = (uint8_t*)pNewMapping + THUNKS_MAP_SIZE;
if (!PalVirtualProtect(pDataSection, THUNKS_MAP_SIZE, PAGE_READWRITE) ||
!PalVirtualProtect(pThunksSection, THUNKS_MAP_SIZE, PAGE_EXECUTE_READWRITE))
{
PalVirtualFree(pNewMapping, THUNKS_MAP_SIZE * 2);
return NULL;
}
#if defined(HOST_APPLE) && defined(HOST_ARM64)
#if defined(HOST_MACCATALYST) || defined(HOST_IOS) || defined(HOST_TVOS)
RhFailFast(); // we don't expect to get here on these platforms
#elif defined(HOST_OSX)
pthread_jit_write_protect_np(0);
#else
#error "Unknown OS"
#endif
#endif
#endif
int numBlocksPerMap = RhpGetNumThunkBlocksPerMapping();
int numThunksPerBlock = RhpGetNumThunksPerBlock();
for (int m = 0; m < numBlocksPerMap; m++)
{
uint8_t* pDataBlockAddress = (uint8_t*)pDataSection + m * OS_PAGE_SIZE;
uint8_t* pThunkBlockAddress = (uint8_t*)pThunksSection + m * OS_PAGE_SIZE;
for (int i = 0; i < numThunksPerBlock; i++)
{
uint8_t* pCurrentThunkAddress = pThunkBlockAddress + THUNK_SIZE * i;
uint8_t* pCurrentDataAddress = pDataBlockAddress + i * POINTER_SIZE * 2;
#ifdef TARGET_AMD64
// mov r10,<thunk data address>
// jmp [r10 + <delta to get to last qword in data page]
*((uint16_t*)pCurrentThunkAddress) = 0xba49;
pCurrentThunkAddress += 2;
*((void **)pCurrentThunkAddress) = (void *)pCurrentDataAddress;
pCurrentThunkAddress += 8;
*((uint32_t*)pCurrentThunkAddress) = 0x00a2ff41;
pCurrentThunkAddress += 3;
*((uint32_t*)pCurrentThunkAddress) = OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2);
pCurrentThunkAddress += 4;
// nops for alignment
*pCurrentThunkAddress++ = 0x90;
*pCurrentThunkAddress++ = 0x90;
*pCurrentThunkAddress++ = 0x90;
#elif TARGET_X86
// mov eax,<thunk data address>
// jmp [eax + <delta to get to last dword in data page]
*pCurrentThunkAddress++ = 0xb8;
*((void **)pCurrentThunkAddress) = (void *)pCurrentDataAddress;
pCurrentThunkAddress += 4;
*((uint16_t*)pCurrentThunkAddress) = 0xa0ff;
pCurrentThunkAddress += 2;
*((uint32_t*)pCurrentThunkAddress) = OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2);
pCurrentThunkAddress += 4;
// nops for alignment
*pCurrentThunkAddress++ = 0x90;
#elif TARGET_ARM
// mov r12,<thunk data address>
// str r12,[sp,#-4]
// ldr r12,[r12, <delta to get to last dword in data page]
// bx r12
EncodeThumb2Mov32((uint16_t*)pCurrentThunkAddress, (uint32_t)pCurrentDataAddress, 12);
pCurrentThunkAddress += 8;
*((uint32_t*)pCurrentThunkAddress) = 0xcc04f84d;
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0xc000f8dc | ((OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2)) << 16);
pCurrentThunkAddress += 4;
*((uint16_t*)pCurrentThunkAddress) = 0x4760;
pCurrentThunkAddress += 2;
// nops for alignment
*((uint16_t*)pCurrentThunkAddress) = 0xbf00;
pCurrentThunkAddress += 2;
#elif TARGET_ARM64
//adr xip0, <delta PC to thunk data address>
//ldr xip1, [xip0, <delta to get to last qword in data page>]
//br xip1
//brk 0xf000 //Stubs need to be 16 byte aligned therefore we fill with a break here
int delta = (int)(pCurrentDataAddress - pCurrentThunkAddress);
*((uint32_t*)pCurrentThunkAddress) = 0x10000010 | (((delta & 0x03) << 29) | (((delta & 0x1FFFFC) >> 2) << 5));
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0xF9400211 | (((OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2)) / 8) << 10);
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0xD61F0220;
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0xD43E0000;
pCurrentThunkAddress += 4;
#elif TARGET_LOONGARCH64
//pcaddi $t7, <delta PC to thunk data address>
//pcaddi $t8, -
//ld.d $t8, $t8, <delta to get to last qword in data page>
//jirl $r0, $t8, 0
int delta = (int)(pCurrentDataAddress - pCurrentThunkAddress);
*((uint32_t*)pCurrentThunkAddress) = 0x18000013 | (((delta & 0x3FFFFC) >> 2) << 5);
pCurrentThunkAddress += 4;
delta += OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2) - 4;
*((uint32_t*)pCurrentThunkAddress) = 0x18000014 | (((delta & 0x3FFFFC) >> 2) << 5);
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0x28C00294;
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0x4C000280;
pCurrentThunkAddress += 4;
#elif defined(TARGET_RISCV64)
//auipc t1, hi(<delta PC to thunk data address>)
//addi t1, t1, lo(<delta PC to thunk data address>)
//auipc t0, hi(<delta to get to last word in data page>)
//ld t0, (t0)
//jalr zero, t0, 0
int delta = (int)(pCurrentDataAddress - pCurrentThunkAddress);
*((uint32_t*)pCurrentThunkAddress) = 0x00000317 | ((((delta + 0x800) & 0xFFFFF000) >> 12) << 12); // auipc t1, delta[31:12]
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0x00030313 | ((delta & 0xFFF) << 20); // addi t1, t1, delta[11:0]
pCurrentThunkAddress += 4;
delta += OS_PAGE_SIZE - POINTER_SIZE - (i * POINTER_SIZE * 2) - 8;
*((uint32_t*)pCurrentThunkAddress) = 0x00000297 | ((((delta + 0x800) & 0xFFFFF000) >> 12) << 12); // auipc t0, delta[31:12]
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0x0002b283 | ((delta & 0xFFF) << 20); // ld t0, (delta[11:0])(t0)
pCurrentThunkAddress += 4;
*((uint32_t*)pCurrentThunkAddress) = 0x00008282; // jalr zero, t0, 0
pCurrentThunkAddress += 4;
#else
UNREFERENCED_PARAMETER(pCurrentDataAddress);
UNREFERENCED_PARAMETER(pCurrentThunkAddress);
PORTABILITY_ASSERT("RhAllocateThunksMapping");
#endif
}
}
#if defined(HOST_APPLE) && defined(HOST_ARM64)
#if defined(HOST_MACCATALYST) || defined(HOST_IOS) || defined(HOST_TVOS)
RhFailFast(); // we don't expect to get here on these platforms
#elif defined(HOST_OSX)
pthread_jit_write_protect_np(1);
#else
#error "Unknown OS"
#endif
#else
if (!PalVirtualProtect(pThunksSection, THUNKS_MAP_SIZE, PAGE_EXECUTE_READ))
{
PalVirtualFree(pNewMapping, THUNKS_MAP_SIZE * 2);
return NULL;
}
#endif
PalFlushInstructionCache(pThunksSection, THUNKS_MAP_SIZE);
return pThunksSection;
}
// FEATURE_RX_THUNKS
#elif FEATURE_FIXED_POOL_THUNKS
// This is used by the thunk code to find the stub data for the called thunk slot
extern "C" uintptr_t g_pThunkStubData;
uintptr_t g_pThunkStubData = NULL;
FCDECL0(int, RhpGetThunkBlockCount);
FCDECL0(int, RhpGetNumThunkBlocksPerMapping);
FCDECL0(int, RhpGetThunkBlockSize);
FCDECL1(void*, RhpGetThunkDataBlockAddress, void* addr);
FCDECL1(void*, RhpGetThunkStubsBlockAddress, void* addr);
EXTERN_C void* QCALLTYPE RhAllocateThunksMapping()
{
static int nextThunkDataMapping = 0;
int thunkBlocksPerMapping = RhpGetNumThunkBlocksPerMapping();
int thunkBlockSize = RhpGetThunkBlockSize();
int blockCount = RhpGetThunkBlockCount();
ASSERT(blockCount % thunkBlocksPerMapping == 0)
int thunkDataMappingSize = thunkBlocksPerMapping * thunkBlockSize;
int thunkDataMappingCount = blockCount / thunkBlocksPerMapping;
if (nextThunkDataMapping == thunkDataMappingCount)
{
return NULL;
}
if (g_pThunkStubData == NULL)
{
int thunkDataSize = thunkDataMappingSize * thunkDataMappingCount;
g_pThunkStubData = (uintptr_t)VirtualAlloc(NULL, thunkDataSize, MEM_RESERVE, PAGE_READWRITE);
if (g_pThunkStubData == NULL)
{
return NULL;
}
}
void* pThunkDataBlock = (int8_t*)g_pThunkStubData + nextThunkDataMapping * thunkDataMappingSize;
if (VirtualAlloc(pThunkDataBlock, thunkDataMappingSize, MEM_COMMIT, PAGE_READWRITE) == NULL)
{
return NULL;
}
nextThunkDataMapping++;
void* pThunks = RhpGetThunkStubsBlockAddress(pThunkDataBlock);
ASSERT(RhpGetThunkDataBlockAddress(pThunks) == pThunkDataBlock);
return pThunks;
}
#else // FEATURE_FIXED_POOL_THUNKS
FCDECL0(void*, RhpGetThunksBase);
FCDECL0(int, RhpGetNumThunkBlocksPerMapping);
FCDECL0(int, RhpGetNumThunksPerBlock);
FCDECL0(int, RhpGetThunkSize);
FCDECL0(int, RhpGetThunkBlockSize);
EXTERN_C void* QCALLTYPE RhAllocateThunksMapping()
{
static void* pThunksTemplateAddress = NULL;
void *pThunkMap = NULL;
int thunkBlocksPerMapping = RhpGetNumThunkBlocksPerMapping();
int thunkBlockSize = RhpGetThunkBlockSize();
int templateSize = thunkBlocksPerMapping * thunkBlockSize;
#ifndef TARGET_APPLE // Apple platforms cannot use the initial template
if (pThunksTemplateAddress == NULL)
{
// First, we use the thunks directly from the thunks template sections in the module until all
// thunks in that template are used up.
pThunksTemplateAddress = RhpGetThunksBase();
pThunkMap = pThunksTemplateAddress;
}
else
#endif
{
// We've already used the thunks template in the module for some previous thunks, and we
// cannot reuse it here. Now we need to create a new mapping of the thunks section in order to have
// more thunks
uint8_t* pModuleBase = (uint8_t*)PalGetModuleHandleFromPointer(RhpGetThunksBase());
int templateRva = (int)((uint8_t*)RhpGetThunksBase() - pModuleBase);
if (!PalAllocateThunksFromTemplate((HANDLE)pModuleBase, templateRva, templateSize, &pThunkMap))
return NULL;
}
if (!PalMarkThunksAsValidCallTargets(
pThunkMap,
RhpGetThunkSize(),
RhpGetNumThunksPerBlock(),
thunkBlockSize,
thunkBlocksPerMapping))
{
if (pThunkMap != pThunksTemplateAddress)
PalFreeThunksFromTemplate(pThunkMap, templateSize);
return NULL;
}
return pThunkMap;
}
#endif // FEATURE_RX_THUNKS