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