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