linux_machdep.c revision 1.40 1 1.40 matt /* $NetBSD: linux_machdep.c,v 1.40 2009/12/14 00:47:11 matt Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Frank van der Linden and Emmanuel Dreyfus.
9 1.1 manu *
10 1.1 manu * Redistribution and use in source and binary forms, with or without
11 1.1 manu * modification, are permitted provided that the following conditions
12 1.1 manu * are met:
13 1.1 manu * 1. Redistributions of source code must retain the above copyright
14 1.1 manu * notice, this list of conditions and the following disclaimer.
15 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer in the
17 1.1 manu * documentation and/or other materials provided with the distribution.
18 1.1 manu *
19 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
30 1.1 manu */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.40 matt __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.40 2009/12/14 00:47:11 matt Exp $");
34 1.1 manu
35 1.1 manu #include <sys/param.h>
36 1.1 manu #include <sys/systm.h>
37 1.1 manu #include <sys/signalvar.h>
38 1.1 manu #include <sys/kernel.h>
39 1.1 manu #include <sys/proc.h>
40 1.1 manu #include <sys/buf.h>
41 1.1 manu #include <sys/reboot.h>
42 1.1 manu #include <sys/conf.h>
43 1.1 manu #include <sys/exec.h>
44 1.1 manu #include <sys/file.h>
45 1.1 manu #include <sys/callout.h>
46 1.1 manu #include <sys/malloc.h>
47 1.1 manu #include <sys/mbuf.h>
48 1.1 manu #include <sys/msgbuf.h>
49 1.1 manu #include <sys/mount.h>
50 1.1 manu #include <sys/vnode.h>
51 1.1 manu #include <sys/device.h>
52 1.1 manu #include <sys/syscallargs.h>
53 1.1 manu #include <sys/filedesc.h>
54 1.1 manu #include <sys/exec_elf.h>
55 1.1 manu #include <sys/disklabel.h>
56 1.1 manu #include <sys/ioctl.h>
57 1.2 manu #include <sys/sysctl.h>
58 1.26 yamt #include <sys/kauth.h>
59 1.1 manu #include <miscfs/specfs/specdev.h>
60 1.1 manu
61 1.1 manu #include <compat/linux/common/linux_types.h>
62 1.1 manu #include <compat/linux/common/linux_signal.h>
63 1.1 manu #include <compat/linux/common/linux_util.h>
64 1.1 manu #include <compat/linux/common/linux_ioctl.h>
65 1.1 manu #include <compat/linux/common/linux_hdio.h>
66 1.1 manu #include <compat/linux/common/linux_exec.h>
67 1.1 manu #include <compat/linux/common/linux_machdep.h>
68 1.1 manu
69 1.1 manu #include <compat/linux/linux_syscallargs.h>
70 1.1 manu
71 1.32 ad #include <sys/cpu.h>
72 1.1 manu #include <machine/psl.h>
73 1.1 manu #include <machine/reg.h>
74 1.1 manu #include <machine/regnum.h>
75 1.1 manu #include <machine/vmparam.h>
76 1.1 manu #include <machine/locore.h>
77 1.1 manu
78 1.7 manu #include <mips/cache.h>
79 1.7 manu
80 1.1 manu /*
81 1.1 manu * To see whether wscons is configured (for virtual console ioctl calls).
82 1.1 manu */
83 1.1 manu #if defined(_KERNEL_OPT)
84 1.1 manu #include "wsdisplay.h"
85 1.1 manu #endif
86 1.1 manu #if (NWSDISPLAY > 0)
87 1.1 manu #include <dev/wscons/wsconsio.h>
88 1.1 manu #include <dev/wscons/wsdisplay_usl_io.h>
89 1.1 manu #endif
90 1.1 manu
91 1.22 perry /*
92 1.1 manu * Set set up registers on exec.
93 1.1 manu * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
94 1.1 manu * entry uses NetBSD's native setregs instead of linux_setregs
95 1.1 manu */
96 1.1 manu void
97 1.34 dsl linux_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
98 1.22 perry {
99 1.17 thorpej setregs(l, pack, stack);
100 1.1 manu return;
101 1.1 manu }
102 1.1 manu
103 1.1 manu /*
104 1.1 manu * Send an interrupt to process.
105 1.1 manu *
106 1.1 manu * Adapted from sys/arch/mips/mips/mips_machdep.c
107 1.1 manu *
108 1.1 manu * XXX Does not work well yet with RT signals
109 1.1 manu *
110 1.1 manu */
111 1.1 manu
112 1.1 manu void
113 1.34 dsl linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
114 1.1 manu {
115 1.21 manu const int sig = ksi->ksi_signo;
116 1.17 thorpej struct lwp *l = curlwp;
117 1.17 thorpej struct proc *p = l->l_proc;
118 1.1 manu struct linux_sigframe *fp;
119 1.1 manu struct frame *f;
120 1.29 ad int i, onstack, error;
121 1.15 thorpej sig_t catcher = SIGACTION(p, sig).sa_handler;
122 1.1 manu struct linux_sigframe sf;
123 1.1 manu
124 1.4 manu #ifdef DEBUG_LINUX
125 1.1 manu printf("linux_sendsig()\n");
126 1.4 manu #endif /* DEBUG_LINUX */
127 1.17 thorpej f = (struct frame *)l->l_md.md_regs;
128 1.1 manu
129 1.22 perry /*
130 1.22 perry * Do we need to jump onto the signal stack?
131 1.1 manu */
132 1.1 manu onstack =
133 1.29 ad (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
134 1.1 manu (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
135 1.1 manu
136 1.1 manu /*
137 1.6 manu * Signal stack is broken (see at the end of linux_sigreturn), so we do
138 1.1 manu * not use it yet. XXX fix this.
139 1.1 manu */
140 1.1 manu onstack=0;
141 1.1 manu
142 1.22 perry /*
143 1.22 perry * Allocate space for the signal handler context.
144 1.1 manu */
145 1.1 manu if (onstack)
146 1.1 manu fp = (struct linux_sigframe *)
147 1.40 matt ((uintptr_t)l->l_sigstk.ss_sp
148 1.29 ad + l->l_sigstk.ss_size);
149 1.1 manu else
150 1.40 matt /* cast for O64 ABI case */
151 1.40 matt fp = (struct linux_sigframe *)(uintptr_t)f->f_regs[_R_SP];
152 1.1 manu
153 1.22 perry /*
154 1.22 perry * Build stack frame for signal trampoline.
155 1.4 manu */
156 1.1 manu memset(&sf, 0, sizeof sf);
157 1.4 manu
158 1.4 manu /*
159 1.4 manu * This is the signal trampoline used by Linux, we don't use it,
160 1.6 manu * but we set it up in case an application expects it to be there
161 1.4 manu */
162 1.1 manu sf.lsf_code[0] = 0x24020000; /* li v0, __NR_sigreturn */
163 1.4 manu sf.lsf_code[1] = 0x0000000c; /* syscall */
164 1.4 manu
165 1.11 rafal native_to_linux_sigset(&sf.lsf_mask, mask);
166 1.8 manu for (i=0; i<32; i++) {
167 1.1 manu sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
168 1.8 manu }
169 1.21 manu sf.lsf_sc.lsc_mdhi = f->f_regs[_R_MULHI];
170 1.21 manu sf.lsf_sc.lsc_mdlo = f->f_regs[_R_MULLO];
171 1.21 manu sf.lsf_sc.lsc_pc = f->f_regs[_R_PC];
172 1.21 manu sf.lsf_sc.lsc_status = f->f_regs[_R_SR];
173 1.21 manu sf.lsf_sc.lsc_cause = f->f_regs[_R_CAUSE];
174 1.21 manu sf.lsf_sc.lsc_badvaddr = f->f_regs[_R_BADVADDR];
175 1.29 ad sendsig_reset(l, sig);
176 1.1 manu
177 1.22 perry /*
178 1.1 manu * Save signal stack. XXX broken
179 1.1 manu */
180 1.29 ad /* kregs.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; */
181 1.1 manu
182 1.1 manu /*
183 1.1 manu * Install the sigframe onto the stack
184 1.1 manu */
185 1.1 manu fp -= sizeof(struct linux_sigframe);
186 1.37 ad mutex_exit(p->p_lock);
187 1.31 christos error = copyout(&sf, fp, sizeof(sf));
188 1.37 ad mutex_enter(p->p_lock);
189 1.29 ad
190 1.29 ad if (error != 0) {
191 1.1 manu /*
192 1.1 manu * Process has trashed its stack; give it an illegal
193 1.1 manu * instruction to halt it in its tracks.
194 1.1 manu */
195 1.4 manu #ifdef DEBUG_LINUX
196 1.4 manu printf("linux_sendsig: stack trashed\n");
197 1.4 manu #endif /* DEBUG_LINUX */
198 1.17 thorpej sigexit(l, SIGILL);
199 1.1 manu /* NOTREACHED */
200 1.1 manu }
201 1.1 manu
202 1.1 manu /* Set up the registers to return to sigcode. */
203 1.21 manu f->f_regs[_R_A0] = native_to_linux_signo[sig];
204 1.21 manu f->f_regs[_R_A1] = 0;
205 1.21 manu f->f_regs[_R_A2] = (unsigned long)&fp->lsf_sc;
206 1.1 manu
207 1.4 manu #ifdef DEBUG_LINUX
208 1.4 manu printf("sigcontext is at %p\n", &fp->lsf_sc);
209 1.4 manu #endif /* DEBUG_LINUX */
210 1.1 manu
211 1.21 manu f->f_regs[_R_SP] = (unsigned long)fp;
212 1.1 manu /* Signal trampoline code is at base of user stack. */
213 1.21 manu f->f_regs[_R_RA] = (unsigned long)p->p_sigctx.ps_sigcode;
214 1.21 manu f->f_regs[_R_T9] = (unsigned long)catcher;
215 1.21 manu f->f_regs[_R_PC] = (unsigned long)catcher;
216 1.1 manu
217 1.1 manu /* Remember that we're now on the signal stack. */
218 1.1 manu if (onstack)
219 1.29 ad l->l_sigstk.ss_flags |= SS_ONSTACK;
220 1.8 manu
221 1.1 manu return;
222 1.1 manu }
223 1.1 manu
224 1.1 manu /*
225 1.1 manu * System call to cleanup state after a signal
226 1.1 manu * has been taken. Reset signal mask and
227 1.1 manu * stack state from context left by sendsig (above).
228 1.1 manu */
229 1.1 manu int
230 1.35 dsl linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
231 1.1 manu {
232 1.35 dsl /* {
233 1.8 manu syscallarg(struct linux_sigframe *) sf;
234 1.35 dsl } */
235 1.17 thorpej struct proc *p = l->l_proc;
236 1.1 manu struct linux_sigframe *sf, ksf;
237 1.1 manu struct frame *f;
238 1.1 manu sigset_t mask;
239 1.1 manu int i, error;
240 1.1 manu
241 1.4 manu #ifdef DEBUG_LINUX
242 1.1 manu printf("linux_sys_sigreturn()\n");
243 1.4 manu #endif /* DEBUG_LINUX */
244 1.4 manu
245 1.1 manu /*
246 1.1 manu * The trampoline code hands us the context.
247 1.1 manu * It is unsafe to keep track of it ourselves, in the event that a
248 1.1 manu * program jumps out of a signal handler.
249 1.1 manu */
250 1.22 perry sf = SCARG(uap, sf);
251 1.1 manu
252 1.1 manu if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
253 1.1 manu return (error);
254 1.1 manu
255 1.1 manu /* Restore the register context. */
256 1.17 thorpej f = (struct frame *)l->l_md.md_regs;
257 1.1 manu for (i=0; i<32; i++)
258 1.8 manu f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
259 1.21 manu f->f_regs[_R_MULLO] = ksf.lsf_sc.lsc_mdlo;
260 1.21 manu f->f_regs[_R_MULHI] = ksf.lsf_sc.lsc_mdhi;
261 1.21 manu f->f_regs[_R_PC] = ksf.lsf_sc.lsc_pc;
262 1.21 manu f->f_regs[_R_BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
263 1.21 manu f->f_regs[_R_CAUSE] = ksf.lsf_sc.lsc_cause;
264 1.1 manu
265 1.37 ad mutex_enter(p->p_lock);
266 1.29 ad
267 1.1 manu /* Restore signal stack. */
268 1.29 ad l->l_sigstk.ss_flags &= ~SS_ONSTACK;
269 1.1 manu
270 1.1 manu /* Restore signal mask. */
271 1.11 rafal linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask);
272 1.29 ad (void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
273 1.29 ad
274 1.37 ad mutex_exit(p->p_lock);
275 1.1 manu
276 1.1 manu return (EJUSTRETURN);
277 1.1 manu }
278 1.1 manu
279 1.1 manu
280 1.1 manu int
281 1.35 dsl linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
282 1.1 manu {
283 1.17 thorpej return (ENOSYS);
284 1.1 manu }
285 1.1 manu
286 1.1 manu
287 1.1 manu #if 0
288 1.1 manu int
289 1.35 dsl linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *uap, register_t *retval)
290 1.1 manu {
291 1.22 perry /*
292 1.1 manu * This syscall is not implemented in Linux/Mips: we should not
293 1.1 manu * be here
294 1.1 manu */
295 1.1 manu #ifdef DEBUG_LINUX
296 1.22 perry printf("linux_sys_modify_ldt: should not be here.\n");
297 1.4 manu #endif /* DEBUG_LINUX */
298 1.1 manu return 0;
299 1.1 manu }
300 1.1 manu #endif
301 1.1 manu
302 1.22 perry /*
303 1.1 manu * major device numbers remapping
304 1.1 manu */
305 1.1 manu dev_t
306 1.34 dsl linux_fakedev(dev_t dev, int raw)
307 1.1 manu {
308 1.14 jdolecek /* XXX write me */
309 1.14 jdolecek return dev;
310 1.1 manu }
311 1.1 manu
312 1.1 manu /*
313 1.1 manu * We come here in a last attempt to satisfy a Linux ioctl() call
314 1.1 manu */
315 1.1 manu int
316 1.35 dsl linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
317 1.22 perry {
318 1.1 manu return 0;
319 1.1 manu }
320 1.1 manu
321 1.1 manu /*
322 1.1 manu * See above. If a root process tries to set access to an I/O port,
323 1.1 manu * just let it have the whole range.
324 1.1 manu */
325 1.1 manu int
326 1.35 dsl linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *uap, register_t *retval)
327 1.1 manu {
328 1.22 perry /*
329 1.1 manu * This syscall is not implemented in Linux/Mips: we should not be here
330 1.1 manu */
331 1.1 manu #ifdef DEBUG_LINUX
332 1.22 perry printf("linux_sys_ioperm: should not be here.\n");
333 1.4 manu #endif /* DEBUG_LINUX */
334 1.1 manu return 0;
335 1.1 manu }
336 1.1 manu
337 1.1 manu /*
338 1.22 perry * wrapper linux_sys_new_uname() -> linux_sys_uname()
339 1.1 manu */
340 1.22 perry int
341 1.35 dsl linux_sys_new_uname(struct lwp *l, const struct linux_sys_new_uname_args *uap, register_t *retval)
342 1.1 manu {
343 1.4 manu /*
344 1.22 perry * Use this if you want to try Linux emulation with a glibc-2.2
345 1.4 manu * or higher. Note that signals will not work
346 1.4 manu */
347 1.4 manu #if 0
348 1.4 manu struct linux_sys_uname_args /* {
349 1.4 manu syscallarg(struct linux_utsname *) up;
350 1.4 manu } */ *uap = v;
351 1.4 manu struct linux_utsname luts;
352 1.4 manu
353 1.33 njoly strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
354 1.33 njoly strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
355 1.33 njoly strlcpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
356 1.33 njoly strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
357 1.33 njoly strlcpy(luts.l_machine, machine, sizeof(luts.l_machine));
358 1.33 njoly strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
359 1.4 manu
360 1.4 manu return copyout(&luts, SCARG(uap, up), sizeof(luts));
361 1.4 manu #else
362 1.35 dsl return linux_sys_uname(l, (const void *)uap, retval);
363 1.4 manu #endif
364 1.1 manu }
365 1.1 manu
366 1.1 manu /*
367 1.17 thorpej * In Linux, cacheflush is currently implemented
368 1.1 manu * as a whole cache flush (arguments are ignored)
369 1.1 manu * we emulate this broken beahior.
370 1.1 manu */
371 1.1 manu int
372 1.35 dsl linux_sys_cacheflush(struct lwp *l, const struct linux_sys_cacheflush_args *uap, register_t *retval)
373 1.1 manu {
374 1.7 manu mips_icache_sync_all();
375 1.7 manu mips_dcache_wbinv_all();
376 1.1 manu return 0;
377 1.1 manu }
378 1.1 manu
379 1.2 manu /*
380 1.22 perry * This system call is depecated in Linux, but
381 1.2 manu * some binaries and some libraries use it.
382 1.2 manu */
383 1.2 manu int
384 1.35 dsl linux_sys_sysmips(struct lwp *l, const struct linux_sys_sysmips_args *uap, register_t *retval)
385 1.2 manu {
386 1.35 dsl #if 0
387 1.2 manu struct linux_sys_sysmips_args {
388 1.2 manu syscallarg(int) cmd;
389 1.2 manu syscallarg(int) arg1;
390 1.2 manu syscallarg(int) arg2;
391 1.2 manu syscallarg(int) arg3;
392 1.2 manu } *uap = v;
393 1.35 dsl #endif
394 1.2 manu int error;
395 1.22 perry
396 1.2 manu switch (SCARG(uap, cmd)) {
397 1.2 manu case LINUX_SETNAME: {
398 1.2 manu char nodename [LINUX___NEW_UTS_LEN + 1];
399 1.20 atatat int name[2];
400 1.2 manu size_t len;
401 1.2 manu
402 1.22 perry if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
403 1.2 manu LINUX___NEW_UTS_LEN, &len)) != 0)
404 1.2 manu return error;
405 1.2 manu
406 1.20 atatat name[0] = CTL_KERN;
407 1.20 atatat name[1] = KERN_HOSTNAME;
408 1.20 atatat return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL));
409 1.22 perry
410 1.2 manu break;
411 1.2 manu }
412 1.3 manu case LINUX_MIPS_ATOMIC_SET: {
413 1.3 manu void *addr;
414 1.3 manu int s;
415 1.18 jdolecek u_int8_t value = 0;
416 1.3 manu
417 1.3 manu addr = (void *)SCARG(uap, arg1);
418 1.3 manu
419 1.3 manu s = splhigh();
420 1.3 manu /*
421 1.3 manu * No error testing here. This is bad, but Linux does
422 1.3 manu * it like this. The source aknowledge "This is broken"
423 1.3 manu * in a comment...
424 1.3 manu */
425 1.18 jdolecek (void) copyin(addr, &value, 1);
426 1.18 jdolecek *retval = value;
427 1.18 jdolecek value = (u_int8_t) SCARG(uap, arg2);
428 1.18 jdolecek error = copyout(&value, addr, 1);
429 1.3 manu splx(s);
430 1.3 manu
431 1.3 manu return 0;
432 1.2 manu break;
433 1.3 manu }
434 1.2 manu case LINUX_MIPS_FIXADE: /* XXX not implemented */
435 1.2 manu break;
436 1.2 manu case LINUX_FLUSH_CACHE:
437 1.7 manu mips_icache_sync_all();
438 1.7 manu mips_dcache_wbinv_all();
439 1.2 manu break;
440 1.2 manu case LINUX_MIPS_RDNVRAM:
441 1.2 manu return EIO;
442 1.2 manu break;
443 1.2 manu default:
444 1.2 manu return EINVAL;
445 1.2 manu break;
446 1.2 manu }
447 1.3 manu #ifdef DEBUG_LINUX
448 1.22 perry printf("linux_sys_sysmips(): unimplemented command %d\n",
449 1.22 perry SCARG(uap,cmd));
450 1.2 manu #endif /* DEBUG_LINUX */
451 1.2 manu return 0;
452 1.2 manu }
453 1.23 fvdl
454 1.23 fvdl int
455 1.23 fvdl linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
456 1.23 fvdl {
457 1.23 fvdl return 0;
458 1.23 fvdl }
459