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