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