1 /* $NetBSD: linux_machdep.c,v 1.34 2007/12/08 18:36:06 dsl 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.34 2007/12/08 18:36:06 dsl 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/syscallargs.h> 61 #include <sys/filedesc.h> 62 #include <sys/exec_elf.h> 63 #include <sys/disklabel.h> 64 #include <sys/ioctl.h> 65 #include <sys/sysctl.h> 66 #include <sys/kauth.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 <sys/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(struct lwp *l, struct exec_package *pack, u_long stack) 106 { 107 setregs(l, pack, stack); 108 return; 109 } 110 111 /* 112 * Send an interrupt to process. 113 * 114 * Adapted from sys/arch/mips/mips/mips_machdep.c 115 * 116 * XXX Does not work well yet with RT signals 117 * 118 */ 119 120 void 121 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 122 { 123 const int sig = ksi->ksi_signo; 124 struct lwp *l = curlwp; 125 struct proc *p = l->l_proc; 126 struct linux_sigframe *fp; 127 struct frame *f; 128 int i, onstack, error; 129 sig_t catcher = SIGACTION(p, sig).sa_handler; 130 struct linux_sigframe sf; 131 132 #ifdef DEBUG_LINUX 133 printf("linux_sendsig()\n"); 134 #endif /* DEBUG_LINUX */ 135 f = (struct frame *)l->l_md.md_regs; 136 137 /* 138 * Do we need to jump onto the signal stack? 139 */ 140 onstack = 141 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 142 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 143 144 /* 145 * Signal stack is broken (see at the end of linux_sigreturn), so we do 146 * not use it yet. XXX fix this. 147 */ 148 onstack=0; 149 150 /* 151 * Allocate space for the signal handler context. 152 */ 153 if (onstack) 154 fp = (struct linux_sigframe *) 155 ((uint8_t *)l->l_sigstk.ss_sp 156 + l->l_sigstk.ss_size); 157 else 158 /* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */ 159 fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[_R_SP]; 160 161 /* 162 * Build stack frame for signal trampoline. 163 */ 164 memset(&sf, 0, sizeof sf); 165 166 /* 167 * This is the signal trampoline used by Linux, we don't use it, 168 * but we set it up in case an application expects it to be there 169 */ 170 sf.lsf_code[0] = 0x24020000; /* li v0, __NR_sigreturn */ 171 sf.lsf_code[1] = 0x0000000c; /* syscall */ 172 173 native_to_linux_sigset(&sf.lsf_mask, mask); 174 for (i=0; i<32; i++) { 175 sf.lsf_sc.lsc_regs[i] = f->f_regs[i]; 176 } 177 sf.lsf_sc.lsc_mdhi = f->f_regs[_R_MULHI]; 178 sf.lsf_sc.lsc_mdlo = f->f_regs[_R_MULLO]; 179 sf.lsf_sc.lsc_pc = f->f_regs[_R_PC]; 180 sf.lsf_sc.lsc_status = f->f_regs[_R_SR]; 181 sf.lsf_sc.lsc_cause = f->f_regs[_R_CAUSE]; 182 sf.lsf_sc.lsc_badvaddr = f->f_regs[_R_BADVADDR]; 183 sendsig_reset(l, sig); 184 185 /* 186 * Save signal stack. XXX broken 187 */ 188 /* kregs.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; */ 189 190 /* 191 * Install the sigframe onto the stack 192 */ 193 fp -= sizeof(struct linux_sigframe); 194 mutex_exit(&p->p_smutex); 195 error = copyout(&sf, fp, sizeof(sf)); 196 mutex_enter(&p->p_smutex); 197 198 if (error != 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 l->l_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(struct lwp *l, void *v, register_t *retval) 239 { 240 struct linux_sys_sigreturn_args /* { 241 syscallarg(struct linux_sigframe *) sf; 242 } */ *uap = v; 243 struct proc *p = l->l_proc; 244 struct linux_sigframe *sf, ksf; 245 struct frame *f; 246 sigset_t mask; 247 int i, error; 248 249 #ifdef DEBUG_LINUX 250 printf("linux_sys_sigreturn()\n"); 251 #endif /* DEBUG_LINUX */ 252 253 /* 254 * The trampoline code hands us the context. 255 * It is unsafe to keep track of it ourselves, in the event that a 256 * program jumps out of a signal handler. 257 */ 258 sf = SCARG(uap, sf); 259 260 if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0) 261 return (error); 262 263 /* Restore the register context. */ 264 f = (struct frame *)l->l_md.md_regs; 265 for (i=0; i<32; i++) 266 f->f_regs[i] = ksf.lsf_sc.lsc_regs[i]; 267 f->f_regs[_R_MULLO] = ksf.lsf_sc.lsc_mdlo; 268 f->f_regs[_R_MULHI] = ksf.lsf_sc.lsc_mdhi; 269 f->f_regs[_R_PC] = ksf.lsf_sc.lsc_pc; 270 f->f_regs[_R_BADVADDR] = ksf.lsf_sc.lsc_badvaddr; 271 f->f_regs[_R_CAUSE] = ksf.lsf_sc.lsc_cause; 272 273 mutex_enter(&p->p_smutex); 274 275 /* Restore signal stack. */ 276 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 277 278 /* Restore signal mask. */ 279 linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask); 280 (void)sigprocmask1(l, SIG_SETMASK, &mask, 0); 281 282 mutex_exit(&p->p_smutex); 283 284 return (EJUSTRETURN); 285 } 286 287 288 int 289 linux_sys_rt_sigreturn(struct lwp *l, void *v, register_t *retval) 290 { 291 return (ENOSYS); 292 } 293 294 295 #if 0 296 int 297 linux_sys_modify_ldt(struct lwp *l, void *v, register_t *retval) 298 { 299 /* 300 * This syscall is not implemented in Linux/Mips: we should not 301 * be here 302 */ 303 #ifdef DEBUG_LINUX 304 printf("linux_sys_modify_ldt: should not be here.\n"); 305 #endif /* DEBUG_LINUX */ 306 return 0; 307 } 308 #endif 309 310 /* 311 * major device numbers remapping 312 */ 313 dev_t 314 linux_fakedev(dev_t dev, int raw) 315 { 316 /* XXX write me */ 317 return dev; 318 } 319 320 /* 321 * We come here in a last attempt to satisfy a Linux ioctl() call 322 */ 323 int 324 linux_machdepioctl(struct lwp *l, void *v, register_t *retval) 325 { 326 return 0; 327 } 328 329 /* 330 * See above. If a root process tries to set access to an I/O port, 331 * just let it have the whole range. 332 */ 333 int 334 linux_sys_ioperm(struct lwp *l, void *v, register_t *retval) 335 { 336 /* 337 * This syscall is not implemented in Linux/Mips: we should not be here 338 */ 339 #ifdef DEBUG_LINUX 340 printf("linux_sys_ioperm: should not be here.\n"); 341 #endif /* DEBUG_LINUX */ 342 return 0; 343 } 344 345 /* 346 * wrapper linux_sys_new_uname() -> linux_sys_uname() 347 */ 348 int 349 linux_sys_new_uname(struct lwp *l, void *v, register_t *retval) 350 { 351 /* 352 * Use this if you want to try Linux emulation with a glibc-2.2 353 * or higher. Note that signals will not work 354 */ 355 #if 0 356 struct linux_sys_uname_args /* { 357 syscallarg(struct linux_utsname *) up; 358 } */ *uap = v; 359 struct linux_utsname luts; 360 361 strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname)); 362 strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 363 strlcpy(luts.l_release, "2.4.0", sizeof(luts.l_release)); 364 strlcpy(luts.l_version, linux_version, sizeof(luts.l_version)); 365 strlcpy(luts.l_machine, machine, sizeof(luts.l_machine)); 366 strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname)); 367 368 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 369 #else 370 return linux_sys_uname(l, v, retval); 371 #endif 372 } 373 374 /* 375 * In Linux, cacheflush is currently implemented 376 * as a whole cache flush (arguments are ignored) 377 * we emulate this broken beahior. 378 */ 379 int 380 linux_sys_cacheflush(struct lwp *l, void *v, register_t *retval) 381 { 382 mips_icache_sync_all(); 383 mips_dcache_wbinv_all(); 384 return 0; 385 } 386 387 /* 388 * This system call is depecated in Linux, but 389 * some binaries and some libraries use it. 390 */ 391 int 392 linux_sys_sysmips(struct lwp *l, void *v, register_t *retval) 393 { 394 struct linux_sys_sysmips_args { 395 syscallarg(int) cmd; 396 syscallarg(int) arg1; 397 syscallarg(int) arg2; 398 syscallarg(int) arg3; 399 } *uap = v; 400 int error; 401 402 switch (SCARG(uap, cmd)) { 403 case LINUX_SETNAME: { 404 char nodename [LINUX___NEW_UTS_LEN + 1]; 405 int name[2]; 406 size_t len; 407 408 if ((error = kauth_authorize_generic(l->l_cred, 409 KAUTH_GENERIC_ISSUSER, NULL)) != 0) 410 return error; 411 if ((error = copyinstr((char *)SCARG(uap, arg1), nodename, 412 LINUX___NEW_UTS_LEN, &len)) != 0) 413 return error; 414 415 name[0] = CTL_KERN; 416 name[1] = KERN_HOSTNAME; 417 return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL)); 418 419 break; 420 } 421 case LINUX_MIPS_ATOMIC_SET: { 422 void *addr; 423 int s; 424 u_int8_t value = 0; 425 426 addr = (void *)SCARG(uap, arg1); 427 428 s = splhigh(); 429 /* 430 * No error testing here. This is bad, but Linux does 431 * it like this. The source aknowledge "This is broken" 432 * in a comment... 433 */ 434 (void) copyin(addr, &value, 1); 435 *retval = value; 436 value = (u_int8_t) SCARG(uap, arg2); 437 error = copyout(&value, addr, 1); 438 splx(s); 439 440 return 0; 441 break; 442 } 443 case LINUX_MIPS_FIXADE: /* XXX not implemented */ 444 break; 445 case LINUX_FLUSH_CACHE: 446 mips_icache_sync_all(); 447 mips_dcache_wbinv_all(); 448 break; 449 case LINUX_MIPS_RDNVRAM: 450 return EIO; 451 break; 452 default: 453 return EINVAL; 454 break; 455 } 456 #ifdef DEBUG_LINUX 457 printf("linux_sys_sysmips(): unimplemented command %d\n", 458 SCARG(uap,cmd)); 459 #endif /* DEBUG_LINUX */ 460 return 0; 461 } 462 463 int 464 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg) 465 { 466 return 0; 467 } 468