xref: /netbsd/sys/arch/arm/arm/process_machdep.c (revision 6550d01e)
1 /*	$NetBSD: process_machdep.c,v 1.22 2009/11/21 20:32:17 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 1993 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * From:
35  *	Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel
36  */
37 
38 /*
39  * Copyright (c) 1995 Frank Lancaster.  All rights reserved.
40  * Copyright (c) 1995 Tools GmbH.  All rights reserved.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *      This product includes software developed by TooLs GmbH.
54  * 4. The name of TooLs GmbH may not be used to endorse or promote products
55  *    derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
71  * Copyright (c) 1993 Jan-Simon Pendry
72  * All rights reserved.
73  *
74  * This code is derived from software contributed to Berkeley by
75  * Jan-Simon Pendry.
76  *
77  * Redistribution and use in source and binary forms, with or without
78  * modification, are permitted provided that the following conditions
79  * are met:
80  * 1. Redistributions of source code must retain the above copyright
81  *    notice, this list of conditions and the following disclaimer.
82  * 2. Redistributions in binary form must reproduce the above copyright
83  *    notice, this list of conditions and the following disclaimer in the
84  *    documentation and/or other materials provided with the distribution.
85  * 3. All advertising materials mentioning features or use of this software
86  *    must display the following acknowledgement:
87  *	This product includes software developed by the University of
88  *	California, Berkeley and its contributors.
89  * 4. Neither the name of the University nor the names of its contributors
90  *    may be used to endorse or promote products derived from this software
91  *    without specific prior written permission.
92  *
93  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103  * SUCH DAMAGE.
104  *
105  * From:
106  *	Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel
107  */
108 
109 /*
110  * This file may seem a bit stylized, but that so that it's easier to port.
111  * Functions to be implemented here are:
112  *
113  * process_read_regs(proc, regs)
114  *	Get the current user-visible register set from the process
115  *	and copy it into the regs structure (<machine/reg.h>).
116  *	The process is stopped at the time read_regs is called.
117  *
118  * process_write_regs(proc, regs)
119  *	Update the current register set from the passed in regs
120  *	structure.  Take care to avoid clobbering special CPU
121  *	registers or privileged bits in the PSL.
122  *	The process is stopped at the time write_regs is called.
123  *
124  * process_sstep(proc, sstep)
125  *	Arrange for the process to trap or not trap depending on sstep
126  *	after executing a single instruction.
127  *
128  * process_set_pc(proc)
129  *	Set the process's program counter.
130  */
131 
132 #include "opt_armfpe.h"
133 
134 #include <sys/param.h>
135 
136 __KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.22 2009/11/21 20:32:17 rmind Exp $");
137 
138 #include <sys/proc.h>
139 #include <sys/ptrace.h>
140 #include <sys/systm.h>
141 
142 #include <machine/frame.h>
143 #include <machine/pcb.h>
144 #include <machine/reg.h>
145 
146 #include <arm/armreg.h>
147 
148 #ifdef ARMFPE
149 #include <arm/fpe-arm/armfpe.h>
150 #endif
151 
152 int
153 process_read_regs(struct lwp *l, struct reg *regs)
154 {
155 	struct trapframe *tf = process_frame(l);
156 
157 	KASSERT(tf != NULL);
158 	memcpy((void *)regs->r, (void *)&tf->tf_r0, sizeof(regs->r));
159 	regs->r_sp = tf->tf_usr_sp;
160 	regs->r_lr = tf->tf_usr_lr;
161 	regs->r_pc = tf->tf_pc;
162 	regs->r_cpsr = tf->tf_spsr;
163 
164 #ifdef THUMB_CODE
165 	if (tf->tf_spsr & PSR_T_bit)
166 		regs->r_pc |= 1;
167 #endif
168 #ifdef DIAGNOSTIC
169 	if ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE
170 	     && (tf->tf_spsr & IF32_bits))
171 		panic("process_read_regs: IRQs/FIQs blocked in user process");
172 #endif
173 
174 	return(0);
175 }
176 
177 int
178 process_read_fpregs(struct lwp *l, struct fpreg *regs)
179 {
180 #ifdef ARMFPE
181 	arm_fpe_getcontext(p, regs);
182 	return(0);
183 #else	/* ARMFPE */
184 	/* No hardware FP support */
185 	memset(regs, 0, sizeof(struct fpreg));
186 	return(0);
187 #endif	/* ARMFPE */
188 }
189 
190 int
191 process_write_regs(struct lwp *l, const struct reg *regs)
192 {
193 	struct trapframe *tf = process_frame(l);
194 
195 	KASSERT(tf != NULL);
196 	memcpy(&tf->tf_r0, regs->r, sizeof(regs->r));
197 	tf->tf_usr_sp = regs->r_sp;
198 	tf->tf_usr_lr = regs->r_lr;
199 #ifdef __PROG32
200 	tf->tf_pc = regs->r_pc;
201 	tf->tf_spsr &=  ~(PSR_FLAGS | PSR_T_bit);
202 	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
203 #ifdef THUMB_CODE
204 	if ((regs->r_pc & 1) || (regs->r_cpsr & PSR_T_bit))
205 		tf->tf_spsr |= PSR_T_bit;
206 #endif
207 #ifdef DIAGNOSTIC
208 	if ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE
209 	     && (tf->tf_spsr & IF32_bits))
210 		panic("process_read_regs: IRQs/FIQs blocked in user process");
211 #endif
212 #else /* __PROG26 */
213 	if ((regs->r_pc & (R15_MODE | R15_IRQ_DISABLE | R15_FIQ_DISABLE)) != 0)
214 		return EPERM;
215 
216 	tf->tf_r15 = regs->r_pc;
217 #endif
218 
219 	return(0);
220 }
221 
222 int
223 process_write_fpregs(struct lwp *l, const struct fpreg *regs)
224 {
225 #ifdef ARMFPE
226 	arm_fpe_setcontext(p, regs);
227 	return(0);
228 #else	/* ARMFPE */
229 	/* No hardware FP support */
230 	return(0);
231 #endif	/* ARMFPE */
232 }
233 
234 int
235 process_set_pc(struct lwp *l, void *addr)
236 {
237 	struct trapframe *tf = process_frame(l);
238 
239 	KASSERT(tf != NULL);
240 #ifdef __PROG32
241 	tf->tf_pc = (int)addr;
242 #ifdef THUMB_CODE
243 	if (((int)addr) & 1)
244 		tf->tf_spsr |= PSR_T_bit;
245 	else
246 		tf->tf_spsr &= ~PSR_T_bit;
247 #endif
248 #else /* __PROG26 */
249 	/* Only set the PC, not the PSR */
250 	if (((register_t)addr & R15_PC) != (register_t)addr)
251 		return EINVAL;
252 	tf->tf_r15 = (tf->tf_r15 & ~R15_PC) | (register_t)addr;
253 #endif
254 
255 	return (0);
256 }
257