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