0.6.12.7.flaky1:
[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
33 #include <sys/types.h>
34 #include <signal.h>
35 /* #include <sys/sysinfo.h> */
36 #include <sys/proc.h>
37 #include "validate.h"
38 vm_size_t os_vm_page_size;
39
40 #if defined GENCGC
41 #include "gencgc.h"
42 #endif
43
44 /* The different BSD variants have diverged in exactly where they
45  * store signal context information, but at least they tend to use the
46  * same stems to name the structure fields, so by using this macro we
47  * can share a fair amount of code between different variants. */
48 #if defined __FreeBSD__
49 #define CONTEXT_ADDR_FROM_STEM(stem) &context->uc_mcontext.mc_ ## stem
50 #elif defined __OpenBSD__
51 #define CONTEXT_ADDR_FROM_STEM(stem) &context->sc_ ## stem
52 #else
53 #error unsupported BSD variant
54 #endif
55 \f
56 void
57 os_init(void)
58 {
59     os_vm_page_size = getpagesize();
60 }
61
62 /* KLUDGE: There is strong family resemblance in the signal context
63  * stuff in FreeBSD and OpenBSD, but in detail they're different in
64  * almost every line of code. It would be nice to find some way to
65  * factor out the commonality better; failing that, it might be best
66  * just to split this generic-BSD code into one variant for each BSD. */
67    
68 int *
69 os_context_register_addr(os_context_t *context, int offset)
70 {
71     switch(offset) {
72     case  0:
73         return CONTEXT_ADDR_FROM_STEM(eax);
74     case  2:
75         return CONTEXT_ADDR_FROM_STEM(ecx);
76     case  4:
77         return CONTEXT_ADDR_FROM_STEM(edx);
78     case  6:
79         return CONTEXT_ADDR_FROM_STEM(ebx);
80     case  8:
81         return CONTEXT_ADDR_FROM_STEM(esp);
82     case 10:
83         return CONTEXT_ADDR_FROM_STEM(ebp);
84     case 12:
85         return CONTEXT_ADDR_FROM_STEM(esi);
86     case 14:
87         return CONTEXT_ADDR_FROM_STEM(edi);
88     default:
89         return 0;
90     }
91 }
92
93 int *
94 os_context_pc_addr(os_context_t *context)
95 {
96 #if defined __FreeBSD__
97     return CONTEXT_ADDR_FROM_STEM(eip);
98 #elif defined __OpenBSD__
99     return CONTEXT_ADDR_FROM_STEM(pc);
100 #else
101 #error unsupported BSD variant
102 #endif
103 }
104
105 int *
106 os_context_sp_addr(os_context_t *context)
107 {
108     return CONTEXT_ADDR_FROM_STEM(esp);
109 }
110
111 sigset_t *
112 os_context_sigmask_addr(os_context_t *context)
113 {
114     /* (Unlike most of the other context fields that we access, the
115      * signal mask field is a field of the basic, outermost context
116      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
117 #if defined __FreeBSD__
118     return &context->uc_sigmask;
119 #elif defined __OpenBSD__
120     return &context->sc_mask;
121 #else
122 #error unsupported BSD variant
123 #endif
124 }
125
126 os_vm_address_t
127 os_validate(os_vm_address_t addr, os_vm_size_t len)
128 {
129     int flags = MAP_PRIVATE | MAP_ANON;
130
131     if (addr)
132         flags |= MAP_FIXED;
133
134     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
135
136     if (addr == MAP_FAILED) {
137         perror("mmap");
138         return NULL;
139     }
140
141     return addr;
142 }
143
144 void
145 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
146 {
147     if (munmap(addr, len) == -1)
148         perror("munmap");
149 }
150
151 os_vm_address_t
152 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
153 {
154     addr = mmap(addr, len,
155                 OS_VM_PROT_ALL,
156                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
157                 fd, (off_t) offset);
158
159     if (addr == MAP_FAILED) {
160         perror("mmap");
161         lose("unexpected mmap(..) failure");
162     }
163
164     return addr;
165 }
166
167 void
168 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
169 {
170 }
171
172 void
173 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
174 {
175     if (mprotect(address, length, prot) == -1) {
176         perror("mprotect");
177     }
178 }
179 \f
180 static boolean
181 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
182 {
183     char* beg = (char*) sbeg;
184     char* end = (char*) sbeg + slen;
185     char* adr = (char*) a;
186     return (adr >= beg && adr < end);
187 }
188
189 boolean
190 is_valid_lisp_addr(os_vm_address_t addr)
191 {
192     return in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
193         || in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE   )
194         || in_range_p(addr, DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE  )
195         || in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE  )
196         || in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE  );
197 }
198 \f
199 /*
200  * any OS-dependent special low-level handling for signals
201  */
202
203 #if !defined GENCGC
204
205 void
206 os_install_interrupt_handlers(void)
207 {
208     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
209 }
210
211 #else
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     if (!gencgc_handle_wp_violation(fault_addr)) {
230         interrupt_handle_now(signal, siginfo, void_context);
231     }
232 }
233 void
234 os_install_interrupt_handlers(void)
235 {
236     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
237     SHOW("**1"); /* REMOVEME */
238 #if defined __FreeBSD__
239     SHOW("**2"); /* REMOVEME */
240     SHOW("__FreeBSD__ case");
241     interrupt_install_low_level_handler(SIGBUS, memory_fault_handler);
242 #elif defined __OpenBSD__
243     SHOW("**3"); /* REMOVEME */
244     FSHOW((stderr, "/__OpenBSD__ case, SIGSEGV=%d\n", SIGSEGV));
245     interrupt_install_low_level_handler(SIGSEGV, memory_fault_handler);
246 #else
247 #error unsupported BSD variant
248 #endif
249     SHOW("**4"); /* REMOVEME */
250     SHOW("leaving os_install_interrupt_handlers()");
251 }
252
253 #endif /* !defined GENCGC */