linux_machdep.c revision 1.6 1 /* $NetBSD: linux_machdep.c,v 1.6 2002/02/17 23:05:08 bjh21 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.6 2002/02/17 23:05:08 bjh21 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/map.h>
48 #include <sys/proc.h>
49 #include <sys/user.h>
50 #include <sys/buf.h>
51 #include <sys/reboot.h>
52 #include <sys/conf.h>
53 #include <sys/exec.h>
54 #include <sys/file.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/msgbuf.h>
59 #include <sys/mount.h>
60 #include <sys/vnode.h>
61 #include <sys/device.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(p, epp, stack)
81 struct proc *p;
82 struct exec_package *epp;
83 u_long stack;
84 {
85 /* struct pcb *pcb = &p->p_addr->u_pcb; */
86
87 setregs(p, epp, stack);
88 }
89
90 static __inline struct trapframe *
91 process_frame(struct proc *p)
92 {
93
94 return p->p_addr->u_pcb.pcb_tf;
95 }
96
97 void
98 linux_sendsig(catcher, sig, mask, code)
99 sig_t catcher;
100 int sig;
101 sigset_t *mask;
102 u_long code;
103 {
104 struct proc *p = curproc;
105 struct trapframe *tf;
106 struct linux_sigframe *fp, frame;
107 int onstack;
108
109 tf = process_frame(p);
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(p, SIGILL);
174 /* NOTREACHED */
175 }
176 /*
177 * Build context to run handler in.
178 */
179 tf->tf_r0 = native_to_linux_sig[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 */
203 int
204 linux_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
214 int
215 linux_sys_sigreturn(p, v, retval)
216 struct proc *p;
217 void *v;
218 register_t *retval;
219 {
220 struct linux_sigframe *sfp, frame;
221 struct trapframe *tf;
222 sigset_t mask;
223
224 tf = process_frame(p);
225
226 /*
227 * The trampoline code hands us the context.
228 * It is unsafe to keep track of it ourselves, in the event that a
229 * program jumps out of a signal handler.
230 */
231 sfp = (struct linux_sigframe *)tf->tf_usr_sp;
232 if (copyin((caddr_t)sfp, &frame, sizeof(*sfp)) != 0)
233 return (EFAULT);
234
235 /*
236 * Make sure the processor mode has not been tampered with and
237 * interrupts have not been disabled.
238 */
239 #ifdef __PROG32
240 if ((frame.sf_sc.sc_cpsr & PSR_MODE) != PSR_USR32_MODE ||
241 (frame.sf_sc.sc_cpsr & (I32_bit | F32_bit)) != 0)
242 return (EINVAL);
243 #else /* __PROG26 */
244 if ((frame.sf_sc.sc_pc & R15_MODE) != R15_MODE_USR ||
245 (frame.sf_sc.sc_pc & (R15_IRQ_DISABLE | R15_FIQ_DISABLE)) != 0)
246 return EINVAL;
247 #endif
248
249 /* Restore register context. */
250 tf = process_frame(p);
251 tf->tf_r0 = frame.sf_sc.sc_r0;
252 tf->tf_r1 = frame.sf_sc.sc_r1;
253 tf->tf_r2 = frame.sf_sc.sc_r2;
254 tf->tf_r3 = frame.sf_sc.sc_r3;
255 tf->tf_r4 = frame.sf_sc.sc_r4;
256 tf->tf_r5 = frame.sf_sc.sc_r5;
257 tf->tf_r6 = frame.sf_sc.sc_r6;
258 tf->tf_r7 = frame.sf_sc.sc_r7;
259 tf->tf_r8 = frame.sf_sc.sc_r8;
260 tf->tf_r9 = frame.sf_sc.sc_r9;
261 tf->tf_r10 = frame.sf_sc.sc_r10;
262 tf->tf_r11 = frame.sf_sc.sc_r11;
263 tf->tf_r12 = frame.sf_sc.sc_r12;
264 tf->tf_usr_sp = frame.sf_sc.sc_sp;
265 tf->tf_usr_lr = frame.sf_sc.sc_lr;
266 tf->tf_pc = frame.sf_sc.sc_pc;
267 tf->tf_spsr = frame.sf_sc.sc_cpsr;
268
269 /* Restore signal stack. */
270 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
271
272 /* Restore signal mask. */
273 linux_old_extra_to_native_sigset(&mask, &frame.sf_sc.sc_mask,
274 frame.sf_extramask);
275 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
276
277 return (EJUSTRETURN);
278 }
279
280 /*
281 * major device numbers remapping
282 */
283 dev_t
284 linux_fakedev(dev, raw)
285 dev_t dev;
286 int raw;
287 {
288 /* XXX write me */
289 return dev;
290 }
291
292 /*
293 * We come here in a last attempt to satisfy a Linux ioctl() call
294 */
295 int
296 linux_machdepioctl(p, v, retval)
297 struct proc *p;
298 void *v;
299 register_t *retval;
300 {
301 struct linux_sys_ioctl_args /* {
302 syscallarg(int) fd;
303 syscallarg(u_long) com;
304 syscallarg(caddr_t) data;
305 } */ *uap = v;
306 struct sys_ioctl_args bia;
307 u_long com;
308
309 SCARG(&bia, fd) = SCARG(uap, fd);
310 SCARG(&bia, data) = SCARG(uap, data);
311 com = SCARG(uap, com);
312
313 switch (com) {
314 default:
315 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
316 return EINVAL;
317 }
318 SCARG(&bia, com) = com;
319 return sys_ioctl(p, &bia, retval);
320 }
321