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