linux_machdep.c revision 1.1.2.6 1 /* $NetBSD: linux_machdep.c,v 1.1.2.6 2001/12/06 09:30:54 wdk 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.1.2.6 2001/12/06 09:30:54 wdk 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/map.h>
47 #include <sys/proc.h>
48 #include <sys/user.h>
49 #include <sys/buf.h>
50 #include <sys/reboot.h>
51 #include <sys/conf.h>
52 #include <sys/exec.h>
53 #include <sys/file.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/msgbuf.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/device.h>
61 #include <sys/syscallargs.h>
62 #include <sys/filedesc.h>
63 #include <sys/exec_elf.h>
64 #include <sys/disklabel.h>
65 #include <sys/ioctl.h>
66 #include <sys/sysctl.h>
67 #include <miscfs/specfs/specdev.h>
68
69 #include <compat/linux/common/linux_types.h>
70 #include <compat/linux/common/linux_signal.h>
71 #include <compat/linux/common/linux_util.h>
72 #include <compat/linux/common/linux_ioctl.h>
73 #include <compat/linux/common/linux_hdio.h>
74 #include <compat/linux/common/linux_exec.h>
75 #include <compat/linux/common/linux_machdep.h>
76
77 #include <compat/linux/linux_syscallargs.h>
78
79 #include <machine/cpu.h>
80 #include <machine/psl.h>
81 #include <machine/reg.h>
82 #include <machine/regnum.h>
83 #include <machine/vmparam.h>
84 #include <machine/locore.h>
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(l, pack, stack)
105 struct lwp *l;
106 struct exec_package *pack;
107 u_long stack;
108 {
109 setregs(l, 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(catcher, sig, mask, code) /* XXX Check me */
124 sig_t catcher;
125 int sig;
126 sigset_t *mask;
127 u_long code;
128 {
129 struct lwp *l = curproc;
130 struct proc *p = l->l_proc;
131 struct linux_sigframe *fp;
132 struct frame *f;
133 int i,onstack;
134 struct linux_sigframe sf;
135
136 #ifdef DEBUG_LINUX
137 printf("linux_sendsig()\n");
138 #endif /* DEBUG_LINUX */
139 f = (struct frame *)l->l_md.md_regs;
140 #ifdef DEBUG_LINUX
141 printf("f = %p\n", f);
142 #endif /* DEBUG_LINUX */
143
144 /*
145 * Do we need to jump onto the signal stack?
146 */
147 onstack =
148 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
149 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
150
151 /*
152 * Signal stack is broken (see at the end of linux_Sigreturn), so we do
153 * not use it yet. XXX fix this.
154 */
155 onstack=0;
156
157 /*
158 * Allocate space for the signal handler context.
159 */
160 if (onstack)
161 fp = (struct linux_sigframe *)
162 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
163 + p->p_sigctx.ps_sigstk.ss_size);
164 else
165 /* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
166 fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];
167
168 #ifdef DEBUG_LINUX
169 printf("fp = %p, sf = %p\n", fp, &sf);
170 #endif /* DEBUG_LINUX */
171
172 /*
173 * Build stack frame for signal trampoline.
174 */
175 memset(&sf, 0, sizeof sf);
176
177 /*
178 * This is the signal trampoline used by Linux, we don't use it,
179 * but we set ip up in case an application expects it to be there
180 */
181 sf.lsf_code[0] = 0x24020000; /* li v0, __NR_sigreturn */
182 sf.lsf_code[1] = 0x0000000c; /* syscall */
183
184 native_to_linux_sigset(mask, &sf.lsf_mask);
185 for (i=0; i<32; i++)
186 sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
187 sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
188 sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
189 sf.lsf_sc.lsc_pc = f->f_regs[PC];
190 sf.lsf_sc.lsc_status = f->f_regs[SR]; /* XXX */
191 sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
192 sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR]; /* XXX */
193
194 /*
195 * Save signal stack. XXX broken
196 */
197 /* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */
198
199 /*
200 * Install the sigframe onto the stack
201 */
202 fp -= sizeof(struct linux_sigframe);
203 if (copyout(&sf, fp, sizeof(sf)) != 0) {
204 /*
205 * Process has trashed its stack; give it an illegal
206 * instruction to halt it in its tracks.
207 */
208 #ifdef DEBUG_LINUX
209 printf("linux_sendsig: stack trashed\n");
210 #endif /* DEBUG_LINUX */
211 sigexit(l, SIGILL);
212 /* NOTREACHED */
213 }
214
215 /* Set up the registers to return to sigcode. */
216 f->f_regs[A0] = native_to_linux_sig[sig];
217 f->f_regs[A1] = 0;
218 f->f_regs[A2] = (unsigned long)&fp->lsf_sc;
219
220 #ifdef DEBUG_LINUX
221 printf("sigcontext is at %p\n", &fp->lsf_sc);
222 #endif /* DEBUG_LINUX */
223
224 f->f_regs[SP] = (unsigned long)fp;
225 /* Signal trampoline code is at base of user stack. */
226 f->f_regs[RA] = (unsigned long)p->p_sigctx.ps_sigcode;
227 f->f_regs[T9] = (unsigned long)catcher;
228 f->f_regs[PC] = (unsigned long)catcher;
229
230 /* Remember that we're now on the signal stack. */
231 if (onstack)
232 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
233
234 return;
235 }
236
237 /*
238 * System call to cleanup state after a signal
239 * has been taken. Reset signal mask and
240 * stack state from context left by sendsig (above).
241 */
242 int
243 linux_sys_sigreturn(l, v, retval)
244 struct lwp *l;
245 void *v;
246 register_t *retval;
247 {
248 struct linux_sys_sigreturn_args /* {
249 syscallarg(struct linux_pt_regs) regs;
250 } */ *uap = v;
251 struct proc *p = l->l_proc;
252 struct linux_pt_regs regs, kregs;
253 struct linux_sigframe *sf, ksf;
254 struct frame *f;
255 sigset_t mask;
256 int i, error;
257
258 #ifdef DEBUG_LINUX
259 printf("linux_sys_sigreturn()\n");
260 #endif /* DEBUG_LINUX */
261
262 /*
263 * The trampoline code hands us the context.
264 * It is unsafe to keep track of it ourselves, in the event that a
265 * program jumps out of a signal handler.
266 */
267 regs = SCARG(uap, regs);
268
269 kregs = regs;
270 /* if ((error = copyin(regs, &kregs, sizeof(kregs))) != 0)
271 return (error); */
272
273 sf = (struct linux_sigframe *)kregs.lregs[29];
274 if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
275 return (error);
276
277 /* Restore the register context. */
278 f = (struct frame *)l->l_md.md_regs;
279 #ifdef DEBUG_LINUX
280 printf("sf = %p, f = %p\n", sf, f);
281 #endif /* DEBUG_LINUX */
282 for (i=0; i<32; i++)
283 f->f_regs[i] = kregs.lregs[i];
284 f->f_regs[MULLO] = kregs.llo;
285 f->f_regs[MULHI] = kregs.lhi;
286 f->f_regs[PC] = kregs.lcp0_epc;
287 f->f_regs[BADVADDR] = kregs.lcp0_badvaddr;
288 f->f_regs[CAUSE] = kregs.lcp0_cause;
289
290 /* Restore signal stack. */
291 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
292
293 /* Restore signal mask. */
294 linux_to_native_sigset((linux_sigset_t *)&ksf.lsf_mask, &mask);
295 (void)sigprocmask1(p, SIG_SETMASK, &mask, 0);
296
297 return (EJUSTRETURN);
298 }
299
300
301 int
302 linux_sys_rt_sigreturn(l, v, retval)
303 struct lwp *l;
304 void *v;
305 register_t *retval;
306 {
307 return (ENOSYS);
308 }
309
310
311 #if 0
312 int
313 linux_sys_modify_ldt(l, v, retval)
314 struct lwp *l;
315 void *v;
316 register_t *retval;
317 {
318 /*
319 * This syscall is not implemented in Linux/Mips: we should not
320 * be here
321 */
322 #ifdef DEBUG_LINUX
323 printf("linux_sys_modify_ldt: should not be here.\n");
324 #endif /* DEBUG_LINUX */
325 return 0;
326 }
327 #endif
328
329 /*
330 * major device numbers remapping
331 */
332 dev_t
333 linux_fakedev(dev)
334 dev_t dev;
335 {
336 /* XXX write me */
337 return dev;
338 }
339
340 /*
341 * We come here in a last attempt to satisfy a Linux ioctl() call
342 */
343 int
344 linux_machdepioctl(p, v, retval)
345 struct proc *p;
346 void *v;
347 register_t *retval;
348 {
349 return 0;
350 }
351
352 /*
353 * See above. If a root process tries to set access to an I/O port,
354 * just let it have the whole range.
355 */
356 int
357 linux_sys_ioperm(l, v, retval)
358 struct lwp *l;
359 void *v;
360 register_t *retval;
361 {
362 /*
363 * This syscall is not implemented in Linux/Mips: we should not be here
364 */
365 #ifdef DEBUG_LINUX
366 printf("linux_sys_ioperm: should not be here.\n");
367 #endif /* DEBUG_LINUX */
368 return 0;
369 }
370
371 /*
372 * wrapper linux_sys_new_uname() -> linux_sys_uname()
373 */
374 int
375 linux_sys_new_uname(l, v, retval)
376 struct lwp *l;
377 void *v;
378 register_t *retval;
379 {
380 /*
381 * Use this if you want to try Linux emulation with a glibc-2.2
382 * or higher. Note that signals will not work
383 */
384 #if 0
385 struct linux_sys_uname_args /* {
386 syscallarg(struct linux_utsname *) up;
387 } */ *uap = v;
388 struct linux_utsname luts;
389
390 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
391 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
392 strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
393 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
394 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
395 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
396
397 return copyout(&luts, SCARG(uap, up), sizeof(luts));
398 #else
399 return linux_sys_uname(l, v, retval);
400 #endif
401 }
402
403 /*
404 * In Linux, cacheflush is currently implemented
405 * as a whole cache flush (arguments are ignored)
406 * we emulate this broken beahior.
407 */
408 int
409 linux_sys_cacheflush(l, v, retval)
410 struct lwp *l;
411 void *v;
412 register_t *retval;
413 {
414 mips_icache_sync_all();
415 mips_dcache_wbinv_all();
416 return 0;
417 }
418
419 /*
420 * This system call is depecated in Linux, but
421 * some binaries and some libraries use it.
422 */
423 int
424 linux_sys_sysmips(l, v, retval)
425 struct lwp *l;
426 void *v;
427 register_t *retval;
428 {
429 struct linux_sys_sysmips_args {
430 syscallarg(int) cmd;
431 syscallarg(int) arg1;
432 syscallarg(int) arg2;
433 syscallarg(int) arg3;
434 } *uap = v;
435 struct proc *p = l->l_proc;
436 int error;
437
438 switch (SCARG(uap, cmd)) {
439 case LINUX_SETNAME: {
440 char nodename [LINUX___NEW_UTS_LEN + 1];
441 int name;
442 size_t len;
443
444 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
445 return error;
446 if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
447 LINUX___NEW_UTS_LEN, &len)) != 0)
448 return error;
449
450 name = KERN_HOSTNAME;
451 return (kern_sysctl(&name, 1, 0, 0, nodename, len, p));
452
453 break;
454 }
455 case LINUX_MIPS_ATOMIC_SET: {
456 void *addr;
457 int s;
458
459 addr = (void *)SCARG(uap, arg1);
460
461 if ((uvm_useracc((caddr_t)addr, sizeof(int),
462 B_READ | B_WRITE)) != 1)
463 return EFAULT;
464
465 s = splhigh();
466 /*
467 * No error testing here. This is bad, but Linux does
468 * it like this. The source aknowledge "This is broken"
469 * in a comment...
470 */
471 *retval = (register_t)fubyte(addr);
472 error = subyte(addr, SCARG(uap, arg2));
473 splx(s);
474
475 return 0;
476 break;
477 }
478 case LINUX_MIPS_FIXADE: /* XXX not implemented */
479 break;
480 case LINUX_FLUSH_CACHE:
481 mips_icache_sync_all();
482 mips_dcache_wbinv_all();
483 break;
484 case LINUX_MIPS_RDNVRAM:
485 return EIO;
486 break;
487 default:
488 return EINVAL;
489 break;
490 }
491 #ifdef DEBUG_LINUX
492 printf("linux_sys_sysmips(): unimplemented command %d\n",
493 SCARG(uap,cmd));
494 #endif /* DEBUG_LINUX */
495 return 0;
496 }
497