]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/linux/linux_sysvec.c
MFV: r355716
[FreeBSD/FreeBSD.git] / sys / arm64 / linux / linux_sysvec.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/cdefs.h>
35 #include <sys/elf.h>
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/signalvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47
48 #include <vm/vm_param.h>
49
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_ioctl.h>
55 #include <compat/linux/linux_mib.h>
56 #include <compat/linux/linux_misc.h>
57 #include <compat/linux/linux_vdso.h>
58
59 MODULE_VERSION(linux64elf, 1);
60
61 const char *linux_kplatform;
62 static int linux_szsigcode;
63 static vm_object_t linux_shared_page_obj;
64 static char *linux_shared_page_mapping;
65 extern char _binary_linux_locore_o_start;
66 extern char _binary_linux_locore_o_end;
67
68 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
69
70 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
71
72 static int      linux_copyout_strings(struct image_params *imgp,
73                     uintptr_t *stack_base);
74 static int      linux_elf_fixup(uintptr_t *stack_base,
75                     struct image_params *iparams);
76 static bool     linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
77 static void     linux_vdso_install(const void *param);
78 static void     linux_vdso_deinstall(const void *param);
79 static void     linux_set_syscall_retval(struct thread *td, int error);
80 static int      linux_fetch_syscall_args(struct thread *td);
81 static void     linux_exec_setregs(struct thread *td, struct image_params *imgp,
82                     uintptr_t stack);
83 static int      linux_vsyscall(struct thread *td);
84
85 /* DTrace init */
86 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
87
88 /* DTrace probes */
89 LIN_SDT_PROBE_DEFINE2(sysvec, linux_translate_traps, todo, "int", "int");
90 LIN_SDT_PROBE_DEFINE0(sysvec, linux_exec_setregs, todo);
91 LIN_SDT_PROBE_DEFINE0(sysvec, linux_copyout_auxargs, todo);
92 LIN_SDT_PROBE_DEFINE0(sysvec, linux_elf_fixup, todo);
93 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sigreturn, todo);
94 LIN_SDT_PROBE_DEFINE0(sysvec, linux_rt_sendsig, todo);
95 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vsyscall, todo);
96 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_install, todo);
97 LIN_SDT_PROBE_DEFINE0(sysvec, linux_vdso_deinstall, todo);
98
99 /* LINUXTODO: do we have traps to translate? */
100 static int
101 linux_translate_traps(int signal, int trap_code)
102 {
103
104         LIN_SDT_PROBE2(sysvec, linux_translate_traps, todo, signal, trap_code);
105         return (signal);
106 }
107
108 LINUX_VDSO_SYM_CHAR(linux_platform);
109
110 static int
111 linux_fetch_syscall_args(struct thread *td)
112 {
113         struct proc *p;
114         struct syscall_args *sa;
115         register_t *ap;
116
117         p = td->td_proc;
118         ap = td->td_frame->tf_x;
119         sa = &td->td_sa;
120
121         sa->code = td->td_frame->tf_x[8];
122         /* LINUXTODO: generic syscall? */
123         if (sa->code >= p->p_sysent->sv_size)
124                 sa->callp = &p->p_sysent->sv_table[0];
125         else
126                 sa->callp = &p->p_sysent->sv_table[sa->code];
127
128         sa->narg = sa->callp->sy_narg;
129         if (sa->narg > 8)
130                 panic("ARM64TODO: Could we have more than 8 args?");
131         memcpy(sa->args, ap, 8 * sizeof(register_t));
132
133         td->td_retval[0] = 0;
134         return (0);
135 }
136
137 static void
138 linux_set_syscall_retval(struct thread *td, int error)
139 {
140
141         td->td_retval[1] = td->td_frame->tf_x[1];
142         cpu_set_syscall_retval(td, error);
143 }
144
145 static int
146 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
147 {
148         Elf_Auxargs *args;
149         Elf_Auxinfo *argarray, *pos;
150         struct proc *p;
151         int error, issetugid;
152
153         LIN_SDT_PROBE0(sysvec, linux_copyout_auxargs, todo);
154         p = imgp->proc;
155
156         args = (Elf64_Auxargs *)imgp->auxargs;
157         argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
158             M_WAITOK | M_ZERO);
159
160         issetugid = p->p_flag & P_SUGID ? 1 : 0;
161         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
162             imgp->proc->p_sysent->sv_shared_page_base);
163 #if 0   /* LINUXTODO: implement arm64 LINUX_AT_HWCAP */
164         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
165 #endif
166         AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
167         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
168         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
169         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
170         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
171         AUXARGS_ENTRY(pos, AT_BASE, args->base);
172         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
173         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
174         AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
175         AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
176         AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
177         AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
178         AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
179 #if 0   /* LINUXTODO: implement arm64 LINUX_AT_PLATFORM */
180         AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
181 #endif
182         AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
183         if (imgp->execpathp != 0)
184                 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
185         if (args->execfd != -1)
186                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
187         AUXARGS_ENTRY(pos, AT_NULL, 0);
188         free(imgp->auxargs, M_TEMP);
189         imgp->auxargs = NULL;
190         KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
191
192         error = copyout(argarray, (void *)base,
193             sizeof(*argarray) * LINUX_AT_COUNT);
194         free(argarray, M_TEMP);
195         return (error);
196 }
197
198 static int
199 linux_elf_fixup(uintptr_t *stack_base, struct image_params *imgp)
200 {
201
202         LIN_SDT_PROBE0(sysvec, linux_elf_fixup, todo);
203
204         return (0);
205 }
206
207 /*
208  * Copy strings out to the new process address space, constructing new arg
209  * and env vector tables. Return a pointer to the base so that it can be used
210  * as the initial stack pointer.
211  * LINUXTODO: deduplicate against other linuxulator archs
212  */
213 static int
214 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
215 {
216         char **vectp;
217         char *stringp;
218         uintptr_t destp, ustringp;
219         struct ps_strings *arginfo;
220         char canary[LINUX_AT_RANDOM_LEN];
221         size_t execpath_len;
222         struct proc *p;
223         int argc, envc, error;
224
225         /* Calculate string base and vector table pointers. */
226         if (imgp->execpath != NULL && imgp->auxargs != NULL)
227                 execpath_len = strlen(imgp->execpath) + 1;
228         else
229                 execpath_len = 0;
230
231         p = imgp->proc;
232         arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
233         destp = (uintptr_t)arginfo;
234
235         if (execpath_len != 0) {
236                 destp -= execpath_len;
237                 destp = rounddown2(destp, sizeof(void *));
238                 imgp->execpathp = destp;
239                 error = copyout(imgp->execpath, (void *)destp, execpath_len);
240                 if (error != 0)
241                         return (error);
242         }
243
244         /* Prepare the canary for SSP. */
245         arc4rand(canary, sizeof(canary), 0);
246         destp -= roundup(sizeof(canary), sizeof(void *));
247         imgp->canary = destp;
248         error = copyout(canary, (void *)destp, sizeof(canary));
249         if (error != 0)
250                 return (error);
251
252         /* Allocate room for the argument and environment strings. */
253         destp -= ARG_MAX - imgp->args->stringspace;
254         destp = rounddown2(destp, sizeof(void *));
255         ustringp = destp;
256
257         if (imgp->auxargs) {
258                 /*
259                  * Allocate room on the stack for the ELF auxargs
260                  * array.  It has up to LINUX_AT_COUNT entries.
261                  */
262                 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
263                 destp = rounddown2(destp, sizeof(void *));
264         }
265
266         vectp = (char **)destp;
267
268         /*
269          * Allocate room for argc and the argv[] and env vectors including the
270          * terminating NULL pointers.
271          */
272         vectp -= 1 + imgp->args->argc + 1 + imgp->args->envc + 1;
273         vectp = (char **)STACKALIGN(vectp);
274
275         /* vectp also becomes our initial stack base. */
276         *stack_base = (uintptr_t)vectp;
277
278         stringp = imgp->args->begin_argv;
279         argc = imgp->args->argc;
280         envc = imgp->args->envc;
281
282         /* Copy out strings - arguments and environment. */
283         error = copyout(stringp, (void *)ustringp,
284             ARG_MAX - imgp->args->stringspace);
285         if (error != 0)
286                 return (error);
287
288         /* Fill in "ps_strings" struct for ps, w, etc. */
289         if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
290             suword(&arginfo->ps_nargvstr, argc) != 0)
291                 return (EFAULT);
292
293         if (suword(vectp++, argc) != 0)
294                 return (EFAULT);
295
296         /* Fill in argument portion of vector table. */
297         for (; argc > 0; --argc) {
298                 if (suword(vectp++, ustringp) != 0)
299                         return (EFAULT);
300                 while (*stringp++ != 0)
301                         ustringp++;
302                 ustringp++;
303         }
304
305         /* A null vector table pointer separates the argp's from the envp's. */
306         if (suword(vectp++, 0) != 0)
307                 return (EFAULT);
308
309         if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
310             suword(&arginfo->ps_nenvstr, envc) != 0)
311                 return (EFAULT);
312
313         /* Fill in environment portion of vector table. */
314         for (; envc > 0; --envc) {
315                 if (suword(vectp++, ustringp) != 0)
316                         return (EFAULT);
317                 while (*stringp++ != 0)
318                         ustringp++;
319                 ustringp++;
320         }
321
322         /* The end of the vector table is a null pointer. */
323         if (suword(vectp, 0) != 0)
324                 return (EFAULT);
325
326         if (imgp->auxargs) {
327                 vectp++;
328                 error = imgp->sysent->sv_copyout_auxargs(imgp,
329                     (uintptr_t)vectp);
330                 if (error != 0)
331                         return (error);
332         }
333
334         return (0);
335 }
336
337 /*
338  * Reset registers to default values on exec.
339  */
340 static void
341 linux_exec_setregs(struct thread *td, struct image_params *imgp,
342     uintptr_t stack)
343 {
344         struct trapframe *regs = td->td_frame;
345
346         /* LINUXTODO: validate */
347         LIN_SDT_PROBE0(sysvec, linux_exec_setregs, todo);
348
349         memset(regs, 0, sizeof(*regs));
350         /* glibc start.S registers function pointer in x0 with atexit. */
351         regs->tf_sp = stack;
352 #if 0   /* LINUXTODO: See if this is used. */
353         regs->tf_lr = imgp->entry_addr;
354 #else
355         regs->tf_lr = 0xffffffffffffffff;
356 #endif
357         regs->tf_elr = imgp->entry_addr;
358 }
359
360 int
361 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
362 {
363
364         /* LINUXTODO: implement */
365         LIN_SDT_PROBE0(sysvec, linux_rt_sigreturn, todo);
366         return (EDOOFUS);
367 }
368
369 static void
370 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
371 {
372
373         /* LINUXTODO: implement */
374         LIN_SDT_PROBE0(sysvec, linux_rt_sendsig, todo);
375 }
376
377 static int
378 linux_vsyscall(struct thread *td)
379 {
380
381         /* LINUXTODO: implement */
382         LIN_SDT_PROBE0(sysvec, linux_vsyscall, todo);
383         return (EDOOFUS);
384 }
385
386 struct sysentvec elf_linux_sysvec = {
387         .sv_size        = LINUX_SYS_MAXSYSCALL,
388         .sv_table       = linux_sysent,
389         .sv_errsize     = ELAST + 1,
390         .sv_errtbl      = linux_errtbl,
391         .sv_transtrap   = linux_translate_traps,
392         .sv_fixup       = linux_elf_fixup,
393         .sv_sendsig     = linux_rt_sendsig,
394         .sv_sigcode     = &_binary_linux_locore_o_start,
395         .sv_szsigcode   = &linux_szsigcode,
396         .sv_name        = "Linux ELF64",
397         .sv_coredump    = elf64_coredump,
398         .sv_imgact_try  = linux_exec_imgact_try,
399         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
400         .sv_minuser     = VM_MIN_ADDRESS,
401         .sv_maxuser     = VM_MAXUSER_ADDRESS,
402         .sv_usrstack    = USRSTACK,
403         .sv_psstrings   = PS_STRINGS, /* XXX */
404         .sv_stackprot   = VM_PROT_READ | VM_PROT_WRITE,
405         .sv_copyout_auxargs = linux_copyout_auxargs,
406         .sv_copyout_strings = linux_copyout_strings,
407         .sv_setregs     = linux_exec_setregs,
408         .sv_fixlimit    = NULL,
409         .sv_maxssiz     = NULL,
410         .sv_flags       = SV_ABI_LINUX | SV_LP64 | SV_SHP,
411         .sv_set_syscall_retval = linux_set_syscall_retval,
412         .sv_fetch_syscall_args = linux_fetch_syscall_args,
413         .sv_syscallnames = NULL,
414         .sv_shared_page_base = SHAREDPAGE,
415         .sv_shared_page_len = PAGE_SIZE,
416         .sv_schedtail   = linux_schedtail,
417         .sv_thread_detach = linux_thread_detach,
418         .sv_trap        = linux_vsyscall,
419 };
420
421 static void
422 linux_vdso_install(const void *param)
423 {
424
425         linux_szsigcode = (&_binary_linux_locore_o_end -
426             &_binary_linux_locore_o_start);
427
428         if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
429                 panic("invalid Linux VDSO size\n");
430
431         __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
432
433         linux_shared_page_obj = __elfN(linux_shared_page_init)
434             (&linux_shared_page_mapping);
435
436         __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
437
438         memcpy(linux_shared_page_mapping, elf_linux_sysvec.sv_sigcode,
439             linux_szsigcode);
440         elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
441
442         printf("LINUXTODO: %s: fix linux_kplatform\n", __func__);
443 #if 0
444         linux_kplatform = linux_shared_page_mapping +
445             (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
446 #else
447         linux_kplatform = "arm64";
448 #endif
449 }
450 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
451     linux_vdso_install, NULL);
452
453 static void
454 linux_vdso_deinstall(const void *param)
455 {
456
457         LIN_SDT_PROBE0(sysvec, linux_vdso_deinstall, todo);
458         __elfN(linux_shared_page_fini)(linux_shared_page_obj);
459 }
460 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
461     linux_vdso_deinstall, NULL);
462
463 static char GNU_ABI_VENDOR[] = "GNU";
464 static int GNU_ABI_LINUX = 0;
465
466 /* LINUXTODO: deduplicate */
467 static bool
468 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
469 {
470         const Elf32_Word *desc;
471         uintptr_t p;
472
473         p = (uintptr_t)(note + 1);
474         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
475
476         desc = (const Elf32_Word *)p;
477         if (desc[0] != GNU_ABI_LINUX)
478                 return (false);
479
480         *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
481         return (true);
482 }
483
484 static Elf_Brandnote linux64_brandnote = {
485         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
486         .hdr.n_descsz   = 16,
487         .hdr.n_type     = 1,
488         .vendor         = GNU_ABI_VENDOR,
489         .flags          = BN_TRANSLATE_OSREL,
490         .trans_osrel    = linux_trans_osrel
491 };
492
493 static Elf64_Brandinfo linux_glibc2brand = {
494         .brand          = ELFOSABI_LINUX,
495         .machine        = EM_AARCH64,
496         .compat_3_brand = "Linux",
497         .emul_path      = "/compat/linux",
498         .interp_path    = "/lib64/ld-linux-x86-64.so.2",
499         .sysvec         = &elf_linux_sysvec,
500         .interp_newpath = NULL,
501         .brand_note     = &linux64_brandnote,
502         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
503 };
504
505 Elf64_Brandinfo *linux_brandlist[] = {
506         &linux_glibc2brand,
507         NULL
508 };
509
510 static int
511 linux64_elf_modevent(module_t mod, int type, void *data)
512 {
513         Elf64_Brandinfo **brandinfo;
514         struct linux_ioctl_handler**lihp;
515         int error;
516
517         error = 0;
518         switch(type) {
519         case MOD_LOAD:
520                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
521                     ++brandinfo)
522                         if (elf64_insert_brand_entry(*brandinfo) < 0)
523                                 error = EINVAL;
524                 if (error == 0) {
525                         SET_FOREACH(lihp, linux_ioctl_handler_set)
526                                 linux_ioctl_register_handler(*lihp);
527                         stclohz = (stathz ? stathz : hz);
528                         if (bootverbose)
529                                 printf("Linux arm64 ELF exec handler installed\n");
530                 }
531                 break;
532         case MOD_UNLOAD:
533                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
534                     ++brandinfo)
535                         if (elf64_brand_inuse(*brandinfo))
536                                 error = EBUSY;
537                 if (error == 0) {
538                         for (brandinfo = &linux_brandlist[0];
539                             *brandinfo != NULL; ++brandinfo)
540                                 if (elf64_remove_brand_entry(*brandinfo) < 0)
541                                         error = EINVAL;
542                 }
543                 if (error == 0) {
544                         SET_FOREACH(lihp, linux_ioctl_handler_set)
545                                 linux_ioctl_unregister_handler(*lihp);
546                         if (bootverbose)
547                                 printf("Linux ELF exec handler removed\n");
548                 } else
549                         printf("Could not deinstall ELF interpreter entry\n");
550                 break;
551         default:
552                 return (EOPNOTSUPP);
553         }
554         return (error);
555 }
556
557 static moduledata_t linux64_elf_mod = {
558         "linux64elf",
559         linux64_elf_modevent,
560         0
561 };
562
563 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
564 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
565 FEATURE(linux64, "AArch64 Linux 64bit support");