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