| 1 |
/** |
| 2 |
* This file has no copyright assigned and is placed in the Public Domain. |
| 3 |
* This file is part of the mingw-w64 runtime package. |
| 4 |
* No warranty is given; refer to the file DISCLAIMER.PD within this package. |
| 5 |
*/ |
| 6 |
|
| 7 |
#undef __MSVCRT_VERSION__ |
| 8 |
#define _UCRT |
| 9 |
|
| 10 |
#include <setjmp.h> |
| 11 |
#include <windef.h> |
| 12 |
#include <winbase.h> |
| 13 |
|
| 14 |
static inline UINT fpcsr_to_mxcsr( UINT fpcr, UINT fpsr ) |
| 15 |
{ |
| 16 |
UINT ret = 0; |
| 17 |
|
| 18 |
if (fpsr & 0x0001) ret |= 0x0001; /* invalid operation */ |
| 19 |
if (fpsr & 0x0002) ret |= 0x0004; /* zero-divide */ |
| 20 |
if (fpsr & 0x0004) ret |= 0x0008; /* overflow */ |
| 21 |
if (fpsr & 0x0008) ret |= 0x0010; /* underflow */ |
| 22 |
if (fpsr & 0x0010) ret |= 0x0020; /* precision */ |
| 23 |
if (fpsr & 0x0080) ret |= 0x0002; /* denormal */ |
| 24 |
|
| 25 |
if (fpcr & 0x0080000) ret |= 0x0040; /* denormals are zero */ |
| 26 |
if (!(fpcr & 0x0000100)) ret |= 0x0080; /* invalid operation mask */ |
| 27 |
if (!(fpcr & 0x0000200)) ret |= 0x0200; /* zero-divide mask */ |
| 28 |
if (!(fpcr & 0x0000400)) ret |= 0x0400; /* overflow mask */ |
| 29 |
if (!(fpcr & 0x0000800)) ret |= 0x0800; /* underflow mask */ |
| 30 |
if (!(fpcr & 0x0001000)) ret |= 0x1000; /* precision mask */ |
| 31 |
if (!(fpcr & 0x0008000)) ret |= 0x0100; /* denormal mask */ |
| 32 |
if (fpcr & 0x0400000) ret |= 0x4000; /* round up */ |
| 33 |
if (fpcr & 0x0800000) ret |= 0x2000; /* round down */ |
| 34 |
if (fpcr & 0x1000000) ret |= 0x8000; /* flush to zero */ |
| 35 |
return ret; |
| 36 |
} |
| 37 |
|
| 38 |
/* unwind context by one call frame */ |
| 39 |
static void unwind_one_frame( CONTEXT *context ) |
| 40 |
{ |
| 41 |
void *data; |
| 42 |
ULONG_PTR base, frame, pc = context->Rip - 4; |
| 43 |
RUNTIME_FUNCTION *func = RtlLookupFunctionEntry( pc, &base, NULL ); |
| 44 |
|
| 45 |
RtlVirtualUnwind( UNW_FLAG_NHANDLER, base, pc, func, context, &data, &frame, NULL ); |
| 46 |
} |
| 47 |
|
| 48 |
/* fixup jump buffer information; helper for _setjmpex */ |
| 49 |
static int __attribute__((used)) do_setjmpex( _JUMP_BUFFER *buf, UINT fpcr, UINT fpsr ) |
| 50 |
{ |
| 51 |
CONTEXT context = { .ContextFlags = CONTEXT_FULL }; |
| 52 |
|
| 53 |
buf->MxCsr = fpcsr_to_mxcsr( fpcr, fpsr ); |
| 54 |
buf->FpCsr = 0x27f; |
| 55 |
|
| 56 |
/* If the caller is x64, the buffer contains an entry thunk capture. |
| 57 |
* Attempt to unwind it to retrieve the actual x64 context, if applicable. */ |
| 58 |
context.Rbx = buf->Rbx; |
| 59 |
context.Rsp = buf->Rsp; |
| 60 |
context.Rbp = buf->Rbp; |
| 61 |
context.Rsi = buf->Rsi; |
| 62 |
context.Rdi = buf->Rdi; |
| 63 |
context.R12 = buf->R12; |
| 64 |
context.R13 = buf->R13; |
| 65 |
context.R14 = buf->R14; |
| 66 |
context.R15 = buf->R15; |
| 67 |
context.Rip = buf->Rip; |
| 68 |
memcpy( &context.Xmm6, &buf->Xmm6, 10 * sizeof(context.Xmm6) ); |
| 69 |
unwind_one_frame( &context ); |
| 70 |
if (!RtlIsEcCode( context.Rip )) /* caller is x64, use its context instead of the ARM one */ |
| 71 |
{ |
| 72 |
buf->Rbx = context.Rbx; |
| 73 |
buf->Rsp = context.Rsp; |
| 74 |
buf->Rbp = context.Rbp; |
| 75 |
buf->Rsi = context.Rsi; |
| 76 |
buf->Rdi = context.Rdi; |
| 77 |
buf->R12 = context.R12; |
| 78 |
buf->R13 = context.R13; |
| 79 |
buf->R14 = context.R14; |
| 80 |
buf->R15 = context.R15; |
| 81 |
buf->Rip = context.Rip; |
| 82 |
memcpy( &buf->Xmm6, &context.Xmm6, 10 * sizeof(context.Xmm6) ); |
| 83 |
} |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
int __attribute__((naked)) __intrinsic_setjmpex( jmp_buf buf, void *frame ); |
| 88 |
int __attribute__((naked)) __intrinsic_setjmpex( jmp_buf buf, void *frame ) |
| 89 |
{ |
| 90 |
asm( ".seh_proc \"#__intrinsic_setjmpex\"\n\t" |
| 91 |
".seh_endprologue\n\t" |
| 92 |
"stp x1, x27, [x0]\n\t" /* jmp_buf->Frame,Rbx */ |
| 93 |
"mov x1, sp\n\t" |
| 94 |
"stp x1, x29, [x0, #0x10]\n\t" /* jmp_buf->Rsp,Rbp */ |
| 95 |
"stp x25, x26, [x0, #0x20]\n\t" /* jmp_buf->Rsi,Rdi */ |
| 96 |
"stp x19, x20, [x0, #0x30]\n\t" /* jmp_buf->R12,R13 */ |
| 97 |
"stp x21, x22, [x0, #0x40]\n\t" /* jmp_buf->R14,R15 */ |
| 98 |
"str x30, [x0, #0x50]\n\t" /* jmp_buf->Rip */ |
| 99 |
"stp d8, d9, [x0, #0x80]\n\t" /* jmp_buf->Xmm8,Xmm9 */ |
| 100 |
"stp d10, d11, [x0, #0xa0]\n\t" /* jmp_buf->Xmm10,Xmm11 */ |
| 101 |
"stp d12, d13, [x0, #0xc0]\n\t" /* jmp_buf->Xmm12,Xmm13 */ |
| 102 |
"stp d14, d15, [x0, #0xe0]\n\t" /* jmp_buf->Xmm14,Xmm15 */ |
| 103 |
"mrs x1, fpcr\n\t" |
| 104 |
"mrs x2, fpsr\n\t" |
| 105 |
"b \"#do_setjmpex\"\n\t" |
| 106 |
".seh_endproc" ); |
| 107 |
} |