0.9.4.53:
[sbcl.git] / src / runtime / linux-os.c
1 /*
2  * the Linux incarnation of OS-dependent routines.  See also
3  * $(sbcl_arch)-linux-os.c
4  *
5  * This file (along with os.h) exports an OS-independent interface to
6  * the operating system VM facilities. Surprise surprise, this
7  * interface looks a lot like the Mach interface (but simpler in some
8  * places). For some operating systems, a subset of these functions
9  * will have to be emulated.
10  */
11
12 /*
13  * This software is part of the SBCL system. See the README file for
14  * more information.
15  *
16  * This software is derived from the CMU CL system, which was
17  * written at Carnegie Mellon University and released into the
18  * public domain. The software is in the public domain and is
19  * provided with absolutely no warranty. See the COPYING and CREDITS
20  * files for more information.
21  */
22
23 #include <stdio.h>
24 #include <sys/param.h>
25 #include <sys/file.h>
26 #include "sbcl.h"
27 #include "./signal.h"
28 #include "os.h"
29 #include "arch.h"
30 #include "globals.h"
31 #include "interrupt.h"
32 #include "interr.h"
33 #include "lispregs.h"
34 #include "runtime.h"
35 #include "genesis/static-symbols.h"
36 #include "genesis/fdefn.h"
37 #include <sys/socket.h>
38 #include <sys/utsname.h>
39
40 #include <sys/types.h>
41 #include <signal.h>
42 /* #include <sys/sysinfo.h> */
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <linux/version.h>
47
48 #include "validate.h"
49 #include "thread.h"
50 #include "gc.h"
51 #if defined LISP_FEATURE_GENCGC
52 #include "gencgc-internal.h"
53 #endif
54
55 #ifdef LISP_FEATURE_LINUX
56 #include <sys/personality.h>
57 #endif
58
59 size_t os_vm_page_size;
60
61 #ifdef LISP_FEATURE_SB_THREAD
62 #include <sys/syscall.h>
63 #include <unistd.h>
64 #include <errno.h>
65
66 /* values taken from the kernel's linux/futex.h.  This header file
67    doesn't exist in userspace, which is our excuse for not grovelling
68    them automatically */
69 #define FUTEX_WAIT (0)
70 #define FUTEX_WAKE (1)
71 #define FUTEX_FD (2)
72 #define FUTEX_REQUEUE (3)
73
74 #define sys_futex sbcl_sys_futex
75 static inline int sys_futex (void *futex, int op, int val, struct timespec *rel)
76 {
77     return syscall (SYS_futex, futex, op, val, rel);
78 }
79
80 int
81 futex_wait(int *lock_word, int oldval)
82 {
83     int t= sys_futex(lock_word,FUTEX_WAIT,oldval, 0);
84     return t;
85 }
86
87 int
88 futex_wake(int *lock_word, int n)
89 {
90     return sys_futex(lock_word,FUTEX_WAKE,n,0);
91 }
92 #endif
93
94 \f
95 int linux_sparc_siginfo_bug = 0;
96 int linux_no_threads_p = 0;
97
98 #ifdef LISP_FEATURE_SB_THREAD
99 int isnptl (void)
100 {
101   size_t n = confstr (_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
102   if (n > 0)
103     {
104       char *buf = alloca (n);
105       confstr (_CS_GNU_LIBPTHREAD_VERSION, buf, n);
106       if (strstr (buf, "NPTL"))
107         return 1;
108     }
109   return 0;
110 }
111 #endif
112
113 void
114 os_init(char *argv[], char *envp[])
115 {
116     /* Conduct various version checks: do we have enough mmap(), is
117      * this a sparc running 2.2, can we do threads? */
118 #ifdef LISP_FEATURE_SB_THREAD
119     int *futex=0;
120 #endif
121     struct utsname name;
122     int major_version;
123     int minor_version;
124     char *p;
125     uname(&name);
126     p=name.release;
127     major_version = atoi(p);
128     p=strchr(p,'.')+1;
129     minor_version = atoi(p);
130     if (major_version<2) {
131         lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
132              major_version);
133     }
134     if (!(major_version>2 || minor_version >= 4)) {
135 #ifdef LISP_FEATURE_SPARC
136         FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
137         linux_sparc_siginfo_bug = 1;
138 #endif
139     }
140 #ifdef LISP_FEATURE_SB_THREAD
141     futex_wait(futex,-1);
142     if(errno==ENOSYS) {
143        lose("This version of sbcl is compiled with threading support, but your kernel is too old to support this.\n\
144 Please use a more recent kernel or a version of sbcl without threading support.\n");
145     }
146     if(! isnptl()) {
147        lose("This version of sbcl only works correctly with the NPTL threading library. Please use a newer glibc, older sbcl or stop using LD_ASSUME_KERNEL");
148     }
149 #endif
150     os_vm_page_size = getpagesize();
151
152     /* KLUDGE: Disable memory randomization on new Linux kernels
153      * by setting a personality flag and re-executing. (We need
154      * to re-execute, since the memory maps that can conflict with
155      * the SBCL spaces have already been done at this point).
156      */
157 #if defined(LISP_FEATURE_X86)
158     if ((major_version == 2 && minor_version >= 6)
159         || major_version >= 3)
160      {
161        long pers = personality(-1);
162        /* 0x40000 aka. ADDR_NO_RANDOMIZE */
163        if (!(pers & 0x40000)) {
164           if (personality(pers | 0x40000) != -1) {
165               /* Use /proc/self/exe instead of trying to figure out the
166                * executable path from PATH and argv[0], since that's
167                * unreliable. We follow the symlink instead of executing
168                * the file directly to avoid top from displaying the
169                * name of the process as "exe".
170                */
171               char runtime[PATH_MAX+1];
172               int i = readlink("/proc/self/exe", runtime, PATH_MAX);
173               if (i != -1) {
174                   runtime[i] = '\0';
175                   execve(runtime, argv, envp);
176               }
177           }
178           /* Either changing the personality or execve() failed. Either
179            * way we might as well continue, and hope that the random
180            * memory maps are ok this time around.
181            */
182           fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
183        }
184     }
185 #endif
186 }
187
188
189 #ifdef LISP_FEATURE_ALPHA
190 /* The Alpha is a 64 bit CPU.  SBCL is a 32 bit application.  Due to all
191  * the places that assume we can get a pointer into a fixnum with no
192  * information loss, we have to make sure it allocates all its ram in the
193  * 0-2Gb region.  */
194
195 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
196 #endif
197
198 os_vm_address_t
199 os_validate(os_vm_address_t addr, os_vm_size_t len)
200 {
201     int flags =  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
202     os_vm_address_t actual;
203
204 #ifdef LISP_FEATURE_ALPHA
205     if (!addr) {
206         addr=under_2gb_free_pointer;
207     }
208 #endif
209     actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
210     if (actual == MAP_FAILED || (addr && (addr!=actual))) {
211         perror("mmap");
212         return 0;               /* caller should check this */
213     }
214
215 #ifdef LISP_FEATURE_ALPHA
216
217     len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
218     under_2gb_free_pointer+=len;
219 #endif
220
221     return actual;
222 }
223
224 void
225 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
226 {
227     if (munmap(addr,len) == -1) {
228         perror("munmap");
229     }
230 }
231
232 os_vm_address_t
233 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
234 {
235     os_vm_address_t actual;
236
237     actual = mmap(addr, len, OS_VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
238                   fd, (off_t) offset);
239     if (actual == MAP_FAILED || (addr && (addr != actual))) {
240         perror("mmap");
241         lose("unexpected mmap(..) failure");
242     }
243
244     return actual;
245 }
246
247 void
248 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
249 {
250     if (mprotect(address, length, prot) == -1) {
251         perror("mprotect");
252     }
253 }
254 \f
255 boolean
256 is_valid_lisp_addr(os_vm_address_t addr)
257 {
258     struct thread *th;
259     size_t ad = (size_t) addr;
260
261     if ((READ_ONLY_SPACE_START <= ad && ad < READ_ONLY_SPACE_END)
262         || (STATIC_SPACE_START <= ad && ad < STATIC_SPACE_END)
263 #if defined LISP_FEATURE_GENCGC
264         || (DYNAMIC_SPACE_START <= ad && ad < DYNAMIC_SPACE_END)
265 #else
266         || (DYNAMIC_0_SPACE_START <= ad && ad < DYNAMIC_0_SPACE_END)
267         || (DYNAMIC_1_SPACE_START <= ad && ad < DYNAMIC_1_SPACE_END)
268 #endif
269         )
270         return 1;
271     for_each_thread(th) {
272         if((size_t)(th->control_stack_start) <= ad
273            && ad < (size_t)(th->control_stack_end))
274             return 1;
275         if((size_t)(th->binding_stack_start) <= ad
276            && ad < (size_t)(th->binding_stack_start + BINDING_STACK_SIZE))
277             return 1;
278     }
279     return 0;
280 }
281 \f
282 /*
283  * any OS-dependent special low-level handling for signals
284  */
285
286
287 #if defined LISP_FEATURE_GENCGC
288
289 /*
290  * The GENCGC needs to be hooked into whatever signal is raised for
291  * page fault on this OS.
292  */
293 static void
294 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
295 {
296     os_context_t *context = arch_os_get_context(&void_context);
297     void* fault_addr = (void*)info->si_addr;
298     if (!gencgc_handle_wp_violation(fault_addr))
299         if(!handle_guard_page_triggered(context,fault_addr))
300 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
301             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
302 #else
303             interrupt_handle_now(signal, info, context);
304 #endif
305 }
306
307 #else
308
309 static void
310 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
311 {
312     os_context_t *context = arch_os_get_context(&void_context);
313     os_vm_address_t addr = arch_get_bad_addr(signal,info,context);
314
315 #ifdef LISP_FEATURE_ALPHA
316     /* Alpha stuff: This is the end of a pseudo-atomic section during
317        which a signal was received.  We must deal with the pending
318        interrupt (see also interrupt.c, ../code/interrupt.lisp)
319
320        (how we got here: when interrupting, we set bit 63 in reg_ALLOC.
321        At the end of the atomic section we tried to write to reg_ALLOC,
322        got a SIGSEGV (there's nothing mapped there) so ended up here. */
323     if (addr != NULL &&
324         *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
325         *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
326         interrupt_handle_pending(context);
327         return;
328     }
329 #endif
330
331     if(!interrupt_maybe_gc(signal, info, context))
332         if(!handle_guard_page_triggered(context,addr))
333             interrupt_handle_now(signal, info, context);
334 }
335 #endif
336
337 void
338 os_install_interrupt_handlers(void)
339 {
340     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
341                                                  sigsegv_handler);
342 #ifdef LISP_FEATURE_SB_THREAD
343     undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
344                                                  interrupt_thread_handler);
345     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
346                                                  sig_stop_for_gc_handler);
347 #endif
348 }