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