/[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.7 - (show annotations)
Wed Mar 13 08:02:04 2002 UTC (12 years, 1 month ago) by moore
Branch: MAIN
CVS Tags: PRE_LINKAGE_TABLE
Changes since 1.6: +7 -1 lines
File MIME type: text/plain
Support for dynamic loading in FreeBSD 4.0 and later.  This involves moving
the static space up, so a cross-compile is required to bootstrap these changes.
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.7 2002/03/13 08:02:04 moore 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 "validate.h"
36 vm_size_t os_vm_page_size;
37
38 #define DPRINTF(t,a) {if (t) fprintf a;}
39
40 #if defined GENCGC
41 #include "gencgc.h"
42 #endif
43
44 #if __FreeBSD_version > 400000
45 /* The lisp runtime is dynamically linked, but we need a definition of
46 errno for genesis. */
47 #undef errno
48 int errno;
49 #endif
50
51 void os_init(void)
52 {
53 os_vm_page_size = OS_VM_DEFAULT_PAGESIZE;
54 }
55
56 int sc_reg(struct sigcontext *c, int offset)
57 {
58 switch(offset)
59 {
60 case 0: return c->sc_eax;
61 case 2: return c->sc_ecx;
62 case 4: return c->sc_edx;
63 case 6: return c->sc_ebx;
64 case 8: return c->sc_esp;
65 case 10: return c->sc_ebp;
66 case 12: return c->sc_esi;
67 case 14: return c->sc_edi;
68 }
69 return 0;
70 }
71
72 void os_save_context(void)
73 {
74 /*
75 * Called from interrupt handlers so C stuff knows things set in Lisp.
76 */
77 }
78
79 void os_set_context(void)
80 {
81 }
82
83 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
84 {
85 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
86
87 if (addr)
88 flags |= MAP_FIXED;
89 else
90 flags |= MAP_VARIABLE;
91
92 DPRINTF(0, (stderr, "os_validate %x %d => ", addr, len));
93
94 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
95
96 if (addr == (os_vm_address_t) -1)
97 {
98 perror("mmap");
99 return NULL;
100 }
101
102 DPRINTF(0, (stderr, "%x\n", addr));
103
104 return addr;
105 }
106
107 void os_invalidate(os_vm_address_t addr, os_vm_size_t len)
108 {
109 DPRINTF(0, (stderr, "os_invalidate %x %d\n", addr, len));
110
111 if (munmap(addr, len) == -1)
112 perror("munmap");
113 }
114
115 os_vm_address_t os_map(int fd, int offset, os_vm_address_t addr,
116 os_vm_size_t len)
117 {
118 addr = mmap(addr, len,
119 OS_VM_PROT_ALL,
120 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
121 fd, (off_t) offset);
122
123 if (addr == (os_vm_address_t) -1)
124 perror("mmap");
125
126 return addr;
127 }
128
129 void os_flush_icache(os_vm_address_t address, os_vm_size_t length)
130 {
131 }
132
133 void os_protect(os_vm_address_t address, os_vm_size_t length,
134 os_vm_prot_t prot)
135 {
136 if (mprotect(address, length, prot) == -1)
137 perror("mprotect");
138 }
139
140
141
142 static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
143 {
144 char* beg = (char*) sbeg;
145 char* end = (char*) sbeg + slen;
146 char* adr = (char*) a;
147 return (adr >= beg && adr < end);
148 }
149
150 boolean valid_addr(os_vm_address_t addr)
151 {
152 int ret;
153 os_vm_address_t newaddr;
154 newaddr = os_trunc_to_page(addr);
155
156 if ( in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
157 || in_range_p(addr, STATIC_SPACE_START , STATIC_SPACE_SIZE )
158 || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size )
159 || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size )
160 || in_range_p(addr, CONTROL_STACK_START , CONTROL_STACK_SIZE )
161 || in_range_p(addr, BINDING_STACK_START , BINDING_STACK_SIZE ))
162 return TRUE;
163 return FALSE;
164 }
165
166
167 static void sigbus_handler(int signal, int code, struct sigcontext *context,
168 void *fault_addr)
169 {
170 #if defined GENCGC
171 int page_index = find_page_index(fault_addr);
172
173 #if SIGBUS_VERBOSE
174 fprintf(stderr,"Signal %d, fault_addr=%x, page_index=%d:\n",
175 signal, fault_addr, page_index);
176 #endif
177
178 /* Check if the fault is within the dynamic space. */
179 if (page_index != -1) {
180 /* Un-protect the page */
181
182 /* The page should have been marked write protected */
183 if (!PAGE_WRITE_PROTECTED(page_index))
184 fprintf(stderr, "*** Sigbus in page not marked as write protected\n");
185
186 os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
187 page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
188 page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
189
190 return;
191 }
192 #endif
193
194 DPRINTF(0, (stderr, "sigbus:\n"));
195 interrupt_handle_now(signal, code, context);
196 }
197
198 static void sigsegv_handler(int signal, int code, struct sigcontext *context)
199 {
200 DPRINTF(0, (stderr, "os_sigsegv\n"));
201 interrupt_handle_now(signal, code, context);
202 }
203
204 void os_install_interrupt_handlers(void)
205 {
206 interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
207 interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
208 }

  ViewVC Help
Powered by ViewVC 1.1.5