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