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