1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Fault Injection Test harness (FI)
4 * Copyright (C) Intel Crop.
7 /* Id: pf_in.c,v 1.1.1.1 2002/11/12 05:56:32 brlock Exp
8 * Copyright by Intel Crop., 2002
14 #include <linux/ptrace.h> /* struct pt_regs */
18 /* IA32 Manual 3, 2-1 */
19 static unsigned char prefix_codes[] = {
20 0xF0, 0xF2, 0xF3, 0x2E, 0x36, 0x3E, 0x26, 0x64,
23 /* IA32 Manual 3, 3-432*/
24 static unsigned int reg_rop[] = {
25 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
27 static unsigned int reg_wop[] = { 0x88, 0x89, 0xAA, 0xAB };
28 static unsigned int imm_wop[] = { 0xC6, 0xC7 };
29 /* IA32 Manual 3, 3-432*/
30 static unsigned int rw8[] = { 0x88, 0x8A, 0xC6, 0xAA };
31 static unsigned int rw32[] = {
32 0x89, 0x8B, 0xC7, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
34 static unsigned int mw8[] = { 0x88, 0x8A, 0xC6, 0xB60F, 0xBE0F, 0xAA };
35 static unsigned int mw16[] = { 0xB70F, 0xBF0F };
36 static unsigned int mw32[] = { 0x89, 0x8B, 0xC7, 0xAB };
37 static unsigned int mw64[] = {};
38 #else /* not __i386__ */
39 static unsigned char prefix_codes[] = {
40 0x66, 0x67, 0x2E, 0x3E, 0x26, 0x64, 0x65, 0x36,
43 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
44 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
46 /* AMD64 Manual 3, Appendix A*/
47 static unsigned int reg_rop[] = {
48 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
50 static unsigned int reg_wop[] = { 0x88, 0x89, 0xAA, 0xAB };
51 static unsigned int imm_wop[] = { 0xC6, 0xC7 };
52 static unsigned int rw8[] = { 0xC6, 0x88, 0x8A, 0xAA };
53 static unsigned int rw32[] = {
54 0xC7, 0x89, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
57 static unsigned int mw8[] = { 0xC6, 0x88, 0x8A, 0xB60F, 0xBE0F, 0xAA };
59 static unsigned int mw16[] = { 0xB70F, 0xBF0F };
61 static unsigned int mw32[] = { 0xC7 };
62 /* 16, 32 or 64 bit */
63 static unsigned int mw64[] = { 0x89, 0x8B, 0xAB };
64 #endif /* not __i386__ */
73 static int skip_prefix(unsigned char *addr, struct prefix_bits *prf)
76 unsigned char *p = addr;
83 for (i = 0; i < ARRAY_SIZE(prefix_codes); i++) {
84 if (*p == prefix_codes[i]) {
88 if ((*p & 0xf8) == 0x48)
90 if ((*p & 0xf4) == 0x44)
92 if ((*p & 0xf0) == 0x40)
103 static int get_opcode(unsigned char *addr, unsigned int *opcode)
108 /* 0x0F is extension instruction */
109 *opcode = *(unsigned short *)addr;
119 #define CHECK_OP_TYPE(opcode, array, type) \
120 for (i = 0; i < ARRAY_SIZE(array); i++) { \
121 if (array[i] == opcode) { \
127 enum reason_type get_ins_type(unsigned long ins_addr)
131 struct prefix_bits prf;
133 enum reason_type rv = OTHERS;
135 p = (unsigned char *)ins_addr;
136 p += skip_prefix(p, &prf);
137 p += get_opcode(p, &opcode);
139 CHECK_OP_TYPE(opcode, reg_rop, REG_READ);
140 CHECK_OP_TYPE(opcode, reg_wop, REG_WRITE);
141 CHECK_OP_TYPE(opcode, imm_wop, IMM_WRITE);
148 static unsigned int get_ins_reg_width(unsigned long ins_addr)
152 struct prefix_bits prf;
155 p = (unsigned char *)ins_addr;
156 p += skip_prefix(p, &prf);
157 p += get_opcode(p, &opcode);
159 for (i = 0; i < ARRAY_SIZE(rw8); i++)
160 if (rw8[i] == opcode)
163 for (i = 0; i < ARRAY_SIZE(rw32); i++)
164 if (rw32[i] == opcode)
165 return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
167 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
171 unsigned int get_ins_mem_width(unsigned long ins_addr)
175 struct prefix_bits prf;
178 p = (unsigned char *)ins_addr;
179 p += skip_prefix(p, &prf);
180 p += get_opcode(p, &opcode);
182 for (i = 0; i < ARRAY_SIZE(mw8); i++)
183 if (mw8[i] == opcode)
186 for (i = 0; i < ARRAY_SIZE(mw16); i++)
187 if (mw16[i] == opcode)
190 for (i = 0; i < ARRAY_SIZE(mw32); i++)
191 if (mw32[i] == opcode)
192 return prf.shorted ? 2 : 4;
194 for (i = 0; i < ARRAY_SIZE(mw64); i++)
195 if (mw64[i] == opcode)
196 return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
198 printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
203 * Define register ident in mod/rm byte.
204 * Note: these are NOT the same as in ptrace-abi.h.
236 static unsigned char *get_reg_w8(int no, int rex, struct pt_regs *regs)
238 unsigned char *rv = NULL;
242 rv = (unsigned char *)®s->ax;
245 rv = (unsigned char *)®s->bx;
248 rv = (unsigned char *)®s->cx;
251 rv = (unsigned char *)®s->dx;
255 rv = (unsigned char *)®s->r8;
258 rv = (unsigned char *)®s->r9;
261 rv = (unsigned char *)®s->r10;
264 rv = (unsigned char *)®s->r11;
267 rv = (unsigned char *)®s->r12;
270 rv = (unsigned char *)®s->r13;
273 rv = (unsigned char *)®s->r14;
276 rv = (unsigned char *)®s->r15;
288 * If REX prefix exists, access low bytes of SI etc.
293 rv = (unsigned char *)®s->si;
296 rv = (unsigned char *)®s->di;
299 rv = (unsigned char *)®s->bp;
302 rv = (unsigned char *)®s->sp;
310 rv = 1 + (unsigned char *)®s->ax;
313 rv = 1 + (unsigned char *)®s->bx;
316 rv = 1 + (unsigned char *)®s->cx;
319 rv = 1 + (unsigned char *)®s->dx;
327 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
332 static unsigned long *get_reg_w32(int no, struct pt_regs *regs)
334 unsigned long *rv = NULL;
388 printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
394 unsigned long get_ins_reg_val(unsigned long ins_addr, struct pt_regs *regs)
399 struct prefix_bits prf;
402 p = (unsigned char *)ins_addr;
403 p += skip_prefix(p, &prf);
404 p += get_opcode(p, &opcode);
405 for (i = 0; i < ARRAY_SIZE(reg_rop); i++)
406 if (reg_rop[i] == opcode)
409 for (i = 0; i < ARRAY_SIZE(reg_wop); i++)
410 if (reg_wop[i] == opcode)
413 printk(KERN_ERR "mmiotrace: Not a register instruction, opcode "
418 /* for STOS, source register is fixed */
419 if (opcode == 0xAA || opcode == 0xAB) {
422 unsigned char mod_rm = *p;
423 reg = ((mod_rm >> 3) & 0x7) | (prf.rexr << 3);
425 switch (get_ins_reg_width(ins_addr)) {
427 return *get_reg_w8(reg, prf.rex, regs);
430 return *(unsigned short *)get_reg_w32(reg, regs);
433 return *(unsigned int *)get_reg_w32(reg, regs);
437 return *(unsigned long *)get_reg_w32(reg, regs);
441 printk(KERN_ERR "mmiotrace: Error width# %d\n", reg);
448 unsigned long get_ins_imm_val(unsigned long ins_addr)
451 unsigned char mod_rm;
454 struct prefix_bits prf;
457 p = (unsigned char *)ins_addr;
458 p += skip_prefix(p, &prf);
459 p += get_opcode(p, &opcode);
460 for (i = 0; i < ARRAY_SIZE(imm_wop); i++)
461 if (imm_wop[i] == opcode)
464 printk(KERN_ERR "mmiotrace: Not an immediate instruction, opcode "
474 /* if r/m is 5 we have a 32 disp (IA32 Manual 3, Table 2-2) */
475 /* AMD64: XXX Check for address size prefix? */
476 if ((mod_rm & 0x7) == 0x5)
490 printk(KERN_ERR "mmiotrace: not a memory access instruction "
491 "at 0x%lx, rm_mod=0x%02x\n",
495 switch (get_ins_reg_width(ins_addr)) {
497 return *(unsigned char *)p;
500 return *(unsigned short *)p;
503 return *(unsigned int *)p;
507 return *(unsigned long *)p;
511 printk(KERN_ERR "mmiotrace: Error: width.\n");