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