Fix cross build.
[sbcl.git] / src / runtime / x86-64-win32-os.c
blobe8d9da7ca6f787202119513f679e5c6cdc953fe8
1 /*
2 * The x86 Win32 incarnation of arch-dependent OS-dependent routines.
3 * See also "win32-os.c".
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
17 #include <stdio.h>
18 #include <stddef.h>
19 #include <sys/param.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <errno.h>
25 #include "os.h"
26 #include "arch.h"
27 #include "globals.h"
28 #include "interrupt.h"
29 #include "interr.h"
30 #include "lispregs.h"
31 #include "genesis/sbcl.h"
33 #include <sys/types.h>
34 #include "runtime.h"
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include "thread.h" /* dynamic_values_bytes */
39 #include "align.h"
41 #include "validate.h"
43 int arch_os_thread_init(struct thread *thread)
45 #ifndef LISP_FEATURE_OS_THREAD_STACK
46 void *cur_stack_end;
47 MEMORY_BASIC_INFORMATION stack_memory;
49 asm volatile ("mov %%gs:8,%0": "=r" (cur_stack_end));
51 /* Can't pull stack start from fs:4 or fs:8 or whatever,
52 * because that's only what currently has memory behind
53 * it from being used, so do a quick VirtualQuery() and
54 * grab the AllocationBase. -AB 2006/11/25
57 /* This memset is probably not really necessary but it shuts up a warning
58 * about uninitialized memory. Compiler might be confused that stack_memory
59 * is both the address being queried, and the address of the result */
60 memset(&stack_memory, 0, sizeof(stack_memory));
61 if (!VirtualQuery(&stack_memory, &stack_memory, sizeof(stack_memory))) {
62 fprintf(stderr, "VirtualQuery: 0x%lx.\n", GetLastError());
63 lose("Could not query stack memory information.");
66 thread->control_stack_start = stack_memory.AllocationBase;
67 thread->control_stack_end = cur_stack_end;
68 #endif
70 extern void win32_set_stack_guarantee(void);
71 win32_set_stack_guarantee();
73 return 1;
76 /* free any arch/os-specific resources used by thread, which is now
77 * defunct. Not called on live threads
80 int arch_os_thread_cleanup(struct thread *thread) {
81 return 0;
84 sigset_t *os_context_sigmask_addr(os_context_t *context)
86 return &context->sigmask;
89 void visit_context_registers(void (*proc)(os_context_register_t,void*),
90 os_context_t *context, void* arg)
92 proc(context->win32_context->Rip, arg);
93 proc(context->win32_context->Rax, arg);
94 proc(context->win32_context->Rcx, arg);
95 proc(context->win32_context->Rdx, arg);
96 proc(context->win32_context->Rbx, arg);
97 // don't bother with rsp or rbp
98 proc(context->win32_context->Rsi, arg);
99 proc(context->win32_context->Rdi, arg);
100 proc(context->win32_context->R8, arg);
101 proc(context->win32_context->R9, arg);
102 proc(context->win32_context->R10, arg);
103 proc(context->win32_context->R11, arg);
104 proc(context->win32_context->R12, arg);
105 proc(context->win32_context->R13, arg);
106 proc(context->win32_context->R14, arg);
107 proc(context->win32_context->R15, arg);
110 os_context_register_t *
111 os_context_register_addr(os_context_t *context, int offset)
113 static const size_t offsets[16] = {
114 offsetof(CONTEXT,Rax),
115 offsetof(CONTEXT,Rcx),
116 offsetof(CONTEXT,Rdx),
117 offsetof(CONTEXT,Rbx),
118 offsetof(CONTEXT,Rsp),
119 offsetof(CONTEXT,Rbp),
120 offsetof(CONTEXT,Rsi),
121 offsetof(CONTEXT,Rdi),
122 offsetof(CONTEXT,R8),
123 offsetof(CONTEXT,R9),
124 offsetof(CONTEXT,R10),
125 offsetof(CONTEXT,R11),
126 offsetof(CONTEXT,R12),
127 offsetof(CONTEXT,R13),
128 offsetof(CONTEXT,R14),
129 offsetof(CONTEXT,R15),
131 return (void*)
132 ((offset >= 0 && offset < 16) ?
133 ((char*)(context->win32_context)) + offsets[offset] : 0);
136 os_context_register_t *
137 os_context_sp_addr(os_context_t *context)
139 return (void*)&context->win32_context->Rsp; /* REG_UESP */
142 os_context_register_t *
143 os_context_fp_addr(os_context_t *context)
145 return (void*)&context->win32_context->Rbp; /* REG_EBP */
148 unsigned long
149 os_context_fp_control(os_context_t *context)
151 return ((((context->win32_context->FloatSave.ControlWord) & 0xffff) ^ 0x3f) |
152 (((context->win32_context->FloatSave.StatusWord) & 0xffff) << 16));
155 void
156 os_restore_fp_control(os_context_t *context)
158 asm ("fldcw %0" : : "m" (context->win32_context->FloatSave.ControlWord));
161 os_context_register_t *
162 os_context_float_register_addr(os_context_t *context, int offset)
164 return (os_context_register_t*)&context->win32_context->FloatSave.XmmRegisters[offset];
167 void
168 os_flush_icache(os_vm_address_t address, os_vm_size_t length)