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