Remove more disassembler bogosity
[sbcl.git] / src / runtime / bsd-os.c
blob2979bdf423d8cd611861ed8f7f1860174f323b07
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 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);
317 void
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,
322 sigsegv_handler);
325 #endif /* defined GENCGC */
327 #ifdef __NetBSD__
328 static void netbsd_init()
330 struct rlimit rl;
331 int mib[2], osrev;
332 size_t len;
334 /* Are we running on a sufficiently functional kernel? */
335 mib[0] = CTL_KERN;
336 mib[1] = KERN_OSREV;
338 len = sizeof(osrev);
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) {
353 fprintf (stderr,
354 "RUNTIME WARNING: unable to raise process data size limit:\n\
355 %s.\n\
356 The system may fail to start.\n",
357 strerror(errno));
360 max_allocation_size = (os_vm_size_t)((rl.rlim_cur / 2) &
361 ~(32 * 1024 * 1024));
363 #ifdef LISP_FEATURE_X86
365 size_t len;
366 int sse;
368 len = sizeof(sse);
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);
399 DIR *
400 _opendir(const char *filename)
402 return opendir(filename);
404 struct dirent *
405 _readdir(DIR *dirp)
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)
425 #ifndef __GLIBC__
426 extern int getosreldate(void);
427 #endif
429 int sig_memory_fault;
431 static void freebsd_init()
433 /* Memory fault signal on FreeBSD was changed from SIGBUS to
434 * SIGSEGV. */
435 #ifdef __GLIBC__
436 sig_memory_fault = SIGSEGV;
437 #else
438 if (getosreldate() < 700004)
439 sig_memory_fault = SIGBUS;
440 else
441 sig_memory_fault = SIGSEGV;
442 #endif
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
448 * x86-assem.S.
450 #ifdef LISP_FEATURE_X86
452 size_t len;
453 int instruction_sse;
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;
471 int ret;
473 if (sec < 0)
474 ret = umtx_wait((void *)lock_word, oldval, NULL);
475 else {
476 timeout.tv_sec = sec;
477 timeout.tv_nsec = usec * 1000;
478 ret = umtx_wait((void *)lock_word, oldval, &timeout);
481 switch (ret) {
482 case 0:
483 return 0;
484 case ETIMEDOUT:
485 return 1;
486 case EINTR:
487 return 2;
488 default:
489 /* EWOULDBLOCK and others, need to check the lock */
490 return -1;
495 futex_wake(int *lock_word, int n)
497 return umtx_wake((void *)lock_word, n);
499 #endif
500 #endif /* __FreeBSD__ */
502 #ifdef __DragonFly__
503 static void dragonfly_init()
505 #ifdef LISP_FEATURE_X86
506 size_t len;
507 int instruction_sse;
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)
524 int ret;
526 if (sec < 0)
527 ret = umtx_sleep(lock_word, oldval, 0);
528 else {
529 int count = usec + 1000000 * sec;
530 ret = umtx_sleep(lock_word, oldval, count);
533 if (ret == 0) return 0;
534 else {
535 switch (errno) {
536 case EWOULDBLOCK: // Operation timed out
537 return 1;
538 case EINTR:
539 return 2;
540 default: // Such as EINVAL or EBUSY
541 return -1;
547 futex_wake(int *lock_word, int n)
549 return umtx_wakeup(lock_word, n);
551 #endif
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
559 #endif
561 char *
562 os_get_runtime_executable_path(int external)
564 char path[PATH_MAX + 1];
566 #ifndef __GLIBC__
567 if (getosreldate() >= 600024) {
568 #endif
569 /* KERN_PROC_PATHNAME is available */
570 size_t len = PATH_MAX + 1;
571 int mib[4];
573 mib[0] = CTL_KERN;
574 mib[1] = KERN_PROC;
575 mib[2] = KERN_PROC_PATHNAME;
576 mib[3] = -1;
577 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
578 return NULL;
579 #ifndef __GLIBC__
580 } else {
581 int size;
582 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
583 if (size < 0)
584 return NULL;
585 path[size] = '\0';
587 #endif
588 if (strcmp(path, "unknown") == 0)
589 return NULL;
590 return copied_string(path);
592 #elif defined(LISP_FEATURE_DRAGONFLY)
593 char *
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);
598 if (size < 0)
599 return NULL;
600 path[size] = '\0';
602 if (strcmp(path, "unknown") == 0)
603 return NULL;
604 return copied_string(path);
606 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
607 char *
608 os_get_runtime_executable_path(int external)
610 struct stat sb;
611 if (!external && stat("/proc/curproc/file", &sb) == 0)
612 return copied_string("/proc/curproc/file");
613 return NULL;
615 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
616 char *
617 os_get_runtime_executable_path(int external)
619 return NULL;
621 #endif
623 #ifdef __OpenBSD__
625 int openbsd_use_fxsave = 0;
627 void
628 openbsd_init()
630 #ifdef LISP_FEATURE_X86
631 int mib[2];
632 size_t size;
633 #endif
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;
640 #else
641 const int wantfree = 32 * 1024 * 1024;
642 #endif
643 struct rlimit rl;
645 #ifdef LISP_FEATURE_X86
646 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
647 mib[0] = CTL_MACHDEP;
648 mib[1] = CPU_OSFXSR;
649 size = sizeof (openbsd_use_fxsave);
650 sysctl(mib, 2, &openbsd_use_fxsave, &size, NULL, 0);
651 #endif
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
657 * space sizes.
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) {
663 fprintf (stderr,
664 "RUNTIME WARNING: unable to raise process data size limit:\n\
665 %s.\n\
666 The system may fail to start.\n",
667 strerror(errno));
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)
678 fprintf (stderr,
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
690 * volatile variable.
692 void *
693 os_dlsym(void *handle, const char *symbol)
695 void * volatile ret = dlsym(handle, symbol);
696 return ret;
699 #endif
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.
707 os_create_wtimer()
709 int kq = kqueue();
710 if (kq == -1)
711 lose("os_create_wtimer: kqueue");
712 return kq;
716 os_wait_for_wtimer(int kq)
718 struct kevent ev;
719 int n;
720 if ( (n = kevent(kq, 0, 0, &ev, 1, 0)) == -1) {
721 if (errno != EINTR)
722 lose("os_wtimer_listen failed");
723 n = 0;
725 return n != 1;
728 void
729 os_close_wtimer(int kq)
731 if (close(kq) == -1)
732 lose("os_close_wtimer failed");
735 void
736 os_set_wtimer(int kq, int sec, int nsec)
738 long long msec
739 = ((long long) sec) * 1000 + (long long) (nsec+999999) / 1000000;
740 if (msec > INT_MAX) msec = INT_MAX;
742 struct kevent ev;
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");
748 void
749 os_cancel_wtimer(int kq)
751 struct kevent ev;
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");
756 #endif