]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/ptrace_machdep.c
zfs: merge openzfs/zfs@21bd76613 (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / ptrace_machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/elf.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/ptrace.h>
42 #include <sys/reg.h>
43 #include <sys/rwlock.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
48 #include <sys/ucontext.h>
49
50 #include <machine/armreg.h>
51
52 /* Only used to get/set 32bits VFP regs */
53 int
54 cpu_ptrace(struct thread *td, int req, void *arg, int data)
55 {
56 #if defined(VFP) && defined(COMPAT_FREEBSD32)
57         mcontext32_vfp_t vfp;
58         int error;
59
60         if (!SV_CURPROC_FLAG(SV_ILP32))
61                 return (EINVAL);
62         switch (req) {
63                 case PT_GETVFPREGS32:
64                         get_fpcontext32(td, &vfp);
65                         error = copyout(&vfp, arg, sizeof(vfp));
66                         break;
67                 case PT_SETVFPREGS32:
68                         error = copyin(arg, &vfp, sizeof(vfp));
69                         if (error == 0)
70                                 set_fpcontext32(td, &vfp);
71                         break;
72                 default:
73                         error = EINVAL;
74         }
75
76         return (error);
77 #else
78         return (EINVAL);
79 #endif
80 }
81
82 #if defined(VFP) && defined(COMPAT_FREEBSD32)
83 static bool
84 get_arm_vfp(struct regset *rs, struct thread *td, void *buf, size_t *sizep)
85 {
86         if (buf != NULL) {
87                 KASSERT(*sizep == sizeof(mcontext32_vfp_t),
88                     ("%s: invalid size", __func__));
89                 get_fpcontext32(td, buf);
90         }
91         *sizep = sizeof(mcontext32_vfp_t);
92         return (true);
93 }
94
95 static bool
96 set_arm_vfp(struct regset *rs, struct thread *td, void *buf,
97     size_t size)
98 {
99         KASSERT(size == sizeof(mcontext32_vfp_t), ("%s: invalid size",
100             __func__));
101         set_fpcontext32(td, buf);
102         return (true);
103 }
104
105 static struct regset regset_arm_vfp = {
106         .note = NT_ARM_VFP,
107         .size = sizeof(mcontext32_vfp_t),
108         .get = get_arm_vfp,
109         .set = set_arm_vfp,
110 };
111 ELF32_REGSET(regset_arm_vfp);
112 #endif
113
114 static bool
115 get_arm64_tls(struct regset *rs, struct thread *td, void *buf,
116     size_t *sizep)
117 {
118         if (buf != NULL) {
119                 KASSERT(*sizep == sizeof(td->td_pcb->pcb_tpidr_el0),
120                     ("%s: invalid size", __func__));
121                 memcpy(buf, &td->td_pcb->pcb_tpidr_el0,
122                     sizeof(td->td_pcb->pcb_tpidr_el0));
123         }
124         *sizep = sizeof(td->td_pcb->pcb_tpidr_el0);
125
126         return (true);
127 }
128
129 static struct regset regset_arm64_tls = {
130         .note = NT_ARM_TLS,
131         .size = sizeof(uint64_t),
132         .get = get_arm64_tls,
133 };
134 ELF_REGSET(regset_arm64_tls);
135
136 #ifdef COMPAT_FREEBSD32
137 static bool
138 get_arm_tls(struct regset *rs, struct thread *td, void *buf,
139     size_t *sizep)
140 {
141         if (buf != NULL) {
142                 uint32_t tp;
143
144                 KASSERT(*sizep == sizeof(uint32_t),
145                     ("%s: invalid size", __func__));
146                 tp = (uint32_t)td->td_pcb->pcb_tpidr_el0;
147                 memcpy(buf, &tp, sizeof(tp));
148         }
149         *sizep = sizeof(uint32_t);
150
151         return (true);
152 }
153
154 static struct regset regset_arm_tls = {
155         .note = NT_ARM_TLS,
156         .size = sizeof(uint32_t),
157         .get = get_arm_tls,
158 };
159 ELF32_REGSET(regset_arm_tls);
160 #endif
161
162 int
163 ptrace_set_pc(struct thread *td, u_long addr)
164 {
165
166         td->td_frame->tf_elr = addr;
167         return (0);
168 }
169
170 int
171 ptrace_single_step(struct thread *td)
172 {
173         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
174         if ((td->td_frame->tf_spsr & PSR_SS) == 0) {
175                 td->td_frame->tf_spsr |= PSR_SS;
176                 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
177                 td->td_dbgflags |= TDB_STEP;
178         }
179         return (0);
180 }
181
182 int
183 ptrace_clear_single_step(struct thread *td)
184 {
185         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
186         td->td_frame->tf_spsr &= ~PSR_SS;
187         td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
188         td->td_dbgflags &= ~TDB_STEP;
189         return (0);
190 }