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