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/module.h>
30 #include <linux/ptrace.h> /* struct pt_regs */
34 /* IA32 Manual 3, 2-1 */
35 static unsigned char prefix_codes[] = {
36 0xF0, 0xF2, 0xF3, 0x2E, 0x36, 0x3E, 0x26, 0x64,
37 0x65, 0x2E, 0x3E, 0x66, 0x67
39 /* IA32 Manual 3, 3-432*/
40 static unsigned int reg_rop[] = {
41 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
43 static unsigned int reg_wop[] = { 0x88, 0x89 };
44 static unsigned int imm_wop[] = { 0xC6, 0xC7 };
45 /* IA32 Manual 3, 3-432*/
46 static unsigned int rw8[] = { 0x88, 0x8A, 0xC6 };
47 static unsigned int rw32[] = {
48 0x89, 0x8B, 0xC7, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
50 static unsigned int mw8[] = { 0x88, 0x8A, 0xC6, 0xB60F, 0xBE0F };
51 static unsigned int mw16[] = { 0xB70F, 0xBF0F };
52 static unsigned int mw32[] = { 0x89, 0x8B, 0xC7 };
53 static unsigned int mw64[] = {};
54 #else /* not __i386__ */
55 static unsigned char prefix_codes[] = {
56 0x66, 0x67, 0x2E, 0x3E, 0x26, 0x64, 0x65, 0x36,
59 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
60 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
62 /* AMD64 Manual 3, Appendix A*/
63 static unsigned int reg_rop[] = {
64 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
66 static unsigned int reg_wop[] = { 0x88, 0x89 };
67 static unsigned int imm_wop[] = { 0xC6, 0xC7 };
68 static unsigned int rw8[] = { 0xC6, 0x88, 0x8A };
69 static unsigned int rw32[] = {
70 0xC7, 0x89, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
73 static unsigned int mw8[] = { 0xC6, 0x88, 0x8A, 0xB60F, 0xBE0F };
75 static unsigned int mw16[] = { 0xB70F, 0xBF0F };
77 static unsigned int mw32[] = { 0xC7 };
78 /* 16, 32 or 64 bit */
79 static unsigned int mw64[] = { 0x89, 0x8B };
80 #endif /* not __i386__ */
82 static int skip_prefix(unsigned char *addr, int *shorted, int *enlarged,
86 unsigned char *p = addr;
92 for (i = 0; i < ARRAY_SIZE(prefix_codes); i++) {
93 if (*p == prefix_codes[i]) {
97 if ((*p & 0xf8) == 0x48)
99 if ((*p & 0xf4) == 0x44)
110 static int get_opcode(unsigned char *addr, unsigned int *opcode)
115 /* 0x0F is extension instruction */
116 *opcode = *(unsigned short *)addr;
126 #define CHECK_OP_TYPE(opcode, array, type) \
127 for (i = 0; i < ARRAY_SIZE(array); i++) { \
128 if (array[i] == opcode) { \
134 enum reason_type get_ins_type(unsigned long ins_addr)
138 int shorted, enlarged, rexr;
140 enum reason_type rv = OTHERS;
142 p = (unsigned char *)ins_addr;
143 p += skip_prefix(p, &shorted, &enlarged, &rexr);
144 p += get_opcode(p, &opcode);
146 CHECK_OP_TYPE(opcode, reg_rop, REG_READ);
147 CHECK_OP_TYPE(opcode, reg_wop, REG_WRITE);
148 CHECK_OP_TYPE(opcode, imm_wop, IMM_WRITE);
155 static unsigned int get_ins_reg_width(unsigned long ins_addr)
159 int i, shorted, enlarged, rexr;
161 p = (unsigned char *)ins_addr;
162 p += skip_prefix(p, &shorted, &enlarged, &rexr);
163 p += get_opcode(p, &opcode);
165 for (i = 0; i < ARRAY_SIZE(rw8); i++)
166 if (rw8[i] == opcode)
169 for (i = 0; i < ARRAY_SIZE(rw32); i++)
170 if (rw32[i] == opcode)
171 return (shorted ? 2 : (enlarged ? 8 : 4));
173 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
177 unsigned int get_ins_mem_width(unsigned long ins_addr)
181 int i, shorted, enlarged, rexr;
183 p = (unsigned char *)ins_addr;
184 p += skip_prefix(p, &shorted, &enlarged, &rexr);
185 p += get_opcode(p, &opcode);
187 for (i = 0; i < ARRAY_SIZE(mw8); i++)
188 if (mw8[i] == opcode)
191 for (i = 0; i < ARRAY_SIZE(mw16); i++)
192 if (mw16[i] == opcode)
195 for (i = 0; i < ARRAY_SIZE(mw32); i++)
196 if (mw32[i] == opcode)
197 return shorted ? 2 : 4;
199 for (i = 0; i < ARRAY_SIZE(mw64); i++)
200 if (mw64[i] == opcode)
201 return shorted ? 2 : (enlarged ? 8 : 4);
203 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
208 * Define register ident in mod/rm byte.
209 * Note: these are NOT the same as in ptrace-abi.h.
241 static unsigned char *get_reg_w8(int no, struct pt_regs *regs)
243 unsigned char *rv = NULL;
247 rv = (unsigned char *)®s->ax;
250 rv = (unsigned char *)®s->bx;
253 rv = (unsigned char *)®s->cx;
256 rv = (unsigned char *)®s->dx;
259 rv = 1 + (unsigned char *)®s->ax;
262 rv = 1 + (unsigned char *)®s->bx;
265 rv = 1 + (unsigned char *)®s->cx;
268 rv = 1 + (unsigned char *)®s->dx;
272 rv = (unsigned char *)®s->r8;
275 rv = (unsigned char *)®s->r9;
278 rv = (unsigned char *)®s->r10;
281 rv = (unsigned char *)®s->r11;
284 rv = (unsigned char *)®s->r12;
287 rv = (unsigned char *)®s->r13;
290 rv = (unsigned char *)®s->r14;
293 rv = (unsigned char *)®s->r15;
297 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
303 static unsigned long *get_reg_w32(int no, struct pt_regs *regs)
305 unsigned long *rv = NULL;
359 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
365 unsigned long get_ins_reg_val(unsigned long ins_addr, struct pt_regs *regs)
368 unsigned char mod_rm;
371 int i, shorted, enlarged, rexr;
374 p = (unsigned char *)ins_addr;
375 p += skip_prefix(p, &shorted, &enlarged, &rexr);
376 p += get_opcode(p, &opcode);
377 for (i = 0; i < ARRAY_SIZE(reg_rop); i++)
378 if (reg_rop[i] == opcode) {
383 for (i = 0; i < ARRAY_SIZE(reg_wop); i++)
384 if (reg_wop[i] == opcode) {
389 printk(KERN_ERR "mmiotrace: Not a register instruction, opcode "
395 reg = ((mod_rm >> 3) & 0x7) | (rexr << 3);
396 switch (get_ins_reg_width(ins_addr)) {
398 return *get_reg_w8(reg, regs);
401 return *(unsigned short *)get_reg_w32(reg, regs);
404 return *(unsigned int *)get_reg_w32(reg, regs);
408 return *(unsigned long *)get_reg_w32(reg, regs);
412 printk(KERN_ERR "mmiotrace: Error width# %d\n", reg);
419 unsigned long get_ins_imm_val(unsigned long ins_addr)
422 unsigned char mod_rm;
425 int i, shorted, enlarged, rexr;
428 p = (unsigned char *)ins_addr;
429 p += skip_prefix(p, &shorted, &enlarged, &rexr);
430 p += get_opcode(p, &opcode);
431 for (i = 0; i < ARRAY_SIZE(imm_wop); i++)
432 if (imm_wop[i] == opcode) {
437 printk(KERN_ERR "mmiotrace: Not an immediate instruction, opcode "
447 /* if r/m is 5 we have a 32 disp (IA32 Manual 3, Table 2-2) */
448 /* AMD64: XXX Check for address size prefix? */
449 if ((mod_rm & 0x7) == 0x5)
463 printk(KERN_ERR "mmiotrace: not a memory access instruction "
464 "at 0x%lx, rm_mod=0x%02x\n",
468 switch (get_ins_reg_width(ins_addr)) {
470 return *(unsigned char *)p;
473 return *(unsigned short *)p;
476 return *(unsigned int *)p;
480 return *(unsigned long *)p;
484 printk(KERN_ERR "mmiotrace: Error: width.\n");