Annotation of sys/arch/amd64/amd64/process_machdep.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: process_machdep.c,v 1.5 2004/07/10 18:56:59 kettenis Exp $ */
2: /* $NetBSD: process_machdep.c,v 1.1 2003/04/26 18:39:31 fvdl Exp $ */
3:
4: /*-
5: * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Charles M. Hannum.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: /*
41: * This file may seem a bit stylized, but that so that it's easier to port.
42: * Functions to be implemented here are:
43: *
44: * process_read_regs(proc, regs)
45: * Get the current user-visible register set from the process
46: * and copy it into the regs structure (<machine/reg.h>).
47: * The process is stopped at the time read_regs is called.
48: *
49: * process_write_regs(proc, regs)
50: * Update the current register set from the passed in regs
51: * structure. Take care to avoid clobbering special CPU
52: * registers or privileged bits in the PSL.
53: * The process is stopped at the time write_regs is called.
54: *
55: * process_sstep(proc)
56: * Arrange for the process to trap after executing a single instruction.
57: *
58: * process_set_pc(proc)
59: * Set the process's program counter.
60: */
61:
62:
63: #include <sys/param.h>
64: #include <sys/systm.h>
65: #include <sys/time.h>
66: #include <sys/kernel.h>
67: #include <sys/proc.h>
68: #include <sys/user.h>
69: #include <sys/vnode.h>
70: #include <sys/ptrace.h>
71:
72: #include <uvm/uvm_extern.h>
73:
74: #include <machine/psl.h>
75: #include <machine/reg.h>
76: #include <machine/segments.h>
77: #include <machine/fpu.h>
78:
79: static __inline struct trapframe *process_frame(struct proc *);
80: static __inline struct fxsave64 *process_fpframe(struct proc *);
81: #if 0
82: static __inline int verr_gdt(struct pmap *, int sel);
83: static __inline int verr_ldt(struct pmap *, int sel);
84: #endif
85:
86: static __inline struct trapframe *
87: process_frame(struct proc *p)
88: {
89:
90: return (p->p_md.md_regs);
91: }
92:
93: static __inline struct fxsave64 *
94: process_fpframe(struct proc *p)
95: {
96:
97: return (&p->p_addr->u_pcb.pcb_savefpu.fp_fxsave);
98: }
99:
100: int
101: process_read_regs(struct proc *p, struct reg *regs)
102: {
103: struct trapframe *tf = process_frame(p);
104:
105: regs->r_rdi = tf->tf_rdi;
106: regs->r_rsi = tf->tf_rsi;
107: regs->r_rdx = tf->tf_rdx;
108: regs->r_rcx = tf->tf_rcx;
109: regs->r_r8 = tf->tf_r8;
110: regs->r_r9 = tf->tf_r9;
111: regs->r_r10 = tf->tf_r10;
112: regs->r_r11 = tf->tf_r11;
113: regs->r_r12 = tf->tf_r12;
114: regs->r_r13 = tf->tf_r13;
115: regs->r_r14 = tf->tf_r14;
116: regs->r_r15 = tf->tf_r15;
117: regs->r_rbp = tf->tf_rbp;
118: regs->r_rbx = tf->tf_rbx;
119: regs->r_rax = tf->tf_rax;
120: regs->r_rsp = tf->tf_rsp;
121: regs->r_rip = tf->tf_rip;
122: regs->r_rflags = tf->tf_rflags;
123: regs->r_cs = tf->tf_cs;
124: regs->r_ss = tf->tf_ss;
125: regs->r_ds = tf->tf_ds;
126: regs->r_es = tf->tf_es;
127: regs->r_fs = tf->tf_fs;
128: regs->r_gs = tf->tf_gs;
129:
130: return (0);
131: }
132:
133: int
134: process_read_fpregs(struct proc *p, struct fpreg *regs)
135: {
136: struct fxsave64 *frame = process_fpframe(p);
137:
138: if (p->p_md.md_flags & MDP_USEDFPU) {
139: fpusave_proc(p, 1);
140: } else {
141: u_int16_t cw;
142: u_int32_t mxcsr, mxcsr_mask;
143:
144: /*
145: * Fake a FNINIT.
146: * The initial control word was already set by setregs(), so
147: * save it temporarily.
148: */
149: cw = frame->fx_fcw;
150: mxcsr = frame->fx_mxcsr;
151: mxcsr_mask = frame->fx_mxcsr_mask;
152: memset(frame, 0, sizeof(*regs));
153: frame->fx_fcw = cw;
154: frame->fx_fsw = 0x0000;
155: frame->fx_ftw = 0xff;
156: frame->fx_mxcsr = mxcsr;
157: frame->fx_mxcsr_mask = mxcsr_mask;
158: p->p_md.md_flags |= MDP_USEDFPU;
159: }
160:
161: memcpy(®s->fxstate, frame, sizeof(*regs));
162: return (0);
163: }
164:
165: #ifdef PTRACE
166:
167: int
168: process_write_regs(struct proc *p, struct reg *regs)
169: {
170: struct trapframe *tf = process_frame(p);
171:
172: /*
173: * Check for security violations.
174: */
175: if (check_context(regs, tf))
176: return (EINVAL);
177:
178: tf->tf_rdi = regs->r_rdi;
179: tf->tf_rsi = regs->r_rsi;
180: tf->tf_rdx = regs->r_rdx;
181: tf->tf_rcx = regs->r_rcx;
182: tf->tf_r8 = regs->r_r8;
183: tf->tf_r9 = regs->r_r9;
184: tf->tf_r10 = regs->r_r10;
185: tf->tf_r11 = regs->r_r11;
186: tf->tf_r12 = regs->r_r12;
187: tf->tf_r13 = regs->r_r13;
188: tf->tf_r14 = regs->r_r14;
189: tf->tf_r15 = regs->r_r15;
190: tf->tf_rbp = regs->r_rbp;
191: tf->tf_rbx = regs->r_rbx;
192: tf->tf_rax = regs->r_rax;
193: tf->tf_rsp = regs->r_rsp;
194: tf->tf_rip = regs->r_rip;
195: tf->tf_rflags = regs->r_rflags;
196: tf->tf_cs = regs->r_cs;
197: tf->tf_ss = regs->r_ss;
198: tf->tf_ds = regs->r_ds;
199: tf->tf_es = regs->r_es;
200: tf->tf_fs = regs->r_fs;
201: tf->tf_gs = regs->r_gs;
202:
203: return (0);
204: }
205:
206: int
207: process_write_fpregs(struct proc *p, struct fpreg *regs)
208: {
209: struct fxsave64 *frame = process_fpframe(p);
210:
211: if (p->p_md.md_flags & MDP_USEDFPU) {
212: fpusave_proc(p, 0);
213: } else {
214: p->p_md.md_flags |= MDP_USEDFPU;
215: }
216:
217: memcpy(frame, ®s->fxstate, sizeof(*regs));
218: return (0);
219: }
220:
221: int
222: process_sstep(struct proc *p, int sstep)
223: {
224: struct trapframe *tf = process_frame(p);
225:
226: if (sstep)
227: tf->tf_rflags |= PSL_T;
228: else
229: tf->tf_rflags &= ~PSL_T;
230:
231: return (0);
232: }
233:
234: int
235: process_set_pc(struct proc *p, caddr_t addr)
236: {
237: struct trapframe *tf = process_frame(p);
238:
239: if ((u_int64_t)addr > VM_MAXUSER_ADDRESS)
240: return EINVAL;
241: tf->tf_rip = (u_int64_t)addr;
242:
243: return (0);
244: }
245:
246: #endif /* PTRACE */
CVSweb