linux_machdep.c revision 1.8 1 /* $NetBSD: linux_machdep.c,v 1.8 2001/11/20 21:37:51 manu 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.8 2001/11/20 21:37:51 manu 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
86 #include <mips/cache.h>
87
88 /*
89 * To see whether wscons is configured (for virtual console ioctl calls).
90 */
91 #if defined(_KERNEL_OPT)
92 #include "wsdisplay.h"
93 #endif
94 #if (NWSDISPLAY > 0)
95 #include <dev/wscons/wsconsio.h>
96 #include <dev/wscons/wsdisplay_usl_io.h>
97 #endif
98
99 /*
100 * Set set up registers on exec.
101 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
102 * entry uses NetBSD's native setregs instead of linux_setregs
103 */
104 void
105 linux_setregs(p, pack, stack)
106 struct proc *p;
107 struct exec_package *pack;
108 u_long stack;
109 {
110 setregs(p, pack, stack);
111 return;
112 }
113
114 /*
115 * Send an interrupt to process.
116 *
117 * Adapted from sys/arch/mips/mips/mips_machdep.c
118 *
119 * XXX Does not work well yet with RT signals
120 *
121 */
122
123 void
124 linux_sendsig(catcher, sig, mask, code) /* XXX Check me */
125 sig_t catcher;
126 int sig;
127 sigset_t *mask;
128 u_long code;
129 {
130 struct proc *p = curproc;
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 *)p->p_md.md_regs;
140
141 /*
142 * Do we need to jump onto the signal stack?
143 */
144 onstack =
145 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
146 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
147
148 /*
149 * Signal stack is broken (see at the end of linux_sigreturn), so we do
150 * not use it yet. XXX fix this.
151 */
152 onstack=0;
153
154 /*
155 * Allocate space for the signal handler context.
156 */
157 if (onstack)
158 fp = (struct linux_sigframe *)
159 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
160 + p->p_sigctx.ps_sigstk.ss_size);
161 else
162 /* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
163 fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];
164
165 /*
166 * Build stack frame for signal trampoline.
167 */
168 memset(&sf, 0, sizeof sf);
169
170 /*
171 * This is the signal trampoline used by Linux, we don't use it,
172 * but we set it up in case an application expects it to be there
173 */
174 sf.lsf_code[0] = 0x24020000; /* li v0, __NR_sigreturn */
175 sf.lsf_code[1] = 0x0000000c; /* syscall */
176
177 native_to_linux_sigset(mask, &sf.lsf_mask);
178 for (i=0; i<32; i++) {
179 sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
180 }
181 sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
182 sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
183 sf.lsf_sc.lsc_pc = f->f_regs[PC];
184 sf.lsf_sc.lsc_status = f->f_regs[SR]; /* XXX */
185 sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
186 sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR]; /* XXX */
187
188 /*
189 * Save signal stack. XXX broken
190 */
191 /* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */
192
193 /*
194 * Install the sigframe onto the stack
195 */
196 fp -= sizeof(struct linux_sigframe);
197 if (copyout(&sf, fp, sizeof(sf)) != 0) {
198 /*
199 * Process has trashed its stack; give it an illegal
200 * instruction to halt it in its tracks.
201 */
202 #ifdef DEBUG_LINUX
203 printf("linux_sendsig: stack trashed\n");
204 #endif /* DEBUG_LINUX */
205 sigexit(p, SIGILL);
206 /* NOTREACHED */
207 }
208
209 /* Set up the registers to return to sigcode. */
210 f->f_regs[A0] = native_to_linux_sig[sig];
211 f->f_regs[A1] = 0;
212 f->f_regs[A2] = (unsigned long)&fp->lsf_sc;
213
214 #ifdef DEBUG_LINUX
215 printf("sigcontext is at %p\n", &fp->lsf_sc);
216 #endif /* DEBUG_LINUX */
217
218 f->f_regs[SP] = (unsigned long)fp;
219 /* Signal trampoline code is at base of user stack. */
220 f->f_regs[RA] = (unsigned long)p->p_sigctx.ps_sigcode;
221 f->f_regs[T9] = (unsigned long)catcher;
222 f->f_regs[PC] = (unsigned long)catcher;
223
224 /* Remember that we're now on the signal stack. */
225 if (onstack)
226 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
227
228 /* Attempt to flush everything just in case it fixes problems */
229 /* mips_icache_sync_all();
230 mips_dcache_wbinv_all(); */
231
232 return;
233 }
234
235 /*
236 * System call to cleanup state after a signal
237 * has been taken. Reset signal mask and
238 * stack state from context left by sendsig (above).
239 */
240 int
241 linux_sys_sigreturn(p, v, retval)
242 struct proc *p;
243 void *v;
244 register_t *retval;
245 {
246 struct linux_sys_sigreturn_args /* {
247 syscallarg(struct linux_sigframe *) sf;
248 } */ *uap = v;
249 struct linux_sigframe *sf, ksf;
250 struct frame *f;
251 sigset_t mask;
252 int i, error;
253
254 #ifdef DEBUG_LINUX
255 printf("linux_sys_sigreturn()\n");
256 #endif /* DEBUG_LINUX */
257
258 /*
259 * The trampoline code hands us the context.
260 * It is unsafe to keep track of it ourselves, in the event that a
261 * program jumps out of a signal handler.
262 */
263 sf = SCARG(uap, sf);
264
265 if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
266 return (error);
267
268 /* Restore the register context. */
269 f = (struct frame *)p->p_md.md_regs;
270 for (i=0; i<32; i++)
271 f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
272 f->f_regs[MULLO] = ksf.lsf_sc.lsc_mdlo;
273 f->f_regs[MULHI] = ksf.lsf_sc.lsc_mdhi;
274 f->f_regs[PC] = ksf.lsf_sc.lsc_pc;
275 f->f_regs[BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
276 f->f_regs[CAUSE] = ksf.lsf_sc.lsc_cause;
277 f->f_regs[SR] = ksf.lsf_sc.lsc_status;
278
279 /* Restore signal stack. */
280 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
281
282 /* Restore signal mask. */
283 linux_to_native_sigset((linux_sigset_t *)&ksf.lsf_mask, &mask);
284 (void)sigprocmask1(p, SIG_SETMASK, &mask, 0);
285
286 return (EJUSTRETURN);
287 }
288
289
290 int
291 linux_sys_rt_sigreturn(p, v, retval)
292 struct proc *p;
293 void *v;
294 register_t *retval;
295 {
296 return 0;
297 }
298
299
300 #if 0
301 int
302 linux_sys_modify_ldt(p, v, retval)
303 struct proc *p;
304 void *v;
305 register_t *retval;
306 {
307 /*
308 * This syscall is not implemented in Linux/Mips: we should not
309 * be here
310 */
311 #ifdef DEBUG_LINUX
312 printf("linux_sys_modify_ldt: should not be here.\n");
313 #endif /* DEBUG_LINUX */
314 return 0;
315 }
316 #endif
317
318 /*
319 * major device numbers remapping
320 */
321 dev_t
322 linux_fakedev(dev)
323 dev_t dev;
324 {
325 /* XXX write me */
326 return dev;
327 }
328
329 /*
330 * We come here in a last attempt to satisfy a Linux ioctl() call
331 */
332 int
333 linux_machdepioctl(p, v, retval)
334 struct proc *p;
335 void *v;
336 register_t *retval;
337 {
338 return 0;
339 }
340
341 /*
342 * See above. If a root process tries to set access to an I/O port,
343 * just let it have the whole range.
344 */
345 int
346 linux_sys_ioperm(p, v, retval)
347 struct proc *p;
348 void *v;
349 register_t *retval;
350 {
351 /*
352 * This syscall is not implemented in Linux/Mips: we should not be here
353 */
354 #ifdef DEBUG_LINUX
355 printf("linux_sys_ioperm: should not be here.\n");
356 #endif /* DEBUG_LINUX */
357 return 0;
358 }
359
360 /*
361 * wrapper linux_sys_new_uname() -> linux_sys_uname()
362 */
363 int
364 linux_sys_new_uname(p, v, retval)
365 struct proc *p;
366 void *v;
367 register_t *retval;
368 {
369 /*
370 * Use this if you want to try Linux emulation with a glibc-2.2
371 * or higher. Note that signals will not work
372 */
373 #if 0
374 struct linux_sys_uname_args /* {
375 syscallarg(struct linux_utsname *) up;
376 } */ *uap = v;
377 struct linux_utsname luts;
378
379 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
380 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
381 strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
382 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
383 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
384 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
385
386 return copyout(&luts, SCARG(uap, up), sizeof(luts));
387 #else
388 return linux_sys_uname(p, v, retval);
389 #endif
390 }
391
392 /*
393 * In Linux, cacheflush is icurrently implemented
394 * as a whole cache flush (arguments are ignored)
395 * we emulate this broken beahior.
396 */
397 int
398 linux_sys_cacheflush(p, v, retval)
399 struct proc *p;
400 void *v;
401 register_t *retval;
402 {
403 mips_icache_sync_all();
404 mips_dcache_wbinv_all();
405 return 0;
406 }
407
408 /*
409 * This system call is depecated in Linux, but
410 * some binaries and some libraries use it.
411 */
412 int
413 linux_sys_sysmips(p, v, retval)
414 struct proc *p;
415 void *v;
416 register_t *retval;
417 {
418 struct linux_sys_sysmips_args {
419 syscallarg(int) cmd;
420 syscallarg(int) arg1;
421 syscallarg(int) arg2;
422 syscallarg(int) arg3;
423 } *uap = v;
424 int error;
425
426 switch (SCARG(uap, cmd)) {
427 case LINUX_SETNAME: {
428 char nodename [LINUX___NEW_UTS_LEN + 1];
429 int name;
430 size_t len;
431
432 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
433 return error;
434 if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
435 LINUX___NEW_UTS_LEN, &len)) != 0)
436 return error;
437
438 name = KERN_HOSTNAME;
439 return (kern_sysctl(&name, 1, 0, 0, nodename, len, p));
440
441 break;
442 }
443 case LINUX_MIPS_ATOMIC_SET: {
444 void *addr;
445 int s;
446
447 addr = (void *)SCARG(uap, arg1);
448
449 if ((uvm_useracc((caddr_t)addr, sizeof(int),
450 B_READ | B_WRITE)) != 1)
451 return EFAULT;
452
453 s = splhigh();
454 /*
455 * No error testing here. This is bad, but Linux does
456 * it like this. The source aknowledge "This is broken"
457 * in a comment...
458 */
459 *retval = (register_t)fubyte(addr);
460 error = subyte(addr, SCARG(uap, arg2));
461 splx(s);
462
463 return 0;
464 break;
465 }
466 case LINUX_MIPS_FIXADE: /* XXX not implemented */
467 break;
468 case LINUX_FLUSH_CACHE:
469 mips_icache_sync_all();
470 mips_dcache_wbinv_all();
471 break;
472 case LINUX_MIPS_RDNVRAM:
473 return EIO;
474 break;
475 default:
476 return EINVAL;
477 break;
478 }
479 #ifdef DEBUG_LINUX
480 printf("linux_sys_sysmips(): unimplemented command %d\n",
481 SCARG(uap,cmd));
482 #endif /* DEBUG_LINUX */
483 return 0;
484 }
485