1 /* $NetBSD: linux_machdep.c,v 1.24 2005/12/11 12:20:16 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Emmanuel Dreyfus. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.24 2005/12/11 12:20:16 christos Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/signalvar.h> 45 #include <sys/kernel.h> 46 #include <sys/proc.h> 47 #include <sys/user.h> 48 #include <sys/buf.h> 49 #include <sys/reboot.h> 50 #include <sys/conf.h> 51 #include <sys/exec.h> 52 #include <sys/file.h> 53 #include <sys/callout.h> 54 #include <sys/malloc.h> 55 #include <sys/mbuf.h> 56 #include <sys/msgbuf.h> 57 #include <sys/mount.h> 58 #include <sys/vnode.h> 59 #include <sys/device.h> 60 #include <sys/sa.h> 61 #include <sys/syscallargs.h> 62 #include <sys/filedesc.h> 63 #include <sys/exec_elf.h> 64 #include <sys/disklabel.h> 65 #include <sys/ioctl.h> 66 #include <sys/sysctl.h> 67 #include <miscfs/specfs/specdev.h> 68 69 #include <compat/linux/common/linux_types.h> 70 #include <compat/linux/common/linux_signal.h> 71 #include <compat/linux/common/linux_util.h> 72 #include <compat/linux/common/linux_ioctl.h> 73 #include <compat/linux/common/linux_hdio.h> 74 #include <compat/linux/common/linux_exec.h> 75 #include <compat/linux/common/linux_machdep.h> 76 77 #include <compat/linux/linux_syscallargs.h> 78 79 #include <machine/cpu.h> 80 #include <machine/psl.h> 81 #include <machine/reg.h> 82 #include <machine/regnum.h> 83 #include <machine/vmparam.h> 84 #include <machine/locore.h> 85 86 #include <mips/cache.h> 87 88 /* 89 * To see whether wscons is configured (for virtual console ioctl calls). 90 */ 91 #if defined(_KERNEL_OPT) 92 #include "wsdisplay.h" 93 #endif 94 #if (NWSDISPLAY > 0) 95 #include <dev/wscons/wsconsio.h> 96 #include <dev/wscons/wsdisplay_usl_io.h> 97 #endif 98 99 /* 100 * Set set up registers on exec. 101 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT 102 * entry uses NetBSD's native setregs instead of linux_setregs 103 */ 104 void 105 linux_setregs(l, pack, stack) 106 struct lwp *l; 107 struct exec_package *pack; 108 u_long stack; 109 { 110 setregs(l, pack, stack); 111 return; 112 } 113 114 /* 115 * Send an interrupt to process. 116 * 117 * Adapted from sys/arch/mips/mips/mips_machdep.c 118 * 119 * XXX Does not work well yet with RT signals 120 * 121 */ 122 123 void 124 linux_sendsig(ksi, mask) 125 const ksiginfo_t *ksi; 126 const sigset_t *mask; 127 { 128 const int sig = ksi->ksi_signo; 129 struct lwp *l = curlwp; 130 struct proc *p = l->l_proc; 131 struct linux_sigframe *fp; 132 struct frame *f; 133 int i,onstack; 134 sig_t catcher = SIGACTION(p, sig).sa_handler; 135 struct linux_sigframe sf; 136 137 #ifdef DEBUG_LINUX 138 printf("linux_sendsig()\n"); 139 #endif /* DEBUG_LINUX */ 140 f = (struct frame *)l->l_md.md_regs; 141 142 /* 143 * Do we need to jump onto the signal stack? 144 */ 145 onstack = 146 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 147 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 148 149 /* 150 * Signal stack is broken (see at the end of linux_sigreturn), so we do 151 * not use it yet. XXX fix this. 152 */ 153 onstack=0; 154 155 /* 156 * Allocate space for the signal handler context. 157 */ 158 if (onstack) 159 fp = (struct linux_sigframe *) 160 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp 161 + p->p_sigctx.ps_sigstk.ss_size); 162 else 163 /* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */ 164 fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[_R_SP]; 165 166 /* 167 * Build stack frame for signal trampoline. 168 */ 169 memset(&sf, 0, sizeof sf); 170 171 /* 172 * This is the signal trampoline used by Linux, we don't use it, 173 * but we set it up in case an application expects it to be there 174 */ 175 sf.lsf_code[0] = 0x24020000; /* li v0, __NR_sigreturn */ 176 sf.lsf_code[1] = 0x0000000c; /* syscall */ 177 178 native_to_linux_sigset(&sf.lsf_mask, mask); 179 for (i=0; i<32; i++) { 180 sf.lsf_sc.lsc_regs[i] = f->f_regs[i]; 181 } 182 sf.lsf_sc.lsc_mdhi = f->f_regs[_R_MULHI]; 183 sf.lsf_sc.lsc_mdlo = f->f_regs[_R_MULLO]; 184 sf.lsf_sc.lsc_pc = f->f_regs[_R_PC]; 185 sf.lsf_sc.lsc_status = f->f_regs[_R_SR]; 186 sf.lsf_sc.lsc_cause = f->f_regs[_R_CAUSE]; 187 sf.lsf_sc.lsc_badvaddr = f->f_regs[_R_BADVADDR]; 188 189 /* 190 * Save signal stack. XXX broken 191 */ 192 /* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */ 193 194 /* 195 * Install the sigframe onto the stack 196 */ 197 fp -= sizeof(struct linux_sigframe); 198 if (copyout(&sf, fp, sizeof(sf)) != 0) { 199 /* 200 * Process has trashed its stack; give it an illegal 201 * instruction to halt it in its tracks. 202 */ 203 #ifdef DEBUG_LINUX 204 printf("linux_sendsig: stack trashed\n"); 205 #endif /* DEBUG_LINUX */ 206 sigexit(l, SIGILL); 207 /* NOTREACHED */ 208 } 209 210 /* Set up the registers to return to sigcode. */ 211 f->f_regs[_R_A0] = native_to_linux_signo[sig]; 212 f->f_regs[_R_A1] = 0; 213 f->f_regs[_R_A2] = (unsigned long)&fp->lsf_sc; 214 215 #ifdef DEBUG_LINUX 216 printf("sigcontext is at %p\n", &fp->lsf_sc); 217 #endif /* DEBUG_LINUX */ 218 219 f->f_regs[_R_SP] = (unsigned long)fp; 220 /* Signal trampoline code is at base of user stack. */ 221 f->f_regs[_R_RA] = (unsigned long)p->p_sigctx.ps_sigcode; 222 f->f_regs[_R_T9] = (unsigned long)catcher; 223 f->f_regs[_R_PC] = (unsigned long)catcher; 224 225 /* Remember that we're now on the signal stack. */ 226 if (onstack) 227 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 228 229 return; 230 } 231 232 /* 233 * System call to cleanup state after a signal 234 * has been taken. Reset signal mask and 235 * stack state from context left by sendsig (above). 236 */ 237 int 238 linux_sys_sigreturn(l, v, retval) 239 struct lwp *l; 240 void *v; 241 register_t *retval; 242 { 243 struct linux_sys_sigreturn_args /* { 244 syscallarg(struct linux_sigframe *) sf; 245 } */ *uap = v; 246 struct proc *p = l->l_proc; 247 struct linux_sigframe *sf, ksf; 248 struct frame *f; 249 sigset_t mask; 250 int i, error; 251 252 #ifdef DEBUG_LINUX 253 printf("linux_sys_sigreturn()\n"); 254 #endif /* DEBUG_LINUX */ 255 256 /* 257 * The trampoline code hands us the context. 258 * It is unsafe to keep track of it ourselves, in the event that a 259 * program jumps out of a signal handler. 260 */ 261 sf = SCARG(uap, sf); 262 263 if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0) 264 return (error); 265 266 /* Restore the register context. */ 267 f = (struct frame *)l->l_md.md_regs; 268 for (i=0; i<32; i++) 269 f->f_regs[i] = ksf.lsf_sc.lsc_regs[i]; 270 f->f_regs[_R_MULLO] = ksf.lsf_sc.lsc_mdlo; 271 f->f_regs[_R_MULHI] = ksf.lsf_sc.lsc_mdhi; 272 f->f_regs[_R_PC] = ksf.lsf_sc.lsc_pc; 273 f->f_regs[_R_BADVADDR] = ksf.lsf_sc.lsc_badvaddr; 274 f->f_regs[_R_CAUSE] = ksf.lsf_sc.lsc_cause; 275 276 /* Restore signal stack. */ 277 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK; 278 279 /* Restore signal mask. */ 280 linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask); 281 (void)sigprocmask1(p, SIG_SETMASK, &mask, 0); 282 283 return (EJUSTRETURN); 284 } 285 286 287 int 288 linux_sys_rt_sigreturn(l, v, retval) 289 struct lwp *l; 290 void *v; 291 register_t *retval; 292 { 293 return (ENOSYS); 294 } 295 296 297 #if 0 298 int 299 linux_sys_modify_ldt(l, v, retval) 300 struct lwp *l; 301 void *v; 302 register_t *retval; 303 { 304 /* 305 * This syscall is not implemented in Linux/Mips: we should not 306 * be here 307 */ 308 #ifdef DEBUG_LINUX 309 printf("linux_sys_modify_ldt: should not be here.\n"); 310 #endif /* DEBUG_LINUX */ 311 return 0; 312 } 313 #endif 314 315 /* 316 * major device numbers remapping 317 */ 318 dev_t 319 linux_fakedev(dev, raw) 320 dev_t dev; 321 int raw; 322 { 323 /* XXX write me */ 324 return dev; 325 } 326 327 /* 328 * We come here in a last attempt to satisfy a Linux ioctl() call 329 */ 330 int 331 linux_machdepioctl(p, v, retval) 332 struct proc *p; 333 void *v; 334 register_t *retval; 335 { 336 return 0; 337 } 338 339 /* 340 * See above. If a root process tries to set access to an I/O port, 341 * just let it have the whole range. 342 */ 343 int 344 linux_sys_ioperm(l, v, retval) 345 struct lwp *l; 346 void *v; 347 register_t *retval; 348 { 349 /* 350 * This syscall is not implemented in Linux/Mips: we should not be here 351 */ 352 #ifdef DEBUG_LINUX 353 printf("linux_sys_ioperm: should not be here.\n"); 354 #endif /* DEBUG_LINUX */ 355 return 0; 356 } 357 358 /* 359 * wrapper linux_sys_new_uname() -> linux_sys_uname() 360 */ 361 int 362 linux_sys_new_uname(l, v, retval) 363 struct lwp *l; 364 void *v; 365 register_t *retval; 366 { 367 /* 368 * Use this if you want to try Linux emulation with a glibc-2.2 369 * or higher. Note that signals will not work 370 */ 371 #if 0 372 struct linux_sys_uname_args /* { 373 syscallarg(struct linux_utsname *) up; 374 } */ *uap = v; 375 struct linux_utsname luts; 376 377 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname)); 378 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 379 strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release)); 380 strncpy(luts.l_version, linux_version, sizeof(luts.l_version)); 381 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 382 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname)); 383 384 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 385 #else 386 return linux_sys_uname(l, v, retval); 387 #endif 388 } 389 390 /* 391 * In Linux, cacheflush is currently implemented 392 * as a whole cache flush (arguments are ignored) 393 * we emulate this broken beahior. 394 */ 395 int 396 linux_sys_cacheflush(l, v, retval) 397 struct lwp *l; 398 void *v; 399 register_t *retval; 400 { 401 mips_icache_sync_all(); 402 mips_dcache_wbinv_all(); 403 return 0; 404 } 405 406 /* 407 * This system call is depecated in Linux, but 408 * some binaries and some libraries use it. 409 */ 410 int 411 linux_sys_sysmips(l, v, retval) 412 struct lwp *l; 413 void *v; 414 register_t *retval; 415 { 416 struct linux_sys_sysmips_args { 417 syscallarg(int) cmd; 418 syscallarg(int) arg1; 419 syscallarg(int) arg2; 420 syscallarg(int) arg3; 421 } *uap = v; 422 struct proc *p = l->l_proc; 423 int error; 424 425 switch (SCARG(uap, cmd)) { 426 case LINUX_SETNAME: { 427 char nodename [LINUX___NEW_UTS_LEN + 1]; 428 int name[2]; 429 size_t len; 430 431 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 432 return error; 433 if ((error = copyinstr((char *)SCARG(uap, arg1), nodename, 434 LINUX___NEW_UTS_LEN, &len)) != 0) 435 return error; 436 437 name[0] = CTL_KERN; 438 name[1] = KERN_HOSTNAME; 439 return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL)); 440 441 break; 442 } 443 case LINUX_MIPS_ATOMIC_SET: { 444 void *addr; 445 int s; 446 u_int8_t value = 0; 447 448 addr = (void *)SCARG(uap, arg1); 449 450 s = splhigh(); 451 /* 452 * No error testing here. This is bad, but Linux does 453 * it like this. The source aknowledge "This is broken" 454 * in a comment... 455 */ 456 (void) copyin(addr, &value, 1); 457 *retval = value; 458 value = (u_int8_t) SCARG(uap, arg2); 459 error = copyout(&value, addr, 1); 460 splx(s); 461 462 return 0; 463 break; 464 } 465 case LINUX_MIPS_FIXADE: /* XXX not implemented */ 466 break; 467 case LINUX_FLUSH_CACHE: 468 mips_icache_sync_all(); 469 mips_dcache_wbinv_all(); 470 break; 471 case LINUX_MIPS_RDNVRAM: 472 return EIO; 473 break; 474 default: 475 return EINVAL; 476 break; 477 } 478 #ifdef DEBUG_LINUX 479 printf("linux_sys_sysmips(): unimplemented command %d\n", 480 SCARG(uap,cmd)); 481 #endif /* DEBUG_LINUX */ 482 return 0; 483 } 484 485 int 486 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg) 487 { 488 return 0; 489 } 490