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>
31 #include "interrupt.h"
36 #include "genesis/static-symbols.h"
37 #include "genesis/fdefn.h"
39 #include <sys/types.h>
41 /* #include <sys/sysinfo.h> */
44 os_vm_size_t os_vm_page_size;
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
51 static void netbsd_init();
52 #endif /* __NetBSD__ */
55 #include <sys/sysctl.h>
56 #include <osreldate.h>
58 static void freebsd_init();
59 #endif /* __FreeBSD__ */
62 os_init(char *argv[], char *envp[])
64 os_vm_page_size = getpagesize();
68 #endif /* __NetBSD__ */
71 #endif /* __FreeBSD__ */
74 int *os_context_pc_addr(os_context_t *context)
76 #if defined __FreeBSD__
77 return CONTEXT_ADDR_FROM_STEM(eip);
78 #elif defined __OpenBSD__
79 return CONTEXT_ADDR_FROM_STEM(pc);
80 #elif defined __NetBSD__
81 return CONTEXT_ADDR_FROM_STEM(EIP);
82 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
83 return CONTEXT_ADDR_FROM_STEM(eip);
84 #elif defined LISP_FEATURE_DARWIN
85 return &context->uc_mcontext->ss.srr0;
87 #error unsupported BSD variant
92 os_context_sigmask_addr(os_context_t *context)
94 /* (Unlike most of the other context fields that we access, the
95 * signal mask field is a field of the basic, outermost context
96 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
97 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
98 return &context->uc_sigmask;
99 #elif defined (__OpenBSD__)
100 return &context->sc_mask;
102 #error unsupported BSD variant
107 os_validate(os_vm_address_t addr, os_vm_size_t len)
109 int flags = MAP_PRIVATE | MAP_ANON;
114 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
116 if (addr == MAP_FAILED) {
125 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
127 if (munmap(addr, len) == -1)
132 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
134 addr = mmap(addr, len,
136 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
139 if (addr == MAP_FAILED) {
141 lose("unexpected mmap(..) failure\n");
148 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
150 if (mprotect(address, length, prot) == -1) {
156 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
158 char* beg = (char*) sbeg;
159 char* end = (char*) sbeg + slen;
160 char* adr = (char*) a;
161 return (adr >= beg && adr < end);
165 is_valid_lisp_addr(os_vm_address_t addr)
168 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
169 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
170 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
172 for_each_thread(th) {
173 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
175 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
182 * any OS-dependent special low-level handling for signals
185 #if defined LISP_FEATURE_GENCGC
188 * The GENCGC needs to be hooked into whatever signal is raised for
189 * page fault on this OS.
192 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
194 os_context_t *context = arch_os_get_context(&void_context);
195 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
197 #if defined(MEMORY_FAULT_DEBUG)
198 fprintf(stderr, "Memory fault at: %p, PC: %x\n", fault_addr, *os_context_pc_addr(context));
199 #if defined(ARCH_HAS_STACK_POINTER)
200 fprintf(stderr, "Stack pointer: %x\n", *os_context_sp_addr(context));
204 if (!gencgc_handle_wp_violation(fault_addr))
205 if(!handle_guard_page_triggered(context,fault_addr)) {
206 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
207 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
209 if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
210 interrupt_handle_now(signal, siginfo, context);
212 #if defined(LISP_FEATURE_DARWIN)
213 /* Work around G5 bug; fix courtesy gbyers */
214 DARWIN_FIX_CONTEXT(context);
221 os_install_interrupt_handlers(void)
223 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
224 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
225 memory_fault_handler);
226 #ifdef SIG_MEMORY_FAULT2
227 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
228 memory_fault_handler);
230 SHOW("leaving os_install_interrupt_handlers()");
233 #else /* Currently Darwin only */
236 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
238 os_context_t *context = arch_os_get_context(&void_context);
239 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
240 os_vm_address_t addr;
242 addr = arch_get_bad_addr(signal,info,context);
243 if(!interrupt_maybe_gc(signal, info, context))
244 if(!handle_guard_page_triggered(context,addr))
245 interrupt_handle_now(signal, info, context);
246 /* Work around G5 bug; fix courtesy gbyers */
247 DARWIN_FIX_CONTEXT(context);
251 os_install_interrupt_handlers(void)
253 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
254 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
256 #ifdef SIG_MEMORY_FAULT2
257 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
262 #endif /* defined GENCGC */
265 static void netbsd_init()
271 /* Are we running on a sufficiently functional kernel? */
276 sysctl(mib, 2, &osrev, &len, NULL, 0);
278 /* If we're older than 2.0... */
279 if (osrev < 200000000) {
280 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
281 lose("NetBSD kernel too old to run sbcl.\n");
284 /* NetBSD counts mmap()ed space against the process's data size limit,
285 * so yank it up. This might be a nasty thing to do? */
286 getrlimit (RLIMIT_DATA, &rl);
287 /* Amazingly for such a new port, the provenance and meaning of
288 this number are unknown. It might just mean REALLY_BIG_LIMIT,
289 or possibly it should be calculated from dynamic space size.
290 -- CSR, 2004-04-08 */
291 rl.rlim_cur = 1073741824;
292 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
294 "RUNTIME WARNING: unable to raise process data size limit:\n\
296 The system may fail to start.\n",
300 #endif /* __NetBSD__ */
303 static void freebsd_init()
305 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
306 * 4.x with GENERIC kernel, does not enable SSE support even on
307 * SSE capable CPUs". Detect this situation and skip the
308 * fast_bzero sse/base selection logic that's normally done in
311 #ifdef LISP_FEATURE_X86
315 len = sizeof(instruction_sse);
316 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
317 && instruction_sse != 0) {
318 /* Use the SSE detector */
319 fast_bzero_pointer = fast_bzero_detect;
321 #endif /* LISP_FEATURE_X86 */
323 #endif /* __FreeBSD__ */
327 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
328 #ifdef LISP_FEATURE_SB_THREAD
329 #error "Define threading support functions"
331 int arch_os_thread_init(struct thread *thread) {
333 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
334 /* Signal handlers are run on the control stack, so if it is exhausted
335 * we had better use an alternate stack for whatever signal tells us
336 * we've exhausted it */
337 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
339 sigstack.ss_size = 32*SIGSTKSZ;
340 sigaltstack(&sigstack,0);
342 return 1; /* success */
344 int arch_os_thread_cleanup(struct thread *thread) {
345 return 1; /* success */
349 #ifdef LISP_FEATURE_DARWIN
350 /* defined in ppc-darwin-os.c instead */
351 #elif defined(LISP_FEATURE_FREEBSD)
352 #ifndef KERN_PROC_PATHNAME
353 #define KERN_PROC_PATHNAME 12
357 os_get_runtime_executable_path()
359 char path[PATH_MAX + 1];
361 if (getosreldate() >= 600024) {
362 /* KERN_PROC_PATHNAME is available */
363 size_t len = PATH_MAX + 1;
368 mib[2] = KERN_PROC_PATHNAME;
370 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
374 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
379 if (strcmp(path, "unknown") == 0)
381 return copied_string(path);
383 #else /* Not DARWIN or FREEBSD */
385 os_get_runtime_executable_path()