Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/solaris-os.c,v 1.1 1997/09/04 08:46:02 dtc Exp $
*
* OS-dependent routines. This file (along with os.h) exports an
* OS-independent interface to the operating system VM facilities.
* Suprisingly, this interface looks a lot like the Mach interface
* (but simpler in some places). For some operating systems, a subset
* of these functions will have to be emulated.
*
* This is an experimental Solaris version based on sunos-os.c but
* without the VM hack of unmapped pages for the GC trigger which
* caused trouble when system calls were passed unmapped pages.
*
*/
#define DEBUG
#include <stdio.h>
#include <signal.h>
#include <sys/file.h>
#include <unistd.h>
#include <errno.h>
#include <sys/param.h>
#define OS_PROTERR SEGV_ACCERR
#define OS_MAPERR SEGV_MAPERR
#define OS_HASERRNO(code) ((code)->si_errno != 0)
#define OS_ERRNO(code) ((code)->si_errno)
#include "os.h"
/* block size must be larger than the system page size */
#define SPARSE_BLOCK_SIZE (1<<15)
#define SPARSE_SIZE_MASK (SPARSE_BLOCK_SIZE-1)
#define PROT_DEFAULT OS_VM_PROT_ALL
#define OFFSET_NONE ((os_vm_offset_t)(~0))
#define EMPTYFILE "/tmp/empty"
#define ZEROFILE "/dev/zero"
#define INITIAL_MAX_SEGS 32
#define GROW_MAX_SEGS 16
extern char *getenv();
/* ---------------------------------------------------------------- */
#define ADJ_OFFSET(off,adj) (((off)==OFFSET_NONE) ? OFFSET_NONE : ((off)+(adj)))
long os_vm_page_size=(-1);
static long os_real_page_size=(-1);
static int zero_fd=(-1), empty_fd=(-1);
static os_vm_size_t real_page_size_difference=0;
static void os_init_bailout(arg)
char *arg;
{
char buf[500];
sprintf(buf,"os_init: %s",arg);
perror(buf);
exit(1);
}
void os_init()
{
char *empty_file=getenv("CMUCL_EMPTYFILE");
if(empty_file==NULL)
empty_file=EMPTYFILE;
empty_fd=open(empty_file,O_RDONLY|O_CREAT);
if(empty_fd<0)
os_init_bailout(empty_file);
unlink(empty_file);
zero_fd=open(ZEROFILE,O_RDONLY);
if(zero_fd<0)
os_init_bailout(ZEROFILE);
os_vm_page_size = os_real_page_size = sysconf(_SC_PAGESIZE);
if(os_vm_page_size>OS_VM_DEFAULT_PAGESIZE){
fprintf(stderr,"os_init: Pagesize too large (%d > %d)\n",
os_vm_page_size,OS_VM_DEFAULT_PAGESIZE);
exit(1);
}else{
/*
* we do this because there are apparently dependencies on
* the pagesize being OS_VM_DEFAULT_PAGESIZE somewhere...
* but since the OS doesn't know we're using this restriction,
* we have to grovel around a bit to enforce it, thus anything
* that uses real_page_size_difference.
*/
real_page_size_difference=OS_VM_DEFAULT_PAGESIZE-os_vm_page_size;
os_vm_page_size=OS_VM_DEFAULT_PAGESIZE;
}
}
/* ---------------------------------------------------------------- */
os_vm_address_t
os_validate(os_vm_address_t addr, os_vm_size_t len)
{
int flags = MAP_PRIVATE|MAP_NORESERVE;
if(addr) flags|=MAP_FIXED;
if((addr=mmap(addr,len,OS_VM_PROT_ALL,flags,zero_fd,0)) == (os_vm_address_t) -1)
perror("mmap");
return addr;
}
void
os_invalidate(os_vm_address_t addr, os_vm_size_t len)
{
if(munmap(addr,len) == -1)
perror("munmap");
}
os_vm_address_t
os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
{
if((addr=mmap(addr,len,OS_VM_PROT_ALL,MAP_PRIVATE|MAP_FIXED,fd,
(off_t) offset)) == (os_vm_address_t) -1)
perror("mmap");
return addr;
}
void os_flush_icache(address, length)
os_vm_address_t address;
os_vm_size_t length;
{
static int flushit = -1;
/*
* On some systems, iflush needs to be emulated in the kernel
* On those systems, it isn't necessary
* Call getenv() only once.
*/
if (flushit == -1)
flushit = getenv("CMUCL_NO_SPARC_IFLUSH") == 0;
if (flushit) {
static int traceit = -1;
if (traceit == -1)
traceit = getenv("CMUCL_TRACE_SPARC_IFLUSH") != 0;
if (traceit)
fprintf(stderr,";;;iflush %p - %x\n", address,length);
flush_icache(address,length);
}
}
void
os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
{
if(mprotect(address, length, prot) == -1)
perror("mprotect");
}
boolean valid_addr(test)
os_vm_address_t test;
{
/* XXX needs implementing */
return 1;
}
/* ---------------------------------------------------------------- */
static boolean maybe_gc(HANDLER_ARGS)
{
/*
* It's necessary to enable recursive SEGVs, since the handler is
* used for multiple things (e.g., both gc-trigger & faulting in pages).
* We check against recursive gc's though...
*/
boolean did_gc;
static already_trying=0;
if(already_trying)
return FALSE;
SAVE_CONTEXT();
#ifdef POSIX_SIGS
sigprocmask(SIG_SETMASK, &context->uc_sigmask,0);
#else
sigsetmask(context->sc_mask);
#endif
already_trying=TRUE;
did_gc=interrupt_maybe_gc(signal, code, context);
already_trying=FALSE;
return did_gc;
}
/*
* The primary point of catching segmentation violations is to allow
* read only memory to be re-mapped with more permissions when a write
* is attempted. this greatly decreases the residency of the program
* in swap space since read only areas don't take up room
*
* Running into the gc trigger page will also end up here...
*/
void segv_handler(HANDLER_ARGS)
{
caddr_t addr = code->si_addr;
SAVE_CONTEXT();
if(maybe_gc(signal, code, context))
/* we just garbage collected */
return;
else{
/* a *real* protection fault */
fprintf(stderr,
"segv_handler: Real protection violation: 0x%08x\n",
addr);
interrupt_handle_now(signal,code,context);
}
}
void os_install_interrupt_handlers()
{
interrupt_install_low_level_handler(SIGSEGV,segv_handler);
}
/* function defintions for register lvalues */
int * solaris_register_address(struct ucontext *context, int reg)
{
if (reg == 0) {
static int zero;
zero = 0;
return &zero;
} else if (reg < 16) {
return &context->uc_mcontext.gregs[reg+3];
} else if (reg < 32) {
int *sp = (int*) context->uc_mcontext.gregs[REG_SP];
return &sp[reg-16];
} else
return 0;
}
/* function defintions for backward compatibilty and static linking */
/* For now we put in some porting functions */
#ifndef SOLARIS25
int
getdtablesize(void)
{
return sysconf(_SC_OPEN_MAX);
}
char *
getwd(char *path)
{
return getcwd(path, MAXPATHLEN);
}
int
getpagesize(void)
{
return sysconf(_SC_PAGESIZE);
}
#include <sys/procfs.h>
/* Old rusage definition */
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss;
#define ru_first ru_ixrss
long ru_ixrss; /* XXX: 0 */
long ru_idrss; /* XXX: sum of rm_asrss */
long ru_isrss; /* XXX: 0 */
long ru_minflt; /* any page faults not requiring I/O */
long ru_majflt; /* any page faults requiring I/O */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary " */
#define ru_last ru_nivcsw
};
int
getrusage(int who, struct rusage *rp)
{
memset(rp, 0, sizeof(struct rusage));
return 0;
}
int
setreuid()
{
fprintf(stderr,"setreuid unimplemented\n");
errno = ENOSYS;
return -1;
}
int
setregid()
{
fprintf(stderr,"setregid unimplemented\n");
errno = ENOSYS;
return -1;
}
int
gethostid()
{
fprintf(stderr,"gethostid unimplemented\n");
errno = ENOSYS;
return -1;
}
int
killpg(int pgrp, int sig)
{
if (pgrp < 0) {
errno = ESRCH;
return -1;
}
return kill(-pgrp, sig);
}
#endif
int
sigblock(int mask)
{
sigset_t old, new;
sigemptyset(&new);
new.__sigbits[0] = mask;
sigprocmask(SIG_BLOCK, &new, &old);
return old.__sigbits[0];
}
#ifndef SOLARIS25
int
wait3(int *status, int options, struct rusage *rp)
{
if (rp)
memset(rp, 0, sizeof(struct rusage));
return waitpid(-1, status, options);
}
#endif
int
sigsetmask(int mask)
{
sigset_t old, new;
sigemptyset(&new);
new.__sigbits[0] = mask;
sigprocmask(SIG_SETMASK, &new, &old);
return old.__sigbits[0];
}