Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / sunos-os.c
blob84d2dd07a6eaff2a930c0b605037547e16759eb8
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 #ifdef LISP_FEATURE_SB_WTIMER
30 # include <port.h>
31 # include <time.h>
32 # include <errno.h>
33 #endif
35 os_vm_size_t os_vm_page_size=0;
37 void
38 os_init(char *argv[], char *envp[])
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).
47 os_vm_page_size = BACKEND_PAGE_BYTES;
50 os_vm_address_t os_validate(boolean movable, os_vm_address_t addr, os_vm_size_t len)
52 int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANON;
53 if (addr)
54 flags |= MAP_FIXED;
56 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
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;
69 return addr;
72 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
74 if(munmap((void*) addr, len) == -1)
75 perror("munmap");
79 void
80 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
82 if(mprotect((void*)address, length, prot) == -1) {
83 perror("mprotect");
87 #if defined LISP_FEATURE_GENCGC
89 void
90 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
92 void* fault_addr = (void*)info->si_addr;
94 #ifdef LISP_FEATURE_SB_SAFEPOINT
95 if (handle_safepoint_violation(context, fault_addr))
96 return;
97 #endif
99 if (!gencgc_handle_wp_violation(fault_addr))
100 if(!handle_guard_page_triggered(context, fault_addr))
101 lisp_memory_fault_error(context, fault_addr);
104 #else
106 static void
107 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
109 os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
111 if (!cheneygc_handle_wp_violation(context, addr)) {
112 if (!handle_guard_page_triggered(context,addr))
113 lisp_memory_fault_error(context, addr);
117 #endif
119 void
120 os_install_interrupt_handlers()
122 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
123 sigsegv_handler);
125 /* OAOOM c.f. linux-os.c.
126 * Should we have a reusable function gc_install_interrupt_handlers? */
127 #ifdef LISP_FEATURE_SB_THREAD
128 # ifdef LISP_FEATURE_SB_SAFEPOINT
129 # ifdef LISP_FEATURE_SB_THRUPTION
130 undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
131 # endif
132 # else
133 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
134 sig_stop_for_gc_handler);
135 # endif
136 #endif
139 char *
140 os_get_runtime_executable_path(int external)
142 char path[] = "/proc/self/object/a.out";
144 if (external || access(path, R_OK) == -1)
145 return NULL;
147 return copied_string(path);
150 #ifdef LISP_FEATURE_SB_WTIMER
152 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
153 * timer facility using SunOS completion ports.
156 struct os_wtimer {
157 int port;
158 int timer;
161 struct os_wtimer *
162 os_create_wtimer()
164 int port = port_create();
165 if (port == -1) {
166 perror("port_create");
167 lose("os_create_wtimer");
170 port_notify_t pn;
171 pn.portnfy_port = port;
172 pn.portnfy_user = 0;
174 struct sigevent ev;
175 memset(&ev, 0, sizeof(ev));
176 ev.sigev_notify = SIGEV_PORT;
177 ev.sigev_value.sival_ptr = &pn;
179 timer_t timer;
180 if (timer_create(CLOCK_HIGHRES, &ev, &timer) == -1
181 && (errno != EPERM || timer_create(CLOCK_REALTIME, &ev, &timer) == -1))
183 perror("timer_create");
184 lose("os_create_wtimer");
187 struct os_wtimer *wt = malloc(sizeof(struct os_wtimer));
188 if (!wt)
189 lose("os_create_wtimer: malloc");
191 wt->port = port;
192 wt->timer = timer;
193 return wt;
197 os_wait_for_wtimer(struct os_wtimer *wt)
199 port_event_t pe;
200 if (port_get(wt->port, &pe, 0) == -1) {
201 if (errno == EINTR)
202 return 1;
203 perror("port_get");
204 lose("os_wtimer_listen failed");
206 return 0;
209 void
210 os_close_wtimer(struct os_wtimer *wt)
212 if (close(wt->port) == -1) {
213 perror("close");
214 lose("os_close_wtimer");
216 if (timer_delete(wt->timer) == -1) {
217 perror("timer_delete");
218 lose("os_close_wtimer");
220 free(wt);
223 void
224 os_set_wtimer(struct os_wtimer *wt, int sec, int nsec)
226 struct itimerspec spec;
227 spec.it_value.tv_sec = sec;
228 spec.it_value.tv_nsec = nsec;
229 spec.it_interval.tv_sec = 0;
230 spec.it_interval.tv_nsec = 0;
231 if (timer_settime(wt->timer, 0, &spec, 0) == -1) {
232 int x = errno;
233 perror("timer_settime");
234 if (x != EINVAL)
235 lose("os_set_wtimer");
239 void
240 os_cancel_wtimer(struct os_wtimer *wt)
242 os_set_wtimer(wt, 0, 0);
244 #endif