pf_in.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Fault Injection Test harness (FI)
  3. * Copyright (C) Intel Crop.
  4. *
  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.
  9. *
  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.
  14. *
  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,
  18. * USA.
  19. *
  20. */
  21. /* Id: pf_in.c,v 1.1.1.1 2002/11/12 05:56:32 brlock Exp
  22. * Copyright by Intel Crop., 2002
  23. * Louis Zhuang (louis.zhuang@intel.com)
  24. *
  25. * Bjorn Steinbrink (B.Steinbrink@gmx.de), 2007
  26. */
  27. #include <linux/ptrace.h> /* struct pt_regs */
  28. #include "pf_in.h"
  29. #ifdef __i386__
  30. /* IA32 Manual 3, 2-1 */
  31. static unsigned char prefix_codes[] = {
  32. 0xF0, 0xF2, 0xF3, 0x2E, 0x36, 0x3E, 0x26, 0x64,
  33. 0x65, 0x66, 0x67
  34. };
  35. /* IA32 Manual 3, 3-432*/
  36. static unsigned int reg_rop[] = {
  37. 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
  38. };
  39. static unsigned int reg_wop[] = { 0x88, 0x89, 0xAA, 0xAB };
  40. static unsigned int imm_wop[] = { 0xC6, 0xC7 };
  41. /* IA32 Manual 3, 3-432*/
  42. static unsigned int rw8[] = { 0x88, 0x8A, 0xC6, 0xAA };
  43. static unsigned int rw32[] = {
  44. 0x89, 0x8B, 0xC7, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
  45. };
  46. static unsigned int mw8[] = { 0x88, 0x8A, 0xC6, 0xB60F, 0xBE0F, 0xAA };
  47. static unsigned int mw16[] = { 0xB70F, 0xBF0F };
  48. static unsigned int mw32[] = { 0x89, 0x8B, 0xC7, 0xAB };
  49. static unsigned int mw64[] = {};
  50. #else /* not __i386__ */
  51. static unsigned char prefix_codes[] = {
  52. 0x66, 0x67, 0x2E, 0x3E, 0x26, 0x64, 0x65, 0x36,
  53. 0xF0, 0xF3, 0xF2,
  54. /* REX Prefixes */
  55. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  56. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  57. };
  58. /* AMD64 Manual 3, Appendix A*/
  59. static unsigned int reg_rop[] = {
  60. 0x8A, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F
  61. };
  62. static unsigned int reg_wop[] = { 0x88, 0x89, 0xAA, 0xAB };
  63. static unsigned int imm_wop[] = { 0xC6, 0xC7 };
  64. static unsigned int rw8[] = { 0xC6, 0x88, 0x8A, 0xAA };
  65. static unsigned int rw32[] = {
  66. 0xC7, 0x89, 0x8B, 0xB60F, 0xB70F, 0xBE0F, 0xBF0F, 0xAB
  67. };
  68. /* 8 bit only */
  69. static unsigned int mw8[] = { 0xC6, 0x88, 0x8A, 0xB60F, 0xBE0F, 0xAA };
  70. /* 16 bit only */
  71. static unsigned int mw16[] = { 0xB70F, 0xBF0F };
  72. /* 16 or 32 bit */
  73. static unsigned int mw32[] = { 0xC7 };
  74. /* 16, 32 or 64 bit */
  75. static unsigned int mw64[] = { 0x89, 0x8B, 0xAB };
  76. #endif /* not __i386__ */
  77. struct prefix_bits {
  78. unsigned shorted:1;
  79. unsigned enlarged:1;
  80. unsigned rexr:1;
  81. unsigned rex:1;
  82. };
  83. static int skip_prefix(unsigned char *addr, struct prefix_bits *prf)
  84. {
  85. int i;
  86. unsigned char *p = addr;
  87. prf->shorted = 0;
  88. prf->enlarged = 0;
  89. prf->rexr = 0;
  90. prf->rex = 0;
  91. restart:
  92. for (i = 0; i < ARRAY_SIZE(prefix_codes); i++) {
  93. if (*p == prefix_codes[i]) {
  94. if (*p == 0x66)
  95. prf->shorted = 1;
  96. #ifdef __amd64__
  97. if ((*p & 0xf8) == 0x48)
  98. prf->enlarged = 1;
  99. if ((*p & 0xf4) == 0x44)
  100. prf->rexr = 1;
  101. if ((*p & 0xf0) == 0x40)
  102. prf->rex = 1;
  103. #endif
  104. p++;
  105. goto restart;
  106. }
  107. }
  108. return (p - addr);
  109. }
  110. static int get_opcode(unsigned char *addr, unsigned int *opcode)
  111. {
  112. int len;
  113. if (*addr == 0x0F) {
  114. /* 0x0F is extension instruction */
  115. *opcode = *(unsigned short *)addr;
  116. len = 2;
  117. } else {
  118. *opcode = *addr;
  119. len = 1;
  120. }
  121. return len;
  122. }
  123. #define CHECK_OP_TYPE(opcode, array, type) \
  124. for (i = 0; i < ARRAY_SIZE(array); i++) { \
  125. if (array[i] == opcode) { \
  126. rv = type; \
  127. goto exit; \
  128. } \
  129. }
  130. enum reason_type get_ins_type(unsigned long ins_addr)
  131. {
  132. unsigned int opcode;
  133. unsigned char *p;
  134. struct prefix_bits prf;
  135. int i;
  136. enum reason_type rv = OTHERS;
  137. p = (unsigned char *)ins_addr;
  138. p += skip_prefix(p, &prf);
  139. p += get_opcode(p, &opcode);
  140. CHECK_OP_TYPE(opcode, reg_rop, REG_READ);
  141. CHECK_OP_TYPE(opcode, reg_wop, REG_WRITE);
  142. CHECK_OP_TYPE(opcode, imm_wop, IMM_WRITE);
  143. exit:
  144. return rv;
  145. }
  146. #undef CHECK_OP_TYPE
  147. static unsigned int get_ins_reg_width(unsigned long ins_addr)
  148. {
  149. unsigned int opcode;
  150. unsigned char *p;
  151. struct prefix_bits prf;
  152. int i;
  153. p = (unsigned char *)ins_addr;
  154. p += skip_prefix(p, &prf);
  155. p += get_opcode(p, &opcode);
  156. for (i = 0; i < ARRAY_SIZE(rw8); i++)
  157. if (rw8[i] == opcode)
  158. return 1;
  159. for (i = 0; i < ARRAY_SIZE(rw32); i++)
  160. if (rw32[i] == opcode)
  161. return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
  162. printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
  163. return 0;
  164. }
  165. unsigned int get_ins_mem_width(unsigned long ins_addr)
  166. {
  167. unsigned int opcode;
  168. unsigned char *p;
  169. struct prefix_bits prf;
  170. int i;
  171. p = (unsigned char *)ins_addr;
  172. p += skip_prefix(p, &prf);
  173. p += get_opcode(p, &opcode);
  174. for (i = 0; i < ARRAY_SIZE(mw8); i++)
  175. if (mw8[i] == opcode)
  176. return 1;
  177. for (i = 0; i < ARRAY_SIZE(mw16); i++)
  178. if (mw16[i] == opcode)
  179. return 2;
  180. for (i = 0; i < ARRAY_SIZE(mw32); i++)
  181. if (mw32[i] == opcode)
  182. return prf.shorted ? 2 : 4;
  183. for (i = 0; i < ARRAY_SIZE(mw64); i++)
  184. if (mw64[i] == opcode)
  185. return prf.shorted ? 2 : (prf.enlarged ? 8 : 4);
  186. printk(KERN_ERR "mmiotrace: Unknown opcode 0x%02x\n", opcode);
  187. return 0;
  188. }
  189. /*
  190. * Define register ident in mod/rm byte.
  191. * Note: these are NOT the same as in ptrace-abi.h.
  192. */
  193. enum {
  194. arg_AL = 0,
  195. arg_CL = 1,
  196. arg_DL = 2,
  197. arg_BL = 3,
  198. arg_AH = 4,
  199. arg_CH = 5,
  200. arg_DH = 6,
  201. arg_BH = 7,
  202. arg_AX = 0,
  203. arg_CX = 1,
  204. arg_DX = 2,
  205. arg_BX = 3,
  206. arg_SP = 4,
  207. arg_BP = 5,
  208. arg_SI = 6,
  209. arg_DI = 7,
  210. #ifdef __amd64__
  211. arg_R8 = 8,
  212. arg_R9 = 9,
  213. arg_R10 = 10,
  214. arg_R11 = 11,
  215. arg_R12 = 12,
  216. arg_R13 = 13,
  217. arg_R14 = 14,
  218. arg_R15 = 15
  219. #endif
  220. };
  221. static unsigned char *get_reg_w8(int no, int rex, struct pt_regs *regs)
  222. {
  223. unsigned char *rv = NULL;
  224. switch (no) {
  225. case arg_AL:
  226. rv = (unsigned char *)&regs->ax;
  227. break;
  228. case arg_BL:
  229. rv = (unsigned char *)&regs->bx;
  230. break;
  231. case arg_CL:
  232. rv = (unsigned char *)&regs->cx;
  233. break;
  234. case arg_DL:
  235. rv = (unsigned char *)&regs->dx;
  236. break;
  237. #ifdef __amd64__
  238. case arg_R8:
  239. rv = (unsigned char *)&regs->r8;
  240. break;
  241. case arg_R9:
  242. rv = (unsigned char *)&regs->r9;
  243. break;
  244. case arg_R10:
  245. rv = (unsigned char *)&regs->r10;
  246. break;
  247. case arg_R11:
  248. rv = (unsigned char *)&regs->r11;
  249. break;
  250. case arg_R12:
  251. rv = (unsigned char *)&regs->r12;
  252. break;
  253. case arg_R13:
  254. rv = (unsigned char *)&regs->r13;
  255. break;
  256. case arg_R14:
  257. rv = (unsigned char *)&regs->r14;
  258. break;
  259. case arg_R15:
  260. rv = (unsigned char *)&regs->r15;
  261. break;
  262. #endif
  263. default:
  264. break;
  265. }
  266. if (rv)
  267. return rv;
  268. if (rex) {
  269. /*
  270. * If REX prefix exists, access low bytes of SI etc.
  271. * instead of AH etc.
  272. */
  273. switch (no) {
  274. case arg_SI:
  275. rv = (unsigned char *)&regs->si;
  276. break;
  277. case arg_DI:
  278. rv = (unsigned char *)&regs->di;
  279. break;
  280. case arg_BP:
  281. rv = (unsigned char *)&regs->bp;
  282. break;
  283. case arg_SP:
  284. rv = (unsigned char *)&regs->sp;
  285. break;
  286. default:
  287. break;
  288. }
  289. } else {
  290. switch (no) {
  291. case arg_AH:
  292. rv = 1 + (unsigned char *)&regs->ax;
  293. break;
  294. case arg_BH:
  295. rv = 1 + (unsigned char *)&regs->bx;
  296. break;
  297. case arg_CH:
  298. rv = 1 + (unsigned char *)&regs->cx;
  299. break;
  300. case arg_DH:
  301. rv = 1 + (unsigned char *)&regs->dx;
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. if (!rv)
  308. printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
  309. return rv;
  310. }
  311. static unsigned long *get_reg_w32(int no, struct pt_regs *regs)
  312. {
  313. unsigned long *rv = NULL;
  314. switch (no) {
  315. case arg_AX:
  316. rv = &regs->ax;
  317. break;
  318. case arg_BX:
  319. rv = &regs->bx;
  320. break;
  321. case arg_CX:
  322. rv = &regs->cx;
  323. break;
  324. case arg_DX:
  325. rv = &regs->dx;
  326. break;
  327. case arg_SP:
  328. rv = &regs->sp;
  329. break;
  330. case arg_BP:
  331. rv = &regs->bp;
  332. break;
  333. case arg_SI:
  334. rv = &regs->si;
  335. break;
  336. case arg_DI:
  337. rv = &regs->di;
  338. break;
  339. #ifdef __amd64__
  340. case arg_R8:
  341. rv = &regs->r8;
  342. break;
  343. case arg_R9:
  344. rv = &regs->r9;
  345. break;
  346. case arg_R10:
  347. rv = &regs->r10;
  348. break;
  349. case arg_R11:
  350. rv = &regs->r11;
  351. break;
  352. case arg_R12:
  353. rv = &regs->r12;
  354. break;
  355. case arg_R13:
  356. rv = &regs->r13;
  357. break;
  358. case arg_R14:
  359. rv = &regs->r14;
  360. break;
  361. case arg_R15:
  362. rv = &regs->r15;
  363. break;
  364. #endif
  365. default:
  366. printk(KERN_ERR "mmiotrace: Error reg no# %d\n", no);
  367. }
  368. return rv;
  369. }
  370. unsigned long get_ins_reg_val(unsigned long ins_addr, struct pt_regs *regs)
  371. {
  372. unsigned int opcode;
  373. int reg;
  374. unsigned char *p;
  375. struct prefix_bits prf;
  376. int i;
  377. p = (unsigned char *)ins_addr;
  378. p += skip_prefix(p, &prf);
  379. p += get_opcode(p, &opcode);
  380. for (i = 0; i < ARRAY_SIZE(reg_rop); i++)
  381. if (reg_rop[i] == opcode)
  382. goto do_work;
  383. for (i = 0; i < ARRAY_SIZE(reg_wop); i++)
  384. if (reg_wop[i] == opcode)
  385. goto do_work;
  386. printk(KERN_ERR "mmiotrace: Not a register instruction, opcode "
  387. "0x%02x\n", opcode);
  388. goto err;
  389. do_work:
  390. /* for STOS, source register is fixed */
  391. if (opcode == 0xAA || opcode == 0xAB) {
  392. reg = arg_AX;
  393. } else {
  394. unsigned char mod_rm = *p;
  395. reg = ((mod_rm >> 3) & 0x7) | (prf.rexr << 3);
  396. }
  397. switch (get_ins_reg_width(ins_addr)) {
  398. case 1:
  399. return *get_reg_w8(reg, prf.rex, regs);
  400. case 2:
  401. return *(unsigned short *)get_reg_w32(reg, regs);
  402. case 4:
  403. return *(unsigned int *)get_reg_w32(reg, regs);
  404. #ifdef __amd64__
  405. case 8:
  406. return *(unsigned long *)get_reg_w32(reg, regs);
  407. #endif
  408. default:
  409. printk(KERN_ERR "mmiotrace: Error width# %d\n", reg);
  410. }
  411. err:
  412. return 0;
  413. }
  414. unsigned long get_ins_imm_val(unsigned long ins_addr)
  415. {
  416. unsigned int opcode;
  417. unsigned char mod_rm;
  418. unsigned char mod;
  419. unsigned char *p;
  420. struct prefix_bits prf;
  421. int i;
  422. p = (unsigned char *)ins_addr;
  423. p += skip_prefix(p, &prf);
  424. p += get_opcode(p, &opcode);
  425. for (i = 0; i < ARRAY_SIZE(imm_wop); i++)
  426. if (imm_wop[i] == opcode)
  427. goto do_work;
  428. printk(KERN_ERR "mmiotrace: Not an immediate instruction, opcode "
  429. "0x%02x\n", opcode);
  430. goto err;
  431. do_work:
  432. mod_rm = *p;
  433. mod = mod_rm >> 6;
  434. p++;
  435. switch (mod) {
  436. case 0:
  437. /* if r/m is 5 we have a 32 disp (IA32 Manual 3, Table 2-2) */
  438. /* AMD64: XXX Check for address size prefix? */
  439. if ((mod_rm & 0x7) == 0x5)
  440. p += 4;
  441. break;
  442. case 1:
  443. p += 1;
  444. break;
  445. case 2:
  446. p += 4;
  447. break;
  448. case 3:
  449. default:
  450. printk(KERN_ERR "mmiotrace: not a memory access instruction "
  451. "at 0x%lx, rm_mod=0x%02x\n",
  452. ins_addr, mod_rm);
  453. }
  454. switch (get_ins_reg_width(ins_addr)) {
  455. case 1:
  456. return *(unsigned char *)p;
  457. case 2:
  458. return *(unsigned short *)p;
  459. case 4:
  460. return *(unsigned int *)p;
  461. #ifdef __amd64__
  462. case 8:
  463. return *(unsigned long *)p;
  464. #endif
  465. default:
  466. printk(KERN_ERR "mmiotrace: Error: width.\n");
  467. }
  468. err:
  469. return 0;
  470. }