ia32_syscall.c revision 115429
1/*- 2 * Copyright (C) 1994, David Greenman 3 * Copyright (c) 1990, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the University of Utah, and William Jolitz. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * $FreeBSD: head/sys/amd64/ia32/ia32_syscall.c 115429 2003-05-31 06:49:53Z peter $ 38 */ 39 40/* 41 * 386 Trap and System call handling 42 */ 43 44#include "opt_clock.h" 45#include "opt_cpu.h" 46#include "opt_isa.h" 47#include "opt_ktrace.h" 48 49#include <sys/param.h> 50#include <sys/bus.h> 51#include <sys/systm.h> 52#include <sys/proc.h> 53#include <sys/pioctl.h> 54#include <sys/kernel.h> 55#include <sys/ktr.h> 56#include <sys/lock.h> 57#include <sys/mutex.h> 58#include <sys/resourcevar.h> 59#include <sys/signalvar.h> 60#include <sys/syscall.h> 61#include <sys/sysctl.h> 62#include <sys/sysent.h> 63#include <sys/uio.h> 64#include <sys/vmmeter.h> 65#ifdef KTRACE 66#include <sys/ktrace.h> 67#endif 68 69#include <vm/vm.h> 70#include <vm/vm_param.h> 71#include <vm/pmap.h> 72#include <vm/vm_kern.h> 73#include <vm/vm_map.h> 74#include <vm/vm_page.h> 75#include <vm/vm_extern.h> 76 77#include <machine/cpu.h> 78#include <machine/md_var.h> 79 80#include <amd64/isa/icu.h> 81#include <amd64/isa/intr_machdep.h> 82 83#define IDTVEC(name) __CONCAT(X,name) 84 85extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd); 86extern const char *ia32_syscallnames[]; 87 88void ia32_syscall(struct trapframe frame); /* Called from asm code */ 89 90void 91ia32_syscall(struct trapframe frame) 92{ 93 caddr_t params; 94 int i; 95 struct sysent *callp; 96 struct thread *td = curthread; 97 struct proc *p = td->td_proc; 98 register_t orig_tf_rflags; 99 u_int sticks; 100 int error; 101 int narg; 102 u_int32_t args[8]; 103 u_int64_t args64[8]; 104 u_int code; 105 106 /* 107 * note: PCPU_LAZY_INC() can only be used if we can afford 108 * occassional inaccuracy in the count. 109 */ 110 cnt.v_syscall++; 111 112 sticks = td->td_sticks; 113 td->td_frame = &frame; 114 if (td->td_ucred != p->p_ucred) 115 cred_update_thread(td); 116 params = (caddr_t)frame.tf_rsp + sizeof(u_int32_t); 117 code = frame.tf_rax; 118 orig_tf_rflags = frame.tf_rflags; 119 120 if (p->p_sysent->sv_prepsyscall) { 121 /* 122 * The prep code is MP aware. 123 */ 124 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms); 125 } else { 126 /* 127 * Need to check if this is a 32 bit or 64 bit syscall. 128 * fuword is MP aware. 129 */ 130 if (code == SYS_syscall) { 131 /* 132 * Code is first argument, followed by actual args. 133 */ 134 code = fuword32(params); 135 params += sizeof(int); 136 } else if (code == SYS___syscall) { 137 /* 138 * Like syscall, but code is a quad, so as to maintain 139 * quad alignment for the rest of the arguments. 140 * We use a 32-bit fetch in case params is not 141 * aligned. 142 */ 143 code = fuword32(params); 144 params += sizeof(quad_t); 145 } 146 } 147 148 if (p->p_sysent->sv_mask) 149 code &= p->p_sysent->sv_mask; 150 151 if (code >= p->p_sysent->sv_size) 152 callp = &p->p_sysent->sv_table[0]; 153 else 154 callp = &p->p_sysent->sv_table[code]; 155 156 narg = callp->sy_narg & SYF_ARGMASK; 157 158 /* 159 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware 160 */ 161 if (params != NULL && narg != 0) 162 error = copyin(params, (caddr_t)args, 163 (u_int)(narg * sizeof(int))); 164 else 165 error = 0; 166 167 for (i = 0; i < narg; i++) 168 args64[i] = args[i]; 169 170#ifdef KTRACE 171 if (KTRPOINT(td, KTR_SYSCALL)) 172 ktrsyscall(code, narg, args64); 173#endif 174 /* 175 * Try to run the syscall without Giant if the syscall 176 * is MP safe. 177 */ 178 if ((callp->sy_narg & SYF_MPSAFE) == 0) 179 mtx_lock(&Giant); 180 181 if (error == 0) { 182 td->td_retval[0] = 0; 183 td->td_retval[1] = frame.tf_rdx; 184 185 STOPEVENT(p, S_SCE, narg); 186 187 error = (*callp->sy_call)(td, args64); 188 } 189 190 switch (error) { 191 case 0: 192 frame.tf_rax = td->td_retval[0]; 193 frame.tf_rdx = td->td_retval[1]; 194 frame.tf_rflags &= ~PSL_C; 195 break; 196 197 case ERESTART: 198 /* 199 * Reconstruct pc, assuming lcall $X,y is 7 bytes, 200 * int 0x80 is 2 bytes. We saved this in tf_err. 201 */ 202 frame.tf_rip -= frame.tf_err; 203 break; 204 205 case EJUSTRETURN: 206 break; 207 208 default: 209 if (p->p_sysent->sv_errsize) { 210 if (error >= p->p_sysent->sv_errsize) 211 error = -1; /* XXX */ 212 else 213 error = p->p_sysent->sv_errtbl[error]; 214 } 215 frame.tf_rax = error; 216 frame.tf_rflags |= PSL_C; 217 break; 218 } 219 220 /* 221 * Release Giant if we previously set it. 222 */ 223 if ((callp->sy_narg & SYF_MPSAFE) == 0) 224 mtx_unlock(&Giant); 225 226 /* 227 * Traced syscall. 228 */ 229 if (orig_tf_rflags & PSL_T) { 230 frame.tf_rflags &= ~PSL_T; 231 trapsignal(td, SIGTRAP, 0); 232 } 233 234 /* 235 * Handle reschedule and other end-of-syscall issues 236 */ 237 userret(td, &frame, sticks); 238 239#ifdef KTRACE 240 if (KTRPOINT(td, KTR_SYSRET)) 241 ktrsysret(code, error, td->td_retval[0]); 242#endif 243 244 /* 245 * This works because errno is findable through the 246 * register set. If we ever support an emulation where this 247 * is not the case, this code will need to be revisited. 248 */ 249 STOPEVENT(p, S_SCX, code); 250 251#ifdef DIAGNOSTIC 252 cred_free_thread(td); 253#endif 254 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning", 255 (code >= 0 && code < SYS_MAXSYSCALL) ? ia32_syscallnames[code] : "???"); 256 mtx_assert(&sched_lock, MA_NOTOWNED); 257 mtx_assert(&Giant, MA_NOTOWNED); 258} 259 260 261static void 262ia32_syscall_enable(void *dummy) 263{ 264 265 setidt(0x80, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0); 266} 267 268static void 269ia32_syscall_disable(void *dummy) 270{ 271 272 setidt(0x80, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0); 273} 274 275SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL); 276SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL); 277