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!)\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) {
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");
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);
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;
110 addr = mmap(addr, len,
115 if (addr == MAP_FAILED) {
117 lose ("Error in mmap(..)\n");
123 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
125 if(munmap((void*) addr, len) == -1)
132 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
135 addr = mmap(addr, len,
137 MAP_PRIVATE | MAP_FIXED,
140 if (addr == MAP_FAILED) {
142 lose("Unexpedted mmap(..) failure\n");
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) {
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,
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)
182 in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
183 in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
187 for_each_thread(th) {
188 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
190 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
197 #if defined LISP_FEATURE_GENCGC
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);
210 interrupt_handle_now(signal, info, context);
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);
231 os_install_interrupt_handlers()
233 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
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 undoably_install_low_level_interrupt_handler(SIG_RESUME_FROM_GC,
242 sig_resume_from_gc_handler);
247 os_get_runtime_executable_path()
250 char path[] = "/proc/self/object/a.out";
252 ret = access(path, R_OK);
256 return copied_string(path);