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