xref: /netbsd-src/sys/compat/linux/arch/mips/linux_machdep.c (revision 1ca06f9c9235889e2ff6dc77279d01d151d70a9a)
1 /*	$NetBSD: linux_machdep.c,v 1.39 2009/11/23 00:46:07 rmind 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.39 2009/11/23 00:46:07 rmind 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/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/syscallargs.h>
53 #include <sys/filedesc.h>
54 #include <sys/exec_elf.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <sys/sysctl.h>
58 #include <sys/kauth.h>
59 #include <miscfs/specfs/specdev.h>
60 
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_util.h>
64 #include <compat/linux/common/linux_ioctl.h>
65 #include <compat/linux/common/linux_hdio.h>
66 #include <compat/linux/common/linux_exec.h>
67 #include <compat/linux/common/linux_machdep.h>
68 
69 #include <compat/linux/linux_syscallargs.h>
70 
71 #include <sys/cpu.h>
72 #include <machine/psl.h>
73 #include <machine/reg.h>
74 #include <machine/regnum.h>
75 #include <machine/vmparam.h>
76 #include <machine/locore.h>
77 
78 #include <mips/cache.h>
79 
80 /*
81  * To see whether wscons is configured (for virtual console ioctl calls).
82  */
83 #if defined(_KERNEL_OPT)
84 #include "wsdisplay.h"
85 #endif
86 #if (NWSDISPLAY > 0)
87 #include <dev/wscons/wsconsio.h>
88 #include <dev/wscons/wsdisplay_usl_io.h>
89 #endif
90 
91 /*
92  * Set set up registers on exec.
93  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
94  * entry uses NetBSD's native setregs instead of linux_setregs
95  */
96 void
97 linux_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
98 {
99 	setregs(l, pack, stack);
100 	return;
101 }
102 
103 /*
104  * Send an interrupt to process.
105  *
106  * Adapted from sys/arch/mips/mips/mips_machdep.c
107  *
108  * XXX Does not work well yet with RT signals
109  *
110  */
111 
112 void
113 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
114 {
115 	const int sig = ksi->ksi_signo;
116 	struct lwp *l = curlwp;
117 	struct proc *p = l->l_proc;
118 	struct linux_sigframe *fp;
119 	struct frame *f;
120 	int i, onstack, error;
121 	sig_t catcher = SIGACTION(p, sig).sa_handler;
122 	struct linux_sigframe sf;
123 
124 #ifdef DEBUG_LINUX
125 	printf("linux_sendsig()\n");
126 #endif /* DEBUG_LINUX */
127 	f = (struct frame *)l->l_md.md_regs;
128 
129 	/*
130 	 * Do we need to jump onto the signal stack?
131 	 */
132 	onstack =
133 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
134 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
135 
136 	/*
137 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
138 	 * not use it yet. XXX fix this.
139 	 */
140 	onstack=0;
141 
142 	/*
143 	 * Allocate space for the signal handler context.
144 	 */
145 	if (onstack)
146 		fp = (struct linux_sigframe *)
147 		    ((uint8_t *)l->l_sigstk.ss_sp
148 		    + l->l_sigstk.ss_size);
149 	else
150 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
151 		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[_R_SP];
152 
153 	/*
154 	 * Build stack frame for signal trampoline.
155 	 */
156 	memset(&sf, 0, sizeof sf);
157 
158 	/*
159 	 * This is the signal trampoline used by Linux, we don't use it,
160 	 * but we set it up in case an application expects it to be there
161 	 */
162 	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
163 	sf.lsf_code[1] = 0x0000000c;	/* syscall			*/
164 
165 	native_to_linux_sigset(&sf.lsf_mask, mask);
166 	for (i=0; i<32; i++) {
167 		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
168 	}
169 	sf.lsf_sc.lsc_mdhi = f->f_regs[_R_MULHI];
170 	sf.lsf_sc.lsc_mdlo = f->f_regs[_R_MULLO];
171 	sf.lsf_sc.lsc_pc = f->f_regs[_R_PC];
172 	sf.lsf_sc.lsc_status = f->f_regs[_R_SR];
173 	sf.lsf_sc.lsc_cause = f->f_regs[_R_CAUSE];
174 	sf.lsf_sc.lsc_badvaddr = f->f_regs[_R_BADVADDR];
175 	sendsig_reset(l, sig);
176 
177 	/*
178 	 * Save signal stack.  XXX broken
179 	 */
180 	/* kregs.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; */
181 
182 	/*
183 	 * Install the sigframe onto the stack
184 	 */
185 	fp -= sizeof(struct linux_sigframe);
186 	mutex_exit(p->p_lock);
187 	error = copyout(&sf, fp, sizeof(sf));
188 	mutex_enter(p->p_lock);
189 
190 	if (error != 0) {
191 		/*
192 		 * Process has trashed its stack; give it an illegal
193 		 * instruction to halt it in its tracks.
194 		 */
195 #ifdef DEBUG_LINUX
196 		printf("linux_sendsig: stack trashed\n");
197 #endif /* DEBUG_LINUX */
198 		sigexit(l, SIGILL);
199 		/* NOTREACHED */
200 	}
201 
202 	/* Set up the registers to return to sigcode. */
203 	f->f_regs[_R_A0] = native_to_linux_signo[sig];
204 	f->f_regs[_R_A1] = 0;
205 	f->f_regs[_R_A2] = (unsigned long)&fp->lsf_sc;
206 
207 #ifdef DEBUG_LINUX
208 	printf("sigcontext is at %p\n", &fp->lsf_sc);
209 #endif /* DEBUG_LINUX */
210 
211 	f->f_regs[_R_SP] = (unsigned long)fp;
212 	/* Signal trampoline code is at base of user stack. */
213 	f->f_regs[_R_RA] = (unsigned long)p->p_sigctx.ps_sigcode;
214 	f->f_regs[_R_T9] = (unsigned long)catcher;
215 	f->f_regs[_R_PC] = (unsigned long)catcher;
216 
217 	/* Remember that we're now on the signal stack. */
218 	if (onstack)
219 		l->l_sigstk.ss_flags |= SS_ONSTACK;
220 
221 	return;
222 }
223 
224 /*
225  * System call to cleanup state after a signal
226  * has been taken.  Reset signal mask and
227  * stack state from context left by sendsig (above).
228  */
229 int
230 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
231 {
232 	/* {
233 		syscallarg(struct linux_sigframe *) sf;
234 	} */
235 	struct proc *p = l->l_proc;
236 	struct linux_sigframe *sf, ksf;
237 	struct frame *f;
238 	sigset_t mask;
239 	int i, error;
240 
241 #ifdef DEBUG_LINUX
242 	printf("linux_sys_sigreturn()\n");
243 #endif /* DEBUG_LINUX */
244 
245 	/*
246 	 * The trampoline code hands us the context.
247 	 * It is unsafe to keep track of it ourselves, in the event that a
248 	 * program jumps out of a signal handler.
249 	 */
250 	sf = SCARG(uap, sf);
251 
252 	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
253 		return (error);
254 
255 	/* Restore the register context. */
256 	f = (struct frame *)l->l_md.md_regs;
257 	for (i=0; i<32; i++)
258 		f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
259 	f->f_regs[_R_MULLO] = ksf.lsf_sc.lsc_mdlo;
260 	f->f_regs[_R_MULHI] = ksf.lsf_sc.lsc_mdhi;
261 	f->f_regs[_R_PC] = ksf.lsf_sc.lsc_pc;
262 	f->f_regs[_R_BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
263 	f->f_regs[_R_CAUSE] = ksf.lsf_sc.lsc_cause;
264 
265 	mutex_enter(p->p_lock);
266 
267 	/* Restore signal stack. */
268 	l->l_sigstk.ss_flags &= ~SS_ONSTACK;
269 
270 	/* Restore signal mask. */
271 	linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask);
272 	(void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
273 
274 	mutex_exit(p->p_lock);
275 
276 	return (EJUSTRETURN);
277 }
278 
279 
280 int
281 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
282 {
283 	return (ENOSYS);
284 }
285 
286 
287 #if 0
288 int
289 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *uap, register_t *retval)
290 {
291 	/*
292 	 * This syscall is not implemented in Linux/Mips: we should not
293 	 * be here
294 	 */
295 #ifdef DEBUG_LINUX
296 	printf("linux_sys_modify_ldt: should not be here.\n");
297 #endif /* DEBUG_LINUX */
298   return 0;
299 }
300 #endif
301 
302 /*
303  * major device numbers remapping
304  */
305 dev_t
306 linux_fakedev(dev_t dev, int raw)
307 {
308 	/* XXX write me */
309 	return dev;
310 }
311 
312 /*
313  * We come here in a last attempt to satisfy a Linux ioctl() call
314  */
315 int
316 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
317 {
318 	return 0;
319 }
320 
321 /*
322  * See above. If a root process tries to set access to an I/O port,
323  * just let it have the whole range.
324  */
325 int
326 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *uap, register_t *retval)
327 {
328 	/*
329 	 * This syscall is not implemented in Linux/Mips: we should not be here
330 	 */
331 #ifdef DEBUG_LINUX
332 	printf("linux_sys_ioperm: should not be here.\n");
333 #endif /* DEBUG_LINUX */
334 	return 0;
335 }
336 
337 /*
338  * wrapper linux_sys_new_uname() -> linux_sys_uname()
339  */
340 int
341 linux_sys_new_uname(struct lwp *l, const struct linux_sys_new_uname_args *uap, register_t *retval)
342 {
343 /*
344  * Use this if you want to try Linux emulation with a glibc-2.2
345  * or higher. Note that signals will not work
346  */
347 #if 0
348         struct linux_sys_uname_args /* {
349                 syscallarg(struct linux_utsname *) up;
350         } */ *uap = v;
351         struct linux_utsname luts;
352 
353         strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
354         strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
355         strlcpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
356         strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
357         strlcpy(luts.l_machine, machine, sizeof(luts.l_machine));
358         strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
359 
360         return copyout(&luts, SCARG(uap, up), sizeof(luts));
361 #else
362 	return linux_sys_uname(l, (const void *)uap, retval);
363 #endif
364 }
365 
366 /*
367  * In Linux, cacheflush is currently implemented
368  * as a whole cache flush (arguments are ignored)
369  * we emulate this broken beahior.
370  */
371 int
372 linux_sys_cacheflush(struct lwp *l, const struct linux_sys_cacheflush_args *uap, register_t *retval)
373 {
374 	mips_icache_sync_all();
375 	mips_dcache_wbinv_all();
376 	return 0;
377 }
378 
379 /*
380  * This system call is depecated in Linux, but
381  * some binaries and some libraries use it.
382  */
383 int
384 linux_sys_sysmips(struct lwp *l, const struct linux_sys_sysmips_args *uap, register_t *retval)
385 {
386 #if 0
387 	struct linux_sys_sysmips_args {
388 		syscallarg(int) cmd;
389 		syscallarg(int) arg1;
390 		syscallarg(int) arg2;
391 		syscallarg(int) arg3;
392 	} *uap = v;
393 #endif
394 	int error;
395 
396 	switch (SCARG(uap, cmd)) {
397 	case LINUX_SETNAME: {
398 		char nodename [LINUX___NEW_UTS_LEN + 1];
399 		int name[2];
400 		size_t len;
401 
402 		if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
403 		    LINUX___NEW_UTS_LEN, &len)) != 0)
404 			return error;
405 
406 		name[0] = CTL_KERN;
407 		name[1] = KERN_HOSTNAME;
408 		return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL));
409 
410 		break;
411 	}
412 	case LINUX_MIPS_ATOMIC_SET: {
413 		void *addr;
414 		int s;
415 		u_int8_t value = 0;
416 
417 		addr = (void *)SCARG(uap, arg1);
418 
419 		s = splhigh();
420 		/*
421 		 * No error testing here. This is bad, but Linux does
422 		 * it like this. The source aknowledge "This is broken"
423 		 * in a comment...
424 		 */
425 		(void) copyin(addr, &value, 1);
426 		*retval = value;
427 		value = (u_int8_t) SCARG(uap, arg2);
428 		error = copyout(&value, addr, 1);
429 		splx(s);
430 
431 		return 0;
432 		break;
433 	}
434 	case LINUX_MIPS_FIXADE:		/* XXX not implemented */
435 		break;
436 	case LINUX_FLUSH_CACHE:
437 		mips_icache_sync_all();
438 		mips_dcache_wbinv_all();
439 		break;
440 	case LINUX_MIPS_RDNVRAM:
441 		return EIO;
442 		break;
443 	default:
444 		return EINVAL;
445 		break;
446 	}
447 #ifdef DEBUG_LINUX
448 	printf("linux_sys_sysmips(): unimplemented command %d\n",
449 	    SCARG(uap,cmd));
450 #endif /* DEBUG_LINUX */
451 	return 0;
452 }
453 
454 int
455 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
456 {
457 	return 0;
458 }
459