2 * Fault Injection Test harness (FI)
3 * Copyright (C) Intel Crop.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 /* Id: pf_in.c,v 1.1.1.1 2002/11/12 05:56:32 brlock Exp
23 * Copyright by Intel Crop., 2002
24 * Louis Zhuang (louis.zhuang@intel.com)
26 * Bjorn Steinbrink (B.Steinbrink@gmx.de), 2007
29 #include <linux/ptrace.h> /* struct pt_regs */
33 /* IA32 Manual 3, 2-1 */
34 static unsigned char prefix_codes
[] = {
35 0xF0, 0xF2, 0xF3, 0x2E, 0x36, 0x3E, 0x26, 0x64,
38 /* IA32 Manual 3, 3-432*/
39 static unsigned int reg_rop
[] = {
40 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
42 static unsigned int reg_wop
[] = { 0x88, 0x89, 0xAA, 0xAB };
43 static unsigned int imm_wop
[] = { 0xC6, 0xC7 };
44 /* IA32 Manual 3, 3-432*/
45 static unsigned int rw8
[] = { 0x88, 0x8A, 0xC6, 0xAA };
46 static unsigned int rw32
[] = {
47 0x89, 0x8B, 0xC7, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
49 static unsigned int mw8
[] = { 0x88, 0x8A, 0xC6, 0xB60F, 0xBE0F, 0xAA };
50 static unsigned int mw16
[] = { 0xB70F, 0xBF0F };
51 static unsigned int mw32
[] = { 0x89, 0x8B, 0xC7, 0xAB };
52 static unsigned int mw64
[] = {};
53 #else /* not __i386__ */
54 static unsigned char prefix_codes
[] = {
55 0x66, 0x67, 0x2E, 0x3E, 0x26, 0x64, 0x65, 0x36,
58 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
59 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
61 /* AMD64 Manual 3, Appendix A*/
62 static unsigned int reg_rop
[] = {
63 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
65 static unsigned int reg_wop
[] = { 0x88, 0x89, 0xAA, 0xAB };
66 static unsigned int imm_wop
[] = { 0xC6, 0xC7 };
67 static unsigned int rw8
[] = { 0xC6, 0x88, 0x8A, 0xAA };
68 static unsigned int rw32
[] = {
69 0xC7, 0x89, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
72 static unsigned int mw8
[] = { 0xC6, 0x88, 0x8A, 0xB60F, 0xBE0F, 0xAA };
74 static unsigned int mw16
[] = { 0xB70F, 0xBF0F };
76 static unsigned int mw32
[] = { 0xC7 };
77 /* 16, 32 or 64 bit */
78 static unsigned int mw64
[] = { 0x89, 0x8B, 0xAB };
79 #endif /* not __i386__ */
88 static int skip_prefix(unsigned char *addr
, struct prefix_bits
*prf
)
91 unsigned char *p
= addr
;
98 for (i
= 0; i
< ARRAY_SIZE(prefix_codes
); i
++) {
99 if (*p
== prefix_codes
[i
]) {
103 if ((*p
& 0xf8) == 0x48)
105 if ((*p
& 0xf4) == 0x44)
107 if ((*p
& 0xf0) == 0x40)
118 static int get_opcode(unsigned char *addr
, unsigned int *opcode
)
123 /* 0x0F is extension instruction */
124 *opcode
= *(unsigned short *)addr
;
134 #define CHECK_OP_TYPE(opcode, array, type) \
135 for (i = 0; i < ARRAY_SIZE(array); i++) { \
136 if (array[i] == opcode) { \
142 enum reason_type
get_ins_type(unsigned long ins_addr
)
146 struct prefix_bits prf
;
148 enum reason_type rv
= OTHERS
;
150 p
= (unsigned char *)ins_addr
;
151 p
+= skip_prefix(p
, &prf
);
152 p
+= get_opcode(p
, &opcode
);
154 CHECK_OP_TYPE(opcode
, reg_rop
, REG_READ
);
155 CHECK_OP_TYPE(opcode
, reg_wop
, REG_WRITE
);
156 CHECK_OP_TYPE(opcode
, imm_wop
, IMM_WRITE
);
163 static unsigned int get_ins_reg_width(unsigned long ins_addr
)
167 struct prefix_bits prf
;
170 p
= (unsigned char *)ins_addr
;
171 p
+= skip_prefix(p
, &prf
);
172 p
+= get_opcode(p
, &opcode
);
174 for (i
= 0; i
< ARRAY_SIZE(rw8
); i
++)
175 if (rw8
[i
] == opcode
)
178 for (i
= 0; i
< ARRAY_SIZE(rw32
); i
++)
179 if (rw32
[i
] == opcode
)
180 return prf
.shorted
? 2 : (prf
.enlarged
? 8 : 4);
182 printk(KERN_ERR
"mmiotrace: Unknown opcode 0x%02x\n", opcode
);
186 unsigned int get_ins_mem_width(unsigned long ins_addr
)
190 struct prefix_bits prf
;
193 p
= (unsigned char *)ins_addr
;
194 p
+= skip_prefix(p
, &prf
);
195 p
+= get_opcode(p
, &opcode
);
197 for (i
= 0; i
< ARRAY_SIZE(mw8
); i
++)
198 if (mw8
[i
] == opcode
)
201 for (i
= 0; i
< ARRAY_SIZE(mw16
); i
++)
202 if (mw16
[i
] == opcode
)
205 for (i
= 0; i
< ARRAY_SIZE(mw32
); i
++)
206 if (mw32
[i
] == opcode
)
207 return prf
.shorted
? 2 : 4;
209 for (i
= 0; i
< ARRAY_SIZE(mw64
); i
++)
210 if (mw64
[i
] == opcode
)
211 return prf
.shorted
? 2 : (prf
.enlarged
? 8 : 4);
213 printk(KERN_ERR
"mmiotrace: Unknown opcode 0x%02x\n", opcode
);
218 * Define register ident in mod/rm byte.
219 * Note: these are NOT the same as in ptrace-abi.h.
251 static unsigned char *get_reg_w8(int no
, int rex
, struct pt_regs
*regs
)
253 unsigned char *rv
= NULL
;
257 rv
= (unsigned char *)®s
->ax
;
260 rv
= (unsigned char *)®s
->bx
;
263 rv
= (unsigned char *)®s
->cx
;
266 rv
= (unsigned char *)®s
->dx
;
270 rv
= (unsigned char *)®s
->r8
;
273 rv
= (unsigned char *)®s
->r9
;
276 rv
= (unsigned char *)®s
->r10
;
279 rv
= (unsigned char *)®s
->r11
;
282 rv
= (unsigned char *)®s
->r12
;
285 rv
= (unsigned char *)®s
->r13
;
288 rv
= (unsigned char *)®s
->r14
;
291 rv
= (unsigned char *)®s
->r15
;
303 * If REX prefix exists, access low bytes of SI etc.
308 rv
= (unsigned char *)®s
->si
;
311 rv
= (unsigned char *)®s
->di
;
314 rv
= (unsigned char *)®s
->bp
;
317 rv
= (unsigned char *)®s
->sp
;
325 rv
= 1 + (unsigned char *)®s
->ax
;
328 rv
= 1 + (unsigned char *)®s
->bx
;
331 rv
= 1 + (unsigned char *)®s
->cx
;
334 rv
= 1 + (unsigned char *)®s
->dx
;
342 printk(KERN_ERR
"mmiotrace: Error reg no# %d\n", no
);
347 static unsigned long *get_reg_w32(int no
, struct pt_regs
*regs
)
349 unsigned long *rv
= NULL
;
403 printk(KERN_ERR
"mmiotrace: Error reg no# %d\n", no
);
409 unsigned long get_ins_reg_val(unsigned long ins_addr
, struct pt_regs
*regs
)
414 struct prefix_bits prf
;
417 p
= (unsigned char *)ins_addr
;
418 p
+= skip_prefix(p
, &prf
);
419 p
+= get_opcode(p
, &opcode
);
420 for (i
= 0; i
< ARRAY_SIZE(reg_rop
); i
++)
421 if (reg_rop
[i
] == opcode
)
424 for (i
= 0; i
< ARRAY_SIZE(reg_wop
); i
++)
425 if (reg_wop
[i
] == opcode
)
428 printk(KERN_ERR
"mmiotrace: Not a register instruction, opcode "
433 /* for STOS, source register is fixed */
434 if (opcode
== 0xAA || opcode
== 0xAB) {
437 unsigned char mod_rm
= *p
;
438 reg
= ((mod_rm
>> 3) & 0x7) | (prf
.rexr
<< 3);
440 switch (get_ins_reg_width(ins_addr
)) {
442 return *get_reg_w8(reg
, prf
.rex
, regs
);
445 return *(unsigned short *)get_reg_w32(reg
, regs
);
448 return *(unsigned int *)get_reg_w32(reg
, regs
);
452 return *(unsigned long *)get_reg_w32(reg
, regs
);
456 printk(KERN_ERR
"mmiotrace: Error width# %d\n", reg
);
463 unsigned long get_ins_imm_val(unsigned long ins_addr
)
466 unsigned char mod_rm
;
469 struct prefix_bits prf
;
472 p
= (unsigned char *)ins_addr
;
473 p
+= skip_prefix(p
, &prf
);
474 p
+= get_opcode(p
, &opcode
);
475 for (i
= 0; i
< ARRAY_SIZE(imm_wop
); i
++)
476 if (imm_wop
[i
] == opcode
)
479 printk(KERN_ERR
"mmiotrace: Not an immediate instruction, opcode "
489 /* if r/m is 5 we have a 32 disp (IA32 Manual 3, Table 2-2) */
490 /* AMD64: XXX Check for address size prefix? */
491 if ((mod_rm
& 0x7) == 0x5)
505 printk(KERN_ERR
"mmiotrace: not a memory access instruction "
506 "at 0x%lx, rm_mod=0x%02x\n",
510 switch (get_ins_reg_width(ins_addr
)) {
512 return *(unsigned char *)p
;
515 return *(unsigned short *)p
;
518 return *(unsigned int *)p
;
522 return *(unsigned long *)p
;
526 printk(KERN_ERR
"mmiotrace: Error: width.\n");