1.0.22.22: (SETF FIND-CLASSOID) to drop DEFTYPE lambda-lists and source-locations
[sbcl/tcr.git] / src / runtime / sunos-os.c
blob8d8bb2802ff5fdcbddd1647194f96901b798e2d3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <sys/file.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <sys/param.h>
9 #include <sys/utsname.h>
11 #include "sbcl.h"
12 #include "os.h"
13 #include "arch.h"
14 #include "interr.h"
15 #include "interrupt.h"
16 #include "globals.h"
17 #include "validate.h"
18 #include "target-arch-os.h"
20 #ifdef LISP_FEATURE_X86
21 #include "genesis/static-symbols.h"
22 #include "genesis/fdefn.h"
23 #endif
25 #ifdef LISP_FEATURE_GENCGC
26 #include "gencgc-internal.h"
27 #endif
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
33 #else
34 #error "Don't know OS_VM_DEFAULT_PAGESIZE"
35 #endif
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 */
56 void
57 os_init(char *argv[], char *envp[])
59 struct utsname name;
60 int major_version;
61 int minor_version;
63 uname(&name);
64 major_version = atoi(name.release);
65 if (major_version != 5) {
66 lose("sunos major version=%d (which isn't 5!)\n", major_version);
68 minor_version = atoi(name.release+2);
69 if ((minor_version < 8)) {
70 kludge_mmap_fd = open("/dev/zero",O_RDONLY);
71 if (kludge_mmap_fd < 0) {
72 perror("open");
73 lose("Error in open(..)\n");
75 } else if (minor_version > 11) {
76 FSHOW((stderr, "os_init: Solaris version greater than 11?\nUnknown MAP_ANON behaviour.\n"));
77 lose("Unknown mmap() interaction with MAP_ANON\n");
78 } else {
79 /* Versions 8-11*/
80 KLUDGE_MAYBE_MAP_ANON = 0x100;
83 /* I do not understand this at all. FIXME. */
84 os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
86 if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
87 fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
88 os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
89 exit(1);
90 } else {
92 * we do this because there are apparently dependencies on
93 * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
94 * but since the OS doesn't know we're using this restriction,
95 * we have to grovel around a bit to enforce it, thus anything
96 * that uses real_page_size_difference.
98 /* FIXME: Is this still true? */
99 real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
100 os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
104 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
106 int flags = MAP_PRIVATE | MAP_NORESERVE | KLUDGE_MAYBE_MAP_ANON;
107 if (addr)
108 flags |= MAP_FIXED;
110 addr = mmap(addr, len,
111 OS_VM_PROT_ALL,
112 flags,
113 kludge_mmap_fd, 0);
115 if (addr == MAP_FAILED) {
116 perror("mmap");
117 lose ("Error in mmap(..)\n");
120 return addr;
123 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
125 if(munmap((void*) addr, len) == -1)
126 perror("munmap");
131 os_vm_address_t
132 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
135 addr = mmap(addr, len,
136 OS_VM_PROT_ALL,
137 MAP_PRIVATE | MAP_FIXED,
138 fd, (off_t) offset);
140 if (addr == MAP_FAILED) {
141 perror("mmap");
142 lose("Unexpedted mmap(..) failure\n");
145 return addr;
148 void
149 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
151 if(mprotect((void*)address, length, prot) == -1) {
152 perror("mprotect");
156 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
158 char* beg = (char*) sbeg;
159 char* end = (char*) sbeg + slen;
160 char* adr = (char*) a;
161 return (adr >= beg && adr < end);
164 boolean is_valid_lisp_addr(os_vm_address_t addr)
166 /* Old CMUCL comment:
168 Just assume address is valid if it lies within one of the known
169 spaces. (Unlike sunos-os which keeps track of every valid page.) */
171 /* FIXME: this looks like a valid definition for all targets with
172 cheney-gc; it may not be impressively smart (witness the
173 comment above) but maybe associating these functions with the
174 GC rather than the OS would be a maintainability win. -- CSR,
175 2003-04-04 */
176 struct thread *th;
177 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
178 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
179 #ifdef LISP_FEATURE_GENCGC
180 in_range_p(addr, DYNAMIC_SPACE_START , dynamic_space_size)
181 #else
182 in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
183 in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
184 #endif
186 return 1;
187 for_each_thread(th) {
188 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
189 return 1;
190 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
191 return 1;
193 return 0;
197 #if defined LISP_FEATURE_GENCGC
199 void
200 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
202 os_context_t *context = arch_os_get_context(&void_context);
203 void* fault_addr = (void*)info->si_addr;
205 if (!gencgc_handle_wp_violation(fault_addr))
206 if(!handle_guard_page_triggered(context, fault_addr))
207 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
208 lisp_memory_fault_error(context, fault_addr);
209 #else
210 interrupt_handle_now(signal, info, context);
211 #endif
214 #else
216 static void
217 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
219 os_context_t *context = arch_os_get_context(&void_context);
220 os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
222 if (!cheneygc_handle_wp_violation(context, addr)) {
223 if (!handle_guard_page_triggered(context,addr))
224 interrupt_handle_now(signal, info, context);
228 #endif
230 void
231 os_install_interrupt_handlers()
233 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
234 sigsegv_handler);
236 #ifdef LISP_FEATURE_SB_THREAD
237 undoably_install_low_level_interrupt_handler(SIG_INTERRUPT_THREAD,
238 interrupt_thread_handler);
239 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
240 sig_stop_for_gc_handler);
241 #endif
244 char *
245 os_get_runtime_executable_path()
247 int ret;
248 char path[] = "/proc/self/object/a.out";
250 ret = access(path, R_OK);
251 if (ret == -1)
252 return NULL;
254 return copied_string(path);