1.0.13.18: Revived OpenBSD support, contributed by Josh Elsasser
[sbcl/simd.git] / src / runtime / bsd-os.c
bloba364d5a0b0ef5f69e5d441df7961b8b17e559da2
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 <assert.h>
26 #include <errno.h>
27 #include "sbcl.h"
28 #include "./signal.h"
29 #include "os.h"
30 #include "arch.h"
31 #include "globals.h"
32 #include "interrupt.h"
33 #include "interr.h"
34 #include "lispregs.h"
35 #include "thread.h"
36 #include "runtime.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/fdefn.h"
40 #include <sys/types.h>
41 #include <signal.h>
42 /* #include <sys/sysinfo.h> */
43 #include "validate.h"
44 #if defined LISP_FEATURE_GENCGC
45 #include "gencgc-internal.h"
46 #endif
48 os_vm_size_t os_vm_page_size;
50 #ifdef __NetBSD__
51 #include <sys/resource.h>
52 #include <sys/sysctl.h>
53 #include <string.h>
54 #include <sys/stat.h> /* For the stat-family wrappers. */
55 #include <dirent.h> /* For the opendir()/readdir() wrappers */
56 #include <sys/socket.h> /* For the socket() wrapper */
57 static void netbsd_init();
58 #endif /* __NetBSD__ */
60 #ifdef __FreeBSD__
61 #include <sys/sysctl.h>
62 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
63 #include <sys/umtx.h>
64 #endif
66 static void freebsd_init();
67 #endif /* __FreeBSD__ */
69 #ifdef __OpenBSD__
70 #include <sys/types.h>
71 #include <sys/resource.h>
72 #include <sys/stat.h>
73 #include <dlfcn.h>
75 static void openbsd_init();
76 #endif
78 void
79 os_init(char *argv[], char *envp[])
81 os_vm_page_size = getpagesize();
83 #ifdef __NetBSD__
84 netbsd_init();
85 #elif defined(__FreeBSD__)
86 freebsd_init();
87 #elif defined(__OpenBSD__)
88 openbsd_init();
89 #endif
92 sigset_t *
93 os_context_sigmask_addr(os_context_t *context)
95 /* (Unlike most of the other context fields that we access, the
96 * signal mask field is a field of the basic, outermost context
97 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
98 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
99 return &context->uc_sigmask;
100 #elif defined (__OpenBSD__)
101 return &context->sc_mask;
102 #else
103 #error unsupported BSD variant
104 #endif
107 os_vm_address_t
108 os_validate(os_vm_address_t addr, os_vm_size_t len)
110 int flags = MAP_PRIVATE | MAP_ANON;
112 if (addr)
113 flags |= MAP_FIXED;
115 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
117 if (addr == MAP_FAILED) {
118 perror("mmap");
119 return NULL;
122 return addr;
125 void
126 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
128 if (munmap(addr, len) == -1)
129 perror("munmap");
132 os_vm_address_t
133 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
135 addr = mmap(addr, len,
136 OS_VM_PROT_ALL,
137 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
138 fd, (off_t) offset);
140 if (addr == MAP_FAILED) {
141 perror("mmap");
142 lose("unexpected mmap(..) failure\n");
145 return addr;
148 void
149 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
151 if (mprotect(address, length, prot) == -1) {
152 perror("mprotect");
156 static boolean
157 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
159 char* beg = (char*) sbeg;
160 char* end = (char*) sbeg + slen;
161 char* adr = (char*) a;
162 return (adr >= beg && adr < end);
165 boolean
166 is_valid_lisp_addr(os_vm_address_t addr)
168 struct thread *th;
170 if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
171 in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE) ||
172 in_range_p(addr, DYNAMIC_SPACE_START, dynamic_space_size))
173 return 1;
174 for_each_thread(th) {
175 if (((os_vm_address_t)th->control_stack_start <= addr) &&
176 (addr < (os_vm_address_t)th->control_stack_end))
177 return 1;
178 if (in_range_p(addr, (lispobj) th->binding_stack_start,
179 BINDING_STACK_SIZE))
180 return 1;
182 return 0;
186 * any OS-dependent special low-level handling for signals
189 #if defined LISP_FEATURE_GENCGC
192 * The GENCGC needs to be hooked into whatever signal is raised for
193 * page fault on this OS.
196 void
197 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context
198 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
199 /* FreeBSD/amd64 stores fault address only in undocumented 4th arg. */
200 ,void *fault_addr
201 #endif
204 os_context_t *context = arch_os_get_context(&void_context);
205 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
206 /* KLUDGE: Store fault address into si_addr for compatibilities. */
207 siginfo->si_addr = fault_addr;
208 #else
209 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
210 #endif
212 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
213 FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
214 *CONTEXT_ADDR_FROM_STEM(fs)));
215 os_restore_tls_segment_register(context);
216 #endif
218 FSHOW((stderr, "Memory fault at: %p, PC: %p\n", fault_addr, *os_context_pc_addr(context)));
220 if (!gencgc_handle_wp_violation(fault_addr))
221 if(!handle_guard_page_triggered(context,fault_addr)) {
222 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
223 lisp_memory_fault_error(context, fault_addr);
224 #else
225 if (!maybe_gc(context)) {
226 interrupt_handle_now(signal, siginfo, context);
228 #endif
232 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
233 void
234 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context) {
235 lose("Unhandled memory fault. Exiting.");
237 #endif
239 void
240 os_install_interrupt_handlers(void)
242 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
243 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
244 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
245 mach_error_memory_fault_handler);
246 #else
247 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
248 #ifdef LISP_FEATURE_FREEBSD
249 (__siginfohandler_t *)
250 #endif
251 memory_fault_handler);
252 #endif
254 #ifdef LISP_FEATURE_SB_THREAD
255 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
256 interrupt_thread_handler);
257 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
258 sig_stop_for_gc_handler);
259 #ifdef SIG_RESUME_FROM_GC
260 undoably_install_low_level_interrupt_handler(SIG_RESUME_FROM_GC,
261 sig_stop_for_gc_handler);
262 #endif
263 #endif
264 SHOW("leaving os_install_interrupt_handlers()");
267 #else /* Currently PPC/Darwin/Cheney only */
269 static void
270 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
272 os_context_t *context = arch_os_get_context(&void_context);
273 #if 0
274 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
275 #endif
276 os_vm_address_t addr;
278 addr = arch_get_bad_addr(signal, info, context);
279 if (!cheneygc_handle_wp_violation(context, addr))
280 if (!handle_guard_page_triggered(context, addr))
281 interrupt_handle_now(signal, info, context);
284 void
285 os_install_interrupt_handlers(void)
287 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
288 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
289 sigsegv_handler);
292 #endif /* defined GENCGC */
294 #ifdef __NetBSD__
295 static void netbsd_init()
297 struct rlimit rl;
298 int mib[2], osrev;
299 size_t len;
301 /* Are we running on a sufficiently functional kernel? */
302 mib[0] = CTL_KERN;
303 mib[1] = KERN_OSREV;
305 len = sizeof(osrev);
306 sysctl(mib, 2, &osrev, &len, NULL, 0);
308 /* If we're older than 2.0... */
309 if (osrev < 200000000) {
310 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
311 lose("NetBSD kernel too old to run sbcl.\n");
314 /* NetBSD counts mmap()ed space against the process's data size limit,
315 * so yank it up. This might be a nasty thing to do? */
316 getrlimit (RLIMIT_DATA, &rl);
317 /* Amazingly for such a new port, the provenance and meaning of
318 this number are unknown. It might just mean REALLY_BIG_LIMIT,
319 or possibly it should be calculated from dynamic space size.
320 -- CSR, 2004-04-08 */
321 rl.rlim_cur = 1073741824;
322 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
323 fprintf (stderr,
324 "RUNTIME WARNING: unable to raise process data size limit:\n\
325 %s.\n\
326 The system may fail to start.\n",
327 strerror(errno));
331 /* Various routines in NetBSD's C library are compatibility wrappers
332 for old versions. Programs must be processed by the C toolchain in
333 order to get up-to-date definitions of such routines. */
334 /* The stat-family, opendir, and readdir are used only in sb-posix, as
335 of 2007-01-16. -- RMK */
337 _stat(const char *path, struct stat *sb)
339 return stat(path, sb);
342 _lstat(const char *path, struct stat *sb)
344 return lstat(path, sb);
347 _fstat(int fd, struct stat *sb)
349 return fstat(fd, sb);
352 DIR *
353 _opendir(const char *filename)
355 return opendir(filename);
357 struct dirent *
358 _readdir(DIR *dirp)
360 return readdir(dirp);
363 /* Used in sb-bsd-sockets. */
365 _socket(int domain, int type, int protocol)
367 return socket(domain, type, protocol);
369 #endif /* __NetBSD__ */
371 #ifdef __FreeBSD__
372 extern int getosreldate(void);
374 int sig_memory_fault;
376 static void freebsd_init()
378 /* Memory fault signal on FreeBSD was changed from SIGBUS to
379 * SIGSEGV. */
380 if (getosreldate() < 700004)
381 sig_memory_fault = SIGBUS;
382 else
383 sig_memory_fault = SIGSEGV;
385 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
386 * 4.x with GENERIC kernel, does not enable SSE support even on
387 * SSE capable CPUs". Detect this situation and skip the
388 * fast_bzero sse/base selection logic that's normally done in
389 * x86-assem.S.
391 #ifdef LISP_FEATURE_X86
393 size_t len;
394 int instruction_sse;
396 len = sizeof(instruction_sse);
397 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len,
398 NULL, 0) == 0 && instruction_sse != 0) {
399 /* Use the SSE detector */
400 fast_bzero_pointer = fast_bzero_detect;
403 #endif /* LISP_FEATURE_X86 */
406 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
408 futex_wait(int *lock_word, long oldval, long sec, unsigned long usec)
410 struct timespec timeout;
411 int ret;
413 again:
414 if (sec < 0)
415 ret = umtx_wait((void *)lock_word, oldval, NULL);
416 else {
417 timeout.tv_sec = sec;
418 timeout.tv_nsec = usec * 1000;
419 ret = umtx_wait((void *)lock_word, oldval, &timeout);
422 switch (ret) {
423 case 0:
424 return 0;
425 case ETIMEDOUT:
426 return 1;
427 case EINTR:
428 /* spurious wakeup from interrupt */
429 goto again;
430 default:
431 /* EWOULDBLOCK and others, need to check the lock */
432 return -1;
437 futex_wake(int *lock_word, int n)
439 return umtx_wake((void *)lock_word, n);
441 #endif
442 #endif /* __FreeBSD__ */
444 #ifdef LISP_FEATURE_DARWIN
445 /* defined in ppc-darwin-os.c instead */
446 #elif defined(LISP_FEATURE_FREEBSD)
447 #ifndef KERN_PROC_PATHNAME
448 #define KERN_PROC_PATHNAME 12
449 #endif
451 char *
452 os_get_runtime_executable_path()
454 char path[PATH_MAX + 1];
456 if (getosreldate() >= 600024) {
457 /* KERN_PROC_PATHNAME is available */
458 size_t len = PATH_MAX + 1;
459 int mib[4];
461 mib[0] = CTL_KERN;
462 mib[1] = KERN_PROC;
463 mib[2] = KERN_PROC_PATHNAME;
464 mib[3] = -1;
465 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
466 return NULL;
467 } else {
468 int size;
469 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
470 if (size < 0)
471 return NULL;
472 path[size] = '\0';
474 if (strcmp(path, "unknown") == 0)
475 return NULL;
476 return copied_string(path);
478 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
479 char *
480 os_get_runtime_executable_path()
482 struct stat sb;
483 char *path = strdup("/proc/curproc/file");
484 if (path && ((stat(path, &sb)) == 0))
485 return path;
486 else {
487 fprintf(stderr, "Couldn't stat /proc/curproc/file; is /proc mounted?\n");
488 return NULL;
491 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD */
492 char *
493 os_get_runtime_executable_path()
495 return NULL;
497 #endif
499 #ifdef __OpenBSD__
500 void
501 openbsd_init()
503 struct rlimit rl;
505 /* OpenBSD, like NetBSD, counts mmap()ed space against the
506 * process's data size limit. If the soft limit is lower than the
507 * hard limit then try to yank it up, this lets users in the
508 * "staff" login class run sbcl with a default /etc/login.conf
510 getrlimit (RLIMIT_DATA, &rl);
511 if (rl.rlim_cur < rl.rlim_max) {
512 rl.rlim_cur = rl.rlim_max;
513 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
514 fprintf (stderr,
515 "RUNTIME WARNING: unable to raise process data size limit:\n\
516 %s.\n\
517 The system may fail to start.\n",
518 strerror(errno));
522 /* Display a (hopefully) helpful warning if it looks like we won't
523 * be able to allocate enough memory. In testing I found that on
524 * my system at least, a minimum of 25M on top of the three space
525 * sizes was needed to start SBCL. Show a warning below 32M so as
526 * to leave a little breathing room.
528 getrlimit (RLIMIT_DATA, &rl);
529 if (dynamic_space_size + READ_ONLY_SPACE_SIZE + STATIC_SPACE_SIZE +
530 LINKAGE_TABLE_SPACE_SIZE + (32*1024*1024) > rl.rlim_cur)
531 fprintf (stderr,
532 "RUNTIME WARNING: data size resource limit may be too low,\n"
533 " try decreasing the dynamic space size with --dynamic-space-size\n");
536 /* OpenBSD's dlsym() relies on the gcc bulitin
537 * __builtin_return_address(0) returning an address in the
538 * executable's text segment, but when called from lisp it will return
539 * an address in the dynamic space. Work around this by calling this
540 * wrapper function instead. Note that tail-call optimization will
541 * defeat this, disable it by saving the dlsym() return value in a
542 * volatile variable.
544 void *
545 os_dlsym(void *handle, const char *symbol)
547 void * volatile ret = dlsym(handle, symbol);
548 return ret;
551 #endif