1.0.22.13: fixed bug 426: nested inline expansion failure
[sbcl/tcr.git] / src / runtime / x86-win32-os.c
blobfc076de1ff7e62fc7be25706c35423d849bcb2d7
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 "./signal.h"
26 #include "os.h"
27 #include "arch.h"
28 #include "globals.h"
29 #include "interrupt.h"
30 #include "interr.h"
31 #include "lispregs.h"
32 #include "sbcl.h"
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <sys/time.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include "thread.h" /* dynamic_values_bytes */
42 #include "validate.h"
43 size_t os_vm_page_size;
45 int arch_os_thread_init(struct thread *thread)
48 void *top_exception_frame;
49 void *cur_stack_end;
50 void *cur_stack_start;
51 MEMORY_BASIC_INFORMATION stack_memory;
53 asm volatile ("movl %%fs:0,%0": "=r" (top_exception_frame));
54 asm volatile ("movl %%fs:4,%0": "=r" (cur_stack_end));
56 /* Can't pull stack start from fs:4 or fs:8 or whatever,
57 * because that's only what currently has memory behind
58 * it from being used, so do a quick VirtualQuery() and
59 * grab the AllocationBase. -AB 2006/11/25
62 if (!VirtualQuery(&stack_memory, &stack_memory, sizeof(stack_memory))) {
63 fprintf(stderr, "VirtualQuery: 0x%lx.\n", GetLastError());
64 lose("Could not query stack memory information.");
66 cur_stack_start = stack_memory.AllocationBase;
68 /* We use top_exception_frame rather than cur_stack_end to
69 * elide the last few (boring) stack entries at the bottom of
70 * the backtrace.
72 thread->control_stack_start = cur_stack_start;
73 thread->control_stack_end = top_exception_frame;
75 #ifndef LISP_FEATURE_SB_THREAD
77 * Theoretically, threaded SBCL binds directly against
78 * the thread structure for these values. We don't do
79 * threads yet, but we'll probably do the same. We do
80 * need to reset these, though, because they were
81 * initialized based on the wrong stack space.
83 SetSymbolValue(CONTROL_STACK_START,(lispobj)thread->control_stack_start,thread);
84 SetSymbolValue(CONTROL_STACK_END,(lispobj)thread->control_stack_end,thread);
85 #endif
88 #ifdef LISP_FEATURE_SB_THREAD
89 /* this must be called from a function that has an exclusive lock
90 * on all_threads
92 struct user_desc ldt_entry = {
93 1, 0, 0, /* index, address, length filled in later */
94 1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
96 int n;
97 get_spinlock(&modify_ldt_lock,thread);
98 n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
99 /* get next free ldt entry */
101 if(n) {
102 u32 *p;
103 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
104 n++;
106 ldt_entry.entry_number=n;
107 ldt_entry.base_addr=(unsigned long) thread;
108 ldt_entry.limit=dynamic_values_bytes;
109 ldt_entry.limit_in_pages=0;
110 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
111 modify_ldt_lock=0;
112 /* modify_ldt call failed: something magical is not happening */
113 return -1;
115 __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
116 ((n << 3) /* selector number */
117 + (1 << 2) /* TI set = LDT */
118 + 3)); /* privilege level */
119 thread->tls_cookie=n;
120 modify_ldt_lock=0;
122 if(n<0) return 0;
123 #endif
125 return 1;
128 /* free any arch/os-specific resources used by thread, which is now
129 * defunct. Not called on live threads
132 int arch_os_thread_cleanup(struct thread *thread) {
133 return 0;
136 os_context_register_t *
137 os_context_register_addr(os_context_t *context, int offset)
139 switch(offset) {
140 case reg_EAX: return &context->Eax;
141 case reg_ECX: return &context->Ecx;
142 case reg_EDX: return &context->Edx;
143 case reg_EBX: return &context->Ebx;
144 case reg_ESP: return &context->Esp;
145 case reg_EBP: return &context->Ebp;
146 case reg_ESI: return &context->Esi;
147 case reg_EDI: return &context->Edi;
148 default: return 0;
152 os_context_register_t *
153 os_context_pc_addr(os_context_t *context)
155 return &context->Eip; /* REG_EIP */
158 os_context_register_t *
159 os_context_sp_addr(os_context_t *context)
161 return &context->Esp; /* REG_UESP */
164 os_context_register_t *
165 os_context_fp_addr(os_context_t *context)
167 return &context->Ebp; /* REG_EBP */
170 unsigned long
171 os_context_fp_control(os_context_t *context)
173 return ((((context->FloatSave.ControlWord) & 0xffff) ^ 0x3f) |
174 (((context->FloatSave.StatusWord) & 0xffff) << 16));
177 void
178 os_restore_fp_control(os_context_t *context)
180 asm ("fldcw %0" : : "m" (context->FloatSave.ControlWord));
183 void
184 os_flush_icache(os_vm_address_t address, os_vm_size_t length)