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