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