dc58ee4e626c70c168ae2bbd7150c1497d9ed3e4
[sbcl.git] / src / runtime / sunos-os.c
1 #include <stdio.h>
2
3 #include <signal.h>
4 #include <sys/file.h>
5
6 #include <unistd.h>
7 #include <errno.h>
8 #include <sys/param.h>
9
10 #include "os.h"
11 #include "arch.h"
12 #include "interr.h"
13 #include "interrupt.h"
14 #include "globals.h"
15 #include "validate.h"
16 #include "sbcl.h"
17 #include "target-arch-os.h"
18
19 #define OS_VM_DEFAULT_PAGESIZE 8192
20
21 long os_vm_page_size=(-1);
22 static long os_real_page_size=(-1);
23
24 static os_vm_size_t real_page_size_difference=0;
25
26 void
27 os_init(void)
28 {
29     /* I do not understand this at all. FIXME. */
30     os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
31
32     if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
33         fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
34                 os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
35         exit(1);
36     }else{
37         /*
38          * we do this because there are apparently dependencies on
39          * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
40          * but since the OS doesn't know we're using this restriction,
41          * we have to grovel around a bit to enforce it, thus anything
42          * that uses real_page_size_difference.
43          */
44         /* FIXME: Is this still true? */
45         real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
46         os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
47     }
48 }
49
50 os_vm_address_t
51 os_validate(os_vm_address_t addr, os_vm_size_t len)
52 {
53     int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANON;
54     
55     if (addr) 
56         flags |= MAP_FIXED;
57     
58     addr = mmap(addr, len, 
59                 OS_VM_PROT_ALL, 
60                 flags, 
61                 -1, 0);
62     if (addr == MAP_FAILED) {
63         perror("mmap");
64         lose ("Error in mmap(..)");
65     }
66     
67     return addr;
68 }
69
70 void
71 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
72 {
73     if(munmap((void*) addr, len) == -1)
74         perror("munmap");
75 }
76
77 \f
78
79 os_vm_address_t
80 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
81 {
82
83     addr = mmap(addr, len,
84                 OS_VM_PROT_ALL,
85                 MAP_PRIVATE | MAP_FIXED,
86                 fd, (off_t) offset);
87
88     if (addr == MAP_FAILED) {
89         perror("mmap");
90         lose("Unexpedted mmap(..) failure");
91     }
92   
93     return addr;
94 }
95
96 void
97 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
98 {
99     if(mprotect((void*)address, length, prot) == -1) {
100         perror("mprotect");
101     }
102 }
103
104 static boolean
105 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
106 {
107     char* beg = (char*) sbeg;
108     char* end = (char*) sbeg + slen;
109     char* adr = (char*) a;
110     return (adr >= beg && adr < end);
111 }
112
113 boolean
114 is_valid_lisp_addr(os_vm_address_t addr)
115 {
116     /* Just assume address is valid if it lies within one of the known
117        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
118     return (   in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
119                || in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE   )
120                || in_range_p(addr, DYNAMIC_0_SPACE_START, DYNAMIC_SPACE_SIZE  )
121                || in_range_p(addr, DYNAMIC_1_SPACE_START, DYNAMIC_SPACE_SIZE  )
122                || in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE  )
123                || in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE  ));
124 }
125
126 \f
127
128 #if defined GENCGC
129
130 #error "GENCGC is not yet supported (presumably on x86 solaris?)"
131
132 #else
133
134 static void
135 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
136 {
137     os_context_t *context = arch_os_get_context(&void_context);
138     os_vm_address_t addr;
139
140     addr = arch_get_bad_addr(signal, info, context);
141         /* There's some complicated recovery code in linux-os.c here
142            that I'm currently too confused to understand. Fixme. */
143     if(!interrupt_maybe_gc(signal, info, context)) {
144         interrupt_handle_now(signal, info, context);
145     }
146 }
147
148 #endif
149
150 void
151 os_install_interrupt_handlers()
152 {
153     undoably_install_low_level_interrupt_handler(SIGSEGV,sigsegv_handler);
154 }