6 THREADHANDLE start_thread(void *(*start_routine)(void *),void *args)
10 if (pthread_attr_init (&attr) != 0)
11 fatal_error("pthread_attr_init() failed",0);
12 if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) != 0)
13 fatal_error("pthread_attr_setdetachstate() failed",0);
14 if (pthread_create (&thread, &attr, start_routine, args) != 0)
15 fatal_error("pthread_create() failed",0);
16 pthread_attr_destroy(&attr);
20 static void *null_dll;
22 void sleep_nanos(u64 nsec)
27 ts.tv_sec = nsec / 1000000000;
28 ts.tv_nsec = nsec % 1000000000;
29 ret = nanosleep(&ts,&ts_rem);
30 while(ret == -1 && errno == EINTR)
32 memcpy(&ts, &ts_rem, sizeof(ts));
33 ret = nanosleep(&ts, &ts_rem);
37 fatal_error("nanosleep failed", 0);
40 void factor_vm::init_ffi()
42 null_dll = dlopen(NULL,RTLD_LAZY);
45 void factor_vm::ffi_dlopen(dll *dll)
47 dll->handle = dlopen(alien_offset(dll->path), RTLD_LAZY);
50 void *factor_vm::ffi_dlsym_raw(dll *dll, symbol_char *symbol)
52 return dlsym(dll ? dll->handle : null_dll, symbol);
55 void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
57 return FUNCTION_CODE_POINTER(ffi_dlsym_raw(dll, symbol));
61 void *factor_vm::ffi_dlsym_toc(dll *dll, symbol_char *symbol)
63 return FUNCTION_TOC_POINTER(ffi_dlsym_raw(dll, symbol));
67 void factor_vm::ffi_dlclose(dll *dll)
69 if(dlclose(dll->handle))
70 general_error(ERROR_FFI,false_object,false_object);
74 void factor_vm::primitive_existsp()
77 char *path = (char *)(untag_check<byte_array>(ctx->pop()) + 1);
78 ctx->push(tag_boolean(stat(path,&sb) >= 0));
81 void factor_vm::move_file(const vm_char *path1, const vm_char *path2)
86 ret = rename((path1),(path2));
88 while(ret < 0 && errno == EINTR);
91 general_error(ERROR_IO,tag_fixnum(errno),false_object);
94 segment::segment(cell size_, bool executable_p)
98 int pagesize = getpagesize();
102 prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
104 prot = (PROT_READ | PROT_WRITE);
106 char *array = (char *)mmap(NULL,pagesize + size + pagesize,prot,MAP_ANON | MAP_PRIVATE,-1,0);
107 if(array == (char*)-1) out_of_memory();
109 if(mprotect(array,pagesize,PROT_NONE) == -1)
110 fatal_error("Cannot protect low guard page",(cell)array);
112 if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
113 fatal_error("Cannot protect high guard page",(cell)array);
115 start = (cell)(array + pagesize);
121 int pagesize = getpagesize();
122 int retval = munmap((void*)(start - pagesize),pagesize + size + pagesize);
124 fatal_error("Segment deallocation failed",0);
127 void code_heap::guard_safepoint()
129 if(mprotect(safepoint_page,getpagesize(),PROT_NONE) == -1)
130 fatal_error("Cannot protect safepoint guard page",(cell)safepoint_page);
133 void code_heap::unguard_safepoint()
135 if(mprotect(safepoint_page,getpagesize(),PROT_WRITE) == -1)
136 fatal_error("Cannot unprotect safepoint guard page",(cell)safepoint_page);
139 void factor_vm::dispatch_signal(void *uap, void (handler)())
141 dispatch_signal_handler(
142 (cell*)&UAP_STACK_POINTER(uap),
143 (cell*)&UAP_PROGRAM_COUNTER(uap),
144 (cell)FUNCTION_CODE_POINTER(handler)
146 UAP_SET_TOC_POINTER(uap, (cell)FUNCTION_TOC_POINTER(handler));
149 void factor_vm::enqueue_safepoint_signal(cell signal)
151 /* to be implemented, see #297
152 code->guard_safepoint();
156 void factor_vm::start_sampling_profiler_timer()
158 struct itimerval timer;
159 memset((void*)&timer, 0, sizeof(struct itimerval));
160 timer.it_value.tv_usec = 1000000/FACTOR_PROFILE_SAMPLES_PER_SECOND;
161 timer.it_interval.tv_usec = 1000000/FACTOR_PROFILE_SAMPLES_PER_SECOND;
162 setitimer(ITIMER_REAL, &timer, NULL);
165 void factor_vm::end_sampling_profiler_timer()
167 struct itimerval timer;
168 memset((void*)&timer, 0, sizeof(struct itimerval));
169 setitimer(ITIMER_REAL, &timer, NULL);
172 void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap)
174 factor_vm *vm = current_vm();
175 vm->signal_fault_addr = (cell)siginfo->si_addr;
176 vm->dispatch_signal(uap,factor::memory_signal_handler_impl);
179 void synchronous_signal_handler(int signal, siginfo_t *siginfo, void *uap)
181 factor_vm *vm = current_vm_p();
184 vm->signal_number = signal;
185 vm->dispatch_signal(uap,factor::synchronous_signal_handler_impl);
187 fatal_error("Foreign thread received signal", signal);
190 void enqueue_signal_handler(int signal, siginfo_t *siginfo, void *uap)
192 factor_vm *vm = current_vm_p();
194 vm->enqueue_safepoint_signal(signal);
196 fatal_error("Foreign thread received signal", signal);
199 void fep_signal_handler(int signal, siginfo_t *siginfo, void *uap)
201 factor_vm *vm = current_vm_p();
203 vm->enqueue_safepoint_fep();
205 fatal_error("Foreign thread received signal", signal);
208 void sample_signal_handler(int signal, siginfo_t *siginfo, void *uap)
210 factor_vm *vm = current_vm_p();
212 vm->enqueue_safepoint_sample();
214 fatal_error("Foreign thread received signal", signal);
217 void ignore_signal_handler(int signal, siginfo_t *siginfo, void *uap)
221 void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap)
223 factor_vm *vm = current_vm();
224 vm->signal_number = signal;
225 vm->signal_fpu_status = fpu_status(uap_fpu_status(uap));
226 uap_clear_fpu_status(uap);
228 vm->dispatch_signal(uap,
229 (siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF)
230 ? factor::synchronous_signal_handler_impl
231 : factor::fp_signal_handler_impl);
234 static void sigaction_safe(int signum, const struct sigaction *act, struct sigaction *oldact)
239 ret = sigaction(signum, act, oldact);
241 while(ret == -1 && errno == EINTR);
244 fatal_error("sigaction failed", 0);
247 static void init_sigaction_with_handler(struct sigaction *act,
248 void (*handler)(int, siginfo_t*, void*))
250 memset(act, 0, sizeof(struct sigaction));
251 sigemptyset(&act->sa_mask);
252 act->sa_sigaction = handler;
253 act->sa_flags = SA_SIGINFO | SA_ONSTACK;
256 void factor_vm::unix_init_signals()
258 struct sigaction memory_sigaction;
259 struct sigaction synchronous_sigaction;
260 struct sigaction enqueue_sigaction;
261 struct sigaction fep_sigaction;
262 struct sigaction sample_sigaction;
263 struct sigaction fpe_sigaction;
264 struct sigaction ignore_sigaction;
266 init_sigaction_with_handler(&memory_sigaction, memory_signal_handler);
267 sigaction_safe(SIGBUS,&memory_sigaction,NULL);
268 sigaction_safe(SIGSEGV,&memory_sigaction,NULL);
269 sigaction_safe(SIGTRAP,&memory_sigaction,NULL);
271 init_sigaction_with_handler(&fpe_sigaction, fpe_signal_handler);
272 sigaction_safe(SIGFPE,&fpe_sigaction,NULL);
274 init_sigaction_with_handler(&synchronous_sigaction, synchronous_signal_handler);
275 sigaction_safe(SIGILL,&synchronous_sigaction,NULL);
276 sigaction_safe(SIGABRT,&synchronous_sigaction,NULL);
278 init_sigaction_with_handler(&enqueue_sigaction, enqueue_signal_handler);
279 sigaction_safe(SIGUSR1,&enqueue_sigaction,NULL);
280 sigaction_safe(SIGUSR2,&enqueue_sigaction,NULL);
281 sigaction_safe(SIGWINCH,&enqueue_sigaction,NULL);
283 sigaction_safe(SIGINFO,&enqueue_sigaction,NULL);
286 init_sigaction_with_handler(&fep_sigaction, fep_signal_handler);
287 sigaction_safe(SIGQUIT,&fep_sigaction,NULL);
288 sigaction_safe(SIGINT,&fep_sigaction,NULL);
290 init_sigaction_with_handler(&sample_sigaction, sample_signal_handler);
291 sigaction_safe(SIGALRM,&sample_sigaction,NULL);
293 /* We don't use SA_IGN here because then the ignore action is inherited
294 by subprocesses, which we don't want. There is a unit test in
295 io.launcher.unix for this. */
296 init_sigaction_with_handler(&ignore_sigaction, ignore_signal_handler);
297 sigaction_safe(SIGPIPE,&ignore_sigaction,NULL);
300 /* On Unix, shared fds such as stdin cannot be set to non-blocking mode
301 (http://homepages.tesco.net/J.deBoynePollard/FGA/dont-set-shared-file-descriptors-to-non-blocking-mode.html)
302 so we kludge around this by spawning a thread, which waits on a control pipe
303 for a signal, upon receiving this signal it reads one block of data from stdin
304 and writes it to a data pipe. Upon completion, it writes a 4-byte integer to
305 the size pipe, indicating how much data was written to the data pipe.
307 The read end of the size pipe can be set to non-blocking. */
319 void safe_close(int fd)
322 fatal_error("error closing fd",errno);
325 bool check_write(int fd, void *data, ssize_t size)
327 if(write(fd,data,size) == size)
332 return check_write(fd,data,size);
338 void safe_write(int fd, void *data, ssize_t size)
340 if(!check_write(fd,data,size))
341 fatal_error("error writing fd",errno);
344 bool safe_read(int fd, void *data, ssize_t size)
346 ssize_t bytes = read(fd,data,size);
350 return safe_read(fd,data,size);
353 fatal_error("error reading fd",errno);
358 return (bytes == size);
361 void *stdin_loop(void *arg)
363 unsigned char buf[4096];
364 bool loop_running = true;
368 pthread_sigmask(SIG_BLOCK, &mask, NULL);
372 if(!safe_read(control_read,buf,1))
376 fatal_error("stdin_loop: bad data on control fd",buf[0]);
380 ssize_t bytes = read(0,buf,sizeof(buf));
387 loop_running = false;
393 safe_write(size_write,&bytes,sizeof(bytes));
395 if(!check_write(stdin_write,buf,bytes))
396 loop_running = false;
402 safe_close(stdin_write);
403 safe_close(control_read);
408 void safe_pipe(int *in, int *out)
412 if(pipe(filedes) < 0)
413 fatal_error("Error opening pipe",errno);
418 if(fcntl(*in,F_SETFD,FD_CLOEXEC) < 0)
419 fatal_error("Error with fcntl",errno);
421 if(fcntl(*out,F_SETFD,FD_CLOEXEC) < 0)
422 fatal_error("Error with fcntl",errno);
427 safe_pipe(&control_read,&control_write);
428 safe_pipe(&size_read,&size_write);
429 safe_pipe(&stdin_read,&stdin_write);
430 start_thread(stdin_loop,NULL);