Reduce pinned object table size, part 1 of 2.
[sbcl.git] / src / runtime / bsd-os.c
blobb72468af8a46cd121482bc46966a1a5d79c017cd
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 void
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) {
186 perror("mprotect");
190 static boolean
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);
199 boolean
200 is_valid_lisp_addr(os_vm_address_t addr)
202 struct thread *th;
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) ||
208 #endif
209 in_range_p(addr, DYNAMIC_SPACE_START, dynamic_space_size))
210 return 1;
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))
214 return 1;
215 if (in_range_p(addr, (lispobj) th->binding_stack_start,
216 BINDING_STACK_SIZE))
217 return 1;
219 return 0;
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.
233 void
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);
242 #endif
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))
248 #endif
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)
256 void
257 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo,
258 os_context_t *context) {
259 lose("Unhandled memory fault. Exiting.");
261 #endif
263 void
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);
270 #else
271 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
272 #if defined(LISP_FEATURE_FREEBSD) && !defined(__GLIBC__)
273 (__siginfohandler_t *)
274 #endif
275 memory_fault_handler);
276 #endif
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);
282 # endif
283 # else
284 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
285 sig_stop_for_gc_handler);
286 # endif
287 #endif
288 SHOW("leaving os_install_interrupt_handlers()");
291 #else /* Currently PPC/Darwin/Cheney only */
293 static void
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);
304 void
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,
309 sigsegv_handler);
312 #endif /* defined GENCGC */
314 #ifdef __NetBSD__
315 static void netbsd_init()
317 struct rlimit rl;
318 int mib[2], osrev;
319 size_t len;
321 /* Are we running on a sufficiently functional kernel? */
322 mib[0] = CTL_KERN;
323 mib[1] = KERN_OSREV;
325 len = sizeof(osrev);
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) {
340 fprintf (stderr,
341 "RUNTIME WARNING: unable to raise process data size limit:\n\
342 %s.\n\
343 The system may fail to start.\n",
344 strerror(errno));
347 max_allocation_size = (os_vm_size_t)((rl.rlim_cur / 2) &
348 ~(32 * 1024 * 1024));
350 #ifdef LISP_FEATURE_X86
352 size_t len;
353 int sse;
355 len = sizeof(sse);
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);
386 DIR *
387 _opendir(const char *filename)
389 return opendir(filename);
391 struct dirent *
392 _readdir(DIR *dirp)
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)
412 #ifndef __GLIBC__
413 extern int getosreldate(void);
414 #endif
416 int sig_memory_fault;
418 static void freebsd_init()
420 /* Memory fault signal on FreeBSD was changed from SIGBUS to
421 * SIGSEGV. */
422 #ifdef __GLIBC__
423 sig_memory_fault = SIGSEGV;
424 #else
425 if (getosreldate() < 700004)
426 sig_memory_fault = SIGBUS;
427 else
428 sig_memory_fault = SIGSEGV;
429 #endif
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
435 * x86-assem.S.
437 #ifdef LISP_FEATURE_X86
439 size_t len;
440 int instruction_sse;
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;
458 int ret;
460 if (sec < 0)
461 ret = umtx_wait((void *)lock_word, oldval, NULL);
462 else {
463 timeout.tv_sec = sec;
464 timeout.tv_nsec = usec * 1000;
465 ret = umtx_wait((void *)lock_word, oldval, &timeout);
468 switch (ret) {
469 case 0:
470 return 0;
471 case ETIMEDOUT:
472 return 1;
473 case EINTR:
474 return 2;
475 default:
476 /* EWOULDBLOCK and others, need to check the lock */
477 return -1;
482 futex_wake(int *lock_word, int n)
484 return umtx_wake((void *)lock_word, n);
486 #endif
487 #endif /* __FreeBSD__ */
489 #ifdef __DragonFly__
490 static void dragonfly_init()
492 #ifdef LISP_FEATURE_X86
493 size_t len;
494 int instruction_sse;
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)
511 int ret;
513 if (sec < 0)
514 ret = umtx_sleep(lock_word, oldval, 0);
515 else {
516 int count = usec + 1000000 * sec;
517 ret = umtx_sleep(lock_word, oldval, count);
520 if (ret == 0) return 0;
521 else {
522 switch (errno) {
523 case EWOULDBLOCK: // Operation timed out
524 return 1;
525 case EINTR:
526 return 2;
527 default: // Such as EINVAL or EBUSY
528 return -1;
534 futex_wake(int *lock_word, int n)
536 return umtx_wakeup(lock_word, n);
538 #endif
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
546 #endif
548 char *
549 os_get_runtime_executable_path(int external)
551 char path[PATH_MAX + 1];
553 #ifndef __GLIBC__
554 if (getosreldate() >= 600024) {
555 #endif
556 /* KERN_PROC_PATHNAME is available */
557 size_t len = PATH_MAX + 1;
558 int mib[4];
560 mib[0] = CTL_KERN;
561 mib[1] = KERN_PROC;
562 mib[2] = KERN_PROC_PATHNAME;
563 mib[3] = -1;
564 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
565 return NULL;
566 #ifndef __GLIBC__
567 } else {
568 int size;
569 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
570 if (size < 0)
571 return NULL;
572 path[size] = '\0';
574 #endif
575 if (strcmp(path, "unknown") == 0)
576 return NULL;
577 return copied_string(path);
579 #elif defined(LISP_FEATURE_DRAGONFLY)
580 char *
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);
585 if (size < 0)
586 return NULL;
587 path[size] = '\0';
589 if (strcmp(path, "unknown") == 0)
590 return NULL;
591 return copied_string(path);
593 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
594 char *
595 os_get_runtime_executable_path(int external)
597 struct stat sb;
598 if (!external && stat("/proc/curproc/file", &sb) == 0)
599 return copied_string("/proc/curproc/file");
600 return NULL;
602 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD or DragonFly */
603 char *
604 os_get_runtime_executable_path(int external)
606 return NULL;
608 #endif
610 #ifdef __OpenBSD__
612 int openbsd_use_fxsave = 0;
614 void
615 openbsd_init()
617 #ifdef LISP_FEATURE_X86
618 int mib[2];
619 size_t size;
620 #endif
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;
627 #else
628 const int wantfree = 32 * 1024 * 1024;
629 #endif
630 struct rlimit rl;
632 #ifdef LISP_FEATURE_X86
633 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
634 mib[0] = CTL_MACHDEP;
635 mib[1] = CPU_OSFXSR;
636 size = sizeof (openbsd_use_fxsave);
637 sysctl(mib, 2, &openbsd_use_fxsave, &size, NULL, 0);
638 #endif
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
644 * space sizes.
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) {
650 fprintf (stderr,
651 "RUNTIME WARNING: unable to raise process data size limit:\n\
652 %s.\n\
653 The system may fail to start.\n",
654 strerror(errno));
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)
665 fprintf (stderr,
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
677 * volatile variable.
679 void *
680 os_dlsym(void *handle, const char *symbol)
682 void * volatile ret = dlsym(handle, symbol);
683 return ret;
686 #endif
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.
694 os_create_wtimer()
696 int kq = kqueue();
697 if (kq == -1)
698 lose("os_create_wtimer: kqueue");
699 return kq;
703 os_wait_for_wtimer(int kq)
705 struct kevent ev;
706 int n;
707 if ( (n = kevent(kq, 0, 0, &ev, 1, 0)) == -1) {
708 if (errno != EINTR)
709 lose("os_wtimer_listen failed");
710 n = 0;
712 return n != 1;
715 void
716 os_close_wtimer(int kq)
718 if (close(kq) == -1)
719 lose("os_close_wtimer failed");
722 void
723 os_set_wtimer(int kq, int sec, int nsec)
725 long long msec
726 = ((long long) sec) * 1000 + (long long) (nsec+999999) / 1000000;
727 if (msec > INT_MAX) msec = INT_MAX;
729 struct kevent ev;
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");
735 void
736 os_cancel_wtimer(int kq)
738 struct kevent ev;
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");
743 #endif