64aba2cd0a569f71d59b3fb0113af6780f9162f6
[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 vm_size_t os_vm_page_size;
39
40
41 /* The different BSD variants have diverged in exactly where they
42  * store signal context information, but at least they tend to use the
43  * same stems to name the structure fields, so by using this macro we
44  * can share a fair amount of code between different variants. */
45 #if defined __FreeBSD__
46 #define CONTEXT_ADDR_FROM_STEM(stem) &context->uc_mcontext.mc_ ## stem
47 #elif defined __OpenBSD__
48 #define CONTEXT_ADDR_FROM_STEM(stem) &context->sc_ ## stem
49 #else
50 #error unsupported BSD variant
51 #endif
52 \f
53 void
54 os_init(void)
55 {
56     os_vm_page_size = getpagesize();
57 }
58
59 /* KLUDGE: There is strong family resemblance in the signal context
60  * stuff in FreeBSD and OpenBSD, but in detail they're different in
61  * almost every line of code. It would be nice to find some way to
62  * factor out the commonality better; failing that, it might be best
63  * just to split this generic-BSD code into one variant for each BSD. */
64    
65 int *
66 os_context_register_addr(os_context_t *context, int offset)
67 {
68     switch(offset) {
69     case  0:
70         return CONTEXT_ADDR_FROM_STEM(eax);
71     case  2:
72         return CONTEXT_ADDR_FROM_STEM(ecx);
73     case  4:
74         return CONTEXT_ADDR_FROM_STEM(edx);
75     case  6:
76         return CONTEXT_ADDR_FROM_STEM(ebx);
77     case  8:
78         return CONTEXT_ADDR_FROM_STEM(esp);
79     case 10:
80         return CONTEXT_ADDR_FROM_STEM(ebp);
81     case 12:
82         return CONTEXT_ADDR_FROM_STEM(esi);
83     case 14:
84         return CONTEXT_ADDR_FROM_STEM(edi);
85     default:
86         return 0;
87     }
88 }
89
90 int *
91 os_context_pc_addr(os_context_t *context)
92 {
93 #if defined __FreeBSD__
94     return CONTEXT_ADDR_FROM_STEM(eip);
95 #elif defined __OpenBSD__
96     return CONTEXT_ADDR_FROM_STEM(pc);
97 #else
98 #error unsupported BSD variant
99 #endif
100 }
101
102 int *
103 os_context_sp_addr(os_context_t *context)
104 {
105     return CONTEXT_ADDR_FROM_STEM(esp);
106 }
107
108 sigset_t *
109 os_context_sigmask_addr(os_context_t *context)
110 {
111     /* (Unlike most of the other context fields that we access, the
112      * signal mask field is a field of the basic, outermost context
113      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
114 #if defined __FreeBSD__
115     return &context->uc_sigmask;
116 #elif defined __OpenBSD__
117     return &context->sc_mask;
118 #else
119 #error unsupported BSD variant
120 #endif
121 }
122
123 os_vm_address_t
124 os_validate(os_vm_address_t addr, os_vm_size_t len)
125 {
126     int flags = MAP_PRIVATE | MAP_ANON;
127
128     if (addr)
129         flags |= MAP_FIXED;
130
131     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
132
133     if (addr == MAP_FAILED) {
134         perror("mmap");
135         return NULL;
136     }
137
138     return addr;
139 }
140
141 void
142 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
143 {
144     if (munmap(addr, len) == -1)
145         perror("munmap");
146 }
147
148 os_vm_address_t
149 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
150 {
151     addr = mmap(addr, len,
152                 OS_VM_PROT_ALL,
153                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
154                 fd, (off_t) offset);
155
156     if (addr == MAP_FAILED) {
157         perror("mmap");
158         lose("unexpected mmap(..) failure");
159     }
160
161     return addr;
162 }
163
164 /* FIXME: If this can be a no-op on BSD/x86, then it 
165  * deserves a more precise name.
166  *
167  * (Perhaps os_prepare_data_area_to_be_executed()?) */
168 void
169 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
170 {
171 }
172
173 void
174 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
175 {
176     if (mprotect(address, length, prot) == -1) {
177         perror("mprotect");
178     }
179 }
180 \f
181 static boolean
182 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
183 {
184     char* beg = (char*) sbeg;
185     char* end = (char*) sbeg + slen;
186     char* adr = (char*) a;
187     return (adr >= beg && adr < end);
188 }
189
190 boolean
191 is_valid_lisp_addr(os_vm_address_t addr)
192 {
193     struct thread *th;
194     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
195        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
196        in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE))
197         return 1;
198     for_each_thread(th) {
199         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
200             return 1;
201         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
202             return 1;
203     }
204     return 0;
205 }
206 \f
207 /*
208  * any OS-dependent special low-level handling for signals
209  */
210
211 #if defined LISP_FEATURE_GENCGC
212
213 /*
214  * The GENCGC needs to be hooked into whatever signal is raised for
215  * page fault on this OS.
216  */
217 static void
218 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
219 {
220     /* The way that we extract low level information like the fault
221      * address is not specified by POSIX. */
222 #if defined __FreeBSD__
223     void *fault_addr = siginfo->si_addr;
224 #elif defined __OpenBSD__
225     void *fault_addr = siginfo->si_addr;
226 #else
227 #error unsupported BSD variant
228 #endif
229     os_context_t *context = arch_os_get_context(&void_context);
230    if (!gencgc_handle_wp_violation(fault_addr)) 
231         if(!handle_control_stack_guard_triggered(context,fault_addr))
232             /* FIXME is this context or void_context?  not that it */
233             /* makes a difference currently except on linux/sparc */
234             interrupt_handle_now(signal, siginfo, void_context);
235 }
236 void
237 os_install_interrupt_handlers(void)
238 {
239     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
240     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
241                                                  memory_fault_handler);
242     SHOW("leaving os_install_interrupt_handlers()");
243 }
244
245 #else
246 /* As of 2002.07.31, this configuration has never been tested */
247 void
248 os_install_interrupt_handlers(void)
249 {
250     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
251 }
252
253 #endif /* defined GENCGC */
254 \f
255 /* threads */
256
257 /* no threading in any *BSD variant on any CPU (yet? in sbcl-0.8.0 anyway) */
258 #ifdef LISP_FEATURE_SB_THREAD
259 #error "Define threading support functions"
260 #else
261 struct thread *arch_os_get_current_thread() {
262     return all_threads;
263 }
264 int arch_os_thread_init(struct thread *thread) {
265   stack_t sigstack;
266 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
267     /* Signal handlers are run on the control stack, so if it is exhausted
268      * we had better use an alternate stack for whatever signal tells us
269      * we've exhausted it */
270     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
271     sigstack.ss_flags=0;
272     sigstack.ss_size = 32*SIGSTKSZ;
273     sigaltstack(&sigstack,0);
274 #endif
275     return 1;                  /* success */
276 }
277 int arch_os_thread_cleanup(struct thread *thread) {
278     return 1;                  /* success */
279 }
280 #endif