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