linux_sysvec.c revision 36735
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.29 1998/05/07 00:42:25 eivind 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 = (long)envp; 130 (*stack_base)--; 131 **stack_base = (long)argv; 132 (*stack_base)--; 133 **stack_base = (long)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(%d): linux_sendsig(%8x, %d, %d, %ld)\n", 199 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(%d): linux_sigreturn(%8x)\n", p->p_pid, args->scp); 313#endif 314 /* 315 * The trampoline code hands us the context. 316 * It is unsafe to keep track of it ourselves, in the event that a 317 * program jumps out of a signal handler. 318 */ 319 scp = SCARG(args,scp); 320 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0) 321 return (EFAULT); 322 323 /* 324 * Check for security violations. 325 */ 326#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 327 eflags = context.sc_eflags; 328 /* 329 * XXX do allow users to change the privileged flag PSL_RF. The 330 * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 331 * sometimes set it there too. tf_eflags is kept in the signal 332 * context during signal handling and there is no other place 333 * to remember it, so the PSL_RF bit may be corrupted by the 334 * signal handler without us knowing. Corruption of the PSL_RF 335 * bit at worst causes one more or one less debugger trap, so 336 * allowing it is fairly harmless. 337 */ 338 if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { 339 return(EINVAL); 340 } 341 342 /* 343 * Don't allow users to load a valid privileged %cs. Let the 344 * hardware check for invalid selectors, excess privilege in 345 * other selectors, invalid %eip's and invalid %esp's. 346 */ 347#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 348 if (!CS_SECURE(context.sc_cs)) { 349 trapsignal(p, SIGBUS, T_PROTFLT); 350 return(EINVAL); 351 } 352 353 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK; 354 p->p_sigmask = context.sc_mask &~ 355 (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)); 356 /* 357 * Restore signal context. 358 */ 359 /* %fs and %gs were restored by the trampoline. */ 360 regs->tf_es = context.sc_es; 361 regs->tf_ds = context.sc_ds; 362 regs->tf_edi = context.sc_edi; 363 regs->tf_esi = context.sc_esi; 364 regs->tf_ebp = context.sc_ebp; 365 regs->tf_ebx = context.sc_ebx; 366 regs->tf_edx = context.sc_edx; 367 regs->tf_ecx = context.sc_ecx; 368 regs->tf_eax = context.sc_eax; 369 regs->tf_eip = context.sc_eip; 370 regs->tf_cs = context.sc_cs; 371 regs->tf_eflags = eflags; 372 regs->tf_esp = context.sc_esp_at_signal; 373 regs->tf_ss = context.sc_ss; 374 375 return (EJUSTRETURN); 376} 377 378static void 379linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params) 380{ 381 args[0] = tf->tf_ebx; 382 args[1] = tf->tf_ecx; 383 args[2] = tf->tf_edx; 384 args[3] = tf->tf_esi; 385 args[4] = tf->tf_edi; 386 *params = NULL; /* no copyin */ 387} 388 389struct sysentvec linux_sysvec = { 390 LINUX_SYS_MAXSYSCALL, 391 linux_sysent, 392 0xff, 393 NSIG, 394 bsd_to_linux_signal, 395 ELAST, 396 bsd_to_linux_errno, 397 translate_traps, 398 linux_fixup, 399 linux_sendsig, 400 linux_sigcode, 401 &linux_szsigcode, 402 linux_prepsyscall, 403 "Linux a.out" 404}; 405 406struct sysentvec elf_linux_sysvec = { 407 LINUX_SYS_MAXSYSCALL, 408 linux_sysent, 409 0xff, 410 NSIG, 411 bsd_to_linux_signal, 412 ELAST, 413 bsd_to_linux_errno, 414 translate_traps, 415 elf_linux_fixup, 416 linux_sendsig, 417 linux_sigcode, 418 &linux_szsigcode, 419 linux_prepsyscall, 420 "Linux ELF" 421}; 422 423/* 424 * Installed either via SYSINIT() or via LKM stubs. 425 */ 426Elf32_Brandinfo linux_brand = { 427 "Linux", 428 "/compat/linux", 429 "/lib/ld-linux.so.1", 430 &elf_linux_sysvec 431 }; 432 433#ifndef LKM 434/* 435 * XXX: this is WRONG, it needs to be SI_SUB_EXEC, but this is just at the 436 * "proof of concept" stage and will be fixed shortly 437 */ 438static void linux_elf_init __P((void *dummy)); 439 440static void 441linux_elf_init(dummy) 442 void *dummy; 443{ 444 if (elf_insert_brand_entry(&linux_brand) < 0) 445 printf("cannot insert Linux elf brand handler\n"); 446 else if (bootverbose) 447 printf("Linux-ELF exec handler installed\n"); 448} 449 450SYSINIT(linuxelf, SI_SUB_VFS, SI_ORDER_ANY, linux_elf_init, NULL); 451#endif 452