1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * Copyright (c) 2018 Turing Robotic Industries Inc.
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 #include <sys/cdefs.h>
30 #define __ELF_WORD_SIZE 64
31
32 #include <sys/param.h>
33 #include <sys/elf.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/imgact_elf.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/lock.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
48 #include <vm/pmap.h>
49 #include <vm/vm.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_page.h>
52
53 #include <arm64/linux/linux.h>
54 #include <arm64/linux/linux_proto.h>
55 #include <compat/linux/linux_dtrace.h>
56 #include <compat/linux/linux_elf.h>
57 #include <compat/linux/linux_emul.h>
58 #include <compat/linux/linux_fork.h>
59 #include <compat/linux/linux_ioctl.h>
60 #include <compat/linux/linux_mib.h>
61 #include <compat/linux/linux_misc.h>
62 #include <compat/linux/linux_signal.h>
63 #include <compat/linux/linux_util.h>
64 #include <compat/linux/linux_vdso.h>
65
66 #include <arm64/linux/linux_sigframe.h>
67
68 #include <machine/md_var.h>
69
70 #ifdef VFP
71 #include <machine/vfp.h>
72 #endif
73
74 MODULE_VERSION(linux64elf, 1);
75
76 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
77 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \
78 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 - \
86 sizeof(struct ps_strings))
87
88 static int linux_szsigcode;
89 static vm_object_t linux_vdso_obj;
90 static char *linux_vdso_mapping;
91 extern char _binary_linux_vdso_so_o_start;
92 extern char _binary_linux_vdso_so_o_end;
93 static vm_offset_t linux_vdso_base;
94
95 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
96 extern const char *linux_syscallnames[];
97
98 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
99
100 static void linux_vdso_install(const void *param);
101 static void linux_vdso_deinstall(const void *param);
102 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
103 static void linux_set_syscall_retval(struct thread *td, int error);
104 static int linux_fetch_syscall_args(struct thread *td);
105 static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
106 uintptr_t stack);
107 static void linux_exec_sysvec_init(void *param);
108 static int linux_on_exec_vmspace(struct proc *p,
109 struct image_params *imgp);
110
111 /* DTrace init */
112 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
113
114 /* DTrace probes */
115 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
116
117 LINUX_VDSO_SYM_CHAR(linux_platform);
118 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
119 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
120
121 static int
linux_fetch_syscall_args(struct thread * td)122 linux_fetch_syscall_args(struct thread *td)
123 {
124 struct proc *p;
125 struct syscall_args *sa;
126 register_t *ap;
127
128 p = td->td_proc;
129 ap = td->td_frame->tf_x;
130 sa = &td->td_sa;
131
132 sa->code = td->td_frame->tf_x[8];
133 /* LINUXTODO: generic syscall? */
134 if (sa->code >= p->p_sysent->sv_size)
135 sa->callp = &nosys_sysent;
136 else
137 sa->callp = &p->p_sysent->sv_table[sa->code];
138
139 if (sa->callp->sy_narg > MAXARGS)
140 panic("ARM64TODO: Could we have more than %d args?", MAXARGS);
141 memcpy(sa->args, ap, MAXARGS * sizeof(register_t));
142
143 td->td_retval[0] = 0;
144 return (0);
145 }
146
147 static void
linux_set_syscall_retval(struct thread * td,int error)148 linux_set_syscall_retval(struct thread *td, int error)
149 {
150
151 td->td_retval[1] = td->td_frame->tf_x[1];
152 cpu_set_syscall_retval(td, error);
153
154 if (__predict_false(error != 0)) {
155 if (error != ERESTART && error != EJUSTRETURN)
156 td->td_frame->tf_x[0] = bsd_to_linux_errno(error);
157 }
158 }
159
160 void
linux64_arch_copyout_auxargs(struct image_params * imgp,Elf_Auxinfo ** pos)161 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
162 {
163
164 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
165 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap);
166 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2);
167 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
168 }
169
170 /*
171 * Reset registers to default values on exec.
172 */
173 static void
linux_exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)174 linux_exec_setregs(struct thread *td, struct image_params *imgp,
175 uintptr_t stack)
176 {
177 struct trapframe *regs = td->td_frame;
178 struct pcb *pcb = td->td_pcb;
179
180 /* LINUXTODO: validate */
181 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
182
183 memset(regs, 0, sizeof(*regs));
184 /* glibc start.S registers function pointer in x0 with atexit. */
185 regs->tf_sp = stack;
186 #if 0 /* LINUXTODO: See if this is used. */
187 regs->tf_lr = imgp->entry_addr;
188 #else
189 regs->tf_lr = 0xffffffffffffffff;
190 #endif
191 regs->tf_elr = imgp->entry_addr;
192
193 pcb->pcb_tpidr_el0 = 0;
194 pcb->pcb_tpidrro_el0 = 0;
195 WRITE_SPECIALREG(tpidrro_el0, 0);
196 WRITE_SPECIALREG(tpidr_el0, 0);
197
198 #ifdef VFP
199 vfp_reset_state(td, pcb);
200 #endif
201
202 /*
203 * Clear debug register state. It is not applicable to the new process.
204 */
205 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
206 }
207
208 static bool
linux_parse_sigreturn_ctx(struct thread * td,struct l_sigcontext * sc)209 linux_parse_sigreturn_ctx(struct thread *td, struct l_sigcontext *sc)
210 {
211 struct l_fpsimd_context *fpsimd;
212 struct _l_aarch64_ctx *ctx;
213 int offset;
214
215 offset = 0;
216 while (1) {
217 /* The offset must be 16 byte aligned */
218 if ((offset & 15) != 0)
219 return (false);
220
221 /* Check for buffer overflow of the ctx */
222 if ((offset + sizeof(*ctx)) >
223 sizeof(sc->__reserved))
224 return (false);
225
226 ctx = (struct _l_aarch64_ctx *)&sc->__reserved[offset];
227
228 /* Check for buffer overflow of the data */
229 if ((offset + ctx->size) > sizeof(sc->__reserved))
230 return (false);
231
232 switch(ctx->magic) {
233 case 0:
234 if (ctx->size != 0)
235 return (false);
236 return (true);
237 case L_ESR_MAGIC:
238 /* Ignore */
239 break;
240 #ifdef VFP
241 case L_FPSIMD_MAGIC:
242 fpsimd = (struct l_fpsimd_context *)ctx;
243
244 /*
245 * Discard any vfp state for the current thread, we
246 * are about to override it.
247 */
248 critical_enter();
249 vfp_discard(td);
250 critical_exit();
251
252 td->td_pcb->pcb_fpustate.vfp_fpcr = fpsimd->fpcr;
253 td->td_pcb->pcb_fpustate.vfp_fpsr = fpsimd->fpsr;
254 memcpy(td->td_pcb->pcb_fpustate.vfp_regs,
255 fpsimd->vregs, sizeof(fpsimd->vregs));
256
257 break;
258 #endif
259 default:
260 return (false);
261 }
262
263 offset += ctx->size;
264 }
265
266 }
267
268 int
linux_rt_sigreturn(struct thread * td,struct linux_rt_sigreturn_args * args)269 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
270 {
271 struct l_rt_sigframe *sf;
272 struct l_sigframe *frame;
273 struct trapframe *tf;
274 sigset_t bmask;
275 int error;
276
277 sf = malloc(sizeof(*sf), M_LINUX, M_WAITOK | M_ZERO);
278
279 tf = td->td_frame;
280 frame = (struct l_sigframe *)tf->tf_sp;
281 error = copyin((void *)&frame->sf, sf, sizeof(*sf));
282 if (error != 0) {
283 free(sf, M_LINUX);
284 return (error);
285 }
286
287 memcpy(tf->tf_x, sf->sf_uc.uc_sc.regs, sizeof(tf->tf_x));
288 tf->tf_lr = sf->sf_uc.uc_sc.regs[30];
289 tf->tf_sp = sf->sf_uc.uc_sc.sp;
290 tf->tf_elr = sf->sf_uc.uc_sc.pc;
291
292 if ((sf->sf_uc.uc_sc.pstate & PSR_M_MASK) != PSR_M_EL0t ||
293 (sf->sf_uc.uc_sc.pstate & PSR_AARCH32) != 0 ||
294 (sf->sf_uc.uc_sc.pstate & PSR_DAIF) !=
295 (td->td_frame->tf_spsr & PSR_DAIF))
296 goto einval;
297 tf->tf_spsr = sf->sf_uc.uc_sc.pstate;
298
299 if (!linux_parse_sigreturn_ctx(td, &sf->sf_uc.uc_sc))
300 goto einval;
301
302 /* Restore signal mask. */
303 linux_to_bsd_sigset(&sf->sf_uc.uc_sigmask, &bmask);
304 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
305 free(sf, M_LINUX);
306
307 return (EJUSTRETURN);
308 einval:
309 free(sf, M_LINUX);
310 return (EINVAL);
311 }
312
313 static void
linux_rt_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)314 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
315 {
316 struct thread *td;
317 struct proc *p;
318 struct trapframe *tf;
319 struct l_sigframe *fp, *frame;
320 struct l_fpsimd_context *fpsimd;
321 struct l_esr_context *esr;
322 l_stack_t uc_stack;
323 ucontext_t uc;
324 uint8_t *scr;
325 struct sigacts *psp;
326 int onstack, sig, issiginfo;
327
328 td = curthread;
329 p = td->td_proc;
330 PROC_LOCK_ASSERT(p, MA_OWNED);
331
332 sig = ksi->ksi_signo;
333 psp = p->p_sigacts;
334 mtx_assert(&psp->ps_mtx, MA_OWNED);
335
336 tf = td->td_frame;
337 onstack = sigonstack(tf->tf_sp);
338 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
339
340 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
341 catcher, sig);
342
343 /* Allocate and validate space for the signal handler context. */
344 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
345 SIGISMEMBER(psp->ps_sigonstack, sig)) {
346 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
347 td->td_sigstk.ss_size);
348 #if defined(COMPAT_43)
349 td->td_sigstk.ss_flags |= SS_ONSTACK;
350 #endif
351 } else {
352 fp = (struct l_sigframe *)td->td_frame->tf_sp;
353 }
354
355 /* Make room, keeping the stack aligned */
356 fp--;
357 fp = (struct l_sigframe *)STACKALIGN(fp);
358
359 get_mcontext(td, &uc.uc_mcontext, 0);
360 uc.uc_sigmask = *mask;
361
362 uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
363 uc_stack.ss_size = td->td_sigstk.ss_size;
364 uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
365 (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
366 mtx_unlock(&psp->ps_mtx);
367 PROC_UNLOCK(td->td_proc);
368
369 /* Fill in the frame to copy out */
370 frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO);
371
372 memcpy(&frame->sf.sf_uc.uc_sc.regs, tf->tf_x, sizeof(tf->tf_x));
373 frame->sf.sf_uc.uc_sc.regs[30] = tf->tf_lr;
374 frame->sf.sf_uc.uc_sc.sp = tf->tf_sp;
375 frame->sf.sf_uc.uc_sc.pc = tf->tf_lr;
376 frame->sf.sf_uc.uc_sc.pstate = tf->tf_spsr;
377 frame->sf.sf_uc.uc_sc.fault_address = (register_t)ksi->ksi_addr;
378
379 /* Stack frame for unwinding */
380 frame->fp = tf->tf_x[29];
381 frame->lr = tf->tf_lr;
382
383 /* Translate the signal. */
384 sig = bsd_to_linux_signal(sig);
385 siginfo_to_lsiginfo(&ksi->ksi_info, &frame->sf.sf_si, sig);
386 bsd_to_linux_sigset(mask, &frame->sf.sf_uc.uc_sigmask);
387
388 /*
389 * Prepare fpsimd & esr. Does not check sizes, as
390 * __reserved is big enougth.
391 */
392 scr = (uint8_t *)&frame->sf.sf_uc.uc_sc.__reserved;
393 #ifdef VFP
394 fpsimd = (struct l_fpsimd_context *) scr;
395 fpsimd->head.magic = L_FPSIMD_MAGIC;
396 fpsimd->head.size = sizeof(struct l_fpsimd_context);
397 fpsimd->fpsr = uc.uc_mcontext.mc_fpregs.fp_sr;
398 fpsimd->fpcr = uc.uc_mcontext.mc_fpregs.fp_cr;
399
400 memcpy(fpsimd->vregs, &uc.uc_mcontext.mc_fpregs.fp_q,
401 sizeof(uc.uc_mcontext.mc_fpregs.fp_q));
402 scr += roundup(sizeof(struct l_fpsimd_context), 16);
403 #endif
404 if (ksi->ksi_addr != 0) {
405 esr = (struct l_esr_context *) scr;
406 esr->head.magic = L_ESR_MAGIC;
407 esr->head.size = sizeof(struct l_esr_context);
408 esr->esr = tf->tf_esr;
409 }
410
411 memcpy(&frame->sf.sf_uc.uc_stack, &uc_stack, sizeof(uc_stack));
412
413 /* Copy the sigframe out to the user's stack. */
414 if (copyout(frame, fp, sizeof(*fp)) != 0) {
415 /* Process has trashed its stack. Kill it. */
416 free(frame, M_LINUX);
417 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
418 PROC_LOCK(p);
419 sigexit(td, SIGILL);
420 }
421 free(frame, M_LINUX);
422
423 tf->tf_x[0]= sig;
424 if (issiginfo) {
425 tf->tf_x[1] = (register_t)&fp->sf.sf_si;
426 tf->tf_x[2] = (register_t)&fp->sf.sf_uc;
427 } else {
428 tf->tf_x[1] = 0;
429 tf->tf_x[2] = 0;
430 }
431 tf->tf_x[8] = (register_t)catcher;
432 tf->tf_sp = (register_t)fp;
433 tf->tf_elr = (register_t)linux_vdso_sigcode;
434
435 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
436 tf->tf_sp);
437
438 PROC_LOCK(p);
439 mtx_lock(&psp->ps_mtx);
440 }
441
442 struct sysentvec elf_linux_sysvec = {
443 .sv_size = LINUX_SYS_MAXSYSCALL,
444 .sv_table = linux_sysent,
445 .sv_fixup = __elfN(freebsd_fixup),
446 .sv_sendsig = linux_rt_sendsig,
447 .sv_sigcode = &_binary_linux_vdso_so_o_start,
448 .sv_szsigcode = &linux_szsigcode,
449 .sv_name = "Linux ELF64",
450 .sv_coredump = elf64_coredump,
451 .sv_elf_core_osabi = ELFOSABI_NONE,
452 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
453 .sv_elf_core_prepare_notes = linux64_prepare_notes,
454 .sv_imgact_try = linux_exec_imgact_try,
455 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
456 .sv_minuser = VM_MIN_ADDRESS,
457 .sv_maxuser = VM_MAXUSER_ADDRESS,
458 .sv_usrstack = LINUX_USRSTACK,
459 .sv_psstrings = LINUX_PS_STRINGS,
460 .sv_psstringssz = sizeof(struct ps_strings),
461 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
462 .sv_copyout_auxargs = __linuxN(copyout_auxargs),
463 .sv_copyout_strings = __linuxN(copyout_strings),
464 .sv_setregs = linux_exec_setregs,
465 .sv_fixlimit = NULL,
466 .sv_maxssiz = NULL,
467 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
468 SV_SIG_WAITNDQ | SV_TIMEKEEP,
469 .sv_set_syscall_retval = linux_set_syscall_retval,
470 .sv_fetch_syscall_args = linux_fetch_syscall_args,
471 .sv_syscallnames = linux_syscallnames,
472 .sv_shared_page_base = LINUX_SHAREDPAGE,
473 .sv_shared_page_len = PAGE_SIZE,
474 .sv_schedtail = linux_schedtail,
475 .sv_thread_detach = linux_thread_detach,
476 .sv_trap = NULL,
477 .sv_hwcap = &elf_hwcap,
478 .sv_hwcap2 = &elf_hwcap2,
479 .sv_onexec = linux_on_exec_vmspace,
480 .sv_onexit = linux_on_exit,
481 .sv_ontdexit = linux_thread_dtor,
482 .sv_setid_allowed = &linux_setid_allowed_query,
483 };
484
485 static int
linux_on_exec_vmspace(struct proc * p,struct image_params * imgp)486 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
487 {
488 int error;
489
490 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
491 LINUX_VDSOPAGE_SIZE, imgp);
492 if (error == 0)
493 linux_on_exec(p, imgp);
494 return (error);
495 }
496
497 /*
498 * linux_vdso_install() and linux_exec_sysvec_init() must be called
499 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
500 */
501 static void
linux_exec_sysvec_init(void * param)502 linux_exec_sysvec_init(void *param)
503 {
504 l_uintptr_t *ktimekeep_base;
505 struct sysentvec *sv;
506 ptrdiff_t tkoff;
507
508 sv = param;
509 /* Fill timekeep_base */
510 exec_sysvec_init(sv);
511
512 tkoff = kern_timekeep_base - linux_vdso_base;
513 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
514 *ktimekeep_base = sv->sv_timekeep_base;
515 }
516 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
517 linux_exec_sysvec_init, &elf_linux_sysvec);
518
519 static void
linux_vdso_install(const void * param)520 linux_vdso_install(const void *param)
521 {
522 char *vdso_start = &_binary_linux_vdso_so_o_start;
523 char *vdso_end = &_binary_linux_vdso_so_o_end;
524
525 linux_szsigcode = vdso_end - vdso_start;
526 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
527
528 linux_vdso_base = LINUX_VDSOPAGE;
529
530 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
531
532 linux_vdso_obj = __elfN(linux_shared_page_init)
533 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
534 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
535
536 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
537 }
538 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
539 linux_vdso_install, NULL);
540
541 static void
linux_vdso_deinstall(const void * param)542 linux_vdso_deinstall(const void *param)
543 {
544
545 __elfN(linux_shared_page_fini)(linux_vdso_obj,
546 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
547 }
548 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
549 linux_vdso_deinstall, NULL);
550
551 static void
linux_vdso_reloc(char * mapping,Elf_Addr offset)552 linux_vdso_reloc(char *mapping, Elf_Addr offset)
553 {
554 Elf_Size rtype, symidx;
555 const Elf_Rela *rela;
556 const Elf_Shdr *shdr;
557 const Elf_Ehdr *ehdr;
558 Elf_Addr *where;
559 Elf_Addr addr, addend;
560 int i, relacnt;
561
562 MPASS(offset != 0);
563
564 relacnt = 0;
565 ehdr = (const Elf_Ehdr *)mapping;
566 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
567 for (i = 0; i < ehdr->e_shnum; i++)
568 {
569 switch (shdr[i].sh_type) {
570 case SHT_REL:
571 printf("Linux Aarch64 vDSO: unexpected Rel section\n");
572 break;
573 case SHT_RELA:
574 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
575 relacnt = shdr[i].sh_size / sizeof(*rela);
576 }
577 }
578
579 for (i = 0; i < relacnt; i++, rela++) {
580 where = (Elf_Addr *)(mapping + rela->r_offset);
581 addend = rela->r_addend;
582 rtype = ELF_R_TYPE(rela->r_info);
583 symidx = ELF_R_SYM(rela->r_info);
584
585 switch (rtype) {
586 case R_AARCH64_NONE: /* none */
587 break;
588
589 case R_AARCH64_RELATIVE: /* B + A */
590 addr = (Elf_Addr)(mapping + addend);
591 if (*where != addr)
592 *where = addr;
593 break;
594 default:
595 printf("Linux Aarch64 vDSO: unexpected relocation type %ld, "
596 "symbol index %ld\n", rtype, symidx);
597 }
598 }
599 }
600
601 static Elf_Brandnote linux64_brandnote = {
602 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
603 .hdr.n_descsz = 16,
604 .hdr.n_type = 1,
605 .vendor = GNU_ABI_VENDOR,
606 .flags = BN_TRANSLATE_OSREL,
607 .trans_osrel = linux_trans_osrel
608 };
609
610 static Elf64_Brandinfo linux_glibc2brand = {
611 .brand = ELFOSABI_LINUX,
612 .machine = EM_AARCH64,
613 .compat_3_brand = "Linux",
614 .emul_path = linux_emul_path,
615 .interp_path = "/lib64/ld-linux-x86-64.so.2",
616 .sysvec = &elf_linux_sysvec,
617 .interp_newpath = NULL,
618 .brand_note = &linux64_brandnote,
619 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
620 };
621
622 Elf64_Brandinfo *linux_brandlist[] = {
623 &linux_glibc2brand,
624 NULL
625 };
626
627 static int
linux64_elf_modevent(module_t mod,int type,void * data)628 linux64_elf_modevent(module_t mod, int type, void *data)
629 {
630 Elf64_Brandinfo **brandinfo;
631 struct linux_ioctl_handler**lihp;
632 int error;
633
634 error = 0;
635 switch(type) {
636 case MOD_LOAD:
637 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
638 ++brandinfo)
639 if (elf64_insert_brand_entry(*brandinfo) < 0)
640 error = EINVAL;
641 if (error == 0) {
642 SET_FOREACH(lihp, linux_ioctl_handler_set)
643 linux_ioctl_register_handler(*lihp);
644 stclohz = (stathz ? stathz : hz);
645 if (bootverbose)
646 printf("Linux arm64 ELF exec handler installed\n");
647 }
648 break;
649 case MOD_UNLOAD:
650 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
651 ++brandinfo)
652 if (elf64_brand_inuse(*brandinfo))
653 error = EBUSY;
654 if (error == 0) {
655 for (brandinfo = &linux_brandlist[0];
656 *brandinfo != NULL; ++brandinfo)
657 if (elf64_remove_brand_entry(*brandinfo) < 0)
658 error = EINVAL;
659 }
660 if (error == 0) {
661 SET_FOREACH(lihp, linux_ioctl_handler_set)
662 linux_ioctl_unregister_handler(*lihp);
663 if (bootverbose)
664 printf("Linux arm64 ELF exec handler removed\n");
665 } else
666 printf("Could not deinstall Linux arm64 ELF interpreter entry\n");
667 break;
668 default:
669 return (EOPNOTSUPP);
670 }
671 return (error);
672 }
673
674 static moduledata_t linux64_elf_mod = {
675 "linux64elf",
676 linux64_elf_modevent,
677 0
678 };
679
680 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
681 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
682 FEATURE(linux64, "AArch64 Linux 64bit support");
683