0.8.9.6.netbsd.2:
[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
36 #include <sys/types.h>
37 #include <signal.h>
38 /* #include <sys/sysinfo.h> */
39 #include "validate.h"
40
41 \f
42 os_vm_size_t os_vm_page_size;
43
44 #ifdef __NetBSD__
45 #include <sys/resource.h>
46 #include <string.h>
47
48 static void netbsd_init();
49 #endif /* __NetBSD__ */
50  
51 void os_init(void)
52 {
53     os_vm_page_size = getpagesize();
54
55 #ifdef __NetBSD__
56     netbsd_init();
57 #endif /* __NetBSD__ */
58 }
59
60 int *os_context_pc_addr(os_context_t *context)
61 {
62 #if defined __FreeBSD__
63     return CONTEXT_ADDR_FROM_STEM(eip);
64 #elif defined __OpenBSD__
65     return CONTEXT_ADDR_FROM_STEM(pc);
66 #elif defined __NetBSD__
67     return CONTEXT_ADDR_FROM_STEM(EIP);
68 #elif defined LISP_FEATURE_DARWIN
69     return &context->uc_mcontext->ss.srr0;
70 #else
71 #error unsupported BSD variant
72 #endif
73 }
74
75 sigset_t *
76 os_context_sigmask_addr(os_context_t *context)
77 {
78     /* (Unlike most of the other context fields that we access, the
79      * signal mask field is a field of the basic, outermost context
80      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
81 #if defined __FreeBSD__  || __NetBSD__ || defined LISP_FEATURE_DARWIN
82     return &context->uc_sigmask;
83 #elif defined __OpenBSD__
84     return &context->sc_mask;
85 #else
86 #error unsupported BSD variant
87 #endif
88 }
89
90 os_vm_address_t
91 os_validate(os_vm_address_t addr, os_vm_size_t len)
92 {
93     int flags = MAP_PRIVATE | MAP_ANON;
94
95     if (addr)
96         flags |= MAP_FIXED;
97
98     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
99
100     if (addr == MAP_FAILED) {
101         perror("mmap");
102         return NULL;
103     }
104
105     return addr;
106 }
107
108 void
109 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
110 {
111     if (munmap(addr, len) == -1)
112         perror("munmap");
113 }
114
115 os_vm_address_t
116 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
117 {
118     addr = mmap(addr, len,
119                 OS_VM_PROT_ALL,
120                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
121                 fd, (off_t) offset);
122
123     if (addr == MAP_FAILED) {
124         perror("mmap");
125         lose("unexpected mmap(..) failure");
126     }
127
128     return addr;
129 }
130
131 void
132 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
133 {
134     if (mprotect(address, length, prot) == -1) {
135         perror("mprotect");
136     }
137 }
138 \f
139 static boolean
140 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
141 {
142     char* beg = (char*) sbeg;
143     char* end = (char*) sbeg + slen;
144     char* adr = (char*) a;
145     return (adr >= beg && adr < end);
146 }
147
148 boolean
149 is_valid_lisp_addr(os_vm_address_t addr)
150 {
151     struct thread *th;
152     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
153        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
154        in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE))
155         return 1;
156     for_each_thread(th) {
157         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
158             return 1;
159         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
160             return 1;
161     }
162     return 0;
163 }
164 \f
165 /*
166  * any OS-dependent special low-level handling for signals
167  */
168
169 #if defined LISP_FEATURE_GENCGC
170
171 /*
172  * The GENCGC needs to be hooked into whatever signal is raised for
173  * page fault on this OS.
174  */
175 static void
176 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
177 {
178     /* The way that we extract low level information like the fault
179      * address is not specified by POSIX. */
180 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
181     void *fault_addr = siginfo->si_addr;
182 #elif defined LISP_FEATURE_DARWIN
183     void *fault_addr = siginfo->si_addr;
184 #else
185 #error unsupported BSD variant
186 #endif
187
188     os_context_t *context = arch_os_get_context(&void_context);
189     if (!gencgc_handle_wp_violation(fault_addr)) 
190         if(!handle_control_stack_guard_triggered(context,fault_addr))
191             /* FIXME is this context or void_context?  not that it */
192             /* makes a difference currently except on linux/sparc */
193             interrupt_handle_now(signal, siginfo, void_context);
194 }
195 void
196 os_install_interrupt_handlers(void)
197 {
198     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
199     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
200                                                  memory_fault_handler);
201     SHOW("leaving os_install_interrupt_handlers()");
202 }
203
204 #else /* Currently Darwin only */
205
206 static void
207 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
208 {
209     os_context_t *context = arch_os_get_context(&void_context);
210     unsigned int pc =  (unsigned int *)(*os_context_pc_addr(context));
211     os_vm_address_t addr;
212     
213     addr = arch_get_bad_addr(signal,info,context);
214     if(!interrupt_maybe_gc(signal, info, context))
215         if(!handle_control_stack_guard_triggered(context,addr))
216             interrupt_handle_now(signal, info, context);
217     /* Work around G5 bug; fix courtesy gbyers */
218     sigreturn(void_context);
219 }
220
221 void
222 os_install_interrupt_handlers(void)
223 {
224     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
225     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
226                                                  sigsegv_handler);
227 }
228
229 #endif /* defined GENCGC */
230
231 #ifdef __NetBSD__
232 static void netbsd_init()
233 {
234         struct rlimit rl;
235
236         /* NetBSD counts mmap()ed space against the process's data size limit,
237          * so yank it up. This might be a nasty thing to do? */
238         getrlimit (RLIMIT_DATA, &rl);
239         rl.rlim_cur = 1073741824;
240         if (setrlimit (RLIMIT_DATA, &rl) < 0) {
241                 fprintf (stderr, "RUNTIME WARNING: unable to raise process data size limit to 1GB (%s). The system may fail to start.\n",
242                         strerror(errno));
243         }
244 }
245 #endif /* __NetBSD__ */
246 \f
247 /* threads */
248
249 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
250 #ifdef LISP_FEATURE_SB_THREAD
251 #error "Define threading support functions"
252 #else
253 int arch_os_thread_init(struct thread *thread) {
254   stack_t sigstack;
255 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
256     /* Signal handlers are run on the control stack, so if it is exhausted
257      * we had better use an alternate stack for whatever signal tells us
258      * we've exhausted it */
259     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
260     sigstack.ss_flags=0;
261     sigstack.ss_size = 32*SIGSTKSZ;
262     sigaltstack(&sigstack,0);
263 #endif
264     return 1;                  /* success */
265 }
266 int arch_os_thread_cleanup(struct thread *thread) {
267     return 1;                  /* success */
268 }
269 #endif