xref: /freebsd/sys/arm64/linux/linux_machdep.c (revision 2faf504d1ab821fe2b9df9d2afb49bb35e1334f4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Turing Robotic Industries Inc.
5  * Copyright (c) 2000 Marcel Moolenaar
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  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/ktr.h>
38 #include <sys/proc.h>
39 #include <sys/sdt.h>
40 
41 #include <security/audit/audit.h>
42 
43 #include <machine/reg.h>
44 
45 #include <arm64/linux/linux.h>
46 #include <arm64/linux/linux_proto.h>
47 #include <compat/linux/linux_dtrace.h>
48 #include <compat/linux/linux_emul.h>
49 #include <compat/linux/linux_fork.h>
50 #include <compat/linux/linux_misc.h>
51 #include <compat/linux/linux_mmap.h>
52 #include <compat/linux/linux_util.h>
53 
54 /* DTrace init */
55 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
56 
57 /* DTrace probes */
58 LIN_SDT_PROBE_DEFINE0(machdep, linux_mmap2, todo);
59 
60 /*
61  * LINUXTODO: deduplicate; linux_execve is common across archs, except that on
62  * amd64 compat linuxulator it calls freebsd32_exec_copyin_args.
63  */
64 int
65 linux_execve(struct thread *td, struct linux_execve_args *uap)
66 {
67 	struct image_args eargs;
68 	char *path;
69 	int error;
70 
71 	if (!LUSECONVPATH(td)) {
72 		error = exec_copyin_args(&eargs, uap->path, UIO_USERSPACE,
73 		    uap->argp, uap->envp);
74 	} else {
75 		LCONVPATHEXIST(td, uap->path, &path);
76 		error = exec_copyin_args(&eargs, path, UIO_SYSSPACE,
77 		    uap->argp, uap->envp);
78 		LFREEPATH(path);
79 	}
80 	if (error == 0)
81 		error = linux_common_execve(td, &eargs);
82 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
83 	return (error);
84 }
85 
86 int
87 linux_set_upcall(struct thread *td, register_t stack)
88 {
89 
90 	if (stack)
91 		td->td_frame->tf_sp = stack;
92 
93 	/*
94 	 * The newly created Linux thread returns
95 	 * to the user space by the same path that a parent does.
96 	 */
97 	td->td_frame->tf_x[0] = 0;
98 	return (0);
99 }
100 
101 /* LINUXTODO: deduplicate arm64 linux_mmap2 */
102 int
103 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap)
104 {
105 
106 	LIN_SDT_PROBE0(machdep, linux_mmap2, todo);
107 	return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot,
108 	    uap->flags, uap->fd, uap->pgoff));
109 }
110 
111 int
112 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
113 {
114 
115 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len,
116 	    uap->prot));
117 }
118 
119 int
120 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
121 {
122 
123 	return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
124 }
125 
126 int
127 linux_set_cloned_tls(struct thread *td, void *desc)
128 {
129 
130 	if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
131 		return (EPERM);
132 
133 	return (cpu_set_user_tls(td, desc));
134 }
135 
136 void
137 bsd_to_linux_regset(struct reg *b_reg, struct linux_pt_regset *l_regset)
138 {
139 
140 	KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong),
141 	    ("%s: size mismatch\n", __func__));
142 	memcpy(l_regset->x, b_reg->x, sizeof(b_reg->x));
143 
144 	l_regset->x[30] = b_reg->lr;
145 	l_regset->sp = b_reg->sp;
146 	l_regset->pc = b_reg->elr;
147 	l_regset->cpsr = b_reg->spsr;
148 }
149