linux_machdep.c revision 1.4 1 /* $NetBSD: linux_machdep.c,v 1.4 2002/02/15 16:47:59 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.4 2002/02/15 16:47:59 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/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(mask, &frame.sf_sc.sc_mask,
155 frame.sf_extramask);
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 * 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
211 int
212 linux_sys_sigreturn(p, v, retval)
213 struct proc *p;
214 void *v;
215 register_t *retval;
216 {
217 struct linux_sigframe *sfp, frame;
218 struct trapframe *tf;
219 sigset_t mask;
220
221 tf = process_frame(p);
222
223 /*
224 * The trampoline code hands us the context.
225 * It is unsafe to keep track of it ourselves, in the event that a
226 * program jumps out of a signal handler.
227 */
228 sfp = (struct linux_sigframe *)tf->tf_usr_sp;
229 if (copyin((caddr_t)sfp, &frame, sizeof(*sfp)) != 0)
230 return (EFAULT);
231
232 /*
233 * Make sure the processor mode has not been tampered with and
234 * interrupts have not been disabled.
235 */
236 #ifdef __PROG32
237 if ((frame.sf_sc.sc_cpsr & PSR_MODE) != PSR_USR32_MODE ||
238 (frame.sf_sc.sc_cpsr & (I32_bit | F32_bit)) != 0)
239 return (EINVAL);
240 #else /* __PROG26 */
241 if ((frame.sf_sc.sc_pc & R15_MODE) != R15_MODE_USR ||
242 (frame.sf_sc.sc_pc & (R15_IRQ_DISABLE | R15_FIQ_DISABLE)) != 0)
243 return EINVAL;
244 #endif
245
246 /* Restore register context. */
247 tf = process_frame(p);
248 tf->tf_r0 = frame.sf_sc.sc_r0;
249 tf->tf_r1 = frame.sf_sc.sc_r1;
250 tf->tf_r2 = frame.sf_sc.sc_r2;
251 tf->tf_r3 = frame.sf_sc.sc_r3;
252 tf->tf_r4 = frame.sf_sc.sc_r4;
253 tf->tf_r5 = frame.sf_sc.sc_r5;
254 tf->tf_r6 = frame.sf_sc.sc_r6;
255 tf->tf_r7 = frame.sf_sc.sc_r7;
256 tf->tf_r8 = frame.sf_sc.sc_r8;
257 tf->tf_r9 = frame.sf_sc.sc_r9;
258 tf->tf_r10 = frame.sf_sc.sc_r10;
259 tf->tf_r11 = frame.sf_sc.sc_r11;
260 tf->tf_r12 = frame.sf_sc.sc_r12;
261 tf->tf_usr_sp = frame.sf_sc.sc_sp;
262 tf->tf_usr_lr = frame.sf_sc.sc_lr;
263 tf->tf_pc = frame.sf_sc.sc_pc;
264 tf->tf_spsr = frame.sf_sc.sc_cpsr;
265
266 /* Restore signal stack. */
267 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
268
269 /* Restore signal mask. */
270 linux_old_extra_to_native_sigset(&frame.sf_sc.sc_mask,
271 frame.sf_extramask, &mask);
272 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
273
274 return (EJUSTRETURN);
275 }
276
277 /*
278 * major device numbers remapping
279 */
280 dev_t
281 linux_fakedev(dev, raw)
282 dev_t dev;
283 int raw;
284 {
285 /* XXX write me */
286 return dev;
287 }
288
289 /*
290 * We come here in a last attempt to satisfy a Linux ioctl() call
291 */
292 int
293 linux_machdepioctl(p, v, retval)
294 struct proc *p;
295 void *v;
296 register_t *retval;
297 {
298 struct linux_sys_ioctl_args /* {
299 syscallarg(int) fd;
300 syscallarg(u_long) com;
301 syscallarg(caddr_t) data;
302 } */ *uap = v;
303 struct sys_ioctl_args bia;
304 u_long com;
305
306 SCARG(&bia, fd) = SCARG(uap, fd);
307 SCARG(&bia, data) = SCARG(uap, data);
308 com = SCARG(uap, com);
309
310 switch (com) {
311 default:
312 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
313 return EINVAL;
314 }
315 SCARG(&bia, com) = com;
316 return sys_ioctl(p, &bia, retval);
317 }
318