]> gitweb.factorcode.org Git - factor.git/blob - vm/os-unix.c
Initial import
[factor.git] / vm / os-unix.c
1 #include "master.h"
2
3 static void *null_dll;
4
5 s64 current_millis(void)
6 {
7         struct timeval t;
8         gettimeofday(&t,NULL);
9         return (s64)t.tv_sec * 1000 + t.tv_usec / 1000;
10 }
11
12 void sleep_millis(CELL msec)
13 {
14         usleep(msec * 1000);
15 }
16
17 void init_ffi(void)
18 {
19         /* NULL_DLL is "libfactor.dylib" for OS X and NULL for generic unix */
20         null_dll = dlopen(NULL_DLL,RTLD_LAZY);
21 }
22
23 void ffi_dlopen(F_DLL *dll, bool error)
24 {
25         void *dllptr = dlopen(alien_offset(dll->path), RTLD_LAZY);
26
27         if(dllptr == NULL)
28         {
29                 if(error)
30                 {
31                         general_error(ERROR_FFI,F,
32                                 tag_object(from_char_string(dlerror())),
33                                 NULL);
34                 }
35                 else
36                         dll->dll = NULL;
37
38                 return;
39         }
40
41         dll->dll = dllptr;
42 }
43
44 void *ffi_dlsym(F_DLL *dll, F_SYMBOL *symbol)
45 {
46         void *handle = (dll == NULL ? null_dll : dll->dll);
47         return dlsym(handle,symbol);
48 }
49
50 void ffi_dlclose(F_DLL *dll)
51 {
52         if(dlclose(dll->dll))
53         {
54                 general_error(ERROR_FFI,tag_object(
55                         from_char_string(dlerror())),F,NULL);
56         }
57         dll->dll = NULL;
58 }
59
60 DEFINE_PRIMITIVE(stat)
61 {
62         struct stat sb;
63
64         if(stat(unbox_char_string(),&sb) < 0)
65         {
66                 dpush(F);
67                 dpush(F);
68                 dpush(F);
69                 dpush(F);
70         }
71         else
72         {
73                 box_boolean(S_ISDIR(sb.st_mode));
74                 box_signed_4(sb.st_mode & ~S_IFMT);
75                 box_unsigned_8(sb.st_size);
76                 box_unsigned_8(sb.st_mtime);
77         }
78 }
79
80 /* Allocates memory */
81 CELL parse_dir_entry(struct dirent *file)
82 {
83         CELL name = tag_object(from_char_string(file->d_name));
84         if(UNKNOWN_TYPE_P(file))
85                 return name;
86         else
87         {
88                 CELL dirp = tag_boolean(DIRECTORY_P(file));
89                 return allot_array_2(name,dirp);
90         }
91 }
92
93 DEFINE_PRIMITIVE(read_dir)
94 {
95         DIR* dir = opendir(unbox_char_string());
96         GROWABLE_ARRAY(result);
97
98         if(dir != NULL)
99         {
100                 struct dirent* file;
101
102                 while((file = readdir(dir)) != NULL)
103                 {
104                         REGISTER_UNTAGGED(result);
105                         CELL pair = parse_dir_entry(file);
106                         UNREGISTER_UNTAGGED(result);
107                         GROWABLE_ADD(result,pair);
108                 }
109
110                 closedir(dir);
111         }
112
113         GROWABLE_TRIM(result);
114
115         dpush(tag_object(result));
116 }
117
118 DEFINE_PRIMITIVE(cwd)
119 {
120         char wd[MAXPATHLEN];
121         if(getcwd(wd,MAXPATHLEN) == NULL)
122                 io_error();
123         box_char_string(wd);
124 }
125
126 DEFINE_PRIMITIVE(cd)
127 {
128         chdir(unbox_char_string());
129 }
130
131 F_SEGMENT *alloc_segment(CELL size)
132 {
133         int pagesize = getpagesize();
134
135         char *array = mmap(NULL,pagesize + size + pagesize,
136                 PROT_READ | PROT_WRITE | PROT_EXEC,
137                 MAP_ANON | MAP_PRIVATE,-1,0);
138
139         if(array == (char*)-1)
140                 fatal_error("Out of memory in alloc_segment",0);
141
142         if(mprotect(array,pagesize,PROT_NONE) == -1)
143                 fatal_error("Cannot protect low guard page",(CELL)array);
144
145         if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
146                 fatal_error("Cannot protect high guard page",(CELL)array);
147
148         F_SEGMENT *retval = safe_malloc(sizeof(F_SEGMENT));
149
150         retval->start = (CELL)(array + pagesize);
151         retval->size = size;
152         retval->end = retval->start + size;
153
154         return retval;
155 }
156
157 void dealloc_segment(F_SEGMENT *block)
158 {
159         int pagesize = getpagesize();
160
161         int retval = munmap((void*)(block->start - pagesize),
162                 pagesize + block->size + pagesize);
163         
164         if(retval)
165                 fatal_error("dealloc_segment failed",0);
166
167         free(block);
168 }
169   
170 INLINE F_STACK_FRAME *uap_stack_pointer(void *uap)
171 {
172         /* There is a race condition here, but in practice a signal
173         delivered during stack frame setup/teardown or while transitioning
174         from Factor to C is a sign of things seriously gone wrong, not just
175         a divide by zero or stack underflow in the listener */
176         if(in_code_heap_p(UAP_PROGRAM_COUNTER(uap)))
177                 return ucontext_stack_pointer(uap);
178         else
179                 return NULL;
180 }
181
182 void memory_signal_handler_impl(void)
183 {
184         memory_protection_error(signal_fault_addr,signal_callstack_top);
185 }
186
187 void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap)
188 {
189         signal_fault_addr = (CELL)siginfo->si_addr;
190         signal_callstack_top = uap_stack_pointer(uap);
191         UAP_PROGRAM_COUNTER(uap) = (CELL)memory_signal_handler_impl;
192 }
193
194 void misc_signal_handler_impl(void)
195 {
196         signal_error(signal_number,signal_callstack_top);
197 }
198
199 void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap)
200 {
201         signal_number = signal;
202         signal_callstack_top = uap_stack_pointer(uap);
203         UAP_PROGRAM_COUNTER(uap) = (CELL)misc_signal_handler_impl;
204 }
205
206 static void sigaction_safe(int signum, const struct sigaction *act, struct sigaction *oldact)
207 {
208         int ret;
209         do
210         {
211                 ret = sigaction(signum, act, oldact);
212         }
213         while(ret == -1 && errno == EINTR);
214 }
215
216 void unix_init_signals(void)
217 {
218         struct sigaction memory_sigaction;
219         struct sigaction misc_sigaction;
220         struct sigaction ignore_sigaction;
221
222         memset(&memory_sigaction,0,sizeof(struct sigaction));
223         sigemptyset(&memory_sigaction.sa_mask);
224         memory_sigaction.sa_sigaction = memory_signal_handler;
225         memory_sigaction.sa_flags = SA_SIGINFO;
226
227         sigaction_safe(SIGBUS,&memory_sigaction,NULL);
228         sigaction_safe(SIGSEGV,&memory_sigaction,NULL);
229
230         memset(&misc_sigaction,0,sizeof(struct sigaction));
231         sigemptyset(&misc_sigaction.sa_mask);
232         misc_sigaction.sa_sigaction = misc_signal_handler;
233         misc_sigaction.sa_flags = SA_SIGINFO;
234
235         sigaction_safe(SIGABRT,&misc_sigaction,NULL);
236         sigaction_safe(SIGFPE,&misc_sigaction,NULL);
237         sigaction_safe(SIGQUIT,&misc_sigaction,NULL);
238         sigaction_safe(SIGILL,&misc_sigaction,NULL);
239
240         memset(&ignore_sigaction,0,sizeof(struct sigaction));
241         sigemptyset(&ignore_sigaction.sa_mask);
242         ignore_sigaction.sa_handler = SIG_IGN;
243         sigaction_safe(SIGPIPE,&ignore_sigaction,NULL);
244 }
245
246 void reset_stdio(void)
247 {
248         fcntl(0,F_SETFL,0);
249         fcntl(1,F_SETFL,0);
250 }