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>
30 #include "interrupt.h"
34 #include <sys/socket.h>
35 #include <sys/utsname.h>
37 #include <sys/types.h>
39 /* #include <sys/sysinfo.h> */
43 #include <linux/version.h>
47 size_t os_vm_page_size;
51 int linux_sparc_siginfo_bug = 0;
55 /* Conduct various version checks: do we have enough mmap(), is
56 * this a sparc running 2.2, can we do threads? */
64 major_version = atoi(p);
66 minor_version = atoi(p);
67 if (major_version<2) {
68 lose("linux kernel version too old: major version=%d (can't run in version < 2.0.0)",
71 #ifdef LISP_FEATURE_SB_THREAD
72 if ((major_version <2) || (major_version==2 && minor_version < 4)) {
73 lose("linux kernel 2.4 required for thread-enabled SBCL");
76 #ifdef LISP_FEATURE_SPARC
77 if ((major_version <2) || (major_version==2 && minor_version < 4)) {
78 FSHOW((stderr,"linux kernel %d.%d predates 2.4;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", major_version,minor_version));
79 linux_sparc_siginfo_bug = 1;
84 os_vm_page_size = getpagesize();
85 /* This could just as well be in arch_init(), but it's not. */
87 /* FIXME: This used to be here. However, I have just removed it
88 with no apparent ill effects (it may be that earlier kernels
89 started up a process with a different set of traps, or
90 something?) Find out what this was meant to do, and reenable it
91 or delete it if possible. -- CSR, 2002-07-15 */
92 /* SET_FPU_CONTROL_WORD(0x1372|4|8|16|32); no interrupts */
97 #ifdef LISP_FEATURE_ALPHA
98 /* The Alpha is a 64 bit CPU. SBCL is a 32 bit application. Due to all
99 * the places that assume we can get a pointer into a fixnum with no
100 * information loss, we have to make sure it allocates all its ram in the
103 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
107 os_validate(os_vm_address_t addr, os_vm_size_t len)
109 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
110 os_vm_address_t actual ;
114 #ifdef LISP_FEATURE_ALPHA
117 addr=under_2gb_free_pointer;
120 actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
121 if (actual == MAP_FAILED || (addr && (addr!=actual))) {
123 return 0; /* caller should check this */
126 #ifdef LISP_FEATURE_ALPHA
128 len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
129 under_2gb_free_pointer+=len;
136 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
138 if (munmap(addr,len) == -1) {
144 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
146 addr = mmap(addr, len,
148 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
151 if (addr == MAP_FAILED) {
153 lose("unexpected mmap(..) failure");
160 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
162 if (mprotect(address, length, prot) == -1) {
167 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
168 * description of a space, we could probably punt this and just do
169 * (FOO_START <= x && x < FOO_END) everywhere it's called. */
171 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
173 char* beg = (char*)((long)sbeg);
174 char* end = (char*)((long)sbeg) + slen;
175 char* adr = (char*)a;
176 return (adr >= beg && adr < end);
180 is_valid_lisp_addr(os_vm_address_t addr)
183 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
184 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
185 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
187 for_each_thread(th) {
188 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
190 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
197 * any OS-dependent special low-level handling for signals
201 #if defined LISP_FEATURE_GENCGC
204 * The GENCGC needs to be hooked into whatever signal is raised for
205 * page fault on this OS.
208 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
210 os_context_t *context = arch_os_get_context(&void_context);
211 void* fault_addr = (void*)context->uc_mcontext.cr2;
212 if (!gencgc_handle_wp_violation(fault_addr))
213 if(!handle_control_stack_guard_triggered(context,fault_addr))
214 interrupt_handle_now(signal, info, void_context);
220 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
222 os_context_t *context = arch_os_get_context(&void_context);
223 os_vm_address_t addr;
225 addr = arch_get_bad_addr(signal,info,context);
227 *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
229 /* Alpha stuff: This is the end of a pseudo-atomic section
230 * during which a signal was received. We must deal with the
231 * pending interrupt (see also interrupt.c,
232 * ../code/interrupt.lisp)
234 /* (how we got here: when interrupting, we set bit 63 in
235 * reg_Alloc. At the end of the atomic section we tried to
236 * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
237 * there) so ended up here
239 *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
240 interrupt_handle_pending(context);
242 if(!interrupt_maybe_gc(signal, info, context))
243 if(!handle_control_stack_guard_triggered(context,addr))
244 interrupt_handle_now(signal, info, context);
249 void sigcont_handler(int signal, siginfo_t *info, void *void_context)
251 /* we need to have a handler installed for this signal so that
252 * sigwaitinfo() for it actually returns at the appropriate time
257 os_install_interrupt_handlers(void)
259 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
261 undoably_install_low_level_interrupt_handler(SIGCONT,