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_protect(os_vm_address_t address
, os_vm_size_t length
, os_vm_prot_t prot
)
185 if (mprotect(address
, length
, prot
) == -1) {
191 in_range_p(os_vm_address_t a
, lispobj sbeg
, size_t slen
)
193 char* beg
= (char*) sbeg
;
194 char* end
= (char*) sbeg
+ slen
;
195 char* adr
= (char*) a
;
196 return (adr
>= beg
&& adr
< end
);
200 is_valid_lisp_addr(os_vm_address_t addr
)
204 if (in_range_p(addr
, READ_ONLY_SPACE_START
, READ_ONLY_SPACE_SIZE
) ||
205 in_range_p(addr
, STATIC_SPACE_START
, STATIC_SPACE_SIZE
) ||
206 #ifdef LISP_FEATURE_IMMOBILE_SPACE
207 in_range_p(addr
, IMMOBILE_SPACE_START
, IMMOBILE_SPACE_SIZE
) ||
209 in_range_p(addr
, DYNAMIC_SPACE_START
, dynamic_space_size
))
211 for_each_thread(th
) {
212 if (((os_vm_address_t
)th
->control_stack_start
<= addr
) &&
213 (addr
< (os_vm_address_t
)th
->control_stack_end
))
215 if (in_range_p(addr
, (lispobj
) th
->binding_stack_start
,
223 * any OS-dependent special low-level handling for signals
226 #if defined LISP_FEATURE_GENCGC
229 * The GENCGC needs to be hooked into whatever signal is raised for
230 * page fault on this OS.
234 memory_fault_handler(int signal
, siginfo_t
*siginfo
, os_context_t
*context
)
236 void *fault_addr
= arch_get_bad_addr(signal
, siginfo
, context
);
238 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
239 FSHOW_SIGNAL((stderr
, "/ TLS: restoring fs: %p in memory_fault_handler\n",
240 *CONTEXT_ADDR_FROM_STEM(fs
)));
241 os_restore_tls_segment_register(context
);
244 FSHOW((stderr
, "Memory fault at: %p, PC: %p\n", fault_addr
, *os_context_pc_addr(context
)));
246 #ifdef LISP_FEATURE_SB_SAFEPOINT
247 if (!handle_safepoint_violation(context
, fault_addr
))
250 if (!gencgc_handle_wp_violation(fault_addr
))
251 if(!handle_guard_page_triggered(context
,fault_addr
))
252 lisp_memory_fault_error(context
, fault_addr
);
255 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
257 mach_error_memory_fault_handler(int signal
, siginfo_t
*siginfo
,
258 os_context_t
*context
) {
259 lose("Unhandled memory fault. Exiting.");
264 os_install_interrupt_handlers(void)
266 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
267 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
268 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
269 mach_error_memory_fault_handler
);
271 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
272 #if defined(LISP_FEATURE_FREEBSD) && !defined(__GLIBC__)
273 (__siginfohandler_t
*)
275 memory_fault_handler
);
278 #ifdef LISP_FEATURE_SB_THREAD
279 # ifdef LISP_FEATURE_SB_SAFEPOINT
280 # ifdef LISP_FEATURE_SB_THRUPTION
281 undoably_install_low_level_interrupt_handler(SIGPIPE
, thruption_handler
);
284 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC
,
285 sig_stop_for_gc_handler
);
288 SHOW("leaving os_install_interrupt_handlers()");
291 #else /* Currently PPC/Darwin/Cheney only */
294 sigsegv_handler(int signal
, siginfo_t
*info
, os_context_t
*context
)
296 os_vm_address_t addr
;
298 addr
= arch_get_bad_addr(signal
, info
, context
);
299 if (!cheneygc_handle_wp_violation(context
, addr
))
300 if (!handle_guard_page_triggered(context
, addr
))
301 interrupt_handle_now(signal
, info
, context
);
305 os_install_interrupt_handlers(void)
307 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
308 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
312 #endif /* defined GENCGC */
315 static void netbsd_init()
321 /* Are we running on a sufficiently functional kernel? */
326 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
328 /* If we're older than 2.0... */
329 if (osrev
< 200000000) {
330 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
331 lose("NetBSD kernel too old to run sbcl.\n");
334 /* NetBSD counts mmap()ed space against the process's data size limit,
335 * so yank it up. This might be a nasty thing to do? */
336 getrlimit (RLIMIT_DATA
, &rl
);
337 if (rl
.rlim_cur
< rl
.rlim_max
) {
338 rl
.rlim_cur
= rl
.rlim_max
;
339 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
341 "RUNTIME WARNING: unable to raise process data size limit:\n\
343 The system may fail to start.\n",
347 max_allocation_size
= (os_vm_size_t
)((rl
.rlim_cur
/ 2) &
348 ~(32 * 1024 * 1024));
350 #ifdef LISP_FEATURE_X86
356 if (sysctlbyname("machdep.sse", &sse
, &len
,
357 NULL
, 0) == 0 && sse
!= 0) {
358 /* Use the SSE detector */
359 fast_bzero_pointer
= fast_bzero_detect
;
362 #endif /* LISP_FEATURE_X86 */
365 /* Various routines in NetBSD's C library are compatibility wrappers
366 for old versions. Programs must be processed by the C toolchain in
367 order to get up-to-date definitions of such routines. */
368 /* The stat-family, opendir, and readdir are used only in sb-posix, as
369 of 2007-01-16. -- RMK */
371 _stat(const char *path
, struct stat
*sb
)
373 return stat(path
, sb
);
376 _lstat(const char *path
, struct stat
*sb
)
378 return lstat(path
, sb
);
381 _fstat(int fd
, struct stat
*sb
)
383 return fstat(fd
, sb
);
387 _opendir(const char *filename
)
389 return opendir(filename
);
394 return readdir(dirp
);
398 _utime(const char *file
, const struct utimbuf
*timep
)
400 return utime(file
, timep
);
403 /* Used in sb-bsd-sockets. */
405 _socket(int domain
, int type
, int protocol
)
407 return socket(domain
, type
, protocol
);
409 #endif /* __NetBSD__ */
411 #if defined(LISP_FEATURE_FREEBSD)
413 extern int getosreldate(void);
416 int sig_memory_fault
;
418 static void freebsd_init()
420 /* Memory fault signal on FreeBSD was changed from SIGBUS to
423 sig_memory_fault
= SIGSEGV
;
425 if (getosreldate() < 700004)
426 sig_memory_fault
= SIGBUS
;
428 sig_memory_fault
= SIGSEGV
;
431 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
432 * 4.x with GENERIC kernel, does not enable SSE support even on
433 * SSE capable CPUs". Detect this situation and skip the
434 * fast_bzero sse/base selection logic that's normally done in
437 #ifdef LISP_FEATURE_X86
442 len
= sizeof(instruction_sse
);
443 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
444 NULL
, 0) == 0 && instruction_sse
!= 0) {
445 /* Use the SSE detector */
446 fast_bzero_pointer
= fast_bzero_detect
;
449 #endif /* LISP_FEATURE_X86 */
452 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
453 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
455 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
457 struct timespec timeout
;
461 ret
= umtx_wait((void *)lock_word
, oldval
, NULL
);
463 timeout
.tv_sec
= sec
;
464 timeout
.tv_nsec
= usec
* 1000;
465 ret
= umtx_wait((void *)lock_word
, oldval
, &timeout
);
476 /* EWOULDBLOCK and others, need to check the lock */
482 futex_wake(int *lock_word
, int n
)
484 return umtx_wake((void *)lock_word
, n
);
487 #endif /* __FreeBSD__ */
490 static void dragonfly_init()
492 #ifdef LISP_FEATURE_X86
496 len
= sizeof(instruction_sse
);
497 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
498 NULL
, 0) == 0 && instruction_sse
!= 0) {
499 /* Use the SSE detector */
500 fast_bzero_pointer
= fast_bzero_detect
;
502 #endif /* LISP_FEATURE_X86 */
506 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
507 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
509 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
514 ret
= umtx_sleep(lock_word
, oldval
, 0);
516 int count
= usec
+ 1000000 * sec
;
517 ret
= umtx_sleep(lock_word
, oldval
, count
);
520 if (ret
== 0) return 0;
523 case EWOULDBLOCK
: // Operation timed out
527 default: // Such as EINVAL or EBUSY
534 futex_wake(int *lock_word
, int n
)
536 return umtx_wakeup(lock_word
, n
);
539 #endif /* __DragonFly__ */
541 #ifdef LISP_FEATURE_DARWIN
542 /* defined in ppc-darwin-os.c instead */
543 #elif defined(LISP_FEATURE_FREEBSD)
544 #ifndef KERN_PROC_PATHNAME
545 #define KERN_PROC_PATHNAME 12
549 os_get_runtime_executable_path(int external
)
551 char path
[PATH_MAX
+ 1];
554 if (getosreldate() >= 600024) {
556 /* KERN_PROC_PATHNAME is available */
557 size_t len
= PATH_MAX
+ 1;
562 mib
[2] = KERN_PROC_PATHNAME
;
564 if (sysctl(mib
, 4, &path
, &len
, NULL
, 0) != 0)
569 size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
575 if (strcmp(path
, "unknown") == 0)
577 return copied_string(path
);
579 #elif defined(LISP_FEATURE_DRAGONFLY)
581 os_get_runtime_executable_path(int external
)
583 char path
[PATH_MAX
+ 1];
584 int size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
589 if (strcmp(path
, "unknown") == 0)
591 return copied_string(path
);
593 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
595 os_get_runtime_executable_path(int external
)
598 if (!external
&& stat("/proc/curproc/file", &sb
) == 0)
599 return copied_string("/proc/curproc/file");
602 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
604 os_get_runtime_executable_path(int external
)
612 int openbsd_use_fxsave
= 0;
617 #ifdef LISP_FEATURE_X86
622 * Show a warning if it looks like the memory available after
623 * allocating the spaces won't be at least this much.
625 #ifdef LISP_FEATURE_64_BIT
626 const int wantfree
= 64 * 1024 * 1024;
628 const int wantfree
= 32 * 1024 * 1024;
632 #ifdef LISP_FEATURE_X86
633 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
634 mib
[0] = CTL_MACHDEP
;
636 size
= sizeof (openbsd_use_fxsave
);
637 sysctl(mib
, 2, &openbsd_use_fxsave
, &size
, NULL
, 0);
640 /* OpenBSD, like NetBSD, counts mmap()ed space against the
641 * process's data size limit. If the soft limit is lower than the
642 * hard limit then try to yank it up, this lets users in the
643 * "staff" or "daemon" login classes run sbcl with larger dynamic
646 getrlimit (RLIMIT_DATA
, &rl
);
647 if (rl
.rlim_cur
< rl
.rlim_max
) {
648 rl
.rlim_cur
= rl
.rlim_max
;
649 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
651 "RUNTIME WARNING: unable to raise process data size limit:\n\
653 The system may fail to start.\n",
659 * Display a (hopefully) helpful warning if it looks like we won't
660 * be able to allocate enough memory.
662 getrlimit (RLIMIT_DATA
, &rl
);
663 if (dynamic_space_size
+ READ_ONLY_SPACE_SIZE
+ STATIC_SPACE_SIZE
+
664 LINKAGE_TABLE_SPACE_SIZE
+ wantfree
> rl
.rlim_cur
)
666 "RUNTIME WARNING: data size resource limit may be too low,\n"
667 " try decreasing the dynamic space size with --dynamic-space-size\n"
668 " or raising the datasize or datasize-max limits in /etc/login.conf\n");
671 /* OpenBSD's dlsym() relies on the gcc bulitin
672 * __builtin_return_address(0) returning an address in the
673 * executable's text segment, but when called from lisp it will return
674 * an address in the dynamic space. Work around this by calling this
675 * wrapper function instead. Note that tail-call optimization will
676 * defeat this, disable it by saving the dlsym() return value in a
680 os_dlsym(void *handle
, const char *symbol
)
682 void * volatile ret
= dlsym(handle
, symbol
);
688 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
690 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
691 * timer facility using kqueue.
698 lose("os_create_wtimer: kqueue");
703 os_wait_for_wtimer(int kq
)
707 if ( (n
= kevent(kq
, 0, 0, &ev
, 1, 0)) == -1) {
709 lose("os_wtimer_listen failed");
716 os_close_wtimer(int kq
)
719 lose("os_close_wtimer failed");
723 os_set_wtimer(int kq
, int sec
, int nsec
)
726 = ((long long) sec
) * 1000 + (long long) (nsec
+999999) / 1000000;
727 if (msec
> INT_MAX
) msec
= INT_MAX
;
730 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_ADD
|EV_ENABLE
|EV_ONESHOT
, 0, (int)msec
, 0);
731 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1)
732 perror("os_set_wtimer: kevent");
736 os_cancel_wtimer(int kq
)
739 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_DISABLE
, 0, 0, 0);
740 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1 && errno
!= ENOENT
)
741 perror("os_cancel_wtimer: kevent");