2 * The x86 Linux incarnation of arch-dependent OS-dependent routines.
3 * See also "linux-os.c".
7 * This software is part of the SBCL system. See the README file for
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.
19 #include <sys/param.h>
21 #include <sys/types.h>
26 #include <sys/ucontext.h>
34 #include "interrupt.h"
38 #include <sys/socket.h>
39 #include <sys/utsname.h>
41 #include <sys/types.h>
43 /* #include <sys/sysinfo.h> */
48 #include <linux/unistd.h>
50 #include <linux/version.h>
51 #include "thread.h" /* dynamic_values_bytes */
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
54 #define user_desc modify_ldt_ldt_s
57 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
60 size_t os_vm_page_size;
62 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
64 /* This is never actually called, but it's great for calling from gdb when
65 * users have thread-related problems that maintainers can't duplicate */
69 int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
70 printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
73 lispobj modify_ldt_lock; /* protect all calls to modify_ldt */
75 int arch_os_thread_init(struct thread *thread) {
77 #ifdef LISP_FEATURE_SB_THREAD
78 /* this must be called from a function that has an exclusive lock
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
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 */
92 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
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) {
101 /* modify_ldt call failed: something magical is not happening */
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;
113 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
114 /* Signal handlers are run on the control stack, so if it is exhausted
115 * we had better use an alternate stack for whatever signal tells us
116 * we've exhausted it */
117 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
119 sigstack.ss_size = 32*SIGSTKSZ;
120 sigaltstack(&sigstack,0);
125 struct thread *debug_get_fs() {
127 __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs) : );
131 /* free any arch/os-specific resources used by thread, which is now
132 * defunct. Not called on live threads
135 int arch_os_thread_cleanup(struct thread *thread) {
136 struct user_desc ldt_entry = {
138 0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
141 ldt_entry.entry_number=thread->tls_cookie;
142 get_spinlock(&modify_ldt_lock,thread);
143 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
145 /* modify_ldt call failed: something magical is not happening */
153 os_context_register_t *
154 os_context_register_addr(os_context_t *context, int offset)
156 #define RCASE(name) case reg_ ## name: return &context->uc_mcontext.gregs[REG_ ## name];
176 return &context->uc_mcontext.gregs[offset/2+4];
179 return &context->uc_mcontext.gregs[offset];
182 os_context_register_t *
183 os_context_pc_addr(os_context_t *context)
185 return &context->uc_mcontext.gregs[REG_RIP]; /* REG_EIP */
188 os_context_register_t *
189 os_context_sp_addr(os_context_t *context)
191 return &context->uc_mcontext.gregs[REG_RSP];
194 os_context_register_t *
195 os_context_fp_addr(os_context_t *context)
197 return &context->uc_mcontext.gregs[REG_RBP];
201 os_context_fp_control(os_context_t *context)
204 return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
205 (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
212 os_context_sigmask_addr(os_context_t *context)
214 return &context->uc_sigmask;
218 os_restore_fp_control(os_context_t *context)
221 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
226 os_flush_icache(os_vm_address_t address, os_vm_size_t length)