2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * Copyright (c) 2018 Turing Robotic Industries Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/cdefs.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
44 #include <sys/signalvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
48 #include <vm/vm_param.h>
50 #include <arm64/linux/linux.h>
51 #include <arm64/linux/linux_proto.h>
52 #include <compat/linux/linux_dtrace.h>
53 #include <compat/linux/linux_emul.h>
54 #include <compat/linux/linux_futex.h>
55 #include <compat/linux/linux_ioctl.h>
56 #include <compat/linux/linux_mib.h>
57 #include <compat/linux/linux_misc.h>
58 #include <compat/linux/linux_vdso.h>
60 MODULE_VERSION(linux64elf, 1);
63 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
64 linux_sysctl_debug, "A", "64-bit Linux debugging control");
67 const char *linux_kplatform;
68 static int linux_szsigcode;
69 static vm_object_t linux_shared_page_obj;
70 static char *linux_shared_page_mapping;
71 extern char _binary_linux_locore_o_start;
72 extern char _binary_linux_locore_o_end;
74 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
76 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
78 static register_t *linux_copyout_strings(struct image_params *imgp);
79 static int linux_elf_fixup(register_t **stack_base,
80 struct image_params *iparams);
81 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
82 static void linux_vdso_install(const void *param);
83 static void linux_vdso_deinstall(const void *param);
84 static void linux_set_syscall_retval(struct thread *td, int error);
85 static int linux_fetch_syscall_args(struct thread *td);
86 static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
88 static int linux_vsyscall(struct thread *td);
91 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
94 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int");
95 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
96 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo);
97 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo);
98 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo);
99 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vsyscall, todo);
100 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo);
101 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo);
103 /* LINUXTODO: do we have traps to translate? */
105 linux_translate_traps(int signal, int trap_code)
108 LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code);
112 LINUX_VDSO_SYM_CHAR(linux_platform);
115 linux_fetch_syscall_args(struct thread *td)
118 struct syscall_args *sa;
122 ap = td->td_frame->tf_x;
125 sa->code = td->td_frame->tf_x[8];
126 /* LINUXTODO: generic syscall? */
127 if (p->p_sysent->sv_mask)
128 sa->code &= p->p_sysent->sv_mask;
129 if (sa->code >= p->p_sysent->sv_size)
130 sa->callp = &p->p_sysent->sv_table[0];
132 sa->callp = &p->p_sysent->sv_table[sa->code];
134 sa->narg = sa->callp->sy_narg;
136 panic("ARM64TODO: Could we have more than 8 args?");
137 memcpy(sa->args, ap, 8 * sizeof(register_t));
139 td->td_retval[0] = 0;
144 linux_set_syscall_retval(struct thread *td, int error)
147 td->td_retval[1] = td->td_frame->tf_x[1];
148 cpu_set_syscall_retval(td, error);
152 linux_elf_fixup(register_t **stack_base, struct image_params *imgp)
155 Elf_Auxinfo *argarray, *pos;
156 Elf_Addr *auxbase, *base;
157 struct ps_strings *arginfo;
159 int error, issetugid;
161 LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
163 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
165 KASSERT(curthread->td_proc == imgp->proc,
166 ("unsafe linux_elf_fixup(), should be curproc"));
167 base = (Elf64_Addr *)*stack_base;
168 args = (Elf64_Auxargs *)imgp->auxargs;
169 /* Auxargs after argc, and NULL-terminated argv and envv lists. */
170 auxbase = base + 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
171 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
174 issetugid = p->p_flag & P_SUGID ? 1 : 0;
175 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
176 imgp->proc->p_sysent->sv_shared_page_base);
177 #if 0 /* LINUXTODO: implement arm64 LINUX_AT_HWCAP */
178 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
180 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
181 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
182 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
183 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
184 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
185 AUXARGS_ENTRY(pos, AT_BASE, args->base);
186 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
187 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
188 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
189 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
190 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
191 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
192 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
193 #if 0 /* LINUXTODO: implement arm64 LINUX_AT_PLATFORM */
194 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
196 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
197 if (imgp->execpathp != 0)
198 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
199 if (args->execfd != -1)
200 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
201 AUXARGS_ENTRY(pos, AT_NULL, 0);
202 free(imgp->auxargs, M_TEMP);
203 imgp->auxargs = NULL;
204 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
206 error = copyout(argarray, auxbase, sizeof(*argarray) * LINUX_AT_COUNT);
207 free(argarray, M_TEMP);
215 * Copy strings out to the new process address space, constructing new arg
216 * and env vector tables. Return a pointer to the base so that it can be used
217 * as the initial stack pointer.
218 * LINUXTODO: deduplicate against other linuxulator archs
221 linux_copyout_strings(struct image_params *imgp)
224 char *stringp, *destp;
225 register_t *stack_base;
226 struct ps_strings *arginfo;
227 char canary[LINUX_AT_RANDOM_LEN];
232 /* Calculate string base and vector table pointers. */
233 if (imgp->execpath != NULL && imgp->auxargs != NULL)
234 execpath_len = strlen(imgp->execpath) + 1;
239 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
240 destp = (caddr_t)arginfo - SPARE_USRSPACE -
241 roundup(sizeof(canary), sizeof(char *)) -
242 roundup(execpath_len, sizeof(char *)) -
243 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
245 if (execpath_len != 0) {
246 imgp->execpathp = (uintptr_t)arginfo - execpath_len;
247 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
250 /* Prepare the canary for SSP. */
251 arc4rand(canary, sizeof(canary), 0);
252 imgp->canary = (uintptr_t)arginfo -
253 roundup(execpath_len, sizeof(char *)) -
254 roundup(sizeof(canary), sizeof(char *));
255 copyout(canary, (void *)imgp->canary, sizeof(canary));
257 vectp = (char **)destp;
260 * Allocate room on the stack for the ELF auxargs
261 * array. It has up to LINUX_AT_COUNT entries.
263 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf64_Auxinfo),
268 * Allocate room for argc and the argv[] and env vectors including the
269 * terminating NULL pointers.
271 vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
272 vectp = (char **)STACKALIGN(vectp);
274 /* vectp also becomes our initial stack base. */
275 stack_base = (register_t *)vectp;
277 stringp = imgp->args->begin_argv;
278 argc = imgp->args->argc;
279 envc = imgp->args->envc;
281 /* Copy out strings - arguments and environment. */
282 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
284 /* Fill in "ps_strings" struct for ps, w, etc. */
285 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
286 suword(&arginfo->ps_nargvstr, argc);
288 suword(vectp++, argc);
289 /* Fill in argument portion of vector table. */
290 for (; argc > 0; --argc) {
291 suword(vectp++, (long)(intptr_t)destp);
292 while (*stringp++ != 0)
297 /* A null vector table pointer separates the argp's from the envp's. */
300 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
301 suword(&arginfo->ps_nenvstr, envc);
303 /* Fill in environment portion of vector table. */
304 for (; envc > 0; --envc) {
305 suword(vectp++, (long)(intptr_t)destp);
306 while (*stringp++ != 0)
311 /* The end of the vector table is a null pointer. */
317 * Reset registers to default values on exec.
320 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
322 struct trapframe *regs = td->td_frame;
324 /* LINUXTODO: validate */
325 LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
327 memset(regs, 0, sizeof(*regs));
328 /* glibc start.S registers function pointer in x0 with atexit. */
330 #if 0 /* LINUXTODO: See if this is used. */
331 regs->tf_lr = imgp->entry_addr;
333 regs->tf_lr = 0xffffffffffffffff;
335 regs->tf_elr = imgp->entry_addr;
339 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
342 /* LINUXTODO: implement */
343 LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo);
348 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
351 /* LINUXTODO: implement */
352 LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo);
356 linux_vsyscall(struct thread *td)
359 /* LINUXTODO: implement */
360 LIN_SDT_PROBE0(sysvec, linux_vsyscall, todo);
364 struct sysentvec elf_linux_sysvec = {
365 .sv_size = LINUX_SYS_MAXSYSCALL,
366 .sv_table = linux_sysent,
368 .sv_errsize = ELAST + 1,
369 .sv_errtbl = linux_errtbl,
370 .sv_transtrap = linux_translate_traps,
371 .sv_fixup = linux_elf_fixup,
372 .sv_sendsig = linux_rt_sendsig,
373 .sv_sigcode = &_binary_linux_locore_o_start,
374 .sv_szsigcode = &linux_szsigcode,
375 .sv_name = "Linux ELF64",
376 .sv_coredump = elf64_coredump,
377 .sv_imgact_try = linux_exec_imgact_try,
378 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
379 .sv_pagesize = PAGE_SIZE,
380 .sv_minuser = VM_MIN_ADDRESS,
381 .sv_maxuser = VM_MAXUSER_ADDRESS,
382 .sv_usrstack = USRSTACK,
383 .sv_psstrings = PS_STRINGS, /* XXX */
384 .sv_stackprot = VM_PROT_ALL, /* XXX */
385 .sv_copyout_strings = linux_copyout_strings,
386 .sv_setregs = linux_exec_setregs,
389 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP,
390 .sv_set_syscall_retval = linux_set_syscall_retval,
391 .sv_fetch_syscall_args = linux_fetch_syscall_args,
392 .sv_syscallnames = NULL,
393 .sv_shared_page_base = SHAREDPAGE,
394 .sv_shared_page_len = PAGE_SIZE,
395 .sv_schedtail = linux_schedtail,
396 .sv_thread_detach = linux_thread_detach,
397 .sv_trap = linux_vsyscall,
401 linux_vdso_install(const void *param)
404 linux_szsigcode = (&_binary_linux_locore_o_end -
405 &_binary_linux_locore_o_start);
407 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
408 panic("invalid Linux VDSO size\n");
410 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
412 linux_shared_page_obj = __elfN(linux_shared_page_init)
413 (&linux_shared_page_mapping);
415 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
417 memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode,
419 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
421 printf("LINUXTODO: %s: fix linux_kplatform\n", __func__);
423 linux_kplatform = linux_shared_page_mapping +
424 (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
426 linux_kplatform = "arm64";
429 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
430 linux_vdso_install, NULL);
433 linux_vdso_deinstall(const void *param)
436 LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo);
437 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
439 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
440 linux_vdso_deinstall, NULL);
442 static char GNU_ABI_VENDOR[] = "GNU";
443 static int GNU_ABI_LINUX = 0;
445 /* LINUXTODO: deduplicate */
447 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
449 const Elf32_Word *desc;
452 p = (uintptr_t)(note + 1);
453 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
455 desc = (const Elf32_Word *)p;
456 if (desc[0] != GNU_ABI_LINUX)
459 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
463 static Elf_Brandnote linux64_brandnote = {
464 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
467 .vendor = GNU_ABI_VENDOR,
468 .flags = BN_TRANSLATE_OSREL,
469 .trans_osrel = linux_trans_osrel
472 static Elf64_Brandinfo linux_glibc2brand = {
473 .brand = ELFOSABI_LINUX,
474 .machine = EM_AARCH64,
475 .compat_3_brand = "Linux",
476 .emul_path = "/compat/linux",
477 .interp_path = "/lib64/ld-linux-x86-64.so.2",
478 .sysvec = &elf_linux_sysvec,
479 .interp_newpath = NULL,
480 .brand_note = &linux64_brandnote,
481 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
484 Elf64_Brandinfo *linux_brandlist[] = {
490 linux64_elf_modevent(module_t mod, int type, void *data)
492 Elf64_Brandinfo **brandinfo;
493 struct linux_ioctl_handler**lihp;
499 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
501 if (elf64_insert_brand_entry(*brandinfo) < 0)
504 SET_FOREACH(lihp, linux_ioctl_handler_set)
505 linux_ioctl_register_handler(*lihp);
506 LIST_INIT(&futex_list);
507 mtx_init(&futex_mtx, "ftllk64", NULL, MTX_DEF);
508 stclohz = (stathz ? stathz : hz);
510 printf("Linux arm64 ELF exec handler installed\n");
514 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
516 if (elf64_brand_inuse(*brandinfo))
519 for (brandinfo = &linux_brandlist[0];
520 *brandinfo != NULL; ++brandinfo)
521 if (elf64_remove_brand_entry(*brandinfo) < 0)
525 SET_FOREACH(lihp, linux_ioctl_handler_set)
526 linux_ioctl_unregister_handler(*lihp);
527 mtx_destroy(&futex_mtx);
529 printf("Linux ELF exec handler removed\n");
531 printf("Could not deinstall ELF interpreter entry\n");
539 static moduledata_t linux64_elf_mod = {
541 linux64_elf_modevent,
545 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
546 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
547 FEATURE(linux64, "AArch64 Linux 64bit support");