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