Add safepoint mechanism
[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 <sys/syscall.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 #define modify_ldt sbcl_modify_ldt
53 static inline int modify_ldt (int func, void *ptr, unsigned long bytecount)
54 {
55   return syscall (SYS_modify_ldt, func, ptr, bytecount);
56 }
57
58 #include "validate.h"
59 size_t os_vm_page_size;
60
61 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
62
63 /* This is never actually called, but it's great for calling from gdb when
64  * users have thread-related problems that maintainers can't duplicate */
65
66 void debug_get_ldt()
67 {
68     int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
69     printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
70 }
71
72 #ifdef LISP_FEATURE_SB_THREAD
73 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
74 #endif
75
76 int arch_os_thread_init(struct thread *thread) {
77     stack_t sigstack;
78 #ifdef LISP_FEATURE_SB_THREAD
79     struct user_desc ldt_entry = {
80         1, 0, 0, /* index, address, length filled in later */
81         1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
82     };
83     int n;
84     thread_mutex_lock(&modify_ldt_lock);
85     n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
86     /* get next free ldt entry */
87
88     if(n) {
89         u32 *p;
90         for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
91             n++;
92     }
93     ldt_entry.entry_number=n;
94     ldt_entry.base_addr=(unsigned long) thread;
95     ldt_entry.limit=dynamic_values_bytes;
96     ldt_entry.limit_in_pages=0;
97     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
98         thread_mutex_unlock(&modify_ldt_lock);
99         /* modify_ldt call failed: something magical is not happening */
100         return 0;
101     }
102     __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
103                           ((n << 3) /* selector number */
104                            + (1 << 2) /* TI set = LDT */
105                            + 3)); /* privilege level */
106     thread->tls_cookie=n;
107     pthread_mutex_unlock(&modify_ldt_lock);
108
109     /* now %fs:0 refers to the current thread.  Useful!  Less usefully,
110      * Linux/x86 isn't capable of reporting a faulting si_addr on a
111      * segment as defined above (whereas faults on the segment that %gs
112      * usually points are reported just fine...).  As a special
113      * workaround, we store each thread structure's absolute address as
114      * as slot in itself, so that within the thread,
115      *   movl %fs:SELFPTR_OFFSET,x
116      * stores the absolute address of %fs:0 into x.
117      */
118 #ifdef LISP_FEATURE_SB_SAFEPOINT
119     thread->selfptr = thread;
120 #endif
121
122     if(n<0) return 0;
123 #ifdef LISP_FEATURE_GCC_TLS
124     current_thread = thread;
125 #else
126     pthread_setspecific(specials,thread);
127 #endif
128 #endif
129 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
130     /* Signal handlers are run on the control stack, so if it is exhausted
131      * we had better use an alternate stack for whatever signal tells us
132      * we've exhausted it */
133     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
134     sigstack.ss_flags=0;
135     sigstack.ss_size = 32*SIGSTKSZ;
136     if(sigaltstack(&sigstack,0)<0)
137         lose("Cannot sigaltstack: %s\n",strerror(errno));
138 #endif
139     return 1;
140 }
141
142 struct thread *debug_get_fs() {
143     register u32 fs;
144     __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs)  : );
145     return (struct thread *)fs;
146 }
147
148 /* free any arch/os-specific resources used by thread, which is now
149  * defunct.  Not called on live threads
150  */
151
152 int arch_os_thread_cleanup(struct thread *thread) {
153     struct user_desc ldt_entry = {
154         0, 0, 0,
155         0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
156     };
157     int result;
158
159     ldt_entry.entry_number=thread->tls_cookie;
160     thread_mutex_lock(&modify_ldt_lock);
161     result = modify_ldt(1, &ldt_entry, sizeof (ldt_entry));
162     thread_mutex_unlock(&modify_ldt_lock);
163     return result;
164 }
165
166
167
168 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
169  * <sys/ucontext.h> file to define symbolic names for offsets into
170  * gregs[], but it's conditional on __USE_GNU and not defined, so
171  * we need to do this nasty absolute index magic number thing
172  * instead. */
173 os_context_register_t *
174 os_context_register_addr(os_context_t *context, int offset)
175 {
176     switch(offset) {
177     case reg_EAX: return &context->uc_mcontext.gregs[11];
178     case reg_ECX: return &context->uc_mcontext.gregs[10];
179     case reg_EDX: return &context->uc_mcontext.gregs[9];
180     case reg_EBX: return &context->uc_mcontext.gregs[8];
181     case reg_ESP: return &context->uc_mcontext.gregs[7];
182     case reg_EBP: return &context->uc_mcontext.gregs[6];
183     case reg_ESI: return &context->uc_mcontext.gregs[5];
184     case reg_EDI: return &context->uc_mcontext.gregs[4];
185     default: return 0;
186     }
187     return &context->uc_mcontext.gregs[offset];
188 }
189
190 os_context_register_t *
191 os_context_pc_addr(os_context_t *context)
192 {
193     return &context->uc_mcontext.gregs[14]; /*  REG_EIP */
194 }
195
196 os_context_register_t *
197 os_context_sp_addr(os_context_t *context)
198 {
199     return &context->uc_mcontext.gregs[17]; /* REG_UESP */
200 }
201
202 os_context_register_t *
203 os_context_fp_addr(os_context_t *context)
204 {
205     return &context->uc_mcontext.gregs[6]; /* REG_EBP */
206 }
207
208 unsigned long
209 os_context_fp_control(os_context_t *context)
210 {
211     return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
212             (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
213 }
214
215 sigset_t *
216 os_context_sigmask_addr(os_context_t *context)
217 {
218     return &context->uc_sigmask;
219 }
220
221 void
222 os_restore_fp_control(os_context_t *context)
223 {
224     if (context->uc_mcontext.fpregs)
225         asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
226 }
227
228 void
229 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
230 {
231 }