xref: /netbsd-src/sys/compat/linux/arch/arm/linux_machdep.c (revision 41aa5859b647891f23013f613e0858c4b789a7bf)
1 /*	$NetBSD: linux_machdep.c,v 1.34 2021/09/07 11:43:04 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 2000 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.
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 
34 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.34 2021/09/07 11:43:04 riastradh Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/signalvar.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/buf.h>
42 #include <sys/reboot.h>
43 #include <sys/conf.h>
44 #include <sys/exec.h>
45 #include <sys/file.h>
46 #include <sys/callout.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 
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 #include <compat/linux/linux_syscallargs.h>
68 
69 #include <arm/locore.h>
70 
71 void
linux_setregs(struct lwp * l,struct exec_package * epp,vaddr_t stack)72 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
73 {
74 
75 	setregs(l, epp, stack);
76 }
77 
78 void
linux_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)79 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
80 {
81 	struct lwp * const l = curlwp;
82 	struct proc * const p = l->l_proc;
83 	struct trapframe * const tf = lwp_trapframe(l);
84 	struct linux_sigframe *fp, frame;
85 	int onstack, error;
86 	const int sig = ksi->ksi_signo;
87 	sig_t catcher = SIGACTION(p, sig).sa_handler;
88 
89 
90 	/*
91 	 * The Linux version of this code is in
92 	 * linux/arch/arm/kernel/signal.c.
93 	 */
94 
95 	/* Do we need to jump onto the signal stack? */
96 	onstack =
97 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
98 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
99 
100 	/* Allocate space for the signal handler context. */
101 	if (onstack)
102 		fp = (struct linux_sigframe *)((char *)l->l_sigstk.ss_sp +
103 					  l->l_sigstk.ss_size);
104 	else
105 		fp = (struct linux_sigframe *)tf->tf_usr_sp;
106 	fp--;
107 
108 	/* Build stack frame for signal trampoline. */
109 
110 	memset(&frame, 0, sizeof(frame));
111 
112 	/* Save register context. */
113 	frame.sf_sc.sc_r0     = tf->tf_r0;
114 	frame.sf_sc.sc_r1     = tf->tf_r1;
115 	frame.sf_sc.sc_r2     = tf->tf_r2;
116 	frame.sf_sc.sc_r3     = tf->tf_r3;
117 	frame.sf_sc.sc_r4     = tf->tf_r4;
118 	frame.sf_sc.sc_r5     = tf->tf_r5;
119 	frame.sf_sc.sc_r6     = tf->tf_r6;
120 	frame.sf_sc.sc_r7     = tf->tf_r7;
121 	frame.sf_sc.sc_r8     = tf->tf_r8;
122 	frame.sf_sc.sc_r9     = tf->tf_r9;
123 	frame.sf_sc.sc_r10    = tf->tf_r10;
124 	frame.sf_sc.sc_r11    = tf->tf_r11;
125 	frame.sf_sc.sc_r12    = tf->tf_r12;
126 	frame.sf_sc.sc_sp     = tf->tf_usr_sp;
127 	frame.sf_sc.sc_lr     = tf->tf_usr_lr;
128 	frame.sf_sc.sc_pc     = tf->tf_pc;
129 	frame.sf_sc.sc_cpsr   = tf->tf_spsr;
130 
131 	/* Save signal stack. */
132 	/* Linux doesn't save the onstack flag in sigframe */
133 
134 	/* Save signal mask. */
135 	native_to_linux_old_extra_sigset(&frame.sf_sc.sc_mask,
136 	    frame.sf_extramask, mask);
137 
138 	/* Other state (mostly faked) */
139 	/*
140 	 * trapno should indicate the trap that caused the signal:
141 	 * 6  -> undefined instruction
142 	 * 11 -> address exception
143 	 * 14 -> data/prefetch abort
144 	 */
145 	frame.sf_sc.sc_trapno = 0;
146 	frame.sf_sc.sc_error_code = 0;
147 	frame.sf_sc.sc_fault_address = (u_int32_t) ksi->ksi_addr;
148 	sendsig_reset(l, sig);
149 
150 	mutex_exit(p->p_lock);
151 	error = copyout(&frame, fp, sizeof(frame));
152 	mutex_enter(p->p_lock);
153 
154 	if (error != 0) {
155 		/*
156 		 * Process has trashed its stack; give it an illegal
157 		 * instruction to halt it in its tracks.
158 		 */
159 		sigexit(l, SIGILL);
160 		/* NOTREACHED */
161 	}
162 	/*
163 	 * Build context to run handler in.
164 	 */
165 	tf->tf_r0 = native_to_linux_signo[sig];
166 	tf->tf_r1 = 0; /* XXX Should be a siginfo_t */
167 	tf->tf_r2 = 0;
168 	tf->tf_r3 = (register_t)catcher;
169 	tf->tf_usr_sp = (register_t)fp;
170 	tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode;
171 
172 	/* Remember that we're now on the signal stack. */
173 	if (onstack)
174 		l->l_sigstk.ss_flags |= SS_ONSTACK;
175 
176 }
177 
178 #if 0
179 /*
180  * System call to cleanup state after a signal
181  * has been taken.  Reset signal mask and
182  * stack state from context left by sendsig (above).
183  * Return to previous pc and psl as specified by
184  * context left by sendsig. Check carefully to
185  * make sure that the user has not modified the
186  * psl to gain improper privileges or to cause
187  * a machine fault.
188  */
189 int
190 linux_sys_rt_sigreturn(struct proc *p, void *v, register_t *retval)
191 {
192 	/* XXX XAX write me */
193 	return(ENOSYS);
194 }
195 #endif
196 
197 int
linux_sys_sigreturn(struct lwp * l,const struct linux_sys_sigreturn_args * v,register_t * retval)198 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *v,
199 	register_t *retval)
200 {
201 	struct trapframe * const tf = lwp_trapframe(l);
202 	struct proc * const p = l->l_proc;
203 	struct linux_sigframe *sfp, frame;
204 	sigset_t mask;
205 
206 	/*
207 	 * The trampoline code hands us the context.
208 	 * It is unsafe to keep track of it ourselves, in the event that a
209 	 * program jumps out of a signal handler.
210 	 */
211 	sfp = (struct linux_sigframe *)tf->tf_usr_sp;
212 	if (copyin((void *)sfp, &frame, sizeof(*sfp)) != 0)
213 		return (EFAULT);
214 
215 	/*
216 	 * Make sure the processor mode has not been tampered with and
217 	 * interrupts have not been disabled.
218 	 */
219 	if (!VALID_PSR(frame.sf_sc.sc_cpsr))
220 		return EINVAL;
221 
222 	/* Restore register context. */
223 	tf->tf_r0    = frame.sf_sc.sc_r0;
224 	tf->tf_r1    = frame.sf_sc.sc_r1;
225 	tf->tf_r2    = frame.sf_sc.sc_r2;
226 	tf->tf_r3    = frame.sf_sc.sc_r3;
227 	tf->tf_r4    = frame.sf_sc.sc_r4;
228 	tf->tf_r5    = frame.sf_sc.sc_r5;
229 	tf->tf_r6    = frame.sf_sc.sc_r6;
230 	tf->tf_r7    = frame.sf_sc.sc_r7;
231 	tf->tf_r8    = frame.sf_sc.sc_r8;
232 	tf->tf_r9    = frame.sf_sc.sc_r9;
233 	tf->tf_r10   = frame.sf_sc.sc_r10;
234 	tf->tf_r11   = frame.sf_sc.sc_r11;
235 	tf->tf_r12   = frame.sf_sc.sc_r12;
236 	tf->tf_usr_sp = frame.sf_sc.sc_sp;
237 	tf->tf_usr_lr = frame.sf_sc.sc_lr;
238 	tf->tf_pc    = frame.sf_sc.sc_pc;
239 	tf->tf_spsr  = frame.sf_sc.sc_cpsr;
240 
241 	mutex_enter(p->p_lock);
242 
243 	/* Restore signal stack. */
244 	l->l_sigstk.ss_flags &= ~SS_ONSTACK;
245 
246 	/* Restore signal mask. */
247 	linux_old_extra_to_native_sigset(&mask, &frame.sf_sc.sc_mask,
248 	    frame.sf_extramask);
249 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
250 
251 	mutex_exit(p->p_lock);
252 
253 	return (EJUSTRETURN);
254 }
255 
256 /*
257  * major device numbers remapping
258  */
259 dev_t
linux_fakedev(dev_t dev,int raw)260 linux_fakedev(dev_t dev, int raw)
261 {
262 	/* XXX write me */
263 	return dev;
264 }
265 
266 /*
267  * We come here in a last attempt to satisfy a Linux ioctl() call
268  */
269 int
linux_machdepioctl(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)270 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
271 {
272 	/* {
273 		syscallarg(int) fd;
274 		syscallarg(u_long) com;
275 		syscallarg(void *) data;
276 	} */
277 	struct sys_ioctl_args bia;
278 	u_long com;
279 
280 	SCARG(&bia, fd) = SCARG(uap, fd);
281 	SCARG(&bia, data) = SCARG(uap, data);
282 	com = SCARG(uap, com);
283 
284 	switch (com) {
285 	default:
286 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
287 		return EINVAL;
288 	}
289 	SCARG(&bia, com) = com;
290 
291 	return sys_ioctl(l, &bia, retval);
292 }
293 
294 int
linux_usertrap(struct lwp * l,vaddr_t trapaddr,void * arg)295 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
296 {
297 	return 0;
298 }
299