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_page.h> 51 #include <vm/vm_extern.h> 52 #include <sys/exec.h> 53 #include <sys/kernel.h> 54 #include <sys/module.h> 55 #include <machine/cpu.h> 56 57 #include <i386/linux/linux.h> 58 #include <i386/linux/linux_proto.h> 59 60 static int linux_fixup __P((long **stack_base, 61 struct image_params *iparams)); 62 static int elf_linux_fixup __P((long **stack_base, 63 struct image_params *iparams)); 64 static void linux_prepsyscall __P((struct trapframe *tf, int *args, 65 u_int *code, caddr_t *params)); 66 static void linux_sendsig __P((sig_t catcher, int sig, sigset_t *mask, 67 u_long code)); 68 69 /* 70 * Linux syscalls return negative errno's, we do positive and map them 71 */ 72 static int bsd_to_linux_errno[ELAST + 1] = { 73 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 74 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 75 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 76 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 77 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 78 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 79 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 80 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 81 -6, -6, -43, -42, -75, -6, -84 82 }; 83 84 int bsd_to_linux_signal[LINUX_SIGTBLSZ] = { 85 LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, LINUX_SIGILL, 86 LINUX_SIGTRAP, LINUX_SIGABRT, 0, LINUX_SIGFPE, 87 LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, 0, 88 LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, LINUX_SIGURG, 89 LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, LINUX_SIGCHLD, 90 LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, LINUX_SIGXCPU, 91 LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, LINUX_SIGWINCH, 92 0, LINUX_SIGUSR1, LINUX_SIGUSR2 93 }; 94 95 int linux_to_bsd_signal[LINUX_SIGTBLSZ] = { 96 SIGHUP, SIGINT, SIGQUIT, SIGILL, 97 SIGTRAP, SIGABRT, SIGBUS, SIGFPE, 98 SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, 99 SIGPIPE, SIGALRM, SIGTERM, SIGBUS, 100 SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, 101 SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, 102 SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, 103 SIGIO, SIGURG, 0 104 }; 105 106 /* 107 * If FreeBSD & Linux have a difference of opinion about what a trap 108 * means, deal with it here. 109 */ 110 static int 111 translate_traps(int signal, int trap_code) 112 { 113 if (signal != SIGBUS) 114 return signal; 115 switch (trap_code) { 116 case T_PROTFLT: 117 case T_TSSFLT: 118 case T_DOUBLEFLT: 119 case T_PAGEFLT: 120 return SIGSEGV; 121 default: 122 return signal; 123 } 124 } 125 126 static int 127 linux_fixup(long **stack_base, struct image_params *imgp) 128 { 129 long *argv, *envp; 130 131 argv = *stack_base; 132 envp = *stack_base + (imgp->argc + 1); 133 (*stack_base)--; 134 **stack_base = (intptr_t)(void *)envp; 135 (*stack_base)--; 136 **stack_base = (intptr_t)(void *)argv; 137 (*stack_base)--; 138 **stack_base = imgp->argc; 139 return 0; 140 } 141 142 static int 143 elf_linux_fixup(long **stack_base, struct image_params *imgp) 144 { 145 Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs; 146 long *pos; 147 148 pos = *stack_base + (imgp->argc + imgp->envc + 2); 149 150 if (args->trace) { 151 AUXARGS_ENTRY(pos, AT_DEBUG, 1); 152 } 153 if (args->execfd != -1) { 154 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 155 } 156 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 157 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 158 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 159 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 160 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 161 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 162 AUXARGS_ENTRY(pos, AT_BASE, args->base); 163 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid); 164 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid); 165 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid); 166 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid); 167 AUXARGS_ENTRY(pos, AT_NULL, 0); 168 169 free(imgp->auxargs, M_TEMP); 170 imgp->auxargs = NULL; 171 172 (*stack_base)--; 173 **stack_base = (long)imgp->argc; 174 return 0; 175 } 176 177 extern int _ucodesel, _udatasel; 178 179 /* 180 * Send an interrupt to process. 181 * 182 * Stack is set up to allow sigcode stored 183 * in u. to call routine, followed by kcall 184 * to sigreturn routine below. After sigreturn 185 * resets the signal mask, the stack, and the 186 * frame pointer, it returns to the user 187 * specified pc, psl. 188 */ 189 190 static void 191 linux_sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code) 192 { 193 register struct proc *p = curproc; 194 register struct trapframe *regs; 195 struct linux_sigframe *fp, frame; 196 struct sigacts *psp = p->p_sigacts; 197 int oonstack; 198 199 regs = p->p_md.md_regs; 200 oonstack = p->p_sigstk.ss_flags & SS_ONSTACK; 201 202 #ifdef DEBUG 203 printf("Linux-emul(%ld): linux_sendsig(%p, %d, %p, %lu)\n", 204 (long)p->p_pid, catcher, sig, (void*)mask, code); 205 #endif 206 /* 207 * Allocate space for the signal handler context. 208 */ 209 if ((p->p_flag & P_ALTSTACK) && !oonstack && 210 SIGISMEMBER(psp->ps_sigonstack, sig)) { 211 fp = (struct linux_sigframe *)(p->p_sigstk.ss_sp + 212 p->p_sigstk.ss_size - sizeof(struct linux_sigframe)); 213 p->p_sigstk.ss_flags |= SS_ONSTACK; 214 } else { 215 fp = (struct linux_sigframe *)regs->tf_esp - 1; 216 } 217 218 /* 219 * grow() will return FALSE if the fp will not fit inside the stack 220 * and the stack can not be grown. useracc will return FALSE 221 * if access is denied. 222 */ 223 if ((grow_stack (p, (int)fp) == FALSE) || 224 !useracc((caddr_t)fp, sizeof (struct linux_sigframe), 225 VM_PROT_WRITE)) { 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