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