Utility predicates for packing: UNBOUNDED-SC-P and UNBOUNDED-TN-P
[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 #ifdef LISP_FEATURE_SB_WTIMER
30 # include <port.h>
31 # include <time.h>
32 # include <errno.h>
33 #endif
34
35 os_vm_size_t os_vm_page_size=0;
36
37 void
38 os_init(char *argv[], char *envp[])
39 {
40     /*
41      * historically, this used sysconf to select the runtime page size
42      * per recent changes on other arches and discussion on sbcl-devel,
43      * however, this is not necessary -- the VM page size need not match
44      * the OS page size (and the default backend page size has been
45      * ramped up accordingly for efficiency reasons).
46      */
47     os_vm_page_size = BACKEND_PAGE_BYTES;
48 }
49
50 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
51 {
52     int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANON;
53     if (addr)
54         flags |= MAP_FIXED;
55
56     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
57
58     if (addr == MAP_FAILED) {
59         perror("mmap");
60         /* While it is generally hard to recover from out-of-memory
61          * situations, we require callers to decide on the right course
62          * of action here.  Consider thread creation: Failure to mmap
63          * here is common if users have started too many threads, and
64          * often we can recover from that and treat it as an ordinary
65          * error. */
66         return 0;
67     }
68
69     return addr;
70 }
71
72 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
73 {
74     if(munmap((void*) addr, len) == -1)
75         perror("munmap");
76 }
77
78 \f
79
80 os_vm_address_t
81 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
82 {
83
84     addr = mmap(addr, len,
85                 OS_VM_PROT_ALL,
86                 MAP_PRIVATE | MAP_FIXED,
87                 fd, (off_t) offset);
88
89     if (addr == MAP_FAILED) {
90         perror("mmap");
91         lose("Unexpedted mmap(..) failure\n");
92     }
93
94     return addr;
95 }
96
97 void
98 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
99 {
100     if(mprotect((void*)address, length, prot) == -1) {
101         perror("mprotect");
102     }
103 }
104
105 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
106 {
107     char* beg = (char*) sbeg;
108     char* end = (char*) sbeg + slen;
109     char* adr = (char*) a;
110     return (adr >= beg && adr < end);
111 }
112
113 boolean is_valid_lisp_addr(os_vm_address_t addr)
114 {
115     /* Old CMUCL comment:
116
117        Just assume address is valid if it lies within one of the known
118        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
119
120     /* FIXME: this looks like a valid definition for all targets with
121        cheney-gc; it may not be impressively smart (witness the
122        comment above) but maybe associating these functions with the
123        GC rather than the OS would be a maintainability win.  -- CSR,
124        2003-04-04 */
125     struct thread *th;
126     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
127        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
128 #ifdef LISP_FEATURE_GENCGC
129        in_range_p(addr, DYNAMIC_SPACE_START  , dynamic_space_size)
130 #else
131        in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
132        in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
133 #endif
134        )
135         return 1;
136     for_each_thread(th) {
137         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
138             return 1;
139         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
140             return 1;
141     }
142     return 0;
143 }
144 \f
145
146 #if defined LISP_FEATURE_GENCGC
147
148 void
149 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
150 {
151     void* fault_addr = (void*)info->si_addr;
152
153 #ifdef LISP_FEATURE_SB_SAFEPOINT
154     if (handle_safepoint_violation(context, fault_addr))
155             return;
156 #endif
157
158     if (!gencgc_handle_wp_violation(fault_addr))
159         if(!handle_guard_page_triggered(context, fault_addr))
160             lisp_memory_fault_error(context, fault_addr);
161 }
162
163 #else
164
165 static void
166 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
167 {
168     os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
169
170     if (!cheneygc_handle_wp_violation(context, addr)) {
171         if (!handle_guard_page_triggered(context,addr))
172             lisp_memory_fault_error(context, addr);
173     }
174 }
175
176 #endif
177
178 void
179 os_install_interrupt_handlers()
180 {
181     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
182                                                  sigsegv_handler);
183
184     /* OAOOM c.f. linux-os.c.
185      * Should we have a reusable function gc_install_interrupt_handlers? */
186 #ifdef LISP_FEATURE_SB_THREAD
187 # ifdef LISP_FEATURE_SB_SAFEPOINT
188 #  ifdef LISP_FEATURE_SB_THRUPTION
189     undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
190 #  endif
191 # else
192     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
193                                                  sig_stop_for_gc_handler);
194 # endif
195 #endif
196 }
197
198 char *
199 os_get_runtime_executable_path(int external)
200 {
201     char path[] = "/proc/self/object/a.out";
202
203     if (external || access(path, R_OK) == -1)
204         return NULL;
205
206     return copied_string(path);
207 }
208
209 #ifdef LISP_FEATURE_SB_WTIMER
210 /*
211  * Waitable timer implementation for the safepoint-based (SIGALRM-free)
212  * timer facility using SunOS completion ports.
213  */
214
215 struct os_wtimer {
216     int port;
217     int timer;
218 };
219
220 struct os_wtimer *
221 os_create_wtimer()
222 {
223     int port = port_create();
224     if (port == -1) {
225         perror("port_create");
226         lose("os_create_wtimer");
227     }
228
229     port_notify_t pn;
230     pn.portnfy_port = port;
231     pn.portnfy_user = 0;
232
233     struct sigevent ev;
234     memset(&ev, 0, sizeof(ev));
235     ev.sigev_notify = SIGEV_PORT;
236     ev.sigev_value.sival_ptr = &pn;
237
238     timer_t timer;
239     if (timer_create(CLOCK_HIGHRES, &ev, &timer) == -1
240         && (errno != EPERM || timer_create(CLOCK_REALTIME, &ev, &timer) == -1))
241     {
242         perror("timer_create");
243         lose("os_create_wtimer");
244     }
245
246     struct os_wtimer *wt = malloc(sizeof(struct os_wtimer));
247     if (!wt)
248         lose("os_create_wtimer: malloc");
249
250     wt->port = port;
251     wt->timer = timer;
252     return wt;
253 }
254
255 int
256 os_wait_for_wtimer(struct os_wtimer *wt)
257 {
258     port_event_t pe;
259     if (port_get(wt->port, &pe, 0) == -1) {
260         if (errno == EINTR)
261             return 1;
262         perror("port_get");
263         lose("os_wtimer_listen failed");
264     }
265     return 0;
266 }
267
268 void
269 os_close_wtimer(struct os_wtimer *wt)
270 {
271     if (close(wt->port) == -1) {
272         perror("close");
273         lose("os_close_wtimer");
274     }
275     if (timer_delete(wt->timer) == -1) {
276         perror("timer_delete");
277         lose("os_close_wtimer");
278     }
279     free(wt);
280 }
281
282 void
283 os_set_wtimer(struct os_wtimer *wt, int sec, int nsec)
284 {
285     struct itimerspec spec;
286     spec.it_value.tv_sec = sec;
287     spec.it_value.tv_nsec = nsec;
288     spec.it_interval.tv_sec = 0;
289     spec.it_interval.tv_nsec = 0;
290     if (timer_settime(wt->timer, 0, &spec, 0) == -1) {
291         int x = errno;
292         perror("timer_settime");
293         if (x != EINVAL)
294             lose("os_set_wtimer");
295     }
296 }
297
298 void
299 os_cancel_wtimer(struct os_wtimer *wt)
300 {
301     os_set_wtimer(wt, 0, 0);
302 }
303 #endif