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