0.9.3.17
[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 size_t os_vm_page_size;
56
57 #ifdef LISP_FEATURE_SB_THREAD
58 #include <linux/unistd.h>
59 #include <errno.h>
60
61 /* values taken from the kernel's linux/futex.h.  This header file
62    doesn't exist in userspace, which is our excuse for not grovelling
63    them automatically */
64 #define FUTEX_WAIT (0)
65 #define FUTEX_WAKE (1)
66 #define FUTEX_FD (2)
67 #define FUTEX_REQUEUE (3)
68
69 #define __NR_sys_futex __NR_futex
70
71 _syscall4(int,sys_futex,
72           int *, futex,
73           int, op,
74           int, val,
75           struct timespec *, rel);
76
77 int
78 futex_wait(int *lock_word, int oldval)
79 {
80     int t= sys_futex(lock_word,FUTEX_WAIT,oldval, 0);
81     return t;
82 }
83
84 int
85 futex_wake(int *lock_word, int n)
86 {
87     return sys_futex(lock_word,FUTEX_WAKE,n,0);
88 }
89 #endif
90
91 \f
92 int linux_sparc_siginfo_bug = 0;
93 int linux_no_threads_p = 0;
94
95 void
96 os_init(void)
97 {
98     /* Conduct various version checks: do we have enough mmap(), is
99      * this a sparc running 2.2, can we do threads? */
100 #ifdef LISP_FEATURE_SB_THREAD
101     int *futex=0;
102 #endif
103     struct utsname name;
104     int major_version;
105     int minor_version;
106     char *p;
107     uname(&name);
108     p=name.release;
109     major_version = atoi(p);
110     p=strchr(p,'.')+1;
111     minor_version = atoi(p);
112     if (major_version<2) {
113         lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
114              major_version);
115     }
116     if (!(major_version>2 || minor_version >= 4)) {
117 #ifdef LISP_FEATURE_SPARC
118         FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
119         linux_sparc_siginfo_bug = 1;
120 #endif
121     }
122 #ifdef LISP_FEATURE_SB_THREAD
123     futex_wait(futex,-1);
124     if(errno==ENOSYS)  linux_no_threads_p = 1;
125     if(linux_no_threads_p)
126         fprintf(stderr,"Linux with NPTL support (e.g. kernel 2.6 or newer) required for \nthread-enabled SBCL.  Disabling thread support.\n\n");
127 #endif
128     os_vm_page_size = getpagesize();
129 }
130
131
132 #ifdef LISP_FEATURE_ALPHA
133 /* The Alpha is a 64 bit CPU.  SBCL is a 32 bit application.  Due to all
134  * the places that assume we can get a pointer into a fixnum with no
135  * information loss, we have to make sure it allocates all its ram in the
136  * 0-2Gb region.  */
137
138 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
139 #endif
140
141 os_vm_address_t
142 os_validate(os_vm_address_t addr, os_vm_size_t len)
143 {
144     int flags =  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
145     os_vm_address_t actual ;
146
147     if (addr)
148         flags |= MAP_FIXED;
149 #ifdef LISP_FEATURE_ALPHA
150     else {
151         flags |= MAP_FIXED;
152         addr=under_2gb_free_pointer;
153     }
154 #endif
155     actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
156     if (actual == MAP_FAILED || (addr && (addr!=actual))) {
157         perror("mmap");
158         return 0;               /* caller should check this */
159     }
160
161 #ifdef LISP_FEATURE_ALPHA
162
163     len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
164     under_2gb_free_pointer+=len;
165 #endif
166
167     return actual;
168 }
169
170 void
171 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
172 {
173     if (munmap(addr,len) == -1) {
174         perror("munmap");
175     }
176 }
177
178 os_vm_address_t
179 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
180 {
181     os_vm_address_t actual;
182
183     actual = mmap(addr, len, OS_VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
184                   fd, (off_t) offset);
185     if (actual == MAP_FAILED || (addr && (addr != actual))) {
186         perror("mmap");
187         lose("unexpected mmap(..) failure");
188     }
189
190     return actual;
191 }
192
193 void
194 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
195 {
196     if (mprotect(address, length, prot) == -1) {
197         perror("mprotect");
198     }
199 }
200 \f
201 boolean
202 is_valid_lisp_addr(os_vm_address_t addr)
203 {
204     struct thread *th;
205     size_t ad = (size_t) addr;
206
207     if ((READ_ONLY_SPACE_START <= ad && ad < READ_ONLY_SPACE_END)
208         || (STATIC_SPACE_START <= ad && ad < STATIC_SPACE_END)
209 #if defined LISP_FEATURE_GENCGC
210         || (DYNAMIC_SPACE_START <= ad && ad < DYNAMIC_SPACE_END)
211 #else
212         || (DYNAMIC_0_SPACE_START <= ad && ad < DYNAMIC_0_SPACE_END)
213         || (DYNAMIC_1_SPACE_START <= ad && ad < DYNAMIC_1_SPACE_END)
214 #endif
215         )
216         return 1;
217     for_each_thread(th) {
218         if((size_t)(th->control_stack_start) <= ad
219            && ad < (size_t)(th->control_stack_end))
220             return 1;
221         if((size_t)(th->binding_stack_start) <= ad
222            && ad < (size_t)(th->binding_stack_start + BINDING_STACK_SIZE))
223             return 1;
224     }
225     return 0;
226 }
227 \f
228 /*
229  * any OS-dependent special low-level handling for signals
230  */
231
232
233 #if defined LISP_FEATURE_GENCGC
234
235 /*
236  * The GENCGC needs to be hooked into whatever signal is raised for
237  * page fault on this OS.
238  */
239 static void
240 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
241 {
242     os_context_t *context = arch_os_get_context(&void_context);
243     void* fault_addr = (void*)info->si_addr;
244     if (!gencgc_handle_wp_violation(fault_addr))
245         if(!handle_guard_page_triggered(context,fault_addr))
246 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
247             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
248 #else
249             interrupt_handle_now(signal, info, context);
250 #endif
251 }
252
253 #else
254
255 static void
256 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
257 {
258     os_context_t *context = arch_os_get_context(&void_context);
259     os_vm_address_t addr = arch_get_bad_addr(signal,info,context);
260
261 #ifdef LISP_FEATURE_ALPHA
262     /* Alpha stuff: This is the end of a pseudo-atomic section during
263        which a signal was received.  We must deal with the pending
264        interrupt (see also interrupt.c, ../code/interrupt.lisp)
265
266        (how we got here: when interrupting, we set bit 63 in reg_ALLOC.
267        At the end of the atomic section we tried to write to reg_ALLOC,
268        got a SIGSEGV (there's nothing mapped there) so ended up here. */
269     if (addr != NULL &&
270         *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
271         *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
272         interrupt_handle_pending(context);
273         return;
274     }
275 #endif
276
277     if(!interrupt_maybe_gc(signal, info, context))
278         if(!handle_guard_page_triggered(context,addr))
279             interrupt_handle_now(signal, info, context);
280 }
281 #endif
282
283 void
284 os_install_interrupt_handlers(void)
285 {
286     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
287                                                  sigsegv_handler);
288 #ifdef LISP_FEATURE_SB_THREAD
289     undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
290                                                  interrupt_thread_handler);
291     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
292                                                  sig_stop_for_gc_handler);
293 #endif
294 }