forked from factor/factor
/
os-unix.cpp
executable file
·433 lines (363 loc) · 10.2 KB
/
os-unix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "master.hpp"
namespace factor
{
THREADHANDLE start_thread(void *(*start_routine)(void *),void *args)
{
pthread_attr_t attr;
pthread_t thread;
if (pthread_attr_init (&attr) != 0)
fatal_error("pthread_attr_init() failed",0);
if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) != 0)
fatal_error("pthread_attr_setdetachstate() failed",0);
if (pthread_create (&thread, &attr, start_routine, args) != 0)
fatal_error("pthread_create() failed",0);
pthread_attr_destroy(&attr);
return thread;
}
static void *null_dll;
void sleep_nanos(u64 nsec)
{
timespec ts;
timespec ts_rem;
int ret;
ts.tv_sec = nsec / 1000000000;
ts.tv_nsec = nsec % 1000000000;
ret = nanosleep(&ts,&ts_rem);
while(ret == -1 && errno == EINTR)
{
memcpy(&ts, &ts_rem, sizeof(ts));
ret = nanosleep(&ts, &ts_rem);
}
if(ret == -1)
fatal_error("nanosleep failed", 0);
}
void factor_vm::init_ffi()
{
null_dll = dlopen(NULL,RTLD_LAZY);
}
void factor_vm::ffi_dlopen(dll *dll)
{
dll->handle = dlopen(alien_offset(dll->path), RTLD_LAZY);
}
void *factor_vm::ffi_dlsym_raw(dll *dll, symbol_char *symbol)
{
return dlsym(dll ? dll->handle : null_dll, symbol);
}
void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
{
return FUNCTION_CODE_POINTER(ffi_dlsym_raw(dll, symbol));
}
#ifdef FACTOR_PPC
void *factor_vm::ffi_dlsym_toc(dll *dll, symbol_char *symbol)
{
return FUNCTION_TOC_POINTER(ffi_dlsym_raw(dll, symbol));
}
#endif
void factor_vm::ffi_dlclose(dll *dll)
{
if(dlclose(dll->handle))
general_error(ERROR_FFI,false_object,false_object);
dll->handle = NULL;
}
void factor_vm::primitive_existsp()
{
struct stat sb;
char *path = (char *)(untag_check<byte_array>(ctx->pop()) + 1);
ctx->push(tag_boolean(stat(path,&sb) >= 0));
}
void factor_vm::move_file(const vm_char *path1, const vm_char *path2)
{
int ret = 0;
do
{
ret = rename((path1),(path2));
}
while(ret < 0 && errno == EINTR);
if(ret < 0)
general_error(ERROR_IO,tag_fixnum(errno),false_object);
}
segment::segment(cell size_, bool executable_p)
{
size = size_;
int pagesize = getpagesize();
int prot;
if(executable_p)
prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
else
prot = (PROT_READ | PROT_WRITE);
char *array = (char *)mmap(NULL,pagesize + size + pagesize,prot,MAP_ANON | MAP_PRIVATE,-1,0);
if(array == (char*)-1) out_of_memory();
if(mprotect(array,pagesize,PROT_NONE) == -1)
fatal_error("Cannot protect low guard page",(cell)array);
if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
fatal_error("Cannot protect high guard page",(cell)array);
start = (cell)(array + pagesize);
end = start + size;
}
segment::~segment()
{
int pagesize = getpagesize();
int retval = munmap((void*)(start - pagesize),pagesize + size + pagesize);
if(retval)
fatal_error("Segment deallocation failed",0);
}
void code_heap::guard_safepoint()
{
if(mprotect(safepoint_page,getpagesize(),PROT_NONE) == -1)
fatal_error("Cannot protect safepoint guard page",(cell)safepoint_page);
}
void code_heap::unguard_safepoint()
{
if(mprotect(safepoint_page,getpagesize(),PROT_WRITE) == -1)
fatal_error("Cannot unprotect safepoint guard page",(cell)safepoint_page);
}
void factor_vm::dispatch_signal(void *uap, void (handler)())
{
dispatch_signal_handler(
(cell*)&UAP_STACK_POINTER(uap),
(cell*)&UAP_PROGRAM_COUNTER(uap),
(cell)FUNCTION_CODE_POINTER(handler)
);
UAP_SET_TOC_POINTER(uap, (cell)FUNCTION_TOC_POINTER(handler));
}
void factor_vm::enqueue_safepoint_signal(cell signal)
{
/* to be implemented, see #297
code->guard_safepoint();
*/
}
void factor_vm::start_sampling_profiler_timer()
{
struct itimerval timer;
memset((void*)&timer, 0, sizeof(struct itimerval));
timer.it_value.tv_usec = 1000000/FACTOR_PROFILE_SAMPLES_PER_SECOND;
timer.it_interval.tv_usec = 1000000/FACTOR_PROFILE_SAMPLES_PER_SECOND;
setitimer(ITIMER_REAL, &timer, NULL);
}
void factor_vm::end_sampling_profiler_timer()
{
struct itimerval timer;
memset((void*)&timer, 0, sizeof(struct itimerval));
setitimer(ITIMER_REAL, &timer, NULL);
}
void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm();
vm->signal_fault_addr = (cell)siginfo->si_addr;
vm->dispatch_signal(uap,factor::memory_signal_handler_impl);
}
void synchronous_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm_p();
if (vm)
{
vm->signal_number = signal;
vm->dispatch_signal(uap,factor::synchronous_signal_handler_impl);
} else
fatal_error("Foreign thread received signal", signal);
}
void enqueue_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm_p();
if (vm)
vm->enqueue_safepoint_signal(signal);
else
fatal_error("Foreign thread received signal", signal);
}
void fep_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm_p();
if (vm)
vm->enqueue_safepoint_fep();
else
fatal_error("Foreign thread received signal", signal);
}
void sample_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm_p();
if (vm)
vm->enqueue_safepoint_sample();
else
fatal_error("Foreign thread received signal", signal);
}
void ignore_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
}
void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap)
{
factor_vm *vm = current_vm();
vm->signal_number = signal;
vm->signal_fpu_status = fpu_status(uap_fpu_status(uap));
uap_clear_fpu_status(uap);
vm->dispatch_signal(uap,
(siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF)
? factor::synchronous_signal_handler_impl
: factor::fp_signal_handler_impl);
}
static void sigaction_safe(int signum, const struct sigaction *act, struct sigaction *oldact)
{
int ret;
do
{
ret = sigaction(signum, act, oldact);
}
while(ret == -1 && errno == EINTR);
if(ret == -1)
fatal_error("sigaction failed", 0);
}
static void init_sigaction_with_handler(struct sigaction *act,
void (*handler)(int, siginfo_t*, void*))
{
memset(act, 0, sizeof(struct sigaction));
sigemptyset(&act->sa_mask);
act->sa_sigaction = handler;
act->sa_flags = SA_SIGINFO | SA_ONSTACK;
}
void factor_vm::unix_init_signals()
{
struct sigaction memory_sigaction;
struct sigaction synchronous_sigaction;
struct sigaction enqueue_sigaction;
struct sigaction fep_sigaction;
struct sigaction sample_sigaction;
struct sigaction fpe_sigaction;
struct sigaction ignore_sigaction;
init_sigaction_with_handler(&memory_sigaction, memory_signal_handler);
sigaction_safe(SIGBUS,&memory_sigaction,NULL);
sigaction_safe(SIGSEGV,&memory_sigaction,NULL);
sigaction_safe(SIGTRAP,&memory_sigaction,NULL);
init_sigaction_with_handler(&fpe_sigaction, fpe_signal_handler);
sigaction_safe(SIGFPE,&fpe_sigaction,NULL);
init_sigaction_with_handler(&synchronous_sigaction, synchronous_signal_handler);
sigaction_safe(SIGILL,&synchronous_sigaction,NULL);
sigaction_safe(SIGABRT,&synchronous_sigaction,NULL);
init_sigaction_with_handler(&enqueue_sigaction, enqueue_signal_handler);
sigaction_safe(SIGUSR1,&enqueue_sigaction,NULL);
sigaction_safe(SIGUSR2,&enqueue_sigaction,NULL);
sigaction_safe(SIGWINCH,&enqueue_sigaction,NULL);
#ifdef SIGINFO
sigaction_safe(SIGINFO,&enqueue_sigaction,NULL);
#endif
init_sigaction_with_handler(&fep_sigaction, fep_signal_handler);
sigaction_safe(SIGQUIT,&fep_sigaction,NULL);
sigaction_safe(SIGINT,&fep_sigaction,NULL);
init_sigaction_with_handler(&sample_sigaction, sample_signal_handler);
sigaction_safe(SIGALRM,&sample_sigaction,NULL);
/* We don't use SA_IGN here because then the ignore action is inherited
by subprocesses, which we don't want. There is a unit test in
io.launcher.unix for this. */
init_sigaction_with_handler(&ignore_sigaction, ignore_signal_handler);
sigaction_safe(SIGPIPE,&ignore_sigaction,NULL);
}
/* On Unix, shared fds such as stdin cannot be set to non-blocking mode
(http://homepages.tesco.net/J.deBoynePollard/FGA/dont-set-shared-file-descriptors-to-non-blocking-mode.html)
so we kludge around this by spawning a thread, which waits on a control pipe
for a signal, upon receiving this signal it reads one block of data from stdin
and writes it to a data pipe. Upon completion, it writes a 4-byte integer to
the size pipe, indicating how much data was written to the data pipe.
The read end of the size pipe can be set to non-blocking. */
extern "C" {
int stdin_read;
int stdin_write;
int control_read;
int control_write;
int size_read;
int size_write;
}
void safe_close(int fd)
{
if(close(fd) < 0)
fatal_error("error closing fd",errno);
}
bool check_write(int fd, void *data, ssize_t size)
{
if(write(fd,data,size) == size)
return true;
else
{
if(errno == EINTR)
return check_write(fd,data,size);
else
return false;
}
}
void safe_write(int fd, void *data, ssize_t size)
{
if(!check_write(fd,data,size))
fatal_error("error writing fd",errno);
}
bool safe_read(int fd, void *data, ssize_t size)
{
ssize_t bytes = read(fd,data,size);
if(bytes < 0)
{
if(errno == EINTR)
return safe_read(fd,data,size);
else
{
fatal_error("error reading fd",errno);
return false;
}
}
else
return (bytes == size);
}
void *stdin_loop(void *arg)
{
unsigned char buf[4096];
bool loop_running = true;
sigset_t mask;
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
while(loop_running)
{
if(!safe_read(control_read,buf,1))
break;
if(buf[0] != 'X')
fatal_error("stdin_loop: bad data on control fd",buf[0]);
for(;;)
{
ssize_t bytes = read(0,buf,sizeof(buf));
if(bytes < 0)
{
if(errno == EINTR)
continue;
else
{
loop_running = false;
break;
}
}
else if(bytes >= 0)
{
safe_write(size_write,&bytes,sizeof(bytes));
if(!check_write(stdin_write,buf,bytes))
loop_running = false;
break;
}
}
}
safe_close(stdin_write);
safe_close(control_read);
return NULL;
}
void safe_pipe(int *in, int *out)
{
int filedes[2];
if(pipe(filedes) < 0)
fatal_error("Error opening pipe",errno);
*in = filedes[0];
*out = filedes[1];
if(fcntl(*in,F_SETFD,FD_CLOEXEC) < 0)
fatal_error("Error with fcntl",errno);
if(fcntl(*out,F_SETFD,FD_CLOEXEC) < 0)
fatal_error("Error with fcntl",errno);
}
void open_console()
{
safe_pipe(&control_read,&control_write);
safe_pipe(&size_read,&size_write);
safe_pipe(&stdin_read,&stdin_write);
start_thread(stdin_loop,NULL);
}
}