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