371cbb61ceaec042f3bfba236911bf6d4e169152
[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 size_t os_vm_page_size;
46
47 #include "gc.h"
48 \f
49
50 #ifdef sparc
51 int early_kernel = 0;
52 #endif
53 void os_init(void)
54 {
55     /* Early versions of Linux don't support the mmap(..) functionality
56      * that we need. */
57     {
58         struct utsname name;
59         int major_version;
60 #ifdef sparc
61         int minor_version;
62 #endif
63         uname(&name);
64         major_version = atoi(name.release);
65         if (major_version < 2) {
66             lose("linux major version=%d (can't run in version < 2.0.0)",
67                  major_version);
68         }
69 #ifdef sparc
70         /* KLUDGE: This will break if Linux moves to a uname() version number
71          * that has more than one digit initially -- CSR, 2002-02-12 */
72         minor_version = atoi(name.release+2);
73         if (minor_version < 4) {
74             FSHOW((stderr,"linux minor version=%d;\n enabling workarounds for SPARC kernel bugs in signal handling.\n", minor_version));
75             early_kernel = 1;
76         }
77 #endif
78     }
79
80     os_vm_page_size = getpagesize();
81     /* This could just as well be in arch_init(), but it's not. */
82 #ifdef __i386__
83     /* FIXME: This used to be here.  However, I have just removed it
84        with no apparent ill effects (it may be that earlier kernels
85        started up a process with a different set of traps, or
86        something?) Find out what this was meant to do, and reenable it
87        or delete it if possible. -- CSR, 2002-07-15 */
88     /* SET_FPU_CONTROL_WORD(0x1372|4|8|16|32);  no interrupts */
89 #endif
90 }
91
92 /* In Debian CMU CL ca. 2.4.9, it was possible to get an infinite
93  * cascade of errors from do_mmap(..). This variable is a counter to
94  * prevent that; when it counts down to zero, an error in do_mmap
95  * causes the low-level monitor to be called. */
96 int n_do_mmap_ignorable_errors = 3;
97
98 /* Return 0 for success. */
99 static int
100 do_mmap(os_vm_address_t *addr, os_vm_size_t len, int flags)
101 {
102     /* We *must* have the memory where we expect it. */
103     os_vm_address_t old_addr = *addr;
104
105     *addr = mmap(*addr, len, OS_VM_PROT_ALL, flags, -1, 0);
106     if (*addr == MAP_FAILED ||
107         ((old_addr != NULL) && (*addr != old_addr))) {
108         FSHOW((stderr,
109                "/retryable error in allocating memory from the OS\n"
110                "(addr=0x%lx, len=0x%lx, flags=0x%lx)\n",
111                (long) addr,
112                (long) len,
113                (long) flags));
114         if (n_do_mmap_ignorable_errors > 0) {
115             --n_do_mmap_ignorable_errors;
116         } else {
117             lose("too many errors in allocating memory from the OS");
118         }
119         perror("mmap");
120         return 1;
121     }
122     return 0;
123 }
124
125 os_vm_address_t
126 os_validate(os_vm_address_t addr, os_vm_size_t len)
127 {
128     if (addr) {
129         int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
130         os_vm_address_t base_addr = addr;
131         do {
132             /* KLUDGE: It looks as though this code allocates memory
133              * in chunks of size no larger than 'magic', but why? What
134              * is the significance of 0x1000000 here? Also, can it be
135              * right that if the first few 'do_mmap' calls succeed,
136              * then one fails, we leave the memory allocated by the
137              * first few in place even while we return a code for
138              * complete failure? -- WHN 19991020
139              *
140              * Peter Van Eynde writes (20000211)
141              *     This was done because the kernel would only check for
142              *   overcommit for every allocation seperately. So if you
143              *   had 16MB of free mem+swap you could allocate 16M. And
144              *   again, and again, etc. 
145              *     This in [Linux] 2.X could be bad as they changed the memory
146              *   system. A side effect was/is (I don't really know) that
147              *   programs with a lot of memory mappings run slower. But
148              *   of course for 2.2.2X we now have the NO_RESERVE flag that
149              *   helps...
150              *
151              * FIXME: The logic is also flaky w.r.t. failed
152              * allocations. If we make one or more successful calls to
153              * do_mmap(..) before one fails, then we've allocated
154              * memory, and we should ensure that it gets deallocated
155              * sometime somehow. If this function's response to any
156              * failed do_mmap(..) is to give up and return NULL (as in
157              * sbcl-0.6.7), then any failed do_mmap(..) after any
158              * successful do_mmap(..) causes a memory leak. */
159             int magic = 0x1000000;
160             if (len <= magic) {
161                 if (do_mmap(&addr, len, flags)) {
162                     return NULL;
163                 }
164                 len = 0;
165             } else {
166                 if (do_mmap(&addr, magic, flags)) {
167                     return NULL;
168                 }
169                 addr += magic;
170                 len = len - magic;
171             }
172         } while (len > 0);
173         return base_addr;
174     } else {
175         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
176         if (do_mmap(&addr, len, flags)) {
177             return NULL;
178         } else {
179             return addr;
180         }
181     }
182 }
183
184 void
185 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
186 {
187     if (munmap(addr,len) == -1) {
188         perror("munmap");
189     }
190 }
191
192 os_vm_address_t
193 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
194 {
195     addr = mmap(addr, len,
196                 OS_VM_PROT_ALL,
197                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
198                 fd, (off_t) offset);
199
200     if (addr == MAP_FAILED) {
201         perror("mmap");
202         lose("unexpected mmap(..) failure");
203     }
204
205     return addr;
206 }
207
208 void
209 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
210 {
211     if (mprotect(address, length, prot) == -1) {
212         perror("mprotect");
213     }
214 }
215 \f
216 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
217  * description of a space, we could probably punt this and just do
218  * (FOO_START <= x && x < FOO_END) everywhere it's called. */
219 static boolean
220 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
221 {
222     char* beg = (char*)((long)sbeg);
223     char* end = (char*)((long)sbeg) + slen;
224     char* adr = (char*)a;
225     return (adr >= beg && adr < end);
226 }
227
228 boolean
229 is_valid_lisp_addr(os_vm_address_t addr)
230 {
231     return
232         in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
233         in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
234         in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE) ||
235         in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE) ||
236         in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE);
237 }
238 \f
239 /*
240  * any OS-dependent special low-level handling for signals
241  */
242
243
244 #if defined LISP_FEATURE_GENCGC
245
246 /*
247  * The GENCGC needs to be hooked into whatever signal is raised for
248  * page fault on this OS.
249  */
250 void
251 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
252 {
253     os_context_t *context = arch_os_get_context(&void_context);
254     void* fault_addr = (void*)context->uc_mcontext.cr2;
255     if (!gencgc_handle_wp_violation(fault_addr)) 
256         if(!handle_control_stack_guard_triggered(context,fault_addr))
257             interrupt_handle_now(signal, info, void_context);
258 }
259
260 #else
261
262 static void
263 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
264 {
265     os_context_t *context = arch_os_get_context(&void_context);
266     os_vm_address_t addr;
267
268     addr = arch_get_bad_addr(signal,info,context);
269     if (addr != NULL && 
270         *os_context_register_addr(context,reg_ALLOC) & (1L<<63)){
271         
272         /* Alpha stuff: This is the end of a pseudo-atomic section
273          * during which a signal was received.  We must deal with the
274          * pending interrupt (see also interrupt.c,
275          * ../code/interrupt.lisp)
276          */
277         /* (how we got here: when interrupting, we set bit 63 in
278          * reg_Alloc.  At the end of the atomic section we tried to
279          * write to reg_ALLOC, got a SIGSEGV (there's nothing mapped
280          * there) so ended up here
281          */
282         *os_context_register_addr(context,reg_ALLOC) -= (1L<<63);
283         interrupt_handle_pending(context);
284     } else {
285         if(!interrupt_maybe_gc(signal, info, context))
286             if(!handle_control_stack_guard_triggered(context,addr))
287                 interrupt_handle_now(signal, info, context);
288     }
289 }
290 #endif
291
292 void
293 os_install_interrupt_handlers(void)
294 {
295     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
296                                                  sigsegv_handler);
297 }
298