0.pre8.28
[sbcl/lichteblau.git] / src / runtime / x86-linux-os.c
blobaa1a0ad3dc74614e5a3fbe3876bc087a8a429dad
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>
24 #include "./signal.h"
25 #include "os.h"
26 #include "arch.h"
27 #include "globals.h"
28 #include "interrupt.h"
29 #include "interr.h"
30 #include "lispregs.h"
31 #include "sbcl.h"
32 #include <sys/socket.h>
33 #include <sys/utsname.h>
35 #include <sys/types.h>
36 #include <signal.h>
37 /* #include <sys/sysinfo.h> */
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <asm/ldt.h>
42 #include <linux/unistd.h>
43 #include <sys/mman.h>
44 #include "thread.h" /* dynamic_values_bytes */
46 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
48 #include "validate.h"
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 */
57 void debug_get_ldt()
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 int arch_os_thread_init(struct thread *thread) {
64 stack_t sigstack;
65 #ifdef LISP_FEATURE_SB_THREAD
66 /* this must be called from a function that has an exclusive lock
67 * on all_threads
69 struct modify_ldt_ldt_s ldt_entry = {
70 1, 0, 0, /* index, address, length filled in later */
71 1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
72 };
73 /* get next free ldt entry */
74 int n=__modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
75 if(n) {
76 u32 *p;
77 for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
78 n++;
80 ldt_entry.entry_number=n;
81 ldt_entry.base_addr=(unsigned long) thread;
82 ldt_entry.limit=dynamic_values_bytes;
83 ldt_entry.limit_in_pages=0;
84 if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0)
85 /* modify_ldt call failed: something magical is not happening */
86 return -1;
87 __asm__ __volatile__ ("movw %w0, %%gs" : : "q"
88 ((n << 3) /* selector number */
89 + (1 << 2) /* TI set = LDT */
90 + 3)); /* privilege level */
91 thread->tls_cookie=n;
92 if(n<0) return 0;
93 #endif
94 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
95 /* Signal handlers are run on the control stack, so if it is exhausted
96 * we had better use an alternate stack for whatever signal tells us
97 * we've exhausted it */
98 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
99 sigstack.ss_flags=0;
100 sigstack.ss_size = 32*SIGSTKSZ;
101 sigaltstack(&sigstack,0);
102 #endif
103 return 1;
106 /* if you can't do something like this (maybe because you're using a
107 * register for thread base that is only available in Lisp code)
108 * you'll just have to find_thread_by_pid(getpid())
110 struct thread *arch_os_get_current_thread() {
111 #ifdef LISP_FEATURE_SB_THREAD
112 register struct thread *me=0;
113 if(all_threads)
114 __asm__ ("movl %%gs:%c1,%0" : "=r" (me)
115 : "i" (offsetof (struct thread,this)));
116 return me;
117 #else
118 return all_threads;
119 #endif
122 /* free any arch/os-specific resources used by thread, which is now
123 * defunct. Not called on live threads
126 int arch_os_thread_cleanup(struct thread *thread) {
127 struct modify_ldt_ldt_s ldt_entry = {
128 0, 0, 0,
129 0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
132 ldt_entry.entry_number=thread->tls_cookie;
133 if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0)
134 /* modify_ldt call failed: something magical is not happening */
135 return 0;
136 return 1;
141 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
142 * <sys/ucontext.h> file to define symbolic names for offsets into
143 * gregs[], but it's conditional on __USE_GNU and not defined, so
144 * we need to do this nasty absolute index magic number thing
145 * instead. */
146 os_context_register_t *
147 os_context_register_addr(os_context_t *context, int offset)
149 switch(offset) {
150 case 0: return &context->uc_mcontext.gregs[11]; /* EAX */
151 case 2: return &context->uc_mcontext.gregs[10]; /* ECX */
152 case 4: return &context->uc_mcontext.gregs[9]; /* EDX */
153 case 6: return &context->uc_mcontext.gregs[8]; /* EBX */
154 case 8: return &context->uc_mcontext.gregs[7]; /* ESP */
155 case 10: return &context->uc_mcontext.gregs[6]; /* EBP */
156 case 12: return &context->uc_mcontext.gregs[5]; /* ESI */
157 case 14: return &context->uc_mcontext.gregs[4]; /* EDI */
158 default: return 0;
160 return &context->uc_mcontext.gregs[offset];
163 os_context_register_t *
164 os_context_pc_addr(os_context_t *context)
166 return &context->uc_mcontext.gregs[14]; /* REG_EIP */
169 os_context_register_t *
170 os_context_sp_addr(os_context_t *context)
172 return &context->uc_mcontext.gregs[17]; /* REG_UESP */
175 os_context_register_t *
176 os_context_fp_addr(os_context_t *context)
178 return &context->uc_mcontext.gregs[6]; /* REG_EBP */
181 unsigned long
182 os_context_fp_control(os_context_t *context)
184 return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
185 (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
188 sigset_t *
189 os_context_sigmask_addr(os_context_t *context)
191 return &context->uc_sigmask;
194 void
195 os_restore_fp_control(os_context_t *context)
197 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
200 void
201 os_flush_icache(os_vm_address_t address, os_vm_size_t length)