0.8.0.78.vector-nil-string.1:
[sbcl.git] / src / runtime / x86-linux-os.c
1 /*
2  * The x86 Linux incarnation of arch-dependent OS-dependent routines.
3  * See also "linux-os.c".
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
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.
15  */
16
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>
24
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>
35
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 <linux/unistd.h>
44 #include <sys/mman.h>
45 #include "thread.h"             /* dynamic_values_bytes */
46
47 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount );
48
49 #include "validate.h"
50 size_t os_vm_page_size;
51
52 u32 local_ldt_copy[LDT_ENTRIES*LDT_ENTRY_SIZE/sizeof(u32)];
53
54 /* XXX this could be conditionally compiled based on some
55  * "debug-friendly" flag.  But it doesn't really make stuff slower,
56  * just the runtime gets fractionally larger */
57
58 void debug_get_ldt()
59
60     int n=modify_ldt (0, local_ldt_copy, sizeof local_ldt_copy);
61     printf("%d bytes in ldt: print/x local_ldt_copy\n", n);
62 }
63
64 lispobj modify_ldt_lock;        /* protect all calls to modify_ldt */
65
66 int arch_os_thread_init(struct thread *thread) {
67     stack_t sigstack;
68 #ifdef LISP_FEATURE_SB_THREAD
69     /* this must be called from a function that has an exclusive lock
70      * on all_threads
71      */
72     struct modify_ldt_ldt_s ldt_entry = {
73         1, 0, 0, /* index, address, length filled in later */
74         1, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 1
75     }; 
76     int n;
77     get_spinlock(&modify_ldt_lock,thread);
78     n=modify_ldt(0,local_ldt_copy,sizeof local_ldt_copy);
79     /* get next free ldt entry */
80
81     if(n) {
82         u32 *p;
83         for(n=0,p=local_ldt_copy;*p;p+=LDT_ENTRY_SIZE/sizeof(u32))
84             n++;
85     }
86     ldt_entry.entry_number=n;
87     ldt_entry.base_addr=(unsigned long) thread;
88     ldt_entry.limit=dynamic_values_bytes;
89     ldt_entry.limit_in_pages=0;
90     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
91         modify_ldt_lock=0;
92         /* modify_ldt call failed: something magical is not happening */
93         return -1;
94     }
95     __asm__ __volatile__ ("movw %w0, %%fs" : : "q" 
96                           ((n << 3) /* selector number */
97                            + (1 << 2) /* TI set = LDT */
98                            + 3)); /* privilege level */
99     thread->tls_cookie=n;
100     modify_ldt_lock=0;
101
102     if(n<0) return 0;
103 #endif
104 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
105     /* Signal handlers are run on the control stack, so if it is exhausted
106      * we had better use an alternate stack for whatever signal tells us
107      * we've exhausted it */
108     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
109     sigstack.ss_flags=0;
110     sigstack.ss_size = 32*SIGSTKSZ;
111     sigaltstack(&sigstack,0);
112 #endif
113     return 1;
114 }
115
116 /* if you can't do something like this (maybe because you're using a 
117  * register for thread base that is only available in Lisp code)
118  * you'll just have to find_thread_by_pid(getpid())
119  */
120 struct thread *arch_os_get_current_thread() {
121 #ifdef LISP_FEATURE_SB_THREAD
122     register struct thread *me=0;
123     if(all_threads)
124         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
125                  : "i" (offsetof (struct thread,this)));
126     return me;
127 #else
128     return all_threads;
129 #endif
130 }
131 struct thread *debug_get_fs() {
132     register u32 fs;
133     __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs)  : );
134     return fs;
135 }
136
137 /* free any arch/os-specific resources used by thread, which is now
138  * defunct.  Not called on live threads
139  */
140
141 int arch_os_thread_cleanup(struct thread *thread) {
142     struct modify_ldt_ldt_s ldt_entry = {
143         0, 0, 0, 
144         0, MODIFY_LDT_CONTENTS_DATA, 0, 0, 0, 0
145     }; 
146
147     ldt_entry.entry_number=thread->tls_cookie;
148     get_spinlock(&modify_ldt_lock,thread);
149     if (modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) {
150         modify_ldt_lock=0;
151         /* modify_ldt call failed: something magical is not happening */
152         return 0;
153     }
154     modify_ldt_lock=0;
155     return 1;
156 }
157
158
159
160 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
161  * <sys/ucontext.h> file to define symbolic names for offsets into
162  * gregs[], but it's conditional on __USE_GNU and not defined, so
163  * we need to do this nasty absolute index magic number thing
164  * instead. */
165 os_context_register_t *
166 os_context_register_addr(os_context_t *context, int offset)
167 {
168     switch(offset) {
169     case  0: return &context->uc_mcontext.gregs[11]; /* EAX */
170     case  2: return &context->uc_mcontext.gregs[10]; /* ECX */
171     case  4: return &context->uc_mcontext.gregs[9]; /* EDX */
172     case  6: return &context->uc_mcontext.gregs[8]; /* EBX */
173     case  8: return &context->uc_mcontext.gregs[7]; /* ESP */
174     case 10: return &context->uc_mcontext.gregs[6]; /* EBP */
175     case 12: return &context->uc_mcontext.gregs[5]; /* ESI */
176     case 14: return &context->uc_mcontext.gregs[4]; /* EDI */
177     default: return 0;
178     }
179     return &context->uc_mcontext.gregs[offset];
180 }
181
182 os_context_register_t *
183 os_context_pc_addr(os_context_t *context)
184 {
185     return &context->uc_mcontext.gregs[14]; /*  REG_EIP */
186 }
187
188 os_context_register_t *
189 os_context_sp_addr(os_context_t *context)
190 {                               
191     return &context->uc_mcontext.gregs[17]; /* REG_UESP */
192 }
193
194 os_context_register_t *
195 os_context_fp_addr(os_context_t *context)
196 {
197     return &context->uc_mcontext.gregs[6]; /* REG_EBP */
198 }
199
200 unsigned long
201 os_context_fp_control(os_context_t *context)
202 {
203     return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
204             (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
205 }
206
207 sigset_t *
208 os_context_sigmask_addr(os_context_t *context)
209 {
210     return &context->uc_sigmask;
211 }
212
213 void
214 os_restore_fp_control(os_context_t *context)
215 {
216     asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
217 }
218
219 void
220 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
221 {
222 }
223