1 /*	$OpenBSD: process_machdep.c,v 1.15 2024/03/29 21:14:31 miod Exp $	*/
2 /*	$NetBSD: process_machdep.c,v 1.10 2000/09/26 22:05:50 eeh Exp $ */
3 
4 /*
5  * Copyright (c) 1993 The Regents of the University of California.
6  * Copyright (c) 1993 Jan-Simon Pendry
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * from: Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel
37  */
38 
39 /*
40  * This file may seem a bit stylized, but that so that it's easier to port.
41  * Functions to be implemented here are:
42  *
43  * process_read_regs(proc, regs)
44  *	Get the current user-visible register set from the process
45  *	and copy it into the regs structure (<machine/reg.h>).
46  *	The process is stopped at the time read_regs is called.
47  *
48  * process_write_regs(proc, regs)
49  *	Update the current register set from the passed in regs
50  *	structure.  Take care to avoid clobbering special CPU
51  *	registers or privileged bits in the PSL.
52  *	The process is stopped at the time write_regs is called.
53  *
54  * process_sstep(proc)
55  *	Arrange for the process to trap after executing a single instruction.
56  *
57  * process_set_pc(proc)
58  *	Set the process's program counter.
59  */
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/time.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/proc.h>
67 #include <sys/user.h>
68 #include <sys/vnode.h>
69 #include <machine/psl.h>
70 #include <machine/reg.h>
71 #include <machine/frame.h>
72 #include <sys/ptrace.h>
73 
74 /* Unfortunately we need to convert struct trapframe to struct reg */
75 int
process_read_regs(struct proc * p,struct reg * regs)76 process_read_regs(struct proc *p, struct reg *regs)
77 {
78 	struct trapframe *tf = p->p_md.md_tf;
79 	int i;
80 
81 	/* copy out regs */
82 	regs->r_tstate = tf->tf_tstate;
83 	regs->r_pc = tf->tf_pc;
84 	regs->r_npc = tf->tf_npc;
85 	regs->r_y = tf->tf_y;
86 	for (i = 0; i < 8; i++) {
87 		regs->r_global[i] = tf->tf_global[i];
88 		regs->r_out[i] = tf->tf_out[i];
89 		regs->r_local[i] = tf->tf_local[i];
90 		regs->r_in[i] = tf->tf_in[i];
91 	}
92 	return (0);
93 }
94 
95 int
process_read_fpregs(struct proc * p,struct fpreg * regs)96 process_read_fpregs(struct proc *p, struct fpreg *regs)
97 {
98 	extern const struct fpstate initfpstate;
99 	const struct fpstate *statep = &initfpstate;
100 
101 	/* NOTE: struct fpreg == struct fpstate */
102 	if (p->p_md.md_fpstate) {
103 		fpusave_proc(p, 1);
104 		statep = p->p_md.md_fpstate;
105 	}
106 
107 	/* copy in fregs */
108 	bcopy(statep, regs, sizeof(struct fpreg));
109 	return 0;
110 }
111 
112 #ifdef PTRACE
113 
114 int
process_write_regs(struct proc * p,struct reg * regs)115 process_write_regs(struct proc *p, struct reg *regs)
116 {
117 	struct trapframe *tf = p->p_md.md_tf;
118 	int i;
119 
120 	/* copy in regs */
121 	tf->tf_pc = regs->r_pc;
122 	tf->tf_npc = regs->r_npc;
123 	tf->tf_y = regs->r_y;
124 	for (i = 0; i < 8; i++) {
125 		tf->tf_global[i] = regs->r_global[i];
126 		tf->tf_out[i] = regs->r_out[i];
127 	}
128 	/* We should also read in the ins and locals.  See signal stuff */
129 	tf->tf_tstate = (tf->tf_tstate & ~TSTATE_CCR) |
130 		(regs->r_tstate & TSTATE_CCR);
131 	return (0);
132 }
133 
134 #ifdef PT_STEP
135 int
process_sstep(struct proc * p,int sstep)136 process_sstep(struct proc *p, int sstep)
137 {
138 	if (sstep)
139 		return EINVAL;
140 	return (0);
141 }
142 #endif
143 
144 int
process_set_pc(struct proc * p,caddr_t addr)145 process_set_pc(struct proc *p, caddr_t addr)
146 {
147 	p->p_md.md_tf->tf_pc = (vaddr_t)addr;
148 	p->p_md.md_tf->tf_npc = (vaddr_t)addr + 4;
149 	return (0);
150 }
151 
152 int
process_write_fpregs(struct proc * p,struct fpreg * regs)153 process_write_fpregs(struct proc *p, struct fpreg *regs)
154 {
155 	if (p->p_md.md_fpstate == NULL) {
156 		p->p_md.md_fpstate = malloc(sizeof(struct fpstate),
157 		    M_SUBPROC, M_WAITOK);
158 	} else
159 		fpusave_proc(p, 1);
160 
161 	/* copy in fregs */
162 	bcopy(regs, p->p_md.md_fpstate, sizeof(struct fpreg));
163 	return 0;
164 }
165 
166 #endif	/* PTRACE */
167 
168 register_t
process_get_wcookie(struct proc * p)169 process_get_wcookie(struct proc *p)
170 {
171 	return p->p_addr->u_pcb.pcb_wcookie;
172 }
173