xref: /freebsd/sys/arm64/arm64/ptrace_machdep.c (revision f126890a)
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/param.h>
29 #include <sys/systm.h>
30 #include <sys/elf.h>
31 #include <sys/exec.h>
32 #include <sys/imgact.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/ptrace.h>
39 #include <sys/reg.h>
40 #include <sys/rwlock.h>
41 #include <sys/signalvar.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysent.h>
44 #include <sys/sysproto.h>
45 #include <sys/ucontext.h>
46 
47 #include <machine/armreg.h>
48 #include <machine/pcb.h>
49 
50 /* Only used to get/set 32bits VFP regs */
51 int
52 cpu_ptrace(struct thread *td, int req, void *arg, int data)
53 {
54 #if defined(VFP) && defined(COMPAT_FREEBSD32)
55 	mcontext32_vfp_t vfp;
56 	int error;
57 
58 	if (!SV_CURPROC_FLAG(SV_ILP32))
59 		return (EINVAL);
60 	switch (req) {
61 		case PT_GETVFPREGS32:
62 			get_fpcontext32(td, &vfp);
63 			error = copyout(&vfp, arg, sizeof(vfp));
64 			break;
65 		case PT_SETVFPREGS32:
66 			error = copyin(arg, &vfp, sizeof(vfp));
67 			if (error == 0)
68 				set_fpcontext32(td, &vfp);
69 			break;
70 		default:
71 			error = EINVAL;
72 	}
73 
74 	return (error);
75 #else
76 	return (EINVAL);
77 #endif
78 }
79 
80 #if defined(VFP) && defined(COMPAT_FREEBSD32)
81 static bool
82 get_arm_vfp(struct regset *rs, struct thread *td, void *buf, size_t *sizep)
83 {
84 	if (buf != NULL) {
85 		KASSERT(*sizep == sizeof(mcontext32_vfp_t),
86 		    ("%s: invalid size", __func__));
87 		get_fpcontext32(td, buf);
88 	}
89 	*sizep = sizeof(mcontext32_vfp_t);
90 	return (true);
91 }
92 
93 static bool
94 set_arm_vfp(struct regset *rs, struct thread *td, void *buf,
95     size_t size)
96 {
97 	KASSERT(size == sizeof(mcontext32_vfp_t), ("%s: invalid size",
98 	    __func__));
99 	set_fpcontext32(td, buf);
100 	return (true);
101 }
102 
103 static struct regset regset_arm_vfp = {
104 	.note = NT_ARM_VFP,
105 	.size = sizeof(mcontext32_vfp_t),
106 	.get = get_arm_vfp,
107 	.set = set_arm_vfp,
108 };
109 ELF32_REGSET(regset_arm_vfp);
110 #endif
111 
112 static bool
113 get_arm64_tls(struct regset *rs, struct thread *td, void *buf,
114     size_t *sizep)
115 {
116 	if (buf != NULL) {
117 		KASSERT(*sizep == sizeof(td->td_pcb->pcb_tpidr_el0),
118 		    ("%s: invalid size", __func__));
119 		memcpy(buf, &td->td_pcb->pcb_tpidr_el0,
120 		    sizeof(td->td_pcb->pcb_tpidr_el0));
121 	}
122 	*sizep = sizeof(td->td_pcb->pcb_tpidr_el0);
123 
124 	return (true);
125 }
126 
127 static struct regset regset_arm64_tls = {
128 	.note = NT_ARM_TLS,
129 	.size = sizeof(uint64_t),
130 	.get = get_arm64_tls,
131 };
132 ELF_REGSET(regset_arm64_tls);
133 
134 #ifdef COMPAT_FREEBSD32
135 static bool
136 get_arm_tls(struct regset *rs, struct thread *td, void *buf,
137     size_t *sizep)
138 {
139 	if (buf != NULL) {
140 		uint32_t tp;
141 
142 		KASSERT(*sizep == sizeof(uint32_t),
143 		    ("%s: invalid size", __func__));
144 		tp = (uint32_t)td->td_pcb->pcb_tpidr_el0;
145 		memcpy(buf, &tp, sizeof(tp));
146 	}
147 	*sizep = sizeof(uint32_t);
148 
149 	return (true);
150 }
151 
152 static struct regset regset_arm_tls = {
153 	.note = NT_ARM_TLS,
154 	.size = sizeof(uint32_t),
155 	.get = get_arm_tls,
156 };
157 ELF32_REGSET(regset_arm_tls);
158 #endif
159 
160 int
161 ptrace_set_pc(struct thread *td, u_long addr)
162 {
163 
164 	td->td_frame->tf_elr = addr;
165 	return (0);
166 }
167 
168 int
169 ptrace_single_step(struct thread *td)
170 {
171 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
172 	if ((td->td_frame->tf_spsr & PSR_SS) == 0) {
173 		td->td_frame->tf_spsr |= PSR_SS;
174 		td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
175 		td->td_dbgflags |= TDB_STEP;
176 	}
177 	return (0);
178 }
179 
180 int
181 ptrace_clear_single_step(struct thread *td)
182 {
183 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
184 	td->td_frame->tf_spsr &= ~PSR_SS;
185 	td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
186 	td->td_dbgflags &= ~TDB_STEP;
187 	return (0);
188 }
189 
190