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