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