xref: /netbsd-src/sys/arch/amd64/include/ptrace.h (revision eb82852fbea0e6d8dccfadbfcf61128ced92d3ac)
1 /*	$NetBSD: ptrace.h,v 1.23 2023/11/20 03:05:48 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1993 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef _AMD64_PTRACE_H_
33 #define _AMD64_PTRACE_H_
34 
35 #ifdef __x86_64__
36 /*
37  * amd64-dependent ptrace definitions
38  */
39 #define	PT_STEP			(PT_FIRSTMACH + 0)
40 #define	PT_GETREGS		(PT_FIRSTMACH + 1)
41 #define	PT_SETREGS		(PT_FIRSTMACH + 2)
42 #define	PT_GETFPREGS		(PT_FIRSTMACH + 3)
43 #define	PT_SETFPREGS		(PT_FIRSTMACH + 4)
44 #define	PT_GETDBREGS		(PT_FIRSTMACH + 5)
45 #define	PT_SETDBREGS		(PT_FIRSTMACH + 6)
46 #define	PT_SETSTEP		(PT_FIRSTMACH + 7)
47 #define	PT_CLEARSTEP		(PT_FIRSTMACH + 8)
48 #define	PT_GETXSTATE		(PT_FIRSTMACH + 9)
49 #define	PT_SETXSTATE		(PT_FIRSTMACH + 10)
50 #ifdef _KERNEL
51 /*
52  * Only used internally for COMPAT_NETBSD32
53  */
54 #define	PT_GETXMMREGS		(PT_FIRSTMACH + 11)
55 #define	PT_SETXMMREGS		(PT_FIRSTMACH + 12)
56 #endif
57 
58 /* We have machine-dependent process tracing needs. */
59 #define	__HAVE_PTRACE_MACHDEP
60 
61 #define PT_MACHDEP_STRINGS \
62 	"PT_STEP", \
63 	"PT_GETREGS", \
64 	"PT_SETREGS", \
65 	"PT_GETFPREGS", \
66 	"PT_SETFPREGS", \
67 	"PT_GETDBREGS", \
68 	"PT_SETDBREGS", \
69 	"PT_SETSTEP", \
70 	"PT_CLEARSTEP", \
71 	"PT_GETXSTATE", \
72 	"PT_SETXSTATE", \
73 	"PT_GETXMMREGS", \
74 	"PT_SETXMMREGS"
75 
76 #include <machine/reg.h>
77 #define PTRACE_REG_PC(r)	(r)->regs[_REG_RIP]
78 #define PTRACE_REG_FP(r)	(r)->regs[_REG_RBP]
79 #define PTRACE_REG_SET_PC(r, v)	(r)->regs[_REG_RIP] = (v)
80 #define PTRACE_REG_SP(r)	(r)->regs[_REG_RSP]
81 #define PTRACE_REG_INTRV(r)	(r)->regs[_REG_RAX]
82 
83 #define PTRACE_ILLEGAL_ASM	__asm __volatile ("ud2" : : : "memory")
84 
85 #define PTRACE_BREAKPOINT	((const uint8_t[]) { 0xcc })
86 #define PTRACE_BREAKPOINT_ASM	__asm __volatile ("int3" : : : "memory")
87 #define PTRACE_BREAKPOINT_SIZE	1
88 #define PTRACE_BREAKPOINT_ADJ	1
89 
90 #ifdef _KERNEL
91 
92 /*
93  * These are used in sys_ptrace() to find good ptrace(2) requests.
94  */
95 #define	PTRACE_MACHDEP_REQUEST_CASES					\
96 	case PT_GETXSTATE:						\
97 	case PT_SETXSTATE:						\
98 	case PT_GETXMMREGS:						\
99 	case PT_SETXMMREGS:
100 
101 int process_machdep_doxstate(struct lwp *, struct lwp *, struct uio *);
102 int process_machdep_validfpu(struct proc *);
103 
104 /*
105  * The fpregs structure contains an fxsave area, which must have 16-byte
106  * alignment.
107  */
108 #define PTRACE_REGS_ALIGN __aligned(16)
109 
110 #include <sys/module_hook.h>
111 MODULE_HOOK(netbsd32_process_doxmmregs_hook, int,
112     (struct lwp *, struct lwp *, void *, bool));
113 
114 #ifdef EXEC_ELF32
115 #include <machine/netbsd32_machdep.h>
116 #endif
117 #define PT64_GETXSTATE		PT_GETXSTATE
118 #define COREDUMP_MACHDEP_LWP_NOTES(l, ns, name)				\
119 {									\
120 	struct xstate xstate;	/* XXX FIXME big stack object */	\
121 	memset(&xstate, 0, sizeof(xstate));				\
122 	if (!process_read_xstate(l, &xstate))				\
123 	{								\
124 		ELFNAMEEND(coredump_savenote)(ns,			\
125 		    CONCAT(CONCAT(PT, ELFSIZE), _GETXSTATE), name,	\
126 		    &xstate, sizeof(xstate));				\
127 	}								\
128 }
129 
130 #endif /* _KERNEL */
131 
132 #ifdef _KERNEL_OPT
133 #include "opt_compat_netbsd32.h"
134 
135 #ifdef COMPAT_NETBSD32
136 #include <machine/netbsd32_machdep.h>
137 
138 #define process_read_regs32	netbsd32_process_read_regs
139 #define process_read_fpregs32	netbsd32_process_read_fpregs
140 #define process_read_dbregs32	netbsd32_process_read_dbregs
141 
142 #define process_write_regs32	netbsd32_process_write_regs
143 #define process_write_fpregs32	netbsd32_process_write_fpregs
144 #define process_write_dbregs32	netbsd32_process_write_dbregs
145 
146 #define process_reg32		struct reg32
147 #define process_fpreg32		struct fpreg32
148 #define process_dbreg32		struct dbreg32
149 
150 #define PTRACE_TRANSLATE_REQUEST32(x) netbsd32_ptrace_translate_request(x)
151 #endif	/* COMPAT_NETBSD32 */
152 #endif	/* _KERNEL_OPT */
153 
154 #else	/* !__x86_64__ */
155 
156 #include <i386/ptrace.h>
157 
158 #endif	/* __x86_64__ */
159 
160 #endif	/* _AMD64_PTRACE_H_ */
161