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> */
44 #if defined LISP_FEATURE_GENCGC
45 #include "gencgc-internal.h"
48 os_vm_size_t os_vm_page_size
;
51 #include <sys/resource.h>
52 #include <sys/sysctl.h>
54 #include <sys/stat.h> /* For the stat-family wrappers. */
55 #include <dirent.h> /* For the opendir()/readdir() wrappers */
56 #include <sys/socket.h> /* For the socket() wrapper */
57 static void netbsd_init();
58 #endif /* __NetBSD__ */
61 #include <sys/sysctl.h>
63 static void freebsd_init();
64 #endif /* __FreeBSD__ */
67 os_init(char *argv
[], char *envp
[])
69 os_vm_page_size
= getpagesize();
73 #elif defined(__FreeBSD__)
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__) || defined(__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\n");
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
)
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 (((os_vm_address_t
)th
->control_stack_start
<= addr
) &&
162 (addr
< (os_vm_address_t
)th
->control_stack_end
))
164 if (in_range_p(addr
, (lispobj
) th
->binding_stack_start
,
172 * any OS-dependent special low-level handling for signals
175 #if defined LISP_FEATURE_GENCGC
178 * The GENCGC needs to be hooked into whatever signal is raised for
179 * page fault on this OS.
183 memory_fault_handler(int signal
, siginfo_t
*siginfo
, void *void_context
184 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
185 /* FreeBSD/amd64 stores fault address only in undocumented 4th arg. */
190 os_context_t
*context
= arch_os_get_context(&void_context
);
191 #if defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_X86_64)
192 /* KLUDGE: Store fault address into si_addr for compatibilities. */
193 siginfo
->si_addr
= fault_addr
;
195 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: %p\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 lisp_memory_fault_error(context
, fault_addr
);
211 if (!maybe_gc(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
);
222 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
224 mach_error_memory_fault_handler(int signal
, siginfo_t
*siginfo
, void *void_context
) {
225 lose("Unhandled memory fault. Exiting.");
230 os_install_interrupt_handlers(void)
232 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
233 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
234 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
235 mach_error_memory_fault_handler
);
237 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
238 #ifdef LISP_FEATURE_FREEBSD
239 (__siginfohandler_t
*)
241 memory_fault_handler
);
242 #ifdef SIG_MEMORY_FAULT2
243 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2
,
244 #ifdef LISP_FEATURE_FREEBSD
245 (__siginfohandler_t
*)
247 memory_fault_handler
);
251 #ifdef LISP_FEATURE_SB_THREAD
252 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD
,
253 interrupt_thread_handler
);
254 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC
,
255 sig_stop_for_gc_handler
);
256 #ifdef SIG_RESUME_FROM_GC
257 undoably_install_low_level_interrupt_handler(SIG_RESUME_FROM_GC
,
258 sig_stop_for_gc_handler
);
261 SHOW("leaving os_install_interrupt_handlers()");
264 #else /* Currently PPC/Darwin/Cheney only */
267 sigsegv_handler(int signal
, siginfo_t
*info
, void* void_context
)
269 os_context_t
*context
= arch_os_get_context(&void_context
);
271 unsigned int pc
= (unsigned int *)(*os_context_pc_addr(context
));
273 os_vm_address_t addr
;
275 addr
= arch_get_bad_addr(signal
, info
, context
);
276 if (!cheneygc_handle_wp_violation(context
, addr
))
277 if (!handle_guard_page_triggered(context
, addr
))
278 interrupt_handle_now(signal
, info
, context
);
279 /* Work around G5 bug; fix courtesy gbyers */
280 DARWIN_FIX_CONTEXT(context
);
284 os_install_interrupt_handlers(void)
286 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
287 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT
,
289 #ifdef SIG_MEMORY_FAULT2
290 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT2
,
295 #endif /* defined GENCGC */
298 static void netbsd_init()
304 /* Are we running on a sufficiently functional kernel? */
309 sysctl(mib
, 2, &osrev
, &len
, NULL
, 0);
311 /* If we're older than 2.0... */
312 if (osrev
< 200000000) {
313 fprintf(stderr
, "osrev = %d (needed at least 200000000).\n", osrev
);
314 lose("NetBSD kernel too old to run sbcl.\n");
317 /* NetBSD counts mmap()ed space against the process's data size limit,
318 * so yank it up. This might be a nasty thing to do? */
319 getrlimit (RLIMIT_DATA
, &rl
);
320 /* Amazingly for such a new port, the provenance and meaning of
321 this number are unknown. It might just mean REALLY_BIG_LIMIT,
322 or possibly it should be calculated from dynamic space size.
323 -- CSR, 2004-04-08 */
324 rl
.rlim_cur
= 1073741824;
325 if (setrlimit (RLIMIT_DATA
, &rl
) < 0) {
327 "RUNTIME WARNING: unable to raise process data size limit:\n\
329 The system may fail to start.\n",
334 /* Various routines in NetBSD's C library are compatibility wrappers
335 for old versions. Programs must be processed by the C toolchain in
336 order to get up-to-date definitions of such routines. */
337 /* The stat-family, opendir, and readdir are used only in sb-posix, as
338 of 2007-01-16. -- RMK */
340 _stat(const char *path
, struct stat
*sb
)
342 return stat(path
, sb
);
345 _lstat(const char *path
, struct stat
*sb
)
347 return lstat(path
, sb
);
350 _fstat(int fd
, struct stat
*sb
)
352 return fstat(fd
, sb
);
356 _opendir(const char *filename
)
358 return opendir(filename
);
363 return readdir(dirp
);
366 /* Used in sb-bsd-sockets. */
368 _socket(int domain
, int type
, int protocol
)
370 return socket(domain
, type
, protocol
);
372 #endif /* __NetBSD__ */
375 static void freebsd_init()
377 /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
378 * 4.x with GENERIC kernel, does not enable SSE support even on
379 * SSE capable CPUs". Detect this situation and skip the
380 * fast_bzero sse/base selection logic that's normally done in
383 #ifdef LISP_FEATURE_X86
387 len
= sizeof(instruction_sse
);
388 if (sysctlbyname("hw.instruction_sse", &instruction_sse
, &len
, NULL
, 0) == 0
389 && instruction_sse
!= 0) {
390 /* Use the SSE detector */
391 fast_bzero_pointer
= fast_bzero_detect
;
393 #endif /* LISP_FEATURE_X86 */
395 #endif /* __FreeBSD__ */
397 #ifdef LISP_FEATURE_DARWIN
398 /* defined in ppc-darwin-os.c instead */
399 #elif defined(LISP_FEATURE_FREEBSD)
400 #ifndef KERN_PROC_PATHNAME
401 #define KERN_PROC_PATHNAME 12
404 extern int getosreldate(void);
407 os_get_runtime_executable_path()
409 char path
[PATH_MAX
+ 1];
411 if (getosreldate() >= 600024) {
412 /* KERN_PROC_PATHNAME is available */
413 size_t len
= PATH_MAX
+ 1;
418 mib
[2] = KERN_PROC_PATHNAME
;
420 if (sysctl(mib
, 4, &path
, &len
, NULL
, 0) != 0)
424 size
= readlink("/proc/curproc/file", path
, sizeof(path
) - 1);
429 if (strcmp(path
, "unknown") == 0)
431 return copied_string(path
);
433 #elif defined(LISP_FEATURE_NETBSD)
435 os_get_runtime_executable_path()
438 char *path
= strdup("/proc/curproc/file");
439 if (path
&& ((stat(path
, &sb
)) == 0))
442 fprintf(stderr
, "Couldn't stat /proc/curproc/file; is /proc mounted?\n");
446 #else /* Not DARWIN or FREEBSD or NETBSD */
448 os_get_runtime_executable_path()