1.0.26.18: Solaris x86-64 support
[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 || defined LISP_FEATURE_X86_64
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         kludge_mmap_fd = open("/dev/zero",O_RDONLY);
71         if (kludge_mmap_fd < 0) {
72             perror("open");
73             lose("Error in open(..)\n");
74         }
75     } else if (minor_version > 11) {
76         FSHOW((stderr, "os_init: Solaris version greater than 11?\nUnknown MAP_ANON behaviour.\n"));
77         lose("Unknown mmap() interaction with MAP_ANON\n");
78     } else {
79         /* Versions 8-11*/
80         KLUDGE_MAYBE_MAP_ANON = 0x100;
81     }
82
83     /* I do not understand this at all. FIXME. */
84     os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
85
86     if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
87         fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
88                 os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
89         exit(1);
90     } else {
91         /*
92          * we do this because there are apparently dependencies on
93          * the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
94          * but since the OS doesn't know we're using this restriction,
95          * we have to grovel around a bit to enforce it, thus anything
96          * that uses real_page_size_difference.
97          */
98         /* FIXME: Is this still true? */
99         real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
100         os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
101     }
102 }
103
104 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
105 {
106     int flags = MAP_PRIVATE | MAP_NORESERVE | KLUDGE_MAYBE_MAP_ANON;
107     if (addr)
108         flags |= MAP_FIXED;
109
110     addr = mmap(addr, len,
111                 OS_VM_PROT_ALL,
112                 flags,
113                 kludge_mmap_fd, 0);
114
115     if (addr == MAP_FAILED) {
116         perror("mmap");
117         lose ("Error in mmap(..)\n");
118     }
119
120     return addr;
121 }
122
123 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
124 {
125     if(munmap((void*) addr, len) == -1)
126         perror("munmap");
127 }
128
129 \f
130
131 os_vm_address_t
132 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
133 {
134
135     addr = mmap(addr, len,
136                 OS_VM_PROT_ALL,
137                 MAP_PRIVATE | MAP_FIXED,
138                 fd, (off_t) offset);
139
140     if (addr == MAP_FAILED) {
141         perror("mmap");
142         lose("Unexpedted mmap(..) failure\n");
143     }
144
145     return addr;
146 }
147
148 void
149 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
150 {
151     if(mprotect((void*)address, length, prot) == -1) {
152         perror("mprotect");
153     }
154 }
155
156 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
157 {
158     char* beg = (char*) sbeg;
159     char* end = (char*) sbeg + slen;
160     char* adr = (char*) a;
161     return (adr >= beg && adr < end);
162 }
163
164 boolean is_valid_lisp_addr(os_vm_address_t addr)
165 {
166     /* Old CMUCL comment:
167
168        Just assume address is valid if it lies within one of the known
169        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
170
171     /* FIXME: this looks like a valid definition for all targets with
172        cheney-gc; it may not be impressively smart (witness the
173        comment above) but maybe associating these functions with the
174        GC rather than the OS would be a maintainability win.  -- CSR,
175        2003-04-04 */
176     struct thread *th;
177     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
178        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
179 #ifdef LISP_FEATURE_GENCGC
180        in_range_p(addr, DYNAMIC_SPACE_START  , dynamic_space_size)
181 #else
182        in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
183        in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
184 #endif
185        )
186         return 1;
187     for_each_thread(th) {
188         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
189             return 1;
190         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
191             return 1;
192     }
193     return 0;
194 }
195 \f
196
197 #if defined LISP_FEATURE_GENCGC
198
199 void
200 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
201 {
202     void* fault_addr = (void*)info->si_addr;
203
204     if (!gencgc_handle_wp_violation(fault_addr))
205         if(!handle_guard_page_triggered(context, fault_addr))
206             lisp_memory_fault_error(context, fault_addr);
207 }
208
209 #else
210
211 static void
212 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
213 {
214     os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
215
216     if (!cheneygc_handle_wp_violation(context, addr)) {
217         if (!handle_guard_page_triggered(context,addr))
218             lisp_memory_fault_error(context, fault_addr);
219     }
220 }
221
222 #endif
223
224 void
225 os_install_interrupt_handlers()
226 {
227     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
228                                                  sigsegv_handler);
229
230 #ifdef LISP_FEATURE_SB_THREAD
231     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
232                                                  sig_stop_for_gc_handler);
233 #endif
234 }
235
236 char *
237 os_get_runtime_executable_path()
238 {
239     int ret;
240     char path[] = "/proc/self/object/a.out";
241
242     ret = access(path, R_OK);
243     if (ret == -1)
244         return NULL;
245
246     return copied_string(path);
247 }
248