1 /* $NetBSD: linux_machdep.c,v 1.34 1997/10/16 04:23:38 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Frank van der Linden 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project 18 * by Frank van der Linden 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/signalvar.h> 37 #include <sys/kernel.h> 38 #include <sys/map.h> 39 #include <sys/proc.h> 40 #include <sys/user.h> 41 #include <sys/buf.h> 42 #include <sys/reboot.h> 43 #include <sys/conf.h> 44 #include <sys/exec.h> 45 #include <sys/file.h> 46 #include <sys/callout.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/msgbuf.h> 50 #include <sys/mount.h> 51 #include <sys/vnode.h> 52 #include <sys/device.h> 53 #include <sys/syscallargs.h> 54 #include <sys/filedesc.h> 55 56 #include <compat/linux/linux_types.h> 57 #include <compat/linux/linux_signal.h> 58 #include <compat/linux/linux_syscallargs.h> 59 #include <compat/linux/linux_util.h> 60 #include <compat/linux/linux_ioctl.h> 61 62 #include <machine/cpu.h> 63 #include <machine/cpufunc.h> 64 #include <machine/psl.h> 65 #include <machine/reg.h> 66 #include <machine/segments.h> 67 #include <machine/specialreg.h> 68 #include <machine/sysarch.h> 69 #include <machine/vm86.h> 70 #include <machine/vmparam.h> 71 #include <machine/linux_machdep.h> 72 73 /* 74 * To see whether pcvt is configured (for virtual console ioctl calls). 75 */ 76 #ifndef NVT 77 #include "vt.h" 78 #endif 79 #if NVT > 0 80 #include <arch/i386/isa/pcvt/pcvt_ioctl.h> 81 #endif 82 83 #ifdef USER_LDT 84 #include <machine/cpu.h> 85 int linux_read_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *, 86 register_t *)); 87 int linux_write_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *, 88 register_t *)); 89 #endif 90 91 /* 92 * Deal with some i386-specific things in the Linux emulation code. 93 * This means just signals for now, will include stuff like 94 * I/O map permissions and V86 mode sometime. 95 */ 96 97 /* 98 * Send an interrupt to process. 99 * 100 * Stack is set up to allow sigcode stored 101 * in u. to call routine, followed by kcall 102 * to sigreturn routine below. After sigreturn 103 * resets the signal mask, the stack, and the 104 * frame pointer, it returns to the user 105 * specified pc, psl. 106 */ 107 108 void 109 linux_sendsig(catcher, sig, mask, code) 110 sig_t catcher; 111 int sig, mask; 112 u_long code; 113 { 114 register struct proc *p = curproc; 115 register struct trapframe *tf; 116 struct linux_sigframe *fp, frame; 117 struct sigacts *psp = p->p_sigacts; 118 int oonstack; 119 extern char linux_sigcode[], linux_esigcode[]; 120 121 tf = p->p_md.md_regs; 122 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 123 124 /* 125 * Allocate space for the signal handler context. 126 */ 127 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack && 128 (psp->ps_sigonstack & sigmask(sig))) { 129 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp + 130 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe)); 131 psp->ps_sigstk.ss_flags |= SS_ONSTACK; 132 } else { 133 fp = (struct linux_sigframe *)tf->tf_esp - 1; 134 } 135 136 frame.sf_handler = catcher; 137 frame.sf_sig = bsd_to_linux_sig[sig]; 138 139 /* 140 * Build the signal context to be used by sigreturn. 141 */ 142 frame.sf_sc.sc_mask = mask; 143 #ifdef VM86 144 if (tf->tf_eflags & PSL_VM) { 145 frame.sf_sc.sc_gs = tf->tf_vm86_gs; 146 frame.sf_sc.sc_fs = tf->tf_vm86_fs; 147 frame.sf_sc.sc_es = tf->tf_vm86_es; 148 frame.sf_sc.sc_ds = tf->tf_vm86_ds; 149 frame.sf_sc.sc_eflags = get_vflags(p); 150 } else 151 #endif 152 { 153 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs)); 154 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs)); 155 frame.sf_sc.sc_es = tf->tf_es; 156 frame.sf_sc.sc_ds = tf->tf_ds; 157 frame.sf_sc.sc_eflags = tf->tf_eflags; 158 } 159 frame.sf_sc.sc_edi = tf->tf_edi; 160 frame.sf_sc.sc_esi = tf->tf_esi; 161 frame.sf_sc.sc_ebp = tf->tf_ebp; 162 frame.sf_sc.sc_ebx = tf->tf_ebx; 163 frame.sf_sc.sc_edx = tf->tf_edx; 164 frame.sf_sc.sc_ecx = tf->tf_ecx; 165 frame.sf_sc.sc_eax = tf->tf_eax; 166 frame.sf_sc.sc_eip = tf->tf_eip; 167 frame.sf_sc.sc_cs = tf->tf_cs; 168 frame.sf_sc.sc_esp_at_signal = tf->tf_esp; 169 frame.sf_sc.sc_ss = tf->tf_ss; 170 frame.sf_sc.sc_err = tf->tf_err; 171 frame.sf_sc.sc_trapno = tf->tf_trapno; 172 173 if (copyout(&frame, fp, sizeof(frame)) != 0) { 174 /* 175 * Process has trashed its stack; give it an illegal 176 * instruction to halt it in its tracks. 177 */ 178 sigexit(p, SIGILL); 179 /* NOTREACHED */ 180 } 181 182 /* 183 * Build context to run handler in. 184 */ 185 tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL); 186 tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL); 187 tf->tf_eip = (int)(((char *)PS_STRINGS) - 188 (linux_esigcode - linux_sigcode)); 189 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL); 190 tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC); 191 tf->tf_esp = (int)fp; 192 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL); 193 } 194 195 /* 196 * System call to cleanup state after a signal 197 * has been taken. Reset signal mask and 198 * stack state from context left by sendsig (above). 199 * Return to previous pc and psl as specified by 200 * context left by sendsig. Check carefully to 201 * make sure that the user has not modified the 202 * psl to gain improper privileges or to cause 203 * a machine fault. 204 */ 205 int 206 linux_sys_sigreturn(p, v, retval) 207 struct proc *p; 208 void *v; 209 register_t *retval; 210 { 211 struct linux_sys_sigreturn_args /* { 212 syscallarg(struct linux_sigcontext *) scp; 213 } */ *uap = v; 214 struct linux_sigcontext *scp, context; 215 register struct trapframe *tf; 216 217 tf = p->p_md.md_regs; 218 219 /* 220 * The trampoline code hands us the context. 221 * It is unsafe to keep track of it ourselves, in the event that a 222 * program jumps out of a signal handler. 223 */ 224 scp = SCARG(uap, scp); 225 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0) 226 return (EFAULT); 227 228 /* 229 * Restore signal context. 230 */ 231 #ifdef VM86 232 if (context.sc_eflags & PSL_VM) { 233 tf->tf_vm86_gs = context.sc_gs; 234 tf->tf_vm86_fs = context.sc_fs; 235 tf->tf_vm86_es = context.sc_es; 236 tf->tf_vm86_ds = context.sc_ds; 237 set_vflags(p, context.sc_eflags); 238 } else 239 #endif 240 { 241 /* 242 * Check for security violations. If we're returning to 243 * protected mode, the CPU will validate the segment registers 244 * automatically and generate a trap on violations. We handle 245 * the trap, rather than doing all of the checking here. 246 */ 247 if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 || 248 !USERMODE(context.sc_cs, context.sc_eflags)) 249 return (EINVAL); 250 251 /* %fs and %gs were restored by the trampoline. */ 252 tf->tf_es = context.sc_es; 253 tf->tf_ds = context.sc_ds; 254 tf->tf_eflags = context.sc_eflags; 255 } 256 tf->tf_edi = context.sc_edi; 257 tf->tf_esi = context.sc_esi; 258 tf->tf_ebp = context.sc_ebp; 259 tf->tf_ebx = context.sc_ebx; 260 tf->tf_edx = context.sc_edx; 261 tf->tf_ecx = context.sc_ecx; 262 tf->tf_eax = context.sc_eax; 263 tf->tf_eip = context.sc_eip; 264 tf->tf_cs = context.sc_cs; 265 tf->tf_esp = context.sc_esp_at_signal; 266 tf->tf_ss = context.sc_ss; 267 268 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK; 269 p->p_sigmask = context.sc_mask & ~sigcantmask; 270 271 return (EJUSTRETURN); 272 } 273 274 #ifdef USER_LDT 275 276 int 277 linux_read_ldt(p, uap, retval) 278 struct proc *p; 279 struct linux_sys_modify_ldt_args /* { 280 syscallarg(int) func; 281 syscallarg(void *) ptr; 282 syscallarg(size_t) bytecount; 283 } */ *uap; 284 register_t *retval; 285 { 286 struct i386_get_ldt_args gl; 287 int error; 288 caddr_t sg; 289 char *parms; 290 291 sg = stackgap_init(p->p_emul); 292 293 gl.start = 0; 294 gl.desc = SCARG(uap, ptr); 295 gl.num = SCARG(uap, bytecount) / sizeof(union descriptor); 296 297 parms = stackgap_alloc(&sg, sizeof(gl)); 298 299 if ((error = copyout(&gl, parms, sizeof(gl))) != 0) 300 return (error); 301 302 if ((error = i386_get_ldt(p, parms, retval)) != 0) 303 return (error); 304 305 *retval *= sizeof(union descriptor); 306 return (0); 307 } 308 309 struct linux_ldt_info { 310 u_int entry_number; 311 u_long base_addr; 312 u_int limit; 313 u_int seg_32bit:1; 314 u_int contents:2; 315 u_int read_exec_only:1; 316 u_int limit_in_pages:1; 317 u_int seg_not_present:1; 318 }; 319 320 int 321 linux_write_ldt(p, uap, retval) 322 struct proc *p; 323 struct linux_sys_modify_ldt_args /* { 324 syscallarg(int) func; 325 syscallarg(void *) ptr; 326 syscallarg(size_t) bytecount; 327 } */ *uap; 328 register_t *retval; 329 { 330 struct linux_ldt_info ldt_info; 331 struct segment_descriptor sd; 332 struct i386_set_ldt_args sl; 333 int error; 334 caddr_t sg; 335 char *parms; 336 337 if (SCARG(uap, bytecount) != sizeof(ldt_info)) 338 return (EINVAL); 339 if ((error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info))) != 0) 340 return error; 341 if (ldt_info.contents == 3) 342 return (EINVAL); 343 344 sg = stackgap_init(p->p_emul); 345 346 sd.sd_lobase = ldt_info.base_addr & 0xffffff; 347 sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff; 348 sd.sd_lolimit = ldt_info.limit & 0xffff; 349 sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf; 350 sd.sd_type = 351 16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1); 352 sd.sd_dpl = SEL_UPL; 353 sd.sd_p = !ldt_info.seg_not_present; 354 sd.sd_def32 = ldt_info.seg_32bit; 355 sd.sd_gran = ldt_info.limit_in_pages; 356 357 sl.start = ldt_info.entry_number; 358 sl.desc = stackgap_alloc(&sg, sizeof(sd)); 359 sl.num = 1; 360 361 #if 0 362 printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n", 363 ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit); 364 #endif 365 366 parms = stackgap_alloc(&sg, sizeof(sl)); 367 368 if ((error = copyout(&sd, sl.desc, sizeof(sd))) != 0) 369 return (error); 370 if ((error = copyout(&sl, parms, sizeof(sl))) != 0) 371 return (error); 372 373 if ((error = i386_set_ldt(p, parms, retval)) != 0) 374 return (error); 375 376 *retval = 0; 377 return (0); 378 } 379 380 #endif /* USER_LDT */ 381 382 int 383 linux_sys_modify_ldt(p, v, retval) 384 struct proc *p; 385 void *v; 386 register_t *retval; 387 { 388 struct linux_sys_modify_ldt_args /* { 389 syscallarg(int) func; 390 syscallarg(void *) ptr; 391 syscallarg(size_t) bytecount; 392 } */ *uap = v; 393 394 switch (SCARG(uap, func)) { 395 #ifdef USER_LDT 396 case 0: 397 return (linux_read_ldt(p, uap, retval)); 398 399 case 1: 400 return (linux_write_ldt(p, uap, retval)); 401 #endif /* USER_LDT */ 402 403 default: 404 return (ENOSYS); 405 } 406 } 407 408 /* 409 * XXX Pathetic hack to make svgalib work. This will fake the major 410 * device number of an opened VT so that svgalib likes it. grmbl. 411 * Should probably do it 'wrong the right way' and use a mapping 412 * array for all major device numbers, and map linux_mknod too. 413 */ 414 dev_t 415 linux_fakedev(dev) 416 dev_t dev; 417 { 418 419 if (major(dev) == NETBSD_CONS_MAJOR) 420 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1)); 421 return dev; 422 } 423 424 /* 425 * We come here in a last attempt to satisfy a Linux ioctl() call 426 */ 427 int 428 linux_machdepioctl(p, v, retval) 429 struct proc *p; 430 void *v; 431 register_t *retval; 432 { 433 struct linux_sys_ioctl_args /* { 434 syscallarg(int) fd; 435 syscallarg(u_long) com; 436 syscallarg(caddr_t) data; 437 } */ *uap = v; 438 struct sys_ioctl_args bia; 439 u_long com; 440 #if NVT > 0 441 int error; 442 struct vt_mode lvt; 443 caddr_t bvtp, sg; 444 #endif 445 446 SCARG(&bia, fd) = SCARG(uap, fd); 447 SCARG(&bia, data) = SCARG(uap, data); 448 com = SCARG(uap, com); 449 450 switch (com) { 451 #if NVT > 0 452 case LINUX_KDGKBMODE: 453 com = KDGKBMODE; 454 break; 455 case LINUX_KDSKBMODE: 456 com = KDSKBMODE; 457 if ((unsigned)SCARG(uap, data) == LINUX_K_MEDIUMRAW) 458 SCARG(&bia, data) = (caddr_t)K_RAW; 459 break; 460 case LINUX_KDMKTONE: 461 com = KDMKTONE; 462 break; 463 case LINUX_KDSETMODE: 464 com = KDSETMODE; 465 break; 466 case LINUX_KDENABIO: 467 com = KDENABIO; 468 break; 469 case LINUX_KDDISABIO: 470 com = KDDISABIO; 471 break; 472 case LINUX_KDGETLED: 473 com = KDGETLED; 474 break; 475 case LINUX_KDSETLED: 476 com = KDSETLED; 477 break; 478 case LINUX_VT_OPENQRY: 479 com = VT_OPENQRY; 480 break; 481 case LINUX_VT_GETMODE: 482 SCARG(&bia, com) = VT_GETMODE; 483 if ((error = sys_ioctl(p, &bia, retval))) 484 return error; 485 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt, 486 sizeof (struct vt_mode)))) 487 return error; 488 lvt.relsig = bsd_to_linux_sig[lvt.relsig]; 489 lvt.acqsig = bsd_to_linux_sig[lvt.acqsig]; 490 lvt.frsig = bsd_to_linux_sig[lvt.frsig]; 491 return copyout((caddr_t)&lvt, SCARG(uap, data), 492 sizeof (struct vt_mode)); 493 case LINUX_VT_SETMODE: 494 com = VT_SETMODE; 495 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt, 496 sizeof (struct vt_mode)))) 497 return error; 498 lvt.relsig = linux_to_bsd_sig[lvt.relsig]; 499 lvt.acqsig = linux_to_bsd_sig[lvt.acqsig]; 500 lvt.frsig = linux_to_bsd_sig[lvt.frsig]; 501 sg = stackgap_init(p->p_emul); 502 bvtp = stackgap_alloc(&sg, sizeof (struct vt_mode)); 503 if ((error = copyout(&lvt, bvtp, sizeof (struct vt_mode)))) 504 return error; 505 SCARG(&bia, data) = bvtp; 506 break; 507 case LINUX_VT_RELDISP: 508 com = VT_RELDISP; 509 break; 510 case LINUX_VT_ACTIVATE: 511 com = VT_ACTIVATE; 512 break; 513 case LINUX_VT_WAITACTIVE: 514 com = VT_WAITACTIVE; 515 break; 516 #endif 517 default: 518 printf("linux_machdepioctl: invalid ioctl %08lx\n", com); 519 return EINVAL; 520 } 521 SCARG(&bia, com) = com; 522 return sys_ioctl(p, &bia, retval); 523 } 524 525 /* 526 * Set I/O permissions for a process. Just set the maximum level 527 * right away (ignoring the argument), otherwise we would have 528 * to rely on I/O permission maps, which are not implemented. 529 */ 530 int 531 linux_sys_iopl(p, v, retval) 532 struct proc *p; 533 void *v; 534 register_t *retval; 535 { 536 #if 0 537 struct linux_sys_iopl_args /* { 538 syscallarg(int) level; 539 } */ *uap = v; 540 #endif 541 struct trapframe *fp = p->p_md.md_regs; 542 543 if (suser(p->p_ucred, &p->p_acflag) != 0) 544 return EPERM; 545 fp->tf_eflags |= PSL_IOPL; 546 *retval = 0; 547 return 0; 548 } 549 550 /* 551 * See above. If a root process tries to set access to an I/O port, 552 * just let it have the whole range. 553 */ 554 int 555 linux_sys_ioperm(p, v, retval) 556 struct proc *p; 557 void *v; 558 register_t *retval; 559 { 560 struct linux_sys_ioperm_args /* { 561 syscallarg(unsigned int) lo; 562 syscallarg(unsigned int) hi; 563 syscallarg(int) val; 564 } */ *uap = v; 565 struct trapframe *fp = p->p_md.md_regs; 566 567 if (suser(p->p_ucred, &p->p_acflag) != 0) 568 return EPERM; 569 if (SCARG(uap, val)) 570 fp->tf_eflags |= PSL_IOPL; 571 *retval = 0; 572 return 0; 573 } 574