summaryrefslogtreecommitdiff
path: root/src/vm/amd64/unixasmhelpers.S
blob: ccf552367c84994d9e9aab258086b22876c98a69 (plain)
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

.intel_syntax noprefix
#include "unixasmmacros.inc"
#include "asmconstants.h"


//////////////////////////////////////////////////////////////////////////
//
// PrecodeFixupThunk
//
// The call in fixup precode initally points to this function.
// The pupose of this function is to load the MethodDesc and forward the call the prestub.
//
// EXTERN_C VOID __stdcall PrecodeFixupThunk()
LEAF_ENTRY PrecodeFixupThunk, _TEXT

        pop     rax         // Pop the return address. It points right after the call instruction in the precode.

        // Inline computation done by FixupPrecode::GetMethodDesc()
        movzx   r10,byte ptr [rax+2]    // m_PrecodeChunkIndex
        movzx   r11,byte ptr [rax+1]    // m_MethodDescChunkIndex
        mov     rax,qword ptr [rax+r10*8+3]
        lea     METHODDESC_REGISTER,[rax+r11*8]

        // Tail call to prestub
        jmp C_FUNC(ThePreStub)

LEAF_END PrecodeFixupThunk, _TEXT

// EXTERN_C int __fastcall HelperMethodFrameRestoreState(
//         INDEBUG_COMMA(HelperMethodFrame *pFrame)
//         MachState *pState
//         )
LEAF_ENTRY HelperMethodFrameRestoreState, _TEXT

#ifdef _DEBUG
        mov     rdi, rsi
#endif

        // Check if the MachState is valid
        xor     eax, eax
        cmp     qword ptr [rdi + OFFSETOF__MachState___pRetAddr], rax
        jne      DoRestore
        REPRET
DoRestore:

        //
        // If a preserved register were pushed onto the stack between
        // the managed caller and the H_M_F, m_pReg will point to its
        // location on the stack and it would have been updated on the
        // stack by the GC already and it will be popped back into the
        // appropriate register when the appropriate epilog is run.
        // 
        // Otherwise, the register is preserved across all the code
        // in this HCALL or FCALL, so we need to update those registers
        // here because the GC will have updated our copies in the 
        // frame.
        //
        // So, if m_pReg points into the MachState, we need to update
        // the register here.  That's what this macro does.
        //
#define RestoreReg(reg, regnum) \
        lea     rax, [rdi + OFFSETOF__MachState__m_Capture + 8 * regnum]; \
        mov     rdx, [rdi + OFFSETOF__MachState__m_Ptrs + 8 * regnum]; \
        cmp     rax, rdx; \
        cmove   reg, [rax];

        // regnum has to match ENUM_CALLEE_SAVED_REGISTERS macro
        RestoreReg(R12, 0)
        RestoreReg(R13, 1)
        RestoreReg(R14, 2)
        RestoreReg(R15, 3)
        RestoreReg(Rbx, 4)
        RestoreReg(Rbp, 5)

        xor     eax, eax
        ret

LEAF_END HelperMethodFrameRestoreState, _TEXT

//////////////////////////////////////////////////////////////////////////
//
// NDirectImportThunk
//
// In addition to being called by the EE, this function can be called
//  directly from code generated by JIT64 for CRT optimized direct
//  P/Invoke calls. If it is modified, the JIT64 compiler's code
//  generation will need to altered accordingly.
//
// EXTERN_C VOID __stdcall NDirectImportThunk()//
NESTED_ENTRY NDirectImportThunk, _TEXT, NoHandler

        //
        // Save integer parameter registers.
        // Make sure to preserve r11 as well as it is used to pass the stack argument size from JIT
        //
        PUSH_ARGUMENT_REGISTERS
        push_register r11
        
        //
        // Allocate space for XMM parameter registers
        //
        alloc_stack     0x80 

        SAVE_FLOAT_ARGUMENT_REGISTERS 0

    END_PROLOGUE

        //
        // Call NDirectImportWorker w/ the NDirectMethodDesc*
        //
        mov             rdi, METHODDESC_REGISTER
        call            C_FUNC(NDirectImportWorker)
        
        RESTORE_FLOAT_ARGUMENT_REGISTERS 0

        //
        // epilogue, rax contains the native target address
        //
        free_stack      0x80

        //
        // Restore integer parameter registers and r11
        //
        pop_register r11
        POP_ARGUMENT_REGISTERS
        
    TAILJMP_RAX
NESTED_END NDirectImportThunk, _TEXT

// EXTERN_C void moveOWord(LPVOID* src, LPVOID* target);
// <NOTE>
// MOVDQA is not an atomic operation.  You need to call this function in a crst.
// </NOTE>
LEAF_ENTRY moveOWord, _TEXT
        movdqu          xmm0, xmmword ptr [rdi]
        movdqu          xmmword ptr [rsi], xmm0

        ret
LEAF_END moveOWord, _TEXT

//------------------------------------------------
// JIT_RareDisableHelper
//
// The JIT expects this helper to preserve registers used for return values
//
NESTED_ENTRY JIT_RareDisableHelper, _TEXT, NoHandler

    // First integer return register
    push_register rax
    // Second integer return register
    push_register rdx
    alloc_stack         0x28
    END_PROLOGUE
    // First float return register
    movdqa              xmmword ptr [rsp], xmm0
    // Second float return register
    movdqa              xmmword ptr [rsp+0x10], xmm1

    call                C_FUNC(JIT_RareDisableHelperWorker)

    movdqa              xmm0, xmmword ptr [rsp]
    movdqa              xmm1, xmmword ptr [rsp+0x10]
    free_stack          0x28
    pop_register        rdx
    pop_register        rax
    ret

NESTED_END JIT_RareDisableHelper, _TEXT

#ifdef FEATURE_HIJACK

//------------------------------------------------
// OnHijackScalarTripThread
//
NESTED_ENTRY OnHijackScalarTripThread, _TEXT, NoHandler

    // Make room for the real return address (rip)
    push_register rax

    PUSH_CALLEE_SAVED_REGISTERS

    // Push rax again - this is where integer/pointer return values are returned
    push_register rax

    mov                 rdi, rsp

    alloc_stack         0x20

    // First float return register
    movdqa              [rsp], xmm0
    // Second float return register
    movdqa              [rsp+0x10], xmm1

    END_PROLOGUE

    call                C_FUNC(OnHijackScalarWorker)

    movdqa              xmm0, [rsp]
    movdqa              xmm1, [rsp+0x10]
    free_stack          0x20
    pop_register        rax

    POP_CALLEE_SAVED_REGISTERS
    ret

NESTED_END OnHijackScalarTripThread, _TEXT

//------------------------------------------------
// OnHijackObjectTripThread
//
NESTED_ENTRY OnHijackObjectTripThread, _TEXT, NoHandler 

    // Make room for the real return address (rip)
    push_register rax

    PUSH_CALLEE_SAVED_REGISTERS

    // Push rax again - this is where integer/pointer return values are returned
    push_register rax

    mov                 rdi, rsp

    END_PROLOGUE

    call                C_FUNC(OnHijackObjectWorker)

    pop_register        rax

    POP_CALLEE_SAVED_REGISTERS
    ret

NESTED_END OnHijackObjectTripThread, _TEXT

//------------------------------------------------
// OnHijackInteriorPointerTripThread
//
NESTED_ENTRY OnHijackInteriorPointerTripThread, _TEXT, NoHandler 

    // Make room for the real return address (rip)
    push_register rax

    PUSH_CALLEE_SAVED_REGISTERS

    // Push rax again - this is where integer/pointer return values are returned
    push_register rax

    mov                 rdi, rsp

    END_PROLOGUE

    call                C_FUNC(OnHijackInteriorPointerWorker)

    pop_register        rax

    POP_CALLEE_SAVED_REGISTERS
    ret

NESTED_END OnHijackInteriorPointerTripThread, _TEXT

#endif // FEATURE_HIJACK

LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT                                        
                                                                                      
        test    rdi, rdi                                                              
        jz      NullObject                                                            
                                                                                      
                                                                                      
        mov     rax, [rdi + OFFSETOF__DelegateObject___methodPtr]                     
        mov     rdi, [rdi + OFFSETOF__DelegateObject___target]  // replace "this" pointer
                                                                                      
        jmp     rax                                                                   
                                                                                      
NullObject:                                                                           
        mov     rdi, CORINFO_NullReferenceException_ASM                               
        jmp     C_FUNC(JIT_InternalThrow)
                                                                                      
LEAF_END SinglecastDelegateInvokeStub, _TEXT

//////////////////////////////////////////////////////////////////////////
//
// This function initiates unwinding of native frames during the unwinding of a managed 
// exception. The managed exception can be propagated over several managed / native ranges 
// until it is finally handled by a managed handler or leaves the stack unhandled and
// aborts the current process.
// It creates a stack frame right below the target frame, restores all callee saved registers
// from the passed in context and finally sets the RSP to that frame and sets the return
// address to the target frame's RIP.
//
// EXTERN_C void StartUnwindingNativeFrames(CONTEXT* context, PAL_SEHException* ex);
LEAF_ENTRY StartUnwindingNativeFrames, _TEXT
        // Save the RBP to the stack so that the unwind can work at the instruction after
        // loading the RBP from the context, but before loading the RSP from the context.
        push_nonvol_reg rbp
        mov     r12, [rdi + OFFSETOF__CONTEXT__R12]
        mov     r13, [rdi + OFFSETOF__CONTEXT__R13]
        mov     r14, [rdi + OFFSETOF__CONTEXT__R14]
        mov     r15, [rdi + OFFSETOF__CONTEXT__R15]
        mov     rbx, [rdi + OFFSETOF__CONTEXT__Rbx]
        mov     rbp, [rdi + OFFSETOF__CONTEXT__Rbp]
        mov     rsp, [rdi + OFFSETOF__CONTEXT__Rsp]
        // The RSP was set to the target frame's value, so the current function's
        // CFA is now right at the RSP.
        .cfi_def_cfa_offset 0

        // Indicate that now that we have moved the RSP to the target address, 
        // the RBP is no longer saved in the current stack frame. 
        .cfi_restore rbp

        mov     rax, [rdi + OFFSETOF__CONTEXT__Rip]

        // Store return address to the stack
        push_register rax
        push_nonvol_reg rbp
        // The PAL_SEHException pointer
        mov     rdi, rsi 
        call    EXTERNAL_C_FUNC(ThrowExceptionHelper)
LEAF_END StartUnwindingNativeFrames, _TEXT