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__ */
89 static int skip_prefix(unsigned char *addr, struct prefix_bits *prf)
92 unsigned char *p = addr;
99 for (i = 0; i < ARRAY_SIZE(prefix_codes); i++) {
100 if (*p == prefix_codes[i]) {
104 if ((*p & 0xf8) == 0x48)
106 if ((*p & 0xf4) == 0x44)
108 if ((*p & 0xf0) == 0x40)
119 static int get_opcode(unsigned char *addr, unsigned int *opcode)
124 /* 0x0F is extension instruction */
125 *opcode = *(unsigned short *)addr;
135 #define CHECK_OP_TYPE(opcode, array, type) \
136 for (i = 0; i < ARRAY_SIZE(array); i++) { \
137 if (array[i] == opcode) { \
143 enum reason_type get_ins_type(unsigned long ins_addr)
147 struct prefix_bits prf;
149 enum reason_type rv = OTHERS;
151 p = (unsigned char *)ins_addr;
152 p += skip_prefix(p, &prf);
153 p += get_opcode(p, &opcode);
155 CHECK_OP_TYPE(opcode, reg_rop, REG_READ);
156 CHECK_OP_TYPE(opcode, reg_wop, REG_WRITE);
157 CHECK_OP_TYPE(opcode, imm_wop, IMM_WRITE);
164 static unsigned int get_ins_reg_width(unsigned long ins_addr)
168 struct prefix_bits prf;
171 p = (unsigned char *)ins_addr;
172 p += skip_prefix(p, &prf);
173 p += get_opcode(p, &opcode);
175 for (i = 0; i < ARRAY_SIZE(rw8); i++)
176 if (rw8[i] == opcode)
179 for (i = 0; i < ARRAY_SIZE(rw32); i++)
180 if (rw32[i] == opcode)
181 return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
183 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
187 unsigned int get_ins_mem_width(unsigned long ins_addr)
191 struct prefix_bits prf;
194 p = (unsigned char *)ins_addr;
195 p += skip_prefix(p, &prf);
196 p += get_opcode(p, &opcode);
198 for (i = 0; i < ARRAY_SIZE(mw8); i++)
199 if (mw8[i] == opcode)
202 for (i = 0; i < ARRAY_SIZE(mw16); i++)
203 if (mw16[i] == opcode)
206 for (i = 0; i < ARRAY_SIZE(mw32); i++)
207 if (mw32[i] == opcode)
208 return prf.shorted ? 2 : 4;
210 for (i = 0; i < ARRAY_SIZE(mw64); i++)
211 if (mw64[i] == opcode)
212 return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
214 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
219 * Define register ident in mod/rm byte.
220 * Note: these are NOT the same as in ptrace-abi.h.
252 static unsigned char *get_reg_w8(int no, int rex, struct pt_regs *regs)
254 unsigned char *rv = NULL;
258 rv = (unsigned char *)®s->ax;
261 rv = (unsigned char *)®s->bx;
264 rv = (unsigned char *)®s->cx;
267 rv = (unsigned char *)®s->dx;
271 rv = (unsigned char *)®s->r8;
274 rv = (unsigned char *)®s->r9;
277 rv = (unsigned char *)®s->r10;
280 rv = (unsigned char *)®s->r11;
283 rv = (unsigned char *)®s->r12;
286 rv = (unsigned char *)®s->r13;
289 rv = (unsigned char *)®s->r14;
292 rv = (unsigned char *)®s->r15;
304 * If REX prefix exists, access low bytes of SI etc.
309 rv = (unsigned char *)®s->si;
312 rv = (unsigned char *)®s->di;
315 rv = (unsigned char *)®s->bp;
318 rv = (unsigned char *)®s->sp;
326 rv = 1 + (unsigned char *)®s->ax;
329 rv = 1 + (unsigned char *)®s->bx;
332 rv = 1 + (unsigned char *)®s->cx;
335 rv = 1 + (unsigned char *)®s->dx;
343 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
348 static unsigned long *get_reg_w32(int no, struct pt_regs *regs)
350 unsigned long *rv = NULL;
404 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
410 unsigned long get_ins_reg_val(unsigned long ins_addr, struct pt_regs *regs)
413 unsigned char mod_rm;
416 struct prefix_bits prf;
420 p = (unsigned char *)ins_addr;
421 p += skip_prefix(p, &prf);
422 p += get_opcode(p, &opcode);
423 for (i = 0; i < ARRAY_SIZE(reg_rop); i++)
424 if (reg_rop[i] == opcode) {
429 for (i = 0; i < ARRAY_SIZE(reg_wop); i++)
430 if (reg_wop[i] == opcode) {
435 printk(KERN_ERR "mmiotrace: Not a register instruction, opcode "
441 reg = ((mod_rm >> 3) & 0x7) | (prf.rexr << 3);
442 switch (get_ins_reg_width(ins_addr)) {
444 return *get_reg_w8(reg, prf.rex, regs);
447 return *(unsigned short *)get_reg_w32(reg, regs);
450 return *(unsigned int *)get_reg_w32(reg, regs);
454 return *(unsigned long *)get_reg_w32(reg, regs);
458 printk(KERN_ERR "mmiotrace: Error width# %d\n", reg);
465 unsigned long get_ins_imm_val(unsigned long ins_addr)
468 unsigned char mod_rm;
471 struct prefix_bits prf;
475 p = (unsigned char *)ins_addr;
476 p += skip_prefix(p, &prf);
477 p += get_opcode(p, &opcode);
478 for (i = 0; i < ARRAY_SIZE(imm_wop); i++)
479 if (imm_wop[i] == opcode) {
484 printk(KERN_ERR "mmiotrace: Not an immediate instruction, opcode "
494 /* if r/m is 5 we have a 32 disp (IA32 Manual 3, Table 2-2) */
495 /* AMD64: XXX Check for address size prefix? */
496 if ((mod_rm & 0x7) == 0x5)
510 printk(KERN_ERR "mmiotrace: not a memory access instruction "
511 "at 0x%lx, rm_mod=0x%02x\n",
515 switch (get_ins_reg_width(ins_addr)) {
517 return *(unsigned char *)p;
520 return *(unsigned short *)p;
523 return *(unsigned int *)p;
527 return *(unsigned long *)p;
531 printk(KERN_ERR "mmiotrace: Error: width.\n");