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>
28 #include "interrupt.h"
32 #include <sys/socket.h>
33 #include <sys/utsname.h>
35 #include <sys/types.h>
37 /* #include <sys/sysinfo.h> */
42 #include <linux/unistd.h>
44 #include "thread.h" /* dynamic_values_bytes */
46 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
49 size_t os_vm_page_size;
51 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
53 /* XXX this could be conditionally compiled based on some
54 * "debug-friendly" flag. But it doesn't really make stuff slower,
55 * just the runtime gets fractionally larger */
59 int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
60 printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
63 lispobj modify_ldt_lock; /* protect all calls to modify_ldt */
65 int arch_os_thread_init(struct thread *thread) {
67 #ifdef LISP_FEATURE_SB_THREAD
68 /* this must be called from a function that has an exclusive lock
71 struct modify_ldt_ldt_s ldt_entry = {
72 1, 0, 0, /* index, address, length filled in later */
73 1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
75 get_spinlock(&modify_ldt_lock,thread);
77 /* get next free ldt entry */
78 int n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
81 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
84 ldt_entry.entry_number=n;
85 ldt_entry.base_addr=(unsigned long) thread;
86 ldt_entry.limit=dynamic_values_bytes;
87 ldt_entry.limit_in_pages=0;
88 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
90 /* modify_ldt call failed: something magical is not happening */
92 __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
93 ((n << 3) /* selector number */
94 + (1 << 2) /* TI set = LDT */
95 + 3)); /* privilege level */
101 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
102 /* Signal handlers are run on the control stack, so if it is exhausted
103 * we had better use an alternate stack for whatever signal tells us
104 * we've exhausted it */
105 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
107 sigstack.ss_size = 32*SIGSTKSZ;
108 sigaltstack(&sigstack,0);
113 /* if you can't do something like this (maybe because you're using a
114 * register for thread base that is only available in Lisp code)
115 * you'll just have to find_thread_by_pid(getpid())
117 struct thread *arch_os_get_current_thread() {
118 #ifdef LISP_FEATURE_SB_THREAD
119 register struct thread *me=0;
121 __asm__ ("movl %%fs:%c1,%0" : "=r" (me)
122 : "i" (offsetof (struct thread,this)));
129 /* free any arch/os-specific resources used by thread, which is now
130 * defunct. Not called on live threads
133 int arch_os_thread_cleanup(struct thread *thread) {
134 struct modify_ldt_ldt_s ldt_entry = {
136 0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
139 ldt_entry.entry_number=thread->tls_cookie;
140 get_spinlock(&modify_ldt_lock,thread);
141 if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
143 /* modify_ldt call failed: something magical is not happening */
152 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
153 * <sys/ucontext.h> file to define symbolic names for offsets into
154 * gregs[], but it's conditional on __USE_GNU and not defined, so
155 * we need to do this nasty absolute index magic number thing
157 os_context_register_t *
158 os_context_register_addr(os_context_t *context, int offset)
161 case 0: return &context->uc_mcontext.gregs[11]; /* EAX */
162 case 2: return &context->uc_mcontext.gregs[10]; /* ECX */
163 case 4: return &context->uc_mcontext.gregs[9]; /* EDX */
164 case 6: return &context->uc_mcontext.gregs[8]; /* EBX */
165 case 8: return &context->uc_mcontext.gregs[7]; /* ESP */
166 case 10: return &context->uc_mcontext.gregs[6]; /* EBP */
167 case 12: return &context->uc_mcontext.gregs[5]; /* ESI */
168 case 14: return &context->uc_mcontext.gregs[4]; /* EDI */
171 return &context->uc_mcontext.gregs[offset];
174 os_context_register_t *
175 os_context_pc_addr(os_context_t *context)
177 return &context->uc_mcontext.gregs[14]; /* REG_EIP */
180 os_context_register_t *
181 os_context_sp_addr(os_context_t *context)
183 return &context->uc_mcontext.gregs[17]; /* REG_UESP */
186 os_context_register_t *
187 os_context_fp_addr(os_context_t *context)
189 return &context->uc_mcontext.gregs[6]; /* REG_EBP */
193 os_context_fp_control(os_context_t *context)
195 return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
196 (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
200 os_context_sigmask_addr(os_context_t *context)
202 return &context->uc_sigmask;
206 os_restore_fp_control(os_context_t *context)
208 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
212 os_flush_icache(os_vm_address_t address, os_vm_size_t length)