Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/amd64/ia32/ia32_syscall.c
39507 views
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
/*
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
49
#include <sys/param.h>
50
#include <sys/bus.h>
51
#include <sys/systm.h>
52
#include <sys/proc.h>
53
#include <sys/kernel.h>
54
#include <sys/ktr.h>
55
#include <sys/lock.h>
56
#include <sys/msan.h>
57
#include <sys/mutex.h>
58
#include <sys/proc.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
#include <security/audit/audit.h>
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/intr_machdep.h>
79
#include <machine/md_var.h>
80
81
#include <compat/freebsd32/freebsd32_signal.h>
82
#include <compat/freebsd32/freebsd32_util.h>
83
#include <compat/ia32/ia32_signal.h>
84
#include <machine/psl.h>
85
#include <machine/segments.h>
86
#include <machine/specialreg.h>
87
#include <machine/sysarch.h>
88
#include <machine/frame.h>
89
#include <machine/md_var.h>
90
#include <machine/pcb.h>
91
#include <machine/cpufunc.h>
92
93
#include "vdso_ia32_offsets.h"
94
95
extern const char _binary_elf_vdso32_so_1_start[];
96
extern const char _binary_elf_vdso32_so_1_end[];
97
extern char _binary_elf_vdso32_so_1_size;
98
99
#define IDTVEC(name) __CONCAT(X,name)
100
101
extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(int0x80_syscall_pti),
102
IDTVEC(rsvd), IDTVEC(rsvd_pti);
103
104
void ia32_syscall(struct trapframe *frame); /* Called from asm code */
105
106
void
107
ia32_set_syscall_retval(struct thread *td, int error)
108
{
109
110
cpu_set_syscall_retval(td, error);
111
}
112
113
int
114
ia32_fetch_syscall_args(struct thread *td)
115
{
116
struct proc *p;
117
struct trapframe *frame;
118
struct syscall_args *sa;
119
caddr_t params;
120
u_int32_t args[8], tmp;
121
int error, i;
122
#ifdef COMPAT_43
123
u_int32_t eip;
124
int cs;
125
#endif
126
127
p = td->td_proc;
128
frame = td->td_frame;
129
sa = &td->td_sa;
130
131
#ifdef COMPAT_43
132
if (__predict_false(frame->tf_cs == 7 && frame->tf_rip == 2)) {
133
/*
134
* In lcall $7,$0 after int $0x80. Convert the user
135
* frame to what it would be for a direct int 0x80 instead
136
* of lcall $7,$0, by popping the lcall return address.
137
*/
138
error = fueword32((void *)frame->tf_rsp, &eip);
139
if (error == -1)
140
return (EFAULT);
141
cs = fuword16((void *)(frame->tf_rsp + sizeof(u_int32_t)));
142
if (cs == -1)
143
return (EFAULT);
144
145
/*
146
* Unwind in-kernel frame after all stack frame pieces
147
* were successfully read.
148
*/
149
frame->tf_rip = eip;
150
frame->tf_cs = cs;
151
frame->tf_rsp += 2 * sizeof(u_int32_t);
152
frame->tf_err = 7; /* size of lcall $7,$0 */
153
}
154
#endif
155
156
params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
157
sa->code = frame->tf_rax;
158
sa->original_code = sa->code;
159
160
/*
161
* Need to check if this is a 32 bit or 64 bit syscall.
162
*/
163
if (sa->code == SYS_syscall) {
164
/*
165
* Code is first argument, followed by actual args.
166
*/
167
error = fueword32(params, &tmp);
168
if (error == -1)
169
return (EFAULT);
170
sa->code = tmp;
171
params += sizeof(int);
172
} else if (sa->code == SYS___syscall) {
173
/*
174
* Like syscall, but code is a quad, so as to maintain
175
* quad alignment for the rest of the arguments.
176
* We use a 32-bit fetch in case params is not
177
* aligned.
178
*/
179
error = fueword32(params, &tmp);
180
if (error == -1)
181
return (EFAULT);
182
sa->code = tmp;
183
params += sizeof(quad_t);
184
}
185
if (sa->code >= p->p_sysent->sv_size)
186
sa->callp = &nosys_sysent;
187
else
188
sa->callp = &p->p_sysent->sv_table[sa->code];
189
190
if (params != NULL && sa->callp->sy_narg != 0)
191
error = copyin(params, (caddr_t)args,
192
(u_int)(sa->callp->sy_narg * sizeof(int)));
193
else
194
error = 0;
195
196
for (i = 0; i < sa->callp->sy_narg; i++)
197
sa->args[i] = args[i];
198
199
if (error == 0) {
200
td->td_retval[0] = 0;
201
td->td_retval[1] = frame->tf_rdx;
202
}
203
204
return (error);
205
}
206
207
#include "../../kern/subr_syscall.c"
208
209
void
210
ia32_syscall(struct trapframe *frame)
211
{
212
struct thread *td;
213
register_t orig_tf_rflags;
214
ksiginfo_t ksi;
215
216
kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
217
218
orig_tf_rflags = frame->tf_rflags;
219
td = curthread;
220
td->td_frame = frame;
221
222
syscallenter(td);
223
224
/*
225
* Traced syscall.
226
*/
227
if (orig_tf_rflags & PSL_T) {
228
frame->tf_rflags &= ~PSL_T;
229
ksiginfo_init_trap(&ksi);
230
ksi.ksi_signo = SIGTRAP;
231
ksi.ksi_code = TRAP_TRACE;
232
ksi.ksi_addr = (void *)frame->tf_rip;
233
trapsignal(td, &ksi);
234
}
235
236
syscallret(td);
237
amd64_syscall_ret_flush_l1d(td->td_errno);
238
}
239
240
static void
241
ia32_syscall_enable(void *dummy)
242
{
243
244
setidt(IDT_SYSCALL, pti ? &IDTVEC(int0x80_syscall_pti) :
245
&IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
246
}
247
248
static void
249
ia32_syscall_disable(void *dummy)
250
{
251
252
setidt(IDT_SYSCALL, pti ? &IDTVEC(rsvd_pti) : &IDTVEC(rsvd),
253
SDT_SYSIGT, SEL_KPL, 0);
254
}
255
256
SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
257
SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
258
259
#ifdef COMPAT_43
260
int
261
setup_lcall_gate(void)
262
{
263
struct i386_ldt_args uap;
264
struct user_segment_descriptor desc;
265
uint32_t lcall_addr;
266
int error;
267
268
bzero(&uap, sizeof(uap));
269
uap.start = 0;
270
uap.num = 1;
271
lcall_addr = PROC_PS_STRINGS(curproc) -
272
(_binary_elf_vdso32_so_1_end - _binary_elf_vdso32_so_1_start) +
273
VDSO_LCALL_TRAMP_OFFSET;
274
bzero(&desc, sizeof(desc));
275
desc.sd_type = SDT_MEMERA;
276
desc.sd_dpl = SEL_UPL;
277
desc.sd_p = 1;
278
desc.sd_def32 = 1;
279
desc.sd_gran = 1;
280
desc.sd_lolimit = 0xffff;
281
desc.sd_hilimit = 0xf;
282
desc.sd_lobase = lcall_addr;
283
desc.sd_hibase = lcall_addr >> 24;
284
error = amd64_set_ldt(curthread, &uap, &desc);
285
if (error != 0)
286
return (error);
287
288
return (0);
289
}
290
#endif
291
292