/[cmucl]/src/lisp/FreeBSD-os.c
ViewVC logotype

Contents of /src/lisp/FreeBSD-os.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (hide annotations)
Tue Mar 18 09:22:55 2008 UTC (6 years, 1 month ago) by cshapiro
Branch: MAIN
CVS Tags: snapshot-2008-08, snapshot-2008-09, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, unicode-utf16-sync-2008-07, unicode-utf16-sync-2008-09, snapshot-2008-04, unicode-utf16-extfmts-pre-sync-2008-11, unicode-utf16-string-support, unicode-utf16-base
Branch point for: unicode-utf16-branch, unicode-utf16-extfmt-branch
Changes since 1.22: +3 -3 lines
File MIME type: text/plain
The FreeBSD-os.h header imported headers that its definitions did not
make direct use of.  Remove the includes of these headers and fix-up
several files that indirectly depended on them.

* FreeBSD-os.c - add stdio.h and unistd.h includes that were
  implicitly depended on.  Remove unused or commented system includes.

* FreeBSD-os.h - remove unused includes.  Include signal.h to directly
  provide the SIGBUS, SIGSEGV, and sub-code definitions.  Redefine
  os_vm_address_t and os_vm_size_t in terms of standard types.  Now
  that the sys/param.h include is gone we no longer need to undefine
  PAGE_SIZE.

* elf.c - add includes that were implicitly depended on.

* lisp.c - remove unused system includes including sys/param.h which
  redefines PAGE_SIZE.
1 ram 1.1 /*
2     * FreeBSD-os.c. Maybe could be just BSD-os.c
3     * From osf1-os.c,v 1.1 94/03/27 15:30:51 hallgren Exp $
4     *
5     * OS-dependent routines. This file (along with os.h) exports an
6     * OS-independent interface to the operating system VM facilities.
7     * Suprisingly, this interface looks a lot like the Mach interface
8     * (but simpler in some places). For some operating systems, a subset
9     * of these functions will have to be emulated.
10     *
11     * This is the OSF1 version. By Sean Hallgren.
12     * Much hacked by Paul Werkowski
13 dtc 1.2 * GENCGC support by Douglas Crosher, 1996, 1997.
14     *
15 cshapiro 1.23 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/FreeBSD-os.c,v 1.23 2008/03/18 09:22:55 cshapiro Exp $
16 ram 1.1 *
17     */
18    
19 fgilham 1.12 #include "os.h"
20 ram 1.1 #include <sys/file.h>
21 cshapiro 1.22 #include <machine/npx.h>
22 ram 1.1 #include <errno.h>
23     #include "arch.h"
24     #include "globals.h"
25     #include "interrupt.h"
26     #include "lispregs.h"
27     #include "internals.h"
28    
29 dtc 1.2 #include <signal.h>
30 moore 1.8 #include <dlfcn.h>
31 pw 1.3 #include "validate.h"
32 cshapiro 1.23 #include <stdio.h>
33     #include <unistd.h>
34 dtc 1.2
35     #if defined GENCGC
36     #include "gencgc.h"
37     #endif
38    
39 gerd 1.10 vm_size_t os_vm_page_size;
40 rtoy 1.11
41 gerd 1.10
42     void
43 rtoy 1.11 os_init(void)
44 ram 1.1 {
45 rtoy 1.11 os_vm_page_size = getpagesize();
46 ram 1.1 }
47    
48 cshapiro 1.22 unsigned long *
49     os_sigcontext_reg(ucontext_t *scp, int index)
50 ram 1.1 {
51 cshapiro 1.22 switch (index) {
52     case 0:
53     return (unsigned long *) &scp->uc_mcontext.mc_eax;
54     case 2:
55     return (unsigned long *) &scp->uc_mcontext.mc_ecx;
56     case 4:
57     return (unsigned long *) &scp->uc_mcontext.mc_edx;
58     case 6:
59     return (unsigned long *) &scp->uc_mcontext.mc_ebx;
60     case 8:
61     return (unsigned long *) &scp->uc_mcontext.mc_esp;
62     case 10:
63     return (unsigned long *) &scp->uc_mcontext.mc_ebp;
64     case 12:
65     return (unsigned long *) &scp->uc_mcontext.mc_esi;
66     case 14:
67     return (unsigned long *) &scp->uc_mcontext.mc_edi;
68 ram 1.1 }
69 cshapiro 1.22 return NULL;
70     }
71    
72     unsigned long *
73     os_sigcontext_pc(ucontext_t *scp)
74     {
75     return (unsigned long *) &scp->uc_mcontext.mc_eip;
76     }
77    
78     unsigned char *
79     os_sigcontext_fpu_reg(ucontext_t *scp, int index)
80     {
81     union savefpu *sv = (union savefpu *) scp->uc_mcontext.mc_fpstate;
82     int fpformat = scp->uc_mcontext.mc_fpformat;
83     unsigned char *reg = NULL;
84 rtoy 1.11
85 cshapiro 1.22 switch (fpformat) {
86     case _MC_FPFMT_XMM:
87     reg = sv->sv_xmm.sv_fp[index].fp_acc.fp_bytes;
88     break;
89     case _MC_FPFMT_387:
90     reg = sv->sv_87.sv_ac[index].fp_bytes;
91     break;
92     case _MC_FPFMT_NODEV:
93     reg = NULL;
94     break;
95     }
96     return reg;
97     }
98    
99     unsigned long
100     os_sigcontext_fpu_modes(ucontext_t *scp)
101     {
102     union savefpu *sv = (union savefpu *) scp->uc_mcontext.mc_fpstate;
103     unsigned long modes;
104     int fpformat = scp->uc_mcontext.mc_fpformat;
105     unsigned short cw, sw;
106    
107     if (fpformat == _MC_FPFMT_XMM) {
108     cw = sv->sv_xmm.sv_env.en_cw;
109     sw = sv->sv_xmm.sv_env.en_sw;
110     } else if (fpformat == _MC_FPFMT_387) {
111     cw = sv->sv_87.sv_env.en_cw & 0xffff;
112     sw = sv->sv_87.sv_env.en_sw & 0xffff;
113     } else { /* _MC_FPFMT_NODEV */
114     cw = 0;
115     sw = 0x3f;
116     }
117     modes = (sw & 0xff) << 16 | cw;
118     modes ^= 0x3f;
119     return modes;
120 ram 1.1 }
121 dtc 1.5
122 rtoy 1.11 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
123 ram 1.1 {
124 cshapiro 1.15 int flags = MAP_PRIVATE | MAP_ANON;
125 dtc 1.5
126 rtoy 1.11 if (addr)
127     flags |= MAP_FIXED;
128 ram 1.1
129 rtoy 1.11 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
130 dtc 1.5
131 rtoy 1.11 if (addr == (os_vm_address_t) - 1) {
132     perror("mmap");
133     return NULL;
134 ram 1.1 }
135 dtc 1.5
136 rtoy 1.11 return addr;
137 ram 1.1 }
138    
139 gerd 1.10 void
140 rtoy 1.11 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
141 ram 1.1 {
142 rtoy 1.11 if (munmap(addr, len) == -1)
143     perror("munmap");
144 ram 1.1 }
145    
146 gerd 1.10 os_vm_address_t
147 rtoy 1.11 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
148 ram 1.1 {
149 rtoy 1.11 addr = mmap(addr, len, OS_VM_PROT_ALL,
150     MAP_PRIVATE | MAP_FILE | MAP_FIXED, fd, (off_t) offset);
151 dtc 1.5
152 rtoy 1.11 if (addr == (os_vm_address_t) - 1)
153     perror("mmap");
154 dtc 1.5
155 rtoy 1.11 return addr;
156 ram 1.1 }
157    
158 gerd 1.10 void
159 rtoy 1.11 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
160 ram 1.1 {
161     }
162    
163 gerd 1.10 void
164 rtoy 1.11 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
165 ram 1.1 {
166 rtoy 1.11 if (mprotect(address, length, prot) == -1)
167     perror("mprotect");
168 ram 1.1 }
169 rtoy 1.11
170 dtc 1.5
171    
172 gerd 1.10 static boolean
173 rtoy 1.11 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
174 ram 1.1 {
175 rtoy 1.11 char *beg = (char *) sbeg;
176     char *end = (char *) sbeg + slen;
177     char *adr = (char *) a;
178    
179     return adr >= beg && adr < end;
180 ram 1.1 }
181 dtc 1.5
182 rtoy 1.11 boolean valid_addr(os_vm_address_t addr)
183 ram 1.1 {
184 rtoy 1.11 os_vm_address_t newaddr;
185 ram 1.1
186 rtoy 1.11 newaddr = os_trunc_to_page(addr);
187    
188     if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
189     || in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE)
190     || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size)
191 gerd 1.10 #ifndef GENCGC
192 rtoy 1.11 || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
193 gerd 1.10 #endif
194 rtoy 1.11 || in_range_p(addr, CONTROL_STACK_START, CONTROL_STACK_SIZE)
195     || in_range_p(addr, BINDING_STACK_START, BINDING_STACK_SIZE))
196     return TRUE;
197     return FALSE;
198 ram 1.1 }
199 rtoy 1.11
200 ram 1.1
201 gerd 1.10 static void
202 cshapiro 1.16 sigbus_handler(int signal, siginfo_t *info, ucontext_t *context)
203 gerd 1.10 {
204 rtoy 1.11 int page_index;
205    
206 gerd 1.10 #ifdef RED_ZONE_HIT
207 cshapiro 1.16 if (os_control_stack_overflow(info->si_addr, context))
208 rtoy 1.11 return;
209 gerd 1.10 #endif
210 rtoy 1.11
211 dtc 1.2 #if defined GENCGC
212 cshapiro 1.21 if (info->si_code == PROTECTION_VIOLATION_CODE) {
213 cshapiro 1.16 page_index = find_page_index(info->si_addr);
214 dtc 1.2
215 cshapiro 1.16 /* Check if the fault is within the dynamic space. */
216     if (page_index != -1) {
217     /* Un-protect the page */
218    
219     /* The page should have been marked write protected */
220     if (!PAGE_WRITE_PROTECTED(page_index))
221     fprintf(stderr,
222     "*** Sigbus in page not marked as write protected\n");
223    
224     os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
225     page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
226     page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
227 dtc 1.5
228 cshapiro 1.16 return;
229     }
230 dtc 1.2 }
231 gerd 1.10 #endif /* GENCGC */
232 dtc 1.2
233 cshapiro 1.16 interrupt_handle_now(signal, info, context);
234 ram 1.1 }
235 dtc 1.5
236 cshapiro 1.22 /*
237     * Restore the exception flags cleared by the kernel. These bits must
238     * be set for Lisp to determine which exception caused the signal. At
239     * present, there is no way to distinguish underflow exceptions from
240     * denormalized operand exceptions. An underflow exception is assumed
241     * if the subcode is FPE_FLTUND.
242     */
243     static void
244     sigfpe_handler(int signal, siginfo_t *info, ucontext_t *context)
245     {
246     union savefpu *sv = (union savefpu *) context->uc_mcontext.mc_fpstate;
247     int fpformat = context->uc_mcontext.mc_fpformat;
248     int code = info->si_code;
249     unsigned char trap = 0;
250    
251     switch (code) {
252     case FPE_FLTDIV: /* ZE */
253     trap = 0x04;
254     break;
255     case FPE_FLTOVF: /* OE */
256     trap = 0x08;
257     break;
258     case FPE_FLTUND: /* DE or UE */
259     trap = 0x10;
260     break;
261     case FPE_FLTRES: /* PE */
262     trap = 0x20;
263     break;
264     case FPE_FLTINV: /* IE */
265     trap = 0x01;
266     break;
267     }
268     switch (fpformat) {
269     case _MC_FPFMT_XMM:
270     sv->sv_xmm.sv_env.en_sw |= trap;
271     break;
272     case _MC_FPFMT_387:
273     sv->sv_87.sv_env.en_sw |= trap;
274     break;
275     }
276     interrupt_handle_now(signal, info, context);
277     }
278    
279 gerd 1.10 void
280 rtoy 1.11 os_install_interrupt_handlers(void)
281 ram 1.1 {
282 rtoy 1.11 interrupt_install_low_level_handler
283 cshapiro 1.21 (PROTECTION_VIOLATION_SIGNAL, sigbus_handler);
284 cshapiro 1.22 interrupt_install_low_level_handler(SIGFPE, sigfpe_handler);
285 moore 1.8 }
286    
287 gerd 1.10 void *
288 rtoy 1.11 os_dlsym(const char *sym_name, lispobj lib_list)
289 moore 1.8 {
290 rtoy 1.11 if (lib_list != NIL) {
291     lispobj lib_list_head;
292    
293     for (lib_list_head = lib_list;
294     lib_list_head != NIL; lib_list_head = CONS(lib_list_head)->cdr) {
295     struct cons *lib_cons = CONS(CONS(lib_list_head)->car);
296     struct sap *dlhandle = (struct sap *) PTR(lib_cons->car);
297     void *sym_addr = dlsym((void *) dlhandle->pointer, sym_name);
298 moore 1.8
299 rtoy 1.11 if (sym_addr)
300     return sym_addr;
301 moore 1.8 }
302     }
303 rtoy 1.11
304     return dlsym(RTLD_DEFAULT, sym_name);
305 ram 1.1 }
306 cshapiro 1.22
307     void
308     restore_fpu(ucontext_t *scp)
309     {
310     union savefpu *sv = (union savefpu *) scp->uc_mcontext.mc_fpstate;
311     int fpformat = scp->uc_mcontext.mc_fpformat;
312     unsigned short cw;
313    
314     if (fpformat == _MC_FPFMT_XMM) {
315     cw = sv->sv_xmm.sv_env.en_cw;
316     } else if (fpformat == _MC_FPFMT_387) {
317     cw = sv->sv_87.sv_env.en_cw & 0xffff;
318     } else { /* _MC_FPFMT_NODEV */
319     return;
320     }
321     __asm__ __volatile__ ("fldcw %0" : : "m" (*&cw));
322     }

  ViewVC Help
Powered by ViewVC 1.1.5