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
83 return &context->uc_mcontext->ss.srr0;
85 #error unsupported BSD variant
90 os_context_sigmask_addr(os_context_t *context)
92 /* (Unlike most of the other context fields that we access, the
93 * signal mask field is a field of the basic, outermost context
94 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
95 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
96 return &context->uc_sigmask;
97 #elif defined __OpenBSD__
98 return &context->sc_mask;
100 #error unsupported BSD variant
105 os_validate(os_vm_address_t addr, os_vm_size_t len)
107 int flags = MAP_PRIVATE | MAP_ANON;
112 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
114 if (addr == MAP_FAILED) {
123 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
125 if (munmap(addr, len) == -1)
130 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
132 addr = mmap(addr, len,
134 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
137 if (addr == MAP_FAILED) {
139 lose("unexpected mmap(..) failure\n");
146 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
148 if (mprotect(address, length, prot) == -1) {
154 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
156 char* beg = (char*) sbeg;
157 char* end = (char*) sbeg + slen;
158 char* adr = (char*) a;
159 return (adr >= beg && adr < end);
163 is_valid_lisp_addr(os_vm_address_t addr)
166 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
167 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
168 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
170 for_each_thread(th) {
171 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
173 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
180 * any OS-dependent special low-level handling for signals
183 #if defined LISP_FEATURE_GENCGC
186 * The GENCGC needs to be hooked into whatever signal is raised for
187 * page fault on this OS.
190 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
192 os_context_t *context = arch_os_get_context(&void_context);
193 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
195 if (!gencgc_handle_wp_violation(fault_addr))
196 if(!handle_guard_page_triggered(context,fault_addr)) {
197 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
198 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
200 if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
201 interrupt_handle_now(signal, siginfo, context);
203 #if defined(LISP_FEATURE_DARWIN)
204 /* Work around G5 bug; fix courtesy gbyers */
205 DARWIN_FIX_CONTEXT(context);
212 os_install_interrupt_handlers(void)
214 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
215 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
216 memory_fault_handler);
217 #ifdef SIG_MEMORY_FAULT2
218 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
219 memory_fault_handler);
221 SHOW("leaving os_install_interrupt_handlers()");
224 #else /* Currently Darwin only */
227 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
229 os_context_t *context = arch_os_get_context(&void_context);
230 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
231 os_vm_address_t addr;
233 addr = arch_get_bad_addr(signal,info,context);
234 if(!interrupt_maybe_gc(signal, info, context))
235 if(!handle_guard_page_triggered(context,addr))
236 interrupt_handle_now(signal, info, context);
237 /* Work around G5 bug; fix courtesy gbyers */
238 DARWIN_FIX_CONTEXT(context);
242 os_install_interrupt_handlers(void)
244 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
245 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
247 #ifdef SIG_MEMORY_FAULT2
248 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
253 #endif /* defined GENCGC */
256 static void netbsd_init()
262 /* Are we running on a sufficiently functional kernel? */
267 sysctl(mib, 2, &osrev, &len, NULL, 0);
269 /* If we're older than 2.0... */
270 if (osrev < 200000000) {
271 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
272 lose("NetBSD kernel too old to run sbcl.\n");
275 /* NetBSD counts mmap()ed space against the process's data size limit,
276 * so yank it up. This might be a nasty thing to do? */
277 getrlimit (RLIMIT_DATA, &rl);
278 /* Amazingly for such a new port, the provenance and meaning of
279 this number are unknown. It might just mean REALLY_BIG_LIMIT,
280 or possibly it should be calculated from dynamic space size.
281 -- CSR, 2004-04-08 */
282 rl.rlim_cur = 1073741824;
283 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
285 "RUNTIME WARNING: unable to raise process data size limit:\n\
287 The system may fail to start.\n",
291 #endif /* __NetBSD__ */
294 static void freebsd_init()
296 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
297 * 4.x with GENERIC kernel, does not enable SSE support even on
298 * SSE capable CPUs". Detect this situation and skip the
299 * fast_bzero sse/base selection logic that's normally done in
302 #ifdef LISP_FEATURE_X86
306 len = sizeof(instruction_sse);
307 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
308 && instruction_sse != 0) {
309 /* Use the SSE detector */
310 fast_bzero_pointer = fast_bzero_detect;
312 #endif /* LISP_FEATURE_X86 */
314 #endif /* __FreeBSD__ */
318 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
319 #ifdef LISP_FEATURE_SB_THREAD
320 #error "Define threading support functions"
322 int arch_os_thread_init(struct thread *thread) {
324 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
325 /* Signal handlers are run on the control stack, so if it is exhausted
326 * we had better use an alternate stack for whatever signal tells us
327 * we've exhausted it */
328 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
330 sigstack.ss_size = 32*SIGSTKSZ;
331 sigaltstack(&sigstack,0);
333 return 1; /* success */
335 int arch_os_thread_cleanup(struct thread *thread) {
336 return 1; /* success */
340 #ifdef LISP_FEATURE_DARWIN
341 /* defined in ppc-darwin-os.c instead */
342 #elif defined(LISP_FEATURE_FREEBSD)
343 #ifndef KERN_PROC_PATHNAME
344 #define KERN_PROC_PATHNAME 12
348 os_get_runtime_executable_path()
350 char path[PATH_MAX + 1];
352 if (getosreldate() >= 600024) {
353 /* KERN_PROC_PATHNAME is available */
354 size_t len = PATH_MAX + 1;
359 mib[2] = KERN_PROC_PATHNAME;
361 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
365 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
370 if (strcmp(path, "unknown") == 0)
372 return copied_string(path);
374 #else /* Not DARWIN or FREEBSD */
376 os_get_runtime_executable_path()