9 #include <sys/utsname.h>
15 #include "interrupt.h"
18 #include "target-arch-os.h"
20 #ifdef LISP_FEATURE_X86
21 #include "genesis/static-symbols.h"
22 #include "genesis/fdefn.h"
25 #ifdef LISP_FEATURE_GENCGC
26 #include "gencgc-internal.h"
29 #if defined LISP_FEATURE_SPARC
30 #define OS_VM_DEFAULT_PAGESIZE 8192
31 #elif defined LISP_FEATURE_X86
32 #define OS_VM_DEFAULT_PAGESIZE 4096
34 #error "Don't know OS_VM_DEFAULT_PAGESIZE"
37 long os_vm_page_size=(-1);
38 static long os_real_page_size=(-1);
40 static os_vm_size_t real_page_size_difference=0;
42 /* So, this sucks. Versions of Solaris prior to 8 (SunOS releases
43 earlier than 5.8) do not support MAP_ANON passed as a flag to
44 mmap(). However, we would like SBCL compiled on SunOS 5.7 but
45 running on 5.8 to use MAP_ANON, but because of C's lack of
46 introspection at runtime, we can't grab the right value because
47 it's stuffed in a header file somewhere. We can, however, hardcode
48 it, and test at runtime for whether to use it... -- CSR, 2002-05-06
50 And, in fact, it sucks slightly more, as if you don't use MAP_ANON
51 you need to have /dev/zero open and pass the file descriptor to
52 mmap(). So overall, this counts as a KLUDGE. -- CSR, 2002-05-20 */
53 int KLUDGE_MAYBE_MAP_ANON = 0x0;
54 int kludge_mmap_fd = -1; /* default for MAP_ANON */
57 os_init(char *argv[], char *envp[])
64 major_version = atoi(name.release);
65 if (major_version != 5) {
66 lose("sunos major version=%d (which isn't 5!)", major_version);
68 minor_version = atoi(name.release+2);
69 if ((minor_version == 8) ||
70 (minor_version == 9) ||
71 (minor_version == 10)) {
72 KLUDGE_MAYBE_MAP_ANON = 0x100;
73 } else if (minor_version > 10) {
74 FSHOW((stderr, "os_init: Solaris version greater than 9?\nUnknown MAP_ANON behaviour.\n"));
75 lose("Unknown mmap() interaction with MAP_ANON");
76 } else { /* minor_version < 8 */
77 kludge_mmap_fd = open("/dev/zero",O_RDONLY);
78 if (kludge_mmap_fd < 0) {
80 lose("Error in open(..)");
84 /* I do not understand this at all. FIXME. */
85 os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
87 if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
88 fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
89 os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
93 * we do this because there are apparently dependencies on
94 * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
95 * but since the OS doesn't know we're using this restriction,
96 * we have to grovel around a bit to enforce it, thus anything
97 * that uses real_page_size_difference.
99 /* FIXME: Is this still true? */
100 real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
101 os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
105 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
107 int flags = MAP_PRIVATE | MAP_NORESERVE | KLUDGE_MAYBE_MAP_ANON;
111 addr = mmap(addr, len,
116 if (addr == MAP_FAILED) {
118 lose ("Error in mmap(..)");
124 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
126 if(munmap((void*) addr, len) == -1)
133 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
136 addr = mmap(addr, len,
138 MAP_PRIVATE | MAP_FIXED,
141 if (addr == MAP_FAILED) {
143 lose("Unexpedted mmap(..) failure");
150 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
152 if(mprotect((void*)address, length, prot) == -1) {
157 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
159 char* beg = (char*) sbeg;
160 char* end = (char*) sbeg + slen;
161 char* adr = (char*) a;
162 return (adr >= beg && adr < end);
165 boolean is_valid_lisp_addr(os_vm_address_t addr)
167 /* Old CMUCL comment:
169 Just assume address is valid if it lies within one of the known
170 spaces. (Unlike sunos-os which keeps track of every valid page.) */
172 /* FIXME: this looks like a valid definition for all targets with
173 cheney-gc; it may not be impressively smart (witness the
174 comment above) but maybe associating these functions with the
175 GC rather than the OS would be a maintainability win. -- CSR,
178 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
179 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
180 #ifdef LISP_FEATURE_GENCGC
181 in_range_p(addr, DYNAMIC_SPACE_START, DYNAMIC_SPACE_SIZE)
183 in_range_p(addr, DYNAMIC_0_SPACE_START, DYNAMIC_SPACE_SIZE) ||
184 in_range_p(addr, DYNAMIC_1_SPACE_START, DYNAMIC_SPACE_SIZE)
188 for_each_thread(th) {
189 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
191 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
198 #if defined LISP_FEATURE_GENCGC
201 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
203 os_context_t *context = arch_os_get_context(&void_context);
204 void* fault_addr = (void*)info->si_addr;
205 if(info->si_code == 1)
207 perror("error: SEGV_MAPERR\n");
211 if (!gencgc_handle_wp_violation(fault_addr))
212 if(!handle_guard_page_triggered(context, fault_addr))
213 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
214 arrange_return_to_lisp_function(context,
215 SymbolFunction(MEMORY_FAULT_ERROR));
217 interrupt_handle_now(signal, info, context);
224 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
226 os_context_t *context = arch_os_get_context(&void_context);
227 os_vm_address_t addr;
229 addr = arch_get_bad_addr(signal, info, context);
230 if(!interrupt_maybe_gc(signal, info, context)) {
231 if(!handle_guard_page_triggered(context,addr))
232 interrupt_handle_now(signal, info, context);
239 os_install_interrupt_handlers()
241 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,