2 * OS-dependent routines for BSD-ish systems
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.
11 * This software is part of the SBCL system. See the README file for
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.
22 #include <sys/param.h>
32 #include "interrupt.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/fdefn.h"
40 #include <sys/types.h>
42 /* #include <sys/sysinfo.h> */
44 #if defined LISP_FEATURE_GENCGC
45 #include "gencgc-internal.h"
48 os_vm_size_t os_vm_page_size;
51 #include <sys/resource.h>
52 #include <sys/sysctl.h>
54 #include <sys/stat.h> /* For the stat-family wrappers. */
55 #include <dirent.h> /* For the opendir()/readdir() wrappers */
56 #include <sys/socket.h> /* For the socket() wrapper */
57 static void netbsd_init();
58 #endif /* __NetBSD__ */
61 #include <sys/sysctl.h>
62 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
66 static void freebsd_init();
67 #endif /* __FreeBSD__ */
70 #include <sys/types.h>
71 #include <sys/resource.h>
75 static void openbsd_init();
79 os_init(char *argv[], char *envp[])
81 os_vm_page_size = getpagesize();
85 #elif defined(__FreeBSD__)
87 #elif defined(__OpenBSD__)
93 os_context_sigmask_addr(os_context_t *context)
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;
103 #error unsupported BSD variant
108 os_validate(os_vm_address_t addr, os_vm_size_t len)
110 int flags = MAP_PRIVATE | MAP_ANON;
115 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
117 if (addr == MAP_FAILED) {
126 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
128 if (munmap(addr, len) == -1)
133 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
135 addr = mmap(addr, len,
137 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
140 if (addr == MAP_FAILED) {
142 lose("unexpected mmap(..) failure\n");
149 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
151 if (mprotect(address, length, prot) == -1) {
157 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
159 char* beg = (char*) sbeg;
160 char* end = (char*) sbeg + slen;
161 char* adr = (char*) a;
162 return (adr >= beg && adr < end);
166 is_valid_lisp_addr(os_vm_address_t addr)
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))
174 for_each_thread(th) {
175 if (((os_vm_address_t)th->control_stack_start <= addr) &&
176 (addr < (os_vm_address_t)th->control_stack_end))
178 if (in_range_p(addr, (lispobj) th->binding_stack_start,
186 * any OS-dependent special low-level handling for signals
189 #if defined LISP_FEATURE_GENCGC
192 * The GENCGC needs to be hooked into whatever signal is raised for
193 * page fault on this OS.
197 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context
198 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
199 /* FreeBSD/amd64 stores fault address only in undocumented 4th arg. */
204 os_context_t *context = arch_os_get_context(&void_context);
205 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
206 /* KLUDGE: Store fault address into si_addr for compatibilities. */
207 siginfo->si_addr = fault_addr;
209 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
212 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
213 FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
214 *CONTEXT_ADDR_FROM_STEM(fs)));
215 os_restore_tls_segment_register(context);
218 FSHOW((stderr, "Memory fault at: %p, PC: %p\n", fault_addr, *os_context_pc_addr(context)));
220 if (!gencgc_handle_wp_violation(fault_addr))
221 if(!handle_guard_page_triggered(context,fault_addr)) {
222 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
223 lisp_memory_fault_error(context, fault_addr);
226 /* this disabled section is what used to be here: */
228 /* FIXME: never returns 0 */
229 if (!maybe_gc(context)) {
230 interrupt_handle_now(signal, siginfo, context);
233 /* FIXME: Nowadays, maybe_gc does return 1 to indicate
234 * that GC did happen, but I'm keeping the code as it
237 interrupt_handle_now(signal, siginfo, context);
242 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
244 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context) {
245 lose("Unhandled memory fault. Exiting.");
250 os_install_interrupt_handlers(void)
252 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
253 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
254 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
255 mach_error_memory_fault_handler);
257 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
258 #ifdef LISP_FEATURE_FREEBSD
259 (__siginfohandler_t *)
261 memory_fault_handler);
264 #ifdef LISP_FEATURE_SB_THREAD
265 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
266 interrupt_thread_handler);
267 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
268 sig_stop_for_gc_handler);
270 SHOW("leaving os_install_interrupt_handlers()");
273 #else /* Currently PPC/Darwin/Cheney only */
276 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
278 os_context_t *context = arch_os_get_context(&void_context);
280 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
282 os_vm_address_t addr;
284 addr = arch_get_bad_addr(signal, info, context);
285 if (!cheneygc_handle_wp_violation(context, addr))
286 if (!handle_guard_page_triggered(context, addr))
287 interrupt_handle_now(signal, info, context);
291 os_install_interrupt_handlers(void)
293 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
294 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
298 #endif /* defined GENCGC */
301 static void netbsd_init()
307 /* Are we running on a sufficiently functional kernel? */
312 sysctl(mib, 2, &osrev, &len, NULL, 0);
314 /* If we're older than 2.0... */
315 if (osrev < 200000000) {
316 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
317 lose("NetBSD kernel too old to run sbcl.\n");
320 /* NetBSD counts mmap()ed space against the process's data size limit,
321 * so yank it up. This might be a nasty thing to do? */
322 getrlimit (RLIMIT_DATA, &rl);
323 /* Amazingly for such a new port, the provenance and meaning of
324 this number are unknown. It might just mean REALLY_BIG_LIMIT,
325 or possibly it should be calculated from dynamic space size.
326 -- CSR, 2004-04-08 */
327 rl.rlim_cur = 1073741824;
328 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
330 "RUNTIME WARNING: unable to raise process data size limit:\n\
332 The system may fail to start.\n",
337 /* Various routines in NetBSD's C library are compatibility wrappers
338 for old versions. Programs must be processed by the C toolchain in
339 order to get up-to-date definitions of such routines. */
340 /* The stat-family, opendir, and readdir are used only in sb-posix, as
341 of 2007-01-16. -- RMK */
343 _stat(const char *path, struct stat *sb)
345 return stat(path, sb);
348 _lstat(const char *path, struct stat *sb)
350 return lstat(path, sb);
353 _fstat(int fd, struct stat *sb)
355 return fstat(fd, sb);
359 _opendir(const char *filename)
361 return opendir(filename);
366 return readdir(dirp);
369 /* Used in sb-bsd-sockets. */
371 _socket(int domain, int type, int protocol)
373 return socket(domain, type, protocol);
375 #endif /* __NetBSD__ */
378 extern int getosreldate(void);
380 int sig_memory_fault;
382 static void freebsd_init()
384 /* Memory fault signal on FreeBSD was changed from SIGBUS to
386 if (getosreldate() < 700004)
387 sig_memory_fault = SIGBUS;
389 sig_memory_fault = SIGSEGV;
391 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
392 * 4.x with GENERIC kernel, does not enable SSE support even on
393 * SSE capable CPUs". Detect this situation and skip the
394 * fast_bzero sse/base selection logic that's normally done in
397 #ifdef LISP_FEATURE_X86
402 len = sizeof(instruction_sse);
403 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len,
404 NULL, 0) == 0 && instruction_sse != 0) {
405 /* Use the SSE detector */
406 fast_bzero_pointer = fast_bzero_detect;
409 #endif /* LISP_FEATURE_X86 */
412 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
414 futex_wait(int *lock_word, long oldval, long sec, unsigned long usec)
416 struct timespec timeout;
421 ret = umtx_wait((void *)lock_word, oldval, NULL);
423 timeout.tv_sec = sec;
424 timeout.tv_nsec = usec * 1000;
425 ret = umtx_wait((void *)lock_word, oldval, &timeout);
434 /* spurious wakeup from interrupt */
437 /* EWOULDBLOCK and others, need to check the lock */
443 futex_wake(int *lock_word, int n)
445 return umtx_wake((void *)lock_word, n);
448 #endif /* __FreeBSD__ */
450 #ifdef LISP_FEATURE_DARWIN
451 /* defined in ppc-darwin-os.c instead */
452 #elif defined(LISP_FEATURE_FREEBSD)
453 #ifndef KERN_PROC_PATHNAME
454 #define KERN_PROC_PATHNAME 12
458 os_get_runtime_executable_path()
460 char path[PATH_MAX + 1];
462 if (getosreldate() >= 600024) {
463 /* KERN_PROC_PATHNAME is available */
464 size_t len = PATH_MAX + 1;
469 mib[2] = KERN_PROC_PATHNAME;
471 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
475 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
480 if (strcmp(path, "unknown") == 0)
482 return copied_string(path);
484 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
486 os_get_runtime_executable_path()
489 char *path = strdup("/proc/curproc/file");
490 if (path && ((stat(path, &sb)) == 0))
493 fprintf(stderr, "Couldn't stat /proc/curproc/file; is /proc mounted?\n");
497 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD */
499 os_get_runtime_executable_path()
511 /* OpenBSD, like NetBSD, counts mmap()ed space against the
512 * process's data size limit. If the soft limit is lower than the
513 * hard limit then try to yank it up, this lets users in the
514 * "staff" login class run sbcl with a default /etc/login.conf
516 getrlimit (RLIMIT_DATA, &rl);
517 if (rl.rlim_cur < rl.rlim_max) {
518 rl.rlim_cur = rl.rlim_max;
519 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
521 "RUNTIME WARNING: unable to raise process data size limit:\n\
523 The system may fail to start.\n",
528 /* Display a (hopefully) helpful warning if it looks like we won't
529 * be able to allocate enough memory. In testing I found that on
530 * my system at least, a minimum of 25M on top of the three space
531 * sizes was needed to start SBCL. Show a warning below 32M so as
532 * to leave a little breathing room.
534 getrlimit (RLIMIT_DATA, &rl);
535 if (dynamic_space_size + READ_ONLY_SPACE_SIZE + STATIC_SPACE_SIZE +
536 LINKAGE_TABLE_SPACE_SIZE + (32*1024*1024) > rl.rlim_cur)
538 "RUNTIME WARNING: data size resource limit may be too low,\n"
539 " try decreasing the dynamic space size with --dynamic-space-size\n");
542 /* OpenBSD's dlsym() relies on the gcc bulitin
543 * __builtin_return_address(0) returning an address in the
544 * executable's text segment, but when called from lisp it will return
545 * an address in the dynamic space. Work around this by calling this
546 * wrapper function instead. Note that tail-call optimization will
547 * defeat this, disable it by saving the dlsym() return value in a
551 os_dlsym(void *handle, const char *symbol)
553 void * volatile ret = dlsym(handle, symbol);