integrated Raymond Wiker's patches to port RUN-PROGRAM from CMU CL and
[sbcl.git] / src / runtime / bsd-os.c
1 /*
2  * OS-dependent routines for BSD-ish systems
3  *
4  * This file (along with os.h) exports an OS-independent interface to
5  * the operating system VM facilities. This interface looks a lot like
6  * the Mach interface (but simpler in some places). For some operating
7  * systems, a subset of these functions will have to be emulated.
8  */
9
10 /*
11  * This software is part of the SBCL system. See the README file for
12  * more information.
13  *
14  * This software is derived from the CMU CL system, which was
15  * written at Carnegie Mellon University and released into the
16  * public domain. The software is in the public domain and is
17  * provided with absolutely no warranty. See the COPYING and CREDITS
18  * files for more information.
19  */
20
21 /*
22  * $Header$
23  */
24
25 #include <stdio.h>
26 #include <sys/param.h>
27 #include <sys/file.h>
28 #include "./signal.h"
29 #include "os.h"
30 #include "arch.h"
31 #include "globals.h"
32 #include "interrupt.h"
33 #include "lispregs.h"
34 #include "sbcl.h"
35
36 #include <sys/types.h>
37 #include <signal.h>
38 /* #include <sys/sysinfo.h> */
39 #include <sys/proc.h>
40 #include "validate.h"
41 vm_size_t os_vm_page_size;
42
43 #if defined GENCGC
44 #include "gencgc.h"
45 #endif
46
47 /* The different BSD variants have diverged in exactly where they
48  * store signal context information, but at least they tend to use the
49  * same stems to name the structure fields, so by using this macro we
50  * can share a fair amount of code between different variants. */
51 #if defined __FreeBSD__
52 #define CONTEXT_ADDR_FROM_STEM(stem) &context->uc_mcontext.mc_ ## stem
53 #elif defined __OpenBSD__
54 #define CONTEXT_ADDR_FROM_STEM(stem) &context->sc_ ## stem
55 #else
56 #error unsupported BSD variant
57 #endif
58 \f
59 void
60 os_init(void)
61 {
62     os_vm_page_size = getpagesize();
63 }
64
65 /* KLUDGE: There is strong family resemblance in the signal context
66  * stuff in FreeBSD and OpenBSD, but in detail they're different in
67  * almost every line of code. It would be nice to find some way to
68  * factor out the commonality better; failing that, it might be best
69  * just to split this generic-BSD code into one variant for each BSD. */
70    
71 int *
72 os_context_register_addr(os_context_t *context, int offset)
73 {
74     switch(offset) {
75     case  0:
76         return CONTEXT_ADDR_FROM_STEM(eax);
77     case  2:
78         return CONTEXT_ADDR_FROM_STEM(ecx);
79     case  4:
80         return CONTEXT_ADDR_FROM_STEM(edx);
81     case  6:
82         return CONTEXT_ADDR_FROM_STEM(ebx);
83     case  8:
84         return CONTEXT_ADDR_FROM_STEM(esp);
85     case 10:
86         return CONTEXT_ADDR_FROM_STEM(ebp);
87     case 12:
88         return CONTEXT_ADDR_FROM_STEM(esi);
89     case 14:
90         return CONTEXT_ADDR_FROM_STEM(edi);
91     default:
92         return 0;
93     }
94 }
95
96 int *
97 os_context_pc_addr(os_context_t *context)
98 {
99 #if defined __FreeBSD__
100     return CONTEXT_ADDR_FROM_STEM(eip);
101 #elif defined __OpenBSD__
102     return CONTEXT_ADDR_FROM_STEM(pc);
103 #else
104 #error unsupported BSD variant
105 #endif
106 }
107
108 int *
109 os_context_sp_addr(os_context_t *context)
110 {
111     return CONTEXT_ADDR_FROM_STEM(esp);
112 }
113
114 sigset_t *
115 os_context_sigmask_addr(os_context_t *context)
116 {
117     /* (Unlike most of the other context fields that we access, the
118      * signal mask field is a field of the basic, outermost context
119      * struct itself both in FreeBSD 4.0 and in OpenBSD 2.6.) */
120 #if defined __FreeBSD__
121     return &context->uc_sigmask;
122 #elif defined __OpenBSD__
123     return &context->sc_mask;
124 #else
125 #error unsupported BSD variant
126 #endif
127 }
128
129 os_vm_address_t
130 os_validate(os_vm_address_t addr, os_vm_size_t len)
131 {
132     int flags = MAP_PRIVATE | MAP_ANON;
133
134     if (addr)
135         flags |= MAP_FIXED;
136
137     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
138
139     if (addr == MAP_FAILED) {
140         perror("mmap");
141         return NULL;
142     }
143
144     return addr;
145 }
146
147 void
148 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
149 {
150     if (munmap(addr, len) == -1)
151         perror("munmap");
152 }
153
154 os_vm_address_t
155 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
156 {
157     addr = mmap(addr, len,
158                 OS_VM_PROT_ALL,
159                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
160                 fd, (off_t) offset);
161
162     if (addr == MAP_FAILED) {
163         perror("mmap");
164         lose("unexpected mmap(..) failure");
165     }
166
167     return addr;
168 }
169
170 void
171 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
172 {
173 }
174
175 void
176 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
177 {
178     if (mprotect(address, length, prot) == -1) {
179         perror("mprotect");
180     }
181 }
182 \f
183 static boolean
184 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
185 {
186     char* beg = (char*) sbeg;
187     char* end = (char*) sbeg + slen;
188     char* adr = (char*) a;
189     return (adr >= beg && adr < end);
190 }
191
192 boolean
193 is_valid_lisp_addr(os_vm_address_t addr)
194 {
195     return in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
196         || in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE   )
197         || in_range_p(addr, DYNAMIC_0_SPACE_START, DYNAMIC_SPACE_SIZE  )
198         || in_range_p(addr, DYNAMIC_1_SPACE_START, DYNAMIC_SPACE_SIZE  )
199         || in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE  )
200         || in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE  );
201 }
202 \f
203 /*
204  * any OS-dependent special low-level handling for signals
205  */
206
207 #if !defined GENCGC
208
209 void
210 os_install_interrupt_handlers(void)
211 {}
212
213 #else
214
215 /*
216  * The GENCGC needs to be hooked into whatever signal is raised for
217  * page fault on this OS.
218  */
219 static void
220 memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context)
221 {
222     /* The way that we extract low level information like the fault
223      * address is not specified by POSIX. */
224 #if defined __FreeBSD__
225     void *fault_addr = siginfo->si_addr;
226 #elif defined __OpenBSD__
227     void *fault_addr = siginfo->si_addr;
228 #else
229 #error unsupported BSD variant
230 #endif
231     if (!gencgc_handle_wp_violation(fault_addr)) {
232         interrupt_handle_now(signal, siginfo, void_context);
233     }
234 }
235 void
236 os_install_interrupt_handlers(void)
237 {
238 #if defined __FreeBSD__
239     interrupt_install_low_level_handler(SIGBUS, memory_fault_handler);
240 #elif defined __OpenBSD__
241     interrupt_install_low_level_handler(SIGSEGV, memory_fault_handler);
242 #else
243 #error unsupported BSD variant
244 #endif
245 }
246
247 #endif /* !defined GENCGC */
248
249 /* feh!
250  *
251  * DL_WORKAROUND enables "stubbing" of various functions from libc et
252  * al. This is necessary when using dynamic linking in FreeBSD, as the
253  * symbols in the dynamic libraries will not have known addresses (in
254  * sbcl.nm).
255  *
256  * FIXME: This flag should be set in Config.bsd */
257 #define DL_WORKAROUND 1
258
259 #if DL_WORKAROUND
260 #include <unistd.h>
261 #include <dlfcn.h>
262 #include <math.h>
263 #include <sys/types.h>
264 #include <dirent.h>
265 #include <stdlib.h>
266 #include <sys/stat.h>
267 #include <time.h>
268 #include <sys/resource.h>
269 #include <signal.h>
270 #include <fcntl.h>
271
272 void *ldso_stub__dlopen(const char *path, int mode)
273 {
274   return dlopen(path, mode);
275 }
276
277 void *ldso_stub__dlsym(void *handle, const char *symbol)
278 {
279   return dlsym(handle, symbol);
280 }
281
282 const char *ldso_stub__dlerror(void)
283 {
284   return dlerror();
285 }
286 int ldso_stub__access(const char *path, int mode)
287 {
288   return access(path, mode);
289 }
290
291 double ldso_stub__acos(double x)
292 {
293   return acos(x);
294 }
295
296 double ldso_stub__acosh(double x)
297 {
298   return acosh(x);
299 }
300
301 double ldso_stub__asin(double x)
302 {
303   return asin(x);
304 }
305
306 double ldso_stub__asinh(double x)
307 {
308   return asin(x);
309 }
310
311 double ldso_stub__atanh(double x)
312 {
313   return atanh(x);
314 }
315
316
317 int ldso_stub__chdir(const char *path)
318 {
319   return chdir(path);
320 }
321
322 int ldso_stub__close(int d)
323 {
324   return close(d);
325 }
326
327 int ldso_stub__closedir(DIR *dirp)
328 {
329   return closedir(dirp);
330 }
331
332 double ldso_stub__cosh(double x)
333 {
334   return cosh(x);
335 }
336
337 void ldso_stub__exit(int status)
338 {
339   exit(status);
340 }
341
342 void ldso_stub__free(void *ptr)
343 {
344   free(ptr);
345 }
346
347 int ldso_stub__fstat(int fd, struct stat *sb)
348 {
349   return fstat(fd, sb);
350 }
351
352 int ldso_stub__fsync(int fd)
353 {
354   return fsync(fd);
355 }
356
357 char *ldso_stub__getenv(const char *name)
358 {
359   return getenv(name);
360 }
361
362 int ldso_stub__gethostname(char *name, int namelen)
363 {
364   return gethostname(name, namelen);
365 }
366
367 pid_t ldso_stub__getpid(void)
368 {
369   return getpid();
370 }
371
372 int ldso_stub__getrusage(int who, struct rusage *rusage)
373 {
374   return getrusage(who, rusage);
375 }
376
377 int ldso_stub__gettimeofday(struct timeval *tp, struct timezone *tzp)
378 {
379   return gettimeofday(tp, tzp);
380 }
381
382 uid_t ldso_stub__getuid(void)
383 {
384   return getuid();
385 }
386
387 char *ldso_stub__getwd(char *buf)
388 {
389   return getwd(buf);
390 }
391
392 double ldso_stub__hypot(double x, double y)
393 {
394   return hypot(x, y);
395 }
396
397 int ldso_stub__kill(pid_t pid, int sig)
398 {
399   return kill(pid, sig);
400 }
401
402 int ldso_stub__killpg(pid_t pgrp, int sig)
403 {
404   return killpg(pgrp, sig);
405 }
406
407 off_t ldso_stub__lseek(int fildes, off_t offset, int whence)
408 {
409   return lseek(fildes, offset, whence);
410 }
411
412 int ldso_stub__lstat(const char *path, struct stat *sb)
413 {
414   return lstat(path, sb);
415 }
416
417 void *ldso_stub__malloc(size_t size)
418 {
419   return malloc(size);
420 }
421
422 int ldso_stub__mkdir(const char *path, mode_t mode)
423 {
424   return mkdir(path, mode);
425 }
426
427 int ldso_stub__open(const char *path, int flags, mode_t mode)
428 {
429   return open(path, flags, mode);
430 }
431
432 DIR *ldso_stub__opendir(const char *filename)
433 {
434   return opendir(filename);
435 }
436
437 double ldso_stub__pow(double x, double y)
438 {
439   return pow(x, y);
440 }
441
442 ssize_t ldso_stub__read(int d, void *buf, size_t nbytes)
443 {
444   return read(d, buf, nbytes);
445 }
446
447 struct dirent *ldso_stub__readdir(DIR *dirp)
448 {
449   return readdir(dirp);
450 }
451
452 int ldso_stub__readlink(const char *path, char *buf, int bufsiz)
453 {
454   return readlink(path, buf, bufsiz);
455 }
456
457 int ldso_stub__rename(const char *from, const char *to)
458 {
459   return rename(from, to);
460 }
461
462 int ldso_stub__select(int nfds, fd_set *readfs, fd_set *writefds, 
463                       fd_set *exceptfds, struct timeval *timeout)
464 {
465   return select(nfds, readfs, writefds, exceptfds, timeout);
466 }
467
468 int ldso_stub__sigblock(int mask)
469 {
470   return sigblock(mask);
471 }
472
473 int ldso_stub__sigpause(int sigmask)
474 {
475   return sigpause(sigmask);
476 }
477
478 int ldso_stub__sigsetmask(int mask)
479 {
480   return sigsetmask(mask);
481 }
482
483 double ldso_stub__sinh(double x)
484 {
485   return sin(x);
486 }
487
488 int ldso_stub__stat(const char *path, struct stat *sb)
489 {
490   return stat(path, sb);
491 }
492
493 double ldso_stub__tanh(double x)
494 {
495   return tanh(x);
496 }
497
498 /* tzname */
499
500 int ldso_stub__unlink(const char *path)
501 {
502   return unlink(path);
503 }
504
505 ssize_t ldso_stub__write(int d, const void *buf, size_t nbytes)
506 {
507   return write(d, buf, nbytes);
508 }
509
510 pid_t ldso_stub__wait3(int *status, int options, struct rusage *rusage)
511 {
512   return wait3(status, options, rusage);
513 }
514
515 #endif /* DL_WORKAROUND */