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>
32 #include "interrupt.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/fdefn.h"
40 #include <sys/types.h>
42 /* #include <sys/sysinfo.h> */
45 os_vm_size_t os_vm_page_size;
48 #include <sys/resource.h>
49 #include <sys/sysctl.h>
51 #include <sys/stat.h> /* For the stat-family wrappers. */
53 static void netbsd_init();
54 #endif /* __NetBSD__ */
57 #include <sys/sysctl.h>
58 #include <osreldate.h>
60 static void freebsd_init();
61 #endif /* __FreeBSD__ */
64 os_init(char *argv[], char *envp[])
66 os_vm_page_size = getpagesize();
70 #endif /* __NetBSD__ */
73 #endif /* __FreeBSD__ */
76 int *os_context_pc_addr(os_context_t *context)
78 #if defined __FreeBSD__
79 return CONTEXT_ADDR_FROM_STEM(eip);
80 #elif defined __OpenBSD__
81 return CONTEXT_ADDR_FROM_STEM(pc);
82 #elif defined __NetBSD__
83 return CONTEXT_ADDR_FROM_STEM(EIP);
84 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
85 return CONTEXT_ADDR_FROM_STEM(eip);
86 #elif defined LISP_FEATURE_DARWIN
87 return &context->uc_mcontext->ss.srr0;
89 #error unsupported BSD variant
94 os_context_sigmask_addr(os_context_t *context)
96 /* (Unlike most of the other context fields that we access, the
97 * signal mask field is a field of the basic, outermost context
98 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
99 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
100 return &context->uc_sigmask;
101 #elif defined (__OpenBSD__)
102 return &context->sc_mask;
104 #error unsupported BSD variant
109 os_validate(os_vm_address_t addr, os_vm_size_t len)
111 int flags = MAP_PRIVATE | MAP_ANON;
116 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
118 if (addr == MAP_FAILED) {
127 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
129 if (munmap(addr, len) == -1)
134 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
136 addr = mmap(addr, len,
138 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
141 if (addr == MAP_FAILED) {
143 lose("unexpected mmap(..) failure\n");
150 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
152 if (mprotect(address, length, prot) == -1) {
158 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
160 char* beg = (char*) sbeg;
161 char* end = (char*) sbeg + slen;
162 char* adr = (char*) a;
163 return (adr >= beg && adr < end);
167 is_valid_lisp_addr(os_vm_address_t addr)
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))
174 for_each_thread(th) {
175 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
177 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
184 * any OS-dependent special low-level handling for signals
187 #if defined LISP_FEATURE_GENCGC
190 * The GENCGC needs to be hooked into whatever signal is raised for
191 * page fault on this OS.
194 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
196 os_context_t *context = arch_os_get_context(&void_context);
197 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
199 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
200 FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
201 *CONTEXT_ADDR_FROM_STEM(fs)));
202 os_restore_tls_segment_register(context);
205 FSHOW((stderr, "Memory fault at: %p, PC: %x\n", fault_addr, *os_context_pc_addr(context)));
207 if (!gencgc_handle_wp_violation(fault_addr))
208 if(!handle_guard_page_triggered(context,fault_addr)) {
209 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
210 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
212 if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
213 interrupt_handle_now(signal, siginfo, context);
215 #if defined(LISP_FEATURE_DARWIN)
216 /* Work around G5 bug; fix courtesy gbyers */
217 DARWIN_FIX_CONTEXT(context);
224 os_install_interrupt_handlers(void)
226 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
227 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
228 memory_fault_handler);
229 #ifdef SIG_MEMORY_FAULT2
230 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
231 memory_fault_handler);
234 #ifdef LISP_FEATURE_SB_THREAD
235 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
236 interrupt_thread_handler);
237 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
238 sig_stop_for_gc_handler);
239 #ifdef SIG_RESUME_FROM_GC
240 undoably_install_low_level_interrupt_handler(SIG_RESUME_FROM_GC,
241 sig_stop_for_gc_handler);
245 SHOW("leaving os_install_interrupt_handlers()");
248 #else /* Currently PPC/Darwin/Cheney only */
251 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
253 os_context_t *context = arch_os_get_context(&void_context);
254 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
255 os_vm_address_t addr;
257 addr = arch_get_bad_addr(signal,info,context);
258 if(!interrupt_maybe_gc(signal, info, context))
259 if(!handle_guard_page_triggered(context,addr))
260 interrupt_handle_now(signal, info, context);
261 /* Work around G5 bug; fix courtesy gbyers */
262 DARWIN_FIX_CONTEXT(context);
266 os_install_interrupt_handlers(void)
268 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
269 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
271 #ifdef SIG_MEMORY_FAULT2
272 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
277 #endif /* defined GENCGC */
280 static void netbsd_init()
286 /* Are we running on a sufficiently functional kernel? */
291 sysctl(mib, 2, &osrev, &len, NULL, 0);
293 /* If we're older than 2.0... */
294 if (osrev < 200000000) {
295 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
296 lose("NetBSD kernel too old to run sbcl.\n");
299 /* NetBSD counts mmap()ed space against the process's data size limit,
300 * so yank it up. This might be a nasty thing to do? */
301 getrlimit (RLIMIT_DATA, &rl);
302 /* Amazingly for such a new port, the provenance and meaning of
303 this number are unknown. It might just mean REALLY_BIG_LIMIT,
304 or possibly it should be calculated from dynamic space size.
305 -- CSR, 2004-04-08 */
306 rl.rlim_cur = 1073741824;
307 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
309 "RUNTIME WARNING: unable to raise process data size limit:\n\
311 The system may fail to start.\n",
316 /* The stat() routines in NetBSD's C library are compatibility
317 wrappers for some very old version of the stat buffer structure.
318 Programs must be processed by the C toolchain in order to get an
319 up-to-date definition of the stat() routine. These wrappers are
320 used only in sb-posix, as of 2006-10-15. -- RMK */
321 int _stat(const char *path, struct stat *sb) {
322 return (stat(path, sb));
325 int _lstat(const char *path, struct stat *sb) {
326 return (lstat(path, sb));
329 int _fstat(int fd, struct stat *sb) {
330 return (fstat(fd, sb));
334 #endif /* __NetBSD__ */
337 static void freebsd_init()
339 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
340 * 4.x with GENERIC kernel, does not enable SSE support even on
341 * SSE capable CPUs". Detect this situation and skip the
342 * fast_bzero sse/base selection logic that's normally done in
345 #ifdef LISP_FEATURE_X86
349 len = sizeof(instruction_sse);
350 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
351 && instruction_sse != 0) {
352 /* Use the SSE detector */
353 fast_bzero_pointer = fast_bzero_detect;
355 #endif /* LISP_FEATURE_X86 */
357 #endif /* __FreeBSD__ */
359 #ifdef LISP_FEATURE_DARWIN
360 /* defined in ppc-darwin-os.c instead */
361 #elif defined(LISP_FEATURE_FREEBSD)
362 #ifndef KERN_PROC_PATHNAME
363 #define KERN_PROC_PATHNAME 12
367 os_get_runtime_executable_path()
369 char path[PATH_MAX + 1];
371 if (getosreldate() >= 600024) {
372 /* KERN_PROC_PATHNAME is available */
373 size_t len = PATH_MAX + 1;
378 mib[2] = KERN_PROC_PATHNAME;
380 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
384 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
389 if (strcmp(path, "unknown") == 0)
391 return copied_string(path);
393 #else /* Not DARWIN or FREEBSD */
395 os_get_runtime_executable_path()