2 * the Linux incarnation of OS-dependent routines. See also
3 * $(sbcl_arch)-linux-os.c
5 * This file (along with os.h) exports an OS-independent interface to
6 * the operating system VM facilities. Surprise surprise, this
7 * interface looks a lot like the Mach interface (but simpler in some
8 * places). For some operating systems, a subset of these functions
9 * will have to be emulated.
13 * This software is part of the SBCL system. See the README file for
16 * This software is derived from the CMU CL system, which was
17 * written at Carnegie Mellon University and released into the
18 * public domain. The software is in the public domain and is
19 * provided with absolutely no warranty. See the COPYING and CREDITS
20 * files for more information.
24 #include <sys/param.h>
31 #include "interrupt.h"
35 #include "genesis/static-symbols.h"
36 #include "genesis/fdefn.h"
37 #include <sys/socket.h>
38 #include <sys/utsname.h>
40 #include <sys/types.h>
42 /* #include <sys/sysinfo.h> */
46 #include <linux/version.h>
51 #if defined LISP_FEATURE_GENCGC
52 #include "gencgc-internal.h"
55 #ifdef LISP_FEATURE_LINUX
56 #include <sys/personality.h>
59 size_t os_vm_page_size;
61 #ifdef LISP_FEATURE_SB_THREAD
62 #include <sys/syscall.h>
66 /* values taken from the kernel's linux/futex.h. This header file
67 doesn't exist in userspace, which is our excuse for not grovelling
69 #define FUTEX_WAIT (0)
70 #define FUTEX_WAKE (1)
72 #define FUTEX_REQUEUE (3)
74 #define sys_futex sbcl_sys_futex
75 static inline int sys_futex (void *futex, int op, int val, struct timespec *rel)
77 return syscall (SYS_futex, futex, op, val, rel);
81 futex_wait(int *lock_word, int oldval)
83 int t= sys_futex(lock_word,FUTEX_WAIT,oldval, 0);
88 futex_wake(int *lock_word, int n)
90 return sys_futex(lock_word,FUTEX_WAKE,n,0);
95 int linux_sparc_siginfo_bug = 0;
96 int linux_no_threads_p = 0;
99 os_init(char *argv[], char *envp[])
101 /* Conduct various version checks: do we have enough mmap(), is
102 * this a sparc running 2.2, can we do threads? */
103 #ifdef LISP_FEATURE_SB_THREAD
112 major_version = atoi(p);
114 minor_version = atoi(p);
115 if (major_version<2) {
116 lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
119 if (!(major_version>2 || minor_version >= 4)) {
120 #ifdef LISP_FEATURE_SPARC
121 FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
122 linux_sparc_siginfo_bug = 1;
125 #ifdef LISP_FEATURE_SB_THREAD
126 futex_wait(futex,-1);
127 if(errno==ENOSYS) linux_no_threads_p = 1;
128 if(linux_no_threads_p)
129 fprintf(stderr,"Linux with NPTL support (e.g. kernel 2.6 or newer) required for \nthread-enabled SBCL. Disabling thread support.\n\n");
131 os_vm_page_size = getpagesize();
133 /* KLUDGE: Disable memory randomization on new Linux kernels
134 * by setting a personality flag and re-executing. (We need
135 * to re-execute, since the memory maps that can conflict with
136 * the SBCL spaces have already been done at this point).
138 #if defined(LISP_FEATURE_X86)
139 if ((major_version == 2 && minor_version >= 6)
140 || major_version >= 3)
142 long pers = personality(-1);
143 /* 0x40000 aka. ADDR_NO_RANDOMIZE */
144 if (!(pers & 0x40000)) {
145 if (personality(pers | 0x40000) != -1) {
146 char runtime[PATH_MAX+1];
147 /* Use /proc/self/exe instead of trying to figure out the
148 * executable path from PATH and argv[0], since that's
151 if (readlink("/proc/self/exe", runtime, PATH_MAX) != -1) {
152 execve(runtime, argv, envp);
155 /* Either changing the personality or execve() failed. Either
156 * way we might as well continue, and hope that the random
157 * memory maps are ok this time around.
159 fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
166 #ifdef LISP_FEATURE_ALPHA
167 /* The Alpha is a 64 bit CPU. SBCL is a 32 bit application. Due to all
168 * the places that assume we can get a pointer into a fixnum with no
169 * information loss, we have to make sure it allocates all its ram in the
172 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
176 os_validate(os_vm_address_t addr, os_vm_size_t len)
178 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
179 os_vm_address_t actual;
181 #ifdef LISP_FEATURE_ALPHA
183 addr=under_2gb_free_pointer;
186 actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
187 if (actual == MAP_FAILED || (addr && (addr!=actual))) {
189 return 0; /* caller should check this */
192 #ifdef LISP_FEATURE_ALPHA
194 len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
195 under_2gb_free_pointer+=len;
202 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
204 if (munmap(addr,len) == -1) {
210 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
212 os_vm_address_t actual;
214 actual = mmap(addr, len, OS_VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
216 if (actual == MAP_FAILED || (addr && (addr != actual))) {
218 lose("unexpected mmap(..) failure");
225 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
227 if (mprotect(address, length, prot) == -1) {
233 is_valid_lisp_addr(os_vm_address_t addr)
236 size_t ad = (size_t) addr;
238 if ((READ_ONLY_SPACE_START <= ad && ad < READ_ONLY_SPACE_END)
239 || (STATIC_SPACE_START <= ad && ad < STATIC_SPACE_END)
240 #if defined LISP_FEATURE_GENCGC
241 || (DYNAMIC_SPACE_START <= ad && ad < DYNAMIC_SPACE_END)
243 || (DYNAMIC_0_SPACE_START <= ad && ad < DYNAMIC_0_SPACE_END)
244 || (DYNAMIC_1_SPACE_START <= ad && ad < DYNAMIC_1_SPACE_END)
248 for_each_thread(th) {
249 if((size_t)(th->control_stack_start) <= ad
250 && ad < (size_t)(th->control_stack_end))
252 if((size_t)(th->binding_stack_start) <= ad
253 && ad < (size_t)(th->binding_stack_start + BINDING_STACK_SIZE))
260 * any OS-dependent special low-level handling for signals
264 #if defined LISP_FEATURE_GENCGC
267 * The GENCGC needs to be hooked into whatever signal is raised for
268 * page fault on this OS.
271 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
273 os_context_t *context = arch_os_get_context(&void_context);
274 void* fault_addr = (void*)info->si_addr;
275 if (!gencgc_handle_wp_violation(fault_addr))
276 if(!handle_guard_page_triggered(context,fault_addr))
277 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
278 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
280 interrupt_handle_now(signal, info, context);
287 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
289 os_context_t *context = arch_os_get_context(&void_context);
290 os_vm_address_t addr = arch_get_bad_addr(signal,info,context);
292 #ifdef LISP_FEATURE_ALPHA
293 /* Alpha stuff: This is the end of a pseudo-atomic section during
294 which a signal was received. We must deal with the pending
295 interrupt (see also interrupt.c, ../code/interrupt.lisp)
297 (how we got here: when interrupting, we set bit 63 in reg_ALLOC.
298 At the end of the atomic section we tried to write to reg_ALLOC,
299 got a SIGSEGV (there's nothing mapped there) so ended up here. */
301 *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
302 *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
303 interrupt_handle_pending(context);
308 if(!interrupt_maybe_gc(signal, info, context))
309 if(!handle_guard_page_triggered(context,addr))
310 interrupt_handle_now(signal, info, context);
315 os_install_interrupt_handlers(void)
317 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
319 #ifdef LISP_FEATURE_SB_THREAD
320 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
321 interrupt_thread_handler);
322 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
323 sig_stop_for_gc_handler);