Reduce pinned object table size, part 1 of 2.
[sbcl.git] / src / runtime / sunos-os.c
blobc24d0b6774508f034554a0dc67edaa42bc3ed071
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(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 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
89 char* beg = (char*) sbeg;
90 char* end = (char*) sbeg + slen;
91 char* adr = (char*) a;
92 return (adr >= beg && adr < end);
95 boolean is_valid_lisp_addr(os_vm_address_t addr)
97 /* Old CMUCL comment:
99 Just assume address is valid if it lies within one of the known
100 spaces. (Unlike sunos-os which keeps track of every valid page.) */
102 /* FIXME: this looks like a valid definition for all targets with
103 cheney-gc; it may not be impressively smart (witness the
104 comment above) but maybe associating these functions with the
105 GC rather than the OS would be a maintainability win. -- CSR,
106 2003-04-04 */
107 struct thread *th;
108 if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
109 in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE) ||
110 #ifdef LISP_FEATURE_GENCGC
111 in_range_p(addr, DYNAMIC_SPACE_START , dynamic_space_size)
112 #else
113 in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
114 in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
115 #endif
117 return 1;
118 for_each_thread(th) {
119 if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
120 return 1;
121 if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
122 return 1;
124 return 0;
128 #if defined LISP_FEATURE_GENCGC
130 void
131 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
133 void* fault_addr = (void*)info->si_addr;
135 #ifdef LISP_FEATURE_SB_SAFEPOINT
136 if (handle_safepoint_violation(context, fault_addr))
137 return;
138 #endif
140 if (!gencgc_handle_wp_violation(fault_addr))
141 if(!handle_guard_page_triggered(context, fault_addr))
142 lisp_memory_fault_error(context, fault_addr);
145 #else
147 static void
148 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
150 os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
152 if (!cheneygc_handle_wp_violation(context, addr)) {
153 if (!handle_guard_page_triggered(context,addr))
154 lisp_memory_fault_error(context, addr);
158 #endif
160 void
161 os_install_interrupt_handlers()
163 undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
164 sigsegv_handler);
166 /* OAOOM c.f. linux-os.c.
167 * Should we have a reusable function gc_install_interrupt_handlers? */
168 #ifdef LISP_FEATURE_SB_THREAD
169 # ifdef LISP_FEATURE_SB_SAFEPOINT
170 # ifdef LISP_FEATURE_SB_THRUPTION
171 undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
172 # endif
173 # else
174 undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
175 sig_stop_for_gc_handler);
176 # endif
177 #endif
180 char *
181 os_get_runtime_executable_path(int external)
183 char path[] = "/proc/self/object/a.out";
185 if (external || access(path, R_OK) == -1)
186 return NULL;
188 return copied_string(path);
191 #ifdef LISP_FEATURE_SB_WTIMER
193 * Waitable timer implementation for the safepoint-based (SIGALRM-free)
194 * timer facility using SunOS completion ports.
197 struct os_wtimer {
198 int port;
199 int timer;
202 struct os_wtimer *
203 os_create_wtimer()
205 int port = port_create();
206 if (port == -1) {
207 perror("port_create");
208 lose("os_create_wtimer");
211 port_notify_t pn;
212 pn.portnfy_port = port;
213 pn.portnfy_user = 0;
215 struct sigevent ev;
216 memset(&ev, 0, sizeof(ev));
217 ev.sigev_notify = SIGEV_PORT;
218 ev.sigev_value.sival_ptr = &pn;
220 timer_t timer;
221 if (timer_create(CLOCK_HIGHRES, &ev, &timer) == -1
222 && (errno != EPERM || timer_create(CLOCK_REALTIME, &ev, &timer) == -1))
224 perror("timer_create");
225 lose("os_create_wtimer");
228 struct os_wtimer *wt = malloc(sizeof(struct os_wtimer));
229 if (!wt)
230 lose("os_create_wtimer: malloc");
232 wt->port = port;
233 wt->timer = timer;
234 return wt;
238 os_wait_for_wtimer(struct os_wtimer *wt)
240 port_event_t pe;
241 if (port_get(wt->port, &pe, 0) == -1) {
242 if (errno == EINTR)
243 return 1;
244 perror("port_get");
245 lose("os_wtimer_listen failed");
247 return 0;
250 void
251 os_close_wtimer(struct os_wtimer *wt)
253 if (close(wt->port) == -1) {
254 perror("close");
255 lose("os_close_wtimer");
257 if (timer_delete(wt->timer) == -1) {
258 perror("timer_delete");
259 lose("os_close_wtimer");
261 free(wt);
264 void
265 os_set_wtimer(struct os_wtimer *wt, int sec, int nsec)
267 struct itimerspec spec;
268 spec.it_value.tv_sec = sec;
269 spec.it_value.tv_nsec = nsec;
270 spec.it_interval.tv_sec = 0;
271 spec.it_interval.tv_nsec = 0;
272 if (timer_settime(wt->timer, 0, &spec, 0) == -1) {
273 int x = errno;
274 perror("timer_settime");
275 if (x != EINVAL)
276 lose("os_set_wtimer");
280 void
281 os_cancel_wtimer(struct os_wtimer *wt)
283 os_set_wtimer(wt, 0, 0);
285 #endif