0.9.7.12:
[sbcl.git] / src / runtime / sunos-os.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <sys/file.h>
5
6 #include <unistd.h>
7 #include <errno.h>
8 #include <sys/param.h>
9 #include <sys/utsname.h>
10
11 #include "sbcl.h"
12 #include "os.h"
13 #include "arch.h"
14 #include "interr.h"
15 #include "interrupt.h"
16 #include "globals.h"
17 #include "validate.h"
18 #include "target-arch-os.h"
19
20 #ifdef LISP_FEATURE_X86
21 #include "genesis/static-symbols.h"
22 #include "genesis/fdefn.h"
23 #endif
24
25 #ifdef LISP_FEATURE_GENCGC
26 #include "gencgc-internal.h"
27 #endif
28
29 #if defined LISP_FEATURE_SPARC
30 #define OS_VM_DEFAULT_PAGESIZE 8192
31 #elif defined LISP_FEATURE_X86
32 #define OS_VM_DEFAULT_PAGESIZE 4096
33 #else
34 #error "Don't know OS_VM_DEFAULT_PAGESIZE"
35 #endif
36
37 long os_vm_page_size=(-1);
38 static long os_real_page_size=(-1);
39
40 static os_vm_size_t real_page_size_difference=0;
41
42 /* So, this sucks. Versions of Solaris prior to 8 (SunOS releases
43    earlier than 5.8) do not support MAP_ANON passed as a flag to
44    mmap(). However, we would like SBCL compiled on SunOS 5.7 but
45    running on 5.8 to use MAP_ANON, but because of C's lack of
46    introspection at runtime, we can't grab the right value because
47    it's stuffed in a header file somewhere. We can, however, hardcode
48    it, and test at runtime for whether to use it... -- CSR, 2002-05-06
49
50    And, in fact, it sucks slightly more, as if you don't use MAP_ANON
51    you need to have /dev/zero open and pass the file descriptor to
52    mmap().  So overall, this counts as a KLUDGE. -- CSR, 2002-05-20 */
53 int KLUDGE_MAYBE_MAP_ANON = 0x0;
54 int kludge_mmap_fd = -1; /* default for MAP_ANON */
55
56 void
57 os_init(char *argv[], char *envp[])
58 {
59     struct utsname name;
60     int major_version;
61     int minor_version;
62
63     uname(&name);
64     major_version = atoi(name.release);
65     if (major_version != 5) {
66         lose("sunos major version=%d (which isn't 5!)\n", major_version);
67     }
68     minor_version = atoi(name.release+2);
69     if ((minor_version == 8) ||
70         (minor_version == 9) ||
71         (minor_version == 10)) {
72         KLUDGE_MAYBE_MAP_ANON = 0x100;
73     } else if (minor_version > 10) {
74         FSHOW((stderr, "os_init: Solaris version greater than 9?\nUnknown MAP_ANON behaviour.\n"));
75         lose("Unknown mmap() interaction with MAP_ANON\n");
76     } else { /* minor_version < 8 */
77         kludge_mmap_fd = open("/dev/zero",O_RDONLY);
78         if (kludge_mmap_fd < 0) {
79             perror("open");
80             lose("Error in open(..)\n");
81         }
82     }
83
84     /* I do not understand this at all. FIXME. */
85     os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
86
87     if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
88         fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
89                 os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
90         exit(1);
91     } else {
92         /*
93          * we do this because there are apparently dependencies on
94          * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
95          * but since the OS doesn't know we're using this restriction,
96          * we have to grovel around a bit to enforce it, thus anything
97          * that uses real_page_size_difference.
98          */
99         /* FIXME: Is this still true? */
100         real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
101         os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
102     }
103 }
104
105 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
106 {
107     int flags = MAP_PRIVATE | MAP_NORESERVE | KLUDGE_MAYBE_MAP_ANON;
108     if (addr)
109         flags |= MAP_FIXED;
110
111     addr = mmap(addr, len,
112                 OS_VM_PROT_ALL,
113                 flags,
114                 kludge_mmap_fd, 0);
115
116     if (addr == MAP_FAILED) {
117         perror("mmap");
118         lose ("Error in mmap(..)\n");
119     }
120
121     return addr;
122 }
123
124 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
125 {
126     if(munmap((void*) addr, len) == -1)
127         perror("munmap");
128 }
129
130 \f
131
132 os_vm_address_t
133 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
134 {
135
136     addr = mmap(addr, len,
137                 OS_VM_PROT_ALL,
138                 MAP_PRIVATE | MAP_FIXED,
139                 fd, (off_t) offset);
140
141     if (addr == MAP_FAILED) {
142         perror("mmap");
143         lose("Unexpedted mmap(..) failure\n");
144     }
145
146     return addr;
147 }
148
149 void
150 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
151 {
152     if(mprotect((void*)address, length, prot) == -1) {
153         perror("mprotect");
154     }
155 }
156
157 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
158 {
159     char* beg = (char*) sbeg;
160     char* end = (char*) sbeg + slen;
161     char* adr = (char*) a;
162     return (adr >= beg && adr < end);
163 }
164
165 boolean is_valid_lisp_addr(os_vm_address_t addr)
166 {
167     /* Old CMUCL comment:
168
169        Just assume address is valid if it lies within one of the known
170        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
171
172     /* FIXME: this looks like a valid definition for all targets with
173        cheney-gc; it may not be impressively smart (witness the
174        comment above) but maybe associating these functions with the
175        GC rather than the OS would be a maintainability win.  -- CSR,
176        2003-04-04 */
177     struct thread *th;
178     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
179        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
180 #ifdef LISP_FEATURE_GENCGC
181        in_range_p(addr, DYNAMIC_SPACE_START, DYNAMIC_SPACE_SIZE)
182 #else
183        in_range_p(addr, DYNAMIC_0_SPACE_START, DYNAMIC_SPACE_SIZE) ||
184        in_range_p(addr, DYNAMIC_1_SPACE_START, DYNAMIC_SPACE_SIZE)
185 #endif
186        )
187         return 1;
188     for_each_thread(th) {
189         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
190             return 1;
191         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
192             return 1;
193     }
194     return 0;
195 }
196 \f
197
198 #if defined LISP_FEATURE_GENCGC
199
200 void
201 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
202 {
203     os_context_t *context = arch_os_get_context(&void_context);
204     void* fault_addr = (void*)info->si_addr;
205     if(info->si_code == 1)
206     {
207         perror("error: SEGV_MAPERR\n");
208         exit(1);
209     }
210
211     if (!gencgc_handle_wp_violation(fault_addr))
212          if(!handle_guard_page_triggered(context, fault_addr))
213 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
214             arrange_return_to_lisp_function(context,
215                                             SymbolFunction(MEMORY_FAULT_ERROR));
216 #else
217     interrupt_handle_now(signal, info, context);
218 #endif
219 }
220
221 #else
222
223 static void
224 sigsegv_handler(int signal, siginfo_t *info, void* void_context)
225 {
226     os_context_t *context = arch_os_get_context(&void_context);
227     os_vm_address_t addr;
228
229     addr = arch_get_bad_addr(signal, info, context);
230     if(!interrupt_maybe_gc(signal, info, context)) {
231         if(!handle_guard_page_triggered(context,addr))
232             interrupt_handle_now(signal, info, context);
233     }
234 }
235
236 #endif
237
238 void
239 os_install_interrupt_handlers()
240 {
241     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
242                                                  sigsegv_handler);
243 }