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