xref: /freebsd/sys/arm64/linux/linux_machdep.c (revision 027d727d4071435376017d3789fe603a8aec805a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #include <sys/param.h>
30 #include <sys/proc.h>
31 #include <sys/ptrace.h>
32 #include <sys/reg.h>
33 
34 #include <vm/vm_param.h>
35 
36 #include <arm64/linux/linux.h>
37 #include <arm64/linux/linux_proto.h>
38 #include <compat/linux/linux_fork.h>
39 #include <compat/linux/linux_misc.h>
40 #include <compat/linux/linux_util.h>
41 
42 #define	LINUX_ARCH_AARCH64		0xc00000b7
43 
44 
45 int
linux_set_upcall(struct thread * td,register_t stack)46 linux_set_upcall(struct thread *td, register_t stack)
47 {
48 
49 	if (stack)
50 		td->td_frame->tf_sp = stack;
51 
52 	/*
53 	 * The newly created Linux thread returns
54 	 * to the user space by the same path that a parent does.
55 	 */
56 	td->td_frame->tf_x[0] = 0;
57 	return (0);
58 }
59 
60 int
linux_set_cloned_tls(struct thread * td,void * desc)61 linux_set_cloned_tls(struct thread *td, void *desc)
62 {
63 
64 	if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
65 		return (EPERM);
66 
67 	return (cpu_set_user_tls(td, desc));
68 }
69 
70 void
bsd_to_linux_regset(const struct reg * b_reg,struct linux_pt_regset * l_regset)71 bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset)
72 {
73 
74 	KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong),
75 	    ("%s: size mismatch\n", __func__));
76 	memcpy(l_regset->x, b_reg->x, sizeof(b_reg->x));
77 
78 	l_regset->x[30] = b_reg->lr;
79 	l_regset->sp = b_reg->sp;
80 	l_regset->pc = b_reg->elr;
81 	l_regset->cpsr = b_reg->spsr;
82 }
83 
84 void
linux_to_bsd_regset(struct reg * b_reg,const struct linux_pt_regset * l_regset)85 linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset)
86 {
87 
88 	KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong),
89 	    ("%s: size mismatch\n", __func__));
90 
91 	memcpy(b_reg->x, l_regset->x, sizeof(b_reg->x));
92 	b_reg->sp = l_regset->sp;
93 	b_reg->elr = l_regset->pc;
94 	b_reg->spsr = l_regset->cpsr;
95 }
96 
97 void
linux_ptrace_get_syscall_info_machdep(const struct reg * reg,struct syscall_info * si)98 linux_ptrace_get_syscall_info_machdep(const struct reg *reg,
99     struct syscall_info *si)
100 {
101 
102 	si->arch = LINUX_ARCH_AARCH64;
103 	si->instruction_pointer = reg->lr;
104 	si->stack_pointer = reg->sp;
105 }
106 
107 int
linux_ptrace_getregs_machdep(struct thread * td __unused,pid_t pid __unused,struct linux_pt_regset * l_regset __unused)108 linux_ptrace_getregs_machdep(struct thread *td __unused, pid_t pid __unused,
109     struct linux_pt_regset *l_regset __unused)
110 {
111 
112 	return (0);
113 }
114 
115 int
linux_ptrace_peekuser(struct thread * td,pid_t pid,void * addr,void * data)116 linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data)
117 {
118 
119 	LINUX_RATELIMIT_MSG_OPT1("PTRACE_PEEKUSER offset %ld not implemented; "
120 	    "returning EINVAL", (uintptr_t)addr);
121 	return (EINVAL);
122 }
123 
124 int
linux_ptrace_pokeuser(struct thread * td,pid_t pid,void * addr,void * data)125 linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data)
126 {
127 
128 	LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld "
129 	    "not implemented; returning EINVAL", (uintptr_t)addr);
130 	return (EINVAL);
131 }
132