/[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.16 - (hide annotations)
Sun Jul 15 09:24:57 2007 UTC (6 years, 9 months ago) by cshapiro
Branch: MAIN
Changes since 1.15: +31 -30 lines
File MIME type: text/plain
Use POSIX signals on FreeBSD.
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.16 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/FreeBSD-os.c,v 1.16 2007/07/15 09:24:57 cshapiro Exp $
16 ram 1.1 *
17     */
18    
19 fgilham 1.12 #include "os.h"
20 ram 1.1 #include <sys/file.h>
21     #include <errno.h>
22     #include "./signal.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     /* #include <sys/sysinfo.h> */
31     #include <sys/proc.h>
32 moore 1.8 #include <dlfcn.h>
33 pw 1.3 #include "validate.h"
34 dtc 1.2
35     #if defined GENCGC
36     #include "gencgc.h"
37     #endif
38    
39 moore 1.7 #if __FreeBSD_version > 400000
40     /* The lisp runtime is dynamically linked, but we need a definition of
41     errno for genesis. */
42     #undef errno
43     int errno;
44     #endif
45 gerd 1.10
46     vm_size_t os_vm_page_size;
47 rtoy 1.11
48 gerd 1.10
49     void
50 rtoy 1.11 os_init(void)
51 ram 1.1 {
52 rtoy 1.11 os_vm_page_size = getpagesize();
53 ram 1.1 }
54    
55 cshapiro 1.13 int *
56 cshapiro 1.16 sc_reg(ucontext_t *context, int offset)
57 ram 1.1 {
58 rtoy 1.11 switch (offset) {
59     case 0:
60 cshapiro 1.16 return &context->uc_mcontext.mc_eax;
61 rtoy 1.11 case 2:
62 cshapiro 1.16 return &context->uc_mcontext.mc_ecx;
63 rtoy 1.11 case 4:
64 cshapiro 1.16 return &context->uc_mcontext.mc_edx;
65 rtoy 1.11 case 6:
66 cshapiro 1.16 return &context->uc_mcontext.mc_ebx;
67 rtoy 1.11 case 8:
68 cshapiro 1.16 return &context->uc_mcontext.mc_esp;
69 rtoy 1.11 case 10:
70 cshapiro 1.16 return &context->uc_mcontext.mc_ebp;
71 rtoy 1.11 case 12:
72 cshapiro 1.16 return &context->uc_mcontext.mc_esi;
73 rtoy 1.11 case 14:
74 cshapiro 1.16 return &context->uc_mcontext.mc_edi;
75 ram 1.1 }
76 rtoy 1.11
77 cshapiro 1.13 return (int *) 0;
78 ram 1.1 }
79 dtc 1.5
80 gerd 1.10 void
81 rtoy 1.11 os_save_context(void)
82 ram 1.1 {
83 rtoy 1.11 /* Called from interrupt handlers so C stuff knows things set in
84     Lisp. */
85 ram 1.1 }
86 dtc 1.5
87 gerd 1.10 void
88 rtoy 1.11 os_set_context(void)
89 ram 1.1 {
90     }
91    
92 rtoy 1.11 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
93 ram 1.1 {
94 cshapiro 1.15 int flags = MAP_PRIVATE | MAP_ANON;
95 dtc 1.5
96 rtoy 1.11 if (addr)
97     flags |= MAP_FIXED;
98 ram 1.1
99 rtoy 1.11 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
100 dtc 1.5
101 rtoy 1.11 if (addr == (os_vm_address_t) - 1) {
102     perror("mmap");
103     return NULL;
104 ram 1.1 }
105 dtc 1.5
106 rtoy 1.11 return addr;
107 ram 1.1 }
108    
109 gerd 1.10 void
110 rtoy 1.11 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
111 ram 1.1 {
112 rtoy 1.11 if (munmap(addr, len) == -1)
113     perror("munmap");
114 ram 1.1 }
115    
116 gerd 1.10 os_vm_address_t
117 rtoy 1.11 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
118 ram 1.1 {
119 rtoy 1.11 addr = mmap(addr, len, OS_VM_PROT_ALL,
120     MAP_PRIVATE | MAP_FILE | MAP_FIXED, fd, (off_t) offset);
121 dtc 1.5
122 rtoy 1.11 if (addr == (os_vm_address_t) - 1)
123     perror("mmap");
124 dtc 1.5
125 rtoy 1.11 return addr;
126 ram 1.1 }
127    
128 gerd 1.10 void
129 rtoy 1.11 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
130 ram 1.1 {
131     }
132    
133 gerd 1.10 void
134 rtoy 1.11 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
135 ram 1.1 {
136 rtoy 1.11 if (mprotect(address, length, prot) == -1)
137     perror("mprotect");
138 ram 1.1 }
139 rtoy 1.11
140 dtc 1.5
141    
142 gerd 1.10 static boolean
143 rtoy 1.11 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
144 ram 1.1 {
145 rtoy 1.11 char *beg = (char *) sbeg;
146     char *end = (char *) sbeg + slen;
147     char *adr = (char *) a;
148    
149     return adr >= beg && adr < end;
150 ram 1.1 }
151 dtc 1.5
152 rtoy 1.11 boolean valid_addr(os_vm_address_t addr)
153 ram 1.1 {
154 rtoy 1.11 os_vm_address_t newaddr;
155 ram 1.1
156 rtoy 1.11 newaddr = os_trunc_to_page(addr);
157    
158     if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
159     || in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE)
160     || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size)
161 gerd 1.10 #ifndef GENCGC
162 rtoy 1.11 || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
163 gerd 1.10 #endif
164 rtoy 1.11 || in_range_p(addr, CONTROL_STACK_START, CONTROL_STACK_SIZE)
165     || in_range_p(addr, BINDING_STACK_START, BINDING_STACK_SIZE))
166     return TRUE;
167     return FALSE;
168 ram 1.1 }
169 rtoy 1.11
170 ram 1.1
171 gerd 1.10 static void
172 cshapiro 1.16 sigbus_handler(int signal, siginfo_t *info, ucontext_t *context)
173 gerd 1.10 {
174 rtoy 1.11 int page_index;
175    
176 gerd 1.10 #ifdef RED_ZONE_HIT
177 cshapiro 1.16 if (os_control_stack_overflow(info->si_addr, context))
178 rtoy 1.11 return;
179 gerd 1.10 #endif
180 rtoy 1.11
181 dtc 1.2 #if defined GENCGC
182 cshapiro 1.16 if (info->si_code == BUS_PAGE_FAULT) {
183     page_index = find_page_index(info->si_addr);
184 dtc 1.2
185 cshapiro 1.16 /* Check if the fault is within the dynamic space. */
186     if (page_index != -1) {
187     /* Un-protect the page */
188    
189     /* The page should have been marked write protected */
190     if (!PAGE_WRITE_PROTECTED(page_index))
191     fprintf(stderr,
192     "*** Sigbus in page not marked as write protected\n");
193    
194     os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
195     page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
196     page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
197 dtc 1.5
198 cshapiro 1.16 return;
199     }
200 dtc 1.2 }
201 gerd 1.10 #endif /* GENCGC */
202 dtc 1.2
203 cshapiro 1.16 interrupt_handle_now(signal, info, context);
204 ram 1.1 }
205 dtc 1.5
206 gerd 1.10 static void
207 cshapiro 1.16 sigsegv_handler(int signal, siginfo_t *info, ucontext_t *context)
208 ram 1.1 {
209 cshapiro 1.16 interrupt_handle_now(signal, info, context);
210 ram 1.1 }
211    
212 gerd 1.10 void
213 rtoy 1.11 os_install_interrupt_handlers(void)
214 ram 1.1 {
215 rtoy 1.11 interrupt_install_low_level_handler
216     (SIGSEGV, (void (*)(HANDLER_ARGS)) sigsegv_handler);
217     interrupt_install_low_level_handler
218     (SIGBUS, (void (*)(HANDLER_ARGS)) sigbus_handler);
219 moore 1.8 }
220    
221 gerd 1.10 void *
222 rtoy 1.11 os_dlsym(const char *sym_name, lispobj lib_list)
223 moore 1.8 {
224 rtoy 1.11 if (lib_list != NIL) {
225     lispobj lib_list_head;
226    
227     for (lib_list_head = lib_list;
228     lib_list_head != NIL; lib_list_head = CONS(lib_list_head)->cdr) {
229     struct cons *lib_cons = CONS(CONS(lib_list_head)->car);
230     struct sap *dlhandle = (struct sap *) PTR(lib_cons->car);
231     void *sym_addr = dlsym((void *) dlhandle->pointer, sym_name);
232 moore 1.8
233 rtoy 1.11 if (sym_addr)
234     return sym_addr;
235 moore 1.8 }
236     }
237 rtoy 1.11
238     return dlsym(RTLD_DEFAULT, sym_name);
239 ram 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.5