]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/amd64/ia32/ia32_syscall.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / amd64 / ia32 / ia32_syscall.c
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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * 386 Trap and System call handling
43  */
44
45 #include "opt_clock.h"
46 #include "opt_cpu.h"
47 #include "opt_isa.h"
48 #include "opt_ktrace.h"
49
50 #include <sys/param.h>
51 #include <sys/bus.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/pioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/ptrace.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscall.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/uio.h>
66 #include <sys/vmmeter.h>
67 #ifdef KTRACE
68 #include <sys/ktrace.h>
69 #endif
70 #include <security/audit/audit.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_param.h>
74 #include <vm/pmap.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_extern.h>
79
80 #include <machine/cpu.h>
81 #include <machine/intr_machdep.h>
82 #include <machine/md_var.h>
83
84 #define IDTVEC(name)    __CONCAT(X,name)
85
86 extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
87 extern const char *freebsd32_syscallnames[];
88
89 void ia32_syscall(struct trapframe *frame);     /* Called from asm code */
90
91 void
92 ia32_syscall(struct trapframe *frame)
93 {
94         caddr_t params;
95         int i;
96         struct sysent *callp;
97         struct thread *td = curthread;
98         struct proc *p = td->td_proc;
99         register_t orig_tf_rflags;
100         int error;
101         int narg;
102         u_int32_t args[8];
103         u_int64_t args64[8];
104         u_int code;
105         ksiginfo_t ksi;
106
107         PCPU_INC(cnt.v_syscall);
108         td->td_pticks = 0;
109         td->td_frame = frame;
110         if (td->td_ucred != p->p_ucred) 
111                 cred_update_thread(td);
112         params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
113         code = frame->tf_rax;
114         orig_tf_rflags = frame->tf_rflags;
115
116         if (p->p_sysent->sv_prepsyscall) {
117                 /*
118                  * The prep code is MP aware.
119                  */
120                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
121         } else {
122                 /*
123                  * Need to check if this is a 32 bit or 64 bit syscall.
124                  * fuword is MP aware.
125                  */
126                 if (code == SYS_syscall) {
127                         /*
128                          * Code is first argument, followed by actual args.
129                          */
130                         code = fuword32(params);
131                         params += sizeof(int);
132                 } else if (code == SYS___syscall) {
133                         /*
134                          * Like syscall, but code is a quad, so as to maintain
135                          * quad alignment for the rest of the arguments.
136                          * We use a 32-bit fetch in case params is not
137                          * aligned.
138                          */
139                         code = fuword32(params);
140                         params += sizeof(quad_t);
141                 }
142         }
143
144         if (p->p_sysent->sv_mask)
145                 code &= p->p_sysent->sv_mask;
146
147         if (code >= p->p_sysent->sv_size)
148                 callp = &p->p_sysent->sv_table[0];
149         else
150                 callp = &p->p_sysent->sv_table[code];
151
152         narg = callp->sy_narg;
153
154         /*
155          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
156          */
157         if (params != NULL && narg != 0)
158                 error = copyin(params, (caddr_t)args,
159                     (u_int)(narg * sizeof(int)));
160         else
161                 error = 0;
162
163         for (i = 0; i < narg; i++)
164                 args64[i] = args[i];
165
166 #ifdef KTRACE
167         if (KTRPOINT(td, KTR_SYSCALL))
168                 ktrsyscall(code, narg, args64);
169 #endif
170         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
171             td->td_proc->p_pid, td->td_proc->p_comm, code);
172
173         if (error == 0) {
174                 td->td_retval[0] = 0;
175                 td->td_retval[1] = frame->tf_rdx;
176
177                 STOPEVENT(p, S_SCE, narg);
178
179                 PTRACESTOP_SC(p, td, S_PT_SCE);
180
181                 AUDIT_SYSCALL_ENTER(code, td);
182                 error = (*callp->sy_call)(td, args64);
183                 AUDIT_SYSCALL_EXIT(error, td);
184         }
185
186         switch (error) {
187         case 0:
188                 frame->tf_rax = td->td_retval[0];
189                 frame->tf_rdx = td->td_retval[1];
190                 frame->tf_rflags &= ~PSL_C;
191                 break;
192
193         case ERESTART:
194                 /*
195                  * Reconstruct pc, assuming lcall $X,y is 7 bytes,
196                  * int 0x80 is 2 bytes. We saved this in tf_err.
197                  */
198                 frame->tf_rip -= frame->tf_err;
199                 break;
200
201         case EJUSTRETURN:
202                 break;
203
204         default:
205                 if (p->p_sysent->sv_errsize) {
206                         if (error >= p->p_sysent->sv_errsize)
207                                 error = -1;     /* XXX */
208                         else
209                                 error = p->p_sysent->sv_errtbl[error];
210                 }
211                 frame->tf_rax = error;
212                 frame->tf_rflags |= PSL_C;
213                 break;
214         }
215
216         /*
217          * Traced syscall.
218          */
219         if (orig_tf_rflags & PSL_T) {
220                 frame->tf_rflags &= ~PSL_T;
221                 ksiginfo_init_trap(&ksi);
222                 ksi.ksi_signo = SIGTRAP;
223                 ksi.ksi_code = TRAP_TRACE;
224                 ksi.ksi_addr = (void *)frame->tf_rip;
225                 trapsignal(td, &ksi);
226         }
227
228         /*
229          * Check for misbehavior.
230          */
231         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
232             (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???");
233         KASSERT(td->td_critnest == 0,
234             ("System call %s returning in a critical section",
235             (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???"));
236         KASSERT(td->td_locks == 0,
237             ("System call %s returning with %d locks held",
238             (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???",
239             td->td_locks));
240
241         /*
242          * Handle reschedule and other end-of-syscall issues
243          */
244         userret(td, frame);
245
246         CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
247             td->td_proc->p_pid, td->td_proc->p_comm, code);
248 #ifdef KTRACE
249         if (KTRPOINT(td, KTR_SYSRET))
250                 ktrsysret(code, error, td->td_retval[0]);
251 #endif
252
253         /*
254          * This works because errno is findable through the
255          * register set.  If we ever support an emulation where this
256          * is not the case, this code will need to be revisited.
257          */
258         STOPEVENT(p, S_SCX, code);
259  
260         PTRACESTOP_SC(p, td, S_PT_SCX);
261 }
262
263
264 static void
265 ia32_syscall_enable(void *dummy)
266 {
267
268         setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
269 }
270
271 static void
272 ia32_syscall_disable(void *dummy)
273 {
274
275         setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
276 }
277
278 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
279 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);