c1925ed5aa325afc4e8671761cb7a023603e91d9
[sbcl.git] / src / runtime / sunos-os.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <sys/file.h>
5
6 #include <unistd.h>
7 #include <errno.h>
8 #include <sys/param.h>
9 #include <sys/utsname.h>
10
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"
19
20 #ifdef LISP_FEATURE_X86
21 #include "genesis/static-symbols.h"
22 #include "genesis/fdefn.h"
23 #endif
24
25 #ifdef LISP_FEATURE_GENCGC
26 #include "gencgc-internal.h"
27 #endif
28
29 os_vm_size_t os_vm_page_size=0;
30
31 void
32 os_init(char *argv[], char *envp[])
33 {
34     /*
35      * historically, this used sysconf to select the runtime page size
36      * per recent changes on other arches and discussion on sbcl-devel,
37      * however, this is not necessary -- the VM page size need not match
38      * the OS page size (and the default backend page size has been
39      * ramped up accordingly for efficiency reasons).
40      */
41     os_vm_page_size = BACKEND_PAGE_BYTES;
42 }
43
44 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
45 {
46     int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANON;
47     if (addr)
48         flags |= MAP_FIXED;
49
50     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
51
52     if (addr == MAP_FAILED) {
53         perror("mmap");
54         lose ("Error in mmap(..)\n");
55     }
56
57     return addr;
58 }
59
60 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
61 {
62     if(munmap((void*) addr, len) == -1)
63         perror("munmap");
64 }
65
66 \f
67
68 os_vm_address_t
69 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
70 {
71
72     addr = mmap(addr, len,
73                 OS_VM_PROT_ALL,
74                 MAP_PRIVATE | MAP_FIXED,
75                 fd, (off_t) offset);
76
77     if (addr == MAP_FAILED) {
78         perror("mmap");
79         lose("Unexpedted mmap(..) failure\n");
80     }
81
82     return addr;
83 }
84
85 void
86 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
87 {
88     if(mprotect((void*)address, length, prot) == -1) {
89         perror("mprotect");
90     }
91 }
92
93 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
94 {
95     char* beg = (char*) sbeg;
96     char* end = (char*) sbeg + slen;
97     char* adr = (char*) a;
98     return (adr >= beg && adr < end);
99 }
100
101 boolean is_valid_lisp_addr(os_vm_address_t addr)
102 {
103     /* Old CMUCL comment:
104
105        Just assume address is valid if it lies within one of the known
106        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
107
108     /* FIXME: this looks like a valid definition for all targets with
109        cheney-gc; it may not be impressively smart (witness the
110        comment above) but maybe associating these functions with the
111        GC rather than the OS would be a maintainability win.  -- CSR,
112        2003-04-04 */
113     struct thread *th;
114     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
115        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
116 #ifdef LISP_FEATURE_GENCGC
117        in_range_p(addr, DYNAMIC_SPACE_START  , dynamic_space_size)
118 #else
119        in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
120        in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
121 #endif
122        )
123         return 1;
124     for_each_thread(th) {
125         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
126             return 1;
127         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
128             return 1;
129     }
130     return 0;
131 }
132 \f
133
134 #if defined LISP_FEATURE_GENCGC
135
136 void
137 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
138 {
139     void* fault_addr = (void*)info->si_addr;
140
141 #ifdef LISP_FEATURE_SB_SAFEPOINT
142     if (handle_safepoint_violation(context, fault_addr))
143             return;
144 #endif
145
146     if (!gencgc_handle_wp_violation(fault_addr))
147         if(!handle_guard_page_triggered(context, fault_addr))
148             lisp_memory_fault_error(context, fault_addr);
149 }
150
151 #else
152
153 static void
154 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
155 {
156     os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
157
158     if (!cheneygc_handle_wp_violation(context, addr)) {
159         if (!handle_guard_page_triggered(context,addr))
160             lisp_memory_fault_error(context, addr);
161     }
162 }
163
164 #endif
165
166 void
167 os_install_interrupt_handlers()
168 {
169     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
170                                                  sigsegv_handler);
171
172     /* OAOOM c.f. linux-os.c.
173      * Should we have a reusable function gc_install_interrupt_handlers? */
174 #ifdef LISP_FEATURE_SB_THREAD
175 # ifdef LISP_FEATURE_SB_SAFEPOINT
176 #  ifdef LISP_FEATURE_SB_THRUPTION
177     undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
178 #  endif
179 # else
180     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
181                                                  sig_stop_for_gc_handler);
182 # endif
183 #endif
184 }
185
186 char *
187 os_get_runtime_executable_path(int external)
188 {
189     char path[] = "/proc/self/object/a.out";
190
191     if (external || access(path, R_OK) == -1)
192         return NULL;
193
194     return copied_string(path);
195 }
196