Add a safepoint-based mechanism to avoid SIGALRM for the TIMER facility
[sbcl.git] / src / runtime / bsd-os.c
1 /*
2  * OS-dependent routines for BSD-ish systems
3  *
4  * This file (along with os.h) exports an OS-independent interface to
5  * the operating system VM facilities. This interface looks a lot like
6  * the Mach interface (but simpler in some places). For some operating
7  * systems, a subset of these functions will have to be emulated.
8  */
9
10 /*
11  * This software is part of the SBCL system. See the README file for
12  * more information.
13  *
14  * This software is derived from the CMU CL system, which was
15  * written at Carnegie Mellon University and released into the
16  * public domain. The software is in the public domain and is
17  * provided with absolutely no warranty. See the COPYING and CREDITS
18  * files for more information.
19  */
20
21 #include <stdio.h>
22 #include <sys/param.h>
23 #include <sys/file.h>
24 #include <unistd.h>
25 #include <utime.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include "sbcl.h"
29 #include "./signal.h"
30 #include "os.h"
31 #include "arch.h"
32 #include "globals.h"
33 #include "interrupt.h"
34 #include "interr.h"
35 #include "lispregs.h"
36 #include "thread.h"
37 #include "runtime.h"
38 #include "genesis/static-symbols.h"
39 #include "genesis/fdefn.h"
40
41 #include <sys/types.h>
42 #include <signal.h>
43 /* #include <sys/sysinfo.h> */
44 #include "validate.h"
45 #if defined LISP_FEATURE_GENCGC
46 #include "gencgc-internal.h"
47 #endif
48
49 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
50 # include <sys/event.h>
51 #endif
52
53 \f
54 os_vm_size_t os_vm_page_size;
55
56 #ifdef __NetBSD__
57 #include <sys/resource.h>
58 #include <sys/sysctl.h>
59 #include <string.h>
60 #include <sys/stat.h> /* For the stat-family wrappers. */
61 #include <dirent.h>   /* For the opendir()/readdir() wrappers */
62 #include <sys/socket.h> /* For the socket() wrapper */
63 static void netbsd_init();
64 #endif /* __NetBSD__ */
65
66 #ifdef __FreeBSD__
67 #include <sys/sysctl.h>
68 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
69 #include <sys/umtx.h>
70 #endif
71
72 static void freebsd_init();
73 #endif /* __FreeBSD__ */
74
75 #ifdef __OpenBSD__
76 #include <sys/types.h>
77 #include <sys/resource.h>
78 #include <sys/stat.h>
79 #include <sys/sysctl.h>
80 #include <dlfcn.h>
81 #ifdef LISP_FEATURE_X86
82 #include <machine/cpu.h>
83 #endif
84
85 static void openbsd_init();
86 #endif
87
88 void
89 os_init(char *argv[], char *envp[])
90 {
91     os_vm_page_size = BACKEND_PAGE_BYTES;
92
93 #ifdef __NetBSD__
94     netbsd_init();
95 #elif defined(__FreeBSD__)
96     freebsd_init();
97 #elif defined(__OpenBSD__)
98     openbsd_init();
99 #elif defined(LISP_FEATURE_DARWIN)
100     darwin_init();
101 #endif
102 }
103
104 sigset_t *
105 os_context_sigmask_addr(os_context_t *context)
106 {
107     /* (Unlike most of the other context fields that we access, the
108      * signal mask field is a field of the basic, outermost context
109      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
110 #if defined(__FreeBSD__)  || defined(__NetBSD__) || defined(LISP_FEATURE_DARWIN)
111     return &context->uc_sigmask;
112 #elif defined (__OpenBSD__)
113     return &context->sc_mask;
114 #else
115 #error unsupported BSD variant
116 #endif
117 }
118
119 os_vm_address_t
120 os_validate(os_vm_address_t addr, os_vm_size_t len)
121 {
122     int flags = MAP_PRIVATE | MAP_ANON;
123
124     if (addr)
125         flags |= MAP_FIXED;
126
127     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
128
129     if (addr == MAP_FAILED) {
130         perror("mmap");
131         return NULL;
132     }
133
134     return addr;
135 }
136
137 void
138 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
139 {
140     if (munmap(addr, len) == -1)
141         perror("munmap");
142 }
143
144 os_vm_address_t
145 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
146 {
147     addr = mmap(addr, len,
148                 OS_VM_PROT_ALL,
149                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
150                 fd, (off_t) offset);
151
152     if (addr == MAP_FAILED) {
153         perror("mmap");
154         lose("unexpected mmap(..) failure\n");
155     }
156
157     return addr;
158 }
159
160 void
161 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
162 {
163     if (mprotect(address, length, prot) == -1) {
164         perror("mprotect");
165     }
166 }
167 \f
168 static boolean
169 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
170 {
171     char* beg = (char*) sbeg;
172     char* end = (char*) sbeg + slen;
173     char* adr = (char*) a;
174     return (adr >= beg && adr < end);
175 }
176
177 boolean
178 is_valid_lisp_addr(os_vm_address_t addr)
179 {
180     struct thread *th;
181
182     if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
183         in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE) ||
184         in_range_p(addr, DYNAMIC_SPACE_START, dynamic_space_size))
185         return 1;
186     for_each_thread(th) {
187         if (((os_vm_address_t)th->control_stack_start <= addr) &&
188             (addr < (os_vm_address_t)th->control_stack_end))
189             return 1;
190         if (in_range_p(addr, (lispobj) th->binding_stack_start,
191                        BINDING_STACK_SIZE))
192             return 1;
193     }
194     return 0;
195 }
196 \f
197 /*
198  * any OS-dependent special low-level handling for signals
199  */
200
201 #if defined LISP_FEATURE_GENCGC
202
203 /*
204  * The GENCGC needs to be hooked into whatever signal is raised for
205  * page fault on this OS.
206  */
207
208 void
209 memory_fault_handler(int signal, siginfo_t *siginfo, os_context_t *context)
210 {
211     void *fault_addr = arch_get_bad_addr(signal, siginfo, context);
212
213 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
214     FSHOW_SIGNAL((stderr, "/ TLS: restoring fs: %p in memory_fault_handler\n",
215                   *CONTEXT_ADDR_FROM_STEM(fs)));
216     os_restore_tls_segment_register(context);
217 #endif
218
219     FSHOW((stderr, "Memory fault at: %p, PC: %p\n", fault_addr, *os_context_pc_addr(context)));
220
221 #ifdef LISP_FEATURE_SB_SAFEPOINT
222     if (!handle_safepoint_violation(context, fault_addr))
223 #endif
224
225     if (!gencgc_handle_wp_violation(fault_addr))
226         if(!handle_guard_page_triggered(context,fault_addr))
227             lisp_memory_fault_error(context, fault_addr);
228 }
229
230 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
231 void
232 mach_error_memory_fault_handler(int signal, siginfo_t *siginfo,
233                                 os_context_t *context) {
234     lose("Unhandled memory fault. Exiting.");
235 }
236 #endif
237
238 void
239 os_install_interrupt_handlers(void)
240 {
241     SHOW("os_install_interrupt_handlers()/bsd-os/defined(GENCGC)");
242 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
243     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
244                                                  mach_error_memory_fault_handler);
245 #else
246     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
247 #ifdef LISP_FEATURE_FREEBSD
248                                                  (__siginfohandler_t *)
249 #endif
250                                                  memory_fault_handler);
251 #endif
252
253 #ifdef LISP_FEATURE_SB_THREAD
254 # ifdef LISP_FEATURE_SB_SAFEPOINT
255 #  ifdef LISP_FEATURE_SB_THRUPTION
256     undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
257 #  endif
258 # else
259     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
260                                                  sig_stop_for_gc_handler);
261 # endif
262 #endif
263     SHOW("leaving os_install_interrupt_handlers()");
264 }
265
266 #else /* Currently PPC/Darwin/Cheney only */
267
268 static void
269 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
270 {
271 #if 0
272     unsigned int pc =  (unsigned int *)(*os_context_pc_addr(context));
273 #endif
274     os_vm_address_t addr;
275
276     addr = arch_get_bad_addr(signal, info, context);
277     if (!cheneygc_handle_wp_violation(context, addr))
278         if (!handle_guard_page_triggered(context, addr))
279             interrupt_handle_now(signal, info, context);
280 }
281
282 void
283 os_install_interrupt_handlers(void)
284 {
285     SHOW("os_install_interrupt_handlers()/bsd-os/!defined(GENCGC)");
286     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
287                                                  sigsegv_handler);
288 }
289
290 #endif /* defined GENCGC */
291
292 #ifdef __NetBSD__
293 static void netbsd_init()
294 {
295     struct rlimit rl;
296     int mib[2], osrev;
297     size_t len;
298
299     /* Are we running on a sufficiently functional kernel? */
300     mib[0] = CTL_KERN;
301     mib[1] = KERN_OSREV;
302
303     len = sizeof(osrev);
304     sysctl(mib, 2, &osrev, &len, NULL, 0);
305
306     /* If we're older than 2.0... */
307     if (osrev < 200000000) {
308         fprintf(stderr, "osrev = %d (needed at least 200000000).\n", osrev);
309         lose("NetBSD kernel too old to run sbcl.\n");
310     }
311
312     /* NetBSD counts mmap()ed space against the process's data size limit,
313      * so yank it up. This might be a nasty thing to do? */
314     getrlimit (RLIMIT_DATA, &rl);
315     /* Amazingly for such a new port, the provenance and meaning of
316        this number are unknown.  It might just mean REALLY_BIG_LIMIT,
317        or possibly it should be calculated from dynamic space size.
318        -- CSR, 2004-04-08 */
319     rl.rlim_cur = 1073741824;
320     if (setrlimit (RLIMIT_DATA, &rl) < 0) {
321         fprintf (stderr,
322                  "RUNTIME WARNING: unable to raise process data size limit:\n\
323   %s.\n\
324 The system may fail to start.\n",
325                  strerror(errno));
326     }
327 }
328
329 /* Various routines in NetBSD's C library are compatibility wrappers
330    for old versions. Programs must be processed by the C toolchain in
331    order to get up-to-date definitions of such routines. */
332 /* The stat-family, opendir, and readdir are used only in sb-posix, as
333    of 2007-01-16. -- RMK */
334 int
335 _stat(const char *path, struct stat *sb)
336 {
337     return stat(path, sb);
338 }
339 int
340 _lstat(const char *path, struct stat *sb)
341 {
342     return lstat(path, sb);
343 }
344 int
345 _fstat(int fd, struct stat *sb)
346 {
347     return fstat(fd, sb);
348 }
349
350 DIR *
351 _opendir(const char *filename)
352 {
353     return opendir(filename);
354 }
355 struct dirent *
356 _readdir(DIR *dirp)
357 {
358     return readdir(dirp);
359 }
360
361 int
362 _utime(const char *file, const struct utimbuf *timep)
363 {
364     return utime(file, timep);
365 }
366
367 /* Used in sb-bsd-sockets. */
368 int
369 _socket(int domain, int type, int protocol)
370 {
371     return socket(domain, type, protocol);
372 }
373 #endif /* __NetBSD__ */
374
375 #ifdef __FreeBSD__
376 extern int getosreldate(void);
377
378 int sig_memory_fault;
379
380 static void freebsd_init()
381 {
382     /* Memory fault signal on FreeBSD was changed from SIGBUS to
383      * SIGSEGV. */
384     if (getosreldate() < 700004)
385         sig_memory_fault = SIGBUS;
386     else
387         sig_memory_fault = SIGSEGV;
388
389     /* Quote from sbcl-devel (NIIMI Satoshi): "Some OSes, like FreeBSD
390      * 4.x with GENERIC kernel, does not enable SSE support even on
391      * SSE capable CPUs". Detect this situation and skip the
392      * fast_bzero sse/base selection logic that's normally done in
393      * x86-assem.S.
394      */
395 #ifdef LISP_FEATURE_X86
396     {
397         size_t len;
398         int instruction_sse;
399
400         len = sizeof(instruction_sse);
401         if (sysctlbyname("hw.instruction_sse", &instruction_sse, &len,
402                          NULL, 0) == 0 && instruction_sse != 0) {
403             /* Use the SSE detector */
404             fast_bzero_pointer = fast_bzero_detect;
405         }
406     }
407 #endif /* LISP_FEATURE_X86 */
408 }
409
410 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_FUTEX) \
411     && !defined(LISP_FEATURE_SB_PTHREAD_FUTEX)
412 int
413 futex_wait(int *lock_word, long oldval, long sec, unsigned long usec)
414 {
415     struct timespec timeout;
416     int ret;
417
418     if (sec < 0)
419         ret = umtx_wait((void *)lock_word, oldval, NULL);
420     else {
421         timeout.tv_sec = sec;
422         timeout.tv_nsec = usec * 1000;
423         ret = umtx_wait((void *)lock_word, oldval, &timeout);
424     }
425
426     switch (ret) {
427     case 0:
428         return 0;
429     case ETIMEDOUT:
430         return 1;
431     case EINTR:
432         return 2;
433     default:
434         /* EWOULDBLOCK and others, need to check the lock */
435         return -1;
436     }
437 }
438
439 int
440 futex_wake(int *lock_word, int n)
441 {
442     return umtx_wake((void *)lock_word, n);
443 }
444 #endif
445 #endif /* __FreeBSD__ */
446
447 #ifdef LISP_FEATURE_DARWIN
448 /* defined in ppc-darwin-os.c instead */
449 #elif defined(LISP_FEATURE_FREEBSD)
450 #ifndef KERN_PROC_PATHNAME
451 #define KERN_PROC_PATHNAME 12
452 #endif
453
454 char *
455 os_get_runtime_executable_path(int external)
456 {
457     char path[PATH_MAX + 1];
458
459     if (getosreldate() >= 600024) {
460         /* KERN_PROC_PATHNAME is available */
461         size_t len = PATH_MAX + 1;
462         int mib[4];
463
464         mib[0] = CTL_KERN;
465         mib[1] = KERN_PROC;
466         mib[2] = KERN_PROC_PATHNAME;
467         mib[3] = -1;
468         if (sysctl(mib, 4, &path, &len, NULL, 0) != 0)
469             return NULL;
470     } else {
471         int size;
472         size = readlink("/proc/curproc/file", path, sizeof(path) - 1);
473         if (size < 0)
474             return NULL;
475         path[size] = '\0';
476     }
477     if (strcmp(path, "unknown") == 0)
478         return NULL;
479     return copied_string(path);
480 }
481 #elif defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD)
482 char *
483 os_get_runtime_executable_path(int external)
484 {
485     struct stat sb;
486     if (!external && stat("/proc/curproc/file", &sb) == 0)
487         return copied_string("/proc/curproc/file");
488     return NULL;
489 }
490 #else /* Not DARWIN or FREEBSD or NETBSD or OPENBSD */
491 char *
492 os_get_runtime_executable_path(int external)
493 {
494     return NULL;
495 }
496 #endif
497
498 #ifdef __OpenBSD__
499
500 int openbsd_use_fxsave = 0;
501
502 void
503 openbsd_init()
504 {
505 #ifdef LISP_FEATURE_X86
506     int mib[2];
507     size_t size;
508 #endif
509     /*
510      * Show a warning if it looks like the memory available after
511      * allocating the spaces won't be at least this much.
512      */
513 #ifdef LISP_FEATURE_X86_64
514     const int wantfree = 64 * 1024 * 1024;
515 #else
516     const int wantfree = 32 * 1024 * 1024;
517 #endif
518     struct rlimit rl;
519
520 #ifdef LISP_FEATURE_X86
521     /* Save the machdep.osfxsr sysctl for use by os_restore_fp_control() */
522     mib[0] = CTL_MACHDEP;
523     mib[1] = CPU_OSFXSR;
524     size = sizeof (openbsd_use_fxsave);
525     sysctl(mib, 2, &openbsd_use_fxsave, &size, NULL, 0);
526 #endif
527
528     /* OpenBSD, like NetBSD, counts mmap()ed space against the
529      * process's data size limit. If the soft limit is lower than the
530      * hard limit then try to yank it up, this lets users in the
531      * "staff" or "daemon" login classes run sbcl with larger dynamic
532      * space sizes.
533      */
534     getrlimit (RLIMIT_DATA, &rl);
535     if (rl.rlim_cur < rl.rlim_max) {
536         rl.rlim_cur = rl.rlim_max;
537         if (setrlimit (RLIMIT_DATA, &rl) < 0) {
538             fprintf (stderr,
539                      "RUNTIME WARNING: unable to raise process data size limit:\n\
540   %s.\n\
541 The system may fail to start.\n",
542                      strerror(errno));
543         }
544     }
545
546     /*
547      * Display a (hopefully) helpful warning if it looks like we won't
548      * be able to allocate enough memory.
549      */
550     getrlimit (RLIMIT_DATA, &rl);
551     if (dynamic_space_size + READ_ONLY_SPACE_SIZE + STATIC_SPACE_SIZE +
552         LINKAGE_TABLE_SPACE_SIZE + wantfree > rl.rlim_cur)
553         fprintf (stderr,
554                  "RUNTIME WARNING: data size resource limit may be too low,\n"
555                  "  try decreasing the dynamic space size with --dynamic-space-size\n"
556                  "  or raising the datasize or datasize-max limits in /etc/login.conf\n");
557 }
558
559 /* OpenBSD's dlsym() relies on the gcc bulitin
560  * __builtin_return_address(0) returning an address in the
561  * executable's text segment, but when called from lisp it will return
562  * an address in the dynamic space.  Work around this by calling this
563  * wrapper function instead. Note that tail-call optimization will
564  * defeat this, disable it by saving the dlsym() return value in a
565  * volatile variable.
566 */
567 void *
568 os_dlsym(void *handle, const char *symbol)
569 {
570     void * volatile ret = dlsym(handle, symbol);
571     return ret;
572 }
573
574 #endif
575
576 #if defined(LISP_FEATURE_SB_WTIMER) && !defined(LISP_FEATURE_DARWIN)
577 /*
578  * Waitable timer implementation for the safepoint-based (SIGALRM-free)
579  * timer facility using kqueue.
580  */
581 int
582 os_create_wtimer()
583 {
584     int kq = kqueue();
585     if (kq == -1)
586         lose("os_create_wtimer: kqueue");
587     return kq;
588 }
589
590 int
591 os_wait_for_wtimer(int kq)
592 {
593     struct kevent ev;
594     int n;
595     if ( (n = kevent(kq, 0, 0, &ev, 1, 0)) == -1) {
596         if (errno != EINTR)
597             lose("os_wtimer_listen failed");
598         n = 0;
599     }
600     return n != 1;
601 }
602
603 void
604 os_close_wtimer(int kq)
605 {
606     if (close(kq) == -1)
607         lose("os_close_wtimer failed");
608 }
609
610 void
611 os_set_wtimer(int kq, int sec, int nsec)
612 {
613     long long msec
614         = ((long long) sec) * 1000 + (long long) (nsec+999999) / 1000000;
615     if (msec > INT_MAX) msec = INT_MAX;
616
617     struct kevent ev;
618     EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE|EV_ONESHOT, 0, (int)msec, 0);
619     if (kevent(kq, &ev, 1, 0, 0, 0) == -1)
620         perror("os_set_wtimer: kevent");
621 }
622
623 void
624 os_cancel_wtimer(int kq)
625 {
626     struct kevent ev;
627     EV_SET(&ev, 1, EVFILT_TIMER, EV_DISABLE, 0, 0, 0);
628     if (kevent(kq, &ev, 1, 0, 0, 0) == -1 && errno != ENOENT)
629         perror("os_cancel_wtimer: kevent");
630 }
631 #endif