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
)
309 os_vm_address_t addr
;
311 addr
= arch_get_bad_addr(signal
, info
, context
);
312 if (!cheneygc_handle_wp_violation(context
, addr
))
313 if (!handle_guard_page_triggered(context
, addr
))
314 interrupt_handle_now(signal
, info
, context
);
318 os_install_interrupt_handlers(void)
320 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
321 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
325 #endif /* defined GENCGC */
328 static void netbsd_init()
334 /* Are we running on a sufficiently functional kernel? */
339 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
341 /* If we're older than 2.0... */
342 if (osrev
< 200000000) {
343 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
344 lose("NetBSD kernel too old to run sbcl.\n");
347 /* NetBSD counts mmap()ed space against the process's data size limit,
348 * so yank it up. This might be a nasty thing to do? */
349 getrlimit (RLIMIT_DATA
, &rl
);
350 if (rl
.rlim_cur
< rl
.rlim_max
) {
351 rl
.rlim_cur
= rl
.rlim_max
;
352 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
354 "RUNTIME WARNING: unable to raise process data size limit:\n\
356 The system may fail to start.\n",
360 max_allocation_size
= (os_vm_size_t
)((rl
.rlim_cur
/ 2) &
361 ~(32 * 1024 * 1024));
363 #ifdef LISP_FEATURE_X86
369 if (sysctlbyname("machdep.sse", &sse
, &len
,
370 NULL
, 0) == 0 && sse
!= 0) {
371 /* Use the SSE detector */
372 fast_bzero_pointer
= fast_bzero_detect
;
375 #endif /* LISP_FEATURE_X86 */
378 /* Various routines in NetBSD's C library are compatibility wrappers
379 for old versions. Programs must be processed by the C toolchain in
380 order to get up-to-date definitions of such routines. */
381 /* The stat-family, opendir, and readdir are used only in sb-posix, as
382 of 2007-01-16. -- RMK */
384 _stat(const char *path
, struct stat
*sb
)
386 return stat(path
, sb
);
389 _lstat(const char *path
, struct stat
*sb
)
391 return lstat(path
, sb
);
394 _fstat(int fd
, struct stat
*sb
)
396 return fstat(fd
, sb
);
400 _opendir(const char *filename
)
402 return opendir(filename
);
407 return readdir(dirp
);
411 _utime(const char *file
, const struct utimbuf
*timep
)
413 return utime(file
, timep
);
416 /* Used in sb-bsd-sockets. */
418 _socket(int domain
, int type
, int protocol
)
420 return socket(domain
, type
, protocol
);
422 #endif /* __NetBSD__ */
424 #if defined(LISP_FEATURE_FREEBSD)
426 extern int getosreldate(void);
429 int sig_memory_fault
;
431 static void freebsd_init()
433 /* Memory fault signal on FreeBSD was changed from SIGBUS to
436 sig_memory_fault
= SIGSEGV
;
438 if (getosreldate() < 700004)
439 sig_memory_fault
= SIGBUS
;
441 sig_memory_fault
= SIGSEGV
;
444 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
445 * 4.x with GENERIC kernel, does not enable SSE support even on
446 * SSE capable CPUs". Detect this situation and skip the
447 * fast_bzero sse/base selection logic that's normally done in
450 #ifdef LISP_FEATURE_X86
455 len
= sizeof(instruction_sse
);
456 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
457 NULL
, 0) == 0 && instruction_sse
!= 0) {
458 /* Use the SSE detector */
459 fast_bzero_pointer
= fast_bzero_detect
;
462 #endif /* LISP_FEATURE_X86 */
465 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
466 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
468 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
470 struct timespec timeout
;
474 ret
= umtx_wait((void *)lock_word
, oldval
, NULL
);
476 timeout
.tv_sec
= sec
;
477 timeout
.tv_nsec
= usec
* 1000;
478 ret
= umtx_wait((void *)lock_word
, oldval
, &timeout
);
489 /* EWOULDBLOCK and others, need to check the lock */
495 futex_wake(int *lock_word
, int n
)
497 return umtx_wake((void *)lock_word
, n
);
500 #endif /* __FreeBSD__ */
503 static void dragonfly_init()
505 #ifdef LISP_FEATURE_X86
509 len
= sizeof(instruction_sse
);
510 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
511 NULL
, 0) == 0 && instruction_sse
!= 0) {
512 /* Use the SSE detector */
513 fast_bzero_pointer
= fast_bzero_detect
;
515 #endif /* LISP_FEATURE_X86 */
519 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
520 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
522 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
527 ret
= umtx_sleep(lock_word
, oldval
, 0);
529 int count
= usec
+ 1000000 * sec
;
530 ret
= umtx_sleep(lock_word
, oldval
, count
);
533 if (ret
== 0) return 0;
536 case EWOULDBLOCK
: // Operation timed out
540 default: // Such as EINVAL or EBUSY
547 futex_wake(int *lock_word
, int n
)
549 return umtx_wakeup(lock_word
, n
);
552 #endif /* __DragonFly__ */
554 #ifdef LISP_FEATURE_DARWIN
555 /* defined in ppc-darwin-os.c instead */
556 #elif defined(LISP_FEATURE_FREEBSD)
557 #ifndef KERN_PROC_PATHNAME
558 #define KERN_PROC_PATHNAME 12
562 os_get_runtime_executable_path(int external
)
564 char path
[PATH_MAX
+ 1];
567 if (getosreldate() >= 600024) {
569 /* KERN_PROC_PATHNAME is available */
570 size_t len
= PATH_MAX
+ 1;
575 mib
[2] = KERN_PROC_PATHNAME
;
577 if (sysctl(mib
, 4, &path
, &len
, NULL
, 0) != 0)
582 size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
588 if (strcmp(path
, "unknown") == 0)
590 return copied_string(path
);
592 #elif defined(LISP_FEATURE_DRAGONFLY)
594 os_get_runtime_executable_path(int external
)
596 char path
[PATH_MAX
+ 1];
597 int size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
602 if (strcmp(path
, "unknown") == 0)
604 return copied_string(path
);
606 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
608 os_get_runtime_executable_path(int external
)
611 if (!external
&& stat("/proc/curproc/file", &sb
) == 0)
612 return copied_string("/proc/curproc/file");
615 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
617 os_get_runtime_executable_path(int external
)
625 int openbsd_use_fxsave
= 0;
630 #ifdef LISP_FEATURE_X86
635 * Show a warning if it looks like the memory available after
636 * allocating the spaces won't be at least this much.
638 #ifdef LISP_FEATURE_64_BIT
639 const int wantfree
= 64 * 1024 * 1024;
641 const int wantfree
= 32 * 1024 * 1024;
645 #ifdef LISP_FEATURE_X86
646 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
647 mib
[0] = CTL_MACHDEP
;
649 size
= sizeof (openbsd_use_fxsave
);
650 sysctl(mib
, 2, &openbsd_use_fxsave
, &size
, NULL
, 0);
653 /* OpenBSD, like NetBSD, counts mmap()ed space against the
654 * process's data size limit. If the soft limit is lower than the
655 * hard limit then try to yank it up, this lets users in the
656 * "staff" or "daemon" login classes run sbcl with larger dynamic
659 getrlimit (RLIMIT_DATA
, &rl
);
660 if (rl
.rlim_cur
< rl
.rlim_max
) {
661 rl
.rlim_cur
= rl
.rlim_max
;
662 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
664 "RUNTIME WARNING: unable to raise process data size limit:\n\
666 The system may fail to start.\n",
672 * Display a (hopefully) helpful warning if it looks like we won't
673 * be able to allocate enough memory.
675 getrlimit (RLIMIT_DATA
, &rl
);
676 if (dynamic_space_size
+ READ_ONLY_SPACE_SIZE
+ STATIC_SPACE_SIZE
+
677 LINKAGE_TABLE_SPACE_SIZE
+ wantfree
> rl
.rlim_cur
)
679 "RUNTIME WARNING: data size resource limit may be too low,\n"
680 " try decreasing the dynamic space size with --dynamic-space-size\n"
681 " or raising the datasize or datasize-max limits in /etc/login.conf\n");
684 /* OpenBSD's dlsym() relies on the gcc bulitin
685 * __builtin_return_address(0) returning an address in the
686 * executable's text segment, but when called from lisp it will return
687 * an address in the dynamic space. Work around this by calling this
688 * wrapper function instead. Note that tail-call optimization will
689 * defeat this, disable it by saving the dlsym() return value in a
693 os_dlsym(void *handle
, const char *symbol
)
695 void * volatile ret
= dlsym(handle
, symbol
);
701 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
703 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
704 * timer facility using kqueue.
711 lose("os_create_wtimer: kqueue");
716 os_wait_for_wtimer(int kq
)
720 if ( (n
= kevent(kq
, 0, 0, &ev
, 1, 0)) == -1) {
722 lose("os_wtimer_listen failed");
729 os_close_wtimer(int kq
)
732 lose("os_close_wtimer failed");
736 os_set_wtimer(int kq
, int sec
, int nsec
)
739 = ((long long) sec
) * 1000 + (long long) (nsec
+999999) / 1000000;
740 if (msec
> INT_MAX
) msec
= INT_MAX
;
743 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_ADD
|EV_ENABLE
|EV_ONESHOT
, 0, (int)msec
, 0);
744 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1)
745 perror("os_set_wtimer: kevent");
749 os_cancel_wtimer(int kq
)
752 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_DISABLE
, 0, 0, 0);
753 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1 && errno
!= ENOENT
)
754 perror("os_cancel_wtimer: kevent");