xref: /netbsd-src/sys/compat/linux/arch/arm/linux_machdep.c (revision 41aa5859b647891f23013f613e0858c4b789a7bf)
1*41aa5859Sriastradh /*	$NetBSD: linux_machdep.c,v 1.34 2021/09/07 11:43:04 riastradh Exp $	*/
264625064Sbjh21 
364625064Sbjh21 /*-
464625064Sbjh21  * Copyright (c) 1995, 2000 The NetBSD Foundation, Inc.
564625064Sbjh21  * All rights reserved.
664625064Sbjh21  *
764625064Sbjh21  * This code is derived from software contributed to The NetBSD Foundation
864625064Sbjh21  * by Frank van der Linden.
964625064Sbjh21  *
1064625064Sbjh21  * Redistribution and use in source and binary forms, with or without
1164625064Sbjh21  * modification, are permitted provided that the following conditions
1264625064Sbjh21  * are met:
1364625064Sbjh21  * 1. Redistributions of source code must retain the above copyright
1464625064Sbjh21  *    notice, this list of conditions and the following disclaimer.
1564625064Sbjh21  * 2. Redistributions in binary form must reproduce the above copyright
1664625064Sbjh21  *    notice, this list of conditions and the following disclaimer in the
1764625064Sbjh21  *    documentation and/or other materials provided with the distribution.
1864625064Sbjh21  *
1964625064Sbjh21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2064625064Sbjh21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2164625064Sbjh21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2264625064Sbjh21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2364625064Sbjh21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2464625064Sbjh21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2564625064Sbjh21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2664625064Sbjh21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2764625064Sbjh21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2864625064Sbjh21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2964625064Sbjh21  * POSSIBILITY OF SUCH DAMAGE.
3064625064Sbjh21  */
3164625064Sbjh21 
32ef314e20Shubertf #include <sys/cdefs.h>
3364625064Sbjh21 
34*41aa5859Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.34 2021/09/07 11:43:04 riastradh Exp $");
3564625064Sbjh21 
3664625064Sbjh21 #include <sys/param.h>
3764625064Sbjh21 #include <sys/systm.h>
3864625064Sbjh21 #include <sys/signalvar.h>
3964625064Sbjh21 #include <sys/kernel.h>
4064625064Sbjh21 #include <sys/proc.h>
4164625064Sbjh21 #include <sys/buf.h>
4264625064Sbjh21 #include <sys/reboot.h>
4364625064Sbjh21 #include <sys/conf.h>
4464625064Sbjh21 #include <sys/exec.h>
4564625064Sbjh21 #include <sys/file.h>
4664625064Sbjh21 #include <sys/callout.h>
4764625064Sbjh21 #include <sys/mbuf.h>
4864625064Sbjh21 #include <sys/msgbuf.h>
4964625064Sbjh21 #include <sys/mount.h>
5064625064Sbjh21 #include <sys/vnode.h>
5164625064Sbjh21 #include <sys/device.h>
5264625064Sbjh21 #include <sys/syscallargs.h>
5364625064Sbjh21 #include <sys/filedesc.h>
5464625064Sbjh21 #include <sys/exec_elf.h>
5564625064Sbjh21 #include <sys/disklabel.h>
5664625064Sbjh21 #include <sys/ioctl.h>
57db757689Smatt 
5864625064Sbjh21 #include <miscfs/specfs/specdev.h>
5964625064Sbjh21 
6064625064Sbjh21 #include <compat/linux/common/linux_types.h>
6164625064Sbjh21 #include <compat/linux/common/linux_signal.h>
6264625064Sbjh21 #include <compat/linux/common/linux_util.h>
6364625064Sbjh21 #include <compat/linux/common/linux_ioctl.h>
6464625064Sbjh21 #include <compat/linux/common/linux_hdio.h>
6564625064Sbjh21 #include <compat/linux/common/linux_exec.h>
6664625064Sbjh21 #include <compat/linux/common/linux_machdep.h>
6764625064Sbjh21 #include <compat/linux/linux_syscallargs.h>
6864625064Sbjh21 
69db757689Smatt #include <arm/locore.h>
70db757689Smatt 
7164625064Sbjh21 void
linux_setregs(struct lwp * l,struct exec_package * epp,vaddr_t stack)7233fa5ccbSchs linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
7364625064Sbjh21 {
7464625064Sbjh21 
7546141a31Sthorpej 	setregs(l, epp, stack);
7664625064Sbjh21 }
7764625064Sbjh21 
7864625064Sbjh21 void
linux_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)790dbe439eSmatt linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
8064625064Sbjh21 {
81f622e63aSmatt 	struct lwp * const l = curlwp;
82f622e63aSmatt 	struct proc * const p = l->l_proc;
83f622e63aSmatt 	struct trapframe * const tf = lwp_trapframe(l);
846ad60873Sbjh21 	struct linux_sigframe *fp, frame;
85b07ec3fcSad 	int onstack, error;
860dbe439eSmatt 	const int sig = ksi->ksi_signo;
87011d4d5fSthorpej 	sig_t catcher = SIGACTION(p, sig).sa_handler;
8864625064Sbjh21 
896ad60873Sbjh21 
906ad60873Sbjh21 	/*
916ad60873Sbjh21 	 * The Linux version of this code is in
926ad60873Sbjh21 	 * linux/arch/arm/kernel/signal.c.
936ad60873Sbjh21 	 */
946ad60873Sbjh21 
956ad60873Sbjh21 	/* Do we need to jump onto the signal stack? */
966ad60873Sbjh21 	onstack =
97b07ec3fcSad 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
986ad60873Sbjh21 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
996ad60873Sbjh21 
1006ad60873Sbjh21 	/* Allocate space for the signal handler context. */
1016ad60873Sbjh21 	if (onstack)
1025ebcdbe8Schristos 		fp = (struct linux_sigframe *)((char *)l->l_sigstk.ss_sp +
103b07ec3fcSad 					  l->l_sigstk.ss_size);
1046ad60873Sbjh21 	else
1056ad60873Sbjh21 		fp = (struct linux_sigframe *)tf->tf_usr_sp;
1066ad60873Sbjh21 	fp--;
1076ad60873Sbjh21 
1086ad60873Sbjh21 	/* Build stack frame for signal trampoline. */
1096ad60873Sbjh21 
110*41aa5859Sriastradh 	memset(&frame, 0, sizeof(frame));
111*41aa5859Sriastradh 
1126ad60873Sbjh21 	/* Save register context. */
1136ad60873Sbjh21 	frame.sf_sc.sc_r0     = tf->tf_r0;
1146ad60873Sbjh21 	frame.sf_sc.sc_r1     = tf->tf_r1;
1156ad60873Sbjh21 	frame.sf_sc.sc_r2     = tf->tf_r2;
1166ad60873Sbjh21 	frame.sf_sc.sc_r3     = tf->tf_r3;
1176ad60873Sbjh21 	frame.sf_sc.sc_r4     = tf->tf_r4;
1186ad60873Sbjh21 	frame.sf_sc.sc_r5     = tf->tf_r5;
1196ad60873Sbjh21 	frame.sf_sc.sc_r6     = tf->tf_r6;
1206ad60873Sbjh21 	frame.sf_sc.sc_r7     = tf->tf_r7;
1216ad60873Sbjh21 	frame.sf_sc.sc_r8     = tf->tf_r8;
1226ad60873Sbjh21 	frame.sf_sc.sc_r9     = tf->tf_r9;
1236ad60873Sbjh21 	frame.sf_sc.sc_r10    = tf->tf_r10;
1246ad60873Sbjh21 	frame.sf_sc.sc_r11    = tf->tf_r11;
1256ad60873Sbjh21 	frame.sf_sc.sc_r12    = tf->tf_r12;
1266ad60873Sbjh21 	frame.sf_sc.sc_sp     = tf->tf_usr_sp;
1276ad60873Sbjh21 	frame.sf_sc.sc_lr     = tf->tf_usr_lr;
1286ad60873Sbjh21 	frame.sf_sc.sc_pc     = tf->tf_pc;
1296ad60873Sbjh21 	frame.sf_sc.sc_cpsr   = tf->tf_spsr;
1306ad60873Sbjh21 
1316ad60873Sbjh21 	/* Save signal stack. */
1326ad60873Sbjh21 	/* Linux doesn't save the onstack flag in sigframe */
1336ad60873Sbjh21 
1346ad60873Sbjh21 	/* Save signal mask. */
1350fd75dd4Sbjh21 	native_to_linux_old_extra_sigset(&frame.sf_sc.sc_mask,
1360fd75dd4Sbjh21 	    frame.sf_extramask, mask);
1376ad60873Sbjh21 
1386ad60873Sbjh21 	/* Other state (mostly faked) */
1396ad60873Sbjh21 	/*
1406ad60873Sbjh21 	 * trapno should indicate the trap that caused the signal:
1416ad60873Sbjh21 	 * 6  -> undefined instruction
1426ad60873Sbjh21 	 * 11 -> address exception
1436ad60873Sbjh21 	 * 14 -> data/prefetch abort
1446ad60873Sbjh21 	 */
1456ad60873Sbjh21 	frame.sf_sc.sc_trapno = 0;
1466ad60873Sbjh21 	frame.sf_sc.sc_error_code = 0;
1470dbe439eSmatt 	frame.sf_sc.sc_fault_address = (u_int32_t) ksi->ksi_addr;
148b07ec3fcSad 	sendsig_reset(l, sig);
1496ad60873Sbjh21 
150284c2b9aSad 	mutex_exit(p->p_lock);
151b07ec3fcSad 	error = copyout(&frame, fp, sizeof(frame));
152284c2b9aSad 	mutex_enter(p->p_lock);
153b07ec3fcSad 
154b07ec3fcSad 	if (error != 0) {
1556ad60873Sbjh21 		/*
1566ad60873Sbjh21 		 * Process has trashed its stack; give it an illegal
1576ad60873Sbjh21 		 * instruction to halt it in its tracks.
1586ad60873Sbjh21 		 */
15946141a31Sthorpej 		sigexit(l, SIGILL);
1606ad60873Sbjh21 		/* NOTREACHED */
1616ad60873Sbjh21 	}
1626ad60873Sbjh21 	/*
1636ad60873Sbjh21 	 * Build context to run handler in.
1646ad60873Sbjh21 	 */
165ac29fb37Schristos 	tf->tf_r0 = native_to_linux_signo[sig];
1666ad60873Sbjh21 	tf->tf_r1 = 0; /* XXX Should be a siginfo_t */
1676ad60873Sbjh21 	tf->tf_r2 = 0;
1686ad60873Sbjh21 	tf->tf_r3 = (register_t)catcher;
1696ad60873Sbjh21 	tf->tf_usr_sp = (register_t)fp;
1706ad60873Sbjh21 	tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode;
1716ad60873Sbjh21 
1726ad60873Sbjh21 	/* Remember that we're now on the signal stack. */
1736ad60873Sbjh21 	if (onstack)
174b07ec3fcSad 		l->l_sigstk.ss_flags |= SS_ONSTACK;
1756ad60873Sbjh21 
17664625064Sbjh21 }
1773113edfbSbjh21 
1783113edfbSbjh21 #if 0
17964625064Sbjh21 /*
18064625064Sbjh21  * System call to cleanup state after a signal
18164625064Sbjh21  * has been taken.  Reset signal mask and
18264625064Sbjh21  * stack state from context left by sendsig (above).
18364625064Sbjh21  * Return to previous pc and psl as specified by
18464625064Sbjh21  * context left by sendsig. Check carefully to
18564625064Sbjh21  * make sure that the user has not modified the
18664625064Sbjh21  * psl to gain improper privileges or to cause
18764625064Sbjh21  * a machine fault.
18864625064Sbjh21  */
18964625064Sbjh21 int
19028bae79bSdsl linux_sys_rt_sigreturn(struct proc *p, void *v, register_t *retval)
19164625064Sbjh21 {
19264625064Sbjh21 	/* XXX XAX write me */
19364625064Sbjh21 	return(ENOSYS);
19464625064Sbjh21 }
1953113edfbSbjh21 #endif
19664625064Sbjh21 
19764625064Sbjh21 int
linux_sys_sigreturn(struct lwp * l,const struct linux_sys_sigreturn_args * v,register_t * retval)19839a278beSmatt linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *v,
19939a278beSmatt 	register_t *retval)
20064625064Sbjh21 {
201f622e63aSmatt 	struct trapframe * const tf = lwp_trapframe(l);
202f622e63aSmatt 	struct proc * const p = l->l_proc;
2036ad60873Sbjh21 	struct linux_sigframe *sfp, frame;
2046ad60873Sbjh21 	sigset_t mask;
20564625064Sbjh21 
2066ad60873Sbjh21 	/*
2076ad60873Sbjh21 	 * The trampoline code hands us the context.
2086ad60873Sbjh21 	 * It is unsafe to keep track of it ourselves, in the event that a
2096ad60873Sbjh21 	 * program jumps out of a signal handler.
2106ad60873Sbjh21 	 */
2116ad60873Sbjh21 	sfp = (struct linux_sigframe *)tf->tf_usr_sp;
21253524e44Schristos 	if (copyin((void *)sfp, &frame, sizeof(*sfp)) != 0)
2136ad60873Sbjh21 		return (EFAULT);
2146ad60873Sbjh21 
2156ad60873Sbjh21 	/*
2166ad60873Sbjh21 	 * Make sure the processor mode has not been tampered with and
2176ad60873Sbjh21 	 * interrupts have not been disabled.
2186ad60873Sbjh21 	 */
219496e2b65Sskrll 	if (!VALID_PSR(frame.sf_sc.sc_cpsr))
2206ad60873Sbjh21 		return EINVAL;
2216ad60873Sbjh21 
2226ad60873Sbjh21 	/* Restore register context. */
2236ad60873Sbjh21 	tf->tf_r0    = frame.sf_sc.sc_r0;
2246ad60873Sbjh21 	tf->tf_r1    = frame.sf_sc.sc_r1;
2256ad60873Sbjh21 	tf->tf_r2    = frame.sf_sc.sc_r2;
2266ad60873Sbjh21 	tf->tf_r3    = frame.sf_sc.sc_r3;
2276ad60873Sbjh21 	tf->tf_r4    = frame.sf_sc.sc_r4;
2286ad60873Sbjh21 	tf->tf_r5    = frame.sf_sc.sc_r5;
2296ad60873Sbjh21 	tf->tf_r6    = frame.sf_sc.sc_r6;
2306ad60873Sbjh21 	tf->tf_r7    = frame.sf_sc.sc_r7;
2316ad60873Sbjh21 	tf->tf_r8    = frame.sf_sc.sc_r8;
2326ad60873Sbjh21 	tf->tf_r9    = frame.sf_sc.sc_r9;
2336ad60873Sbjh21 	tf->tf_r10   = frame.sf_sc.sc_r10;
2346ad60873Sbjh21 	tf->tf_r11   = frame.sf_sc.sc_r11;
2356ad60873Sbjh21 	tf->tf_r12   = frame.sf_sc.sc_r12;
2366ad60873Sbjh21 	tf->tf_usr_sp = frame.sf_sc.sc_sp;
2376ad60873Sbjh21 	tf->tf_usr_lr = frame.sf_sc.sc_lr;
2386ad60873Sbjh21 	tf->tf_pc    = frame.sf_sc.sc_pc;
2396ad60873Sbjh21 	tf->tf_spsr  = frame.sf_sc.sc_cpsr;
2406ad60873Sbjh21 
241284c2b9aSad 	mutex_enter(p->p_lock);
242b07ec3fcSad 
2436ad60873Sbjh21 	/* Restore signal stack. */
244b07ec3fcSad 	l->l_sigstk.ss_flags &= ~SS_ONSTACK;
2456ad60873Sbjh21 
2466ad60873Sbjh21 	/* Restore signal mask. */
2470fd75dd4Sbjh21 	linux_old_extra_to_native_sigset(&mask, &frame.sf_sc.sc_mask,
2480fd75dd4Sbjh21 	    frame.sf_extramask);
249b07ec3fcSad 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
250b07ec3fcSad 
251284c2b9aSad 	mutex_exit(p->p_lock);
2526ad60873Sbjh21 
2536ad60873Sbjh21 	return (EJUSTRETURN);
25464625064Sbjh21 }
25564625064Sbjh21 
25664625064Sbjh21 /*
25764625064Sbjh21  * major device numbers remapping
25864625064Sbjh21  */
25964625064Sbjh21 dev_t
linux_fakedev(dev_t dev,int raw)26028bae79bSdsl linux_fakedev(dev_t dev, int raw)
26164625064Sbjh21 {
26264625064Sbjh21 	/* XXX write me */
26364625064Sbjh21 	return dev;
26464625064Sbjh21 }
26564625064Sbjh21 
26664625064Sbjh21 /*
26764625064Sbjh21  * We come here in a last attempt to satisfy a Linux ioctl() call
26864625064Sbjh21  */
26964625064Sbjh21 int
linux_machdepioctl(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)2707e2790cfSdsl linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
27164625064Sbjh21 {
2727e2790cfSdsl 	/* {
27364625064Sbjh21 		syscallarg(int) fd;
27464625064Sbjh21 		syscallarg(u_long) com;
27553524e44Schristos 		syscallarg(void *) data;
2767e2790cfSdsl 	} */
27764625064Sbjh21 	struct sys_ioctl_args bia;
27864625064Sbjh21 	u_long com;
27964625064Sbjh21 
28064625064Sbjh21 	SCARG(&bia, fd) = SCARG(uap, fd);
28164625064Sbjh21 	SCARG(&bia, data) = SCARG(uap, data);
28264625064Sbjh21 	com = SCARG(uap, com);
28364625064Sbjh21 
28464625064Sbjh21 	switch (com) {
28564625064Sbjh21 	default:
28664625064Sbjh21 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
28764625064Sbjh21 		return EINVAL;
28864625064Sbjh21 	}
28964625064Sbjh21 	SCARG(&bia, com) = com;
29095e1ffb1Schristos 
29195e1ffb1Schristos 	return sys_ioctl(l, &bia, retval);
29264625064Sbjh21 }
29333e2d79fSfvdl 
29433e2d79fSfvdl int
linux_usertrap(struct lwp * l,vaddr_t trapaddr,void * arg)29533e2d79fSfvdl linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
29633e2d79fSfvdl {
29733e2d79fSfvdl 	return 0;
29833e2d79fSfvdl }
299