xref: /linux/arch/hexagon/kernel/ptrace.c (revision 905e46acd3272d04566fec49afbd7ad9e2ed9ae3)
1 /*
2  * Ptrace support for Hexagon
3  *
4  * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 #include <generated/compile.h>
22 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/sched/task_stack.h>
26 #include <linux/mm.h>
27 #include <linux/smp.h>
28 #include <linux/errno.h>
29 #include <linux/ptrace.h>
30 #include <linux/regset.h>
31 #include <linux/user.h>
32 #include <linux/elf.h>
33 
34 #include <asm/user.h>
35 
36 #if arch_has_single_step()
37 /*  Both called from ptrace_resume  */
38 void user_enable_single_step(struct task_struct *child)
39 {
40 	pt_set_singlestep(task_pt_regs(child));
41 	set_tsk_thread_flag(child, TIF_SINGLESTEP);
42 }
43 
44 void user_disable_single_step(struct task_struct *child)
45 {
46 	pt_clr_singlestep(task_pt_regs(child));
47 	clear_tsk_thread_flag(child, TIF_SINGLESTEP);
48 }
49 #endif
50 
51 static int genregs_get(struct task_struct *target,
52 		   const struct user_regset *regset,
53 		   unsigned int pos, unsigned int count,
54 		   void *kbuf, void __user *ubuf)
55 {
56 	int ret;
57 	unsigned int dummy;
58 	struct pt_regs *regs = task_pt_regs(target);
59 
60 
61 	if (!regs)
62 		return -EIO;
63 
64 	/* The general idea here is that the copyout must happen in
65 	 * exactly the same order in which the userspace expects these
66 	 * regs. Now, the sequence in userspace does not match the
67 	 * sequence in the kernel, so everything past the 32 gprs
68 	 * happens one at a time.
69 	 */
70 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
71 				  &regs->r00, 0, 32*sizeof(unsigned long));
72 
73 #define ONEXT(KPT_REG, USR_REG) \
74 	if (!ret) \
75 		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, \
76 			KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
77 			offsetof(struct user_regs_struct, USR_REG) + \
78 				 sizeof(unsigned long));
79 
80 	/* Must be exactly same sequence as struct user_regs_struct */
81 	ONEXT(&regs->sa0, sa0);
82 	ONEXT(&regs->lc0, lc0);
83 	ONEXT(&regs->sa1, sa1);
84 	ONEXT(&regs->lc1, lc1);
85 	ONEXT(&regs->m0, m0);
86 	ONEXT(&regs->m1, m1);
87 	ONEXT(&regs->usr, usr);
88 	ONEXT(&regs->preds, p3_0);
89 	ONEXT(&regs->gp, gp);
90 	ONEXT(&regs->ugp, ugp);
91 	ONEXT(&pt_elr(regs), pc);
92 	dummy = pt_cause(regs);
93 	ONEXT(&dummy, cause);
94 	ONEXT(&pt_badva(regs), badva);
95 #if CONFIG_HEXAGON_ARCH_VERSION >=4
96 	ONEXT(&regs->cs0, cs0);
97 	ONEXT(&regs->cs1, cs1);
98 #endif
99 
100 	/* Pad the rest with zeros, if needed */
101 	if (!ret)
102 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
103 					offsetof(struct user_regs_struct, pad1), -1);
104 	return ret;
105 }
106 
107 static int genregs_set(struct task_struct *target,
108 		   const struct user_regset *regset,
109 		   unsigned int pos, unsigned int count,
110 		   const void *kbuf, const void __user *ubuf)
111 {
112 	int ret;
113 	unsigned long bucket;
114 	struct pt_regs *regs = task_pt_regs(target);
115 
116 	if (!regs)
117 		return -EIO;
118 
119 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
120 				 &regs->r00, 0, 32*sizeof(unsigned long));
121 
122 #define INEXT(KPT_REG, USR_REG) \
123 	if (!ret) \
124 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
125 			KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
126 			offsetof(struct user_regs_struct, USR_REG) + \
127 				sizeof(unsigned long));
128 
129 	/* Must be exactly same sequence as struct user_regs_struct */
130 	INEXT(&regs->sa0, sa0);
131 	INEXT(&regs->lc0, lc0);
132 	INEXT(&regs->sa1, sa1);
133 	INEXT(&regs->lc1, lc1);
134 	INEXT(&regs->m0, m0);
135 	INEXT(&regs->m1, m1);
136 	INEXT(&regs->usr, usr);
137 	INEXT(&regs->preds, p3_0);
138 	INEXT(&regs->gp, gp);
139 	INEXT(&regs->ugp, ugp);
140 	INEXT(&pt_elr(regs), pc);
141 
142 	/* CAUSE and BADVA aren't writeable. */
143 	INEXT(&bucket, cause);
144 	INEXT(&bucket, badva);
145 
146 #if CONFIG_HEXAGON_ARCH_VERSION >=4
147 	INEXT(&regs->cs0, cs0);
148 	INEXT(&regs->cs1, cs1);
149 #endif
150 
151 	/* Ignore the rest, if needed */
152 	if (!ret)
153 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
154 					offsetof(struct user_regs_struct, pad1), -1);
155 
156 	if (ret)
157 		return ret;
158 
159 	/*
160 	 * This is special; SP is actually restored by the VM via the
161 	 * special event record which is set by the special trap.
162 	 */
163 	regs->hvmer.vmpsp = regs->r29;
164 	return 0;
165 }
166 
167 enum hexagon_regset {
168 	REGSET_GENERAL,
169 };
170 
171 static const struct user_regset hexagon_regsets[] = {
172 	[REGSET_GENERAL] = {
173 		.core_note_type = NT_PRSTATUS,
174 		.n = ELF_NGREG,
175 		.size = sizeof(unsigned long),
176 		.align = sizeof(unsigned long),
177 		.get = genregs_get,
178 		.set = genregs_set,
179 	},
180 };
181 
182 static const struct user_regset_view hexagon_user_view = {
183 	.name = UTS_MACHINE,
184 	.e_machine = ELF_ARCH,
185 	.ei_osabi = ELF_OSABI,
186 	.regsets = hexagon_regsets,
187 	.e_flags = ELF_CORE_EFLAGS,
188 	.n = ARRAY_SIZE(hexagon_regsets)
189 };
190 
191 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
192 {
193 	return &hexagon_user_view;
194 }
195 
196 void ptrace_disable(struct task_struct *child)
197 {
198 	/* Boilerplate - resolves to null inline if no HW single-step */
199 	user_disable_single_step(child);
200 }
201 
202 long arch_ptrace(struct task_struct *child, long request,
203 		 unsigned long addr, unsigned long data)
204 {
205 	return ptrace_request(child, request, addr, data);
206 }
207