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