| 1 |
; =========================================================================== |
| 2 |
; Copyright (C) 2011 Thilo Schulz <[email protected]> |
| 3 |
; |
| 4 |
; This file is part of Quake III Arena source code. |
| 5 |
; |
| 6 |
; Quake III Arena source code is free software; you can redistribute it |
| 7 |
; and/or modify it under the terms of the GNU General Public License as |
| 8 |
; published by the Free Software Foundation; either version 2 of the License, |
| 9 |
; or (at your option) any later version. |
| 10 |
; |
| 11 |
; Quake III Arena source code is distributed in the hope that it will be |
| 12 |
; useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
; GNU General Public License for more details. |
| 15 |
; |
| 16 |
; You should have received a copy of the GNU General Public License |
| 17 |
; along with Quake III Arena source code; if not, write to the Free Software |
| 18 |
; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
; =========================================================================== |
| 20 |
|
| 21 |
; Call wrapper for vm_x86 when built with MSVC in 64 bit mode, |
| 22 |
; since MSVC does not support inline x64 assembler code anymore. |
| 23 |
; |
| 24 |
; assumes __fastcall calling convention |
| 25 |
|
| 26 |
.code |
| 27 |
|
| 28 |
; Call to compiled code after setting up the register environment for the VM |
| 29 |
; prototype: |
| 30 |
; uint8_t qvmcall64(int *programStack, int *opStack, intptr_t *instructionPointers, byte *dataBase); |
| 31 |
; |
| 32 |
; This call-stub has its own custom calling convention due to pushing all non-volatile registers |
| 33 |
; to the stack. The game uses set/longjmp which on Windows uses "RtlUnwindEx" to unwind the callstack. |
| 34 |
; This function cannot be unwound by default due to the custom calling convention. |
| 35 |
; To allow unwinding, we need to add custom SEH unwind data to the function. |
| 36 |
|
| 37 |
qvmcall64 PROC FRAME |
| 38 |
push r12 ; push all non-volatile registers to stack |
| 39 |
.pushreg r12 |
| 40 |
push r13 |
| 41 |
.pushreg r13 |
| 42 |
push r14 |
| 43 |
.pushreg r14 |
| 44 |
push r15 |
| 45 |
.pushreg r15 |
| 46 |
push rdi |
| 47 |
.pushreg rdi |
| 48 |
push rsi |
| 49 |
.pushreg rsi |
| 50 |
push rbx |
| 51 |
.pushreg rbx |
| 52 |
push rbp |
| 53 |
.pushreg rbp |
| 54 |
|
| 55 |
; need to save pointer in rcx so we can write back the programData value to caller |
| 56 |
push rcx |
| 57 |
.pushreg rcx |
| 58 |
|
| 59 |
.endprolog ; custom unwind data ends here |
| 60 |
|
| 61 |
; registers r8 and r9 have correct value already thanx to __fastcall |
| 62 |
xor rbx, rbx ; opStackOfs starts out being 0 |
| 63 |
mov rdi, rdx ; opStack |
| 64 |
mov esi, dword ptr [rcx] ; programStack |
| 65 |
|
| 66 |
call qword ptr [r8] ; instructionPointers[0] is also the entry point |
| 67 |
|
| 68 |
pop rcx |
| 69 |
|
| 70 |
mov dword ptr [rcx], esi ; write back the programStack value |
| 71 |
mov al, bl ; return opStack offset |
| 72 |
|
| 73 |
pop rbp ; restore all non-volatile registers after the call |
| 74 |
pop rbx |
| 75 |
pop rsi |
| 76 |
pop rdi |
| 77 |
pop r15 |
| 78 |
pop r14 |
| 79 |
pop r13 |
| 80 |
pop r12 |
| 81 |
|
| 82 |
ret |
| 83 |
qvmcall64 ENDP |
| 84 |
|
| 85 |
end |