xref: /netbsd/sys/arch/i386/i386/linux_syscall.c (revision 6550d01e)
1 /*	$NetBSD: linux_syscall.c,v 1.50 2009/11/21 03:11:00 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_syscall.c,v 1.50 2009/11/21 03:11:00 rmind Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_vm86.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/signal.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallvar.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #include <machine/cpu.h>
49 #include <machine/psl.h>
50 #include <machine/userret.h>
51 
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_errno.h>
54 #include <compat/linux/linux_syscall.h>
55 #include <compat/linux/common/linux_signal.h>
56 #include <compat/linux/arch/i386/linux_machdep.h>
57 
58 static void linux_syscall(struct trapframe *);
59 extern struct sysent linux_sysent[];
60 
61 void
62 linux_syscall_intern(struct proc *p)
63 {
64 
65 	p->p_md.md_syscall = linux_syscall;
66 }
67 
68 /*
69  * syscall(frame):
70  *	System call request from POSIX system call gate interface to kernel.
71  * Like trap(), argument is call by reference.
72  */
73 void
74 linux_syscall(struct trapframe *frame)
75 {
76 	register const struct sysent *callp;
77 	struct lwp *l;
78 	int error;
79 	register_t code, args[6], rval[2];
80 
81 	l = curlwp;
82 	LWP_CACHE_CREDS(l, l->l_proc);
83 
84 	code = frame->tf_eax & (LINUX_SYS_NSYSENT - 1);
85 	callp = linux_sysent;
86 
87 	callp += code;
88 	/*
89 	 * Linux passes the args in ebx, ecx, edx, esi, edi, ebp, in
90 	 * increasing order.
91 	 */
92 	args[0] = frame->tf_ebx;
93 	args[1] = frame->tf_ecx;
94 	args[2] = frame->tf_edx;
95 	args[3] = frame->tf_esi;
96 	args[4] = frame->tf_edi;
97 	args[5] = frame->tf_ebp;
98 
99 	rval[0] = 0;
100 	rval[1] = 0;
101 
102 	if (__predict_false(l->l_proc->p_trace_enabled)) {
103 		error = trace_enter(code, args, callp->sy_narg);
104 		if (__predict_true(error == 0)) {
105 			error = sy_call(callp, l, args, rval);
106 			code = frame->tf_eax & (LINUX_SYS_NSYSENT - 1);
107 			trace_exit(code, rval, error);
108 		}
109 	} else
110 		error = sy_call(callp, l, args, rval);
111 
112 	if (__predict_true(error == 0)) {
113 		frame->tf_eax = rval[0];
114 		/*
115 		 * XXX The linux libc code I (dsl) looked at doesn't use the
116 		 * carry bit.
117 		 * Values above 0xfffff000 are assumed to be errno values and
118 		 * not result codes!
119 		 */
120 		frame->tf_eflags &= ~PSL_C;	/* carry bit */
121 	} else {
122 		switch (error) {
123 		case ERESTART:
124 			/*
125 			 * The offset to adjust the PC by depends on whether
126 			 * we entered the kernel through the trap or call gate.
127 			 * We save the instruction size in tf_err on entry.
128 			 */
129 			frame->tf_eip -= frame->tf_err;
130 			break;
131 		case EJUSTRETURN:
132 			/* nothing to do */
133 			break;
134 		default:
135 			error = native_to_linux_errno[error];
136 			frame->tf_eax = error;
137 			frame->tf_eflags |= PSL_C;	/* carry bit */
138 			break;
139 		}
140 	}
141 
142 	userret(l);
143 }
144