0.8.2.39:
[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 "./signal.h"
27 #include "os.h"
28 #include "arch.h"
29 #include "globals.h"
30 #include "interrupt.h"
31 #include "interr.h"
32 #include "lispregs.h"
33 #include "sbcl.h"
34 #include <sys/socket.h>
35 #include <sys/utsname.h>
36
37 #include <sys/types.h>
38 #include <signal.h>
39 /* #include <sys/sysinfo.h> */
40 #include <sys/time.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <linux/version.h>
44
45 #include "validate.h"
46 #include "thread.h"
47 size_t os_vm_page_size;
48
49 #include "gc.h"
50 \f
51 int linux_sparc_siginfo_bug = 0;
52
53 void os_init(void)
54 {
55     /* Conduct various version checks: do we have enough mmap(), is
56      * this a sparc running 2.2, can we do threads? */
57     {
58         struct utsname name;
59         int major_version;
60         int minor_version;
61         char *p;
62         uname(&name);
63         p=name.release;  
64         major_version = atoi(p);
65         p=strchr(p,'.')+1;
66         minor_version = atoi(p);
67         if (major_version<2) {
68             lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
69                  major_version);
70         }
71 #ifdef LISP_FEATURE_SB_THREAD
72         if ((major_version <2) || (major_version==2 && minor_version < 4)) {
73             lose("linux kernel 2.4 required for thread-enabled SBCL");
74         }
75 #endif
76 #ifdef LISP_FEATURE_SPARC
77         if ((major_version <2) || (major_version==2 && minor_version < 4)) {
78             FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
79             linux_sparc_siginfo_bug = 1;
80         }
81 #endif
82     }
83
84     os_vm_page_size = getpagesize();
85     /* This could just as well be in arch_init(), but it's not. */
86 #ifdef __i386__
87     /* FIXME: This used to be here.  However, I have just removed it
88        with no apparent ill effects (it may be that earlier kernels
89        started up a process with a different set of traps, or
90        something?) Find out what this was meant to do, and reenable it
91        or delete it if possible. -- CSR, 2002-07-15 */
92     /* SET_FPU_CONTROL_WORD(0x1372|4|8|16|32);  no interrupts */
93 #endif
94 }
95
96
97 #ifdef LISP_FEATURE_ALPHA
98 /* The Alpha is a 64 bit CPU.  SBCL is a 32 bit application.  Due to all
99  * the places that assume we can get a pointer into a fixnum with no 
100  * information loss, we have to make sure it allocates all its ram in the
101  * 0-2Gb region.  */
102
103 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
104 #endif
105
106 os_vm_address_t
107 os_validate(os_vm_address_t addr, os_vm_size_t len)
108 {
109     int flags =  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
110     os_vm_address_t actual ;
111
112     if (addr) 
113         flags |= MAP_FIXED;
114 #ifdef LISP_FEATURE_ALPHA
115     else {
116         flags |= MAP_FIXED;
117         addr=under_2gb_free_pointer;
118     }
119 #endif  
120     actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
121     if (actual == MAP_FAILED || (addr && (addr!=actual))) {
122         perror("mmap");
123         return 0;               /* caller should check this */
124     }
125
126 #ifdef LISP_FEATURE_ALPHA
127
128     len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
129     under_2gb_free_pointer+=len;
130 #endif
131
132     return actual;
133 }
134
135 void
136 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
137 {
138     if (munmap(addr,len) == -1) {
139         perror("munmap");
140     }
141 }
142
143 os_vm_address_t
144 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
145 {
146     addr = mmap(addr, len,
147                 OS_VM_PROT_ALL,
148                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
149                 fd, (off_t) offset);
150
151     if (addr == MAP_FAILED) {
152         perror("mmap");
153         lose("unexpected mmap(..) failure");
154     }
155
156     return addr;
157 }
158
159 void
160 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
161 {
162     if (mprotect(address, length, prot) == -1) {
163         perror("mprotect");
164     }
165 }
166 \f
167 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
168  * description of a space, we could probably punt this and just do
169  * (FOO_START <= x && x < FOO_END) everywhere it's called. */
170 static boolean
171 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
172 {
173     char* beg = (char*)((long)sbeg);
174     char* end = (char*)((long)sbeg) + slen;
175     char* adr = (char*)a;
176     return (adr >= beg && adr < end);
177 }
178
179 boolean
180 is_valid_lisp_addr(os_vm_address_t addr)
181 {
182     struct thread *th;
183     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
184        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
185        in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE))
186         return 1;
187     for_each_thread(th) {
188         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
189             return 1;
190         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
191             return 1;
192     }
193     return 0;
194 }
195 \f
196 /*
197  * any OS-dependent special low-level handling for signals
198  */
199
200
201 #if defined LISP_FEATURE_GENCGC
202
203 /*
204  * The GENCGC needs to be hooked into whatever signal is raised for
205  * page fault on this OS.
206  */
207 void
208 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
209 {
210     os_context_t *context = arch_os_get_context(&void_context);
211     void* fault_addr = (void*)context->uc_mcontext.cr2;
212     if (!gencgc_handle_wp_violation(fault_addr)) 
213         if(!handle_control_stack_guard_triggered(context,fault_addr))
214             interrupt_handle_now(signal, info, void_context);
215 }
216
217 #else
218
219 static void
220 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
221 {
222     os_context_t *context = arch_os_get_context(&void_context);
223     os_vm_address_t addr;
224
225     addr = arch_get_bad_addr(signal,info,context);
226     if (addr != NULL && 
227         *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
228         
229         /* Alpha stuff: This is the end of a pseudo-atomic section
230          * during which a signal was received.  We must deal with the
231          * pending interrupt (see also interrupt.c,
232          * ../code/interrupt.lisp)
233          */
234         /* (how we got here: when interrupting, we set bit 63 in
235          * reg_Alloc.  At the end of the atomic section we tried to
236          * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
237          * there) so ended up here
238          */
239         *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
240         interrupt_handle_pending(context);
241     } else {
242         if(!interrupt_maybe_gc(signal, info, context))
243             if(!handle_control_stack_guard_triggered(context,addr))
244                 interrupt_handle_now(signal, info, context);
245     }
246 }
247 #endif
248
249 void sigcont_handler(int signal, siginfo_t *info, void *void_context)
250 {
251     /* we need to have a handler installed for this signal so that
252      * sigwaitinfo() for it actually returns at the appropriate time
253      */
254     fprintf(stderr, "Thread %d received stray SIGCONT\n",
255             arch_os_get_current_thread()->pid);
256 }
257
258 void
259 os_install_interrupt_handlers(void)
260 {
261     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
262                                                  sigsegv_handler);
263 #ifdef LISP_FEATURE_SB_THREAD
264     undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
265                                                  handle_rt_signal);
266 #endif
267     undoably_install_low_level_interrupt_handler(SIGCONT,
268                                                  sigcont_handler);
269 }
270