cab00ba58cf50e3f0054bae4a0dd3d50538fbe00
[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 os_vm_size_t os_vm_page_size=0;
30
31 void
32 os_init(char *argv[], char *envp[])
33 {
34     /*
35      * historically, this used sysconf to select the runtime page size
36      * per recent changes on other arches and discussion on sbcl-devel,
37      * however, this is not necessary -- the VM page size need not match
38      * the OS page size (and the default backend page size has been
39      * ramped up accordingly for efficiency reasons).
40      */
41     os_vm_page_size = BACKEND_PAGE_BYTES;
42 }
43
44 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
45 {
46     int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANON;
47     if (addr)
48         flags |= MAP_FIXED;
49
50     addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
51
52     if (addr == MAP_FAILED) {
53         perror("mmap");
54         /* While it is generally hard to recover from out-of-memory
55          * situations, we require callers to decide on the right course
56          * of action here.  Consider thread creation: Failure to mmap
57          * here is common if users have started too many threads, and
58          * often we can recover from that and treat it as an ordinary
59          * error. */
60         return 0;
61     }
62
63     return addr;
64 }
65
66 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
67 {
68     if(munmap((void*) addr, len) == -1)
69         perror("munmap");
70 }
71
72 \f
73
74 os_vm_address_t
75 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
76 {
77
78     addr = mmap(addr, len,
79                 OS_VM_PROT_ALL,
80                 MAP_PRIVATE | MAP_FIXED,
81                 fd, (off_t) offset);
82
83     if (addr == MAP_FAILED) {
84         perror("mmap");
85         lose("Unexpedted mmap(..) failure\n");
86     }
87
88     return addr;
89 }
90
91 void
92 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
93 {
94     if(mprotect((void*)address, length, prot) == -1) {
95         perror("mprotect");
96     }
97 }
98
99 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
100 {
101     char* beg = (char*) sbeg;
102     char* end = (char*) sbeg + slen;
103     char* adr = (char*) a;
104     return (adr >= beg && adr < end);
105 }
106
107 boolean is_valid_lisp_addr(os_vm_address_t addr)
108 {
109     /* Old CMUCL comment:
110
111        Just assume address is valid if it lies within one of the known
112        spaces.  (Unlike sunos-os which keeps track of every valid page.) */
113
114     /* FIXME: this looks like a valid definition for all targets with
115        cheney-gc; it may not be impressively smart (witness the
116        comment above) but maybe associating these functions with the
117        GC rather than the OS would be a maintainability win.  -- CSR,
118        2003-04-04 */
119     struct thread *th;
120     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
121        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
122 #ifdef LISP_FEATURE_GENCGC
123        in_range_p(addr, DYNAMIC_SPACE_START  , dynamic_space_size)
124 #else
125        in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size) ||
126        in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
127 #endif
128        )
129         return 1;
130     for_each_thread(th) {
131         if((th->control_stack_start <= addr) && (addr < th->control_stack_end))
132             return 1;
133         if(in_range_p(addr, th->binding_stack_start, BINDING_STACK_SIZE))
134             return 1;
135     }
136     return 0;
137 }
138 \f
139
140 #if defined LISP_FEATURE_GENCGC
141
142 void
143 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
144 {
145     void* fault_addr = (void*)info->si_addr;
146
147 #ifdef LISP_FEATURE_SB_SAFEPOINT
148     if (handle_safepoint_violation(context, fault_addr))
149             return;
150 #endif
151
152     if (!gencgc_handle_wp_violation(fault_addr))
153         if(!handle_guard_page_triggered(context, fault_addr))
154             lisp_memory_fault_error(context, fault_addr);
155 }
156
157 #else
158
159 static void
160 sigsegv_handler(int signal, siginfo_t *info, os_context_t *context)
161 {
162     os_vm_address_t addr = arch_get_bad_addr(signal, info, context);
163
164     if (!cheneygc_handle_wp_violation(context, addr)) {
165         if (!handle_guard_page_triggered(context,addr))
166             lisp_memory_fault_error(context, addr);
167     }
168 }
169
170 #endif
171
172 void
173 os_install_interrupt_handlers()
174 {
175     undoably_install_low_level_interrupt_handler(SIG_MEMORY_FAULT,
176                                                  sigsegv_handler);
177
178     /* OAOOM c.f. linux-os.c.
179      * Should we have a reusable function gc_install_interrupt_handlers? */
180 #ifdef LISP_FEATURE_SB_THREAD
181 # ifdef LISP_FEATURE_SB_SAFEPOINT
182 #  ifdef LISP_FEATURE_SB_THRUPTION
183     undoably_install_low_level_interrupt_handler(SIGPIPE, thruption_handler);
184 #  endif
185 # else
186     undoably_install_low_level_interrupt_handler(SIG_STOP_FOR_GC,
187                                                  sig_stop_for_gc_handler);
188 # endif
189 #endif
190 }
191
192 char *
193 os_get_runtime_executable_path(int external)
194 {
195     char path[] = "/proc/self/object/a.out";
196
197     if (external || access(path, R_OK) == -1)
198         return NULL;
199
200     return copied_string(path);
201 }
202