linux_machdep.c revision 1.18 1 /* $NetBSD: linux_machdep.c,v 1.18 1995/09/08 07:57:15 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Frank van der Linden
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/kernel.h>
38 #include <sys/map.h>
39 #include <sys/proc.h>
40 #include <sys/user.h>
41 #include <sys/buf.h>
42 #include <sys/reboot.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/sysctl.h>
53 #include <sys/syscallargs.h>
54 #include <sys/filedesc.h>
55
56 #include <compat/linux/linux_types.h>
57 #include <compat/linux/linux_signal.h>
58 #include <compat/linux/linux_syscallargs.h>
59 #include <compat/linux/linux_util.h>
60
61 #include <machine/cpu.h>
62 #include <machine/cpufunc.h>
63 #include <machine/psl.h>
64 #include <machine/reg.h>
65 #include <machine/segments.h>
66 #include <machine/specialreg.h>
67 #include <machine/sysarch.h>
68 #include <machine/linux_machdep.h>
69
70 /*
71 * To see whether pcvt is configured (for virtual console ioctl calls).
72 */
73 #include "vt.h"
74 #if NVT > 0
75 #include <arch/i386/isa/pcvt/pcvt_ioctl.h>
76 #endif
77
78 /*
79 * Deal with some i386-specific things in the Linux emulation code.
80 * This means just signals for now, will include stuff like
81 * I/O map permissions and V86 mode sometime.
82 */
83
84 /*
85 * Send an interrupt to process.
86 *
87 * Stack is set up to allow sigcode stored
88 * in u. to call routine, followed by kcall
89 * to sigreturn routine below. After sigreturn
90 * resets the signal mask, the stack, and the
91 * frame pointer, it returns to the user
92 * specified pc, psl.
93 */
94
95 void
96 linux_sendsig(catcher, sig, mask, code)
97 sig_t catcher;
98 int sig, mask;
99 u_long code;
100 {
101 register struct proc *p = curproc;
102 register struct trapframe *tf;
103 struct linux_sigframe *fp, frame;
104 struct sigacts *psp = p->p_sigacts;
105 int oonstack;
106 extern char linux_sigcode[], linux_esigcode[];
107
108 tf = p->p_md.md_regs;
109 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
110
111 /*
112 * Allocate space for the signal handler context.
113 */
114 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
115 (psp->ps_sigonstack & sigmask(sig))) {
116 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
117 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
118 psp->ps_sigstk.ss_flags |= SS_ONSTACK;
119 } else {
120 fp = (struct linux_sigframe *)tf->tf_esp - 1;
121 }
122
123 frame.sf_handler = catcher;
124 frame.sf_sig = bsd_to_linux_sig[sig];
125
126 /*
127 * Build the signal context to be used by sigreturn.
128 */
129 frame.sf_sc.sc_mask = mask;
130 #ifdef VM86
131 if (tf->tf_eflags & PSL_VM) {
132 frame.sf_sc.sc_gs = tf->tf_vm86_gs;
133 frame.sf_sc.sc_fs = tf->tf_vm86_fs;
134 frame.sf_sc.sc_es = tf->tf_vm86_es;
135 frame.sf_sc.sc_ds = tf->tf_vm86_ds;
136 } else
137 #endif
138 {
139 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
140 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
141 frame.sf_sc.sc_es = tf->tf_es;
142 frame.sf_sc.sc_ds = tf->tf_ds;
143 }
144 frame.sf_sc.sc_edi = tf->tf_edi;
145 frame.sf_sc.sc_esi = tf->tf_esi;
146 frame.sf_sc.sc_ebp = tf->tf_ebp;
147 frame.sf_sc.sc_ebx = tf->tf_ebx;
148 frame.sf_sc.sc_edx = tf->tf_edx;
149 frame.sf_sc.sc_ecx = tf->tf_ecx;
150 frame.sf_sc.sc_eax = tf->tf_eax;
151 frame.sf_sc.sc_eip = tf->tf_eip;
152 frame.sf_sc.sc_cs = tf->tf_cs;
153 frame.sf_sc.sc_eflags = tf->tf_eflags;
154 frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
155 frame.sf_sc.sc_ss = tf->tf_ss;
156 frame.sf_sc.sc_err = tf->tf_err;
157 frame.sf_sc.sc_trapno = tf->tf_trapno;
158
159 if (copyout(&frame, fp, sizeof(frame)) != 0) {
160 /*
161 * Process has trashed its stack; give it an illegal
162 * instruction to halt it in its tracks.
163 */
164 sigexit(p, SIGILL);
165 /* NOTREACHED */
166 }
167
168 /*
169 * Build context to run handler in.
170 */
171 tf->tf_esp = (int)fp;
172 tf->tf_eip = (int)(((char *)PS_STRINGS) -
173 (linux_esigcode - linux_sigcode));
174 #ifdef VM86
175 tf->tf_eflags &= ~PSL_VM;
176 #endif
177 tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
178 tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
179 tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
180 tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
181 }
182
183 /*
184 * System call to cleanup state after a signal
185 * has been taken. Reset signal mask and
186 * stack state from context left by sendsig (above).
187 * Return to previous pc and psl as specified by
188 * context left by sendsig. Check carefully to
189 * make sure that the user has not modified the
190 * psl to gain improper privileges or to cause
191 * a machine fault.
192 */
193 int
194 linux_sigreturn(p, uap, retval)
195 struct proc *p;
196 struct linux_sigreturn_args /* {
197 syscallarg(struct linux_sigcontext *) scp;
198 } */ *uap;
199 register_t *retval;
200 {
201 struct linux_sigcontext *scp, context;
202 register struct trapframe *tf;
203
204 tf = p->p_md.md_regs;
205
206 /*
207 * The trampoline code hands us the context.
208 * It is unsafe to keep track of it ourselves, in the event that a
209 * program jumps out of a signal handler.
210 */
211 scp = SCARG(uap, scp);
212 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
213 return (EFAULT);
214
215 /*
216 * Check for security violations.
217 */
218 if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
219 ISPL(context.sc_cs) != SEL_UPL)
220 return (EINVAL);
221
222 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
223 p->p_sigmask = context.sc_mask & ~sigcantmask;
224
225 /*
226 * Restore signal context.
227 */
228 #ifdef VM86
229 if (context.sc_eflags & PSL_VM) {
230 tf->tf_vm86_gs = context.sc_gs;
231 tf->tf_vm86_fs = context.sc_fs;
232 tf->tf_vm86_es = context.sc_es;
233 tf->tf_vm86_ds = context.sc_ds;
234 } else
235 #endif
236 {
237 /* %fs and %gs were restored by the trampoline. */
238 tf->tf_es = context.sc_es;
239 tf->tf_ds = context.sc_ds;
240 }
241 tf->tf_edi = context.sc_edi;
242 tf->tf_esi = context.sc_esi;
243 tf->tf_ebp = context.sc_ebp;
244 tf->tf_ebx = context.sc_ebx;
245 tf->tf_edx = context.sc_edx;
246 tf->tf_ecx = context.sc_ecx;
247 tf->tf_eax = context.sc_eax;
248 tf->tf_eip = context.sc_eip;
249 tf->tf_cs = context.sc_cs;
250 tf->tf_eflags = context.sc_eflags;
251 tf->tf_esp = context.sc_esp_at_signal;
252 tf->tf_ss = context.sc_ss;
253
254 return (EJUSTRETURN);
255 }
256
257 #ifdef USER_LDT
258
259 int
260 linux_read_ldt(p, uap, retval)
261 struct proc *p;
262 struct linux_modify_ldt_args /* {
263 syscallarg(int) func;
264 syscallarg(void *) ptr;
265 syscallarg(size_t) bytecount;
266 } */ *uap;
267 register_t *retval;
268 {
269 struct i386_get_ldt_args gl;
270 int error;
271 caddr_t sg;
272 char *parms;
273
274 sg = stackgap_init(p->p_emul);
275
276 gl.start = 0;
277 gl.desc = SCARG(uap, ptr);
278 gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
279
280 parms = stackgap_alloc(&sg, sizeof(gl));
281
282 if (error = copyout(&gl, parms, sizeof(gl)))
283 return (error);
284
285 if (error = i386_get_ldt(p, parms, retval))
286 return (error);
287
288 *retval *= sizeof(union descriptor);
289 return (0);
290 }
291
292 struct linux_ldt_info {
293 u_int entry_number;
294 u_long base_addr;
295 u_int limit;
296 u_int seg_32bit:1;
297 u_int contents:2;
298 u_int read_exec_only:1;
299 u_int limit_in_pages:1;
300 u_int seg_not_present:1;
301 };
302
303 int
304 linux_write_ldt(p, uap, retval)
305 struct proc *p;
306 struct linux_modify_ldt_args /* {
307 syscallarg(int) func;
308 syscallarg(void *) ptr;
309 syscallarg(size_t) bytecount;
310 } */ *uap;
311 register_t *retval;
312 {
313 struct linux_ldt_info ldt_info;
314 struct segment_descriptor sd;
315 struct i386_set_ldt_args sl;
316 int error;
317 caddr_t sg;
318 char *parms;
319
320 if (SCARG(uap, bytecount) != sizeof(ldt_info))
321 return (EINVAL);
322 if (error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info)))
323 return error;
324 if (ldt_info.contents == 3)
325 return (EINVAL);
326
327 sg = stackgap_init(p->p_emul);
328
329 sd.sd_lobase = ldt_info.base_addr & 0xffffff;
330 sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
331 sd.sd_lolimit = ldt_info.limit & 0xffff;
332 sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
333 sd.sd_type =
334 16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
335 sd.sd_dpl = SEL_UPL;
336 sd.sd_p = !ldt_info.seg_not_present;
337 sd.sd_def32 = ldt_info.seg_32bit;
338 sd.sd_gran = ldt_info.limit_in_pages;
339
340 sl.start = ldt_info.entry_number;
341 sl.desc = stackgap_alloc(&sg, sizeof(sd));
342 sl.num = 1;
343
344 #if 0
345 printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
346 ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
347 #endif
348
349 parms = stackgap_alloc(&sg, sizeof(sl));
350
351 if (error = copyout(&sd, sl.desc, sizeof(sd)))
352 return (error);
353 if (error = copyout(&sl, parms, sizeof(sl)))
354 return (error);
355
356 if (error = i386_set_ldt(p, parms, retval))
357 return (error);
358
359 *retval = 0;
360 return (0);
361 }
362
363 #endif /* USER_LDT */
364
365 int
366 linux_modify_ldt(p, uap, retval)
367 struct proc *p;
368 struct linux_modify_ldt_args /* {
369 syscallarg(int) func;
370 syscallarg(void *) ptr;
371 syscallarg(size_t) bytecount;
372 } */ *uap;
373 register_t *retval;
374 {
375
376 switch (SCARG(uap, func)) {
377 #ifdef USER_LDT
378 case 0:
379 return (linux_read_ldt(p, uap, retval));
380
381 case 1:
382 return (linux_write_ldt(p, uap, retval));
383 #endif /* USER_LDT */
384
385 default:
386 return (ENOSYS);
387 }
388 }
389
390 /*
391 * XXX Pathetic hack to make svgalib work. This will fake the major
392 * device number of an opened VT so that svgalib likes it. grmbl.
393 * Should probably do it 'wrong the right way' and use a mapping
394 * array for all major device numbers, and map linux_mknod too.
395 */
396 dev_t
397 linux_fakedev(dev)
398 dev_t dev;
399 {
400 if (major(dev) == NETBSD_CONS_MAJOR)
401 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
402 return dev;
403 }
404
405 /*
406 * We come here in a last attempt to satisfy a Linux ioctl() call
407 */
408 int
409 linux_machdepioctl(p, uap, retval)
410 struct proc *p;
411 struct linux_ioctl_args /* {
412 syscallarg(int) fd;
413 syscallarg(u_long) com;
414 syscallarg(caddr_t) data;
415 } */ *uap;
416 register_t *retval;
417 {
418 struct ioctl_args bia, tmparg;
419 u_long com;
420 #if NVT > 0
421 int error, mode;
422 struct vt_mode lvt;
423 caddr_t bvtp, sg;
424 u_int fd;
425 struct file *fp;
426 struct filedesc *fdp;
427 #endif
428
429 SCARG(&bia, fd) = SCARG(uap, fd);
430 SCARG(&bia, data) = SCARG(uap, data);
431 com = SCARG(uap, com);
432
433 switch (com) {
434 #if NVT > 0
435 case LINUX_KDGKBMODE:
436 /*
437 * Could be implemented but somehow KDGKBMODE is the
438 * same as KBDGLEDS.
439 */
440 mode = K_XLATE;
441 return copyout(&mode, SCARG(uap, data), sizeof mode);
442 case LINUX_KDSKBMODE:
443 com = KDSKBMODE;
444 if ((unsigned)SCARG(uap, data) == LINUX_K_MEDIUMRAW)
445 SCARG(&bia, data) = (caddr_t)K_RAW;
446 break;
447 case LINUX_KDMKTONE:
448 com = KDMKTONE;
449 break;
450 case LINUX_KDSETMODE:
451 com = KDSETMODE;
452 break;
453 case LINUX_KDENABIO:
454 com = KDENABIO;
455 break;
456 case LINUX_KDDISABIO:
457 com = KDDISABIO;
458 break;
459 case LINUX_KDGETLED:
460 com = KDGETLED;
461 break;
462 case LINUX_KDSETLED:
463 com = KDSETLED;
464 break;
465 case LINUX_VT_OPENQRY:
466 com = VT_OPENQRY;
467 break;
468 case LINUX_VT_GETMODE:
469 SCARG(&bia, com) = VT_GETMODE;
470 if ((error = ioctl(p, &bia, retval)))
471 return error;
472 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
473 sizeof (struct vt_mode))))
474 return error;
475 lvt.relsig = bsd_to_linux_sig[lvt.relsig];
476 lvt.acqsig = bsd_to_linux_sig[lvt.acqsig];
477 lvt.frsig = bsd_to_linux_sig[lvt.frsig];
478 return copyout((caddr_t)&lvt, SCARG(uap, data),
479 sizeof (struct vt_mode));
480 case LINUX_VT_SETMODE:
481 com = VT_SETMODE;
482 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
483 sizeof (struct vt_mode))))
484 return error;
485 lvt.relsig = linux_to_bsd_sig[lvt.relsig];
486 lvt.acqsig = linux_to_bsd_sig[lvt.acqsig];
487 lvt.frsig = linux_to_bsd_sig[lvt.frsig];
488 sg = stackgap_init(p->p_emul);
489 bvtp = stackgap_alloc(&sg, sizeof (struct vt_mode));
490 if ((error = copyout(&lvt, bvtp, sizeof (struct vt_mode))))
491 return error;
492 SCARG(&bia, data) = bvtp;
493 break;
494 case LINUX_VT_RELDISP:
495 com = VT_RELDISP;
496 break;
497 case LINUX_VT_ACTIVATE:
498 com = VT_ACTIVATE;
499 break;
500 case LINUX_VT_WAITACTIVE:
501 com = VT_WAITACTIVE;
502 break;
503 #endif
504 default:
505 return EINVAL;
506 }
507 SCARG(&bia, com) = com;
508 return ioctl(p, &bia, retval);
509 }
510
511 /*
512 * Set I/O permissions for a process. Just set the maximum level
513 * right away (ignoring the argument), otherwise we would have
514 * to rely on I/O permission maps, which are not implemented.
515 */
516 int
517 linux_iopl(p, uap, retval)
518 struct proc *p;
519 struct linux_iopl_args /* {
520 syscallarg(int) level;
521 } */ *uap;
522 register_t *retval;
523 {
524 struct trapframe *fp = p->p_md.md_regs;
525
526 if (suser(p->p_ucred, &p->p_acflag) != 0)
527 return EPERM;
528 fp->tf_eflags |= PSL_IOPL;
529 *retval = 0;
530 return 0;
531 }
532
533 /*
534 * See above. If a root process tries to set access to an I/O port,
535 * just let it have the whole range.
536 */
537 int
538 linux_ioperm(p, uap, retval)
539 struct proc *p;
540 struct linux_ioperm_args /* {
541 syscallarg(unsigned int) lo;
542 syscallarg(unsigned int) hi;
543 syscallarg(int) val;
544 } */ *uap;
545 register_t *retval;
546 {
547 struct trapframe *fp = p->p_md.md_regs;
548
549 if (suser(p->p_ucred, &p->p_acflag) != 0)
550 return EPERM;
551 if (SCARG(uap, val))
552 fp->tf_eflags |= PSL_IOPL;
553 *retval = 0;
554 return 0;
555 }
556