1 /*
2  * Copyright 2003 PathScale, Inc.
3  * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4  *
5  * Licensed under the GPL
6  */
7 
8 #ifndef __SYSDEP_X86_64_PTRACE_H
9 #define __SYSDEP_X86_64_PTRACE_H
10 
11 #include "user_constants.h"
12 #include "sysdep/faultinfo.h"
13 
14 #define MAX_REG_OFFSET (UM_FRAME_SIZE)
15 #define MAX_REG_NR ((MAX_REG_OFFSET) / sizeof(unsigned long))
16 
17 #include "skas_ptregs.h"
18 
19 #define REGS_IP(r) ((r)[HOST_IP])
20 #define REGS_SP(r) ((r)[HOST_SP])
21 
22 #define REGS_RBX(r) ((r)[HOST_RBX])
23 #define REGS_RCX(r) ((r)[HOST_RCX])
24 #define REGS_RDX(r) ((r)[HOST_RDX])
25 #define REGS_RSI(r) ((r)[HOST_RSI])
26 #define REGS_RDI(r) ((r)[HOST_RDI])
27 #define REGS_RBP(r) ((r)[HOST_RBP])
28 #define REGS_RAX(r) ((r)[HOST_RAX])
29 #define REGS_R8(r) ((r)[HOST_R8])
30 #define REGS_R9(r) ((r)[HOST_R9])
31 #define REGS_R10(r) ((r)[HOST_R10])
32 #define REGS_R11(r) ((r)[HOST_R11])
33 #define REGS_R12(r) ((r)[HOST_R12])
34 #define REGS_R13(r) ((r)[HOST_R13])
35 #define REGS_R14(r) ((r)[HOST_R14])
36 #define REGS_R15(r) ((r)[HOST_R15])
37 #define REGS_CS(r) ((r)[HOST_CS])
38 #define REGS_EFLAGS(r) ((r)[HOST_EFLAGS])
39 #define REGS_SS(r) ((r)[HOST_SS])
40 
41 #define HOST_FS_BASE 21
42 #define HOST_GS_BASE 22
43 #define HOST_DS 23
44 #define HOST_ES 24
45 #define HOST_FS 25
46 #define HOST_GS 26
47 
48 /* Also defined in asm/ptrace-x86_64.h, but not in libc headers.  So, these
49  * are already defined for kernel code, but not for userspace code.
50  */
51 #ifndef FS_BASE
52 /* These aren't defined in ptrace.h, but exist in struct user_regs_struct,
53  * which is what x86_64 ptrace actually uses.
54  */
55 #define FS_BASE (HOST_FS_BASE * sizeof(long))
56 #define GS_BASE (HOST_GS_BASE * sizeof(long))
57 #define DS (HOST_DS * sizeof(long))
58 #define ES (HOST_ES * sizeof(long))
59 #define FS (HOST_FS * sizeof(long))
60 #define GS (HOST_GS * sizeof(long))
61 #endif
62 
63 #define REGS_FS_BASE(r) ((r)[HOST_FS_BASE])
64 #define REGS_GS_BASE(r) ((r)[HOST_GS_BASE])
65 #define REGS_DS(r) ((r)[HOST_DS])
66 #define REGS_ES(r) ((r)[HOST_ES])
67 #define REGS_FS(r) ((r)[HOST_FS])
68 #define REGS_GS(r) ((r)[HOST_GS])
69 
70 #define REGS_ORIG_RAX(r) ((r)[HOST_ORIG_RAX])
71 
72 #define REGS_SET_SYSCALL_RETURN(r, res) REGS_RAX(r) = (res)
73 
74 #define REGS_RESTART_SYSCALL(r) IP_RESTART_SYSCALL(REGS_IP(r))
75 
76 #define REGS_SEGV_IS_FIXABLE(r) SEGV_IS_FIXABLE((r)->trap_type)
77 
78 #define REGS_FAULT_ADDR(r) ((r)->fault_addr)
79 
80 #define REGS_FAULT_WRITE(r) FAULT_WRITE((r)->fault_type)
81 
82 #define REGS_TRAP(r) ((r)->trap_type)
83 
84 #define REGS_ERR(r) ((r)->fault_type)
85 
86 struct uml_pt_regs {
87 	unsigned long gp[MAX_REG_NR];
88 	struct faultinfo faultinfo;
89 	long syscall;
90 	int is_user;
91 };
92 
93 #define EMPTY_UML_PT_REGS { }
94 
95 #define UPT_RBX(r) REGS_RBX((r)->gp)
96 #define UPT_RCX(r) REGS_RCX((r)->gp)
97 #define UPT_RDX(r) REGS_RDX((r)->gp)
98 #define UPT_RSI(r) REGS_RSI((r)->gp)
99 #define UPT_RDI(r) REGS_RDI((r)->gp)
100 #define UPT_RBP(r) REGS_RBP((r)->gp)
101 #define UPT_RAX(r) REGS_RAX((r)->gp)
102 #define UPT_R8(r) REGS_R8((r)->gp)
103 #define UPT_R9(r) REGS_R9((r)->gp)
104 #define UPT_R10(r) REGS_R10((r)->gp)
105 #define UPT_R11(r) REGS_R11((r)->gp)
106 #define UPT_R12(r) REGS_R12((r)->gp)
107 #define UPT_R13(r) REGS_R13((r)->gp)
108 #define UPT_R14(r) REGS_R14((r)->gp)
109 #define UPT_R15(r) REGS_R15((r)->gp)
110 #define UPT_CS(r) REGS_CS((r)->gp)
111 #define UPT_FS_BASE(r) REGS_FS_BASE((r)->gp)
112 #define UPT_FS(r) REGS_FS((r)->gp)
113 #define UPT_GS_BASE(r) REGS_GS_BASE((r)->gp)
114 #define UPT_GS(r) REGS_GS((r)->gp)
115 #define UPT_DS(r) REGS_DS((r)->gp)
116 #define UPT_ES(r) REGS_ES((r)->gp)
117 #define UPT_CS(r) REGS_CS((r)->gp)
118 #define UPT_SS(r) REGS_SS((r)->gp)
119 #define UPT_ORIG_RAX(r) REGS_ORIG_RAX((r)->gp)
120 
121 #define UPT_IP(r) REGS_IP((r)->gp)
122 #define UPT_SP(r) REGS_SP((r)->gp)
123 
124 #define UPT_EFLAGS(r) REGS_EFLAGS((r)->gp)
125 #define UPT_SYSCALL_NR(r) ((r)->syscall)
126 #define UPT_SYSCALL_RET(r) UPT_RAX(r)
127 
128 extern int user_context(unsigned long sp);
129 
130 #define UPT_IS_USER(r) ((r)->is_user)
131 
132 #define UPT_SYSCALL_ARG1(r) UPT_RDI(r)
133 #define UPT_SYSCALL_ARG2(r) UPT_RSI(r)
134 #define UPT_SYSCALL_ARG3(r) UPT_RDX(r)
135 #define UPT_SYSCALL_ARG4(r) UPT_R10(r)
136 #define UPT_SYSCALL_ARG5(r) UPT_R8(r)
137 #define UPT_SYSCALL_ARG6(r) UPT_R9(r)
138 
139 struct syscall_args {
140 	unsigned long args[6];
141 };
142 
143 #define SYSCALL_ARGS(r) ((struct syscall_args) \
144 			 { .args = { UPT_SYSCALL_ARG1(r),	 \
145 				     UPT_SYSCALL_ARG2(r),	 \
146 				     UPT_SYSCALL_ARG3(r),	 \
147 				     UPT_SYSCALL_ARG4(r),	 \
148 				     UPT_SYSCALL_ARG5(r),	 \
149 				     UPT_SYSCALL_ARG6(r) } } )
150 
151 #define UPT_REG(regs, reg) \
152 	({      unsigned long val;		\
153 		switch(reg){						\
154 		case R8: val = UPT_R8(regs); break;			\
155 		case R9: val = UPT_R9(regs); break;			\
156 		case R10: val = UPT_R10(regs); break;			\
157 		case R11: val = UPT_R11(regs); break;			\
158 		case R12: val = UPT_R12(regs); break;			\
159 		case R13: val = UPT_R13(regs); break;			\
160 		case R14: val = UPT_R14(regs); break;			\
161 		case R15: val = UPT_R15(regs); break;			\
162 		case RIP: val = UPT_IP(regs); break;			\
163 		case RSP: val = UPT_SP(regs); break;			\
164 		case RAX: val = UPT_RAX(regs); break;			\
165 		case RBX: val = UPT_RBX(regs); break;			\
166 		case RCX: val = UPT_RCX(regs); break;			\
167 		case RDX: val = UPT_RDX(regs); break;			\
168 		case RSI: val = UPT_RSI(regs); break;			\
169 		case RDI: val = UPT_RDI(regs); break;			\
170 		case RBP: val = UPT_RBP(regs); break;			\
171 		case ORIG_RAX: val = UPT_ORIG_RAX(regs); break;		\
172 		case CS: val = UPT_CS(regs); break;			\
173 		case SS: val = UPT_SS(regs); break;			\
174 		case FS_BASE: val = UPT_FS_BASE(regs); break;		\
175 		case GS_BASE: val = UPT_GS_BASE(regs); break;		\
176 		case DS: val = UPT_DS(regs); break;			\
177 		case ES: val = UPT_ES(regs); break;			\
178 		case FS : val = UPT_FS (regs); break;			\
179 		case GS: val = UPT_GS(regs); break;			\
180 		case EFLAGS: val = UPT_EFLAGS(regs); break;		\
181 		default :						\
182 			panic("Bad register in UPT_REG : %d\n", reg);	\
183 			val = -1;					\
184 		}							\
185 		val;							\
186 	})
187 
188 
189 #define UPT_SET(regs, reg, val) \
190 	({      unsigned long __upt_val = val;	\
191 		switch(reg){						\
192 		case R8: UPT_R8(regs) = __upt_val; break;		\
193 		case R9: UPT_R9(regs) = __upt_val; break;		\
194 		case R10: UPT_R10(regs) = __upt_val; break;		\
195 		case R11: UPT_R11(regs) = __upt_val; break;		\
196 		case R12: UPT_R12(regs) = __upt_val; break;		\
197 		case R13: UPT_R13(regs) = __upt_val; break;		\
198 		case R14: UPT_R14(regs) = __upt_val; break;		\
199 		case R15: UPT_R15(regs) = __upt_val; break;		\
200 		case RIP: UPT_IP(regs) = __upt_val; break;		\
201 		case RSP: UPT_SP(regs) = __upt_val; break;		\
202 		case RAX: UPT_RAX(regs) = __upt_val; break;		\
203 		case RBX: UPT_RBX(regs) = __upt_val; break;		\
204 		case RCX: UPT_RCX(regs) = __upt_val; break;		\
205 		case RDX: UPT_RDX(regs) = __upt_val; break;		\
206 		case RSI: UPT_RSI(regs) = __upt_val; break;		\
207 		case RDI: UPT_RDI(regs) = __upt_val; break;		\
208 		case RBP: UPT_RBP(regs) = __upt_val; break;		\
209 		case ORIG_RAX: UPT_ORIG_RAX(regs) = __upt_val; break;	\
210 		case CS: UPT_CS(regs) = __upt_val; break;		\
211 		case SS: UPT_SS(regs) = __upt_val; break;		\
212 		case FS_BASE: UPT_FS_BASE(regs) = __upt_val; break;	\
213 		case GS_BASE: UPT_GS_BASE(regs) = __upt_val; break;	\
214 		case DS: UPT_DS(regs) = __upt_val; break;		\
215 		case ES: UPT_ES(regs) = __upt_val; break;		\
216 		case FS: UPT_FS(regs) = __upt_val; break;		\
217 		case GS: UPT_GS(regs) = __upt_val; break;		\
218 		case EFLAGS: UPT_EFLAGS(regs) = __upt_val; break;	\
219 		default :						\
220 			panic("Bad register in UPT_SET : %d\n", reg);	\
221 			break;						\
222 		}							\
223 		__upt_val;						\
224 	})
225 
226 #define UPT_SET_SYSCALL_RETURN(r, res) \
227 	REGS_SET_SYSCALL_RETURN((r)->regs, (res))
228 
229 #define UPT_RESTART_SYSCALL(r) REGS_RESTART_SYSCALL((r)->gp)
230 
231 #define UPT_SEGV_IS_FIXABLE(r) REGS_SEGV_IS_FIXABLE(&r->skas)
232 
233 #define UPT_FAULTINFO(r) (&(r)->faultinfo)
234 
arch_init_registers(int pid)235 static inline void arch_init_registers(int pid)
236 {
237 }
238 
239 #endif
240