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 /* FIXME is this context or void_context? not that it */
192 /* makes a difference currently except on linux/sparc */
193 interrupt_handle_now(signal, siginfo, void_context);
196 os_install_interrupt_handlers(void)
198 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
199 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
200 memory_fault_handler);
201 SHOW("leaving os_install_interrupt_handlers()");
204 #else /* Currently Darwin only */
207 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
209 os_context_t *context = arch_os_get_context(&void_context);
210 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
211 os_vm_address_t addr;
213 addr = arch_get_bad_addr(signal,info,context);
214 if(!interrupt_maybe_gc(signal, info, context))
215 if(!handle_guard_page_triggered(context,addr))
216 interrupt_handle_now(signal, info, context);
217 /* Work around G5 bug; fix courtesy gbyers */
218 sigreturn(void_context);
222 os_install_interrupt_handlers(void)
224 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
225 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
229 #endif /* defined GENCGC */
232 static void netbsd_init()
238 /* Are we running on a sufficiently functional kernel? */
243 sysctl(mib, 2, &osrev, &len, NULL, 0);
245 /* If we're older than 2.0... */
246 if (osrev < 200000000) {
247 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
248 lose("NetBSD kernel too old to run sbcl.\n");
251 /* NetBSD counts mmap()ed space against the process's data size limit,
252 * so yank it up. This might be a nasty thing to do? */
253 getrlimit (RLIMIT_DATA, &rl);
254 /* Amazingly for such a new port, the provenance and meaning of
255 this number are unknown. It might just mean REALLY_BIG_LIMIT,
256 or possibly it should be calculated from dynamic space size.
257 -- CSR, 2004-04-08 */
258 rl.rlim_cur = 1073741824;
259 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
261 "RUNTIME WARNING: unable to raise process data size limit:\n\
263 The system may fail to start.\n",
267 #endif /* __NetBSD__ */
271 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
272 #ifdef LISP_FEATURE_SB_THREAD
273 #error "Define threading support functions"
275 int arch_os_thread_init(struct thread *thread) {
277 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
278 /* Signal handlers are run on the control stack, so if it is exhausted
279 * we had better use an alternate stack for whatever signal tells us
280 * we've exhausted it */
281 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
283 sigstack.ss_size = 32*SIGSTKSZ;
284 sigaltstack(&sigstack,0);
286 return 1; /* success */
288 int arch_os_thread_cleanup(struct thread *thread) {
289 return 1; /* success */