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__ */
56 os_vm_page_size
= getpagesize();
60 #endif /* __NetBSD__ */
63 int *os_context_pc_addr(os_context_t
*context
)
65 #if defined __FreeBSD__
66 return CONTEXT_ADDR_FROM_STEM(eip
);
67 #elif defined __OpenBSD__
68 return CONTEXT_ADDR_FROM_STEM(pc
);
69 #elif defined __NetBSD__
70 return CONTEXT_ADDR_FROM_STEM(EIP
);
71 #elif defined LISP_FEATURE_DARWIN
72 return &context
->uc_mcontext
->ss
.srr0
;
74 #error unsupported BSD variant
79 os_context_sigmask_addr(os_context_t
*context
)
81 /* (Unlike most of the other context fields that we access, the
82 * signal mask field is a field of the basic, outermost context
83 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
84 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
85 return &context
->uc_sigmask
;
86 #elif defined __OpenBSD__
87 return &context
->sc_mask
;
89 #error unsupported BSD variant
94 os_validate(os_vm_address_t addr
, os_vm_size_t len
)
96 int flags
= MAP_PRIVATE
| MAP_ANON
;
101 addr
= mmap(addr
, len
, OS_VM_PROT_ALL
, flags
, -1, 0);
103 if (addr
== MAP_FAILED
) {
112 os_invalidate(os_vm_address_t addr
, os_vm_size_t len
)
114 if (munmap(addr
, len
) == -1)
119 os_map(int fd
, int offset
, os_vm_address_t addr
, os_vm_size_t len
)
121 addr
= mmap(addr
, len
,
123 MAP_PRIVATE
| MAP_FILE
| MAP_FIXED
,
126 if (addr
== MAP_FAILED
) {
128 lose("unexpected mmap(..) failure");
135 os_protect(os_vm_address_t address
, os_vm_size_t length
, os_vm_prot_t prot
)
137 if (mprotect(address
, length
, prot
) == -1) {
143 in_range_p(os_vm_address_t a
, lispobj sbeg
, size_t slen
)
145 char* beg
= (char*) sbeg
;
146 char* end
= (char*) sbeg
+ slen
;
147 char* adr
= (char*) a
;
148 return (adr
>= beg
&& adr
< end
);
152 is_valid_lisp_addr(os_vm_address_t addr
)
155 if(in_range_p(addr
, READ_ONLY_SPACE_START
, READ_ONLY_SPACE_SIZE
) ||
156 in_range_p(addr
, STATIC_SPACE_START
, STATIC_SPACE_SIZE
) ||
157 in_range_p(addr
, DYNAMIC_SPACE_START
, DYNAMIC_SPACE_SIZE
))
159 for_each_thread(th
) {
160 if((th
->control_stack_start
<= addr
) && (addr
< th
->control_stack_end
))
162 if(in_range_p(addr
, th
->binding_stack_start
, BINDING_STACK_SIZE
))
169 * any OS-dependent special low-level handling for signals
172 #if defined LISP_FEATURE_GENCGC
175 * The GENCGC needs to be hooked into whatever signal is raised for
176 * page fault on this OS.
179 memory_fault_handler(int signal
, siginfo_t
*siginfo
, void *void_context
)
181 /* The way that we extract low level information like the fault
182 * address is not specified by POSIX. */
183 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
184 void *fault_addr
= siginfo
->si_addr
;
185 #elif defined LISP_FEATURE_DARWIN
186 void *fault_addr
= siginfo
->si_addr
;
188 #error unsupported BSD variant
191 os_context_t
*context
= arch_os_get_context(&void_context
);
192 if (!gencgc_handle_wp_violation(fault_addr
))
193 if(!handle_guard_page_triggered(context
,fault_addr
))
194 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
195 arrange_return_to_lisp_function(context
, SymbolFunction(MEMORY_FAULT_ERROR
));
197 interrupt_handle_now(signal
, siginfo
, context
);
201 os_install_interrupt_handlers(void)
203 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
204 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
205 memory_fault_handler
);
206 SHOW("leaving os_install_interrupt_handlers()");
209 #else /* Currently Darwin only */
212 sigsegv_handler(int signal
, siginfo_t
*info
, void* void_context
)
214 os_context_t
*context
= arch_os_get_context(&void_context
);
215 unsigned int pc
= (unsigned int *)(*os_context_pc_addr(context
));
216 os_vm_address_t addr
;
218 addr
= arch_get_bad_addr(signal
,info
,context
);
219 if(!interrupt_maybe_gc(signal
, info
, context
))
220 if(!handle_guard_page_triggered(context
,addr
))
221 interrupt_handle_now(signal
, info
, context
);
222 /* Work around G5 bug; fix courtesy gbyers */
223 DARWIN_FIX_CONTEXT(context
);
227 os_install_interrupt_handlers(void)
229 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
230 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
234 #endif /* defined GENCGC */
237 static void netbsd_init()
243 /* Are we running on a sufficiently functional kernel? */
248 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
250 /* If we're older than 2.0... */
251 if (osrev
< 200000000) {
252 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
253 lose("NetBSD kernel too old to run sbcl.\n");
256 /* NetBSD counts mmap()ed space against the process's data size limit,
257 * so yank it up. This might be a nasty thing to do? */
258 getrlimit (RLIMIT_DATA
, &rl
);
259 /* Amazingly for such a new port, the provenance and meaning of
260 this number are unknown. It might just mean REALLY_BIG_LIMIT,
261 or possibly it should be calculated from dynamic space size.
262 -- CSR, 2004-04-08 */
263 rl
.rlim_cur
= 1073741824;
264 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
266 "RUNTIME WARNING: unable to raise process data size limit:\n\
268 The system may fail to start.\n",
272 #endif /* __NetBSD__ */
276 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
277 #ifdef LISP_FEATURE_SB_THREAD
278 #error "Define threading support functions"
280 int arch_os_thread_init(struct thread
*thread
) {
282 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
283 /* Signal handlers are run on the control stack, so if it is exhausted
284 * we had better use an alternate stack for whatever signal tells us
285 * we've exhausted it */
286 sigstack
.ss_sp
=((void *) thread
)+dynamic_values_bytes
;
288 sigstack
.ss_size
= 32*SIGSTKSZ
;
289 sigaltstack(&sigstack
,0);
291 return 1; /* success */
293 int arch_os_thread_cleanup(struct thread
*thread
) {
294 return 1; /* success */