]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/ia32/ia32_syscall.c
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / sys / amd64 / ia32 / ia32_syscall.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1994, David Greenman
5  * Copyright (c) 1990, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the University of Utah, and William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * 386 Trap and System call handling
45  */
46
47 #include "opt_clock.h"
48 #include "opt_compat.h"
49 #include "opt_cpu.h"
50 #include "opt_isa.h"
51
52 #include <sys/param.h>
53 #include <sys/bus.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/pioctl.h>
57 #include <sys/kernel.h>
58 #include <sys/ktr.h>
59 #include <sys/lock.h>
60 #include <sys/mutex.h>
61 #include <sys/proc.h>
62 #include <sys/ptrace.h>
63 #include <sys/resourcevar.h>
64 #include <sys/signalvar.h>
65 #include <sys/syscall.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysent.h>
68 #include <sys/uio.h>
69 #include <sys/vmmeter.h>
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 #include <compat/freebsd32/freebsd32_signal.h>
85 #include <compat/freebsd32/freebsd32_util.h>
86 #include <compat/ia32/ia32_signal.h>
87 #include <machine/psl.h>
88 #include <machine/segments.h>
89 #include <machine/specialreg.h>
90 #include <machine/sysarch.h>
91 #include <machine/frame.h>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
94 #include <machine/cpufunc.h>
95
96 #define IDTVEC(name)    __CONCAT(X,name)
97
98 extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
99
100 void ia32_syscall(struct trapframe *frame);     /* Called from asm code */
101
102 void
103 ia32_set_syscall_retval(struct thread *td, int error)
104 {
105
106         cpu_set_syscall_retval(td, error);
107 }
108
109 int
110 ia32_fetch_syscall_args(struct thread *td)
111 {
112         struct proc *p;
113         struct trapframe *frame;
114         struct syscall_args *sa;
115         caddr_t params;
116         u_int32_t args[8], tmp;
117         int error, i;
118
119         p = td->td_proc;
120         frame = td->td_frame;
121         sa = &td->td_sa;
122
123         params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
124         sa->code = frame->tf_rax;
125
126         /*
127          * Need to check if this is a 32 bit or 64 bit syscall.
128          */
129         if (sa->code == SYS_syscall) {
130                 /*
131                  * Code is first argument, followed by actual args.
132                  */
133                 error = fueword32(params, &tmp);
134                 if (error == -1)
135                         return (EFAULT);
136                 sa->code = tmp;
137                 params += sizeof(int);
138         } else if (sa->code == SYS___syscall) {
139                 /*
140                  * Like syscall, but code is a quad, so as to maintain
141                  * quad alignment for the rest of the arguments.
142                  * We use a 32-bit fetch in case params is not
143                  * aligned.
144                  */
145                 error = fueword32(params, &tmp);
146                 if (error == -1)
147                         return (EFAULT);
148                 sa->code = tmp;
149                 params += sizeof(quad_t);
150         }
151         if (p->p_sysent->sv_mask)
152                 sa->code &= p->p_sysent->sv_mask;
153         if (sa->code >= p->p_sysent->sv_size)
154                 sa->callp = &p->p_sysent->sv_table[0];
155         else
156                 sa->callp = &p->p_sysent->sv_table[sa->code];
157         sa->narg = sa->callp->sy_narg;
158
159         if (params != NULL && sa->narg != 0)
160                 error = copyin(params, (caddr_t)args,
161                     (u_int)(sa->narg * sizeof(int)));
162         else
163                 error = 0;
164
165         for (i = 0; i < sa->narg; i++)
166                 sa->args[i] = args[i];
167
168         if (error == 0) {
169                 td->td_retval[0] = 0;
170                 td->td_retval[1] = frame->tf_rdx;
171         }
172
173         return (error);
174 }
175
176 #include "../../kern/subr_syscall.c"
177
178 void
179 ia32_syscall(struct trapframe *frame)
180 {
181         struct thread *td;
182         register_t orig_tf_rflags;
183         int error;
184         ksiginfo_t ksi;
185
186         orig_tf_rflags = frame->tf_rflags;
187         td = curthread;
188         td->td_frame = frame;
189
190         error = syscallenter(td);
191
192         /*
193          * Traced syscall.
194          */
195         if (orig_tf_rflags & PSL_T) {
196                 frame->tf_rflags &= ~PSL_T;
197                 ksiginfo_init_trap(&ksi);
198                 ksi.ksi_signo = SIGTRAP;
199                 ksi.ksi_code = TRAP_TRACE;
200                 ksi.ksi_addr = (void *)frame->tf_rip;
201                 trapsignal(td, &ksi);
202         }
203
204         syscallret(td, error);
205 }
206
207 static void
208 ia32_syscall_enable(void *dummy)
209 {
210
211         setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
212 }
213
214 static void
215 ia32_syscall_disable(void *dummy)
216 {
217
218         setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
219 }
220
221 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
222 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
223
224 #ifdef COMPAT_43
225 int
226 setup_lcall_gate(void)
227 {
228         struct i386_ldt_args uap;
229         struct user_segment_descriptor desc;
230         uint32_t lcall_addr;
231         int error;
232
233         bzero(&uap, sizeof(uap));
234         uap.start = 0;
235         uap.num = 1;
236         lcall_addr = curproc->p_sysent->sv_psstrings - sz_lcall_tramp;
237         bzero(&desc, sizeof(desc));
238         desc.sd_type = SDT_MEMERA;
239         desc.sd_dpl = SEL_UPL;
240         desc.sd_p = 1;
241         desc.sd_def32 = 1;
242         desc.sd_gran = 1;
243         desc.sd_lolimit = 0xffff;
244         desc.sd_hilimit = 0xf;
245         desc.sd_lobase = lcall_addr;
246         desc.sd_hibase = lcall_addr >> 24;
247         error = amd64_set_ldt(curthread, &uap, &desc);
248         if (error != 0)
249                 return (error);
250
251         return (0);
252 }
253 #endif