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