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
);
163 addr
= mmap(addr
, len
, OS_VM_PROT_ALL
, flags
, -1, 0);
168 if (addr
== MAP_FAILED
) {
177 os_invalidate(os_vm_address_t addr
, os_vm_size_t len
)
179 if (munmap(addr
, len
) == -1)
184 os_map(int fd
, int offset
, os_vm_address_t addr
, os_vm_size_t len
)
186 addr
= mmap(addr
, len
,
188 MAP_PRIVATE
| MAP_FILE
| MAP_FIXED
,
191 if (addr
== MAP_FAILED
) {
193 lose("unexpected mmap(..) failure\n");
200 os_protect(os_vm_address_t address
, os_vm_size_t length
, os_vm_prot_t prot
)
202 if (mprotect(address
, length
, prot
) == -1) {
208 in_range_p(os_vm_address_t a
, lispobj sbeg
, size_t slen
)
210 char* beg
= (char*) sbeg
;
211 char* end
= (char*) sbeg
+ slen
;
212 char* adr
= (char*) a
;
213 return (adr
>= beg
&& adr
< end
);
217 is_valid_lisp_addr(os_vm_address_t addr
)
221 if (in_range_p(addr
, READ_ONLY_SPACE_START
, READ_ONLY_SPACE_SIZE
) ||
222 in_range_p(addr
, STATIC_SPACE_START
, STATIC_SPACE_SIZE
) ||
223 in_range_p(addr
, DYNAMIC_SPACE_START
, dynamic_space_size
))
225 for_each_thread(th
) {
226 if (((os_vm_address_t
)th
->control_stack_start
<= addr
) &&
227 (addr
< (os_vm_address_t
)th
->control_stack_end
))
229 if (in_range_p(addr
, (lispobj
) th
->binding_stack_start
,
237 * any OS-dependent special low-level handling for signals
240 #if defined LISP_FEATURE_GENCGC
243 * The GENCGC needs to be hooked into whatever signal is raised for
244 * page fault on this OS.
248 memory_fault_handler(int signal
, siginfo_t
*siginfo
, os_context_t
*context
)
250 void *fault_addr
= arch_get_bad_addr(signal
, siginfo
, context
);
252 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
253 FSHOW_SIGNAL((stderr
, "/ TLS: restoring fs: %p in memory_fault_handler\n",
254 *CONTEXT_ADDR_FROM_STEM(fs
)));
255 os_restore_tls_segment_register(context
);
258 FSHOW((stderr
, "Memory fault at: %p, PC: %p\n", fault_addr
, *os_context_pc_addr(context
)));
260 #ifdef LISP_FEATURE_SB_SAFEPOINT
261 if (!handle_safepoint_violation(context
, fault_addr
))
264 if (!gencgc_handle_wp_violation(fault_addr
))
265 if(!handle_guard_page_triggered(context
,fault_addr
))
266 lisp_memory_fault_error(context
, fault_addr
);
269 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
271 mach_error_memory_fault_handler(int signal
, siginfo_t
*siginfo
,
272 os_context_t
*context
) {
273 lose("Unhandled memory fault. Exiting.");
278 os_install_interrupt_handlers(void)
280 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
281 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
282 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
283 mach_error_memory_fault_handler
);
285 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
286 #if defined(LISP_FEATURE_FREEBSD) && !defined(__GLIBC__)
287 (__siginfohandler_t
*)
289 memory_fault_handler
);
292 #ifdef LISP_FEATURE_SB_THREAD
293 # ifdef LISP_FEATURE_SB_SAFEPOINT
294 # ifdef LISP_FEATURE_SB_THRUPTION
295 undoably_install_low_level_interrupt_handler(SIGPIPE
, thruption_handler
);
298 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC
,
299 sig_stop_for_gc_handler
);
302 SHOW("leaving os_install_interrupt_handlers()");
305 #else /* Currently PPC/Darwin/Cheney only */
308 sigsegv_handler(int signal
, siginfo_t
*info
, os_context_t
*context
)
311 unsigned int pc
= (unsigned int *)(*os_context_pc_addr(context
));
313 os_vm_address_t addr
;
315 addr
= arch_get_bad_addr(signal
, info
, context
);
316 if (!cheneygc_handle_wp_violation(context
, addr
))
317 if (!handle_guard_page_triggered(context
, addr
))
318 interrupt_handle_now(signal
, info
, context
);
322 os_install_interrupt_handlers(void)
324 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
325 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
329 #endif /* defined GENCGC */
332 static void netbsd_init()
338 /* Are we running on a sufficiently functional kernel? */
343 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
345 /* If we're older than 2.0... */
346 if (osrev
< 200000000) {
347 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
348 lose("NetBSD kernel too old to run sbcl.\n");
351 /* NetBSD counts mmap()ed space against the process's data size limit,
352 * so yank it up. This might be a nasty thing to do? */
353 getrlimit (RLIMIT_DATA
, &rl
);
354 if (rl
.rlim_cur
< rl
.rlim_max
) {
355 rl
.rlim_cur
= rl
.rlim_max
;
356 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
358 "RUNTIME WARNING: unable to raise process data size limit:\n\
360 The system may fail to start.\n",
364 max_allocation_size
= (os_vm_size_t
)((rl
.rlim_cur
/ 2) &
365 ~(32 * 1024 * 1024));
367 #ifdef LISP_FEATURE_X86
373 if (sysctlbyname("machdep.sse", &sse
, &len
,
374 NULL
, 0) == 0 && sse
!= 0) {
375 /* Use the SSE detector */
376 fast_bzero_pointer
= fast_bzero_detect
;
379 #endif /* LISP_FEATURE_X86 */
382 /* Various routines in NetBSD's C library are compatibility wrappers
383 for old versions. Programs must be processed by the C toolchain in
384 order to get up-to-date definitions of such routines. */
385 /* The stat-family, opendir, and readdir are used only in sb-posix, as
386 of 2007-01-16. -- RMK */
388 _stat(const char *path
, struct stat
*sb
)
390 return stat(path
, sb
);
393 _lstat(const char *path
, struct stat
*sb
)
395 return lstat(path
, sb
);
398 _fstat(int fd
, struct stat
*sb
)
400 return fstat(fd
, sb
);
404 _opendir(const char *filename
)
406 return opendir(filename
);
411 return readdir(dirp
);
415 _utime(const char *file
, const struct utimbuf
*timep
)
417 return utime(file
, timep
);
420 /* Used in sb-bsd-sockets. */
422 _socket(int domain
, int type
, int protocol
)
424 return socket(domain
, type
, protocol
);
426 #endif /* __NetBSD__ */
428 #if defined(LISP_FEATURE_FREEBSD)
430 extern int getosreldate(void);
433 int sig_memory_fault
;
435 static void freebsd_init()
437 /* Memory fault signal on FreeBSD was changed from SIGBUS to
440 sig_memory_fault
= SIGSEGV
;
442 if (getosreldate() < 700004)
443 sig_memory_fault
= SIGBUS
;
445 sig_memory_fault
= SIGSEGV
;
448 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
449 * 4.x with GENERIC kernel, does not enable SSE support even on
450 * SSE capable CPUs". Detect this situation and skip the
451 * fast_bzero sse/base selection logic that's normally done in
454 #ifdef LISP_FEATURE_X86
459 len
= sizeof(instruction_sse
);
460 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
461 NULL
, 0) == 0 && instruction_sse
!= 0) {
462 /* Use the SSE detector */
463 fast_bzero_pointer
= fast_bzero_detect
;
466 #endif /* LISP_FEATURE_X86 */
469 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
470 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
472 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
474 struct timespec timeout
;
478 ret
= umtx_wait((void *)lock_word
, oldval
, NULL
);
480 timeout
.tv_sec
= sec
;
481 timeout
.tv_nsec
= usec
* 1000;
482 ret
= umtx_wait((void *)lock_word
, oldval
, &timeout
);
493 /* EWOULDBLOCK and others, need to check the lock */
499 futex_wake(int *lock_word
, int n
)
501 return umtx_wake((void *)lock_word
, n
);
504 #endif /* __FreeBSD__ */
507 static void dragonfly_init()
509 #ifdef LISP_FEATURE_X86
513 len
= sizeof(instruction_sse
);
514 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
,
515 NULL
, 0) == 0 && instruction_sse
!= 0) {
516 /* Use the SSE detector */
517 fast_bzero_pointer
= fast_bzero_detect
;
519 #endif /* LISP_FEATURE_X86 */
523 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
524 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
526 futex_wait(int *lock_word
, long oldval
, long sec
, unsigned long usec
)
531 ret
= umtx_sleep(lock_word
, oldval
, 0);
533 int count
= usec
+ 1000000 * sec
;
534 ret
= umtx_sleep(lock_word
, oldval
, count
);
537 if (ret
== 0) return 0;
540 case EWOULDBLOCK
: // Operation timed out
544 default: // Such as EINVAL or EBUSY
551 futex_wake(int *lock_word
, int n
)
553 return umtx_wakeup(lock_word
, n
);
556 #endif /* __DragonFly__ */
558 #ifdef LISP_FEATURE_DARWIN
559 /* defined in ppc-darwin-os.c instead */
560 #elif defined(LISP_FEATURE_FREEBSD)
561 #ifndef KERN_PROC_PATHNAME
562 #define KERN_PROC_PATHNAME 12
566 os_get_runtime_executable_path(int external
)
568 char path
[PATH_MAX
+ 1];
571 if (getosreldate() >= 600024) {
573 /* KERN_PROC_PATHNAME is available */
574 size_t len
= PATH_MAX
+ 1;
579 mib
[2] = KERN_PROC_PATHNAME
;
581 if (sysctl(mib
, 4, &path
, &len
, NULL
, 0) != 0)
586 size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
592 if (strcmp(path
, "unknown") == 0)
594 return copied_string(path
);
596 #elif defined(LISP_FEATURE_DRAGONFLY)
598 os_get_runtime_executable_path(int external
)
600 char path
[PATH_MAX
+ 1];
601 int size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
606 if (strcmp(path
, "unknown") == 0)
608 return copied_string(path
);
610 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
612 os_get_runtime_executable_path(int external
)
615 if (!external
&& stat("/proc/curproc/file", &sb
) == 0)
616 return copied_string("/proc/curproc/file");
619 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
621 os_get_runtime_executable_path(int external
)
629 int openbsd_use_fxsave
= 0;
634 #ifdef LISP_FEATURE_X86
639 * Show a warning if it looks like the memory available after
640 * allocating the spaces won't be at least this much.
642 #ifdef LISP_FEATURE_X86_64
643 const int wantfree
= 64 * 1024 * 1024;
645 const int wantfree
= 32 * 1024 * 1024;
649 #ifdef LISP_FEATURE_X86
650 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
651 mib
[0] = CTL_MACHDEP
;
653 size
= sizeof (openbsd_use_fxsave
);
654 sysctl(mib
, 2, &openbsd_use_fxsave
, &size
, NULL
, 0);
657 /* OpenBSD, like NetBSD, counts mmap()ed space against the
658 * process's data size limit. If the soft limit is lower than the
659 * hard limit then try to yank it up, this lets users in the
660 * "staff" or "daemon" login classes run sbcl with larger dynamic
663 getrlimit (RLIMIT_DATA
, &rl
);
664 if (rl
.rlim_cur
< rl
.rlim_max
) {
665 rl
.rlim_cur
= rl
.rlim_max
;
666 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
668 "RUNTIME WARNING: unable to raise process data size limit:\n\
670 The system may fail to start.\n",
676 * Display a (hopefully) helpful warning if it looks like we won't
677 * be able to allocate enough memory.
679 getrlimit (RLIMIT_DATA
, &rl
);
680 if (dynamic_space_size
+ READ_ONLY_SPACE_SIZE
+ STATIC_SPACE_SIZE
+
681 LINKAGE_TABLE_SPACE_SIZE
+ wantfree
> rl
.rlim_cur
)
683 "RUNTIME WARNING: data size resource limit may be too low,\n"
684 " try decreasing the dynamic space size with --dynamic-space-size\n"
685 " or raising the datasize or datasize-max limits in /etc/login.conf\n");
688 /* OpenBSD's dlsym() relies on the gcc bulitin
689 * __builtin_return_address(0) returning an address in the
690 * executable's text segment, but when called from lisp it will return
691 * an address in the dynamic space. Work around this by calling this
692 * wrapper function instead. Note that tail-call optimization will
693 * defeat this, disable it by saving the dlsym() return value in a
697 os_dlsym(void *handle
, const char *symbol
)
699 void * volatile ret
= dlsym(handle
, symbol
);
705 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
707 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
708 * timer facility using kqueue.
715 lose("os_create_wtimer: kqueue");
720 os_wait_for_wtimer(int kq
)
724 if ( (n
= kevent(kq
, 0, 0, &ev
, 1, 0)) == -1) {
726 lose("os_wtimer_listen failed");
733 os_close_wtimer(int kq
)
736 lose("os_close_wtimer failed");
740 os_set_wtimer(int kq
, int sec
, int nsec
)
743 = ((long long) sec
) * 1000 + (long long) (nsec
+999999) / 1000000;
744 if (msec
> INT_MAX
) msec
= INT_MAX
;
747 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_ADD
|EV_ENABLE
|EV_ONESHOT
, 0, (int)msec
, 0);
748 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1)
749 perror("os_set_wtimer: kevent");
753 os_cancel_wtimer(int kq
)
756 EV_SET(&ev
, 1, EVFILT_TIMER
, EV_DISABLE
, 0, 0, 0);
757 if (kevent(kq
, &ev
, 1, 0, 0, 0) == -1 && errno
!= ENOENT
)
758 perror("os_cancel_wtimer: kevent");