0.9.7.12:
[sbcl.git] / src / runtime / bsd-os.c
1 /*
2  * OS-dependent routines for BSD-ish systems
3  *
4  * This file (along with os.h) exports an OS-independent interface to
5  * the operating system VM facilities. This interface looks a lot like
6  * the Mach interface (but simpler in some places). For some operating
7  * systems, a subset of these functions will have to be emulated.
8  */
9
10 /*
11  * This software is part of the SBCL system. See the README file for
12  * more information.
13  *
14  * This software is derived from the CMU CL system, which was
15  * written at Carnegie Mellon University and released into the
16  * public domain. The software is in the public domain and is
17  * provided with absolutely no warranty. See the COPYING and CREDITS
18  * files for more information.
19  */
20
21 #include <stdio.h>
22 #include <sys/param.h>
23 #include <sys/file.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include "sbcl.h"
27 #include "./signal.h"
28 #include "os.h"
29 #include "arch.h"
30 #include "globals.h"
31 #include "interrupt.h"
32 #include "interr.h"
33 #include "lispregs.h"
34 #include "thread.h"
35 #include "runtime.h"
36 #include "genesis/static-symbols.h"
37 #include "genesis/fdefn.h"
38
39 #include <sys/types.h>
40 #include <signal.h>
41 /* #include <sys/sysinfo.h> */
42 #include "validate.h"
43 \f
44 os_vm_size_t os_vm_page_size;
45
46 #ifdef __NetBSD__
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <string.h>
50
51 static void netbsd_init();
52 #endif /* __NetBSD__ */
53
54 void
55 os_init(char *argv[], char *envp[])
56 {
57     os_vm_page_size = getpagesize();
58
59 #ifdef __NetBSD__
60     netbsd_init();
61 #endif /* __NetBSD__ */
62 }
63
64 int *os_context_pc_addr(os_context_t *context)
65 {
66 #if defined __FreeBSD__
67     return CONTEXT_ADDR_FROM_STEM(eip);
68 #elif defined __OpenBSD__
69     return CONTEXT_ADDR_FROM_STEM(pc);
70 #elif defined __NetBSD__
71     return CONTEXT_ADDR_FROM_STEM(EIP);
72 #elif defined LISP_FEATURE_DARWIN
73     return &context->uc_mcontext->ss.srr0;
74 #else
75 #error unsupported BSD variant
76 #endif
77 }
78
79 sigset_t *
80 os_context_sigmask_addr(os_context_t *context)
81 {
82     /* (Unlike most of the other context fields that we access, the
83      * signal mask field is a field of the basic, outermost context
84      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
85 #if defined __FreeBSD__  || __NetBSD__ || defined LISP_FEATURE_DARWIN
86     return &context->uc_sigmask;
87 #elif defined __OpenBSD__
88     return &context->sc_mask;
89 #else
90 #error unsupported BSD variant
91 #endif
92 }
93
94 os_vm_address_t
95 os_validate(os_vm_address_t addr, os_vm_size_t len)
96 {
97     int flags = MAP_PRIVATE | MAP_ANON;
98
99     if (addr)
100         flags |= MAP_FIXED;
101
102     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
103
104     if (addr == MAP_FAILED) {
105         perror("mmap");
106         return NULL;
107     }
108
109     return addr;
110 }
111
112 void
113 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
114 {
115     if (munmap(addr, len) == -1)
116         perror("munmap");
117 }
118
119 os_vm_address_t
120 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
121 {
122     addr = mmap(addr, len,
123                 OS_VM_PROT_ALL,
124                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
125                 fd, (off_t) offset);
126
127     if (addr == MAP_FAILED) {
128         perror("mmap");
129         lose("unexpected mmap(..) failure\n");
130     }
131
132     return addr;
133 }
134
135 void
136 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
137 {
138     if (mprotect(address, length, prot) == -1) {
139         perror("mprotect");
140     }
141 }
142 \f
143 static boolean
144 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
145 {
146     char* beg = (char*) sbeg;
147     char* end = (char*) sbeg + slen;
148     char* adr = (char*) a;
149     return (adr >= beg && adr < end);
150 }
151
152 boolean
153 is_valid_lisp_addr(os_vm_address_t addr)
154 {
155     struct thread *th;
156     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
157        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
158        in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE))
159         return 1;
160     for_each_thread(th) {
161         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
162             return 1;
163         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
164             return 1;
165     }
166     return 0;
167 }
168 \f
169 /*
170  * any OS-dependent special low-level handling for signals
171  */
172
173 #if defined LISP_FEATURE_GENCGC
174
175 /*
176  * The GENCGC needs to be hooked into whatever signal is raised for
177  * page fault on this OS.
178  */
179 static void
180 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
181 {
182     /* The way that we extract low level information like the fault
183      * address is not specified by POSIX. */
184 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
185     void *fault_addr = siginfo->si_addr;
186 #elif defined LISP_FEATURE_DARWIN
187     void *fault_addr = siginfo->si_addr;
188 #else
189 #error unsupported BSD variant
190 #endif
191
192     os_context_t *context = arch_os_get_context(&void_context);
193     if (!gencgc_handle_wp_violation(fault_addr))
194         if(!handle_guard_page_triggered(context,fault_addr))
195 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
196             arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
197 #else
198             interrupt_handle_now(signal, siginfo, context);
199 #endif
200 }
201 void
202 os_install_interrupt_handlers(void)
203 {
204     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
205     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
206                                                  memory_fault_handler);
207     SHOW("leaving os_install_interrupt_handlers()");
208 }
209
210 #else /* Currently Darwin only */
211
212 static void
213 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
214 {
215     os_context_t *context = arch_os_get_context(&void_context);
216     unsigned int pc =  (unsigned int *)(*os_context_pc_addr(context));
217     os_vm_address_t addr;
218
219     addr = arch_get_bad_addr(signal,info,context);
220     if(!interrupt_maybe_gc(signal, info, context))
221         if(!handle_guard_page_triggered(context,addr))
222             interrupt_handle_now(signal, info, context);
223     /* Work around G5 bug; fix courtesy gbyers */
224     DARWIN_FIX_CONTEXT(context);
225 }
226
227 void
228 os_install_interrupt_handlers(void)
229 {
230     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
231     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
232                                                  sigsegv_handler);
233 }
234
235 #endif /* defined GENCGC */
236
237 #ifdef __NetBSD__
238 static void netbsd_init()
239 {
240     struct rlimit rl;
241     int mib[2], osrev;
242     size_t len;
243
244     /* Are we running on a sufficiently functional kernel? */
245     mib[0] = CTL_KERN;
246     mib[1] = KERN_OSREV;
247
248     len = sizeof(osrev);
249     sysctl(mib, 2, &osrev, &len, NULL, 0);
250
251     /* If we're older than 2.0... */
252     if (osrev < 200000000) {
253         fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
254         lose("NetBSD kernel too old to run sbcl.\n");
255     }
256
257     /* NetBSD counts mmap()ed space against the process's data size limit,
258      * so yank it up. This might be a nasty thing to do? */
259     getrlimit (RLIMIT_DATA, &rl);
260     /* Amazingly for such a new port, the provenance and meaning of
261        this number are unknown.  It might just mean REALLY_BIG_LIMIT,
262        or possibly it should be calculated from dynamic space size.
263        -- CSR, 2004-04-08 */
264     rl.rlim_cur = 1073741824;
265     if (setrlimit (RLIMIT_DATA, &rl) < 0) {
266         fprintf (stderr,
267                  "RUNTIME WARNING: unable to raise process data size limit:\n\
268   %s.\n\
269 The system may fail to start.\n",
270                  strerror(errno));
271     }
272 }
273 #endif /* __NetBSD__ */
274 \f
275 /* threads */
276
277 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
278 #ifdef LISP_FEATURE_SB_THREAD
279 #error "Define threading support functions"
280 #else
281 int arch_os_thread_init(struct thread *thread) {
282   stack_t sigstack;
283 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
284     /* Signal handlers are run on the control stack, so if it is exhausted
285      * we had better use an alternate stack for whatever signal tells us
286      * we've exhausted it */
287     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
288     sigstack.ss_flags=0;
289     sigstack.ss_size = 32*SIGSTKSZ;
290     sigaltstack(&sigstack,0);
291 #endif
292     return 1;                  /* success */
293 }
294 int arch_os_thread_cleanup(struct thread *thread) {
295     return 1;                  /* success */
296 }
297 #endif