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