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 os_vm_size_t os_vm_page_size;
52 #include <sys/resource.h>
53 #include <sys/sysctl.h>
55 #include <sys/stat.h> /* For the stat-family wrappers. */
56 #include <dirent.h> /* For the opendir()/readdir() wrappers */
57 #include <sys/socket.h> /* For the socket() wrapper */
58 static void netbsd_init();
59 #endif /* __NetBSD__ */
62 #include <sys/sysctl.h>
63 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
67 static void freebsd_init();
68 #endif /* __FreeBSD__ */
71 #include <sys/types.h>
72 #include <sys/resource.h>
74 #include <sys/sysctl.h>
76 #ifdef LISP_FEATURE_X86
77 #include <machine/cpu.h>
80 static void openbsd_init();
84 os_init(char *argv[], char *envp[])
86 os_vm_page_size = BACKEND_PAGE_BYTES;
90 #elif defined(__FreeBSD__)
92 #elif defined(__OpenBSD__)
94 #elif defined(LISP_FEATURE_DARWIN)
100 os_context_sigmask_addr(os_context_t *context)
102 /* (Unlike most of the other context fields that we access, the
103 * signal mask field is a field of the basic, outermost context
104 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
105 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
106 return &context->uc_sigmask;
107 #elif defined (__OpenBSD__)
108 return &context->sc_mask;
110 #error unsupported BSD variant
115 os_validate(os_vm_address_t addr, os_vm_size_t len)
117 int flags = MAP_PRIVATE | MAP_ANON;
122 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
124 if (addr == MAP_FAILED) {
133 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
135 if (munmap(addr, len) == -1)
140 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
142 addr = mmap(addr, len,
144 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
147 if (addr == MAP_FAILED) {
149 lose("unexpected mmap(..) failure\n");
156 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
158 if (mprotect(address, length, prot) == -1) {
164 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
166 char* beg = (char*) sbeg;
167 char* end = (char*) sbeg + slen;
168 char* adr = (char*) a;
169 return (adr >= beg && adr < end);
173 is_valid_lisp_addr(os_vm_address_t addr)
177 if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
178 in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE) ||
179 in_range_p(addr, DYNAMIC_SPACE_START, dynamic_space_size))
181 for_each_thread(th) {
182 if (((os_vm_address_t)th->control_stack_start <= addr) &&
183 (addr < (os_vm_address_t)th->control_stack_end))
185 if (in_range_p(addr, (lispobj) th->binding_stack_start,
193 * any OS-dependent special low-level handling for signals
196 #if defined LISP_FEATURE_GENCGC
199 * The GENCGC needs to be hooked into whatever signal is raised for
200 * page fault on this OS.
204 memory_fault_handler(int signal, siginfo_t *siginfo, os_context_t *context)
206 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
208 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
209 FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
210 *CONTEXT_ADDR_FROM_STEM(fs)));
211 os_restore_tls_segment_register(context);
214 FSHOW((stderr, "Memory fault at: %p, PC: %p\n", fault_addr, *os_context_pc_addr(context)));
216 if (!gencgc_handle_wp_violation(fault_addr))
217 if(!handle_guard_page_triggered(context,fault_addr))
218 lisp_memory_fault_error(context, fault_addr);
221 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
223 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo,
224 os_context_t *context) {
225 lose("Unhandled memory fault. Exiting.");
230 os_install_interrupt_handlers(void)
232 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
233 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
234 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
235 mach_error_memory_fault_handler);
237 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
238 #ifdef LISP_FEATURE_FREEBSD
239 (__siginfohandler_t *)
241 memory_fault_handler);
244 #ifdef THREADS_USING_GCSIGNAL
245 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
246 sig_stop_for_gc_handler);
248 SHOW("leaving os_install_interrupt_handlers()");
251 #else /* Currently PPC/Darwin/Cheney only */
254 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
257 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
259 os_vm_address_t addr;
261 addr = arch_get_bad_addr(signal, info, context);
262 if (!cheneygc_handle_wp_violation(context, addr))
263 if (!handle_guard_page_triggered(context, addr))
264 interrupt_handle_now(signal, info, context);
268 os_install_interrupt_handlers(void)
270 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
271 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
275 #endif /* defined GENCGC */
278 static void netbsd_init()
284 /* Are we running on a sufficiently functional kernel? */
289 sysctl(mib, 2, &osrev, &len, NULL, 0);
291 /* If we're older than 2.0... */
292 if (osrev < 200000000) {
293 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
294 lose("NetBSD kernel too old to run sbcl.\n");
297 /* NetBSD counts mmap()ed space against the process's data size limit,
298 * so yank it up. This might be a nasty thing to do? */
299 getrlimit (RLIMIT_DATA, &rl);
300 /* Amazingly for such a new port, the provenance and meaning of
301 this number are unknown. It might just mean REALLY_BIG_LIMIT,
302 or possibly it should be calculated from dynamic space size.
303 -- CSR, 2004-04-08 */
304 rl.rlim_cur = 1073741824;
305 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
307 "RUNTIME WARNING: unable to raise process data size limit:\n\
309 The system may fail to start.\n",
314 /* Various routines in NetBSD's C library are compatibility wrappers
315 for old versions. Programs must be processed by the C toolchain in
316 order to get up-to-date definitions of such routines. */
317 /* The stat-family, opendir, and readdir are used only in sb-posix, as
318 of 2007-01-16. -- RMK */
320 _stat(const char *path, struct stat *sb)
322 return stat(path, sb);
325 _lstat(const char *path, struct stat *sb)
327 return lstat(path, sb);
330 _fstat(int fd, struct stat *sb)
332 return fstat(fd, sb);
336 _opendir(const char *filename)
338 return opendir(filename);
343 return readdir(dirp);
347 _utime(const char *file, const struct utimbuf *timep)
349 return utime(file, timep);
352 /* Used in sb-bsd-sockets. */
354 _socket(int domain, int type, int protocol)
356 return socket(domain, type, protocol);
358 #endif /* __NetBSD__ */
361 extern int getosreldate(void);
363 int sig_memory_fault;
365 static void freebsd_init()
367 /* Memory fault signal on FreeBSD was changed from SIGBUS to
369 if (getosreldate() < 700004)
370 sig_memory_fault = SIGBUS;
372 sig_memory_fault = SIGSEGV;
374 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
375 * 4.x with GENERIC kernel, does not enable SSE support even on
376 * SSE capable CPUs". Detect this situation and skip the
377 * fast_bzero sse/base selection logic that's normally done in
380 #ifdef LISP_FEATURE_X86
385 len = sizeof(instruction_sse);
386 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len,
387 NULL, 0) == 0 && instruction_sse != 0) {
388 /* Use the SSE detector */
389 fast_bzero_pointer = fast_bzero_detect;
392 #endif /* LISP_FEATURE_X86 */
395 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
396 && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
398 futex_wait(int *lock_word, long oldval, long sec, unsigned long usec)
400 struct timespec timeout;
404 ret = umtx_wait((void *)lock_word, oldval, NULL);
406 timeout.tv_sec = sec;
407 timeout.tv_nsec = usec * 1000;
408 ret = umtx_wait((void *)lock_word, oldval, &timeout);
419 /* EWOULDBLOCK and others, need to check the lock */
425 futex_wake(int *lock_word, int n)
427 return umtx_wake((void *)lock_word, n);
430 #endif /* __FreeBSD__ */
432 #ifdef LISP_FEATURE_DARWIN
433 /* defined in ppc-darwin-os.c instead */
434 #elif defined(LISP_FEATURE_FREEBSD)
435 #ifndef KERN_PROC_PATHNAME
436 #define KERN_PROC_PATHNAME 12
440 os_get_runtime_executable_path(int external)
442 char path[PATH_MAX + 1];
444 if (getosreldate() >= 600024) {
445 /* KERN_PROC_PATHNAME is available */
446 size_t len = PATH_MAX + 1;
451 mib[2] = KERN_PROC_PATHNAME;
453 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
457 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
462 if (strcmp(path, "unknown") == 0)
464 return copied_string(path);
466 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
468 os_get_runtime_executable_path(int external)
471 if (!external && stat("/proc/curproc/file", &sb) == 0)
472 return copied_string("/proc/curproc/file");
475 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD */
477 os_get_runtime_executable_path(int external)
485 int openbsd_use_fxsave = 0;
490 #ifdef LISP_FEATURE_X86
495 * Show a warning if it looks like the memory available after
496 * allocating the spaces won't be at least this much.
498 #ifdef LISP_FEATURE_X86_64
499 const int wantfree = 64 * 1024 * 1024;
501 const int wantfree = 32 * 1024 * 1024;
505 #ifdef LISP_FEATURE_X86
506 /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
507 mib[0] = CTL_MACHDEP;
509 size = sizeof (openbsd_use_fxsave);
510 sysctl(mib, 2, &openbsd_use_fxsave, &size, NULL, 0);
513 /* OpenBSD, like NetBSD, counts mmap()ed space against the
514 * process's data size limit. If the soft limit is lower than the
515 * hard limit then try to yank it up, this lets users in the
516 * "staff" or "daemon" login classes run sbcl with larger dynamic
519 getrlimit (RLIMIT_DATA, &rl);
520 if (rl.rlim_cur < rl.rlim_max) {
521 rl.rlim_cur = rl.rlim_max;
522 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
524 "RUNTIME WARNING: unable to raise process data size limit:\n\
526 The system may fail to start.\n",
532 * Display a (hopefully) helpful warning if it looks like we won't
533 * be able to allocate enough memory.
535 getrlimit (RLIMIT_DATA, &rl);
536 if (dynamic_space_size + READ_ONLY_SPACE_SIZE + STATIC_SPACE_SIZE +
537 LINKAGE_TABLE_SPACE_SIZE + wantfree > rl.rlim_cur)
539 "RUNTIME WARNING: data size resource limit may be too low,\n"
540 " try decreasing the dynamic space size with --dynamic-space-size\n"
541 " or raising the datasize or datasize-max limits in /etc/login.conf\n");
544 /* OpenBSD's dlsym() relies on the gcc bulitin
545 * __builtin_return_address(0) returning an address in the
546 * executable's text segment, but when called from lisp it will return
547 * an address in the dynamic space. Work around this by calling this
548 * wrapper function instead. Note that tail-call optimization will
549 * defeat this, disable it by saving the dlsym() return value in a
553 os_dlsym(void *handle, const char *symbol)
555 void * volatile ret = dlsym(handle, symbol);