ia32_syscall.c revision 165303
1114987Speter/*- 2114987Speter * Copyright (C) 1994, David Greenman 3114987Speter * Copyright (c) 1990, 1993 4114987Speter * The Regents of the University of California. All rights reserved. 5114987Speter * 6114987Speter * This code is derived from software contributed to Berkeley by 7114987Speter * the University of Utah, and William Jolitz. 8114987Speter * 9114987Speter * Redistribution and use in source and binary forms, with or without 10114987Speter * modification, are permitted provided that the following conditions 11114987Speter * are met: 12114987Speter * 1. Redistributions of source code must retain the above copyright 13114987Speter * notice, this list of conditions and the following disclaimer. 14114987Speter * 2. Redistributions in binary form must reproduce the above copyright 15114987Speter * notice, this list of conditions and the following disclaimer in the 16114987Speter * documentation and/or other materials provided with the distribution. 17114987Speter * 3. All advertising materials mentioning features or use of this software 18114987Speter * must display the following acknowledgement: 19114987Speter * This product includes software developed by the University of 20114987Speter * California, Berkeley and its contributors. 21114987Speter * 4. Neither the name of the University nor the names of its contributors 22114987Speter * may be used to endorse or promote products derived from this software 23114987Speter * without specific prior written permission. 24114987Speter * 25114987Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26114987Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27114987Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28114987Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29114987Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30114987Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31114987Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32114987Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33114987Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34114987Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35114987Speter * SUCH DAMAGE. 36114987Speter */ 37114987Speter 38118031Sobrien#include <sys/cdefs.h> 39118031Sobrien__FBSDID("$FreeBSD: head/sys/amd64/ia32/ia32_syscall.c 165303 2006-12-17 06:48:40Z kmacy $"); 40118031Sobrien 41114987Speter/* 42114987Speter * 386 Trap and System call handling 43114987Speter */ 44114987Speter 45114987Speter#include "opt_clock.h" 46114987Speter#include "opt_cpu.h" 47114987Speter#include "opt_isa.h" 48114987Speter#include "opt_ktrace.h" 49114987Speter 50114987Speter#include <sys/param.h> 51114987Speter#include <sys/bus.h> 52114987Speter#include <sys/systm.h> 53114987Speter#include <sys/proc.h> 54114987Speter#include <sys/pioctl.h> 55114987Speter#include <sys/kernel.h> 56114987Speter#include <sys/ktr.h> 57114987Speter#include <sys/lock.h> 58114987Speter#include <sys/mutex.h> 59160764Sjhb#include <sys/ptrace.h> 60114987Speter#include <sys/resourcevar.h> 61114987Speter#include <sys/signalvar.h> 62114987Speter#include <sys/syscall.h> 63114987Speter#include <sys/sysctl.h> 64114987Speter#include <sys/sysent.h> 65114987Speter#include <sys/uio.h> 66114987Speter#include <sys/vmmeter.h> 67114987Speter#ifdef KTRACE 68114987Speter#include <sys/ktrace.h> 69114987Speter#endif 70155313Swsalamon#include <security/audit/audit.h> 71114987Speter 72114987Speter#include <vm/vm.h> 73114987Speter#include <vm/vm_param.h> 74114987Speter#include <vm/pmap.h> 75114987Speter#include <vm/vm_kern.h> 76114987Speter#include <vm/vm_map.h> 77114987Speter#include <vm/vm_page.h> 78114987Speter#include <vm/vm_extern.h> 79114987Speter 80114987Speter#include <machine/cpu.h> 81122849Speter#include <machine/intr_machdep.h> 82114987Speter#include <machine/md_var.h> 83114987Speter 84114987Speter#define IDTVEC(name) __CONCAT(X,name) 85114987Speter 86114987Speterextern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd); 87119336Speterextern const char *freebsd32_syscallnames[]; 88114987Speter 89165303Skmacyvoid ia32_syscall(struct trapframe *frame); /* Called from asm code */ 90114987Speter 91114987Spetervoid 92165303Skmacyia32_syscall(struct trapframe *frame) 93114987Speter{ 94114987Speter caddr_t params; 95114987Speter int i; 96114987Speter struct sysent *callp; 97114987Speter struct thread *td = curthread; 98114987Speter struct proc *p = td->td_proc; 99114987Speter register_t orig_tf_rflags; 100114987Speter int error; 101114987Speter int narg; 102114987Speter u_int32_t args[8]; 103114987Speter u_int64_t args64[8]; 104114987Speter u_int code; 105151316Sdavidxu ksiginfo_t ksi; 106114987Speter 107114987Speter /* 108114987Speter * note: PCPU_LAZY_INC() can only be used if we can afford 109114987Speter * occassional inaccuracy in the count. 110114987Speter */ 111144971Sjhb PCPU_LAZY_INC(cnt.v_syscall); 112114987Speter 113155455Sphk td->td_pticks = 0; 114165303Skmacy td->td_frame = frame; 115114987Speter if (td->td_ucred != p->p_ucred) 116114987Speter cred_update_thread(td); 117165303Skmacy params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t); 118165303Skmacy code = frame->tf_rax; 119165303Skmacy orig_tf_rflags = frame->tf_rflags; 120114987Speter 121114987Speter if (p->p_sysent->sv_prepsyscall) { 122114987Speter /* 123114987Speter * The prep code is MP aware. 124114987Speter */ 125165303Skmacy (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms); 126114987Speter } else { 127114987Speter /* 128114987Speter * Need to check if this is a 32 bit or 64 bit syscall. 129114987Speter * fuword is MP aware. 130114987Speter */ 131114987Speter if (code == SYS_syscall) { 132114987Speter /* 133114987Speter * Code is first argument, followed by actual args. 134114987Speter */ 135114987Speter code = fuword32(params); 136114987Speter params += sizeof(int); 137114987Speter } else if (code == SYS___syscall) { 138114987Speter /* 139114987Speter * Like syscall, but code is a quad, so as to maintain 140114987Speter * quad alignment for the rest of the arguments. 141114987Speter * We use a 32-bit fetch in case params is not 142114987Speter * aligned. 143114987Speter */ 144114987Speter code = fuword32(params); 145114987Speter params += sizeof(quad_t); 146114987Speter } 147114987Speter } 148114987Speter 149114987Speter if (p->p_sysent->sv_mask) 150114987Speter code &= p->p_sysent->sv_mask; 151114987Speter 152114987Speter if (code >= p->p_sysent->sv_size) 153114987Speter callp = &p->p_sysent->sv_table[0]; 154114987Speter else 155114987Speter callp = &p->p_sysent->sv_table[code]; 156114987Speter 157160801Sjhb narg = callp->sy_narg; 158114987Speter 159114987Speter /* 160114987Speter * copyin and the ktrsyscall()/ktrsysret() code is MP-aware 161114987Speter */ 162114987Speter if (params != NULL && narg != 0) 163114987Speter error = copyin(params, (caddr_t)args, 164114987Speter (u_int)(narg * sizeof(int))); 165114987Speter else 166114987Speter error = 0; 167114987Speter 168114987Speter for (i = 0; i < narg; i++) 169114987Speter args64[i] = args[i]; 170114987Speter 171114987Speter#ifdef KTRACE 172114987Speter if (KTRPOINT(td, KTR_SYSCALL)) 173114987Speter ktrsyscall(code, narg, args64); 174114987Speter#endif 175160770Sjhb CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td, 176160770Sjhb td->td_proc->p_pid, td->td_proc->p_comm, code); 177160770Sjhb 178114987Speter if (error == 0) { 179114987Speter td->td_retval[0] = 0; 180165303Skmacy td->td_retval[1] = frame->tf_rdx; 181114987Speter 182114987Speter STOPEVENT(p, S_SCE, narg); 183114987Speter 184160764Sjhb PTRACESTOP_SC(p, td, S_PT_SCE); 185160764Sjhb 186155313Swsalamon AUDIT_SYSCALL_ENTER(code, td); 187114987Speter error = (*callp->sy_call)(td, args64); 188155313Swsalamon AUDIT_SYSCALL_EXIT(error, td); 189114987Speter } 190114987Speter 191114987Speter switch (error) { 192114987Speter case 0: 193165303Skmacy frame->tf_rax = td->td_retval[0]; 194165303Skmacy frame->tf_rdx = td->td_retval[1]; 195165303Skmacy frame->tf_rflags &= ~PSL_C; 196114987Speter break; 197114987Speter 198114987Speter case ERESTART: 199114987Speter /* 200114987Speter * Reconstruct pc, assuming lcall $X,y is 7 bytes, 201114987Speter * int 0x80 is 2 bytes. We saved this in tf_err. 202114987Speter */ 203165303Skmacy frame->tf_rip -= frame->tf_err; 204114987Speter break; 205114987Speter 206114987Speter case EJUSTRETURN: 207114987Speter break; 208114987Speter 209114987Speter default: 210114987Speter if (p->p_sysent->sv_errsize) { 211114987Speter if (error >= p->p_sysent->sv_errsize) 212114987Speter error = -1; /* XXX */ 213114987Speter else 214114987Speter error = p->p_sysent->sv_errtbl[error]; 215114987Speter } 216165303Skmacy frame->tf_rax = error; 217165303Skmacy frame->tf_rflags |= PSL_C; 218114987Speter break; 219114987Speter } 220114987Speter 221114987Speter /* 222114987Speter * Traced syscall. 223114987Speter */ 224114987Speter if (orig_tf_rflags & PSL_T) { 225165303Skmacy frame->tf_rflags &= ~PSL_T; 226151316Sdavidxu ksiginfo_init_trap(&ksi); 227151316Sdavidxu ksi.ksi_signo = SIGTRAP; 228151316Sdavidxu ksi.ksi_code = TRAP_TRACE; 229165303Skmacy ksi.ksi_addr = (void *)frame->tf_rip; 230151316Sdavidxu trapsignal(td, &ksi); 231114987Speter } 232114987Speter 233114987Speter /* 234160773Sjhb * Check for misbehavior. 235160773Sjhb */ 236160773Sjhb WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning", 237160773Sjhb (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???"); 238160773Sjhb KASSERT(td->td_critnest == 0, 239160773Sjhb ("System call %s returning in a critical section", 240160773Sjhb (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???")); 241160773Sjhb KASSERT(td->td_locks == 0, 242160773Sjhb ("System call %s returning with %d locks held", 243160773Sjhb (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???", 244160773Sjhb td->td_locks)); 245160773Sjhb 246160773Sjhb /* 247114987Speter * Handle reschedule and other end-of-syscall issues 248114987Speter */ 249165303Skmacy userret(td, frame); 250114987Speter 251160770Sjhb CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td, 252160770Sjhb td->td_proc->p_pid, td->td_proc->p_comm, code); 253114987Speter#ifdef KTRACE 254114987Speter if (KTRPOINT(td, KTR_SYSRET)) 255114987Speter ktrsysret(code, error, td->td_retval[0]); 256114987Speter#endif 257114987Speter 258114987Speter /* 259114987Speter * This works because errno is findable through the 260114987Speter * register set. If we ever support an emulation where this 261114987Speter * is not the case, this code will need to be revisited. 262114987Speter */ 263114987Speter STOPEVENT(p, S_SCX, code); 264160764Sjhb 265160764Sjhb PTRACESTOP_SC(p, td, S_PT_SCX); 266114987Speter} 267114987Speter 268114987Speter 269114987Speterstatic void 270114987Speteria32_syscall_enable(void *dummy) 271114987Speter{ 272114987Speter 273120346Speter setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0); 274114987Speter} 275114987Speter 276114987Speterstatic void 277114987Speteria32_syscall_disable(void *dummy) 278114987Speter{ 279114987Speter 280120346Speter setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0); 281114987Speter} 282114987Speter 283114987SpeterSYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL); 284114987SpeterSYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL); 285