diff options
Diffstat (limited to 'mps/code/protso.c')
| -rw-r--r-- | mps/code/protso.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/mps/code/protso.c b/mps/code/protso.c new file mode 100644 index 00000000000..192a61b3323 --- /dev/null +++ b/mps/code/protso.c | |||
| @@ -0,0 +1,198 @@ | |||
| 1 | /* impl.c.protso: PROTECTION FOR SOLARIS | ||
| 2 | * | ||
| 3 | * $HopeName: MMsrc!protso.c(trunk.5) $ | ||
| 4 | * Copyright (C) 1995,1997 Harlequin Group, all rights reserved | ||
| 5 | * | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "mpm.h" | ||
| 9 | |||
| 10 | #ifndef MPS_OS_SO | ||
| 11 | #error "protso.c is Solaris specific, but MPS_OS_SO is not set" | ||
| 12 | #endif | ||
| 13 | #ifndef PROTECTION | ||
| 14 | #error "protso.c implements protection, but PROTECTION is not set" | ||
| 15 | #endif | ||
| 16 | |||
| 17 | /* open sesame magic */ | ||
| 18 | #define _POSIX_SOURCE | ||
| 19 | #define _POSIX_C_SOURCE 199309L | ||
| 20 | |||
| 21 | #include <limits.h> | ||
| 22 | #include <stddef.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <signal.h> | ||
| 25 | #include <siginfo.h> | ||
| 26 | #include <sys/mman.h> | ||
| 27 | |||
| 28 | #ifndef MPS_OS_SO | ||
| 29 | #error "protso.c is Solaris specific, but MPS_OS_SO is not set" | ||
| 30 | #endif | ||
| 31 | |||
| 32 | SRCID(protso, "$HopeName: MMsrc!protso.c(trunk.5) $"); | ||
| 33 | |||
| 34 | |||
| 35 | /* Fix up unprototyped system calls. */ | ||
| 36 | /* Note that these are not fixed up by std.h because that only fixes */ | ||
| 37 | /* up discrepancies with ANSI. */ | ||
| 38 | |||
| 39 | extern int getpagesize(void); | ||
| 40 | extern pid_t getpid(void); | ||
| 41 | extern int kill(pid_t, int); | ||
| 42 | |||
| 43 | /* Crap that can't be included via "open sesame" */ | ||
| 44 | /* definitions for the sa_flags field */ | ||
| 45 | /* Where is the source for this? (which header files / man pages) @@ */ | ||
| 46 | #define SA_SIGINFO 0x00000008 | ||
| 47 | /* | ||
| 48 | * SIGSEGV signal codes | ||
| 49 | */ | ||
| 50 | |||
| 51 | #define SEGV_MAPERR 1 /* address not mapped to object */ | ||
| 52 | #define SEGV_ACCERR 2 /* invalid permissions */ | ||
| 53 | #define NSIGSEGV 2 | ||
| 54 | |||
| 55 | |||
| 56 | /* The previously-installed signal action, as returned by */ | ||
| 57 | /* sigaction(3). See ProtSetup. */ | ||
| 58 | |||
| 59 | static struct sigaction sigNext; | ||
| 60 | |||
| 61 | |||
| 62 | /* sigHandle -- protection signal handler | ||
| 63 | * | ||
| 64 | * This is the signal handler installed by ProtSetup to deal with | ||
| 65 | * protection faults. It is installed on the SIGSEGV signal. | ||
| 66 | * It decodes the protection fault details from the signal context | ||
| 67 | * and passes them to ArenaAccess, which attempts to handle the | ||
| 68 | * fault and remove its cause. If the fault is handled, then | ||
| 69 | * the handler returns and execution resumes. If it isn't handled, | ||
| 70 | * then sigHandle does its best to pass the signal on to the | ||
| 71 | * previously installed signal handler (sigNext). | ||
| 72 | * | ||
| 73 | * .sigh.addr: We assume that the OS decodes the address to something | ||
| 74 | * sensible | ||
| 75 | * .sigh.limit: We throw away the limit information. | ||
| 76 | */ | ||
| 77 | |||
| 78 | static void sigHandle(int sig, siginfo_t *info, void *context) | ||
| 79 | { | ||
| 80 | AVER(sig == SIGSEGV); | ||
| 81 | AVER(info != NULL); | ||
| 82 | |||
| 83 | if(info->si_code == SEGV_ACCERR) { | ||
| 84 | AccessSet mode; | ||
| 85 | Addr base, limit; | ||
| 86 | |||
| 87 | /* We can't determine the access mode (read, write, etc.) */ | ||
| 88 | /* under Solaris without decoding the faulting instruction. */ | ||
| 89 | /* Don't bother, yet. We can do this if necessary. */ | ||
| 90 | |||
| 91 | mode = AccessREAD | AccessWRITE; | ||
| 92 | |||
| 93 | /* We assume that the access is for one word at the address. */ | ||
| 94 | /* (Nb. ldd has to be dword aligned, | ||
| 95 | * hence cannot cross a page boundary) */ | ||
| 96 | |||
| 97 | base = (Addr)info->si_addr; | ||
| 98 | limit = AddrAdd(base, (Size)sizeof(Addr)); | ||
| 99 | |||
| 100 | /* Offer each protection structure the opportunity to handle the */ | ||
| 101 | /* exception. If it succeeds, then allow the mutator to continue. */ | ||
| 102 | |||
| 103 | /* MutatorFaultContext parameter is a dummy parameter for this */ | ||
| 104 | /* implementation */ | ||
| 105 | if(ArenaAccess(base, mode, NULL)) | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | /* The exception was not handled by any known protection structure, */ | ||
| 110 | /* so throw it to the previously installed handler. */ | ||
| 111 | |||
| 112 | /* @@ This is really weak. | ||
| 113 | * Need to implement rest of the contract of sigaction */ | ||
| 114 | (*sigNext.sa_handler)(sig, info, context); | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | /* ProtSetup -- global protection setup | ||
| 119 | * | ||
| 120 | * Under Solaris, the global setup involves installing a signal handler | ||
| 121 | * on SIGSEGV to catch and handle protection faults (see sigHandle). | ||
| 122 | * The previous handler is recorded so that it can be reached from | ||
| 123 | * sigHandle if it fails to handle the fault. | ||
| 124 | * | ||
| 125 | * NOTE: There are problems with this approach: | ||
| 126 | * 1. we can't honor the wishes of the sigvec(2) entry for the | ||
| 127 | * previous handler, | ||
| 128 | * 2. what if this thread is suspended just after calling signal(3)? | ||
| 129 | * The sigNext variable will never be initialized! | ||
| 130 | */ | ||
| 131 | |||
| 132 | void ProtSetup(void) | ||
| 133 | { | ||
| 134 | struct sigaction sa; | ||
| 135 | int result; | ||
| 136 | |||
| 137 | sa.sa_handler = sigHandle; | ||
| 138 | sa.sa_flags = SA_SIGINFO; | ||
| 139 | |||
| 140 | result = sigaction(SIGSEGV, &sa, &sigNext); | ||
| 141 | AVER(result == 0); | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | /* ProtSet -- set protection | ||
| 146 | * | ||
| 147 | * This is just a thin veneer on top of mprotect(2). | ||
| 148 | */ | ||
| 149 | |||
| 150 | void ProtSet(Addr base, Addr limit, AccessSet mode) | ||
| 151 | { | ||
| 152 | int flags; | ||
| 153 | |||
| 154 | AVER(sizeof(int) == sizeof(Addr)); | ||
| 155 | AVER(base < limit); | ||
| 156 | AVER(base != 0); | ||
| 157 | AVER(AddrOffset(base, limit) <= INT_MAX); /* should be redundant */ | ||
| 158 | |||
| 159 | flags = PROT_READ | PROT_WRITE | PROT_EXEC; | ||
| 160 | if((mode & AccessREAD) != 0) | ||
| 161 | flags &= ~PROT_READ; | ||
| 162 | if((mode & AccessWRITE) != 0) | ||
| 163 | flags &= ~PROT_WRITE; | ||
| 164 | |||
| 165 | if(mprotect((caddr_t)base, (int)AddrOffset(base, limit), flags) != 0) | ||
| 166 | NOTREACHED; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | /* ProtSync -- synchronize protection settings with hardware | ||
| 171 | * | ||
| 172 | * This does nothing under Solaris. | ||
| 173 | */ | ||
| 174 | |||
| 175 | void ProtSync(Arena arena) | ||
| 176 | { | ||
| 177 | NOOP; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | /* ProtTramp -- protection trampoline | ||
| 183 | * | ||
| 184 | * The protection trampoline is trivial under Solaris, as there is nothing | ||
| 185 | * that needs to be done in the dynamic context of the mutator in order | ||
| 186 | * to catch faults. (Contrast this with Win32 Structured Exception | ||
| 187 | * Handling.) | ||
| 188 | */ | ||
| 189 | |||
| 190 | void ProtTramp(void **resultReturn, void *(*f)(void *, size_t), | ||
| 191 | void *p, size_t s) | ||
| 192 | { | ||
| 193 | AVER(resultReturn != NULL); | ||
| 194 | AVER(FUNCHECK(f)); | ||
| 195 | /* Can't check p and s as they are interpreted by the client */ | ||
| 196 | |||
| 197 | *resultReturn = (*f)(p, s); | ||
| 198 | } | ||