1 /* $NetBSD: linux_machdep.c,v 1.39 2009/11/23 00:46:07 rmind Exp $ */
4 * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.39 2009/11/23 00:46:07 rmind Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/signalvar.h>
38 #include <sys/kernel.h>
41 #include <sys/reboot.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/syscallargs.h>
53 #include <sys/filedesc.h>
54 #include <sys/exec_elf.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <sys/sysctl.h>
58 #include <sys/kauth.h>
59 #include <miscfs/specfs/specdev.h>
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>
69 #include <compat/linux/linux_syscallargs.h>
72 #include <machine/psl.h>
73 #include <machine/reg.h>
74 #include <machine/regnum.h>
75 #include <machine/vmparam.h>
76 #include <machine/locore.h>
78 #include <mips/cache.h>
81 * To see whether wscons is configured (for virtual console ioctl calls).
83 #if defined(_KERNEL_OPT)
84 #include "wsdisplay.h"
87 #include <dev/wscons/wsconsio.h>
88 #include <dev/wscons/wsdisplay_usl_io.h>
92 * Set set up registers on exec.
93 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
94 * entry uses NetBSD's native setregs instead of linux_setregs
97 linux_setregs(struct lwp
*l
, struct exec_package
*pack
, u_long stack
)
99 setregs(l
, pack
, stack
);
104 * Send an interrupt to process.
106 * Adapted from sys/arch/mips/mips/mips_machdep.c
108 * XXX Does not work well yet with RT signals
113 linux_sendsig(const ksiginfo_t
*ksi
, const sigset_t
*mask
)
115 const int sig
= ksi
->ksi_signo
;
116 struct lwp
*l
= curlwp
;
117 struct proc
*p
= l
->l_proc
;
118 struct linux_sigframe
*fp
;
120 int i
, onstack
, error
;
121 sig_t catcher
= SIGACTION(p
, sig
).sa_handler
;
122 struct linux_sigframe sf
;
125 printf("linux_sendsig()\n");
126 #endif /* DEBUG_LINUX */
127 f
= (struct frame
*)l
->l_md
.md_regs
;
130 * Do we need to jump onto the signal stack?
133 (l
->l_sigstk
.ss_flags
& (SS_DISABLE
| SS_ONSTACK
)) == 0 &&
134 (SIGACTION(p
, sig
).sa_flags
& SA_ONSTACK
) != 0;
137 * Signal stack is broken (see at the end of linux_sigreturn), so we do
138 * not use it yet. XXX fix this.
143 * Allocate space for the signal handler context.
146 fp
= (struct linux_sigframe
*)
147 ((uintptr_t)l
->l_sigstk
.ss_sp
148 + l
->l_sigstk
.ss_size
);
150 /* cast for O64 ABI case */
151 fp
= (struct linux_sigframe
*)(uintptr_t)f
->f_regs
[_R_SP
];
154 * Build stack frame for signal trampoline.
156 memset(&sf
, 0, sizeof sf
);
159 * This is the signal trampoline used by Linux, we don't use it,
160 * but we set it up in case an application expects it to be there
162 sf
.lsf_code
[0] = 0x24020000; /* li v0, __NR_sigreturn */
163 sf
.lsf_code
[1] = 0x0000000c; /* syscall */
165 native_to_linux_sigset(&sf
.lsf_mask
, mask
);
166 for (i
=0; i
<32; i
++) {
167 sf
.lsf_sc
.lsc_regs
[i
] = f
->f_regs
[i
];
169 sf
.lsf_sc
.lsc_mdhi
= f
->f_regs
[_R_MULHI
];
170 sf
.lsf_sc
.lsc_mdlo
= f
->f_regs
[_R_MULLO
];
171 sf
.lsf_sc
.lsc_pc
= f
->f_regs
[_R_PC
];
172 sf
.lsf_sc
.lsc_status
= f
->f_regs
[_R_SR
];
173 sf
.lsf_sc
.lsc_cause
= f
->f_regs
[_R_CAUSE
];
174 sf
.lsf_sc
.lsc_badvaddr
= f
->f_regs
[_R_BADVADDR
];
175 sendsig_reset(l
, sig
);
178 * Save signal stack. XXX broken
180 /* kregs.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; */
183 * Install the sigframe onto the stack
185 fp
-= sizeof(struct linux_sigframe
);
186 mutex_exit(p
->p_lock
);
187 error
= copyout(&sf
, fp
, sizeof(sf
));
188 mutex_enter(p
->p_lock
);
192 * Process has trashed its stack; give it an illegal
193 * instruction to halt it in its tracks.
196 printf("linux_sendsig: stack trashed\n");
197 #endif /* DEBUG_LINUX */
202 /* Set up the registers to return to sigcode. */
203 f
->f_regs
[_R_A0
] = native_to_linux_signo
[sig
];
204 f
->f_regs
[_R_A1
] = 0;
205 f
->f_regs
[_R_A2
] = (unsigned long)&fp
->lsf_sc
;
208 printf("sigcontext is at %p\n", &fp
->lsf_sc
);
209 #endif /* DEBUG_LINUX */
211 f
->f_regs
[_R_SP
] = (unsigned long)fp
;
212 /* Signal trampoline code is at base of user stack. */
213 f
->f_regs
[_R_RA
] = (unsigned long)p
->p_sigctx
.ps_sigcode
;
214 f
->f_regs
[_R_T9
] = (unsigned long)catcher
;
215 f
->f_regs
[_R_PC
] = (unsigned long)catcher
;
217 /* Remember that we're now on the signal stack. */
219 l
->l_sigstk
.ss_flags
|= SS_ONSTACK
;
225 * System call to cleanup state after a signal
226 * has been taken. Reset signal mask and
227 * stack state from context left by sendsig (above).
230 linux_sys_sigreturn(struct lwp
*l
, const struct linux_sys_sigreturn_args
*uap
, register_t
*retval
)
233 syscallarg(struct linux_sigframe *) sf;
235 struct proc
*p
= l
->l_proc
;
236 struct linux_sigframe
*sf
, ksf
;
242 printf("linux_sys_sigreturn()\n");
243 #endif /* DEBUG_LINUX */
246 * The trampoline code hands us the context.
247 * It is unsafe to keep track of it ourselves, in the event that a
248 * program jumps out of a signal handler.
252 if ((error
= copyin(sf
, &ksf
, sizeof(ksf
))) != 0)
255 /* Restore the register context. */
256 f
= (struct frame
*)l
->l_md
.md_regs
;
258 f
->f_regs
[i
] = ksf
.lsf_sc
.lsc_regs
[i
];
259 f
->f_regs
[_R_MULLO
] = ksf
.lsf_sc
.lsc_mdlo
;
260 f
->f_regs
[_R_MULHI
] = ksf
.lsf_sc
.lsc_mdhi
;
261 f
->f_regs
[_R_PC
] = ksf
.lsf_sc
.lsc_pc
;
262 f
->f_regs
[_R_BADVADDR
] = ksf
.lsf_sc
.lsc_badvaddr
;
263 f
->f_regs
[_R_CAUSE
] = ksf
.lsf_sc
.lsc_cause
;
265 mutex_enter(p
->p_lock
);
267 /* Restore signal stack. */
268 l
->l_sigstk
.ss_flags
&= ~SS_ONSTACK
;
270 /* Restore signal mask. */
271 linux_to_native_sigset(&mask
, (linux_sigset_t
*)&ksf
.lsf_mask
);
272 (void)sigprocmask1(l
, SIG_SETMASK
, &mask
, 0);
274 mutex_exit(p
->p_lock
);
276 return (EJUSTRETURN
);
281 linux_sys_rt_sigreturn(struct lwp
*l
, const void *v
, register_t
*retval
)
289 linux_sys_modify_ldt(struct lwp
*l
, const struct linux_sys_modify_ldt_args
*uap
, register_t
*retval
)
292 * This syscall is not implemented in Linux/Mips: we should not
296 printf("linux_sys_modify_ldt: should not be here.\n");
297 #endif /* DEBUG_LINUX */
303 * major device numbers remapping
306 linux_fakedev(dev_t dev
, int raw
)
313 * We come here in a last attempt to satisfy a Linux ioctl() call
316 linux_machdepioctl(struct lwp
*l
, const struct linux_sys_ioctl_args
*uap
, register_t
*retval
)
322 * See above. If a root process tries to set access to an I/O port,
323 * just let it have the whole range.
326 linux_sys_ioperm(struct lwp
*l
, const struct linux_sys_ioperm_args
*uap
, register_t
*retval
)
329 * This syscall is not implemented in Linux/Mips: we should not be here
332 printf("linux_sys_ioperm: should not be here.\n");
333 #endif /* DEBUG_LINUX */
338 * wrapper linux_sys_new_uname() -> linux_sys_uname()
341 linux_sys_new_uname(struct lwp
*l
, const struct linux_sys_new_uname_args
*uap
, register_t
*retval
)
344 * Use this if you want to try Linux emulation with a glibc-2.2
345 * or higher. Note that signals will not work
348 struct linux_sys_uname_args
/* {
349 syscallarg(struct linux_utsname *) up;
351 struct linux_utsname luts
;
353 strlcpy(luts
.l_sysname
, linux_sysname
, sizeof(luts
.l_sysname
));
354 strlcpy(luts
.l_nodename
, hostname
, sizeof(luts
.l_nodename
));
355 strlcpy(luts
.l_release
, "2.4.0", sizeof(luts
.l_release
));
356 strlcpy(luts
.l_version
, linux_version
, sizeof(luts
.l_version
));
357 strlcpy(luts
.l_machine
, machine
, sizeof(luts
.l_machine
));
358 strlcpy(luts
.l_domainname
, domainname
, sizeof(luts
.l_domainname
));
360 return copyout(&luts
, SCARG(uap
, up
), sizeof(luts
));
362 return linux_sys_uname(l
, (const void *)uap
, retval
);
367 * In Linux, cacheflush is currently implemented
368 * as a whole cache flush (arguments are ignored)
369 * we emulate this broken beahior.
372 linux_sys_cacheflush(struct lwp
*l
, const struct linux_sys_cacheflush_args
*uap
, register_t
*retval
)
374 mips_icache_sync_all();
375 mips_dcache_wbinv_all();
380 * This system call is depecated in Linux, but
381 * some binaries and some libraries use it.
384 linux_sys_sysmips(struct lwp
*l
, const struct linux_sys_sysmips_args
*uap
, register_t
*retval
)
387 struct linux_sys_sysmips_args
{
389 syscallarg(int) arg1
;
390 syscallarg(int) arg2
;
391 syscallarg(int) arg3
;
396 switch (SCARG(uap
, cmd
)) {
397 case LINUX_SETNAME
: {
398 char nodename
[LINUX___NEW_UTS_LEN
+ 1];
402 if ((error
= copyinstr((char *)SCARG(uap
, arg1
), nodename
,
403 LINUX___NEW_UTS_LEN
, &len
)) != 0)
407 name
[1] = KERN_HOSTNAME
;
408 return (old_sysctl(&name
[0], 2, 0, 0, nodename
, len
, NULL
));
412 case LINUX_MIPS_ATOMIC_SET
: {
417 addr
= (void *)SCARG(uap
, arg1
);
421 * No error testing here. This is bad, but Linux does
422 * it like this. The source aknowledge "This is broken"
425 (void) copyin(addr
, &value
, 1);
427 value
= (u_int8_t
) SCARG(uap
, arg2
);
428 error
= copyout(&value
, addr
, 1);
434 case LINUX_MIPS_FIXADE
: /* XXX not implemented */
436 case LINUX_FLUSH_CACHE
:
437 mips_icache_sync_all();
438 mips_dcache_wbinv_all();
440 case LINUX_MIPS_RDNVRAM
:
448 printf("linux_sys_sysmips(): unimplemented command %d\n",
450 #endif /* DEBUG_LINUX */
455 linux_usertrap(struct lwp
*l
, vaddr_t trapaddr
, void *arg
)