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 <sys/types.h>
38 /* #include <sys/sysinfo.h> */
41 os_vm_size_t os_vm_page_size;
44 #include <sys/resource.h>
45 #include <sys/sysctl.h>
48 static void netbsd_init();
49 #endif /* __NetBSD__ */
53 os_vm_page_size = getpagesize();
57 #endif /* __NetBSD__ */
60 int *os_context_pc_addr(os_context_t *context)
62 #if defined __FreeBSD__
63 return CONTEXT_ADDR_FROM_STEM(eip);
64 #elif defined __OpenBSD__
65 return CONTEXT_ADDR_FROM_STEM(pc);
66 #elif defined __NetBSD__
67 return CONTEXT_ADDR_FROM_STEM(EIP);
68 #elif defined LISP_FEATURE_DARWIN
69 return &context->uc_mcontext->ss.srr0;
71 #error unsupported BSD variant
76 os_context_sigmask_addr(os_context_t *context)
78 /* (Unlike most of the other context fields that we access, the
79 * signal mask field is a field of the basic, outermost context
80 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
81 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
82 return &context->uc_sigmask;
83 #elif defined __OpenBSD__
84 return &context->sc_mask;
86 #error unsupported BSD variant
91 os_validate(os_vm_address_t addr, os_vm_size_t len)
93 int flags = MAP_PRIVATE | MAP_ANON;
98 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
100 if (addr == MAP_FAILED) {
109 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
111 if (munmap(addr, len) == -1)
116 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
118 addr = mmap(addr, len,
120 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
123 if (addr == MAP_FAILED) {
125 lose("unexpected mmap(..) failure");
132 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
134 if (mprotect(address, length, prot) == -1) {
140 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
142 char* beg = (char*) sbeg;
143 char* end = (char*) sbeg + slen;
144 char* adr = (char*) a;
145 return (adr >= beg && adr < end);
149 is_valid_lisp_addr(os_vm_address_t addr)
152 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
153 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
154 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
156 for_each_thread(th) {
157 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
159 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
166 * any OS-dependent special low-level handling for signals
169 #if defined LISP_FEATURE_GENCGC
172 * The GENCGC needs to be hooked into whatever signal is raised for
173 * page fault on this OS.
176 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
178 /* The way that we extract low level information like the fault
179 * address is not specified by POSIX. */
180 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
181 void *fault_addr = siginfo->si_addr;
182 #elif defined LISP_FEATURE_DARWIN
183 void *fault_addr = siginfo->si_addr;
185 #error unsupported BSD variant
188 os_context_t *context = arch_os_get_context(&void_context);
189 if (!gencgc_handle_wp_violation(fault_addr))
190 if(!handle_guard_page_triggered(context,fault_addr))
191 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
192 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_HANDLER));
194 interrupt_handle_now(signal, siginfo, context);
198 os_install_interrupt_handlers(void)
200 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
201 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
202 memory_fault_handler);
203 SHOW("leaving os_install_interrupt_handlers()");
206 #else /* Currently Darwin only */
209 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
211 os_context_t *context = arch_os_get_context(&void_context);
212 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
213 os_vm_address_t addr;
215 addr = arch_get_bad_addr(signal,info,context);
216 if(!interrupt_maybe_gc(signal, info, context))
217 if(!handle_guard_page_triggered(context,addr))
218 interrupt_handle_now(signal, info, context);
219 /* Work around G5 bug; fix courtesy gbyers */
220 sigreturn(void_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,
231 #endif /* defined GENCGC */
234 static void netbsd_init()
240 /* Are we running on a sufficiently functional kernel? */
245 sysctl(mib, 2, &osrev, &len, NULL, 0);
247 /* If we're older than 2.0... */
248 if (osrev < 200000000) {
249 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
250 lose("NetBSD kernel too old to run sbcl.\n");
253 /* NetBSD counts mmap()ed space against the process's data size limit,
254 * so yank it up. This might be a nasty thing to do? */
255 getrlimit (RLIMIT_DATA, &rl);
256 /* Amazingly for such a new port, the provenance and meaning of
257 this number are unknown. It might just mean REALLY_BIG_LIMIT,
258 or possibly it should be calculated from dynamic space size.
259 -- CSR, 2004-04-08 */
260 rl.rlim_cur = 1073741824;
261 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
263 "RUNTIME WARNING: unable to raise process data size limit:\n\
265 The system may fail to start.\n",
269 #endif /* __NetBSD__ */
273 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
274 #ifdef LISP_FEATURE_SB_THREAD
275 #error "Define threading support functions"
277 int arch_os_thread_init(struct thread *thread) {
279 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
280 /* Signal handlers are run on the control stack, so if it is exhausted
281 * we had better use an alternate stack for whatever signal tells us
282 * we've exhausted it */
283 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
285 sigstack.ss_size = 32*SIGSTKSZ;
286 sigaltstack(&sigstack,0);
288 return 1; /* success */
290 int arch_os_thread_cleanup(struct thread *thread) {
291 return 1; /* success */