0.8.4.48:
[sbcl/simd.git] / src / runtime / x86-linux-os.c
blobb39df5ad0ea2e7389703d5da73fa14173c800178
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 "./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 #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 "modify-ldt-struct-name.h"
44 #include <linux/unistd.h>
45 #include <sys/mman.h>
46 #include "thread.h" /* dynamic_values_bytes */
48 #ifndef MODIFY_LDT_STRUCT_NAMED_USER_DESC
49 /* old glibc */
50 #define user_desc modify_ldt_ldt_s
51 #endif
53 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
55 #include "validate.h"
56 size_t os_vm_page_size;
58 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
60 /* This is never actually called, but it's great for calling from gdb when
61 * users have thread-related problems that maintainers can't duplicate */
63 void debug_get_ldt()
65 int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
66 printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
69 lispobj modify_ldt_lock; /* protect all calls to modify_ldt */
71 int arch_os_thread_init(struct thread *thread) {
72 stack_t sigstack;
73 #ifdef LISP_FEATURE_SB_THREAD
74 /* this must be called from a function that has an exclusive lock
75 * on all_threads
77 struct user_desc ldt_entry = {
78 1, 0, 0, /* index, address, length filled in later */
79 1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
80 };
81 int n;
82 get_spinlock(&modify_ldt_lock,thread);
83 n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
84 /* get next free ldt entry */
86 if(n) {
87 u32 *p;
88 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
89 n++;
91 ldt_entry.entry_number=n;
92 ldt_entry.base_addr=(unsigned long) thread;
93 ldt_entry.limit=dynamic_values_bytes;
94 ldt_entry.limit_in_pages=0;
95 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
96 modify_ldt_lock=0;
97 /* modify_ldt call failed: something magical is not happening */
98 return -1;
100 __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
101 ((n << 3) /* selector number */
102 + (1 << 2) /* TI set = LDT */
103 + 3)); /* privilege level */
104 thread->tls_cookie=n;
105 modify_ldt_lock=0;
107 if(n<0) return 0;
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 #endif
118 return 1;
121 struct thread *debug_get_fs() {
122 register u32 fs;
123 __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs) : );
124 return fs;
127 /* free any arch/os-specific resources used by thread, which is now
128 * defunct. Not called on live threads
131 int arch_os_thread_cleanup(struct thread *thread) {
132 struct user_desc ldt_entry = {
133 0, 0, 0,
134 0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
137 ldt_entry.entry_number=thread->tls_cookie;
138 get_spinlock(&modify_ldt_lock,thread);
139 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
140 modify_ldt_lock=0;
141 /* modify_ldt call failed: something magical is not happening */
142 return 0;
144 modify_ldt_lock=0;
145 return 1;
150 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
151 * <sys/ucontext.h> file to define symbolic names for offsets into
152 * gregs[], but it's conditional on __USE_GNU and not defined, so
153 * we need to do this nasty absolute index magic number thing
154 * instead. */
155 os_context_register_t *
156 os_context_register_addr(os_context_t *context, int offset)
158 switch(offset) {
159 case reg_EAX: return &context->uc_mcontext.gregs[11];
160 case reg_ECX: return &context->uc_mcontext.gregs[10];
161 case reg_EDX: return &context->uc_mcontext.gregs[9];
162 case reg_EBX: return &context->uc_mcontext.gregs[8];
163 case reg_ESP: return &context->uc_mcontext.gregs[7];
164 case reg_EBP: return &context->uc_mcontext.gregs[6];
165 case reg_ESI: return &context->uc_mcontext.gregs[5];
166 case reg_EDI: return &context->uc_mcontext.gregs[4];
167 default: return 0;
169 return &context->uc_mcontext.gregs[offset];
172 os_context_register_t *
173 os_context_pc_addr(os_context_t *context)
175 return &context->uc_mcontext.gregs[14]; /* REG_EIP */
178 os_context_register_t *
179 os_context_sp_addr(os_context_t *context)
181 return &context->uc_mcontext.gregs[17]; /* REG_UESP */
184 os_context_register_t *
185 os_context_fp_addr(os_context_t *context)
187 return &context->uc_mcontext.gregs[6]; /* REG_EBP */
190 unsigned long
191 os_context_fp_control(os_context_t *context)
193 return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
194 (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
197 sigset_t *
198 os_context_sigmask_addr(os_context_t *context)
200 return &context->uc_sigmask;
203 void
204 os_restore_fp_control(os_context_t *context)
206 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
209 void
210 os_flush_icache(os_vm_address_t address, os_vm_size_t length)