0.6.10.13:
[sbcl.git] / src / runtime / linux-os.c
1 /*
2  * the Linux incarnation of OS-dependent routines
3  *
4  * This file (along with os.h) exports an OS-independent interface to
5  * the operating system VM facilities. Surprise surprise, this
6  * interface looks a lot like the Mach interface (but simpler in some
7  * places). For some operating systems, a subset of these functions
8  * will have to be emulated.
9  */
10
11 /*
12  * This software is part of the SBCL system. See the README file for
13  * more information.
14  *
15  * This software is derived from the CMU CL system, which was
16  * written at Carnegie Mellon University and released into the
17  * public domain. The software is in the public domain and is
18  * provided with absolutely no warranty. See the COPYING and CREDITS
19  * files for more information.
20  */
21
22 #include <stdio.h>
23 #include <sys/param.h>
24 #include <sys/file.h>
25 #include "./signal.h"
26 #include "os.h"
27 #include "arch.h"
28 #include "globals.h"
29 #include "interrupt.h"
30 #include "interr.h"
31 #include "lispregs.h"
32 #include "sbcl.h"
33 #include <sys/socket.h>
34 #include <sys/utsname.h>
35
36 #include <sys/types.h>
37 #include <signal.h>
38 /* #include <sys/sysinfo.h> */
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42
43 #include "validate.h"
44 size_t os_vm_page_size;
45
46 #if defined GENCGC
47 #include "gencgc.h"
48 #endif
49 \f
50 void os_init(void)
51 {
52     /* Early versions of Linux don't support the mmap(..) functionality
53      * that we need. */
54     {
55         struct utsname name;
56         int major_version;
57         uname(&name);
58         major_version = atoi(name.release);
59         if (major_version < 2) {
60             lose("linux major version=%d (can't run in version < 2.0.0)",
61                  major_version);
62         }
63     }
64
65     os_vm_page_size = getpagesize();
66
67     SET_FPU_CONTROL_WORD(0x1372|4|8|16|32); /* no interrupts */
68 }
69
70 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
71  * <sys/ucontext.h> file to define symbolic names for offsets into
72  * gregs[], but it's conditional on __USE_GNU and not defined, so
73  * we need to do this nasty absolute index magic number thing
74  * instead. */
75 int *
76 os_context_register_addr(os_context_t *context, int offset)
77 {
78     switch(offset) {
79     case  0: return &context->uc_mcontext.gregs[11]; /* EAX */
80     case  2: return &context->uc_mcontext.gregs[10]; /* ECX */
81     case  4: return &context->uc_mcontext.gregs[9]; /* EDX */
82     case  6: return &context->uc_mcontext.gregs[8]; /* EBX */
83     case  8: return &context->uc_mcontext.gregs[7]; /* ESP */
84     case 10: return &context->uc_mcontext.gregs[6]; /* EBP */
85     case 12: return &context->uc_mcontext.gregs[5]; /* ESI */
86     case 14: return &context->uc_mcontext.gregs[4]; /* EDI */
87     default: return 0;
88     }
89 }
90 int *
91 os_context_pc_addr(os_context_t *context)
92 {
93     return &context->uc_mcontext.gregs[14];
94 }
95 int *
96 os_context_sp_addr(os_context_t *context)
97 {
98     return &context->uc_mcontext.gregs[17];
99 }
100
101 sigset_t *
102 os_context_sigmask_addr(os_context_t *context)
103 {
104     return &context->uc_sigmask;
105 }
106
107 /* In Debian CMU CL ca. 2.4.9, it was possible to get an infinite
108  * cascade of errors from do_mmap(..). This variable is a counter to
109  * prevent that; when it counts down to zero, an error in do_mmap
110  * causes the low-level monitor to be called. */
111 int n_do_mmap_ignorable_errors = 3;
112
113 /* Return 0 for success. */
114 static int
115 do_mmap(os_vm_address_t *addr, os_vm_size_t len, int flags)
116 {
117     /* We *must* have the memory where we want it. */
118     os_vm_address_t old_addr=*addr;
119
120     *addr = mmap(*addr, len, OS_VM_PROT_ALL, flags, -1, 0);
121     if (*addr == MAP_FAILED ||
122         ((old_addr != NULL) && (*addr != old_addr))) {
123         FSHOW((stderr,
124                "error in allocating memory from the OS\n"
125                "(addr=%lx, len=%lx, flags=%lx)\n",
126                (long) addr,
127                (long) len,
128                (long) flags));
129         if (n_do_mmap_ignorable_errors > 0) {
130             --n_do_mmap_ignorable_errors;
131         } else {
132             lose("too many errors in allocating memory from the OS");
133         }
134         perror("mmap");
135         return 1;
136     }
137     return 0;
138 }
139
140 os_vm_address_t
141 os_validate(os_vm_address_t addr, os_vm_size_t len)
142 {
143     if (addr) {
144         int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
145         os_vm_address_t base_addr = addr;
146         do {
147             /* KLUDGE: It looks as though this code allocates memory
148              * in chunks of size no larger than 'magic', but why? What
149              * is the significance of 0x1000000 here? Also, can it be
150              * right that if the first few 'do_mmap' calls succeed,
151              * then one fails, we leave the memory allocated by the
152              * first few in place even while we return a code for
153              * complete failure? -- WHN 19991020
154              *
155              * Peter Van Eynde writes (20000211)
156              *     This was done because the kernel would only check for
157              *   overcommit for every allocation seperately. So if you
158              *   had 16MB of free mem+swap you could allocate 16M. And
159              *   again, and again, etc. 
160              *     This in [Linux] 2.X could be bad as they changed the memory
161              *   system. A side effect was/is (I don't really know) that
162              *   programs with a lot of memory mappings run slower. But
163              *   of course for 2.2.2X we now have the NO_RESERVE flag that
164              *   helps...
165              *
166              * FIXME: The logic is also flaky w.r.t. failed
167              * allocations. If we make one or more successful calls to
168              * do_mmap(..) before one fails, then we've allocated
169              * memory, and we should ensure that it gets deallocated
170              * sometime somehow. If this function's response to any
171              * failed do_mmap(..) is to give up and return NULL (as in
172              * sbcl-0.6.7), then any failed do_mmap(..) after any
173              * successful do_mmap(..) causes a memory leak. */
174             int magic = 0x1000000;
175             if (len <= magic) {
176                 if (do_mmap(&addr, len, flags)) {
177                     return NULL;
178                 }
179                 len = 0;
180             } else {
181                 if (do_mmap(&addr, magic, flags)) {
182                     return NULL;
183                 }
184                 addr += magic;
185                 len = len - magic;
186             }
187         } while (len > 0);
188         return base_addr;
189     } else {
190         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
191         if (do_mmap(&addr, len, flags)) {
192             return NULL;
193         } else {
194             return addr;
195         }
196     }
197 }
198
199 void
200 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
201 {
202     if (munmap(addr,len) == -1) {
203         perror("munmap");
204     }
205 }
206
207 os_vm_address_t
208 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
209 {
210     addr = mmap(addr, len,
211                 OS_VM_PROT_ALL,
212                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
213                 fd, (off_t) offset);
214
215     if(addr == MAP_FAILED) {
216         perror("mmap");
217         lose("unexpected mmap(..) failure");
218     }
219
220     return addr;
221 }
222
223 void
224 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
225 {
226 }
227
228 void
229 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
230 {
231     if (mprotect(address, length, prot) == -1) {
232         perror("mprotect");
233     }
234 }
235 \f
236 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
237  * description of a space, we could probably punt this and just do
238  * (FOO_START <= x && x < FOO_END) everywhere it's called. */
239 static boolean
240 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
241 {
242     char* beg = (char*)sbeg;
243     char* end = (char*)sbeg + slen;
244     char* adr = (char*)a;
245     return (adr >= beg && adr < end);
246 }
247
248 boolean
249 is_valid_lisp_addr(os_vm_address_t addr)
250 {
251     return
252         in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
253         in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
254         in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE) ||
255         in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE) ||
256         in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE);
257 }
258 \f
259 /*
260  * any OS-dependent special low-level handling for signals
261  */
262
263 #if !defined GENCGC
264
265 void
266 os_install_interrupt_handlers(void)
267 {}
268
269 #else
270
271 /*
272  * The GENCGC needs to be hooked into whatever signal is raised for
273  * page fault on this OS.
274  */
275 void
276 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
277 {
278     os_context_t *context = (os_context_t*)void_context;
279     void* fault_addr = (void*)context->uc_mcontext.cr2;
280     if (!gencgc_handle_wp_violation(fault_addr)) {
281         interrupt_handle_now(signal, info, void_context);
282     }
283 }
284 void
285 os_install_interrupt_handlers(void)
286 {
287     interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
288 }
289
290 #endif