0.9.9.36:
[sbcl.git] / src / runtime / bsd-os.c
1 /*
2  * OS-dependent routines for BSD-ish systems
3  *
4  * This file (along with os.h) exports an OS-independent interface to
5  * the operating system VM facilities. This interface looks a lot like
6  * the Mach interface (but simpler in some places). For some operating
7  * systems, a subset of these functions will have to be emulated.
8  */
9
10 /*
11  * This software is part of the SBCL system. See the README file for
12  * more information.
13  *
14  * This software is derived from the CMU CL system, which was
15  * written at Carnegie Mellon University and released into the
16  * public domain. The software is in the public domain and is
17  * provided with absolutely no warranty. See the COPYING and CREDITS
18  * files for more information.
19  */
20
21 #include <stdio.h>
22 #include <sys/param.h>
23 #include <sys/file.h>
24 #include <unistd.h>
25 #include <assert.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 "thread.h"
35 #include "runtime.h"
36 #include "genesis/static-symbols.h"
37 #include "genesis/fdefn.h"
38
39 #include <sys/types.h>
40 #include <signal.h>
41 /* #include <sys/sysinfo.h> */
42 #include "validate.h"
43 \f
44 os_vm_size_t os_vm_page_size;
45
46 #ifdef __NetBSD__
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <string.h>
50
51 static void netbsd_init();
52 #endif /* __NetBSD__ */
53
54 #ifdef __FreeBSD__
55 #include <sys/sysctl.h>
56
57 static void freebsd_init();
58 #endif /* __FreeBSD__ */
59
60 void
61 os_init(char *argv[], char *envp[])
62 {
63     os_vm_page_size = getpagesize();
64
65 #ifdef __NetBSD__
66     netbsd_init();
67 #endif /* __NetBSD__ */
68 #ifdef __FreeBSD__
69     freebsd_init();
70 #endif /* __FreeBSD__ */
71 }
72
73 int *os_context_pc_addr(os_context_t *context)
74 {
75 #if defined __FreeBSD__
76     return CONTEXT_ADDR_FROM_STEM(eip);
77 #elif defined __OpenBSD__
78     return CONTEXT_ADDR_FROM_STEM(pc);
79 #elif defined __NetBSD__
80     return CONTEXT_ADDR_FROM_STEM(EIP);
81 #elif defined LISP_FEATURE_DARWIN
82     return &context->uc_mcontext->ss.srr0;
83 #else
84 #error unsupported BSD variant
85 #endif
86 }
87
88 sigset_t *
89 os_context_sigmask_addr(os_context_t *context)
90 {
91     /* (Unlike most of the other context fields that we access, the
92      * signal mask field is a field of the basic, outermost context
93      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
94 #if defined __FreeBSD__  || __NetBSD__ || defined LISP_FEATURE_DARWIN
95     return &context->uc_sigmask;
96 #elif defined __OpenBSD__
97     return &context->sc_mask;
98 #else
99 #error unsupported BSD variant
100 #endif
101 }
102
103 os_vm_address_t
104 os_validate(os_vm_address_t addr, os_vm_size_t len)
105 {
106     int flags = MAP_PRIVATE | MAP_ANON;
107
108     if (addr)
109         flags |= MAP_FIXED;
110
111     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
112
113     if (addr == MAP_FAILED) {
114         perror("mmap");
115         return NULL;
116     }
117
118     return addr;
119 }
120
121 void
122 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
123 {
124     if (munmap(addr, len) == -1)
125         perror("munmap");
126 }
127
128 os_vm_address_t
129 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
130 {
131     addr = mmap(addr, len,
132                 OS_VM_PROT_ALL,
133                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
134                 fd, (off_t) offset);
135
136     if (addr == MAP_FAILED) {
137         perror("mmap");
138         lose("unexpected mmap(..) failure\n");
139     }
140
141     return addr;
142 }
143
144 void
145 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
146 {
147     if (mprotect(address, length, prot) == -1) {
148         perror("mprotect");
149     }
150 }
151 \f
152 static boolean
153 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
154 {
155     char* beg = (char*) sbeg;
156     char* end = (char*) sbeg + slen;
157     char* adr = (char*) a;
158     return (adr >= beg && adr < end);
159 }
160
161 boolean
162 is_valid_lisp_addr(os_vm_address_t addr)
163 {
164     struct thread *th;
165     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
166        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
167        in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE))
168         return 1;
169     for_each_thread(th) {
170         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
171             return 1;
172         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
173             return 1;
174     }
175     return 0;
176 }
177 \f
178 /*
179  * any OS-dependent special low-level handling for signals
180  */
181
182 #if defined LISP_FEATURE_GENCGC
183
184 /*
185  * The GENCGC needs to be hooked into whatever signal is raised for
186  * page fault on this OS.
187  */
188 static void
189 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
190 {
191     os_context_t *context = arch_os_get_context(&void_context);
192     void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
193
194     if (!gencgc_handle_wp_violation(fault_addr))
195         if(!handle_guard_page_triggered(context,fault_addr)) {
196 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
197             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
198 #else
199             if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
200                 interrupt_handle_now(signal, siginfo, context);
201             }
202 #if defined(LISP_FEATURE_DARWIN)
203             /* Work around G5 bug; fix courtesy gbyers */
204             DARWIN_FIX_CONTEXT(context);
205 #endif
206 #endif
207         }
208 }
209
210 void
211 os_install_interrupt_handlers(void)
212 {
213     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
214     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
215                                                  memory_fault_handler);
216 #ifdef SIG_MEMORY_FAULT2
217     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
218                                                  memory_fault_handler);
219 #endif
220     SHOW("leaving os_install_interrupt_handlers()");
221 }
222
223 #else /* Currently Darwin only */
224
225 static void
226 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
227 {
228     os_context_t *context = arch_os_get_context(&void_context);
229     unsigned int pc =  (unsigned int *)(*os_context_pc_addr(context));
230     os_vm_address_t addr;
231
232     addr = arch_get_bad_addr(signal,info,context);
233     if(!interrupt_maybe_gc(signal, info, context))
234         if(!handle_guard_page_triggered(context,addr))
235             interrupt_handle_now(signal, info, context);
236     /* Work around G5 bug; fix courtesy gbyers */
237     DARWIN_FIX_CONTEXT(context);
238 }
239
240 void
241 os_install_interrupt_handlers(void)
242 {
243     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
244     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
245                                                  sigsegv_handler);
246 #ifdef SIG_MEMORY_FAULT2
247     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
248                                                  sigsegv_handler);
249 #endif
250 }
251
252 #endif /* defined GENCGC */
253
254 #ifdef __NetBSD__
255 static void netbsd_init()
256 {
257     struct rlimit rl;
258     int mib[2], osrev;
259     size_t len;
260
261     /* Are we running on a sufficiently functional kernel? */
262     mib[0] = CTL_KERN;
263     mib[1] = KERN_OSREV;
264
265     len = sizeof(osrev);
266     sysctl(mib, 2, &osrev, &len, NULL, 0);
267
268     /* If we're older than 2.0... */
269     if (osrev < 200000000) {
270         fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
271         lose("NetBSD kernel too old to run sbcl.\n");
272     }
273
274     /* NetBSD counts mmap()ed space against the process's data size limit,
275      * so yank it up. This might be a nasty thing to do? */
276     getrlimit (RLIMIT_DATA, &rl);
277     /* Amazingly for such a new port, the provenance and meaning of
278        this number are unknown.  It might just mean REALLY_BIG_LIMIT,
279        or possibly it should be calculated from dynamic space size.
280        -- CSR, 2004-04-08 */
281     rl.rlim_cur = 1073741824;
282     if (setrlimit (RLIMIT_DATA, &rl) < 0) {
283         fprintf (stderr,
284                  "RUNTIME WARNING: unable to raise process data size limit:\n\
285   %s.\n\
286 The system may fail to start.\n",
287                  strerror(errno));
288     }
289 }
290 #endif /* __NetBSD__ */
291
292 #ifdef __FreeBSD__
293 static void freebsd_init()
294 {
295     /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
296      * 4.x with GENERIC kernel, does not enable SSE support even on
297      * SSE capable CPUs". Detect this situation and skip the
298      * fast_bzero sse/base selection logic that's normally done in
299      * x86-assem.S.
300      */
301 #ifdef LISP_FEATURE_X86
302     size_t len;
303     int instruction_sse;
304
305     len = sizeof(instruction_sse);
306     if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
307         && instruction_sse != 0) {
308         /* Use the SSE detector */
309         fast_bzero_pointer = fast_bzero_detect;
310     }
311 #endif /* LISP_FEATURE_X86 */
312 }
313 #endif /* __FreeBSD__ */
314 \f
315 /* threads */
316
317 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
318 #ifdef LISP_FEATURE_SB_THREAD
319 #error "Define threading support functions"
320 #else
321 int arch_os_thread_init(struct thread *thread) {
322   stack_t sigstack;
323 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
324     /* Signal handlers are run on the control stack, so if it is exhausted
325      * we had better use an alternate stack for whatever signal tells us
326      * we've exhausted it */
327     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
328     sigstack.ss_flags=0;
329     sigstack.ss_size = 32*SIGSTKSZ;
330     sigaltstack(&sigstack,0);
331 #endif
332     return 1;                  /* success */
333 }
334 int arch_os_thread_cleanup(struct thread *thread) {
335     return 1;                  /* success */
336 }
337 #endif
338
339 #ifdef LISP_FEATURE_DARWIN
340 /* defined in ppc-darwin-os.c instead */
341 #elif defined(LISP_FEATURE_FREEBSD)
342 char *
343 os_get_runtime_executable_path()
344 {
345     char path[PATH_MAX + 1];
346     /* Defined on FreeBSD 6 and later. -- JES, 2006-02-06. */
347 #ifdef KERN_PROC_PATHNAME
348     size_t len = PATH_MAX + 1;
349     int mib[4];
350
351     mib[0] = CTL_KERN;
352     mib[1] = KERN_PROC;
353     mib[2] = KERN_PROC_PATHNAME;
354     mib[3] = -1;
355     if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
356         /* FIXME: Should we rather fall back into the /proc/-based
357          * code below, so that an SBCL executable core made on
358          * FreeBSD 6 would have at least some chance of working on
359          * FreeBSD 4? -- JES, 2006-02-06.
360          */
361         return NULL;
362 #else
363     int size;
364     size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
365     if (size < 0)
366         return NULL;
367     path[size] = '\0';
368     if (strcmp(path, "unknown") == 0)
369         return NULL;
370 #endif
371     return copied_string(path);
372 }
373 #else /* Not DARWIN or FREEBSD */
374 char *
375 os_get_runtime_executable_path()
376 {
377     return NULL;
378 }
379 #endif