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> */
42 os_vm_size_t os_vm_page_size;
45 #include <sys/resource.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_control_stack_guard_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_control_stack_guard_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()
236 /* NetBSD counts mmap()ed space against the process's data size limit,
237 * so yank it up. This might be a nasty thing to do? */
238 getrlimit (RLIMIT_DATA, &rl);
239 rl.rlim_cur = 1073741824;
240 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
241 fprintf (stderr, "RUNTIME WARNING: unable to raise process data size limit to 1GB (%s). The system may fail to start.\n",
245 #endif /* __NetBSD__ */
249 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
250 #ifdef LISP_FEATURE_SB_THREAD
251 #error "Define threading support functions"
253 int arch_os_thread_init(struct thread *thread) {
255 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
256 /* Signal handlers are run on the control stack, so if it is exhausted
257 * we had better use an alternate stack for whatever signal tells us
258 * we've exhausted it */
259 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
261 sigstack.ss_size = 32*SIGSTKSZ;
262 sigaltstack(&sigstack,0);
264 return 1; /* success */
266 int arch_os_thread_cleanup(struct thread *thread) {
267 return 1; /* success */