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