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>
32 #include "interrupt.h"
37 #include "genesis/static-symbols.h"
38 #include "genesis/fdefn.h"
40 #include <sys/types.h>
42 /* #include <sys/sysinfo.h> */
45 os_vm_size_t os_vm_page_size;
48 #include <sys/resource.h>
49 #include <sys/sysctl.h>
52 static void netbsd_init();
53 #endif /* __NetBSD__ */
56 #include <sys/sysctl.h>
57 #include <osreldate.h>
59 static void freebsd_init();
60 #endif /* __FreeBSD__ */
63 os_init(char *argv[], char *envp[])
65 os_vm_page_size = getpagesize();
69 #endif /* __NetBSD__ */
72 #endif /* __FreeBSD__ */
75 int *os_context_pc_addr(os_context_t *context)
77 #if defined __FreeBSD__
78 return CONTEXT_ADDR_FROM_STEM(eip);
79 #elif defined __OpenBSD__
80 return CONTEXT_ADDR_FROM_STEM(pc);
81 #elif defined __NetBSD__
82 return CONTEXT_ADDR_FROM_STEM(EIP);
83 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
84 return CONTEXT_ADDR_FROM_STEM(eip);
85 #elif defined LISP_FEATURE_DARWIN
86 return &context->uc_mcontext->ss.srr0;
88 #error unsupported BSD variant
93 os_context_sigmask_addr(os_context_t *context)
95 /* (Unlike most of the other context fields that we access, the
96 * signal mask field is a field of the basic, outermost context
97 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
98 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
99 return &context->uc_sigmask;
100 #elif defined (__OpenBSD__)
101 return &context->sc_mask;
103 #error unsupported BSD variant
108 os_validate(os_vm_address_t addr, os_vm_size_t len)
110 int flags = MAP_PRIVATE | MAP_ANON;
115 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
117 if (addr == MAP_FAILED) {
126 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
128 if (munmap(addr, len) == -1)
133 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
135 addr = mmap(addr, len,
137 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
140 if (addr == MAP_FAILED) {
142 lose("unexpected mmap(..) failure\n");
149 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
151 if (mprotect(address, length, prot) == -1) {
157 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
159 char* beg = (char*) sbeg;
160 char* end = (char*) sbeg + slen;
161 char* adr = (char*) a;
162 return (adr >= beg && adr < end);
166 is_valid_lisp_addr(os_vm_address_t addr)
169 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
170 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
171 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
173 for_each_thread(th) {
174 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
176 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
183 * any OS-dependent special low-level handling for signals
186 #if defined LISP_FEATURE_GENCGC
189 * The GENCGC needs to be hooked into whatever signal is raised for
190 * page fault on this OS.
193 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
195 os_context_t *context = arch_os_get_context(&void_context);
196 void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
198 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
199 FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
200 *CONTEXT_ADDR_FROM_STEM(fs)));
201 os_restore_tls_segment_register(context);
204 FSHOW((stderr, "Memory fault at: %p, PC: %x\n", fault_addr, *os_context_pc_addr(context)));
206 if (!gencgc_handle_wp_violation(fault_addr))
207 if(!handle_guard_page_triggered(context,fault_addr)) {
208 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
209 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
211 if (!interrupt_maybe_gc_int(signal, siginfo, context)) {
212 interrupt_handle_now(signal, siginfo, context);
214 #if defined(LISP_FEATURE_DARWIN)
215 /* Work around G5 bug; fix courtesy gbyers */
216 DARWIN_FIX_CONTEXT(context);
223 os_install_interrupt_handlers(void)
225 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
226 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
227 memory_fault_handler);
228 #ifdef SIG_MEMORY_FAULT2
229 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
230 memory_fault_handler);
233 #ifdef LISP_FEATURE_SB_THREAD
234 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
235 interrupt_thread_handler);
236 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
237 sig_stop_for_gc_handler);
238 #ifdef SIG_RESUME_FROM_GC
239 undoably_install_low_level_interrupt_handler(SIG_RESUME_FROM_GC,
240 sig_stop_for_gc_handler);
244 SHOW("leaving os_install_interrupt_handlers()");
247 #else /* Currently PPC/Darwin/Cheney only */
250 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
252 os_context_t *context = arch_os_get_context(&void_context);
253 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
254 os_vm_address_t addr;
256 addr = arch_get_bad_addr(signal,info,context);
257 if(!interrupt_maybe_gc(signal, info, context))
258 if(!handle_guard_page_triggered(context,addr))
259 interrupt_handle_now(signal, info, context);
260 /* Work around G5 bug; fix courtesy gbyers */
261 DARWIN_FIX_CONTEXT(context);
265 os_install_interrupt_handlers(void)
267 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
268 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
270 #ifdef SIG_MEMORY_FAULT2
271 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2,
276 #endif /* defined GENCGC */
279 static void netbsd_init()
285 /* Are we running on a sufficiently functional kernel? */
290 sysctl(mib, 2, &osrev, &len, NULL, 0);
292 /* If we're older than 2.0... */
293 if (osrev < 200000000) {
294 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
295 lose("NetBSD kernel too old to run sbcl.\n");
298 /* NetBSD counts mmap()ed space against the process's data size limit,
299 * so yank it up. This might be a nasty thing to do? */
300 getrlimit (RLIMIT_DATA, &rl);
301 /* Amazingly for such a new port, the provenance and meaning of
302 this number are unknown. It might just mean REALLY_BIG_LIMIT,
303 or possibly it should be calculated from dynamic space size.
304 -- CSR, 2004-04-08 */
305 rl.rlim_cur = 1073741824;
306 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
308 "RUNTIME WARNING: unable to raise process data size limit:\n\
310 The system may fail to start.\n",
314 #endif /* __NetBSD__ */
317 static void freebsd_init()
319 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
320 * 4.x with GENERIC kernel, does not enable SSE support even on
321 * SSE capable CPUs". Detect this situation and skip the
322 * fast_bzero sse/base selection logic that's normally done in
325 #ifdef LISP_FEATURE_X86
329 len = sizeof(instruction_sse);
330 if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len, NULL, 0) == 0
331 && instruction_sse != 0) {
332 /* Use the SSE detector */
333 fast_bzero_pointer = fast_bzero_detect;
335 #endif /* LISP_FEATURE_X86 */
337 #endif /* __FreeBSD__ */
339 #ifdef LISP_FEATURE_DARWIN
340 /* defined in ppc-darwin-os.c instead */
341 #elif defined(LISP_FEATURE_FREEBSD)
342 #ifndef KERN_PROC_PATHNAME
343 #define KERN_PROC_PATHNAME 12
347 os_get_runtime_executable_path()
349 char path[PATH_MAX + 1];
351 if (getosreldate() >= 600024) {
352 /* KERN_PROC_PATHNAME is available */
353 size_t len = PATH_MAX + 1;
358 mib[2] = KERN_PROC_PATHNAME;
360 if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
364 size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
369 if (strcmp(path, "unknown") == 0)
371 return copied_string(path);
373 #else /* Not DARWIN or FREEBSD */
375 os_get_runtime_executable_path()