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