2 * OS-dependent routines for BSD-ish systems
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.
11 * This software is part of the SBCL system. See the README file for
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.
22 #include <sys/param.h>
28 #include "interrupt.h"
32 #include <sys/types.h>
34 /* #include <sys/sysinfo.h> */
37 vm_size_t os_vm_page_size;
43 /* The different BSD variants have diverged in exactly where they
44 * store signal context information, but at least they tend to use the
45 * same stems to name the structure fields, so by using this macro we
46 * can share a fair amount of code between different variants. */
47 #if defined __FreeBSD__
48 #define CONTEXT_ADDR_FROM_STEM(stem) &context->uc_mcontext.mc_ ## stem
49 #elif defined __OpenBSD__
50 #define CONTEXT_ADDR_FROM_STEM(stem) &context->sc_ ## stem
52 #error unsupported BSD variant
58 os_vm_page_size = getpagesize();
61 /* KLUDGE: There is strong family resemblance in the signal context
62 * stuff in FreeBSD and OpenBSD, but in detail they're different in
63 * almost every line of code. It would be nice to find some way to
64 * factor out the commonality better; failing that, it might be best
65 * just to split this generic-BSD code into one variant for each BSD. */
68 os_context_register_addr(os_context_t *context, int offset)
72 return CONTEXT_ADDR_FROM_STEM(eax);
74 return CONTEXT_ADDR_FROM_STEM(ecx);
76 return CONTEXT_ADDR_FROM_STEM(edx);
78 return CONTEXT_ADDR_FROM_STEM(ebx);
80 return CONTEXT_ADDR_FROM_STEM(esp);
82 return CONTEXT_ADDR_FROM_STEM(ebp);
84 return CONTEXT_ADDR_FROM_STEM(esi);
86 return CONTEXT_ADDR_FROM_STEM(edi);
93 os_context_pc_addr(os_context_t *context)
95 #if defined __FreeBSD__
96 return CONTEXT_ADDR_FROM_STEM(eip);
97 #elif defined __OpenBSD__
98 return CONTEXT_ADDR_FROM_STEM(pc);
100 #error unsupported BSD variant
105 os_context_sp_addr(os_context_t *context)
107 return CONTEXT_ADDR_FROM_STEM(esp);
111 os_context_sigmask_addr(os_context_t *context)
113 /* (Unlike most of the other context fields that we access, the
114 * signal mask field is a field of the basic, outermost context
115 * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
116 #if defined __FreeBSD__
117 return &context->uc_sigmask;
118 #elif defined __OpenBSD__
119 return &context->sc_mask;
121 #error unsupported BSD variant
126 os_validate(os_vm_address_t addr, os_vm_size_t len)
128 int flags = MAP_PRIVATE | MAP_ANON;
133 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
135 if (addr == MAP_FAILED) {
144 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
146 if (munmap(addr, len) == -1)
151 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
153 addr = mmap(addr, len,
155 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
158 if (addr == MAP_FAILED) {
160 lose("unexpected mmap(..) failure");
167 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
172 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
174 if (mprotect(address, length, prot) == -1) {
180 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
182 char* beg = (char*) sbeg;
183 char* end = (char*) sbeg + slen;
184 char* adr = (char*) a;
185 return (adr >= beg && adr < end);
189 is_valid_lisp_addr(os_vm_address_t addr)
191 return in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
192 || in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE )
193 || in_range_p(addr, DYNAMIC_SPACE_START , DYNAMIC_SPACE_SIZE )
194 || in_range_p(addr, CONTROL_STACK_START , CONTROL_STACK_SIZE )
195 || in_range_p(addr, BINDING_STACK_START , BINDING_STACK_SIZE );
199 * any OS-dependent special low-level handling for signals
205 os_install_interrupt_handlers(void)
211 * The GENCGC needs to be hooked into whatever signal is raised for
212 * page fault on this OS.
215 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
217 /* The way that we extract low level information like the fault
218 * address is not specified by POSIX. */
219 #if defined __FreeBSD__
220 void *fault_addr = siginfo->si_addr;
221 #elif defined __OpenBSD__
222 void *fault_addr = siginfo->si_addr;
224 #error unsupported BSD variant
226 if (!gencgc_handle_wp_violation(fault_addr)) {
227 interrupt_handle_now(signal, siginfo, void_context);
231 os_install_interrupt_handlers(void)
233 #if defined __FreeBSD__
234 interrupt_install_low_level_handler(SIGBUS, memory_fault_handler);
235 #elif defined __OpenBSD__
236 interrupt_install_low_level_handler(SIGSEGV, memory_fault_handler);
238 #error unsupported BSD variant
242 #endif /* !defined GENCGC */