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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
.intel_syntax noprefix
#include <unixasmmacros.inc>
#include <AsmOffsets.inc> // generated by the build from AsmOffsets.cpp
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RhpThrowHwEx
//
// INPUT: RDI: exception code of fault
// RSI: faulting RIP
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpThrowHwEx, _TEXT, NoHandler
STACKSIZEOF_ExInfo = ((SIZEOF__ExInfo + 15) & (~15))
rsp_offsetof_Context = STACKSIZEOF_ExInfo
mov rax, rsp // save the faulting RSP
add rsi, 1 // 'faulting IP' += 1, we do this because everywhere else we treat the faulting IP as
// a return-address and optionally subtract one when doing EH-related things (but not
// subtracting 1 when doing GC-related things). The fault IP here will be the start
// of the faulting instruction, so +1 will point to either the next instruction or the
// middle of this instruction. Either way, when the dispatch / stackwalk code deals
// with this address it'll apply a -1 for EH range checks and the GC-related operations
// don't need to be precise here because the fault location isn't a GC safe point
xor rdx, rdx
push_register rdx // padding
// struct PAL_LIMITED_CONTEXT
// {
push_nonvol_reg r15
push_nonvol_reg r14
push_nonvol_reg r13
push_nonvol_reg r12
push_register rdx // rdx set to 0
push_nonvol_reg rbx
push_register rdx // rax set to 0
push_nonvol_reg rbp
push_register rax // faulting RSP
push_register rsi // faulting IP
// }
// allocate outgoing args area and space for the ExInfo
alloc_stack STACKSIZEOF_ExInfo
END_PROLOGUE
mov rbx, rdi
INLINE_GETTHREAD
mov rdi, rbx
mov rsi, rsp // rsi <- ExInfo*
xor rdx, rdx
mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null
mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass
mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF
mov byte ptr [rsi + OFFSETOF__ExInfo__m_kind], 2 // ExKind.HardwareFault
// link the ExInfo into the thread's ExInfo chain
mov rdx, [rax + OFFSETOF__Thread__m_pExInfoStackHead]
mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdx // pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo
// set the exception context field on the ExInfo
lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT*
mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext
// rdi still contains the exception code
// rsi contains the address of the ExInfo
call C_FUNC(RhThrowHwEx)
ALTERNATE_ENTRY RhpThrowHwEx2
// no return
int 3
NESTED_END RhpThrowHwEx, _TEXT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RhpThrowEx
//
// INPUT: RDI: exception object
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpThrowEx, _TEXT, NoHandler
STACKSIZEOF_ExInfo = ((SIZEOF__ExInfo + 15) & (~ 15))
rsp_offsetof_Context = STACKSIZEOF_ExInfo
lea rax, [rsp+8] // save the RSP of the throw site
mov rsi, [rsp] // get return address
xor rdx, rdx
push_register rdx // padding
// struct PAL_LIMITED_CONTEXT
// {
push_nonvol_reg r15
push_nonvol_reg r14
push_nonvol_reg r13
push_nonvol_reg r12
push_register rdx // rdx set to 0
push_nonvol_reg rbx
push_register rdx // rax set to 0
push_nonvol_reg rbp
push_register rax // 'faulting' RSP
push_register rsi // 'faulting' IP
// }
// allocate space for the ExInfo
alloc_stack STACKSIZEOF_ExInfo
END_PROLOGUE
mov rbx, rdi
INLINE_GETTHREAD
mov rdi, rbx
lea rbx, [rsp + rsp_offsetof_Context + SIZEOF__PAL_LIMITED_CONTEXT + 0x8] // rbx <- addr of return address
// There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return
// address could have been hijacked when we were in that C# code and we must remove the hijack and
// reflect the correct return address in our exception context record. The other throw helpers don't
// need this because they cannot be tail-called from C#.
INLINE_THREAD_UNHIJACK rax, rcx, rsi // trashes RCX, RSI
mov rsi, [rbx] // rdx <- return address
mov [rsp + rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP], rsi // set 'faulting' IP after unhijack
mov rsi, rsp // rsi <- ExInfo*
mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null
mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass
mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF
mov byte ptr [rsi + OFFSETOF__ExInfo__m_kind], 1 // ExKind.Throw
// link the ExInfo into the thread's ExInfo chain
mov rdx, [rax + OFFSETOF__Thread__m_pExInfoStackHead]
mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdx // pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo
// set the exception context field on the ExInfo
lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT*
mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext
// rdi still contains the exception object
// rsi contains the address of the ExInfo
call C_FUNC(RhThrowEx)
ALTERNATE_ENTRY RhpThrowEx2
// no return
int 3
NESTED_END RhpThrowEx, _TEXT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void FASTCALL RhpRethrow()
//
// SUMMARY: Similar to RhpThrowEx, except that it passes along the currently active ExInfo
//
// INPUT:
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpRethrow, _TEXT, NoHandler
STACKSIZEOF_ExInfo = ((SIZEOF__ExInfo + 15) & (~ 15))
rsp_offsetof_Context = STACKSIZEOF_ExInfo
lea rax, [rsp+8] // save the RSP of the throw site
mov rsi, [rsp] // get return address
xor rdx, rdx
push_register rdx // padding
// struct PAL_LIMITED_CONTEXT
// {
push_nonvol_reg r15
push_nonvol_reg r14
push_nonvol_reg r13
push_nonvol_reg r12
push_register rdx // rdx set to 0
push_nonvol_reg rbx
push_register rdx // rax set to 0
push_nonvol_reg rbp
push_register rax // 'faulting' RSP
push_register rsi // 'faulting' IP
// }
// allocate space for the ExInfo
alloc_stack STACKSIZEOF_ExInfo
END_PROLOGUE
INLINE_GETTHREAD
mov rsi, rsp // rsi <- ExInfo*
mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null
mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass
mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF
// link the ExInfo into the thread's ExInfo chain
mov rdi, [rax + OFFSETOF__Thread__m_pExInfoStackHead] // rdi <- currently active ExInfo
mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdi // pExInfo->m_pPrevExInfo = m_pExInfoStackHead
mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo
// set the exception context field on the ExInfo
lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT*
mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext
// rdi contains the currently active ExInfo
// rsi contains the address of the new ExInfo
call C_FUNC(RhRethrow)
ALTERNATE_ENTRY RhpRethrow2
// no return
int 3
NESTED_END RhpRethrow, _TEXT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void* FASTCALL RhpCallCatchFunclet(RtuObjectRef exceptionObj, void* pHandlerIP, REGDISPLAY* pRegDisplay,
// ExInfo* pExInfo)
//
// INPUT: RDI: exception object
// RSI: handler funclet address
// RDX: REGDISPLAY*
// RCX: ExInfo*
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpCallCatchFunclet, _TEXT, NoHandler
push_nonvol_reg r15 // save preserved regs for OS stackwalker
push_nonvol_reg r14 // ...
push_nonvol_reg r13 // ...
push_nonvol_reg r12 // ...
push_nonvol_reg rbx // ...
push_nonvol_reg rbp // ...
locThread = 0
locResumeIp = 8
locArg0 = 0x10
locArg1 = 0x18
locArg2 = 0x20
locArg3 = 0x28
alloc_stack 0x38 // locals
END_PROLOGUE
mov [rsp + locArg0], rdi // save arguments for later
mov [rsp + locArg1], rsi
mov [rsp + locArg2], rdx
mov [rsp + locArg3], rcx
mov rbx, rdx
INLINE_GETTHREAD
mov rdx, rbx
mov [rsp + locThread], rax // save Thread* for later
// Clear the DoNotTriggerGc state before calling out to our managed catch funclet.
lock and dword ptr [rax + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbx]
mov rbx, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbp]
mov rbp, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR12]
mov r12, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR13]
mov r13, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR14]
mov r14, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR15]
mov r15, [rax]
#if 0 // _DEBUG // @TODO: temporarily removed because trashing RBP breaks the debugger
// trash the values at the old homes to make sure nobody uses them
mov rcx, 0xbaaddeed
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbx]
mov [rax], rcx
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbp]
mov [rax], rcx
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR12]
mov [rax], rcx
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR13]
mov [rax], rcx
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR14]
mov [rax], rcx
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR15]
mov [rax], rcx
#endif
mov rdi, [rdx + OFFSETOF__REGDISPLAY__SP] // rdi <- establisher frame
mov rsi, [rsp + locArg0] // rsi <- exception object
call qword ptr [rsp + locArg1] // call handler funclet
ALTERNATE_ENTRY RhpCallCatchFunclet2
mov rdx, [rsp + locArg2] // rdx <- dispatch context
#ifdef _DEBUG
// Call into some C++ code to validate the pop of the ExInfo. We only do this in debug because we
// have to spill all the preserved registers and then refill them after the call.
mov [rsp + locResumeIp], rax // save resume IP for later
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pRbx]
mov [rdi] , rbx
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pRbp]
mov [rdi] , rbp
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pR12]
mov [rdi] , r12
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pR13]
mov [rdi] , r13
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pR14]
mov [rdi] , r14
mov rdi, [rdx + OFFSETOF__REGDISPLAY__pR15]
mov [rdi] , r15
mov rdi, [rsp] // rdi <- Thread*
mov rsi, [rsp + locArg3] // rsi <- current ExInfo *
mov rdx, [rdx + OFFSETOF__REGDISPLAY__SP] // rdx <- resume SP value
call C_FUNC(RhpValidateExInfoPop)
mov rdx, [rsp + locArg2] // rdx <- dispatch context
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbx]
mov rbx, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbp]
mov rbp, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR12]
mov r12, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR13]
mov r13, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR14]
mov r14, [rax]
mov rax, [rdx + OFFSETOF__REGDISPLAY__pR15]
mov r15, [rax]
mov rax, [rsp + locResumeIp] // reload resume IP
#endif
mov rsi, [rsp + locThread] // rsi <- Thread*
// We must unhijack the thread at this point because the section of stack where the hijack is applied
// may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack.
INLINE_THREAD_UNHIJACK rsi, rdi, rcx // Thread in rsi, trashes rdi and rcx
mov rdi, [rsp + locArg3] // rdi <- current ExInfo *
mov rdx, [rdx + OFFSETOF__REGDISPLAY__SP] // rdx <- resume SP value
xor ecx, ecx // rcx <- 0
LOCAL_LABEL(ExInfoLoop):
mov rdi, [rdi + OFFSETOF__ExInfo__m_pPrevExInfo] // rdi <- next ExInfo
cmp rdi, rcx
je LOCAL_LABEL(ExInfoLoopDone) // we're done if it's null
cmp rdi, rdx
jl LOCAL_LABEL(ExInfoLoop) // keep looping if it's lower than the new SP
LOCAL_LABEL(ExInfoLoopDone):
mov [rsi + OFFSETOF__Thread__m_pExInfoStackHead], rdi // store the new head on the Thread
// reset RSP and jump to the continuation address
mov rsp, rdx // reset the SP
jmp rax
NESTED_END RhpCallCatchFunclet, _TEXT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void FASTCALL RhpCallFinallyFunclet(void* pHandlerIP, REGDISPLAY* pRegDisplay)
//
// INPUT: RDI: handler funclet address
// RSI: REGDISPLAY*
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpCallFinallyFunclet, _TEXT, NoHandler
push_nonvol_reg r15 // save preserved regs
push_nonvol_reg r14 // ...
push_nonvol_reg r13 // ...
push_nonvol_reg r12 // ...
push_nonvol_reg rbx // ...
push_nonvol_reg rbp // ...
locThread = 0
locArg0 = 8
locArg1 = 0x10
alloc_stack 0x18 // locals
END_PROLOGUE
mov [rsp + locArg0], rdi // save arguments for later
mov [rsp + locArg1], rsi
mov rbx, rsi
INLINE_GETTHREAD
mov rsi, rbx
mov [rsp + locThread], rax // save Thread* for later
//
// We want to suppress hijacking between invocations of subsequent finallys. We do this because we
// cannot tolerate a GC after one finally has run (and possibly side-effected the GC state of the
// method) and then been popped off the stack, leaving behind no trace of its effect.
//
// So we clear the state before and set it after invocation of the handler.
//
lock and dword ptr [rax + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbx]
mov rbx, [rax]
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbp]
mov rbp, [rax]
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR12]
mov r12, [rax]
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR13]
mov r13, [rax]
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR14]
mov r14, [rax]
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR15]
mov r15, [rax]
#if 0 // _DEBUG // @TODO: temporarily removed because trashing RBP breaks the debugger
// trash the values at the old homes to make sure nobody uses them
mov rcx, 0xbaaddeed
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbx]
mov [rax], rcx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbp]
mov [rax], rcx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR12]
mov [rax], rcx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR13]
mov [rax], rcx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR14]
mov [rax], rcx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR15]
mov [rax], rcx
#endif
mov rdi, [rsi + OFFSETOF__REGDISPLAY__SP] // rdi <- establisher frame
call qword ptr [rsp + locArg0] // handler funclet address
ALTERNATE_ENTRY RhpCallFinallyFunclet2
mov rsi, [rsp + locArg1] // rsi <- regdisplay
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbx]
mov [rax] , rbx
mov rax, [rsi + OFFSETOF__REGDISPLAY__pRbp]
mov [rax] , rbp
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR12]
mov [rax] , r12
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR13]
mov [rax] , r13
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR14]
mov [rax] , r14
mov rax, [rsi + OFFSETOF__REGDISPLAY__pR15]
mov [rax] , r15
mov rax, [rsp + locThread] // rax <- Thread*
lock or dword ptr [rax + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc
add rsp, 0x18
pop rbp
pop rbx
pop r12
pop r13
pop r14
pop r15
ret
NESTED_END RhpCallFinallyFunclet, _TEXT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// void* FASTCALL RhpCallFilterFunclet(RtuObjectRef exceptionObj, void* pFilterIP, REGDISPLAY* pRegDisplay)
//
// INPUT: RDI: exception object
// RSI: filter funclet address
// RDX: REGDISPLAY*
//
// OUTPUT:
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpCallFilterFunclet, _TEXT, NoHandler
push_nonvol_reg rbp // we'll be setting rbp to the funclet's frame pointer
END_PROLOGUE
mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbp]
mov rbp, [rax]
mov rax, rsi // rax <- handler funclet address
mov rsi, rdi // rsi <- exception object
mov rdi, [rdx + OFFSETOF__REGDISPLAY__SP] // rdi <- establisher frame
call rax
ALTERNATE_ENTRY RhpCallFilterFunclet2
// RAX contains the result of the filter execution
pop rbp
ret
NESTED_END RhpCallFilterFunclet, _TEXT
|