0.9.2.42:
[sbcl.git] / src / runtime / x86-linux-os.c
1 /*
2  * The x86 Linux incarnation of arch-dependent OS-dependent routines.
3  * See also "linux-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 "sbcl.h"
26 #include "./signal.h"
27 #include "os.h"
28 #include "arch.h"
29 #include "globals.h"
30 #include "interrupt.h"
31 #include "interr.h"
32 #include "lispregs.h"
33 #include <sys/socket.h>
34 #include <sys/utsname.h>
35
36 #include <sys/types.h>
37 #include <signal.h>
38 /* #include <sys/sysinfo.h> */
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <asm/ldt.h>
43 #include <linux/unistd.h>
44 #include <sys/mman.h>
45 #include <linux/version.h>
46 #include "thread.h"             /* dynamic_values_bytes */
47
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
49 #define user_desc  modify_ldt_ldt_s
50 #endif
51
52 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
53
54 #include "validate.h"
55 size_t os_vm_page_size;
56
57 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
58
59 /* This is never actually called, but it's great for calling from gdb when
60  * users have thread-related problems that maintainers can't duplicate */
61
62 void debug_get_ldt()
63 {
64     int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
65     printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
66 }
67
68 volatile lispobj modify_ldt_lock;       /* protect all calls to modify_ldt */
69
70 int arch_os_thread_init(struct thread *thread) {
71     stack_t sigstack;
72 #ifdef LISP_FEATURE_SB_THREAD
73     /* FIXME Lock ordering rules: all_threads_lock must usually be
74      * held when getting modify_ldt_lock
75      */
76     struct user_desc ldt_entry = {
77         1, 0, 0, /* index, address, length filled in later */
78         1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
79     };
80     int n;
81     get_spinlock(&modify_ldt_lock,(long)thread);
82     n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
83     /* get next free ldt entry */
84
85     if(n) {
86         u32 *p;
87         for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
88             n++;
89     }
90     ldt_entry.entry_number=n;
91     ldt_entry.base_addr=(unsigned long) thread;
92     ldt_entry.limit=dynamic_values_bytes;
93     ldt_entry.limit_in_pages=0;
94     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
95         modify_ldt_lock=0;
96         /* modify_ldt call failed: something magical is not happening */
97         return -1;
98     }
99     __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
100                           ((n << 3) /* selector number */
101                            + (1 << 2) /* TI set = LDT */
102                            + 3)); /* privilege level */
103     thread->tls_cookie=n;
104     modify_ldt_lock=0;
105
106     if(n<0) return 0;
107     pthread_setspecific(specials,thread);
108 #endif
109 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
110     /* Signal handlers are run on the control stack, so if it is exhausted
111      * we had better use an alternate stack for whatever signal tells us
112      * we've exhausted it */
113     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
114     sigstack.ss_flags=0;
115     sigstack.ss_size = 32*SIGSTKSZ;
116     sigaltstack(&sigstack,0);
117     if(sigaltstack(&sigstack,0)<0) {
118         lose("Cannot sigaltstack: %s\n",strerror(errno));
119     }
120 #endif
121     return 1;
122 }
123
124 struct thread *debug_get_fs() {
125     register u32 fs;
126     __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs)  : );
127     return fs;
128 }
129
130 /* free any arch/os-specific resources used by thread, which is now
131  * defunct.  Not called on live threads
132  */
133
134 int arch_os_thread_cleanup(struct thread *thread) {
135     struct user_desc ldt_entry = {
136         0, 0, 0,
137         0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
138     };
139
140     ldt_entry.entry_number=thread->tls_cookie;
141     get_spinlock(&modify_ldt_lock,(long)thread);
142     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
143         modify_ldt_lock=0;
144         /* modify_ldt call failed: something magical is not happening */
145         return 0;
146     }
147     modify_ldt_lock=0;
148     return 1;
149 }
150
151
152
153 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
154  * <sys/ucontext.h> file to define symbolic names for offsets into
155  * gregs[], but it's conditional on __USE_GNU and not defined, so
156  * we need to do this nasty absolute index magic number thing
157  * instead. */
158 os_context_register_t *
159 os_context_register_addr(os_context_t *context, int offset)
160 {
161     switch(offset) {
162     case reg_EAX: return &context->uc_mcontext.gregs[11];
163     case reg_ECX: return &context->uc_mcontext.gregs[10];
164     case reg_EDX: return &context->uc_mcontext.gregs[9];
165     case reg_EBX: return &context->uc_mcontext.gregs[8];
166     case reg_ESP: return &context->uc_mcontext.gregs[7];
167     case reg_EBP: return &context->uc_mcontext.gregs[6];
168     case reg_ESI: return &context->uc_mcontext.gregs[5];
169     case reg_EDI: return &context->uc_mcontext.gregs[4];
170     default: return 0;
171     }
172     return &context->uc_mcontext.gregs[offset];
173 }
174
175 os_context_register_t *
176 os_context_pc_addr(os_context_t *context)
177 {
178     return &context->uc_mcontext.gregs[14]; /*  REG_EIP */
179 }
180
181 os_context_register_t *
182 os_context_sp_addr(os_context_t *context)
183 {
184     return &context->uc_mcontext.gregs[17]; /* REG_UESP */
185 }
186
187 os_context_register_t *
188 os_context_fp_addr(os_context_t *context)
189 {
190     return &context->uc_mcontext.gregs[6]; /* REG_EBP */
191 }
192
193 unsigned long
194 os_context_fp_control(os_context_t *context)
195 {
196     return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
197             (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
198 }
199
200 sigset_t *
201 os_context_sigmask_addr(os_context_t *context)
202 {
203     return &context->uc_sigmask;
204 }
205
206 void
207 os_restore_fp_control(os_context_t *context)
208 {
209     asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
210 }
211
212 void
213 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
214 {
215 }
216