/[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.5 - (hide annotations)
Sun Jul 15 21:33:14 2007 UTC (6 years, 9 months ago) by cshapiro
Branch: MAIN
Changes since 1.4: +1 -14 lines
File MIME type: text/plain
Remove unused context saving and setting functions.
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.5 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/OpenBSD-os.c,v 1.5 2007/07/15 21:33:14 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 os_vm_address_t
77     os_validate(os_vm_address_t addr, os_vm_size_t len)
78 pmai 1.1 {
79 cshapiro 1.4 int flags = MAP_PRIVATE | MAP_ANON;
80 pmai 1.1
81 rtoy 1.2 if (addr)
82     flags |= MAP_FIXED;
83 pmai 1.1
84 rtoy 1.2 DPRINTF(0, (stderr, "os_validate %x %d => ", addr, len));
85 pmai 1.1
86 rtoy 1.2 addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);
87 pmai 1.1
88 rtoy 1.2 if (addr == (os_vm_address_t) - 1) {
89     perror("mmap");
90     return NULL;
91 pmai 1.1 }
92    
93 rtoy 1.2 DPRINTF(0, (stderr, "%x\n", addr));
94 pmai 1.1
95 rtoy 1.2 return addr;
96 pmai 1.1 }
97    
98 rtoy 1.2 void
99     os_invalidate(os_vm_address_t addr, os_vm_size_t len)
100 pmai 1.1 {
101 rtoy 1.2 DPRINTF(0, (stderr, "os_invalidate %x %d\n", addr, len));
102 pmai 1.1
103 rtoy 1.2 if (munmap(addr, len) == -1)
104     perror("munmap");
105 pmai 1.1 }
106    
107 rtoy 1.2 os_vm_address_t
108     os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
109 pmai 1.1 {
110 rtoy 1.2 addr = mmap(addr, len,
111     OS_VM_PROT_ALL,
112     MAP_PRIVATE | MAP_FILE | MAP_FIXED, fd, (off_t) offset);
113 pmai 1.1
114 rtoy 1.2 if (addr == (os_vm_address_t) - 1)
115     perror("mmap");
116 pmai 1.1
117 rtoy 1.2 return addr;
118 pmai 1.1 }
119    
120 rtoy 1.2 void
121     os_flush_icache(os_vm_address_t address, os_vm_size_t length)
122 pmai 1.1 {
123     }
124    
125 rtoy 1.2 void
126     os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
127 pmai 1.1 {
128 rtoy 1.2 if (mprotect(address, length, prot) == -1)
129     perror("mprotect");
130 pmai 1.1 }
131 rtoy 1.2
132 pmai 1.1
133    
134 rtoy 1.2 static boolean
135     in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
136 pmai 1.1 {
137 rtoy 1.2 char *beg = (char *) sbeg;
138     char *end = (char *) sbeg + slen;
139     char *adr = (char *) a;
140    
141     return (adr >= beg && adr < end);
142 pmai 1.1 }
143    
144 rtoy 1.2 boolean
145     valid_addr(os_vm_address_t addr)
146     {
147     int ret;
148     os_vm_address_t newaddr;
149    
150     newaddr = os_trunc_to_page(addr);
151    
152     if (in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
153     || in_range_p(addr, STATIC_SPACE_START, STATIC_SPACE_SIZE)
154     || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size)
155     || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
156     || in_range_p(addr, CONTROL_STACK_START, CONTROL_STACK_SIZE)
157     || in_range_p(addr, BINDING_STACK_START, BINDING_STACK_SIZE))
158     return TRUE;
159     return FALSE;
160     }
161 pmai 1.1
162 rtoy 1.2
163     static void
164     sigsegv_handler(HANDLER_ARGS)
165 pmai 1.1 {
166     #if defined GENCGC
167 rtoy 1.2 caddr_t fault_addr = code->si_addr;
168     int page_index = find_page_index((void *) fault_addr);
169 pmai 1.1
170     #if SIGSEGV_VERBOSE
171 rtoy 1.2 fprintf(stderr, "Signal %d, fault_addr=%x, page_index=%d:\n",
172     signal, fault_addr, page_index);
173 pmai 1.1 #endif
174    
175 rtoy 1.2 /* Check if the fault is within the dynamic space. */
176     if (page_index != -1) {
177     /* Un-protect the page */
178    
179     /* The page should have been marked write protected */
180     if (!PAGE_WRITE_PROTECTED(page_index))
181     fprintf(stderr,
182     "*** Sigsegv in page not marked as write protected\n");
183    
184     os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
185     page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
186     page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
187 pmai 1.1
188 rtoy 1.2 return;
189 pmai 1.1 }
190     #endif
191    
192 rtoy 1.2 SAVE_CONTEXT();
193 pmai 1.1
194 rtoy 1.2 DPRINTF(0, (stderr, "sigsegv:\n"));
195     interrupt_handle_now(signal, code, context);
196 pmai 1.1 }
197    
198 rtoy 1.2 static void
199     sigbus_handler(HANDLER_ARGS)
200 pmai 1.1 {
201 rtoy 1.2 SAVE_CONTEXT();
202 pmai 1.1
203 rtoy 1.2 DPRINTF(0, (stderr, "sigbus:\n"));
204     interrupt_handle_now(signal, code, context);
205 pmai 1.1 }
206    
207 rtoy 1.2 void
208     os_install_interrupt_handlers(void)
209 pmai 1.1 {
210 rtoy 1.2 interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
211     interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
212 pmai 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.5