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>
57 static void freebsd_init();
58 #endif /* __FreeBSD__ */
61 os_init(char *argv[], char *envp[])
63 os_vm_page_size = getpagesize();
67 #endif /* __NetBSD__ */
70 #endif /* __FreeBSD__ */
73 int *os_context_pc_addr(os_context_t *context)
75 #if defined __FreeBSD__
76 return CONTEXT_ADDR_FROM_STEM(eip);
77 #elif defined __OpenBSD__
78 return CONTEXT_ADDR_FROM_STEM(pc);
79 #elif defined __NetBSD__
80 return CONTEXT_ADDR_FROM_STEM(EIP);
81 #elif defined LISP_FEATURE_DARWIN
82 return &context->uc_mcontext->ss.srr0;
84 #error unsupported BSD variant
89 os_context_sigmask_addr(os_context_t *context)
91 /* (Unlike most of the other context fields that we access, the
92 * signal mask field is a field of the basic, outermost context
93 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
94 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
95 return &context->uc_sigmask;
96 #elif defined __OpenBSD__
97 return &context->sc_mask;
99 #error unsupported BSD variant
104 os_validate(os_vm_address_t addr, os_vm_size_t len)
106 int flags = MAP_PRIVATE | MAP_ANON;
111 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
113 if (addr == MAP_FAILED) {
122 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
124 if (munmap(addr, len) == -1)
129 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
131 addr = mmap(addr, len,
133 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
136 if (addr == MAP_FAILED) {
138 lose("unexpected mmap(..) failure\n");
145 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
147 if (mprotect(address, length, prot) == -1) {
153 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
155 char* beg = (char*) sbeg;
156 char* end = (char*) sbeg + slen;
157 char* adr = (char*) a;
158 return (adr >= beg && adr < end);
162 is_valid_lisp_addr(os_vm_address_t addr)
165 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
166 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
167 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
169 for_each_thread(th) {
170 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
172 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
179 * any OS-dependent special low-level handling for signals
182 #if defined LISP_FEATURE_GENCGC
185 * The GENCGC needs to be hooked into whatever signal is raised for
186 * page fault on this OS.
189 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
191 os_context_t *context = arch_os_get_context(&void_context);
192 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
194 if (!gencgc_handle_wp_violation(fault_addr))
195 if(!handle_guard_page_triggered(context,fault_addr)) {
196 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
197 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
199 if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
200 interrupt_handle_now(signal, siginfo, context);
202 #if defined(LISP_FEATURE_DARWIN)
203 /* Work around G5 bug; fix courtesy gbyers */
204 DARWIN_FIX_CONTEXT(context);
211 os_install_interrupt_handlers(void)
213 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
214 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
215 memory_fault_handler);
216 #ifdef SIG_MEMORY_FAULT2
217 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
218 memory_fault_handler);
220 SHOW("leaving os_install_interrupt_handlers()");
223 #else /* Currently Darwin only */
226 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
228 os_context_t *context = arch_os_get_context(&void_context);
229 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
230 os_vm_address_t addr;
232 addr = arch_get_bad_addr(signal,info,context);
233 if(!interrupt_maybe_gc(signal, info, context))
234 if(!handle_guard_page_triggered(context,addr))
235 interrupt_handle_now(signal, info, context);
236 /* Work around G5 bug; fix courtesy gbyers */
237 DARWIN_FIX_CONTEXT(context);
241 os_install_interrupt_handlers(void)
243 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
244 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
246 #ifdef SIG_MEMORY_FAULT2
247 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
252 #endif /* defined GENCGC */
255 static void netbsd_init()
261 /* Are we running on a sufficiently functional kernel? */
266 sysctl(mib, 2, &osrev, &len, NULL, 0);
268 /* If we're older than 2.0... */
269 if (osrev < 200000000) {
270 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
271 lose("NetBSD kernel too old to run sbcl.\n");
274 /* NetBSD counts mmap()ed space against the process's data size limit,
275 * so yank it up. This might be a nasty thing to do? */
276 getrlimit (RLIMIT_DATA, &rl);
277 /* Amazingly for such a new port, the provenance and meaning of
278 this number are unknown. It might just mean REALLY_BIG_LIMIT,
279 or possibly it should be calculated from dynamic space size.
280 -- CSR, 2004-04-08 */
281 rl.rlim_cur = 1073741824;
282 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
284 "RUNTIME WARNING: unable to raise process data size limit:\n\
286 The system may fail to start.\n",
290 #endif /* __NetBSD__ */
293 static void freebsd_init()
295 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
296 * 4.x with GENERIC kernel, does not enable SSE support even on
297 * SSE capable CPUs". Detect this situation and skip the
298 * fast_bzero sse/base selection logic that's normally done in
301 #ifdef LISP_FEATURE_X86
305 len = sizeof(instruction_sse);
306 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
307 && instruction_sse != 0) {
308 /* Use the SSE detector */
309 fast_bzero_pointer = fast_bzero_detect;
311 #endif /* LISP_FEATURE_X86 */
313 #endif /* __FreeBSD__ */
317 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
318 #ifdef LISP_FEATURE_SB_THREAD
319 #error "Define threading support functions"
321 int arch_os_thread_init(struct thread *thread) {
323 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
324 /* Signal handlers are run on the control stack, so if it is exhausted
325 * we had better use an alternate stack for whatever signal tells us
326 * we've exhausted it */
327 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
329 sigstack.ss_size = 32*SIGSTKSZ;
330 sigaltstack(&sigstack,0);
332 return 1; /* success */
334 int arch_os_thread_cleanup(struct thread *thread) {
335 return 1; /* success */
339 #ifdef LISP_FEATURE_DARWIN
340 /* defined in ppc-darwin-os.c instead */
341 #elif defined(LISP_FEATURE_FREEBSD)
343 os_get_runtime_executable_path()
345 char path[PATH_MAX + 1];
346 /* Defined on FreeBSD 6 and later. -- JES, 2006-02-06. */
347 #ifdef KERN_PROC_PATHNAME
348 size_t len = PATH_MAX + 1;
353 mib[2] = KERN_PROC_PATHNAME;
355 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
356 /* FIXME: Should we rather fall back into the /proc/-based
357 * code below, so that an SBCL executable core made on
358 * FreeBSD 6 would have at least some chance of working on
359 * FreeBSD 4? -- JES, 2006-02-06.
364 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
368 if (strcmp(path, "unknown") == 0)
371 return copied_string(path);
373 #else /* Not DARWIN or FREEBSD */
375 os_get_runtime_executable_path()