0.pre8.40
[sbcl.git] / src / runtime / linux-os.c
1 /*
2  * the Linux incarnation of OS-dependent routines.  See also
3  * $(sbcl_arch)-linux-os.c
4  *
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.
10  */
11
12 /*
13  * This software is part of the SBCL system. See the README file for
14  * more information.
15  *
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.
21  */
22
23 #include <stdio.h>
24 #include <sys/param.h>
25 #include <sys/file.h>
26 #include "./signal.h"
27 #include "os.h"
28 #include "arch.h"
29 #include "globals.h"
30 #include "interrupt.h"
31 #include "interr.h"
32 #include "lispregs.h"
33 #include "sbcl.h"
34 #include <sys/socket.h>
35 #include <sys/utsname.h>
36
37 #include <sys/types.h>
38 #include <signal.h>
39 /* #include <sys/sysinfo.h> */
40 #include <sys/time.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43
44 #include "validate.h"
45 #include "thread.h"
46 size_t os_vm_page_size;
47
48 #include "gc.h"
49 \f
50
51 #ifdef sparc
52 int early_kernel = 0;
53 #endif
54 void os_init(void)
55 {
56     /* Early versions of Linux don't support the mmap(..) functionality
57      * that we need. */
58     {
59         struct utsname name;
60         int major_version;
61 #ifdef sparc
62         int minor_version;
63 #endif
64         uname(&name);
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)",
68                  major_version);
69         }
70 #ifdef sparc
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));
76             early_kernel = 1;
77         }
78 #endif
79     }
80
81     os_vm_page_size = getpagesize();
82     /* This could just as well be in arch_init(), but it's not. */
83 #ifdef __i386__
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 */
90 #endif
91 }
92
93
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
98  * 0-2Gb region.  */
99
100 static void * under_2gb_free_pointer=DYNAMIC_1_SPACE_END;
101 #endif
102
103 os_vm_address_t
104 os_validate(os_vm_address_t addr, os_vm_size_t len)
105 {
106     int flags =  MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
107     os_vm_address_t actual ;
108
109     if (addr) 
110         flags |= MAP_FIXED;
111 #ifdef LISP_FEATURE_ALPHA
112     else {
113         flags |= MAP_FIXED;
114         addr=under_2gb_free_pointer;
115     }
116 #endif  
117     actual = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
118     if (actual == MAP_FAILED || (addr && (addr!=actual))) {
119         perror("mmap");
120         return 0;               /* caller should check this */
121     }
122
123 #ifdef LISP_FEATURE_ALPHA
124
125     len=(len+(os_vm_page_size-1))&(~(os_vm_page_size-1));
126     under_2gb_free_pointer+=len;
127 #endif
128
129     return addr;
130 }
131
132 void
133 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
134 {
135     if (munmap(addr,len) == -1) {
136         perror("munmap");
137     }
138 }
139
140 os_vm_address_t
141 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
142 {
143     addr = mmap(addr, len,
144                 OS_VM_PROT_ALL,
145                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
146                 fd, (off_t) offset);
147
148     if (addr == MAP_FAILED) {
149         perror("mmap");
150         lose("unexpected mmap(..) failure");
151     }
152
153     return addr;
154 }
155
156 void
157 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
158 {
159     if (mprotect(address, length, prot) == -1) {
160         perror("mprotect");
161     }
162 }
163 \f
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. */
167 static boolean
168 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
169 {
170     char* beg = (char*)((long)sbeg);
171     char* end = (char*)((long)sbeg) + slen;
172     char* adr = (char*)a;
173     return (adr >= beg && adr < end);
174 }
175
176 boolean
177 is_valid_lisp_addr(os_vm_address_t addr)
178 {
179     struct thread *th;
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))
183         return 1;
184     for_each_thread(th) {
185         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
186             return 1;
187         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
188             return 1;
189     }
190     return 0;
191 }
192 \f
193 /*
194  * any OS-dependent special low-level handling for signals
195  */
196
197
198 #if defined LISP_FEATURE_GENCGC
199
200 /*
201  * The GENCGC needs to be hooked into whatever signal is raised for
202  * page fault on this OS.
203  */
204 void
205 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
206 {
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);
212 }
213
214 #else
215
216 static void
217 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
218 {
219     os_context_t *context = arch_os_get_context(&void_context);
220     os_vm_address_t addr;
221
222     addr = arch_get_bad_addr(signal,info,context);
223     if (addr != NULL && 
224         *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
225         
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)
230          */
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
235          */
236         *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
237         interrupt_handle_pending(context);
238     } else {
239         if(!interrupt_maybe_gc(signal, info, context))
240             if(!handle_control_stack_guard_triggered(context,addr))
241                 interrupt_handle_now(signal, info, context);
242     }
243 }
244 #endif
245
246 void sigcont_handler(int signal, siginfo_t *info, void *void_context)
247 {
248     /* we need to have a handler installed for this signal so that
249      * sigwaitinfo() for it actually returns at the appropriate time
250      */
251 }
252
253 void
254 os_install_interrupt_handlers(void)
255 {
256     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
257                                                  sigsegv_handler);
258     undoably_install_low_level_interrupt_handler(SIGCONT,
259                                                  sigcont_handler);
260 }
261