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