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"
33 #include <sys/types.h>
35 /* #include <sys/sysinfo.h> */
38 vm_size_t os_vm_page_size;
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
53 #error unsupported BSD variant
59 os_vm_page_size = getpagesize();
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. */
69 os_context_register_addr(os_context_t *context, int offset)
73 return CONTEXT_ADDR_FROM_STEM(eax);
75 return CONTEXT_ADDR_FROM_STEM(ecx);
77 return CONTEXT_ADDR_FROM_STEM(edx);
79 return CONTEXT_ADDR_FROM_STEM(ebx);
81 return CONTEXT_ADDR_FROM_STEM(esp);
83 return CONTEXT_ADDR_FROM_STEM(ebp);
85 return CONTEXT_ADDR_FROM_STEM(esi);
87 return CONTEXT_ADDR_FROM_STEM(edi);
94 os_context_pc_addr(os_context_t *context)
96 #if defined __FreeBSD__
97 return CONTEXT_ADDR_FROM_STEM(eip);
98 #elif defined __OpenBSD__
99 return CONTEXT_ADDR_FROM_STEM(pc);
101 #error unsupported BSD variant
106 os_context_sp_addr(os_context_t *context)
108 return CONTEXT_ADDR_FROM_STEM(esp);
112 os_context_sigmask_addr(os_context_t *context)
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;
122 #error unsupported BSD variant
127 os_validate(os_vm_address_t addr, os_vm_size_t len)
129 int flags = MAP_PRIVATE | MAP_ANON;
134 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
136 if (addr == MAP_FAILED) {
145 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
147 if (munmap(addr, len) == -1)
152 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
154 addr = mmap(addr, len,
156 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
159 if (addr == MAP_FAILED) {
161 lose("unexpected mmap(..) failure");
167 /* FIXME: If this can be a no-op on BSD/x86, then it
168 * deserves a more precise name.
170 * (Perhaps os_prepare_data_area_to_be_executed()?) */
172 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
177 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
179 if (mprotect(address, length, prot) == -1) {
185 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
187 char* beg = (char*) sbeg;
188 char* end = (char*) sbeg + slen;
189 char* adr = (char*) a;
190 return (adr >= beg && adr < end);
194 is_valid_lisp_addr(os_vm_address_t addr)
196 return in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
197 || in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE )
198 || in_range_p(addr, DYNAMIC_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 );
204 * any OS-dependent special low-level handling for signals
210 os_install_interrupt_handlers(void)
212 SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
218 * The GENCGC needs to be hooked into whatever signal is raised for
219 * page fault on this OS.
222 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
224 /* The way that we extract low level information like the fault
225 * address is not specified by POSIX. */
226 #if defined __FreeBSD__
227 void *fault_addr = siginfo->si_addr;
228 #elif defined __OpenBSD__
229 void *fault_addr = siginfo->si_addr;
231 #error unsupported BSD variant
233 if (!gencgc_handle_wp_violation(fault_addr)) {
234 interrupt_handle_now(signal, siginfo, void_context);
238 os_install_interrupt_handlers(void)
240 SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
241 #if defined __FreeBSD__
242 undoably_install_low_level_interrupt_handler(SIGBUS,
243 memory_fault_handler);
244 #elif defined __OpenBSD__
245 undoably_install_low_level_interrupt_handler(SIGSEGV,
246 memory_fault_handler);
248 #error unsupported BSD variant
250 SHOW("leaving os_install_interrupt_handlers()");
253 #endif /* !defined GENCGC */