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