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

  ViewVC Help
Powered by ViewVC 1.1.5