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 * $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/ktr.h> 37 #include <sys/proc.h> 38 #include <sys/ptrace.h> 39 #include <sys/reg.h> 40 #include <sys/sdt.h> 41 42 #include <vm/vm_param.h> 43 44 #include <arm64/linux/linux.h> 45 #include <arm64/linux/linux_proto.h> 46 #include <compat/linux/linux_dtrace.h> 47 #include <compat/linux/linux_fork.h> 48 #include <compat/linux/linux_misc.h> 49 #include <compat/linux/linux_mmap.h> 50 #include <compat/linux/linux_util.h> 51 52 #define LINUX_ARCH_AARCH64 0xc00000b7 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 int 61 linux_set_upcall(struct thread *td, register_t stack) 62 { 63 64 if (stack) 65 td->td_frame->tf_sp = stack; 66 67 /* 68 * The newly created Linux thread returns 69 * to the user space by the same path that a parent does. 70 */ 71 td->td_frame->tf_x[0] = 0; 72 return (0); 73 } 74 75 /* LINUXTODO: deduplicate arm64 linux_mmap2 */ 76 int 77 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap) 78 { 79 80 LIN_SDT_PROBE0(machdep, linux_mmap2, todo); 81 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, 82 uap->flags, uap->fd, uap->pgoff)); 83 } 84 85 int 86 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 87 { 88 89 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, 90 uap->prot)); 91 } 92 93 int 94 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 95 { 96 97 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav)); 98 } 99 100 int 101 linux_set_cloned_tls(struct thread *td, void *desc) 102 { 103 104 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 105 return (EPERM); 106 107 return (cpu_set_user_tls(td, desc)); 108 } 109 110 void 111 bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset) 112 { 113 114 KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong), 115 ("%s: size mismatch\n", __func__)); 116 memcpy(l_regset->x, b_reg->x, sizeof(b_reg->x)); 117 118 l_regset->x[30] = b_reg->lr; 119 l_regset->sp = b_reg->sp; 120 l_regset->pc = b_reg->elr; 121 l_regset->cpsr = b_reg->spsr; 122 } 123 124 void 125 linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset) 126 { 127 128 KASSERT(sizeof(l_regset->x) == sizeof(b_reg->x) + sizeof(l_ulong), 129 ("%s: size mismatch\n", __func__)); 130 131 memcpy(b_reg->x, l_regset->x, sizeof(b_reg->x)); 132 b_reg->sp = l_regset->sp; 133 b_reg->elr = l_regset->pc; 134 b_reg->spsr = l_regset->cpsr; 135 } 136 137 void 138 linux_ptrace_get_syscall_info_machdep(const struct reg *reg, 139 struct syscall_info *si) 140 { 141 142 si->arch = LINUX_ARCH_AARCH64; 143 si->instruction_pointer = reg->lr; 144 si->stack_pointer = reg->sp; 145 } 146 147 int 148 linux_ptrace_getregs_machdep(struct thread *td __unused, pid_t pid __unused, 149 struct linux_pt_regset *l_regset __unused) 150 { 151 152 return (0); 153 } 154 155 int 156 linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data) 157 { 158 159 LINUX_RATELIMIT_MSG_OPT1("PTRACE_PEEKUSER offset %ld not implemented; " 160 "returning EINVAL", (uintptr_t)addr); 161 return (EINVAL); 162 } 163 164 int 165 linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data) 166 { 167 168 LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld " 169 "not implemented; returning EINVAL", (uintptr_t)addr); 170 return (EINVAL); 171 } 172