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