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 os_init(char *argv[], char *envp[])
57 os_vm_page_size = getpagesize();
61 #endif /* __NetBSD__ */
64 int *os_context_pc_addr(os_context_t *context)
66 #if defined __FreeBSD__
67 return CONTEXT_ADDR_FROM_STEM(eip);
68 #elif defined __OpenBSD__
69 return CONTEXT_ADDR_FROM_STEM(pc);
70 #elif defined __NetBSD__
71 return CONTEXT_ADDR_FROM_STEM(EIP);
72 #elif defined LISP_FEATURE_DARWIN
73 return &context->uc_mcontext->ss.srr0;
75 #error unsupported BSD variant
80 os_context_sigmask_addr(os_context_t *context)
82 /* (Unlike most of the other context fields that we access, the
83 * signal mask field is a field of the basic, outermost context
84 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
85 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
86 return &context->uc_sigmask;
87 #elif defined __OpenBSD__
88 return &context->sc_mask;
90 #error unsupported BSD variant
95 os_validate(os_vm_address_t addr, os_vm_size_t len)
97 int flags = MAP_PRIVATE | MAP_ANON;
102 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
104 if (addr == MAP_FAILED) {
113 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
115 if (munmap(addr, len) == -1)
120 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
122 addr = mmap(addr, len,
124 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
127 if (addr == MAP_FAILED) {
129 lose("unexpected mmap(..) failure");
136 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
138 if (mprotect(address, length, prot) == -1) {
144 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
146 char* beg = (char*) sbeg;
147 char* end = (char*) sbeg + slen;
148 char* adr = (char*) a;
149 return (adr >= beg && adr < end);
153 is_valid_lisp_addr(os_vm_address_t addr)
156 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
157 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
158 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
160 for_each_thread(th) {
161 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
163 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
170 * any OS-dependent special low-level handling for signals
173 #if defined LISP_FEATURE_GENCGC
176 * The GENCGC needs to be hooked into whatever signal is raised for
177 * page fault on this OS.
180 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
182 /* The way that we extract low level information like the fault
183 * address is not specified by POSIX. */
184 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
185 void *fault_addr = siginfo->si_addr;
186 #elif defined LISP_FEATURE_DARWIN
187 void *fault_addr = siginfo->si_addr;
189 #error unsupported BSD variant
192 os_context_t *context = arch_os_get_context(&void_context);
193 if (!gencgc_handle_wp_violation(fault_addr))
194 if(!handle_guard_page_triggered(context,fault_addr))
195 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
196 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
198 interrupt_handle_now(signal, siginfo, context);
202 os_install_interrupt_handlers(void)
204 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
205 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
206 memory_fault_handler);
207 SHOW("leaving os_install_interrupt_handlers()");
210 #else /* Currently Darwin only */
213 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
215 os_context_t *context = arch_os_get_context(&void_context);
216 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
217 os_vm_address_t addr;
219 addr = arch_get_bad_addr(signal,info,context);
220 if(!interrupt_maybe_gc(signal, info, context))
221 if(!handle_guard_page_triggered(context,addr))
222 interrupt_handle_now(signal, info, context);
223 /* Work around G5 bug; fix courtesy gbyers */
224 DARWIN_FIX_CONTEXT(context);
228 os_install_interrupt_handlers(void)
230 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
231 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
235 #endif /* defined GENCGC */
238 static void netbsd_init()
244 /* Are we running on a sufficiently functional kernel? */
249 sysctl(mib, 2, &osrev, &len, NULL, 0);
251 /* If we're older than 2.0... */
252 if (osrev < 200000000) {
253 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
254 lose("NetBSD kernel too old to run sbcl.\n");
257 /* NetBSD counts mmap()ed space against the process's data size limit,
258 * so yank it up. This might be a nasty thing to do? */
259 getrlimit (RLIMIT_DATA, &rl);
260 /* Amazingly for such a new port, the provenance and meaning of
261 this number are unknown. It might just mean REALLY_BIG_LIMIT,
262 or possibly it should be calculated from dynamic space size.
263 -- CSR, 2004-04-08 */
264 rl.rlim_cur = 1073741824;
265 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
267 "RUNTIME WARNING: unable to raise process data size limit:\n\
269 The system may fail to start.\n",
273 #endif /* __NetBSD__ */
277 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
278 #ifdef LISP_FEATURE_SB_THREAD
279 #error "Define threading support functions"
281 int arch_os_thread_init(struct thread *thread) {
283 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
284 /* Signal handlers are run on the control stack, so if it is exhausted
285 * we had better use an alternate stack for whatever signal tells us
286 * we've exhausted it */
287 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
289 sigstack.ss_size = 32*SIGSTKSZ;
290 sigaltstack(&sigstack,0);
292 return 1; /* success */
294 int arch_os_thread_cleanup(struct thread *thread) {
295 return 1; /* success */