xref: /freebsd/sys/arm64/linux/linux_machdep.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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/ptrace.h>
40 #include <sys/reg.h>
41 #include <sys/sdt.h>
42 
43 #include <security/audit/audit.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 #define	LINUX_ARCH_AARCH64		0xc00000b7
55 
56 /* DTrace init */
57 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
58 
59 /* DTrace probes */
60 LIN_SDT_PROBE_DEFINE0(machdep, linux_mmap2, todo);
61 
62 /*
63  * LINUXTODO: deduplicate; linux_execve is common across archs, except that on
64  * amd64 compat linuxulator it calls freebsd32_exec_copyin_args.
65  */
66 int
67 linux_execve(struct thread *td, struct linux_execve_args *uap)
68 {
69 	struct image_args eargs;
70 	char *path;
71 	int error;
72 
73 	if (!LUSECONVPATH(td)) {
74 		error = exec_copyin_args(&eargs, uap->path, UIO_USERSPACE,
75 		    uap->argp, uap->envp);
76 	} else {
77 		LCONVPATHEXIST(td, uap->path, &path);
78 		error = exec_copyin_args(&eargs, path, UIO_SYSSPACE,
79 		    uap->argp, uap->envp);
80 		LFREEPATH(path);
81 	}
82 	if (error == 0)
83 		error = linux_common_execve(td, &eargs);
84 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
85 	return (error);
86 }
87 
88 int
89 linux_set_upcall(struct thread *td, register_t stack)
90 {
91 
92 	if (stack)
93 		td->td_frame->tf_sp = stack;
94 
95 	/*
96 	 * The newly created Linux thread returns
97 	 * to the user space by the same path that a parent does.
98 	 */
99 	td->td_frame->tf_x[0] = 0;
100 	return (0);
101 }
102 
103 /* LINUXTODO: deduplicate arm64 linux_mmap2 */
104 int
105 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap)
106 {
107 
108 	LIN_SDT_PROBE0(machdep, linux_mmap2, todo);
109 	return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot,
110 	    uap->flags, uap->fd, uap->pgoff));
111 }
112 
113 int
114 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
115 {
116 
117 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len,
118 	    uap->prot));
119 }
120 
121 int
122 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
123 {
124 
125 	return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
126 }
127 
128 int
129 linux_set_cloned_tls(struct thread *td, void *desc)
130 {
131 
132 	if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
133 		return (EPERM);
134 
135 	return (cpu_set_user_tls(td, desc));
136 }
137 
138 void
139 bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset)
140 {
141 
142 	KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong),
143 	    ("%s: size mismatch\n", __func__));
144 	memcpy(l_regset->x, b_reg->x, sizeof(b_reg->x));
145 
146 	l_regset->x[30] = b_reg->lr;
147 	l_regset->sp = b_reg->sp;
148 	l_regset->pc = b_reg->elr;
149 	l_regset->cpsr = b_reg->spsr;
150 }
151 
152 void
153 linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset)
154 {
155 
156 	KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong),
157 	    ("%s: size mismatch\n", __func__));
158 
159 	memcpy(b_reg->x, l_regset->x, sizeof(b_reg->x));
160 	b_reg->sp = l_regset->sp;
161 	b_reg->elr = l_regset->pc;
162 	b_reg->spsr = l_regset->cpsr;
163 }
164 
165 void
166 linux_ptrace_get_syscall_info_machdep(const struct reg *reg,
167     struct syscall_info *si)
168 {
169 
170 	si->arch = LINUX_ARCH_AARCH64;
171 	si->instruction_pointer = reg->lr;
172 	si->stack_pointer = reg->sp;
173 }
174 
175 int
176 linux_ptrace_getregs_machdep(struct thread *td __unused, pid_t pid __unused,
177     struct linux_pt_regset *l_regset __unused)
178 {
179 
180 	return (0);
181 }
182 
183