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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Tue Jun 12 03:21:46 2007 UTC (6 years, 10 months ago) by cshapiro
Branch: MAIN
CVS Tags: snapshot-2007-07
Changes since 1.3: +2 -4 lines
File MIME type: text/plain
Remove no-op compatibility macros.
1 pmai 1.1 /*
2     * OpenBSD-os.c.
3     * From FreeBSD-os.c 1.6 2000/10/24 13:32:30 dtc 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     * Frobbed for OpenBSD by Pierre R. Mai, 2001.
15     *
16 cshapiro 1.4 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/OpenBSD-os.c,v 1.4 2007/06/12 03:21:46 cshapiro Exp $
17 pmai 1.1 *
18     */
19    
20     #include <stdio.h>
21     #include <sys/param.h>
22     #include <sys/file.h>
23     #include <errno.h>
24     #include "./signal.h"
25     #include "os.h"
26     #include "arch.h"
27     #include "globals.h"
28     #include "interrupt.h"
29     #include "lispregs.h"
30     #include "internals.h"
31    
32     #include <sys/types.h>
33     #include <signal.h>
34     /* #include <sys/sysinfo.h> */
35     #include <sys/proc.h>
36     #include "validate.h"
37     vm_size_t os_vm_page_size;
38    
39     #define DPRINTF(t,a) {if (t) fprintf a;}
40    
41     #if defined GENCGC
42     #include "gencgc.h"
43     #endif
44 rtoy 1.2
45 pmai 1.1
46 rtoy 1.2 void
47     os_init(void)
48 pmai 1.1 {
49 rtoy 1.2 os_vm_page_size = OS_VM_DEFAULT_PAGESIZE;
50 pmai 1.1 }
51    
52 cshapiro 1.3 int *
53 rtoy 1.2 sc_reg(struct sigcontext *c, int offset)
54 pmai 1.1 {
55 rtoy 1.2 switch (offset) {
56     case 0:
57 cshapiro 1.3 return &c->sc_eax;
58 rtoy 1.2 case 2:
59 cshapiro 1.3 return &c->sc_ecx;
60 rtoy 1.2 case 4:
61 cshapiro 1.3 return &c->sc_edx;
62 rtoy 1.2 case 6:
63 cshapiro 1.3 return &c->sc_ebx;
64 rtoy 1.2 case 8:
65 cshapiro 1.3 return &c->sc_esp;
66 rtoy 1.2 case 10:
67 cshapiro 1.3 return &c->sc_ebp;
68 rtoy 1.2 case 12:
69 cshapiro 1.3 return &c->sc_esi;
70 rtoy 1.2 case 14:
71 cshapiro 1.3 return &c->sc_edi;
72 pmai 1.1 }
73 cshapiro 1.3 return (int *) 0;
74 pmai 1.1 }
75    
76 rtoy 1.2 void
77     os_save_context(void)
78 pmai 1.1 {
79 rtoy 1.2 /*
80     * Called from interrupt handlers so C stuff knows things set in Lisp.
81     */
82 pmai 1.1 }
83    
84 rtoy 1.2 void
85     os_set_context(void)
86 pmai 1.1 {
87     }
88    
89 rtoy 1.2 os_vm_address_t
90     os_validate(os_vm_address_t addr, os_vm_size_t len)
91 pmai 1.1 {
92 cshapiro 1.4 int flags = MAP_PRIVATE | MAP_ANON;
93 pmai 1.1
94 rtoy 1.2 if (addr)
95     flags |= MAP_FIXED;
96 pmai 1.1
97 rtoy 1.2 DPRINTF(0, (stderr, "os_validate %x %d => ", addr, len));
98 pmai 1.1
99 rtoy 1.2 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
100 pmai 1.1
101 rtoy 1.2 if (addr == (os_vm_address_t) - 1) {
102     perror("mmap");
103     return NULL;
104 pmai 1.1 }
105    
106 rtoy 1.2 DPRINTF(0, (stderr, "%x\n", addr));
107 pmai 1.1
108 rtoy 1.2 return addr;
109 pmai 1.1 }
110    
111 rtoy 1.2 void
112     os_invalidate(os_vm_address_t addr, os_vm_size_t len)
113 pmai 1.1 {
114 rtoy 1.2 DPRINTF(0, (stderr, "os_invalidate %x %d\n", addr, len));
115 pmai 1.1
116 rtoy 1.2 if (munmap(addr, len) == -1)
117     perror("munmap");
118 pmai 1.1 }
119    
120 rtoy 1.2 os_vm_address_t
121     os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
122 pmai 1.1 {
123 rtoy 1.2 addr = mmap(addr, len,
124     OS_VM_PROT_ALL,
125     MAP_PRIVATE | MAP_FILE | MAP_FIXED, fd, (off_t) offset);
126 pmai 1.1
127 rtoy 1.2 if (addr == (os_vm_address_t) - 1)
128     perror("mmap");
129 pmai 1.1
130 rtoy 1.2 return addr;
131 pmai 1.1 }
132    
133 rtoy 1.2 void
134     os_flush_icache(os_vm_address_t address, os_vm_size_t length)
135 pmai 1.1 {
136     }
137    
138 rtoy 1.2 void
139     os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
140 pmai 1.1 {
141 rtoy 1.2 if (mprotect(address, length, prot) == -1)
142     perror("mprotect");
143 pmai 1.1 }
144 rtoy 1.2
145 pmai 1.1
146    
147 rtoy 1.2 static boolean
148     in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
149 pmai 1.1 {
150 rtoy 1.2 char *beg = (char *) sbeg;
151     char *end = (char *) sbeg + slen;
152     char *adr = (char *) a;
153    
154     return (adr >= beg && adr < end);
155 pmai 1.1 }
156    
157 rtoy 1.2 boolean
158     valid_addr(os_vm_address_t addr)
159     {
160     int ret;
161     os_vm_address_t newaddr;
162    
163     newaddr = os_trunc_to_page(addr);
164    
165     if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
166     || in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE)
167     || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size)
168     || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
169     || in_range_p(addr, CONTROL_STACK_START, CONTROL_STACK_SIZE)
170     || in_range_p(addr, BINDING_STACK_START, BINDING_STACK_SIZE))
171     return TRUE;
172     return FALSE;
173     }
174 pmai 1.1
175 rtoy 1.2
176     static void
177     sigsegv_handler(HANDLER_ARGS)
178 pmai 1.1 {
179     #if defined GENCGC
180 rtoy 1.2 caddr_t fault_addr = code->si_addr;
181     int page_index = find_page_index((void *) fault_addr);
182 pmai 1.1
183     #if SIGSEGV_VERBOSE
184 rtoy 1.2 fprintf(stderr, "Signal %d, fault_addr=%x, page_index=%d:\n",
185     signal, fault_addr, page_index);
186 pmai 1.1 #endif
187    
188 rtoy 1.2 /* Check if the fault is within the dynamic space. */
189     if (page_index != -1) {
190     /* Un-protect the page */
191    
192     /* The page should have been marked write protected */
193     if (!PAGE_WRITE_PROTECTED(page_index))
194     fprintf(stderr,
195     "*** Sigsegv 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 pmai 1.1
201 rtoy 1.2 return;
202 pmai 1.1 }
203     #endif
204    
205 rtoy 1.2 SAVE_CONTEXT();
206 pmai 1.1
207 rtoy 1.2 DPRINTF(0, (stderr, "sigsegv:\n"));
208     interrupt_handle_now(signal, code, context);
209 pmai 1.1 }
210    
211 rtoy 1.2 static void
212     sigbus_handler(HANDLER_ARGS)
213 pmai 1.1 {
214 rtoy 1.2 SAVE_CONTEXT();
215 pmai 1.1
216 rtoy 1.2 DPRINTF(0, (stderr, "sigbus:\n"));
217     interrupt_handle_now(signal, code, context);
218 pmai 1.1 }
219    
220 rtoy 1.2 void
221     os_install_interrupt_handlers(void)
222 pmai 1.1 {
223 rtoy 1.2 interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
224     interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
225 pmai 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.5