Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/i386/linux/linux_sysvec.c
39536 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 1994-1996 Søren Schmidt
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
#define __ELF_WORD_SIZE 32
30
31
#include <sys/param.h>
32
#include <sys/exec.h>
33
#include <sys/fcntl.h>
34
#include <sys/imgact.h>
35
#include <sys/imgact_aout.h>
36
#include <sys/imgact_elf.h>
37
#include <sys/kernel.h>
38
#include <sys/lock.h>
39
#include <sys/malloc.h>
40
#include <sys/module.h>
41
#include <sys/mutex.h>
42
#include <sys/proc.h>
43
#include <sys/stddef.h>
44
#include <sys/syscallsubr.h>
45
#include <sys/sysctl.h>
46
#include <sys/sysent.h>
47
#include <sys/sysproto.h>
48
49
#include <vm/pmap.h>
50
#include <vm/vm.h>
51
#include <vm/vm_map.h>
52
#include <vm/vm_page.h>
53
54
#include <machine/cpu.h>
55
#include <machine/cputypes.h>
56
#include <machine/md_var.h>
57
#include <machine/pcb.h>
58
#include <machine/trap.h>
59
60
#include <x86/linux/linux_x86.h>
61
#include <i386/linux/linux.h>
62
#include <i386/linux/linux_proto.h>
63
#include <compat/linux/linux_elf.h>
64
#include <compat/linux/linux_emul.h>
65
#include <compat/linux/linux_fork.h>
66
#include <compat/linux/linux_ioctl.h>
67
#include <compat/linux/linux_mib.h>
68
#include <compat/linux/linux_misc.h>
69
#include <compat/linux/linux_signal.h>
70
#include <compat/linux/linux_util.h>
71
#include <compat/linux/linux_vdso.h>
72
73
#include <x86/linux/linux_x86_sigframe.h>
74
75
MODULE_VERSION(linux, 1);
76
77
#define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
78
#define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
79
#define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE)
80
/*
81
* PAGE_SIZE - the size
82
* of the native SHAREDPAGE
83
*/
84
#define LINUX_USRSTACK LINUX_SHAREDPAGE
85
#define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
86
87
static int linux_szsigcode;
88
static vm_object_t linux_vdso_obj;
89
static char *linux_vdso_mapping;
90
extern char _binary_linux_vdso_so_o_start;
91
extern char _binary_linux_vdso_so_o_end;
92
static vm_offset_t linux_vdso_base;
93
94
extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
95
extern const char *linux_syscallnames[];
96
97
SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
98
99
static int linux_fixup(uintptr_t *stack_base,
100
struct image_params *iparams);
101
static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
102
static void linux_exec_setregs(struct thread *td,
103
struct image_params *imgp, uintptr_t stack);
104
static void linux_exec_sysvec_init(void *param);
105
static int linux_on_exec_vmspace(struct proc *p,
106
struct image_params *imgp);
107
static void linux_set_fork_retval(struct thread *td);
108
static void linux_vdso_install(const void *param);
109
static void linux_vdso_deinstall(const void *param);
110
static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
111
112
LINUX_VDSO_SYM_CHAR(linux_platform);
113
LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
114
LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
115
LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
116
LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
117
LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
118
LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
119
120
static int
121
linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
122
{
123
register_t *base, *argv, *envp;
124
125
base = (register_t *)*stack_base;
126
argv = base;
127
envp = base + (imgp->args->argc + 1);
128
base--;
129
if (suword(base, (intptr_t)envp) != 0)
130
return (EFAULT);
131
base--;
132
if (suword(base, (intptr_t)argv) != 0)
133
return (EFAULT);
134
base--;
135
if (suword(base, imgp->args->argc) != 0)
136
return (EFAULT);
137
*stack_base = (uintptr_t)base;
138
return (0);
139
}
140
141
void
142
linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
143
{
144
145
AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
146
AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall);
147
AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
148
AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
149
AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
150
}
151
152
static void
153
linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
154
{
155
struct thread *td = curthread;
156
struct proc *p = td->td_proc;
157
struct sigacts *psp;
158
struct trapframe *regs;
159
struct l_rt_sigframe *fp, frame;
160
int sig, code;
161
int oonstack;
162
163
sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
164
code = ksi->ksi_code;
165
PROC_LOCK_ASSERT(p, MA_OWNED);
166
psp = p->p_sigacts;
167
mtx_assert(&psp->ps_mtx, MA_OWNED);
168
regs = td->td_frame;
169
oonstack = sigonstack(regs->tf_esp);
170
171
/* Allocate space for the signal handler context. */
172
if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
173
SIGISMEMBER(psp->ps_sigonstack, sig)) {
174
fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
175
td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
176
} else
177
fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
178
mtx_unlock(&psp->ps_mtx);
179
180
/* Build the argument list for the signal handler. */
181
sig = bsd_to_linux_signal(sig);
182
183
bzero(&frame, sizeof(frame));
184
185
frame.sf_sig = sig;
186
frame.sf_siginfo = PTROUT(&fp->sf_si);
187
frame.sf_ucontext = PTROUT(&fp->sf_uc);
188
189
/* Fill in POSIX parts. */
190
siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
191
192
/* Build the signal context to be used by sigreturn. */
193
frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
194
frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
195
frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
196
? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
197
PROC_UNLOCK(p);
198
199
bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
200
201
frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask;
202
frame.sf_uc.uc_mcontext.sc_gs = rgs();
203
frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs;
204
frame.sf_uc.uc_mcontext.sc_es = regs->tf_es;
205
frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds;
206
frame.sf_uc.uc_mcontext.sc_edi = regs->tf_edi;
207
frame.sf_uc.uc_mcontext.sc_esi = regs->tf_esi;
208
frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_ebp;
209
frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_ebx;
210
frame.sf_uc.uc_mcontext.sc_esp = regs->tf_esp;
211
frame.sf_uc.uc_mcontext.sc_edx = regs->tf_edx;
212
frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_ecx;
213
frame.sf_uc.uc_mcontext.sc_eax = regs->tf_eax;
214
frame.sf_uc.uc_mcontext.sc_eip = regs->tf_eip;
215
frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
216
frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
217
frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
218
frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss;
219
frame.sf_uc.uc_mcontext.sc_err = regs->tf_err;
220
frame.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
221
frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
222
223
if (copyout(&frame, fp, sizeof(frame)) != 0) {
224
/*
225
* Process has trashed its stack; give it an illegal
226
* instruction to halt it in its tracks.
227
*/
228
PROC_LOCK(p);
229
sigexit(td, SIGILL);
230
}
231
232
/* Build context to run handler in. */
233
regs->tf_esp = PTROUT(fp);
234
regs->tf_eip = linux_vdso_rt_sigcode;
235
regs->tf_edi = PTROUT(catcher);
236
regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
237
regs->tf_cs = _ucodesel;
238
regs->tf_ds = _udatasel;
239
regs->tf_es = _udatasel;
240
regs->tf_fs = _udatasel;
241
regs->tf_ss = _udatasel;
242
PROC_LOCK(p);
243
mtx_lock(&psp->ps_mtx);
244
}
245
246
/*
247
* Send an interrupt to process.
248
*
249
* Stack is set up to allow sigcode stored
250
* in u. to call routine, followed by kcall
251
* to sigreturn routine below. After sigreturn
252
* resets the signal mask, the stack, and the
253
* frame pointer, it returns to the user
254
* specified pc, psl.
255
*/
256
static void
257
linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
258
{
259
struct thread *td = curthread;
260
struct proc *p = td->td_proc;
261
struct sigacts *psp;
262
struct trapframe *regs;
263
struct l_sigframe *fp, frame;
264
l_sigset_t lmask;
265
int sig;
266
int oonstack;
267
268
PROC_LOCK_ASSERT(p, MA_OWNED);
269
psp = p->p_sigacts;
270
sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
271
mtx_assert(&psp->ps_mtx, MA_OWNED);
272
if (SIGISMEMBER(psp->ps_siginfo, sig)) {
273
/* Signal handler installed with SA_SIGINFO. */
274
linux_rt_sendsig(catcher, ksi, mask);
275
return;
276
}
277
regs = td->td_frame;
278
oonstack = sigonstack(regs->tf_esp);
279
280
/* Allocate space for the signal handler context. */
281
if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
282
SIGISMEMBER(psp->ps_sigonstack, sig)) {
283
fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
284
td->td_sigstk.ss_size - sizeof(struct l_sigframe));
285
} else
286
fp = (struct l_sigframe *)regs->tf_esp - 1;
287
mtx_unlock(&psp->ps_mtx);
288
PROC_UNLOCK(p);
289
290
/* Build the argument list for the signal handler. */
291
sig = bsd_to_linux_signal(sig);
292
293
bzero(&frame, sizeof(frame));
294
295
frame.sf_sig = sig;
296
frame.sf_sigmask = *mask;
297
bsd_to_linux_sigset(mask, &lmask);
298
299
/* Build the signal context to be used by sigreturn. */
300
frame.sf_sc.sc_mask = lmask.__mask;
301
frame.sf_sc.sc_gs = rgs();
302
frame.sf_sc.sc_fs = regs->tf_fs;
303
frame.sf_sc.sc_es = regs->tf_es;
304
frame.sf_sc.sc_ds = regs->tf_ds;
305
frame.sf_sc.sc_edi = regs->tf_edi;
306
frame.sf_sc.sc_esi = regs->tf_esi;
307
frame.sf_sc.sc_ebp = regs->tf_ebp;
308
frame.sf_sc.sc_ebx = regs->tf_ebx;
309
frame.sf_sc.sc_esp = regs->tf_esp;
310
frame.sf_sc.sc_edx = regs->tf_edx;
311
frame.sf_sc.sc_ecx = regs->tf_ecx;
312
frame.sf_sc.sc_eax = regs->tf_eax;
313
frame.sf_sc.sc_eip = regs->tf_eip;
314
frame.sf_sc.sc_cs = regs->tf_cs;
315
frame.sf_sc.sc_eflags = regs->tf_eflags;
316
frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
317
frame.sf_sc.sc_ss = regs->tf_ss;
318
frame.sf_sc.sc_err = regs->tf_err;
319
frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
320
frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
321
322
if (copyout(&frame, fp, sizeof(frame)) != 0) {
323
/*
324
* Process has trashed its stack; give it an illegal
325
* instruction to halt it in its tracks.
326
*/
327
PROC_LOCK(p);
328
sigexit(td, SIGILL);
329
}
330
331
/* Build context to run handler in. */
332
regs->tf_esp = PTROUT(fp);
333
regs->tf_eip = linux_vdso_sigcode;
334
regs->tf_edi = PTROUT(catcher);
335
regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
336
regs->tf_cs = _ucodesel;
337
regs->tf_ds = _udatasel;
338
regs->tf_es = _udatasel;
339
regs->tf_fs = _udatasel;
340
regs->tf_ss = _udatasel;
341
PROC_LOCK(p);
342
mtx_lock(&psp->ps_mtx);
343
}
344
345
/*
346
* System call to cleanup state after a signal
347
* has been taken. Reset signal mask and
348
* stack state from context left by sendsig (above).
349
* Return to previous pc and psl as specified by
350
* context left by sendsig. Check carefully to
351
* make sure that the user has not modified the
352
* psl to gain improper privileges or to cause
353
* a machine fault.
354
*/
355
int
356
linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
357
{
358
struct l_sigframe frame;
359
struct trapframe *regs;
360
int eflags;
361
ksiginfo_t ksi;
362
363
regs = td->td_frame;
364
365
/*
366
* The trampoline code hands us the sigframe.
367
* It is unsafe to keep track of it ourselves, in the event that a
368
* program jumps out of a signal handler.
369
*/
370
if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
371
return (EFAULT);
372
373
/* Check for security violations. */
374
#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
375
eflags = frame.sf_sc.sc_eflags;
376
if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
377
return (EINVAL);
378
379
/*
380
* Don't allow users to load a valid privileged %cs. Let the
381
* hardware check for invalid selectors, excess privilege in
382
* other selectors, invalid %eip's and invalid %esp's.
383
*/
384
#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
385
if (!CS_SECURE(frame.sf_sc.sc_cs)) {
386
ksiginfo_init_trap(&ksi);
387
ksi.ksi_signo = SIGBUS;
388
ksi.ksi_code = BUS_OBJERR;
389
ksi.ksi_trapno = T_PROTFLT;
390
ksi.ksi_addr = (void *)regs->tf_eip;
391
trapsignal(td, &ksi);
392
return (EINVAL);
393
}
394
395
kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
396
397
/* Restore signal context. */
398
/* %gs was restored by the trampoline. */
399
regs->tf_fs = frame.sf_sc.sc_fs;
400
regs->tf_es = frame.sf_sc.sc_es;
401
regs->tf_ds = frame.sf_sc.sc_ds;
402
regs->tf_edi = frame.sf_sc.sc_edi;
403
regs->tf_esi = frame.sf_sc.sc_esi;
404
regs->tf_ebp = frame.sf_sc.sc_ebp;
405
regs->tf_ebx = frame.sf_sc.sc_ebx;
406
regs->tf_edx = frame.sf_sc.sc_edx;
407
regs->tf_ecx = frame.sf_sc.sc_ecx;
408
regs->tf_eax = frame.sf_sc.sc_eax;
409
regs->tf_eip = frame.sf_sc.sc_eip;
410
regs->tf_cs = frame.sf_sc.sc_cs;
411
regs->tf_eflags = eflags;
412
regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
413
regs->tf_ss = frame.sf_sc.sc_ss;
414
415
return (EJUSTRETURN);
416
}
417
418
/*
419
* System call to cleanup state after a signal
420
* has been taken. Reset signal mask and
421
* stack state from context left by rt_sendsig (above).
422
* Return to previous pc and psl as specified by
423
* context left by sendsig. Check carefully to
424
* make sure that the user has not modified the
425
* psl to gain improper privileges or to cause
426
* a machine fault.
427
*/
428
int
429
linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
430
{
431
struct l_ucontext uc;
432
struct l_sigcontext *context;
433
sigset_t bmask;
434
l_stack_t *lss;
435
stack_t ss;
436
struct trapframe *regs;
437
int eflags;
438
ksiginfo_t ksi;
439
440
regs = td->td_frame;
441
442
/*
443
* The trampoline code hands us the ucontext.
444
* It is unsafe to keep track of it ourselves, in the event that a
445
* program jumps out of a signal handler.
446
*/
447
if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
448
return (EFAULT);
449
450
context = &uc.uc_mcontext;
451
452
/* Check for security violations. */
453
#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
454
eflags = context->sc_eflags;
455
if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
456
return (EINVAL);
457
458
/*
459
* Don't allow users to load a valid privileged %cs. Let the
460
* hardware check for invalid selectors, excess privilege in
461
* other selectors, invalid %eip's and invalid %esp's.
462
*/
463
#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
464
if (!CS_SECURE(context->sc_cs)) {
465
ksiginfo_init_trap(&ksi);
466
ksi.ksi_signo = SIGBUS;
467
ksi.ksi_code = BUS_OBJERR;
468
ksi.ksi_trapno = T_PROTFLT;
469
ksi.ksi_addr = (void *)regs->tf_eip;
470
trapsignal(td, &ksi);
471
return (EINVAL);
472
}
473
474
linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
475
kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
476
477
/* Restore signal context. */
478
/* %gs was restored by the trampoline. */
479
regs->tf_fs = context->sc_fs;
480
regs->tf_es = context->sc_es;
481
regs->tf_ds = context->sc_ds;
482
regs->tf_edi = context->sc_edi;
483
regs->tf_esi = context->sc_esi;
484
regs->tf_ebp = context->sc_ebp;
485
regs->tf_ebx = context->sc_ebx;
486
regs->tf_edx = context->sc_edx;
487
regs->tf_ecx = context->sc_ecx;
488
regs->tf_eax = context->sc_eax;
489
regs->tf_eip = context->sc_eip;
490
regs->tf_cs = context->sc_cs;
491
regs->tf_eflags = eflags;
492
regs->tf_esp = context->sc_esp_at_signal;
493
regs->tf_ss = context->sc_ss;
494
495
/* Call sigaltstack & ignore results. */
496
lss = &uc.uc_stack;
497
ss.ss_sp = PTRIN(lss->ss_sp);
498
ss.ss_size = lss->ss_size;
499
ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
500
501
(void)kern_sigaltstack(td, &ss, NULL);
502
503
return (EJUSTRETURN);
504
}
505
506
static int
507
linux_fetch_syscall_args(struct thread *td)
508
{
509
struct proc *p;
510
struct trapframe *frame;
511
struct syscall_args *sa;
512
513
p = td->td_proc;
514
frame = td->td_frame;
515
sa = &td->td_sa;
516
517
sa->code = frame->tf_eax;
518
sa->original_code = sa->code;
519
sa->args[0] = frame->tf_ebx;
520
sa->args[1] = frame->tf_ecx;
521
sa->args[2] = frame->tf_edx;
522
sa->args[3] = frame->tf_esi;
523
sa->args[4] = frame->tf_edi;
524
sa->args[5] = frame->tf_ebp;
525
526
if (sa->code >= p->p_sysent->sv_size)
527
/* nosys */
528
sa->callp = &nosys_sysent;
529
else
530
sa->callp = &p->p_sysent->sv_table[sa->code];
531
532
td->td_retval[0] = 0;
533
td->td_retval[1] = frame->tf_edx;
534
535
return (0);
536
}
537
538
static void
539
linux_set_syscall_retval(struct thread *td, int error)
540
{
541
struct trapframe *frame = td->td_frame;
542
543
cpu_set_syscall_retval(td, error);
544
545
if (__predict_false(error != 0)) {
546
if (error != ERESTART && error != EJUSTRETURN)
547
frame->tf_eax = bsd_to_linux_errno(error);
548
}
549
}
550
551
static void
552
linux_set_fork_retval(struct thread *td)
553
{
554
struct trapframe *frame = td->td_frame;
555
556
frame->tf_eax = 0;
557
}
558
559
/*
560
* exec_setregs may initialize some registers differently than Linux
561
* does, thus potentially confusing Linux binaries. If necessary, we
562
* override the exec_setregs default(s) here.
563
*/
564
static void
565
linux_exec_setregs(struct thread *td, struct image_params *imgp,
566
uintptr_t stack)
567
{
568
struct pcb *pcb = td->td_pcb;
569
570
exec_setregs(td, imgp, stack);
571
572
/* Linux sets %gs to 0, we default to _udatasel. */
573
pcb->pcb_gs = 0;
574
load_gs(0);
575
576
pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
577
}
578
579
struct sysentvec linux_sysvec = {
580
.sv_size = LINUX_SYS_MAXSYSCALL,
581
.sv_table = linux_sysent,
582
.sv_fixup = linux_fixup,
583
.sv_sendsig = linux_sendsig,
584
.sv_sigcode = &_binary_linux_vdso_so_o_start,
585
.sv_szsigcode = &linux_szsigcode,
586
.sv_name = "Linux a.out",
587
.sv_coredump = NULL,
588
.sv_minsigstksz = LINUX_MINSIGSTKSZ,
589
.sv_minuser = VM_MIN_ADDRESS,
590
.sv_maxuser = VM_MAXUSER_ADDRESS,
591
.sv_usrstack = LINUX_USRSTACK,
592
.sv_psstrings = PS_STRINGS,
593
.sv_psstringssz = sizeof(struct ps_strings),
594
.sv_stackprot = VM_PROT_ALL,
595
.sv_copyout_strings = exec_copyout_strings,
596
.sv_setregs = linux_exec_setregs,
597
.sv_fixlimit = NULL,
598
.sv_maxssiz = NULL,
599
.sv_flags = SV_ABI_LINUX | SV_AOUT | SV_ILP32 |
600
SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
601
.sv_set_syscall_retval = linux_set_syscall_retval,
602
.sv_fetch_syscall_args = linux_fetch_syscall_args,
603
.sv_syscallnames = linux_syscallnames,
604
.sv_schedtail = linux_schedtail,
605
.sv_thread_detach = linux_thread_detach,
606
.sv_trap = NULL,
607
.sv_hwcap = NULL,
608
.sv_hwcap2 = NULL,
609
.sv_hwcap3 = NULL,
610
.sv_hwcap4 = NULL,
611
.sv_onexec = linux_on_exec_vmspace,
612
.sv_onexit = linux_on_exit,
613
.sv_ontdexit = linux_thread_dtor,
614
.sv_setid_allowed = &linux_setid_allowed_query,
615
.sv_set_fork_retval = linux_set_fork_retval,
616
};
617
INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
618
619
struct sysentvec elf_linux_sysvec = {
620
.sv_size = LINUX_SYS_MAXSYSCALL,
621
.sv_table = linux_sysent,
622
.sv_fixup = __elfN(freebsd_fixup),
623
.sv_sendsig = linux_sendsig,
624
.sv_sigcode = &_binary_linux_vdso_so_o_start,
625
.sv_szsigcode = &linux_szsigcode,
626
.sv_name = "Linux ELF32",
627
.sv_coredump = elf32_coredump,
628
.sv_elf_core_osabi = ELFOSABI_NONE,
629
.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
630
.sv_elf_core_prepare_notes = __linuxN(prepare_notes),
631
.sv_minsigstksz = LINUX_MINSIGSTKSZ,
632
.sv_minuser = VM_MIN_ADDRESS,
633
.sv_maxuser = VM_MAXUSER_ADDRESS,
634
.sv_usrstack = LINUX_USRSTACK,
635
.sv_psstrings = LINUX_PS_STRINGS,
636
.sv_psstringssz = sizeof(struct ps_strings),
637
.sv_stackprot = VM_PROT_ALL,
638
.sv_copyout_auxargs = __linuxN(copyout_auxargs),
639
.sv_copyout_strings = __linuxN(copyout_strings),
640
.sv_setregs = linux_exec_setregs,
641
.sv_fixlimit = NULL,
642
.sv_maxssiz = NULL,
643
.sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_SHP |
644
SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
645
.sv_set_syscall_retval = linux_set_syscall_retval,
646
.sv_fetch_syscall_args = linux_fetch_syscall_args,
647
.sv_syscallnames = NULL,
648
.sv_shared_page_base = LINUX_SHAREDPAGE,
649
.sv_shared_page_len = PAGE_SIZE,
650
.sv_schedtail = linux_schedtail,
651
.sv_thread_detach = linux_thread_detach,
652
.sv_trap = NULL,
653
.sv_hwcap = NULL,
654
.sv_hwcap2 = NULL,
655
.sv_hwcap3 = NULL,
656
.sv_hwcap4 = NULL,
657
.sv_onexec = linux_on_exec_vmspace,
658
.sv_onexit = linux_on_exit,
659
.sv_ontdexit = linux_thread_dtor,
660
.sv_setid_allowed = &linux_setid_allowed_query,
661
.sv_set_fork_retval = linux_set_fork_retval,
662
};
663
664
static int
665
linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
666
{
667
int error = 0;
668
669
if (SV_PROC_FLAG(p, SV_SHP) != 0)
670
error = linux_map_vdso(p, linux_vdso_obj,
671
linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
672
if (error == 0)
673
error = linux_on_exec(p, imgp);
674
return (error);
675
}
676
677
/*
678
* linux_vdso_install() and linux_exec_sysvec_init() must be called
679
* after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
680
*/
681
static void
682
linux_exec_sysvec_init(void *param)
683
{
684
l_uintptr_t *ktimekeep_base, *ktsc_selector;
685
struct sysentvec *sv;
686
ptrdiff_t tkoff;
687
688
sv = param;
689
/* Fill timekeep_base */
690
exec_sysvec_init(sv);
691
692
tkoff = kern_timekeep_base - linux_vdso_base;
693
ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
694
*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
695
696
tkoff = kern_tsc_selector - linux_vdso_base;
697
ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
698
*ktsc_selector = linux_vdso_tsc_selector_idx();
699
if (bootverbose)
700
printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
701
702
tkoff = kern_cpu_selector - linux_vdso_base;
703
ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
704
*ktsc_selector = linux_vdso_cpu_selector_idx();
705
if (bootverbose)
706
printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
707
}
708
SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
709
linux_exec_sysvec_init, &elf_linux_sysvec);
710
711
static void
712
linux_vdso_install(const void *param)
713
{
714
char *vdso_start = &_binary_linux_vdso_so_o_start;
715
char *vdso_end = &_binary_linux_vdso_so_o_end;
716
717
linux_szsigcode = vdso_end - vdso_start;
718
MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
719
720
linux_vdso_base = LINUX_VDSOPAGE;
721
722
__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
723
724
linux_vdso_obj = __elfN(linux_shared_page_init)
725
(&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
726
bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
727
728
linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
729
}
730
SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
731
linux_vdso_install, NULL);
732
733
static void
734
linux_vdso_deinstall(const void *param)
735
{
736
737
__elfN(linux_shared_page_fini)(linux_vdso_obj,
738
linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
739
}
740
SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
741
linux_vdso_deinstall, NULL);
742
743
static void
744
linux_vdso_reloc(char *mapping, Elf_Addr offset)
745
{
746
const Elf_Shdr *shdr;
747
const Elf_Rel *rel;
748
const Elf_Ehdr *ehdr;
749
Elf_Addr *where;
750
Elf_Size rtype, symidx;
751
Elf_Addr addr, addend;
752
int i, relcnt;
753
754
MPASS(offset != 0);
755
756
relcnt = 0;
757
ehdr = (const Elf_Ehdr *)mapping;
758
shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
759
for (i = 0; i < ehdr->e_shnum; i++)
760
{
761
switch (shdr[i].sh_type) {
762
case SHT_REL:
763
rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
764
relcnt = shdr[i].sh_size / sizeof(*rel);
765
break;
766
case SHT_RELA:
767
printf("Linux i386 vDSO: unexpected Rela section\n");
768
break;
769
}
770
}
771
772
for (i = 0; i < relcnt; i++, rel++) {
773
where = (Elf_Addr *)(mapping + rel->r_offset);
774
addend = *where;
775
rtype = ELF_R_TYPE(rel->r_info);
776
symidx = ELF_R_SYM(rel->r_info);
777
778
switch (rtype) {
779
case R_386_NONE: /* none */
780
break;
781
782
case R_386_RELATIVE: /* B + A */
783
addr = (Elf_Addr)PTROUT(offset + addend);
784
if (*where != addr)
785
*where = addr;
786
break;
787
788
case R_386_IRELATIVE:
789
printf("Linux i386 vDSO: unexpected ifunc relocation, "
790
"symbol index %d\n", symidx);
791
break;
792
default:
793
printf("Linux i386 vDSO: unexpected relocation type %d, "
794
"symbol index %d\n", rtype, symidx);
795
}
796
}
797
}
798
799
static Elf_Brandnote linux_brandnote = {
800
.hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
801
.hdr.n_descsz = 16, /* XXX at least 16 */
802
.hdr.n_type = 1,
803
.vendor = GNU_ABI_VENDOR,
804
.flags = BN_TRANSLATE_OSREL,
805
.trans_osrel = linux_trans_osrel
806
};
807
808
static Elf32_Brandinfo linux_brand = {
809
.brand = ELFOSABI_LINUX,
810
.machine = EM_386,
811
.compat_3_brand = "Linux",
812
.interp_path = "/lib/ld-linux.so.1",
813
.sysvec = &elf_linux_sysvec,
814
.interp_newpath = NULL,
815
.brand_note = &linux_brandnote,
816
.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
817
};
818
819
static Elf32_Brandinfo linux_glibc2brand = {
820
.brand = ELFOSABI_LINUX,
821
.machine = EM_386,
822
.compat_3_brand = "Linux",
823
.interp_path = "/lib/ld-linux.so.2",
824
.sysvec = &elf_linux_sysvec,
825
.interp_newpath = NULL,
826
.brand_note = &linux_brandnote,
827
.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
828
};
829
830
static Elf32_Brandinfo linux_muslbrand = {
831
.brand = ELFOSABI_LINUX,
832
.machine = EM_386,
833
.compat_3_brand = "Linux",
834
.interp_path = "/lib/ld-musl-i386.so.1",
835
.sysvec = &elf_linux_sysvec,
836
.interp_newpath = NULL,
837
.brand_note = &linux_brandnote,
838
.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
839
LINUX_BI_FUTEX_REQUEUE
840
};
841
842
Elf32_Brandinfo *linux_brandlist[] = {
843
&linux_brand,
844
&linux_glibc2brand,
845
&linux_muslbrand,
846
NULL
847
};
848
849
static int
850
linux_elf_modevent(module_t mod, int type, void *data)
851
{
852
Elf32_Brandinfo **brandinfo;
853
int error;
854
struct linux_ioctl_handler **lihp;
855
856
error = 0;
857
858
switch(type) {
859
case MOD_LOAD:
860
for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
861
++brandinfo)
862
if (elf32_insert_brand_entry(*brandinfo) < 0)
863
error = EINVAL;
864
if (error == 0) {
865
SET_FOREACH(lihp, linux_ioctl_handler_set)
866
linux_ioctl_register_handler(*lihp);
867
linux_dev_shm_create();
868
linux_osd_jail_register();
869
linux_netlink_register();
870
stclohz = (stathz ? stathz : hz);
871
if (bootverbose)
872
printf("Linux ELF exec handler installed\n");
873
} else
874
printf("cannot insert Linux ELF brand handler\n");
875
break;
876
case MOD_UNLOAD:
877
for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
878
++brandinfo)
879
if (elf32_brand_inuse(*brandinfo))
880
error = EBUSY;
881
if (error == 0) {
882
for (brandinfo = &linux_brandlist[0];
883
*brandinfo != NULL; ++brandinfo)
884
if (elf32_remove_brand_entry(*brandinfo) < 0)
885
error = EINVAL;
886
}
887
if (error == 0) {
888
SET_FOREACH(lihp, linux_ioctl_handler_set)
889
linux_ioctl_unregister_handler(*lihp);
890
linux_netlink_deregister();
891
linux_dev_shm_destroy();
892
linux_osd_jail_deregister();
893
if (bootverbose)
894
printf("Linux ELF exec handler removed\n");
895
} else
896
printf("Could not deinstall ELF interpreter entry\n");
897
break;
898
default:
899
return (EOPNOTSUPP);
900
}
901
return (error);
902
}
903
904
static moduledata_t linux_elf_mod = {
905
"linuxelf",
906
linux_elf_modevent,
907
0
908
};
909
910
DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
911
MODULE_DEPEND(linuxelf, netlink, 1, 1, 1);
912
FEATURE(linux, "Linux 32bit support");
913
914