0.9.2.42:
[sbcl/lichteblau.git] / src / runtime / bsd-os.c
blob3199a86ab269cb0ea5be9a3e2a8683a8aa598bfe
1 /*
2 * OS-dependent routines for BSD-ish systems
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 */
11 * This software is part of the SBCL system. See the README file for
12 * more information.
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.
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"
39 #include <sys/types.h>
40 #include <signal.h>
41 /* #include <sys/sysinfo.h> */
42 #include "validate.h"
44 os_vm_size_t os_vm_page_size;
46 #ifdef __NetBSD__
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <string.h>
51 static void netbsd_init();
52 #endif /* __NetBSD__ */
54 void os_init(void)
56 os_vm_page_size = getpagesize();
58 #ifdef __NetBSD__
59 netbsd_init();
60 #endif /* __NetBSD__ */
63 int *os_context_pc_addr(os_context_t *context)
65 #if defined __FreeBSD__
66 return CONTEXT_ADDR_FROM_STEM(eip);
67 #elif defined __OpenBSD__
68 return CONTEXT_ADDR_FROM_STEM(pc);
69 #elif defined __NetBSD__
70 return CONTEXT_ADDR_FROM_STEM(EIP);
71 #elif defined LISP_FEATURE_DARWIN
72 return &context->uc_mcontext->ss.srr0;
73 #else
74 #error unsupported BSD variant
75 #endif
78 sigset_t *
79 os_context_sigmask_addr(os_context_t *context)
81 /* (Unlike most of the other context fields that we access, the
82 * signal mask field is a field of the basic, outermost context
83 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
84 #if defined __FreeBSD__ || __NetBSD__ || defined LISP_FEATURE_DARWIN
85 return &context->uc_sigmask;
86 #elif defined __OpenBSD__
87 return &context->sc_mask;
88 #else
89 #error unsupported BSD variant
90 #endif
93 os_vm_address_t
94 os_validate(os_vm_address_t addr, os_vm_size_t len)
96 int flags = MAP_PRIVATE | MAP_ANON;
98 if (addr)
99 flags |= MAP_FIXED;
101 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
103 if (addr == MAP_FAILED) {
104 perror("mmap");
105 return NULL;
108 return addr;
111 void
112 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
114 if (munmap(addr, len) == -1)
115 perror("munmap");
118 os_vm_address_t
119 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
121 addr = mmap(addr, len,
122 OS_VM_PROT_ALL,
123 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
124 fd, (off_t) offset);
126 if (addr == MAP_FAILED) {
127 perror("mmap");
128 lose("unexpected mmap(..) failure");
131 return addr;
134 void
135 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
137 if (mprotect(address, length, prot) == -1) {
138 perror("mprotect");
142 static boolean
143 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
145 char* beg = (char*) sbeg;
146 char* end = (char*) sbeg + slen;
147 char* adr = (char*) a;
148 return (adr >= beg && adr < end);
151 boolean
152 is_valid_lisp_addr(os_vm_address_t addr)
154 struct thread *th;
155 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
156 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
157 in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE))
158 return 1;
159 for_each_thread(th) {
160 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
161 return 1;
162 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
163 return 1;
165 return 0;
169 * any OS-dependent special low-level handling for signals
172 #if defined LISP_FEATURE_GENCGC
175 * The GENCGC needs to be hooked into whatever signal is raised for
176 * page fault on this OS.
178 static void
179 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
181 /* The way that we extract low level information like the fault
182 * address is not specified by POSIX. */
183 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
184 void *fault_addr = siginfo->si_addr;
185 #elif defined LISP_FEATURE_DARWIN
186 void *fault_addr = siginfo->si_addr;
187 #else
188 #error unsupported BSD variant
189 #endif
191 os_context_t *context = arch_os_get_context(&void_context);
192 if (!gencgc_handle_wp_violation(fault_addr))
193 if(!handle_guard_page_triggered(context,fault_addr))
194 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
195 arrange_return_to_lisp_function(context, SymbolFunction(MEMORY_FAULT_ERROR));
196 #else
197 interrupt_handle_now(signal, siginfo, context);
198 #endif
200 void
201 os_install_interrupt_handlers(void)
203 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
204 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
205 memory_fault_handler);
206 SHOW("leaving os_install_interrupt_handlers()");
209 #else /* Currently Darwin only */
211 static void
212 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
214 os_context_t *context = arch_os_get_context(&void_context);
215 unsigned int pc = (unsigned int *)(*os_context_pc_addr(context));
216 os_vm_address_t addr;
218 addr = arch_get_bad_addr(signal,info,context);
219 if(!interrupt_maybe_gc(signal, info, context))
220 if(!handle_guard_page_triggered(context,addr))
221 interrupt_handle_now(signal, info, context);
222 /* Work around G5 bug; fix courtesy gbyers */
223 DARWIN_FIX_CONTEXT(context);
226 void
227 os_install_interrupt_handlers(void)
229 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
230 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
231 sigsegv_handler);
234 #endif /* defined GENCGC */
236 #ifdef __NetBSD__
237 static void netbsd_init()
239 struct rlimit rl;
240 int mib[2], osrev;
241 size_t len;
243 /* Are we running on a sufficiently functional kernel? */
244 mib[0] = CTL_KERN;
245 mib[1] = KERN_OSREV;
247 len = sizeof(osrev);
248 sysctl(mib, 2, &osrev, &len, NULL, 0);
250 /* If we're older than 2.0... */
251 if (osrev < 200000000) {
252 fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
253 lose("NetBSD kernel too old to run sbcl.\n");
256 /* NetBSD counts mmap()ed space against the process's data size limit,
257 * so yank it up. This might be a nasty thing to do? */
258 getrlimit (RLIMIT_DATA, &rl);
259 /* Amazingly for such a new port, the provenance and meaning of
260 this number are unknown. It might just mean REALLY_BIG_LIMIT,
261 or possibly it should be calculated from dynamic space size.
262 -- CSR, 2004-04-08 */
263 rl.rlim_cur = 1073741824;
264 if (setrlimit (RLIMIT_DATA, &rl) < 0) {
265 fprintf (stderr,
266 "RUNTIME WARNING: unable to raise process data size limit:\n\
267 %s.\n\
268 The system may fail to start.\n",
269 strerror(errno));
272 #endif /* __NetBSD__ */
274 /* threads */
276 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
277 #ifdef LISP_FEATURE_SB_THREAD
278 #error "Define threading support functions"
279 #else
280 int arch_os_thread_init(struct thread *thread) {
281 stack_t sigstack;
282 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
283 /* Signal handlers are run on the control stack, so if it is exhausted
284 * we had better use an alternate stack for whatever signal tells us
285 * we've exhausted it */
286 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
287 sigstack.ss_flags=0;
288 sigstack.ss_size = 32*SIGSTKSZ;
289 sigaltstack(&sigstack,0);
290 #endif
291 return 1; /* success */
293 int arch_os_thread_cleanup(struct thread *thread) {
294 return 1; /* success */
296 #endif