linux_sysvec.c revision 38354
1/*- 2 * Copyright (c) 1994-1996 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_sysvec.c,v 1.31 1998/07/29 16:43:00 bde Exp $ 29 */ 30 31/* XXX we use functions that might not exist. */ 32#include "opt_compat.h" 33 34#ifndef COMPAT_43 35#error "Unable to compile Linux-emulator due to missing COMPAT_43 option!" 36#endif 37 38#include <sys/param.h> 39#include <sys/buf.h> 40#include <sys/proc.h> 41#include <sys/systm.h> 42#include <sys/sysent.h> 43#include <sys/imgact.h> 44#include <sys/imgact_elf.h> 45#include <sys/signalvar.h> 46#include <sys/malloc.h> 47#include <vm/vm.h> 48#include <vm/vm_param.h> 49#include <vm/vm_prot.h> 50#include <vm/vm_page.h> 51#include <vm/vm_extern.h> 52#include <sys/exec.h> 53#include <sys/kernel.h> 54#include <machine/cpu.h> 55 56#include <i386/linux/linux.h> 57#include <i386/linux/linux_proto.h> 58 59static int linux_fixup __P((long **stack_base, 60 struct image_params *iparams)); 61static int elf_linux_fixup __P((long **stack_base, 62 struct image_params *iparams)); 63static void linux_prepsyscall __P((struct trapframe *tf, int *args, 64 u_int *code, caddr_t *params)); 65static void linux_sendsig __P((sig_t catcher, int sig, int mask, 66 u_long code)); 67 68/* 69 * Linux syscalls return negative errno's, we do positive and map them 70 */ 71static int bsd_to_linux_errno[ELAST] = { 72 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 73 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 74 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 75 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 76 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 77 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 78 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 79 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 80 -6, -43, -42 81}; 82 83int bsd_to_linux_signal[NSIG] = { 84 0, LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, 85 LINUX_SIGILL, LINUX_SIGTRAP, LINUX_SIGABRT, 0, 86 LINUX_SIGFPE, LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, 87 0, LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, 88 LINUX_SIGURG, LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, 89 LINUX_SIGCHLD, LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, 90 LINUX_SIGXCPU, LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, 91 LINUX_SIGWINCH, 0, LINUX_SIGUSR1, LINUX_SIGUSR2 92}; 93 94int linux_to_bsd_signal[LINUX_NSIG] = { 95 0, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGEMT, 96 SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM, 97 SIGBUS, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGIO, 98 SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGURG, SIGURG, 0 99}; 100 101/* 102 * If FreeBSD & Linux have a difference of opinion about what a trap 103 * means, deal with it here. 104 */ 105static int 106translate_traps(int signal, int trap_code) 107{ 108 if (signal != SIGBUS) 109 return signal; 110 switch (trap_code) { 111 case T_PROTFLT: 112 case T_TSSFLT: 113 case T_DOUBLEFLT: 114 case T_PAGEFLT: 115 return SIGSEGV; 116 default: 117 return signal; 118 } 119} 120 121static int 122linux_fixup(long **stack_base, struct image_params *imgp) 123{ 124 long *argv, *envp; 125 126 argv = *stack_base; 127 envp = *stack_base + (imgp->argc + 1); 128 (*stack_base)--; 129 **stack_base = (intptr_t)(void *)envp; 130 (*stack_base)--; 131 **stack_base = (intptr_t)(void *)argv; 132 (*stack_base)--; 133 **stack_base = imgp->argc; 134 return 0; 135} 136 137static int 138elf_linux_fixup(long **stack_base, struct image_params *imgp) 139{ 140 Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs; 141 long *pos; 142 143 pos = *stack_base + (imgp->argc + imgp->envc + 2); 144 145 if (args->trace) { 146 AUXARGS_ENTRY(pos, AT_DEBUG, 1); 147 } 148 if (args->execfd != -1) { 149 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 150 } 151 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 152 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 153 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 154 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 155 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 156 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 157 AUXARGS_ENTRY(pos, AT_BASE, args->base); 158 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid); 159 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid); 160 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid); 161 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid); 162 AUXARGS_ENTRY(pos, AT_NULL, 0); 163 164 free(imgp->auxargs, M_TEMP); 165 imgp->auxargs = NULL; 166 167 (*stack_base)--; 168 **stack_base = (long)imgp->argc; 169 return 0; 170} 171 172extern int _ucodesel, _udatasel; 173 174/* 175 * Send an interrupt to process. 176 * 177 * Stack is set up to allow sigcode stored 178 * in u. to call routine, followed by kcall 179 * to sigreturn routine below. After sigreturn 180 * resets the signal mask, the stack, and the 181 * frame pointer, it returns to the user 182 * specified pc, psl. 183 */ 184 185static void 186linux_sendsig(sig_t catcher, int sig, int mask, u_long code) 187{ 188 register struct proc *p = curproc; 189 register struct trapframe *regs; 190 struct linux_sigframe *fp, frame; 191 struct sigacts *psp = p->p_sigacts; 192 int oonstack; 193 194 regs = p->p_md.md_regs; 195 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 196 197#ifdef DEBUG 198 printf("Linux-emul(%ld): linux_sendsig(%p, %d, %d, %lu)\n", 199 (long)p->p_pid, catcher, sig, mask, code); 200#endif 201 /* 202 * Allocate space for the signal handler context. 203 */ 204 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack && 205 (psp->ps_sigonstack & sigmask(sig))) { 206 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp + 207 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe)); 208 psp->ps_sigstk.ss_flags |= SS_ONSTACK; 209 } else { 210 fp = (struct linux_sigframe *)regs->tf_esp - 1; 211 } 212 213 /* 214 * grow() will return FALSE if the fp will not fit inside the stack 215 * and the stack can not be grown. useracc will return FALSE 216 * if access is denied. 217 */ 218 if ((grow(p, (int)fp) == FALSE) || 219 (useracc((caddr_t)fp, sizeof (struct linux_sigframe), B_WRITE) == FALSE)) { 220 /* 221 * Process has trashed its stack; give it an illegal 222 * instruction to halt it in its tracks. 223 */ 224 SIGACTION(p, SIGILL) = SIG_DFL; 225 sig = sigmask(SIGILL); 226 p->p_sigignore &= ~sig; 227 p->p_sigcatch &= ~sig; 228 p->p_sigmask &= ~sig; 229 psignal(p, SIGILL); 230 return; 231 } 232 233 /* 234 * Build the argument list for the signal handler. 235 */ 236 if (p->p_sysent->sv_sigtbl) { 237 if (sig < p->p_sysent->sv_sigsize) 238 sig = p->p_sysent->sv_sigtbl[sig]; 239 else 240 sig = p->p_sysent->sv_sigsize + 1; 241 } 242 243 frame.sf_handler = catcher; 244 frame.sf_sig = sig; 245 246 /* 247 * Build the signal context to be used by sigreturn. 248 */ 249 frame.sf_sc.sc_mask = mask; 250 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs)); 251 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs)); 252 frame.sf_sc.sc_es = regs->tf_es; 253 frame.sf_sc.sc_ds = regs->tf_ds; 254 frame.sf_sc.sc_edi = regs->tf_edi; 255 frame.sf_sc.sc_esi = regs->tf_esi; 256 frame.sf_sc.sc_ebp = regs->tf_ebp; 257 frame.sf_sc.sc_ebx = regs->tf_ebx; 258 frame.sf_sc.sc_edx = regs->tf_edx; 259 frame.sf_sc.sc_ecx = regs->tf_ecx; 260 frame.sf_sc.sc_eax = regs->tf_eax; 261 frame.sf_sc.sc_eip = regs->tf_eip; 262 frame.sf_sc.sc_cs = regs->tf_cs; 263 frame.sf_sc.sc_eflags = regs->tf_eflags; 264 frame.sf_sc.sc_esp_at_signal = regs->tf_esp; 265 frame.sf_sc.sc_ss = regs->tf_ss; 266 frame.sf_sc.sc_err = regs->tf_err; 267 frame.sf_sc.sc_trapno = code; /* XXX ???? */ 268 269 if (copyout(&frame, fp, sizeof(frame)) != 0) { 270 /* 271 * Process has trashed its stack; give it an illegal 272 * instruction to halt it in its tracks. 273 */ 274 sigexit(p, SIGILL); 275 /* NOTREACHED */ 276 } 277 278 /* 279 * Build context to run handler in. 280 */ 281 regs->tf_esp = (int)fp; 282 regs->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode)); 283 regs->tf_eflags &= ~PSL_VM; 284 regs->tf_cs = _ucodesel; 285 regs->tf_ds = _udatasel; 286 regs->tf_es = _udatasel; 287 regs->tf_ss = _udatasel; 288} 289 290/* 291 * System call to cleanup state after a signal 292 * has been taken. Reset signal mask and 293 * stack state from context left by sendsig (above). 294 * Return to previous pc and psl as specified by 295 * context left by sendsig. Check carefully to 296 * make sure that the user has not modified the 297 * psl to gain improper privileges or to cause 298 * a machine fault. 299 */ 300int 301linux_sigreturn(p, args) 302 struct proc *p; 303 struct linux_sigreturn_args *args; 304{ 305 struct linux_sigcontext *scp, context; 306 register struct trapframe *regs; 307 int eflags; 308 309 regs = p->p_md.md_regs; 310 311#ifdef DEBUG 312 printf("Linux-emul(%ld): linux_sigreturn(%p)\n", 313 (long)p->p_pid, (void *)args->scp); 314#endif 315 /* 316 * The trampoline code hands us the context. 317 * It is unsafe to keep track of it ourselves, in the event that a 318 * program jumps out of a signal handler. 319 */ 320 scp = SCARG(args,scp); 321 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0) 322 return (EFAULT); 323 324 /* 325 * Check for security violations. 326 */ 327#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 328 eflags = context.sc_eflags; 329 /* 330 * XXX do allow users to change the privileged flag PSL_RF. The 331 * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 332 * sometimes set it there too. tf_eflags is kept in the signal 333 * context during signal handling and there is no other place 334 * to remember it, so the PSL_RF bit may be corrupted by the 335 * signal handler without us knowing. Corruption of the PSL_RF 336 * bit at worst causes one more or one less debugger trap, so 337 * allowing it is fairly harmless. 338 */ 339 if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { 340 return(EINVAL); 341 } 342 343 /* 344 * Don't allow users to load a valid privileged %cs. Let the 345 * hardware check for invalid selectors, excess privilege in 346 * other selectors, invalid %eip's and invalid %esp's. 347 */ 348#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 349 if (!CS_SECURE(context.sc_cs)) { 350 trapsignal(p, SIGBUS, T_PROTFLT); 351 return(EINVAL); 352 } 353 354 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK; 355 p->p_sigmask = context.sc_mask &~ 356 (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)); 357 /* 358 * Restore signal context. 359 */ 360 /* %fs and %gs were restored by the trampoline. */ 361 regs->tf_es = context.sc_es; 362 regs->tf_ds = context.sc_ds; 363 regs->tf_edi = context.sc_edi; 364 regs->tf_esi = context.sc_esi; 365 regs->tf_ebp = context.sc_ebp; 366 regs->tf_ebx = context.sc_ebx; 367 regs->tf_edx = context.sc_edx; 368 regs->tf_ecx = context.sc_ecx; 369 regs->tf_eax = context.sc_eax; 370 regs->tf_eip = context.sc_eip; 371 regs->tf_cs = context.sc_cs; 372 regs->tf_eflags = eflags; 373 regs->tf_esp = context.sc_esp_at_signal; 374 regs->tf_ss = context.sc_ss; 375 376 return (EJUSTRETURN); 377} 378 379static void 380linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params) 381{ 382 args[0] = tf->tf_ebx; 383 args[1] = tf->tf_ecx; 384 args[2] = tf->tf_edx; 385 args[3] = tf->tf_esi; 386 args[4] = tf->tf_edi; 387 *params = NULL; /* no copyin */ 388} 389 390struct sysentvec linux_sysvec = { 391 LINUX_SYS_MAXSYSCALL, 392 linux_sysent, 393 0xff, 394 NSIG, 395 bsd_to_linux_signal, 396 ELAST, 397 bsd_to_linux_errno, 398 translate_traps, 399 linux_fixup, 400 linux_sendsig, 401 linux_sigcode, 402 &linux_szsigcode, 403 linux_prepsyscall, 404 "Linux a.out" 405}; 406 407struct sysentvec elf_linux_sysvec = { 408 LINUX_SYS_MAXSYSCALL, 409 linux_sysent, 410 0xff, 411 NSIG, 412 bsd_to_linux_signal, 413 ELAST, 414 bsd_to_linux_errno, 415 translate_traps, 416 elf_linux_fixup, 417 linux_sendsig, 418 linux_sigcode, 419 &linux_szsigcode, 420 linux_prepsyscall, 421 "Linux ELF" 422}; 423 424/* 425 * Installed either via SYSINIT() or via LKM stubs. 426 */ 427Elf32_Brandinfo linux_brand = { 428 "Linux", 429 "/compat/linux", 430 "/lib/ld-linux.so.1", 431 &elf_linux_sysvec 432 }; 433 434#ifndef LKM 435/* 436 * XXX: this is WRONG, it needs to be SI_SUB_EXEC, but this is just at the 437 * "proof of concept" stage and will be fixed shortly 438 */ 439static void linux_elf_init __P((void *dummy)); 440 441static void 442linux_elf_init(dummy) 443 void *dummy; 444{ 445 if (elf_insert_brand_entry(&linux_brand) < 0) 446 printf("cannot insert Linux elf brand handler\n"); 447 else if (bootverbose) 448 printf("Linux-ELF exec handler installed\n"); 449} 450 451SYSINIT(linuxelf, SI_SUB_VFS, SI_ORDER_ANY, linux_elf_init, NULL); 452#endif 453