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>
33 #include "interrupt.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/fdefn.h"
41 #include <sys/types.h>
43 /* #include <sys/sysinfo.h> */
45 #if defined LISP_FEATURE_GENCGC
46 #include "gencgc-internal.h"
49 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
50 # include <sys/event.h>
54 os_vm_size_t os_vm_page_size
;
57 #include <sys/resource.h>
58 #include <sys/sysctl.h>
60 #include <sys/stat.h> /* For the stat-family wrappers. */
61 #include <dirent.h> /* For the opendir()/readdir() wrappers */
62 #include <sys/socket.h> /* For the socket() wrapper */
63 static void netbsd_init();
64 static os_vm_size_t max_allocation_size
;
65 #endif /* __NetBSD__ */
67 #if defined LISP_FEATURE_FREEBSD
68 #include <sys/sysctl.h>
69 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
73 static void freebsd_init();
74 #endif /* __FreeBSD__ */
77 #include <sys/sysctl.h>
79 static void dragonfly_init();
80 #endif /* __DragonFly__ */
83 #include <sys/types.h>
84 #include <sys/resource.h>
86 #include <sys/sysctl.h>
88 #ifdef LISP_FEATURE_X86
89 #include <machine/cpu.h>
92 static void openbsd_init();
96 os_init(char *argv
[], char *envp
[])
98 os_vm_page_size
= BACKEND_PAGE_BYTES
;
102 #elif defined(LISP_FEATURE_FREEBSD)
104 #elif defined(__OpenBSD__)
106 #elif defined(LISP_FEATURE_DARWIN)
108 #elif defined(__DragonFly__)
114 os_context_sigmask_addr(os_context_t
*context
)
116 /* (Unlike most of the other context fields that we access, the
117 * signal mask field is a field of the basic, outermost context
118 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
119 #if defined(LISP_FEATURE_FREEBSD) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN) \
120 || defined(__DragonFly__)
121 return &context
->uc_sigmask
;
122 #elif defined (__OpenBSD__)
123 return &context
->sc_mask
;
125 #error unsupported BSD variant
130 os_validate(os_vm_address_t addr
, os_vm_size_t len
)
132 int flags
= MAP_PRIVATE
| MAP_ANON
;
139 os_vm_address_t curaddr
= addr
;
142 os_vm_address_t resaddr
;
143 os_vm_size_t curlen
= MIN(max_allocation_size
, len
);
145 resaddr
= mmap(curaddr
, curlen
, OS_VM_PROT_ALL
, flags
, -1, 0);
147 if (resaddr
== (os_vm_address_t
) - 1) {
150 while (curaddr
> addr
) {
151 curaddr
-= max_allocation_size
;
152 munmap(curaddr
, max_allocation_size
);
164 addr
= mmap(addr
, len
, OS_VM_PROT_ALL
, flags
, -1, 0);
167 if (addr
== MAP_FAILED
) {
176 os_invalidate(os_vm_address_t addr
, os_vm_size_t len
)
178 if (munmap(addr
, len
) == -1)
183 os_map(int fd
, int offset
, os_vm_address_t addr
, os_vm_size_t len
)
185 addr
= mmap(addr
, len
,
187 MAP_PRIVATE
| MAP_FILE
| MAP_FIXED
,
190 if (addr
== MAP_FAILED
) {
192 lose("unexpected mmap(..) failure\n");
199 os_protect(os_vm_address_t address
, os_vm_size_t length
, os_vm_prot_t prot
)
201 if (mprotect(address
, length
, prot
) == -1) {
207 in_range_p(os_vm_address_t a
, lispobj sbeg
, size_t slen
)
209 char* beg
= (char*) sbeg
;
210 char* end
= (char*) sbeg
+ slen
;
211 char* adr
= (char*) a
;
212 return (adr
>= beg
&& adr
< end
);
216 is_valid_lisp_addr(os_vm_address_t addr
)
220 if (in_range_p(addr
, READ_ONLY_SPACE_START
, READ_ONLY_SPACE_SIZE
) ||
221 in_range_p(addr
, STATIC_SPACE_START
, STATIC_SPACE_SIZE
) ||
222 in_range_p(addr
, DYNAMIC_SPACE_START
, dynamic_space_size
))
224 for_each_thread(th
) {
225 if (((os_vm_address_t
)th
->control_stack_start
<= addr
) &&
226 (addr
< (os_vm_address_t
)th
->control_stack_end
))
228 if (in_range_p(addr
, (lispobj
) th
->binding_stack_start
,
236 * any OS-dependent special low-level handling for signals
239 #if defined LISP_FEATURE_GENCGC
242 * The GENCGC needs to be hooked into whatever signal is raised for
243 * page fault on this OS.
247 memory_fault_handler(int signal
, siginfo_t
*siginfo
, os_context_t
*context
)
249 void *fault_addr
= arch_get_bad_addr(signal
, siginfo
, context
);
251 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
252 FSHOW_SIGNAL((stderr
, "/ TLS: restoring fs: %p in memory_fault_handler\n",
253 *CONTEXT_ADDR_FROM_STEM(fs
)));
254 os_restore_tls_segment_register(context
);
257 FSHOW((stderr
, "Memory fault at: %p, PC: %p\n", fault_addr
, *os_context_pc_addr(context
)));
259 #ifdef LISP_FEATURE_SB_SAFEPOINT
260 if (!handle_safepoint_violation(context
, fault_addr
))
263 if (!gencgc_handle_wp_violation(fault_addr
))
264 if(!handle_guard_page_triggered(context
,fault_addr
))
265 lisp_memory_fault_error(context
, fault_addr
);
268 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
270 mach_error_memory_fault_handler(int signal
, siginfo_t
*siginfo
,
271 os_context_t
*context
) {
272 lose("Unhandled memory fault. Exiting.");
277 os_install_interrupt_handlers(void)
279 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
280 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
281 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
282 mach_error_memory_fault_handler
);
284 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
285 #if defined(LISP_FEATURE_FREEBSD) && !defined(__GLIBC__)
286 (__siginfohandler_t
*)
288 memory_fault_handler
);
291 #ifdef LISP_FEATURE_SB_THREAD
292 # ifdef LISP_FEATURE_SB_SAFEPOINT
293 # ifdef LISP_FEATURE_SB_THRUPTION
294 undoably_install_low_level_interrupt_handler(SIGPIPE
, thruption_handler
);
297 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC
,
298 sig_stop_for_gc_handler
);
301 SHOW("leaving os_install_interrupt_handlers()");
304 #else /* Currently PPC/Darwin/Cheney only */
307 sigsegv_handler(int signal
, siginfo_t
*info
, os_context_t
*context
)
310 unsigned int pc
= (unsigned int *)(*os_context_pc_addr(context
));
312 os_vm_address_t addr
;
314 addr
= arch_get_bad_addr(signal
, info
, context
);
315 if (!cheneygc_handle_wp_violation(context
, addr
))
316 if (!handle_guard_page_triggered(context
, addr
))
317 interrupt_handle_now(signal
, info
, context
);
321 os_install_interrupt_handlers(void)
323 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
324 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
328 #endif /* defined GENCGC */
331 static void netbsd_init()
337 /* Are we running on a sufficiently functional kernel? */
342 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
344 /* If we're older than 2.0... */
345 if (osrev
< 200000000) {
346 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
347 lose("NetBSD kernel too old to run sbcl.\n");
350 /* NetBSD counts mmap()ed space against the process's data size limit,
351 * so yank it up. This might be a nasty thing to do? */
352 getrlimit (RLIMIT_DATA
, &rl
);
353 if (rl
.rlim_cur
< rl
.rlim_max
) {
354 rl
.rlim_cur
= rl
.rlim_max
;
355 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
357 "RUNTIME WARNING: unable to raise process data size limit:\n\
359 The system may fail to start.\n",
363 max_allocation_size
= (os_vm_size_t
)((rl
.rlim_cur
/ 2) &
364 ~(32 * 1024 * 1024));
366 #ifdef LISP_FEATURE_X86
372 if (sysctlbyname("machdep.sse", &sse
, &len
,
373 NULL
, 0) == 0 && sse
!= 0) {
374 /* Use the SSE detector */
375 fast_bzero_pointer
= fast_bzero_detect
;
378 #endif /* LISP_FEATURE_X86 */
381 /* Various routines in NetBSD's C library are compatibility wrappers
382 for old versions. Programs must be processed by the C toolchain in
383 order to get up-to-date definitions of such routines. */
384 /* The stat-family, opendir, and readdir are used only in sb-posix, as
385 of 2007-01-16. -- RMK */
387 _stat(const char *path
, struct stat
*sb
)
389 return stat(path
, sb
);
392 _lstat(const char *path
, struct stat
*sb
)
394 return lstat(path
, sb
);
397 _fstat(int fd
, struct stat
*sb
)
399 return fstat(fd
, sb
);
403 _opendir(const char *filename
)
405 return opendir(filename
);
410 return readdir(dirp
);
414 _utime(const char *file
, const struct utimbuf
*timep
)
416 return utime(file
, timep
);
419 /* Used in sb-bsd-sockets. */
421 _socket(int domain
, int type
, int protocol
)
423 return socket(domain
, type
, protocol
);
425 #endif /* __NetBSD__ */
427 #if defined(LISP_FEATURE_FREEBSD)
429 extern int getosreldate(void);
432 int sig_memory_fault
;
434 static void freebsd_init()
436 /* Memory fault signal on FreeBSD was changed from SIGBUS to
439 sig_memory_fault
= SIGSEGV
;
441 if (getosreldate() < 700004)
442 sig_memory_fault
= SIGBUS
;
444 sig_memory_fault
= SIGSEGV
;
447 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
448 * 4.x with GENERIC kernel, does not enable SSE support even on
449 * SSE capable CPUs". Detect this situation and skip the
450 * fast_bzero sse/base selection logic that's normally done in
453 #ifdef LISP_FEATURE_X86
458 len
= sizeof(instruction_sse
);
459 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
460 NULL
, 0) == 0 && instruction_sse
!= 0) {
461 /* Use the SSE detector */
462 fast_bzero_pointer
= fast_bzero_detect
;
465 #endif /* LISP_FEATURE_X86 */
468 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
469 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
471 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
473 struct timespec timeout
;
477 ret
= umtx_wait((void *)lock_word
, oldval
, NULL
);
479 timeout
.tv_sec
= sec
;
480 timeout
.tv_nsec
= usec
* 1000;
481 ret
= umtx_wait((void *)lock_word
, oldval
, &timeout
);
492 /* EWOULDBLOCK and others, need to check the lock */
498 futex_wake(int *lock_word
, int n
)
500 return umtx_wake((void *)lock_word
, n
);
503 #endif /* __FreeBSD__ */
506 static void dragonfly_init()
508 #ifdef LISP_FEATURE_X86
512 len
= sizeof(instruction_sse
);
513 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
514 NULL
, 0) == 0 && instruction_sse
!= 0) {
515 /* Use the SSE detector */
516 fast_bzero_pointer
= fast_bzero_detect
;
518 #endif /* LISP_FEATURE_X86 */
522 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
523 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
525 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
530 ret
= umtx_sleep(lock_word
, oldval
, 0);
532 int count
= usec
+ 1000000 * sec
;
533 ret
= umtx_sleep(lock_word
, oldval
, count
);
536 if (ret
== 0) return 0;
539 case EWOULDBLOCK
: // Operation timed out
543 default: // Such as EINVAL or EBUSY
550 futex_wake(int *lock_word
, int n
)
552 return umtx_wakeup(lock_word
, n
);
555 #endif /* __DragonFly__ */
557 #ifdef LISP_FEATURE_DARWIN
558 /* defined in ppc-darwin-os.c instead */
559 #elif defined(LISP_FEATURE_FREEBSD)
560 #ifndef KERN_PROC_PATHNAME
561 #define KERN_PROC_PATHNAME 12
565 os_get_runtime_executable_path(int external
)
567 char path
[PATH_MAX
+ 1];
570 if (getosreldate() >= 600024) {
572 /* KERN_PROC_PATHNAME is available */
573 size_t len
= PATH_MAX
+ 1;
578 mib
[2] = KERN_PROC_PATHNAME
;
580 if (sysctl(mib
, 4, &path
, &len
, NULL
, 0) != 0)
585 size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
591 if (strcmp(path
, "unknown") == 0)
593 return copied_string(path
);
595 #elif defined(LISP_FEATURE_DRAGONFLY)
597 os_get_runtime_executable_path(int external
)
599 char path
[PATH_MAX
+ 1];
600 int size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
605 if (strcmp(path
, "unknown") == 0)
607 return copied_string(path
);
609 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
611 os_get_runtime_executable_path(int external
)
614 if (!external
&& stat("/proc/curproc/file", &sb
) == 0)
615 return copied_string("/proc/curproc/file");
618 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
620 os_get_runtime_executable_path(int external
)
628 int openbsd_use_fxsave
= 0;
633 #ifdef LISP_FEATURE_X86
638 * Show a warning if it looks like the memory available after
639 * allocating the spaces won't be at least this much.
641 #ifdef LISP_FEATURE_X86_64
642 const int wantfree
= 64 * 1024 * 1024;
644 const int wantfree
= 32 * 1024 * 1024;
648 #ifdef LISP_FEATURE_X86
649 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
650 mib
[0] = CTL_MACHDEP
;
652 size
= sizeof (openbsd_use_fxsave
);
653 sysctl(mib
, 2, &openbsd_use_fxsave
, &size
, NULL
, 0);
656 /* OpenBSD, like NetBSD, counts mmap()ed space against the
657 * process's data size limit. If the soft limit is lower than the
658 * hard limit then try to yank it up, this lets users in the
659 * "staff" or "daemon" login classes run sbcl with larger dynamic
662 getrlimit (RLIMIT_DATA
, &rl
);
663 if (rl
.rlim_cur
< rl
.rlim_max
) {
664 rl
.rlim_cur
= rl
.rlim_max
;
665 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
667 "RUNTIME WARNING: unable to raise process data size limit:\n\
669 The system may fail to start.\n",
675 * Display a (hopefully) helpful warning if it looks like we won't
676 * be able to allocate enough memory.
678 getrlimit (RLIMIT_DATA
, &rl
);
679 if (dynamic_space_size
+ READ_ONLY_SPACE_SIZE
+ STATIC_SPACE_SIZE
+
680 LINKAGE_TABLE_SPACE_SIZE
+ wantfree
> rl
.rlim_cur
)
682 "RUNTIME WARNING: data size resource limit may be too low,\n"
683 " try decreasing the dynamic space size with --dynamic-space-size\n"
684 " or raising the datasize or datasize-max limits in /etc/login.conf\n");
687 /* OpenBSD's dlsym() relies on the gcc bulitin
688 * __builtin_return_address(0) returning an address in the
689 * executable's text segment, but when called from lisp it will return
690 * an address in the dynamic space. Work around this by calling this
691 * wrapper function instead. Note that tail-call optimization will
692 * defeat this, disable it by saving the dlsym() return value in a
696 os_dlsym(void *handle
, const char *symbol
)
698 void * volatile ret
= dlsym(handle
, symbol
);
704 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
706 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
707 * timer facility using kqueue.
714 lose("os_create_wtimer: kqueue");
719 os_wait_for_wtimer(int kq
)
723 if ( (n
= kevent(kq
, 0, 0, &ev
, 1, 0)) == -1) {
725 lose("os_wtimer_listen failed");
732 os_close_wtimer(int kq
)
735 lose("os_close_wtimer failed");
739 os_set_wtimer(int kq
, int sec
, int nsec
)
742 = ((long long) sec
) * 1000 + (long long) (nsec
+999999) / 1000000;
743 if (msec
> INT_MAX
) msec
= INT_MAX
;
746 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_ADD
|EV_ENABLE
|EV_ONESHOT
, 0, (int)msec
, 0);
747 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1)
748 perror("os_set_wtimer: kevent");
752 os_cancel_wtimer(int kq
)
755 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_DISABLE
, 0, 0, 0);
756 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1 && errno
!= ENOENT
)
757 perror("os_cancel_wtimer: kevent");