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>
50 size_t os_vm_page_size;
52 #ifdef LISP_FEATURE_SB_THREAD
53 #include <linux/unistd.h>
56 /* values taken from the kernel's linux/futex.h. This header file
57 doesn't exist in userspace, which is our excuse for not grovelling
59 #define FUTEX_WAIT (0)
60 #define FUTEX_WAKE (1)
62 #define FUTEX_REQUEUE (3)
64 #define __NR_sys_futex __NR_futex
66 _syscall4(int,sys_futex,
70 struct timespec *, rel);
75 int linux_sparc_siginfo_bug = 0;
79 /* Conduct various version checks: do we have enough mmap(), is
80 * this a sparc running 2.2, can we do threads? */
88 major_version = atoi(p);
90 minor_version = atoi(p);
91 if (major_version<2) {
92 lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
95 if (!(major_version>2 || minor_version >= 4)) {
96 #ifdef LISP_FEATURE_SPARC
97 FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
98 linux_sparc_siginfo_bug = 1;
101 #ifdef LISP_FEATURE_SB_THREAD
102 futex_wait(futex,-1);
104 lose("linux with NPTL support (e.g. kernel 2.6 or newer) required for thread-enabled SBCL");
107 os_vm_page_size = getpagesize();
111 #ifdef LISP_FEATURE_ALPHA
112 /* The Alpha is a 64 bit CPU. SBCL is a 32 bit application. Due to all
113 * the places that assume we can get a pointer into a fixnum with no
114 * information loss, we have to make sure it allocates all its ram in the
117 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
121 os_validate(os_vm_address_t addr, os_vm_size_t len)
123 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
124 os_vm_address_t actual ;
128 #ifdef LISP_FEATURE_ALPHA
131 addr=under_2gb_free_pointer;
134 actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
135 if (actual == MAP_FAILED || (addr && (addr!=actual))) {
137 return 0; /* caller should check this */
140 #ifdef LISP_FEATURE_ALPHA
142 len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
143 under_2gb_free_pointer+=len;
150 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
152 if (munmap(addr,len) == -1) {
158 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
160 addr = mmap(addr, len,
162 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
165 if (addr == MAP_FAILED) {
167 lose("unexpected mmap(..) failure");
174 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
176 if (mprotect(address, length, prot) == -1) {
181 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
182 * description of a space, we could probably punt this and just do
183 * (FOO_START <= x && x < FOO_END) everywhere it's called. */
185 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
187 char* beg = (char*)((long)sbeg);
188 char* end = (char*)((long)sbeg) + slen;
189 char* adr = (char*)a;
190 return (adr >= beg && adr < end);
194 is_valid_lisp_addr(os_vm_address_t addr)
197 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
198 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
199 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
201 for_each_thread(th) {
202 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
204 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
211 * any OS-dependent special low-level handling for signals
215 #if defined LISP_FEATURE_GENCGC
218 * The GENCGC needs to be hooked into whatever signal is raised for
219 * page fault on this OS.
222 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
224 os_context_t *context = arch_os_get_context(&void_context);
225 void* fault_addr = (void*)info->si_addr;
226 if (!gencgc_handle_wp_violation(fault_addr))
227 if(!handle_guard_page_triggered(context,fault_addr))
228 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
229 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
231 interrupt_handle_now(signal, info, context);
238 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
240 os_context_t *context = arch_os_get_context(&void_context);
241 os_vm_address_t addr;
243 addr = arch_get_bad_addr(signal,info,context);
245 *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
247 /* Alpha stuff: This is the end of a pseudo-atomic section
248 * during which a signal was received. We must deal with the
249 * pending interrupt (see also interrupt.c,
250 * ../code/interrupt.lisp)
252 /* (how we got here: when interrupting, we set bit 63 in
253 * reg_Alloc. At the end of the atomic section we tried to
254 * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
255 * there) so ended up here
257 *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
258 interrupt_handle_pending(context);
260 if(!interrupt_maybe_gc(signal, info, context))
261 if(!handle_guard_page_triggered(context,addr))
262 interrupt_handle_now(signal, info, context);
267 void sigcont_handler(int signal, siginfo_t *info, void *void_context)
269 /* We need to have a handler installed for this signal so that
270 * sigwaitinfo() for it actually returns at the appropriate time.
271 * We don't need it to actually do anything. This mkes it
272 * possibly the only signal handler in SBCL that doesn't depend on
273 * not-guaranteed-by-POSIX features
278 os_install_interrupt_handlers(void)
280 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
282 #ifdef LISP_FEATURE_SB_THREAD
283 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
284 interrupt_thread_handler);
285 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
286 sig_stop_for_gc_handler);
287 undoably_install_low_level_interrupt_handler(SIG_THREAD_EXIT,
288 thread_exit_handler);
292 #ifdef LISP_FEATURE_SB_THREAD
293 int futex_wait(int *lock_word, int oldval) {
294 int t= sys_futex(lock_word,FUTEX_WAIT,oldval, 0);
297 int futex_wake(int *lock_word, int n){
298 return sys_futex(lock_word,FUTEX_WAKE,n,0);