]> gitweb.factorcode.org Git - factor.git/blob - vm/os-windows-x86.64.cpp
VM: Fixup cast formatting after clang-format
[factor.git] / vm / os-windows-x86.64.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 typedef unsigned char UBYTE;
6
7 const UBYTE UNW_FLAG_EHANDLER = 0x1;
8
9 struct UNWIND_INFO {
10   UBYTE Version : 3;
11   UBYTE Flags : 5;
12   UBYTE SizeOfProlog;
13   UBYTE CountOfCodes;
14   UBYTE FrameRegister : 4;
15   UBYTE FrameOffset : 4;
16   ULONG ExceptionHandler;
17   ULONG ExceptionData[1];
18 };
19
20 struct seh_data {
21   UNWIND_INFO unwind_info;
22   RUNTIME_FUNCTION func;
23   UBYTE handler[32];
24 };
25
26 void factor_vm::c_to_factor_toplevel(cell quot) {
27   /* The annoying thing about Win64 SEH is that the offsets in
28    * function tables are 32-bit integers, and the exception handler
29    * itself must reside between the start and end pointers, so
30    * we stick everything at the beginning of the code heap and
31    * generate a small trampoline that jumps to the real
32    * exception handler. */
33
34   seh_data* seh_area = (seh_data*)code->seh_area;
35   cell base = code->seg->start;
36
37   /* Should look at generating this with the Factor assembler */
38
39   /* mov rax,0 */
40   seh_area->handler[0] = 0x48;
41   seh_area->handler[1] = 0xb8;
42   seh_area->handler[2] = 0x0;
43   seh_area->handler[3] = 0x0;
44   seh_area->handler[4] = 0x0;
45   seh_area->handler[5] = 0x0;
46   seh_area->handler[6] = 0x0;
47   seh_area->handler[7] = 0x0;
48   seh_area->handler[8] = 0x0;
49   seh_area->handler[9] = 0x0;
50
51   /* jmp rax */
52   seh_area->handler[10] = 0x48;
53   seh_area->handler[11] = 0xff;
54   seh_area->handler[12] = 0xe0;
55
56   /* Store address of exception handler in the operand of the 'mov' */
57   cell handler = (cell)&factor::exception_handler;
58   memcpy(&seh_area->handler[2], &handler, sizeof(cell));
59
60   UNWIND_INFO* unwind_info = &seh_area->unwind_info;
61   unwind_info->Version = 1;
62   unwind_info->Flags = UNW_FLAG_EHANDLER;
63   unwind_info->SizeOfProlog = 0;
64   unwind_info->CountOfCodes = 0;
65   unwind_info->FrameRegister = 0;
66   unwind_info->FrameOffset = 0;
67   unwind_info->ExceptionHandler = (DWORD)((cell)&seh_area->handler[0] - base);
68   unwind_info->ExceptionData[0] = 0;
69
70   RUNTIME_FUNCTION* func = &seh_area->func;
71   func->BeginAddress = 0;
72   func->EndAddress = (DWORD)(code->seg->end - base);
73   func->UnwindData = (DWORD)((cell)&seh_area->unwind_info - base);
74
75   if (!RtlAddFunctionTable(func, 1, base))
76     fatal_error("RtlAddFunctionTable() failed", 0);
77
78   c_to_factor(quot);
79
80   if (!RtlDeleteFunctionTable(func))
81     fatal_error("RtlDeleteFunctionTable() failed", 0);
82 }
83
84 }