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