linux_machdep.c revision 1.5 1 1.5 bjh21 /* $NetBSD: linux_machdep.c,v 1.5 2002/02/17 22:00:09 bjh21 Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*-
4 1.1 bjh21 * Copyright (c) 1995, 2000 The NetBSD Foundation, Inc.
5 1.1 bjh21 * All rights reserved.
6 1.1 bjh21 *
7 1.1 bjh21 * This code is derived from software contributed to The NetBSD Foundation
8 1.1 bjh21 * by Frank van der Linden.
9 1.1 bjh21 *
10 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
11 1.1 bjh21 * modification, are permitted provided that the following conditions
12 1.1 bjh21 * are met:
13 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
14 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
15 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
17 1.1 bjh21 * documentation and/or other materials provided with the distribution.
18 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
19 1.1 bjh21 * must display the following acknowledgement:
20 1.1 bjh21 * This product includes software developed by the NetBSD
21 1.1 bjh21 * Foundation, Inc. and its contributors.
22 1.1 bjh21 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 bjh21 * contributors may be used to endorse or promote products derived
24 1.1 bjh21 * from this software without specific prior written permission.
25 1.1 bjh21 *
26 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 bjh21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 bjh21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 bjh21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 bjh21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 bjh21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 bjh21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 bjh21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 bjh21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 bjh21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 bjh21 * POSSIBILITY OF SUCH DAMAGE.
37 1.1 bjh21 */
38 1.1 bjh21
39 1.1 bjh21 #include <sys/param.h>
40 1.1 bjh21
41 1.5 bjh21 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.5 2002/02/17 22:00:09 bjh21 Exp $");
42 1.1 bjh21
43 1.1 bjh21 #include <sys/param.h>
44 1.1 bjh21 #include <sys/systm.h>
45 1.1 bjh21 #include <sys/signalvar.h>
46 1.1 bjh21 #include <sys/kernel.h>
47 1.1 bjh21 #include <sys/map.h>
48 1.1 bjh21 #include <sys/proc.h>
49 1.1 bjh21 #include <sys/user.h>
50 1.1 bjh21 #include <sys/buf.h>
51 1.1 bjh21 #include <sys/reboot.h>
52 1.1 bjh21 #include <sys/conf.h>
53 1.1 bjh21 #include <sys/exec.h>
54 1.1 bjh21 #include <sys/file.h>
55 1.1 bjh21 #include <sys/callout.h>
56 1.1 bjh21 #include <sys/malloc.h>
57 1.1 bjh21 #include <sys/mbuf.h>
58 1.1 bjh21 #include <sys/msgbuf.h>
59 1.1 bjh21 #include <sys/mount.h>
60 1.1 bjh21 #include <sys/vnode.h>
61 1.1 bjh21 #include <sys/device.h>
62 1.1 bjh21 #include <sys/syscallargs.h>
63 1.1 bjh21 #include <sys/filedesc.h>
64 1.1 bjh21 #include <sys/exec_elf.h>
65 1.1 bjh21 #include <sys/disklabel.h>
66 1.1 bjh21 #include <sys/ioctl.h>
67 1.1 bjh21 #include <miscfs/specfs/specdev.h>
68 1.1 bjh21
69 1.1 bjh21 #include <compat/linux/common/linux_types.h>
70 1.1 bjh21 #include <compat/linux/common/linux_signal.h>
71 1.1 bjh21 #include <compat/linux/common/linux_util.h>
72 1.1 bjh21 #include <compat/linux/common/linux_ioctl.h>
73 1.1 bjh21 #include <compat/linux/common/linux_hdio.h>
74 1.1 bjh21 #include <compat/linux/common/linux_exec.h>
75 1.1 bjh21 #include <compat/linux/common/linux_machdep.h>
76 1.1 bjh21
77 1.1 bjh21 #include <compat/linux/linux_syscallargs.h>
78 1.1 bjh21
79 1.1 bjh21 void
80 1.1 bjh21 linux_setregs(p, epp, stack)
81 1.1 bjh21 struct proc *p;
82 1.1 bjh21 struct exec_package *epp;
83 1.1 bjh21 u_long stack;
84 1.1 bjh21 {
85 1.1 bjh21 /* struct pcb *pcb = &p->p_addr->u_pcb; */
86 1.1 bjh21
87 1.1 bjh21 setregs(p, epp, stack);
88 1.1 bjh21 }
89 1.1 bjh21
90 1.2 bjh21 static __inline struct trapframe *
91 1.2 bjh21 process_frame(struct proc *p)
92 1.2 bjh21 {
93 1.2 bjh21
94 1.2 bjh21 return p->p_addr->u_pcb.pcb_tf;
95 1.2 bjh21 }
96 1.2 bjh21
97 1.1 bjh21 void
98 1.1 bjh21 linux_sendsig(catcher, sig, mask, code)
99 1.1 bjh21 sig_t catcher;
100 1.1 bjh21 int sig;
101 1.1 bjh21 sigset_t *mask;
102 1.1 bjh21 u_long code;
103 1.1 bjh21 {
104 1.2 bjh21 struct proc *p = curproc;
105 1.2 bjh21 struct trapframe *tf;
106 1.2 bjh21 struct linux_sigframe *fp, frame;
107 1.2 bjh21 int onstack;
108 1.2 bjh21
109 1.2 bjh21 tf = process_frame(p);
110 1.2 bjh21
111 1.2 bjh21 /*
112 1.2 bjh21 * The Linux version of this code is in
113 1.2 bjh21 * linux/arch/arm/kernel/signal.c.
114 1.2 bjh21 */
115 1.2 bjh21
116 1.2 bjh21 /* Do we need to jump onto the signal stack? */
117 1.2 bjh21 onstack =
118 1.2 bjh21 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
119 1.2 bjh21 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
120 1.2 bjh21
121 1.2 bjh21 /* Allocate space for the signal handler context. */
122 1.2 bjh21 if (onstack)
123 1.2 bjh21 fp = (struct linux_sigframe *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
124 1.2 bjh21 p->p_sigctx.ps_sigstk.ss_size);
125 1.2 bjh21 else
126 1.2 bjh21 fp = (struct linux_sigframe *)tf->tf_usr_sp;
127 1.2 bjh21 fp--;
128 1.2 bjh21
129 1.2 bjh21 /* Build stack frame for signal trampoline. */
130 1.2 bjh21
131 1.2 bjh21 /* Save register context. */
132 1.2 bjh21 frame.sf_sc.sc_r0 = tf->tf_r0;
133 1.2 bjh21 frame.sf_sc.sc_r1 = tf->tf_r1;
134 1.2 bjh21 frame.sf_sc.sc_r2 = tf->tf_r2;
135 1.2 bjh21 frame.sf_sc.sc_r3 = tf->tf_r3;
136 1.2 bjh21 frame.sf_sc.sc_r4 = tf->tf_r4;
137 1.2 bjh21 frame.sf_sc.sc_r5 = tf->tf_r5;
138 1.2 bjh21 frame.sf_sc.sc_r6 = tf->tf_r6;
139 1.2 bjh21 frame.sf_sc.sc_r7 = tf->tf_r7;
140 1.2 bjh21 frame.sf_sc.sc_r8 = tf->tf_r8;
141 1.2 bjh21 frame.sf_sc.sc_r9 = tf->tf_r9;
142 1.2 bjh21 frame.sf_sc.sc_r10 = tf->tf_r10;
143 1.2 bjh21 frame.sf_sc.sc_r11 = tf->tf_r11;
144 1.2 bjh21 frame.sf_sc.sc_r12 = tf->tf_r12;
145 1.2 bjh21 frame.sf_sc.sc_sp = tf->tf_usr_sp;
146 1.2 bjh21 frame.sf_sc.sc_lr = tf->tf_usr_lr;
147 1.2 bjh21 frame.sf_sc.sc_pc = tf->tf_pc;
148 1.2 bjh21 frame.sf_sc.sc_cpsr = tf->tf_spsr;
149 1.2 bjh21
150 1.2 bjh21 /* Save signal stack. */
151 1.2 bjh21 /* Linux doesn't save the onstack flag in sigframe */
152 1.2 bjh21
153 1.2 bjh21 /* Save signal mask. */
154 1.2 bjh21 native_to_linux_old_extra_sigset(mask, &frame.sf_sc.sc_mask,
155 1.2 bjh21 frame.sf_extramask);
156 1.2 bjh21
157 1.2 bjh21 /* Other state (mostly faked) */
158 1.2 bjh21 /*
159 1.2 bjh21 * trapno should indicate the trap that caused the signal:
160 1.2 bjh21 * 6 -> undefined instruction
161 1.2 bjh21 * 11 -> address exception
162 1.2 bjh21 * 14 -> data/prefetch abort
163 1.2 bjh21 */
164 1.2 bjh21 frame.sf_sc.sc_trapno = 0;
165 1.2 bjh21 frame.sf_sc.sc_error_code = 0;
166 1.2 bjh21 frame.sf_sc.sc_fault_address = code;
167 1.2 bjh21
168 1.2 bjh21 if (copyout(&frame, fp, sizeof(frame)) != 0) {
169 1.2 bjh21 /*
170 1.2 bjh21 * Process has trashed its stack; give it an illegal
171 1.2 bjh21 * instruction to halt it in its tracks.
172 1.2 bjh21 */
173 1.2 bjh21 sigexit(p, SIGILL);
174 1.2 bjh21 /* NOTREACHED */
175 1.2 bjh21 }
176 1.2 bjh21 /*
177 1.2 bjh21 * Build context to run handler in.
178 1.2 bjh21 */
179 1.2 bjh21 tf->tf_r0 = native_to_linux_sig[sig];
180 1.2 bjh21 tf->tf_r1 = 0; /* XXX Should be a siginfo_t */
181 1.2 bjh21 tf->tf_r2 = 0;
182 1.2 bjh21 tf->tf_r3 = (register_t)catcher;
183 1.2 bjh21 tf->tf_usr_sp = (register_t)fp;
184 1.2 bjh21 tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode;
185 1.2 bjh21
186 1.2 bjh21 /* Remember that we're now on the signal stack. */
187 1.2 bjh21 if (onstack)
188 1.2 bjh21 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
189 1.1 bjh21
190 1.1 bjh21 }
191 1.5 bjh21
192 1.5 bjh21 #if 0
193 1.1 bjh21 /*
194 1.1 bjh21 * System call to cleanup state after a signal
195 1.1 bjh21 * has been taken. Reset signal mask and
196 1.1 bjh21 * stack state from context left by sendsig (above).
197 1.1 bjh21 * Return to previous pc and psl as specified by
198 1.1 bjh21 * context left by sendsig. Check carefully to
199 1.1 bjh21 * make sure that the user has not modified the
200 1.1 bjh21 * psl to gain improper privileges or to cause
201 1.1 bjh21 * a machine fault.
202 1.1 bjh21 */
203 1.1 bjh21 int
204 1.1 bjh21 linux_sys_rt_sigreturn(p, v, retval)
205 1.1 bjh21 struct proc *p;
206 1.1 bjh21 void *v;
207 1.1 bjh21 register_t *retval;
208 1.1 bjh21 {
209 1.1 bjh21 /* XXX XAX write me */
210 1.1 bjh21 return(ENOSYS);
211 1.1 bjh21 }
212 1.5 bjh21 #endif
213 1.1 bjh21
214 1.1 bjh21 int
215 1.1 bjh21 linux_sys_sigreturn(p, v, retval)
216 1.1 bjh21 struct proc *p;
217 1.1 bjh21 void *v;
218 1.1 bjh21 register_t *retval;
219 1.1 bjh21 {
220 1.2 bjh21 struct linux_sigframe *sfp, frame;
221 1.2 bjh21 struct trapframe *tf;
222 1.2 bjh21 sigset_t mask;
223 1.2 bjh21
224 1.2 bjh21 tf = process_frame(p);
225 1.2 bjh21
226 1.2 bjh21 /*
227 1.2 bjh21 * The trampoline code hands us the context.
228 1.2 bjh21 * It is unsafe to keep track of it ourselves, in the event that a
229 1.2 bjh21 * program jumps out of a signal handler.
230 1.2 bjh21 */
231 1.2 bjh21 sfp = (struct linux_sigframe *)tf->tf_usr_sp;
232 1.2 bjh21 if (copyin((caddr_t)sfp, &frame, sizeof(*sfp)) != 0)
233 1.2 bjh21 return (EFAULT);
234 1.2 bjh21
235 1.2 bjh21 /*
236 1.2 bjh21 * Make sure the processor mode has not been tampered with and
237 1.2 bjh21 * interrupts have not been disabled.
238 1.2 bjh21 */
239 1.2 bjh21 #ifdef __PROG32
240 1.2 bjh21 if ((frame.sf_sc.sc_cpsr & PSR_MODE) != PSR_USR32_MODE ||
241 1.2 bjh21 (frame.sf_sc.sc_cpsr & (I32_bit | F32_bit)) != 0)
242 1.2 bjh21 return (EINVAL);
243 1.2 bjh21 #else /* __PROG26 */
244 1.2 bjh21 if ((frame.sf_sc.sc_pc & R15_MODE) != R15_MODE_USR ||
245 1.2 bjh21 (frame.sf_sc.sc_pc & (R15_IRQ_DISABLE | R15_FIQ_DISABLE)) != 0)
246 1.2 bjh21 return EINVAL;
247 1.2 bjh21 #endif
248 1.2 bjh21
249 1.2 bjh21 /* Restore register context. */
250 1.2 bjh21 tf = process_frame(p);
251 1.2 bjh21 tf->tf_r0 = frame.sf_sc.sc_r0;
252 1.2 bjh21 tf->tf_r1 = frame.sf_sc.sc_r1;
253 1.2 bjh21 tf->tf_r2 = frame.sf_sc.sc_r2;
254 1.2 bjh21 tf->tf_r3 = frame.sf_sc.sc_r3;
255 1.2 bjh21 tf->tf_r4 = frame.sf_sc.sc_r4;
256 1.2 bjh21 tf->tf_r5 = frame.sf_sc.sc_r5;
257 1.2 bjh21 tf->tf_r6 = frame.sf_sc.sc_r6;
258 1.2 bjh21 tf->tf_r7 = frame.sf_sc.sc_r7;
259 1.2 bjh21 tf->tf_r8 = frame.sf_sc.sc_r8;
260 1.2 bjh21 tf->tf_r9 = frame.sf_sc.sc_r9;
261 1.2 bjh21 tf->tf_r10 = frame.sf_sc.sc_r10;
262 1.2 bjh21 tf->tf_r11 = frame.sf_sc.sc_r11;
263 1.2 bjh21 tf->tf_r12 = frame.sf_sc.sc_r12;
264 1.2 bjh21 tf->tf_usr_sp = frame.sf_sc.sc_sp;
265 1.2 bjh21 tf->tf_usr_lr = frame.sf_sc.sc_lr;
266 1.2 bjh21 tf->tf_pc = frame.sf_sc.sc_pc;
267 1.2 bjh21 tf->tf_spsr = frame.sf_sc.sc_cpsr;
268 1.2 bjh21
269 1.2 bjh21 /* Restore signal stack. */
270 1.2 bjh21 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
271 1.2 bjh21
272 1.2 bjh21 /* Restore signal mask. */
273 1.2 bjh21 linux_old_extra_to_native_sigset(&frame.sf_sc.sc_mask,
274 1.2 bjh21 frame.sf_extramask, &mask);
275 1.2 bjh21 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
276 1.1 bjh21
277 1.2 bjh21 return (EJUSTRETURN);
278 1.1 bjh21 }
279 1.1 bjh21
280 1.1 bjh21 /*
281 1.1 bjh21 * major device numbers remapping
282 1.1 bjh21 */
283 1.1 bjh21 dev_t
284 1.4 christos linux_fakedev(dev, raw)
285 1.1 bjh21 dev_t dev;
286 1.4 christos int raw;
287 1.1 bjh21 {
288 1.1 bjh21 /* XXX write me */
289 1.1 bjh21 return dev;
290 1.1 bjh21 }
291 1.1 bjh21
292 1.1 bjh21 /*
293 1.1 bjh21 * We come here in a last attempt to satisfy a Linux ioctl() call
294 1.1 bjh21 */
295 1.1 bjh21 int
296 1.1 bjh21 linux_machdepioctl(p, v, retval)
297 1.1 bjh21 struct proc *p;
298 1.1 bjh21 void *v;
299 1.1 bjh21 register_t *retval;
300 1.1 bjh21 {
301 1.1 bjh21 struct linux_sys_ioctl_args /* {
302 1.1 bjh21 syscallarg(int) fd;
303 1.1 bjh21 syscallarg(u_long) com;
304 1.1 bjh21 syscallarg(caddr_t) data;
305 1.1 bjh21 } */ *uap = v;
306 1.1 bjh21 struct sys_ioctl_args bia;
307 1.1 bjh21 u_long com;
308 1.1 bjh21
309 1.1 bjh21 SCARG(&bia, fd) = SCARG(uap, fd);
310 1.1 bjh21 SCARG(&bia, data) = SCARG(uap, data);
311 1.1 bjh21 com = SCARG(uap, com);
312 1.1 bjh21
313 1.1 bjh21 switch (com) {
314 1.1 bjh21 default:
315 1.1 bjh21 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
316 1.1 bjh21 return EINVAL;
317 1.1 bjh21 }
318 1.1 bjh21 SCARG(&bia, com) = com;
319 1.1 bjh21 return sys_ioctl(p, &bia, retval);
320 1.1 bjh21 }
321