2b1dd2522af94fc3ad7efa1ca5c5d0332f0121c5
[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_X86
56 /* Prototype for personality(2). Done inline here since the header file
57  * for this isn't available on old versions of glibc. */
58 int personality (unsigned long);
59 #endif
60
61 size_t os_vm_page_size;
62
63 #ifdef LISP_FEATURE_SB_THREAD
64 #include <sys/syscall.h>
65 #include <unistd.h>
66 #include <errno.h>
67
68 /* values taken from the kernel's linux/futex.h.  This header file
69    doesn't exist in userspace, which is our excuse for not grovelling
70    them automatically */
71 #define FUTEX_WAIT (0)
72 #define FUTEX_WAKE (1)
73 #define FUTEX_FD (2)
74 #define FUTEX_REQUEUE (3)
75
76 #define sys_futex sbcl_sys_futex
77 static inline int sys_futex (void *futex, int op, int val, struct timespec *rel)
78 {
79     return syscall (SYS_futex, futex, op, val, rel);
80 }
81
82 int
83 futex_wait(int *lock_word, int oldval)
84 {
85     int t= sys_futex(lock_word,FUTEX_WAIT,oldval, 0);
86     return t;
87 }
88
89 int
90 futex_wake(int *lock_word, int n)
91 {
92     return sys_futex(lock_word,FUTEX_WAKE,n,0);
93 }
94 #endif
95
96 \f
97 int linux_sparc_siginfo_bug = 0;
98 int linux_no_threads_p = 0;
99
100 #ifdef LISP_FEATURE_SB_THREAD
101 int
102 isnptl (void)
103 {
104   size_t n = confstr (_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
105   if (n > 0) {
106       char *buf = alloca (n);
107       confstr (_CS_GNU_LIBPTHREAD_VERSION, buf, n);
108       if (strstr (buf, "NPTL")) {
109           return 1;
110       }
111   }
112   return 0;
113 }
114 #endif
115
116 void
117 os_init(char *argv[], char *envp[])
118 {
119     /* Conduct various version checks: do we have enough mmap(), is
120      * this a sparc running 2.2, can we do threads? */
121 #ifdef LISP_FEATURE_SB_THREAD
122     int *futex=0;
123 #endif
124     struct utsname name;
125     int major_version;
126     int minor_version;
127     int patch_version;
128     char *p;
129     uname(&name);
130     p=name.release;
131     major_version = atoi(p);
132     p=strchr(p,'.')+1;
133     minor_version = atoi(p);
134     p=strchr(p,'.')+1;
135     patch_version = atoi(p);
136     if (major_version<2) {
137         lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)\n",
138              major_version);
139     }
140     if (!(major_version>2 || minor_version >= 4)) {
141 #ifdef LISP_FEATURE_SPARC
142         FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
143         linux_sparc_siginfo_bug = 1;
144 #endif
145     }
146 #ifdef LISP_FEATURE_SB_THREAD
147     futex_wait(futex,-1);
148     if(errno==ENOSYS) {
149        lose("This version of SBCL is compiled with threading support, but your kernel\n"
150             "is too old to support this. Please use a more recent kernel or\n"
151             "a version of SBCL without threading support.\n");
152     }
153     if(! isnptl()) {
154        lose("This version of SBCL only works correctly with the NPTL threading\n"
155             "library. Please use a newer glibc, use an older SBCL, or stop using\n"
156             "LD_ASSUME_KERNEL\n");
157     }
158 #endif
159     os_vm_page_size = getpagesize();
160
161     /* KLUDGE: Disable memory randomization on new Linux kernels
162      * by setting a personality flag and re-executing. (We need
163      * to re-execute, since the memory maps that can conflict with
164      * the SBCL spaces have already been done at this point).
165      *
166      * Since randomization is currently implemented only on x86 kernels,
167      * don't do this trick on other platforms.
168      */
169 #ifdef LISP_FEATURE_X86
170     if ((major_version == 2
171          /* Some old kernels will apparently lose unsupported personality flags
172           * on exec() */
173          && ((minor_version == 6 && patch_version >= 11)
174              || (minor_version > 6)))
175         || major_version >= 3)
176     {
177         int pers = personality(0xffffffffUL);
178         /* 0x40000 aka. ADDR_NO_RANDOMIZE */
179         if (!(pers & 0x40000)) {
180             int retval = personality(pers | 0x40000);
181             /* Allegedly some Linux kernels (the reported case was
182              * "hardened Linux 2.6.7") won't set the new personality,
183              * but nor will they return -1 for an error. So as a
184              * workaround query the new personality...
185              */
186             int newpers = personality(0xffffffffUL);
187             /* ... and don't re-execute if either the setting resulted
188              * in an error or if the value didn't change. Otherwise
189              * this might result in an infinite loop.
190              */
191             if (retval != -1 && newpers != pers) {
192                 /* Use /proc/self/exe instead of trying to figure out
193                  * the executable path from PATH and argv[0], since
194                  * that's unreliable. We follow the symlink instead of
195                  * executing the file directly in order to prevent top
196                  * from displaying the name of the process as "exe". */
197                 char runtime[PATH_MAX+1];
198                 int i = readlink("/proc/self/exe", runtime, PATH_MAX);
199                 if (i != -1) {
200                     runtime[i] = '\0';
201                     execve(runtime, argv, envp);
202                 }
203             }
204             /* Either changing the personality or execve() failed. Either
205              * way we might as well continue, and hope that the random
206              * memory maps are ok this time around.
207              */
208             fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
209         }
210     }
211     /* Use SSE detector.  Recent versions of Linux enable SSE support
212      * on SSE capable CPUs.  */
213     /* FIXME: Are there any old versions that does not support SSE?  */
214     fast_bzero_pointer = fast_bzero_detect;
215 #endif
216 }
217
218
219 #ifdef LISP_FEATURE_ALPHA
220 /* The Alpha is a 64 bit CPU.  SBCL is a 32 bit application.  Due to all
221  * the places that assume we can get a pointer into a fixnum with no
222  * information loss, we have to make sure it allocates all its ram in the
223  * 0-2Gb region.  */
224
225 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
226 #endif
227
228 os_vm_address_t
229 os_validate(os_vm_address_t addr, os_vm_size_t len)
230 {
231     int flags =  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
232     os_vm_address_t actual;
233
234 #ifdef LISP_FEATURE_ALPHA
235     if (!addr) {
236         addr=under_2gb_free_pointer;
237     }
238 #endif
239     actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
240     if (actual == MAP_FAILED) {
241         perror("mmap");
242         return 0;               /* caller should check this */
243     }
244
245     if (addr && (addr!=actual)) {
246         fprintf(stderr, "mmap: wanted %lu bytes at %p, actually mapped at %p\n",
247                 (unsigned long) len, addr, actual);
248         return 0;
249     }
250
251 #ifdef LISP_FEATURE_ALPHA
252
253     len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
254     under_2gb_free_pointer+=len;
255 #endif
256
257     return actual;
258 }
259
260 void
261 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
262 {
263     if (munmap(addr,len) == -1) {
264         perror("munmap");
265     }
266 }
267
268 os_vm_address_t
269 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
270 {
271     os_vm_address_t actual;
272
273     actual = mmap(addr, len, OS_VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
274                   fd, (off_t) offset);
275     if (actual == MAP_FAILED || (addr && (addr != actual))) {
276         perror("mmap");
277         lose("unexpected mmap(..) failure\n");
278     }
279
280     return actual;
281 }
282
283 void
284 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
285 {
286     if (mprotect(address, length, prot) == -1) {
287         perror("mprotect");
288     }
289 }
290 \f
291 boolean
292 is_valid_lisp_addr(os_vm_address_t addr)
293 {
294     struct thread *th;
295     size_t ad = (size_t) addr;
296
297     if ((READ_ONLY_SPACE_START <= ad && ad < READ_ONLY_SPACE_END)
298         || (STATIC_SPACE_START <= ad && ad < STATIC_SPACE_END)
299 #if defined LISP_FEATURE_GENCGC
300         || (DYNAMIC_SPACE_START <= ad && ad < DYNAMIC_SPACE_END)
301 #else
302         || (DYNAMIC_0_SPACE_START <= ad && ad < DYNAMIC_0_SPACE_END)
303         || (DYNAMIC_1_SPACE_START <= ad && ad < DYNAMIC_1_SPACE_END)
304 #endif
305         )
306         return 1;
307     for_each_thread(th) {
308         if((size_t)(th->control_stack_start) <= ad
309            && ad < (size_t)(th->control_stack_end))
310             return 1;
311         if((size_t)(th->binding_stack_start) <= ad
312            && ad < (size_t)(th->binding_stack_start + BINDING_STACK_SIZE))
313             return 1;
314     }
315     return 0;
316 }
317 \f
318 /*
319  * any OS-dependent special low-level handling for signals
320  */
321
322 /*
323  * The GC needs to be hooked into whatever signal is raised for
324  * page fault on this OS.
325  */
326 static void
327 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
328 {
329     os_context_t *context = arch_os_get_context(&void_context);
330     os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
331
332 #ifdef LISP_FEATURE_ALPHA
333     /* Alpha stuff: This is the end of a pseudo-atomic section during
334        which a signal was received.  We must deal with the pending
335        interrupt (see also interrupt.c, ../code/interrupt.lisp)
336
337        (how we got here: when interrupting, we set bit 63 in reg_ALLOC.
338        At the end of the atomic section we tried to write to reg_ALLOC,
339        got a SIGSEGV (there's nothing mapped there) so ended up here. */
340     if (addr != NULL &&
341         *os_context_register_addr(context, reg_ALLOC) & (1L<<63)) {
342         *os_context_register_addr(context, reg_ALLOC) -= (1L<<63);
343         interrupt_handle_pending(context);
344         return;
345     }
346 #endif
347
348 #ifdef LISP_FEATURE_GENCGC
349     if (!gencgc_handle_wp_violation(addr))
350 #else
351     if (!interrupt_maybe_gc(signal, info, context))
352 #endif
353         if (!handle_guard_page_triggered(context, addr))
354 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
355             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
356 #else
357             interrupt_handle_now(signal, info, context);
358 #endif
359 }
360
361 void
362 os_install_interrupt_handlers(void)
363 {
364     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
365                                                  sigsegv_handler);
366 #ifdef LISP_FEATURE_SB_THREAD
367     undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
368                                                  interrupt_thread_handler);
369     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
370                                                  sig_stop_for_gc_handler);
371 #endif
372 }