Add a declaration
[sbcl.git] / src / runtime / bsd-os.c
blobe8da71cc5e99d98caa0a96edc9dd863e44dcb55d
1 /*
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.
8 */
11 * This software is part of the SBCL system. See the README file for
12 * more information.
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.
21 #include <stdio.h>
22 #include <sys/param.h>
23 #include <sys/file.h>
24 #include <unistd.h>
25 #include <utime.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include "sbcl.h"
29 #include "./signal.h"
30 #include "os.h"
31 #include "arch.h"
32 #include "globals.h"
33 #include "interrupt.h"
34 #include "interr.h"
35 #include "lispregs.h"
36 #include "thread.h"
37 #include "runtime.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/fdefn.h"
41 #include <sys/types.h>
42 #include <signal.h>
43 /* #include <sys/sysinfo.h> */
44 #include "validate.h"
45 #if defined LISP_FEATURE_GENCGC
46 #include "gencgc-internal.h"
47 #endif
49 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
50 # include <sys/event.h>
51 #endif
54 os_vm_size_t os_vm_page_size;
56 #ifdef __NetBSD__
57 #include <sys/resource.h>
58 #include <sys/sysctl.h>
59 #include <string.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)
70 #include <sys/umtx.h>
71 #endif
73 static void freebsd_init();
74 #endif /* __FreeBSD__ */
76 #ifdef __DragonFly__
77 #include <sys/sysctl.h>
79 static void dragonfly_init();
80 #endif /* __DragonFly__ */
82 #ifdef __OpenBSD__
83 #include <sys/types.h>
84 #include <sys/resource.h>
85 #include <sys/stat.h>
86 #include <sys/sysctl.h>
87 #include <dlfcn.h>
88 #ifdef LISP_FEATURE_X86
89 #include <machine/cpu.h>
90 #endif
92 static void openbsd_init();
93 #endif
95 void
96 os_init(char *argv[], char *envp[])
98 os_vm_page_size = BACKEND_PAGE_BYTES;
100 #ifdef __NetBSD__
101 netbsd_init();
102 #elif defined(LISP_FEATURE_FREEBSD)
103 freebsd_init();
104 #elif defined(__OpenBSD__)
105 openbsd_init();
106 #elif defined(LISP_FEATURE_DARWIN)
107 darwin_init();
108 #elif defined(__DragonFly__)
109 dragonfly_init();
110 #endif
113 sigset_t *
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;
124 #else
125 #error unsupported BSD variant
126 #endif
129 os_vm_address_t
130 os_validate(os_vm_address_t addr, os_vm_size_t len)
132 int flags = MAP_PRIVATE | MAP_ANON;
134 if (addr)
135 flags |= MAP_FIXED;
137 #ifdef __NetBSD__
138 if (addr) {
139 os_vm_address_t curaddr = addr;
141 while (len > 0) {
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) {
148 perror("mmap");
150 while (curaddr > addr) {
151 curaddr -= max_allocation_size;
152 munmap(curaddr, max_allocation_size);
155 return NULL;
158 curaddr += curlen;
159 len -= curlen;
161 } else
162 #endif
164 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
167 if (addr == MAP_FAILED) {
168 perror("mmap");
169 return NULL;
172 return addr;
175 void
176 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
178 if (munmap(addr, len) == -1)
179 perror("munmap");
182 os_vm_address_t
183 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
185 addr = mmap(addr, len,
186 OS_VM_PROT_ALL,
187 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
188 fd, (off_t) offset);
190 if (addr == MAP_FAILED) {
191 perror("mmap");
192 lose("unexpected mmap(..) failure\n");
195 return addr;
198 void
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) {
202 perror("mprotect");
206 static boolean
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);
215 boolean
216 is_valid_lisp_addr(os_vm_address_t addr)
218 struct thread *th;
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))
223 return 1;
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))
227 return 1;
228 if (in_range_p(addr, (lispobj) th->binding_stack_start,
229 BINDING_STACK_SIZE))
230 return 1;
232 return 0;
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.
246 void
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);
255 #endif
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))
261 #endif
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)
269 void
270 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo,
271 os_context_t *context) {
272 lose("Unhandled memory fault. Exiting.");
274 #endif
276 void
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);
283 #else
284 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
285 #if defined(LISP_FEATURE_FREEBSD) && !defined(__GLIBC__)
286 (__siginfohandler_t *)
287 #endif
288 memory_fault_handler);
289 #endif
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);
295 # endif
296 # else
297 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
298 sig_stop_for_gc_handler);
299 # endif
300 #endif
301 SHOW("leaving os_install_interrupt_handlers()");
304 #else /* Currently PPC/Darwin/Cheney only */
306 static void
307 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
309 #if 0
310 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
311 #endif
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);
320 void
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,
325 sigsegv_handler);
328 #endif /* defined GENCGC */
330 #ifdef __NetBSD__
331 static void netbsd_init()
333 struct rlimit rl;
334 int mib[2], osrev;
335 size_t len;
337 /* Are we running on a sufficiently functional kernel? */
338 mib[0] = CTL_KERN;
339 mib[1] = KERN_OSREV;
341 len = sizeof(osrev);
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) {
356 fprintf (stderr,
357 "RUNTIME WARNING: unable to raise process data size limit:\n\
358 %s.\n\
359 The system may fail to start.\n",
360 strerror(errno));
363 max_allocation_size = (os_vm_size_t)((rl.rlim_cur / 2) &
364 ~(32 * 1024 * 1024));
366 #ifdef LISP_FEATURE_X86
368 size_t len;
369 int sse;
371 len = sizeof(sse);
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);
402 DIR *
403 _opendir(const char *filename)
405 return opendir(filename);
407 struct dirent *
408 _readdir(DIR *dirp)
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)
428 #ifndef __GLIBC__
429 extern int getosreldate(void);
430 #endif
432 int sig_memory_fault;
434 static void freebsd_init()
436 /* Memory fault signal on FreeBSD was changed from SIGBUS to
437 * SIGSEGV. */
438 #ifdef __GLIBC__
439 sig_memory_fault = SIGSEGV;
440 #else
441 if (getosreldate() < 700004)
442 sig_memory_fault = SIGBUS;
443 else
444 sig_memory_fault = SIGSEGV;
445 #endif
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
451 * x86-assem.S.
453 #ifdef LISP_FEATURE_X86
455 size_t len;
456 int instruction_sse;
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;
474 int ret;
476 if (sec < 0)
477 ret = umtx_wait((void *)lock_word, oldval, NULL);
478 else {
479 timeout.tv_sec = sec;
480 timeout.tv_nsec = usec * 1000;
481 ret = umtx_wait((void *)lock_word, oldval, &timeout);
484 switch (ret) {
485 case 0:
486 return 0;
487 case ETIMEDOUT:
488 return 1;
489 case EINTR:
490 return 2;
491 default:
492 /* EWOULDBLOCK and others, need to check the lock */
493 return -1;
498 futex_wake(int *lock_word, int n)
500 return umtx_wake((void *)lock_word, n);
502 #endif
503 #endif /* __FreeBSD__ */
505 #ifdef __DragonFly__
506 static void dragonfly_init()
508 #ifdef LISP_FEATURE_X86
509 size_t len;
510 int instruction_sse;
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)
527 int ret;
529 if (sec < 0)
530 ret = umtx_sleep(lock_word, oldval, 0);
531 else {
532 int count = usec + 1000000 * sec;
533 ret = umtx_sleep(lock_word, oldval, count);
536 if (ret == 0) return 0;
537 else {
538 switch (errno) {
539 case EWOULDBLOCK: // Operation timed out
540 return 1;
541 case EINTR:
542 return 2;
543 default: // Such as EINVAL or EBUSY
544 return -1;
550 futex_wake(int *lock_word, int n)
552 return umtx_wakeup(lock_word, n);
554 #endif
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
562 #endif
564 char *
565 os_get_runtime_executable_path(int external)
567 char path[PATH_MAX + 1];
569 #ifndef __GLIBC__
570 if (getosreldate() >= 600024) {
571 #endif
572 /* KERN_PROC_PATHNAME is available */
573 size_t len = PATH_MAX + 1;
574 int mib[4];
576 mib[0] = CTL_KERN;
577 mib[1] = KERN_PROC;
578 mib[2] = KERN_PROC_PATHNAME;
579 mib[3] = -1;
580 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
581 return NULL;
582 #ifndef __GLIBC__
583 } else {
584 int size;
585 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
586 if (size < 0)
587 return NULL;
588 path[size] = '\0';
590 #endif
591 if (strcmp(path, "unknown") == 0)
592 return NULL;
593 return copied_string(path);
595 #elif defined(LISP_FEATURE_DRAGONFLY)
596 char *
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);
601 if (size < 0)
602 return NULL;
603 path[size] = '\0';
605 if (strcmp(path, "unknown") == 0)
606 return NULL;
607 return copied_string(path);
609 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
610 char *
611 os_get_runtime_executable_path(int external)
613 struct stat sb;
614 if (!external && stat("/proc/curproc/file", &sb) == 0)
615 return copied_string("/proc/curproc/file");
616 return NULL;
618 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
619 char *
620 os_get_runtime_executable_path(int external)
622 return NULL;
624 #endif
626 #ifdef __OpenBSD__
628 int openbsd_use_fxsave = 0;
630 void
631 openbsd_init()
633 #ifdef LISP_FEATURE_X86
634 int mib[2];
635 size_t size;
636 #endif
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;
643 #else
644 const int wantfree = 32 * 1024 * 1024;
645 #endif
646 struct rlimit rl;
648 #ifdef LISP_FEATURE_X86
649 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
650 mib[0] = CTL_MACHDEP;
651 mib[1] = CPU_OSFXSR;
652 size = sizeof (openbsd_use_fxsave);
653 sysctl(mib, 2, &openbsd_use_fxsave, &size, NULL, 0);
654 #endif
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
660 * space sizes.
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) {
666 fprintf (stderr,
667 "RUNTIME WARNING: unable to raise process data size limit:\n\
668 %s.\n\
669 The system may fail to start.\n",
670 strerror(errno));
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)
681 fprintf (stderr,
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
693 * volatile variable.
695 void *
696 os_dlsym(void *handle, const char *symbol)
698 void * volatile ret = dlsym(handle, symbol);
699 return ret;
702 #endif
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.
710 os_create_wtimer()
712 int kq = kqueue();
713 if (kq == -1)
714 lose("os_create_wtimer: kqueue");
715 return kq;
719 os_wait_for_wtimer(int kq)
721 struct kevent ev;
722 int n;
723 if ( (n = kevent(kq, 0, 0, &ev, 1, 0)) == -1) {
724 if (errno != EINTR)
725 lose("os_wtimer_listen failed");
726 n = 0;
728 return n != 1;
731 void
732 os_close_wtimer(int kq)
734 if (close(kq) == -1)
735 lose("os_close_wtimer failed");
738 void
739 os_set_wtimer(int kq, int sec, int nsec)
741 long long msec
742 = ((long long) sec) * 1000 + (long long) (nsec+999999) / 1000000;
743 if (msec > INT_MAX) msec = INT_MAX;
745 struct kevent ev;
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");
751 void
752 os_cancel_wtimer(int kq)
754 struct kevent ev;
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");
759 #endif