linux_sysvec.c revision 14331
1/*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software withough specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: linux_sysent.c,v 1.4 1996/01/14 10:59:57 sos Exp $ 29 */ 30 31/* XXX we use functions that might not exist. */ 32#define COMPAT_43 1 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/sysproto.h> 37#include <sys/sysent.h> 38#include <sys/imgact.h> 39#include <sys/signalvar.h> 40#include <vm/vm.h> 41#include <vm/vm_param.h> 42#include <vm/vm_prot.h> 43#include <vm/lock.h> 44#include <vm/vm_kern.h> 45#include <vm/vm_object.h> 46#include <vm/vm_page.h> 47#include <vm/vm_map.h> 48#include <vm/vm_pager.h> 49#include <vm/vm_extern.h> 50#include <sys/user.h> 51#include <sys/exec.h> 52#include <machine/cpu.h> 53#include <machine/frame.h> 54#include <machine/reg.h> 55#include <machine/specialreg.h> 56#include <machine/psl.h> 57#include <machine/sysarch.h> 58#include <machine/md_var.h> 59 60#include <i386/linux/linux.h> 61#include <i386/linux/linux_proto.h> 62#include <i386/linux/linux_syscall.h> 63 64/* 65 * Linux syscalls return negative errno's, we do positive and map them 66 */ 67int bsd_to_linux_errno[ELAST] = { 68 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 69 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 70 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 71 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 72 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 73 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 74 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 75 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 76 -6, 77}; 78 79int bsd_to_linux_signal[NSIG] = { 80 0, LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, 81 LINUX_SIGILL, LINUX_SIGTRAP, LINUX_SIGABRT, 0, 82 LINUX_SIGFPE, LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, 83 0, LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, 84 LINUX_SIGURG, LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, 85 LINUX_SIGCHLD, LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, 86 LINUX_SIGXCPU, LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, 87 LINUX_SIGWINCH, 0, LINUX_SIGUSR1, LINUX_SIGUSR2 88}; 89 90int linux_to_bsd_signal[LINUX_NSIG] = { 91 0, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGEMT, 92 SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM, 93 SIGBUS, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGIO, 94 SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGURG, SIGURG, 0 95}; 96 97int linux_fixup(int **stack_base, struct image_params *imgp) 98{ 99 int *argv, *envp; 100 101 argv = *stack_base; 102 envp = *stack_base + (imgp->argc + 1); 103 (*stack_base)--; 104 **stack_base = (int)envp; 105 (*stack_base)--; 106 **stack_base = (int)argv; 107 (*stack_base)--; 108 **stack_base = (int)imgp->argc; 109 return 0; /* XXX */ 110} 111 112extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 113 114static void linux_sendsig(sig_t action, 115 int sig, 116 int returnmask, 117 unsigned code); 118 119extern int _ucodesel, _udatasel; 120 121/* 122 * Send an interrupt to process. 123 * 124 * Stack is set up to allow sigcode stored 125 * in u. to call routine, followed by kcall 126 * to sigreturn routine below. After sigreturn 127 * resets the signal mask, the stack, and the 128 * frame pointer, it returns to the user 129 * specified pc, psl. 130 */ 131 132static void 133linux_sendsig(catcher, sig, mask, code) 134 sig_t catcher; 135 int sig; 136 int mask; 137 unsigned code; 138{ 139 register struct proc *p = curproc; 140 register int *regs; 141 struct linux_sigframe *fp, frame; 142 struct sigacts *psp = p->p_sigacts; 143 int oonstack; 144 145 regs = p->p_md.md_regs; 146 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 147 148#ifdef DEBUG 149 printf("Linux-emul(%d): linux_sendsig(%8x, %d, %d, %d)\n", 150 p->p_pid, catcher, sig, mask, code); 151#endif 152 /* 153 * Allocate space for the signal handler context. 154 */ 155 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack && 156 (psp->ps_sigonstack & sigmask(sig))) { 157 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp + 158 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe)); 159 psp->ps_sigstk.ss_flags |= SS_ONSTACK; 160 } else { 161 fp = (struct linux_sigframe *)regs[tESP] - 1; 162 } 163 164 /* 165 * grow() will return FALSE if the fp will not fit inside the stack 166 * and the stack can not be grown. useracc will return FALSE 167 * if access is denied. 168 */ 169 if ((grow(p, (int)fp) == FALSE) || 170 (useracc((caddr_t)fp, sizeof (struct linux_sigframe), B_WRITE) == FALSE)) { 171 /* 172 * Process has trashed its stack; give it an illegal 173 * instruction to halt it in its tracks. 174 */ 175 SIGACTION(p, SIGILL) = SIG_DFL; 176 sig = sigmask(SIGILL); 177 p->p_sigignore &= ~sig; 178 p->p_sigcatch &= ~sig; 179 p->p_sigmask &= ~sig; 180 psignal(p, SIGILL); 181 return; 182 } 183 184 /* 185 * Build the argument list for the signal handler. 186 */ 187 if (p->p_sysent->sv_sigtbl) { 188 if (sig < p->p_sysent->sv_sigsize) 189 sig = p->p_sysent->sv_sigtbl[sig]; 190 else 191 sig = p->p_sysent->sv_sigsize + 1; 192 } 193 194 frame.sf_handler = catcher; 195 frame.sf_sig = sig; 196 197 /* 198 * Build the signal context to be used by sigreturn. 199 */ 200 frame.sf_sc.sc_mask = mask; 201 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs)); 202 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs)); 203 frame.sf_sc.sc_es = regs[tES]; 204 frame.sf_sc.sc_ds = regs[tDS]; 205 frame.sf_sc.sc_edi = regs[tEDI]; 206 frame.sf_sc.sc_esi = regs[tESI]; 207 frame.sf_sc.sc_ebp = regs[tEBP]; 208 frame.sf_sc.sc_ebx = regs[tEBX]; 209 frame.sf_sc.sc_edx = regs[tEDX]; 210 frame.sf_sc.sc_ecx = regs[tECX]; 211 frame.sf_sc.sc_eax = regs[tEAX]; 212 frame.sf_sc.sc_eip = regs[tEIP]; 213 frame.sf_sc.sc_cs = regs[tCS]; 214 frame.sf_sc.sc_eflags = regs[tEFLAGS]; 215 frame.sf_sc.sc_esp_at_signal = regs[tESP]; 216 frame.sf_sc.sc_ss = regs[tSS]; 217 frame.sf_sc.sc_err = regs[tERR]; 218 frame.sf_sc.sc_trapno = code; /* XXX ???? */ 219 220 if (copyout(&frame, fp, sizeof(frame)) != 0) { 221 /* 222 * Process has trashed its stack; give it an illegal 223 * instruction to halt it in its tracks. 224 */ 225 sigexit(p, SIGILL); 226 /* NOTREACHED */ 227 } 228 229 /* 230 * Build context to run handler in. 231 */ 232 regs[tESP] = (int)fp; 233 regs[tEIP] = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode)); 234 regs[tEFLAGS] &= ~PSL_VM; 235 regs[tCS] = _ucodesel; 236 regs[tDS] = _udatasel; 237 regs[tES] = _udatasel; 238 regs[tSS] = _udatasel; 239} 240 241/* 242 * System call to cleanup state after a signal 243 * has been taken. Reset signal mask and 244 * stack state from context left by sendsig (above). 245 * Return to previous pc and psl as specified by 246 * context left by sendsig. Check carefully to 247 * make sure that the user has not modified the 248 * psl to gain improper privileges or to cause 249 * a machine fault. 250 */ 251int 252linux_sigreturn(p, args, retval) 253 struct proc *p; 254 struct linux_sigreturn_args *args; 255 int *retval; 256{ 257 struct linux_sigcontext *scp, context; 258 register int *regs; 259 int eflags; 260 261 regs = p->p_md.md_regs; 262 263#ifdef DEBUG 264 printf("Linux-emul(%d): linux_sigreturn(%8x)\n", p->p_pid, args->scp); 265#endif 266 /* 267 * The trampoline code hands us the context. 268 * It is unsafe to keep track of it ourselves, in the event that a 269 * program jumps out of a signal handler. 270 */ 271 scp = args->scp; 272 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0) 273 return (EFAULT); 274 275 /* 276 * Check for security violations. 277 */ 278#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 279 eflags = context.sc_eflags; 280 /* 281 * XXX do allow users to change the privileged flag PSL_RF. The 282 * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 283 * sometimes set it there too. tf_eflags is kept in the signal 284 * context during signal handling and there is no other place 285 * to remember it, so the PSL_RF bit may be corrupted by the 286 * signal handler without us knowing. Corruption of the PSL_RF 287 * bit at worst causes one more or one less debugger trap, so 288 * allowing it is fairly harmless. 289 */ 290 if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs[tEFLAGS] & ~PSL_RF)) { 291 return(EINVAL); 292 } 293 294 /* 295 * Don't allow users to load a valid privileged %cs. Let the 296 * hardware check for invalid selectors, excess privilege in 297 * other selectors, invalid %eip's and invalid %esp's. 298 */ 299#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 300 if (!CS_SECURE(context.sc_cs)) { 301 trapsignal(p, SIGBUS, T_PROTFLT); 302 return(EINVAL); 303 } 304 305 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK; 306 p->p_sigmask = context.sc_mask &~ 307 (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)); 308 /* 309 * Restore signal context. 310 */ 311 /* %fs and %gs were restored by the trampoline. */ 312 regs[tES] = context.sc_es; 313 regs[tDS] = context.sc_ds; 314 regs[tEDI] = context.sc_edi; 315 regs[tESI] = context.sc_esi; 316 regs[tEBP] = context.sc_ebp; 317 regs[tEBX] = context.sc_ebx; 318 regs[tEDX] = context.sc_edx; 319 regs[tECX] = context.sc_ecx; 320 regs[tEAX] = context.sc_eax; 321 regs[tEIP] = context.sc_eip; 322 regs[tCS] = context.sc_cs; 323 regs[tEFLAGS] = eflags; 324 regs[tESP] = context.sc_esp_at_signal; 325 regs[tSS] = context.sc_ss; 326 327 return (EJUSTRETURN); 328} 329 330static void 331linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params) 332{ 333 int i; 334 args[0] = tf->tf_ebx; 335 args[1] = tf->tf_ecx; 336 args[2] = tf->tf_edx; 337 args[3] = tf->tf_esi; 338 args[4] = tf->tf_edi; 339 *params = NULL; /* no copyin */ 340} 341 342extern char linux_sigcode[]; 343extern int linux_szsigcode; 344 345struct sysentvec linux_sysvec = { 346 sizeof (linux_sysent) / sizeof(linux_sysent[0]), 347 linux_sysent, 348 0xff, 349 NSIG, 350 bsd_to_linux_signal, 351 ELAST, 352 bsd_to_linux_errno, 353 linux_fixup, 354 linux_sendsig, 355 linux_sigcode, 356 &linux_szsigcode, 357 linux_prepsyscall, 358}; 359