0.9.8.7:
[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         //unsigned long cur_stack_base;
48         //unsigned long cur_stack_end;
49         void *cur_stack_end;
50
51         //asm volatile ("movl %%fs:8,%0": "=r" (cur_stack_base));
52         //      asm volatile ("movl %%fs:4,%0": "=r" (cur_stack_end));
53
54         asm volatile ("movl %%fs:0,%0": "=r" (cur_stack_end));
55
56         //      fprintf(stderr, "#x%08lx #x%08lx.\n", cur_stack_base, cur_stack_end);
57
58         //if (cur_stack_base > thread->control_stack_start) {
59         //    cur_stack_base = thread->control_stack_start;
60         //}
61
62         //if (cur_stack_end < thread->control_stack_end) {
63         //    cur_stack_end = thread->control_stack_end;
64         //}
65
66         //      fprintf(stderr, "#x%08lx #x%08lx.\n", cur_stack_base, cur_stack_end);
67         //fflush(stderr);
68
69         //getchar();
70
71         //asm volatile ("movl %0,%%fs:8": : "r" (cur_stack_base));
72         //asm volatile ("movl %0,%%fs:4": : "r" (cur_stack_end));
73
74         thread->control_stack_end = cur_stack_end;
75     }
76
77 #ifdef LISP_FEATURE_SB_THREAD
78     /* this must be called from a function that has an exclusive lock
79      * on all_threads
80      */
81     struct user_desc ldt_entry = {
82         1, 0, 0, /* index, address, length filled in later */
83         1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
84     };
85     int n;
86     get_spinlock(&modify_ldt_lock,thread);
87     n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
88     /* get next free ldt entry */
89
90     if(n) {
91         u32 *p;
92         for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
93             n++;
94     }
95     ldt_entry.entry_number=n;
96     ldt_entry.base_addr=(unsigned long) thread;
97     ldt_entry.limit=dynamic_values_bytes;
98     ldt_entry.limit_in_pages=0;
99     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
100         modify_ldt_lock=0;
101         /* modify_ldt call failed: something magical is not happening */
102         return -1;
103     }
104     __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
105                           ((n << 3) /* selector number */
106                            + (1 << 2) /* TI set = LDT */
107                            + 3)); /* privilege level */
108     thread->tls_cookie=n;
109     modify_ldt_lock=0;
110
111     if(n<0) return 0;
112 #endif
113
114     return 1;
115 }
116
117 /* free any arch/os-specific resources used by thread, which is now
118  * defunct.  Not called on live threads
119  */
120
121 int arch_os_thread_cleanup(struct thread *thread) {
122     return 0;
123 }
124
125 os_context_register_t *
126 os_context_register_addr(os_context_t *context, int offset)
127 {
128     switch(offset) {
129     case reg_EAX: return &context->Eax;
130     case reg_ECX: return &context->Ecx;
131     case reg_EDX: return &context->Edx;
132     case reg_EBX: return &context->Ebx;
133     case reg_ESP: return &context->Esp;
134     case reg_EBP: return &context->Ebp;
135     case reg_ESI: return &context->Esi;
136     case reg_EDI: return &context->Edi;
137     default: return 0;
138     }
139 }
140
141 os_context_register_t *
142 os_context_pc_addr(os_context_t *context)
143 {
144     return &context->Eip; /*  REG_EIP */
145 }
146
147 os_context_register_t *
148 os_context_sp_addr(os_context_t *context)
149 {
150     return &context->Esp; /* REG_UESP */
151 }
152
153 os_context_register_t *
154 os_context_fp_addr(os_context_t *context)
155 {
156     return &context->Ebp; /* REG_EBP */
157 }
158
159 unsigned long
160 os_context_fp_control(os_context_t *context)
161 {
162     return ((((context->FloatSave.ControlWord) & 0xffff) ^ 0x3f) |
163             (((context->FloatSave.StatusWord) & 0xffff) << 16));
164 }
165
166 void
167 os_restore_fp_control(os_context_t *context)
168 {
169     asm ("fldcw %0" : : "m" (context->FloatSave.ControlWord));
170 }
171
172 void
173 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
174 {
175 }