1 /* $NetBSD: linux_syscall.c,v 1.19 2002/03/22 16:41:23 christos 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 * 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_syscall.c,v 1.19 2002/03/22 16:41:23 christos Exp $"); 41 42 #if defined(_KERNEL_OPT) 43 #include "opt_syscall_debug.h" 44 #include "opt_vm86.h" 45 #include "opt_ktrace.h" 46 #endif 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/proc.h> 51 #include <sys/user.h> 52 #include <sys/signal.h> 53 #ifdef KTRACE 54 #include <sys/ktrace.h> 55 #endif 56 #include <sys/syscall.h> 57 58 #include <uvm/uvm_extern.h> 59 60 #include <machine/cpu.h> 61 #include <machine/psl.h> 62 #include <machine/userret.h> 63 64 #include <compat/linux/common/linux_types.h> 65 #include <compat/linux/common/linux_errno.h> 66 #include <compat/linux/linux_syscall.h> 67 #include <compat/linux/common/linux_signal.h> 68 #include <compat/linux/arch/i386/linux_machdep.h> 69 70 void linux_syscall_plain __P((struct trapframe)); 71 void linux_syscall_fancy __P((struct trapframe)); 72 extern struct sysent linux_sysent[]; 73 74 void 75 linux_syscall_intern(p) 76 struct proc *p; 77 { 78 79 #ifdef KTRACE 80 if (p->p_traceflag & (KTRFAC_SYSCALL | KTRFAC_SYSRET)) 81 p->p_md.md_syscall = linux_syscall_fancy; 82 else 83 #endif 84 p->p_md.md_syscall = linux_syscall_plain; 85 } 86 87 /* 88 * syscall(frame): 89 * System call request from POSIX system call gate interface to kernel. 90 * Like trap(), argument is call by reference. 91 */ 92 void 93 linux_syscall_plain(frame) 94 struct trapframe frame; 95 { 96 register const struct sysent *callp; 97 register struct proc *p; 98 int error; 99 size_t argsize; 100 register_t code, args[8], rval[2]; 101 102 uvmexp.syscalls++; 103 p = curproc; 104 105 code = frame.tf_eax; 106 callp = linux_sysent; 107 108 code &= (LINUX_SYS_NSYSENT - 1); 109 callp += code; 110 argsize = callp->sy_argsize; 111 if (argsize) { 112 /* 113 * Linux passes the args in ebx, ecx, edx, esi, edi, ebp, in 114 * increasing order. 115 */ 116 switch (argsize >> 2) { 117 case 6: 118 args[5] = frame.tf_ebp; 119 case 5: 120 args[4] = frame.tf_edi; 121 case 4: 122 args[3] = frame.tf_esi; 123 case 3: 124 args[2] = frame.tf_edx; 125 case 2: 126 args[1] = frame.tf_ecx; 127 case 1: 128 args[0] = frame.tf_ebx; 129 break; 130 default: 131 panic("linux syscall %d bogus argument size %d", 132 code, argsize); 133 break; 134 } 135 } 136 #ifdef SYSCALL_DEBUG 137 scdebug_call(p, code, args); 138 #endif /* SYSCALL_DEBUG */ 139 rval[0] = 0; 140 rval[1] = 0; 141 error = (*callp->sy_call)(p, args, rval); 142 switch (error) { 143 case 0: 144 frame.tf_eax = rval[0]; 145 frame.tf_eflags &= ~PSL_C; /* carry bit */ 146 break; 147 case ERESTART: 148 /* 149 * The offset to adjust the PC by depends on whether we entered 150 * the kernel through the trap or call gate. We pushed the 151 * size of the instruction into tf_err on entry. 152 */ 153 frame.tf_eip -= frame.tf_err; 154 break; 155 case EJUSTRETURN: 156 /* nothing to do */ 157 break; 158 default: 159 error = native_to_linux_errno[error]; 160 frame.tf_eax = error; 161 frame.tf_eflags |= PSL_C; /* carry bit */ 162 break; 163 } 164 165 #ifdef SYSCALL_DEBUG 166 scdebug_ret(p, code, error, rval); 167 #endif /* SYSCALL_DEBUG */ 168 userret(p); 169 } 170 171 /* 172 * syscall(frame): 173 * System call request from POSIX system call gate interface to kernel. 174 * Like trap(), argument is call by reference. 175 */ 176 void 177 linux_syscall_fancy(frame) 178 struct trapframe frame; 179 { 180 register const struct sysent *callp; 181 register struct proc *p; 182 int error; 183 size_t argsize; 184 register_t code, args[8], rval[2]; 185 186 uvmexp.syscalls++; 187 p = curproc; 188 189 code = frame.tf_eax; 190 callp = linux_sysent; 191 192 code &= (LINUX_SYS_NSYSENT - 1); 193 callp += code; 194 argsize = callp->sy_argsize; 195 if (argsize) { 196 /* 197 * Linux passes the args in ebx, ecx, edx, esi, edi, ebp, in 198 * increasing order. 199 */ 200 switch (argsize >> 2) { 201 case 6: 202 args[5] = frame.tf_ebp; 203 case 5: 204 args[4] = frame.tf_edi; 205 case 4: 206 args[3] = frame.tf_esi; 207 case 3: 208 args[2] = frame.tf_edx; 209 case 2: 210 args[1] = frame.tf_ecx; 211 case 1: 212 args[0] = frame.tf_ebx; 213 break; 214 default: 215 panic("linux syscall %d bogus argument size %d", 216 code, argsize); 217 break; 218 } 219 } 220 #ifdef SYSCALL_DEBUG 221 scdebug_call(p, code, args); 222 #endif /* SYSCALL_DEBUG */ 223 #ifdef KTRACE 224 if (KTRPOINT(p, KTR_SYSCALL)) 225 ktrsyscall(p, code, argsize, args); 226 #endif /* KTRACE */ 227 rval[0] = 0; 228 rval[1] = 0; 229 error = (*callp->sy_call)(p, args, rval); 230 switch (error) { 231 case 0: 232 frame.tf_eax = rval[0]; 233 frame.tf_eflags &= ~PSL_C; /* carry bit */ 234 break; 235 case ERESTART: 236 /* 237 * The offset to adjust the PC by depends on whether we entered 238 * the kernel through the trap or call gate. We pushed the 239 * size of the instruction into tf_err on entry. 240 */ 241 frame.tf_eip -= frame.tf_err; 242 break; 243 case EJUSTRETURN: 244 /* nothing to do */ 245 break; 246 default: 247 error = native_to_linux_errno[error]; 248 frame.tf_eax = error; 249 frame.tf_eflags |= PSL_C; /* carry bit */ 250 break; 251 } 252 253 #ifdef SYSCALL_DEBUG 254 scdebug_ret(p, code, error, rval); 255 #endif /* SYSCALL_DEBUG */ 256 userret(p); 257 #ifdef KTRACE 258 if (KTRPOINT(p, KTR_SYSRET)) 259 ktrsysret(p, code, error, rval[0]); 260 #endif /* KTRACE */ 261 } 262