0b15ad260f7dbc9e029a68992da4da3ef374ba00
[sbcl.git] / src / runtime / x86-win32-os.c
1 /*
2  * The x86 Win32 incarnation of arch-dependent OS-dependent routines.
3  * See also "win32-os.c".
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
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.
15  */
16
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>
24
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"
33
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 */
40
41
42 #include "validate.h"
43 size_t os_vm_page_size;
44
45 int arch_os_thread_init(struct thread *thread) {
46     {
47         void *top_exception_frame;
48         void *cur_stack_end;
49         void *cur_stack_start;
50
51         asm volatile ("movl %%fs:0,%0": "=r" (top_exception_frame));
52         asm volatile ("movl %%fs:4,%0": "=r" (cur_stack_end));
53
54         /*
55          * Can't pull stack start from fs:4 or fs:8 or whatever,
56          * because that's only what currently has memory behind
57          * it from being used. Our basic options are to know,
58          * a priori, what the stack size is (1 meg by default)
59          * or to grub the default size out of the executable
60          * header in memory by means of hardcoded addresses and
61          * offsets.
62          *
63          * We'll just assume it's 1 megabyte. Easiest that way.
64          */
65         cur_stack_start = cur_stack_end - 0x100000;
66
67         /*
68          * We use top_exception_frame rather than cur_stack_end
69          * to elide the last few (boring) stack entries at the
70          * bottom of the backtrace.
71          */
72         thread->control_stack_start = cur_stack_start;
73         thread->control_stack_end = top_exception_frame;
74
75 #ifndef LISP_FEATURE_SB_THREAD
76         /*
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.
82          */
83         SetSymbolValue(CONTROL_STACK_START,(lispobj)thread->control_stack_start,thread);
84         SetSymbolValue(CONTROL_STACK_END,(lispobj)thread->control_stack_end,thread);
85 #endif
86     }
87
88 #ifdef LISP_FEATURE_SB_THREAD
89     /* this must be called from a function that has an exclusive lock
90      * on all_threads
91      */
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
95     };
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 */
100
101     if(n) {
102         u32 *p;
103         for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
104             n++;
105     }
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;
114     }
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;
121
122     if(n<0) return 0;
123 #endif
124
125     return 1;
126 }
127
128 /* free any arch/os-specific resources used by thread, which is now
129  * defunct.  Not called on live threads
130  */
131
132 int arch_os_thread_cleanup(struct thread *thread) {
133     return 0;
134 }
135
136 os_context_register_t *
137 os_context_register_addr(os_context_t *context, int offset)
138 {
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;
149     }
150 }
151
152 os_context_register_t *
153 os_context_pc_addr(os_context_t *context)
154 {
155     return &context->Eip; /*  REG_EIP */
156 }
157
158 os_context_register_t *
159 os_context_sp_addr(os_context_t *context)
160 {
161     return &context->Esp; /* REG_UESP */
162 }
163
164 os_context_register_t *
165 os_context_fp_addr(os_context_t *context)
166 {
167     return &context->Ebp; /* REG_EBP */
168 }
169
170 unsigned long
171 os_context_fp_control(os_context_t *context)
172 {
173     return ((((context->FloatSave.ControlWord) & 0xffff) ^ 0x3f) |
174             (((context->FloatSave.StatusWord) & 0xffff) << 16));
175 }
176
177 void
178 os_restore_fp_control(os_context_t *context)
179 {
180     asm ("fldcw %0" : : "m" (context->FloatSave.ControlWord));
181 }
182
183 void
184 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
185 {
186 }