xref: /netbsd-src/sys/arch/arm/arm/process_machdep.c (revision 496e2b659f2819a14d8716cb7e5992ffb8b0455f)
1*496e2b65Sskrll /*	$NetBSD: process_machdep.c,v 1.35 2021/02/01 19:31:34 skrll Exp $	*/
2aad01611Sagc 
3aad01611Sagc /*
4aad01611Sagc  * Copyright (c) 1993 The Regents of the University of California.
5aad01611Sagc  * All rights reserved.
6aad01611Sagc  *
7aad01611Sagc  * This code is derived from software contributed to Berkeley by
8aad01611Sagc  * Jan-Simon Pendry.
9aad01611Sagc  *
10aad01611Sagc  * Redistribution and use in source and binary forms, with or without
11aad01611Sagc  * modification, are permitted provided that the following conditions
12aad01611Sagc  * are met:
13aad01611Sagc  * 1. Redistributions of source code must retain the above copyright
14aad01611Sagc  *    notice, this list of conditions and the following disclaimer.
15aad01611Sagc  * 2. Redistributions in binary form must reproduce the above copyright
16aad01611Sagc  *    notice, this list of conditions and the following disclaimer in the
17aad01611Sagc  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
19aad01611Sagc  *    may be used to endorse or promote products derived from this software
20aad01611Sagc  *    without specific prior written permission.
21aad01611Sagc  *
22aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32aad01611Sagc  * SUCH DAMAGE.
33aad01611Sagc  *
34aad01611Sagc  * From:
35aad01611Sagc  *	Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel
36aad01611Sagc  */
37a4e55e0fSbjh21 
38a4e55e0fSbjh21 /*
39a4e55e0fSbjh21  * Copyright (c) 1995 Frank Lancaster.  All rights reserved.
40a4e55e0fSbjh21  * Copyright (c) 1995 Tools GmbH.  All rights reserved.
41fc20faecSws  * All rights reserved.
42fc20faecSws  *
43fc20faecSws  * Redistribution and use in source and binary forms, with or without
44fc20faecSws  * modification, are permitted provided that the following conditions
45fc20faecSws  * are met:
46fc20faecSws  * 1. Redistributions of source code must retain the above copyright
47fc20faecSws  *    notice, this list of conditions and the following disclaimer.
48fc20faecSws  * 2. Redistributions in binary form must reproduce the above copyright
49fc20faecSws  *    notice, this list of conditions and the following disclaimer in the
50fc20faecSws  *    documentation and/or other materials provided with the distribution.
51fc20faecSws  * 3. All advertising materials mentioning features or use of this software
52fc20faecSws  *    must display the following acknowledgement:
53fc20faecSws  *      This product includes software developed by TooLs GmbH.
54fc20faecSws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
55fc20faecSws  *    derived from this software without specific prior written permission.
56fc20faecSws  *
57fc20faecSws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58fc20faecSws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59fc20faecSws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60fc20faecSws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61fc20faecSws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62fc20faecSws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63fc20faecSws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64fc20faecSws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65fc20faecSws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66fc20faecSws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67fc20faecSws  */
68fc20faecSws 
69fc20faecSws /*
70a4e55e0fSbjh21  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
71a4e55e0fSbjh21  * Copyright (c) 1993 Jan-Simon Pendry
72a4e55e0fSbjh21  * All rights reserved.
73a4e55e0fSbjh21  *
74a4e55e0fSbjh21  * This code is derived from software contributed to Berkeley by
75a4e55e0fSbjh21  * Jan-Simon Pendry.
76a4e55e0fSbjh21  *
77a4e55e0fSbjh21  * Redistribution and use in source and binary forms, with or without
78a4e55e0fSbjh21  * modification, are permitted provided that the following conditions
79a4e55e0fSbjh21  * are met:
80a4e55e0fSbjh21  * 1. Redistributions of source code must retain the above copyright
81a4e55e0fSbjh21  *    notice, this list of conditions and the following disclaimer.
82a4e55e0fSbjh21  * 2. Redistributions in binary form must reproduce the above copyright
83a4e55e0fSbjh21  *    notice, this list of conditions and the following disclaimer in the
84a4e55e0fSbjh21  *    documentation and/or other materials provided with the distribution.
85a4e55e0fSbjh21  * 3. All advertising materials mentioning features or use of this software
86a4e55e0fSbjh21  *    must display the following acknowledgement:
87a4e55e0fSbjh21  *	This product includes software developed by the University of
88a4e55e0fSbjh21  *	California, Berkeley and its contributors.
89a4e55e0fSbjh21  * 4. Neither the name of the University nor the names of its contributors
90a4e55e0fSbjh21  *    may be used to endorse or promote products derived from this software
91a4e55e0fSbjh21  *    without specific prior written permission.
92a4e55e0fSbjh21  *
93a4e55e0fSbjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94a4e55e0fSbjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95a4e55e0fSbjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96a4e55e0fSbjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97a4e55e0fSbjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98a4e55e0fSbjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99a4e55e0fSbjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100a4e55e0fSbjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101a4e55e0fSbjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102a4e55e0fSbjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103a4e55e0fSbjh21  * SUCH DAMAGE.
104a4e55e0fSbjh21  *
105a4e55e0fSbjh21  * From:
106a4e55e0fSbjh21  *	Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel
107a4e55e0fSbjh21  */
108a4e55e0fSbjh21 
109a4e55e0fSbjh21 /*
110a4e55e0fSbjh21  * This file may seem a bit stylized, but that so that it's easier to port.
111a4e55e0fSbjh21  * Functions to be implemented here are:
112a4e55e0fSbjh21  *
113a4e55e0fSbjh21  * process_read_regs(proc, regs)
114a4e55e0fSbjh21  *	Get the current user-visible register set from the process
115a4e55e0fSbjh21  *	and copy it into the regs structure (<machine/reg.h>).
116a4e55e0fSbjh21  *	The process is stopped at the time read_regs is called.
117a4e55e0fSbjh21  *
118a4e55e0fSbjh21  * process_write_regs(proc, regs)
119a4e55e0fSbjh21  *	Update the current register set from the passed in regs
120a4e55e0fSbjh21  *	structure.  Take care to avoid clobbering special CPU
121a4e55e0fSbjh21  *	registers or privileged bits in the PSL.
122a4e55e0fSbjh21  *	The process is stopped at the time write_regs is called.
123a4e55e0fSbjh21  *
124a4e55e0fSbjh21  * process_sstep(proc, sstep)
125a4e55e0fSbjh21  *	Arrange for the process to trap or not trap depending on sstep
126a4e55e0fSbjh21  *	after executing a single instruction.
127a4e55e0fSbjh21  *
128a4e55e0fSbjh21  * process_set_pc(proc)
129a4e55e0fSbjh21  *	Set the process's program counter.
130a4e55e0fSbjh21  */
131a4e55e0fSbjh21 
1328e8fe15aSchristos #ifdef _KERNEL_OPT
133a4e55e0fSbjh21 #include "opt_armfpe.h"
1348e8fe15aSchristos #endif
135a4e55e0fSbjh21 
136a4e55e0fSbjh21 #include <sys/param.h>
137a4e55e0fSbjh21 
138*496e2b65Sskrll __KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.35 2021/02/01 19:31:34 skrll Exp $");
139a4e55e0fSbjh21 
140a4e55e0fSbjh21 #include <sys/proc.h>
141a4e55e0fSbjh21 #include <sys/ptrace.h>
142a4e55e0fSbjh21 #include <sys/systm.h>
143a4e55e0fSbjh21 
144a4e55e0fSbjh21 #include <arm/armreg.h>
145cb733fbbSmatt #include <arm/vfpreg.h>
146ab152917Smatt #include <arm/locore.h>
147ab152917Smatt 
148ab152917Smatt #include <machine/pcb.h>
149ab152917Smatt #include <machine/reg.h>
150a4e55e0fSbjh21 
151a4e55e0fSbjh21 int
process_read_regs(struct lwp * l,struct reg * regs)15223bc2503Sthorpej process_read_regs(struct lwp *l, struct reg *regs)
153a4e55e0fSbjh21 {
154f622e63aSmatt 	struct trapframe * const tf = lwp_trapframe(l);
155a4e55e0fSbjh21 
156a4e55e0fSbjh21 	KASSERT(tf != NULL);
157e2cb8590Scegger 	memcpy((void *)regs->r, (void *)&tf->tf_r0, sizeof(regs->r));
158a4e55e0fSbjh21 	regs->r_sp = tf->tf_usr_sp;
159a4e55e0fSbjh21 	regs->r_lr = tf->tf_usr_lr;
160a4e55e0fSbjh21 	regs->r_pc = tf->tf_pc;
161a4e55e0fSbjh21 	regs->r_cpsr = tf->tf_spsr;
162a4e55e0fSbjh21 
163*496e2b65Sskrll 	KASSERT(VALID_PSR(tf->tf_spsr));
164c97e21f5Smatt 
1654cdf758fSrearnsha #ifdef THUMB_CODE
1664cdf758fSrearnsha 	if (tf->tf_spsr & PSR_T_bit)
1674cdf758fSrearnsha 		regs->r_pc |= 1;
1684cdf758fSrearnsha #endif
169b044d8f6Srearnsha 
170439b8760Sskrll 	return 0;
171a4e55e0fSbjh21 }
172a4e55e0fSbjh21 
173a4e55e0fSbjh21 int
process_read_fpregs(struct lwp * l,struct fpreg * regs,size_t * sz)17495739e63Sdsl process_read_fpregs(struct lwp *l, struct fpreg *regs, size_t *sz)
175a4e55e0fSbjh21 {
176cb72f657Smatt #ifdef FPU_VFP
177cb72f657Smatt 	if (curcpu()->ci_vfp_id == 0) {
178c5c215bbSskrll 		memset(regs, 0, sizeof(*regs));
179cb733fbbSmatt 		return 0;
180cb72f657Smatt 	}
181cb72f657Smatt 	const struct pcb * const pcb = lwp_getpcb(l);
182877a3ccfSchs 	vfp_savecontext(l);
183cb72f657Smatt 	regs->fpr_vfp = pcb->pcb_vfp;
184cb733fbbSmatt 	regs->fpr_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
185cb72f657Smatt #endif
186cb733fbbSmatt 	return 0;
187a4e55e0fSbjh21 }
188a4e55e0fSbjh21 
189a4e55e0fSbjh21 int
process_write_regs(struct lwp * l,const struct reg * regs)1908f4aee20Sscw process_write_regs(struct lwp *l, const struct reg *regs)
191a4e55e0fSbjh21 {
192f622e63aSmatt 	struct trapframe * const tf = lwp_trapframe(l);
193a4e55e0fSbjh21 
194a4e55e0fSbjh21 	KASSERT(tf != NULL);
195e2cb8590Scegger 	memcpy(&tf->tf_r0, regs->r, sizeof(regs->r));
196a4e55e0fSbjh21 	tf->tf_usr_sp = regs->r_sp;
197a4e55e0fSbjh21 	tf->tf_usr_lr = regs->r_lr;
198a4e55e0fSbjh21 	tf->tf_pc = regs->r_pc;
1994cdf758fSrearnsha 	tf->tf_spsr &=  ~(PSR_FLAGS | PSR_T_bit);
200a4e55e0fSbjh21 	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
2014cdf758fSrearnsha #ifdef THUMB_CODE
2024cdf758fSrearnsha 	if ((regs->r_pc & 1) || (regs->r_cpsr & PSR_T_bit))
2034cdf758fSrearnsha 		tf->tf_spsr |= PSR_T_bit;
2044cdf758fSrearnsha #endif
205*496e2b65Sskrll 	KASSERT(VALID_PSR(tf->tf_spsr));
206a4e55e0fSbjh21 
207439b8760Sskrll 	return 0;
208a4e55e0fSbjh21 }
209a4e55e0fSbjh21 
210a4e55e0fSbjh21 int
process_write_fpregs(struct lwp * l,const struct fpreg * regs,size_t sz)21195739e63Sdsl process_write_fpregs(struct lwp *l, const struct fpreg *regs, size_t sz)
212a4e55e0fSbjh21 {
213cb72f657Smatt #ifdef FPU_VFP
214cb72f657Smatt 	if (curcpu()->ci_vfp_id == 0) {
215cb72f657Smatt 		return EINVAL;
216cb72f657Smatt 	}
217cb72f657Smatt 	struct pcb * const pcb = lwp_getpcb(l);
218877a3ccfSchs 	vfp_discardcontext(l, true);
219cb72f657Smatt 	pcb->pcb_vfp = regs->fpr_vfp;
220cb733fbbSmatt 	pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
221cb72f657Smatt #endif
222439b8760Sskrll 	return 0;
223a4e55e0fSbjh21 }
224a4e55e0fSbjh21 
225a4e55e0fSbjh21 int
process_set_pc(struct lwp * l,void * addr)22653524e44Schristos process_set_pc(struct lwp *l, void *addr)
227a4e55e0fSbjh21 {
228f622e63aSmatt 	struct trapframe * const tf = lwp_trapframe(l);
229a4e55e0fSbjh21 
230a4e55e0fSbjh21 	KASSERT(tf != NULL);
231a4e55e0fSbjh21 	tf->tf_pc = (int)addr;
2324cdf758fSrearnsha #ifdef THUMB_CODE
2334cdf758fSrearnsha 	if (((int)addr) & 1)
2344cdf758fSrearnsha 		tf->tf_spsr |= PSR_T_bit;
2354cdf758fSrearnsha 	else
2364cdf758fSrearnsha 		tf->tf_spsr &= ~PSR_T_bit;
2374cdf758fSrearnsha #endif
238a4e55e0fSbjh21 
239439b8760Sskrll 	return 0;
240a4e55e0fSbjh21 }
241