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> */
46 size_t os_vm_page_size;
56 /* Early versions of Linux don't support the mmap(..) functionality
65 major_version = atoi(name.release);
66 if (major_version < 2) {
67 lose("linux major version=%d (can't run in version < 2.0.0)",
71 /* KLUDGE: This will break if Linux moves to a uname() version number
72 * that has more than one digit initially -- CSR, 2002-02-12 */
73 minor_version = atoi(name.release+2);
74 if (minor_version < 4) {
75 FSHOW((stderr,"linux minor version=%d;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", minor_version));
81 os_vm_page_size = getpagesize();
82 /* This could just as well be in arch_init(), but it's not. */
84 /* FIXME: This used to be here. However, I have just removed it
85 with no apparent ill effects (it may be that earlier kernels
86 started up a process with a different set of traps, or
87 something?) Find out what this was meant to do, and reenable it
88 or delete it if possible. -- CSR, 2002-07-15 */
89 /* SET_FPU_CONTROL_WORD(0x1372|4|8|16|32); no interrupts */
94 #ifdef LISP_FEATURE_ALPHA
95 /* The Alpha is a 64 bit CPU. SBCL is a 32 bit application. Due to all
96 * the places that assume we can get a pointer into a fixnum with no
97 * information loss, we have to make sure it allocates all its ram in the
100 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
104 os_validate(os_vm_address_t addr, os_vm_size_t len)
106 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
107 os_vm_address_t actual ;
111 #ifdef LISP_FEATURE_ALPHA
114 addr=under_2gb_free_pointer;
117 actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
118 if (actual == MAP_FAILED || (addr && (addr!=actual))) {
120 return 0; /* caller should check this */
123 #ifdef LISP_FEATURE_ALPHA
125 len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
126 under_2gb_free_pointer+=len;
133 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
135 if (munmap(addr,len) == -1) {
141 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
143 addr = mmap(addr, len,
145 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
148 if (addr == MAP_FAILED) {
150 lose("unexpected mmap(..) failure");
157 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
159 if (mprotect(address, length, prot) == -1) {
164 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
165 * description of a space, we could probably punt this and just do
166 * (FOO_START <= x && x < FOO_END) everywhere it's called. */
168 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
170 char* beg = (char*)((long)sbeg);
171 char* end = (char*)((long)sbeg) + slen;
172 char* adr = (char*)a;
173 return (adr >= beg && adr < end);
177 is_valid_lisp_addr(os_vm_address_t addr)
180 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
181 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
182 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
184 for_each_thread(th) {
185 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
187 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
194 * any OS-dependent special low-level handling for signals
198 #if defined LISP_FEATURE_GENCGC
201 * The GENCGC needs to be hooked into whatever signal is raised for
202 * page fault on this OS.
205 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
207 os_context_t *context = arch_os_get_context(&void_context);
208 void* fault_addr = (void*)context->uc_mcontext.cr2;
209 if (!gencgc_handle_wp_violation(fault_addr))
210 if(!handle_control_stack_guard_triggered(context,fault_addr))
211 interrupt_handle_now(signal, info, void_context);
217 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
219 os_context_t *context = arch_os_get_context(&void_context);
220 os_vm_address_t addr;
222 addr = arch_get_bad_addr(signal,info,context);
224 *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
226 /* Alpha stuff: This is the end of a pseudo-atomic section
227 * during which a signal was received. We must deal with the
228 * pending interrupt (see also interrupt.c,
229 * ../code/interrupt.lisp)
231 /* (how we got here: when interrupting, we set bit 63 in
232 * reg_Alloc. At the end of the atomic section we tried to
233 * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
234 * there) so ended up here
236 *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
237 interrupt_handle_pending(context);
239 if(!interrupt_maybe_gc(signal, info, context))
240 if(!handle_control_stack_guard_triggered(context,addr))
241 interrupt_handle_now(signal, info, context);
246 void sigcont_handler(int signal, siginfo_t *info, void *void_context)
248 /* we need to have a handler installed for this signal so that
249 * sigwaitinfo() for it actually returns at the appropriate time
254 os_install_interrupt_handlers(void)
256 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
258 undoably_install_low_level_interrupt_handler(SIGCONT,