xref: /netbsd-src/sys/compat/linux/arch/mips/linux_machdep.c (revision 21a3d2f02241c56556f4b2305ef1b8036f268f70)
1 /*	$NetBSD: linux_machdep.c,v 1.4 2001/10/14 17:21:47 manu 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/param.h>
40 #include <sys/systm.h>
41 #include <sys/signalvar.h>
42 #include <sys/kernel.h>
43 #include <sys/map.h>
44 #include <sys/proc.h>
45 #include <sys/user.h>
46 #include <sys/buf.h>
47 #include <sys/reboot.h>
48 #include <sys/conf.h>
49 #include <sys/exec.h>
50 #include <sys/file.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/msgbuf.h>
55 #include <sys/mount.h>
56 #include <sys/vnode.h>
57 #include <sys/device.h>
58 #include <sys/syscallargs.h>
59 #include <sys/filedesc.h>
60 #include <sys/exec_elf.h>
61 #include <sys/disklabel.h>
62 #include <sys/ioctl.h>
63 #include <sys/sysctl.h>
64 #include <miscfs/specfs/specdev.h>
65 
66 #include <compat/linux/common/linux_types.h>
67 #include <compat/linux/common/linux_signal.h>
68 #include <compat/linux/common/linux_util.h>
69 #include <compat/linux/common/linux_ioctl.h>
70 #include <compat/linux/common/linux_hdio.h>
71 #include <compat/linux/common/linux_exec.h>
72 #include <compat/linux/common/linux_machdep.h>
73 
74 #include <compat/linux/linux_syscallargs.h>
75 
76 #include <machine/cpu.h>
77 #include <machine/psl.h>
78 #include <machine/reg.h>
79 #include <machine/regnum.h>
80 #include <machine/vmparam.h>
81 #include <machine/locore.h>
82 
83 /*
84  * To see whether wscons is configured (for virtual console ioctl calls).
85  */
86 #if defined(_KERNEL_OPT)
87 #include "wsdisplay.h"
88 #endif
89 #if (NWSDISPLAY > 0)
90 #include <dev/wscons/wsconsio.h>
91 #include <dev/wscons/wsdisplay_usl_io.h>
92 #endif
93 
94 /*
95  * Set set up registers on exec.
96  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
97  * entry uses NetBSD's native setregs instead of linux_setregs
98  */
99 void
100 linux_setregs(p, pack, stack)
101 	struct proc *p;
102 	struct exec_package *pack;
103 	u_long stack;
104 {
105 	setregs(p, pack, stack);
106 	return;
107 }
108 
109 /*
110  * Send an interrupt to process.
111  *
112  * Adapted from sys/arch/mips/mips/mips_machdep.c
113  *
114  * XXX Does not work well yet with RT signals
115  *
116  */
117 
118 void
119 linux_sendsig(catcher, sig, mask, code)  /* XXX Check me */
120 	sig_t catcher;
121 	int sig;
122 	sigset_t *mask;
123 	u_long code;
124 {
125 	struct proc *p = curproc;
126 	struct linux_sigframe *fp;
127 	struct frame *f;
128 	int i,onstack;
129 	struct linux_sigframe sf;
130 
131 #ifdef DEBUG_LINUX
132 	printf("linux_sendsig()\n");
133 #endif /* DEBUG_LINUX */
134 	f = (struct frame *)p->p_md.md_regs;
135 #ifdef DEBUG_LINUX
136 	printf("f = %p\n", f);
137 #endif /* DEBUG_LINUX */
138 
139 	/*
140 	 * Do we need to jump onto the signal stack?
141 	 */
142 	onstack =
143 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
144 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
145 
146 	/*
147 	 * Signal stack is broken (see at the end of linux_Sigreturn), so we do
148 	 * not use it yet. XXX fix this.
149 	 */
150 	onstack=0;
151 
152 	/*
153 	 * Allocate space for the signal handler context.
154 	 */
155 	if (onstack)
156 		fp = (struct linux_sigframe *)
157 		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
158 		    + p->p_sigctx.ps_sigstk.ss_size);
159 	else
160 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
161 		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];
162 
163 #ifdef DEBUG_LINUX
164 	printf("fp = %p, sf = %p\n", fp, &sf);
165 #endif /* DEBUG_LINUX */
166 
167 	/*
168 	 * Build stack frame for signal trampoline.
169 	 */
170 	memset(&sf, 0, sizeof sf);
171 
172 	/*
173 	 * This is the signal trampoline used by Linux, we don't use it,
174 	 * but we set ip up in case an application expects it to be there
175 	 */
176 	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
177 	sf.lsf_code[1] = 0x0000000c;	/* syscall			*/
178 
179 	native_to_linux_sigset(mask, &sf.lsf_mask);
180 	for (i=0; i<32; i++)
181 		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
182 	sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
183 	sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
184 	sf.lsf_sc.lsc_pc = f->f_regs[PC];
185 	sf.lsf_sc.lsc_status = f->f_regs[SR];	/* XXX */
186 	sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
187 	sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR];	/* XXX */
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(p, SIGILL);
207 		/* NOTREACHED */
208 	}
209 
210 	/* Set up the registers to return to sigcode. */
211 	f->f_regs[A0] = native_to_linux_sig[sig];
212 	f->f_regs[A1] = 0;
213 	f->f_regs[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[SP] = (unsigned long)fp;
220 	/* Signal trampoline code is at base of user stack. */
221 	f->f_regs[RA] = (unsigned long)p->p_sigctx.ps_sigcode;
222 	f->f_regs[T9] = (unsigned long)catcher;
223 	f->f_regs[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(p, v, retval)
239 	struct proc *p;
240 	void *v;
241 	register_t *retval;
242 {
243 	struct linux_sys_sigreturn_args /* {
244 		syscallarg(struct linux_pt_regs) regs;
245 	} */ *uap = v;
246 	struct linux_pt_regs regs, kregs;
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 	regs = SCARG(uap, regs);
262 
263 	kregs = regs;
264 	/* if ((error = copyin(regs, &kregs, sizeof(kregs))) != 0)
265 		return (error); */
266 
267 	sf = (struct linux_sigframe *)kregs.lregs[29];
268 	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
269 		return (error);
270 
271 	/* Restore the register context. */
272 	f = (struct frame *)p->p_md.md_regs;
273 #ifdef DEBUG_LINUX
274 	printf("sf = %p, f = %p\n", sf, f);
275 #endif /* DEBUG_LINUX */
276 	for (i=0; i<32; i++)
277 		f->f_regs[i] = kregs.lregs[i];
278 	f->f_regs[MULLO] = kregs.llo;
279 	f->f_regs[MULHI] = kregs.lhi;
280 	f->f_regs[PC] = kregs.lcp0_epc;
281 	f->f_regs[BADVADDR] = kregs.lcp0_badvaddr;
282 	f->f_regs[CAUSE] = kregs.lcp0_cause;
283 
284 	/* Restore signal stack. */
285 	p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
286 
287 	/* Restore signal mask. */
288 	linux_to_native_sigset((linux_sigset_t *)&ksf.lsf_mask, &mask);
289 	(void)sigprocmask1(p, SIG_SETMASK, &mask, 0);
290 
291 	return (EJUSTRETURN);
292 }
293 
294 
295 int
296 linux_sys_rt_sigreturn(p, v, retval)
297 	struct proc *p;
298 	void *v;
299 	register_t *retval;
300 {
301 	return 0;
302 }
303 
304 
305 #if 0
306 int
307 linux_sys_modify_ldt(p, v, retval)
308 	struct proc *p;
309 	void *v;
310 	register_t *retval;
311 {
312 	/*
313 	 * This syscall is not implemented in Linux/Mips: we should not
314 	 * be here
315 	 */
316 #ifdef DEBUG_LINUX
317 	printf("linux_sys_modify_ldt: should not be here.\n");
318 #endif /* DEBUG_LINUX */
319   return 0;
320 }
321 #endif
322 
323 /*
324  * major device numbers remapping
325  */
326 dev_t
327 linux_fakedev(dev)
328 	dev_t dev;
329 {
330   /* XXX write me */
331   return dev;
332 }
333 
334 /*
335  * We come here in a last attempt to satisfy a Linux ioctl() call
336  */
337 int
338 linux_machdepioctl(p, v, retval)
339 	struct proc *p;
340 	void *v;
341 	register_t *retval;
342 {
343 	return 0;
344 }
345 
346 /*
347  * See above. If a root process tries to set access to an I/O port,
348  * just let it have the whole range.
349  */
350 int
351 linux_sys_ioperm(p, v, retval)
352 	struct proc *p;
353 	void *v;
354 	register_t *retval;
355 {
356 	/*
357 	 * This syscall is not implemented in Linux/Mips: we should not be here
358 	 */
359 #ifdef DEBUG_LINUX
360 	printf("linux_sys_ioperm: should not be here.\n");
361 #endif /* DEBUG_LINUX */
362 	return 0;
363 }
364 
365 /*
366  * wrapper linux_sys_new_uname() -> linux_sys_uname()
367  */
368 int
369 linux_sys_new_uname(p, v, retval)
370 	struct proc *p;
371 	void *v;
372 	register_t *retval;
373 {
374 /*
375  * Use this if you want to try Linux emulation with a glibc-2.2
376  * or higher. Note that signals will not work
377  */
378 #if 0
379         struct linux_sys_uname_args /* {
380                 syscallarg(struct linux_utsname *) up;
381         } */ *uap = v;
382         struct linux_utsname luts;
383 
384         strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
385         strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
386         strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
387         strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
388         strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
389         strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
390 
391         return copyout(&luts, SCARG(uap, up), sizeof(luts));
392 #else
393 	return linux_sys_uname(p, v, retval);
394 #endif
395 }
396 
397 /*
398  * In Linux, cacheflush is icurrently implemented
399  * as a whole cache flush (arguments are ignored)
400  * we emulate this broken beahior.
401  */
402 int
403 linux_sys_cacheflush(p, v, retval)
404 	struct proc *p;
405 	void *v;
406 	register_t *retval;
407 {
408 	MachFlushCache();
409 	return 0;
410 }
411 
412 /*
413  * This system call is depecated in Linux, but
414  * some binaries and some libraries use it.
415  */
416 int
417 linux_sys_sysmips(p, v, retval)
418 	struct proc *p;
419 	void *v;
420 	register_t *retval;
421 {
422 	struct linux_sys_sysmips_args {
423 		syscallarg(int) cmd;
424 		syscallarg(int) arg1;
425 		syscallarg(int) arg2;
426 		syscallarg(int) arg3;
427 	} *uap = v;
428 	int error;
429 
430 	switch (SCARG(uap, cmd)) {
431 	case LINUX_SETNAME: {
432 		char nodename [LINUX___NEW_UTS_LEN + 1];
433 		int name;
434 		size_t len;
435 
436 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
437 			return error;
438 		if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
439 		    LINUX___NEW_UTS_LEN, &len)) != 0)
440 			return error;
441 
442 		name = KERN_HOSTNAME;
443 		return (kern_sysctl(&name, 1, 0, 0, nodename, len, p));
444 
445 		break;
446 	}
447 	case LINUX_MIPS_ATOMIC_SET: {
448 		void *addr;
449 		int s;
450 
451 		addr = (void *)SCARG(uap, arg1);
452 
453 		if ((uvm_useracc((caddr_t)addr, sizeof(int),
454 		    B_READ | B_WRITE)) != 1)
455 			return EFAULT;
456 
457 		s = splhigh();
458 		/*
459 		 * No error testing here. This is bad, but Linux does
460 		 * it like this. The source aknowledge "This is broken"
461 		 * in a comment...
462 		 */
463 		*retval = (register_t)fubyte(addr);
464 		error = subyte(addr, SCARG(uap, arg2));
465 		splx(s);
466 
467 		return 0;
468 		break;
469 	}
470 	case LINUX_MIPS_FIXADE:		/* XXX not implemented */
471 		break;
472 	case LINUX_FLUSH_CACHE:
473 		MachFlushCache();
474 		break;
475 	case LINUX_MIPS_RDNVRAM:
476 		return EIO;
477 		break;
478 	default:
479 		return EINVAL;
480 		break;
481 	}
482 #ifdef DEBUG_LINUX
483 	printf("linux_sys_sysmips(): unimplemented command %d\n",
484 	    SCARG(uap,cmd));
485 #endif /* DEBUG_LINUX */
486 	return 0;
487 }
488