2 * The x86 Win32 incarnation of arch-dependent OS-dependent routines.
3 * See also "win32-os.c".
7 * This software is part of the SBCL system. See the README file for
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.
19 #include <sys/param.h>
21 #include <sys/types.h>
29 #include "interrupt.h"
34 #include <sys/types.h>
39 #include "thread.h" /* dynamic_values_bytes */
43 size_t os_vm_page_size;
45 int arch_os_thread_init(struct thread *thread)
48 void *top_exception_frame;
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
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);
88 #ifdef LISP_FEATURE_SB_THREAD
89 /* this must be called from a function that has an exclusive lock
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
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 */
103 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
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) {
112 /* modify_ldt call failed: something magical is not happening */
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;
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) {
136 os_context_register_t *
137 os_context_register_addr(os_context_t *context, int 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;
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 */
171 os_context_fp_control(os_context_t *context)
173 return ((((context->FloatSave.ControlWord) & 0xffff) ^ 0x3f) |
174 (((context->FloatSave.StatusWord) & 0xffff) << 16));
178 os_restore_fp_control(os_context_t *context)
180 asm ("fldcw %0" : : "m" (context->FloatSave.ControlWord));
184 os_flush_icache(os_vm_address_t address, os_vm_size_t length)