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>
28 #include "interrupt.h"
34 #include <sys/types.h>
36 /* #include <sys/sysinfo.h> */
40 vm_size_t os_vm_page_size;
44 os_vm_page_size = getpagesize();
47 int *os_context_pc_addr(os_context_t *context)
49 #if defined __FreeBSD__
50 return CONTEXT_ADDR_FROM_STEM(eip);
51 #elif defined __OpenBSD__
52 return CONTEXT_ADDR_FROM_STEM(pc);
54 return &context->uc_mcontext->ss.srr0;
56 #error unsupported BSD variant
61 os_context_sigmask_addr(os_context_t *context)
63 /* (Unlike most of the other context fields that we access, the
64 * signal mask field is a field of the basic, outermost context
65 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
66 #if defined __FreeBSD__ || defined DARWIN
67 return &context->uc_sigmask;
68 #elif defined __OpenBSD__
69 return &context->sc_mask;
71 #error unsupported BSD variant
76 os_validate(os_vm_address_t addr, os_vm_size_t len)
78 int flags = MAP_PRIVATE | MAP_ANON;
83 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
85 if (addr == MAP_FAILED) {
94 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
96 if (munmap(addr, len) == -1)
101 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
103 addr = mmap(addr, len,
105 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
108 if (addr == MAP_FAILED) {
110 lose("unexpected mmap(..) failure");
117 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
119 if (mprotect(address, length, prot) == -1) {
125 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
127 char* beg = (char*) sbeg;
128 char* end = (char*) sbeg + slen;
129 char* adr = (char*) a;
130 return (adr >= beg && adr < end);
134 is_valid_lisp_addr(os_vm_address_t addr)
137 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
138 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
139 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
141 for_each_thread(th) {
142 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
144 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
151 * any OS-dependent special low-level handling for signals
154 #if defined LISP_FEATURE_GENCGC
157 * The GENCGC needs to be hooked into whatever signal is raised for
158 * page fault on this OS.
161 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
163 /* The way that we extract low level information like the fault
164 * address is not specified by POSIX. */
165 #if defined __FreeBSD__
166 void *fault_addr = siginfo->si_addr;
167 #elif defined __OpenBSD__
168 void *fault_addr = siginfo->si_addr;
170 void *fault_addr = siginfo->si_addr;
172 #error unsupported BSD variant
174 os_context_t *context = arch_os_get_context(&void_context);
175 if (!gencgc_handle_wp_violation(fault_addr))
176 if(!handle_control_stack_guard_triggered(context,fault_addr))
177 /* FIXME is this context or void_context? not that it */
178 /* makes a difference currently except on linux/sparc */
179 interrupt_handle_now(signal, siginfo, void_context);
182 os_install_interrupt_handlers(void)
184 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
185 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
186 memory_fault_handler);
187 SHOW("leaving os_install_interrupt_handlers()");
190 #else /* Currently Darwin only */
193 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
195 os_context_t *context = arch_os_get_context(&void_context);
196 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
197 os_vm_address_t addr;
199 addr = arch_get_bad_addr(signal,info,context);
200 if(!interrupt_maybe_gc(signal, info, context))
201 if(!handle_control_stack_guard_triggered(context,addr))
202 interrupt_handle_now(signal, info, context);
206 os_install_interrupt_handlers(void)
208 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
209 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
213 #endif /* defined GENCGC */
217 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
218 #ifdef LISP_FEATURE_SB_THREAD
219 #error "Define threading support functions"
221 int arch_os_thread_init(struct thread *thread) {
223 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
224 /* Signal handlers are run on the control stack, so if it is exhausted
225 * we had better use an alternate stack for whatever signal tells us
226 * we've exhausted it */
227 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
229 sigstack.ss_size = 32*SIGSTKSZ;
230 sigaltstack(&sigstack,0);
232 return 1; /* success */
234 int arch_os_thread_cleanup(struct thread *thread) {
235 return 1; /* success */