insn-x86.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <linux/types.h>
  2. #include "debug.h"
  3. #include "tests/tests.h"
  4. #include "arch-tests.h"
  5. #include "intel-pt-decoder/insn.h"
  6. #include "intel-pt-decoder/intel-pt-insn-decoder.h"
  7. struct test_data {
  8. u8 data[MAX_INSN_SIZE];
  9. int expected_length;
  10. int expected_rel;
  11. const char *expected_op_str;
  12. const char *expected_branch_str;
  13. const char *asm_rep;
  14. };
  15. struct test_data test_data_32[] = {
  16. #include "insn-x86-dat-32.c"
  17. {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee \trdpkru"},
  18. {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef \twrpkru"},
  19. {{0}, 0, 0, NULL, NULL, NULL},
  20. };
  21. struct test_data test_data_64[] = {
  22. #include "insn-x86-dat-64.c"
  23. {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee \trdpkru"},
  24. {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef \twrpkru"},
  25. {{0}, 0, 0, NULL, NULL, NULL},
  26. };
  27. static int get_op(const char *op_str)
  28. {
  29. struct val_data {
  30. const char *name;
  31. int val;
  32. } vals[] = {
  33. {"other", INTEL_PT_OP_OTHER},
  34. {"call", INTEL_PT_OP_CALL},
  35. {"ret", INTEL_PT_OP_RET},
  36. {"jcc", INTEL_PT_OP_JCC},
  37. {"jmp", INTEL_PT_OP_JMP},
  38. {"loop", INTEL_PT_OP_LOOP},
  39. {"iret", INTEL_PT_OP_IRET},
  40. {"int", INTEL_PT_OP_INT},
  41. {"syscall", INTEL_PT_OP_SYSCALL},
  42. {"sysret", INTEL_PT_OP_SYSRET},
  43. {NULL, 0},
  44. };
  45. struct val_data *val;
  46. if (!op_str || !strlen(op_str))
  47. return 0;
  48. for (val = vals; val->name; val++) {
  49. if (!strcmp(val->name, op_str))
  50. return val->val;
  51. }
  52. pr_debug("Failed to get op\n");
  53. return -1;
  54. }
  55. static int get_branch(const char *branch_str)
  56. {
  57. struct val_data {
  58. const char *name;
  59. int val;
  60. } vals[] = {
  61. {"no_branch", INTEL_PT_BR_NO_BRANCH},
  62. {"indirect", INTEL_PT_BR_INDIRECT},
  63. {"conditional", INTEL_PT_BR_CONDITIONAL},
  64. {"unconditional", INTEL_PT_BR_UNCONDITIONAL},
  65. {NULL, 0},
  66. };
  67. struct val_data *val;
  68. if (!branch_str || !strlen(branch_str))
  69. return 0;
  70. for (val = vals; val->name; val++) {
  71. if (!strcmp(val->name, branch_str))
  72. return val->val;
  73. }
  74. pr_debug("Failed to get branch\n");
  75. return -1;
  76. }
  77. static int test_data_item(struct test_data *dat, int x86_64)
  78. {
  79. struct intel_pt_insn intel_pt_insn;
  80. struct insn insn;
  81. int op, branch;
  82. insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64);
  83. insn_get_length(&insn);
  84. if (!insn_complete(&insn)) {
  85. pr_debug("Failed to decode: %s\n", dat->asm_rep);
  86. return -1;
  87. }
  88. if (insn.length != dat->expected_length) {
  89. pr_debug("Failed to decode length (%d vs expected %d): %s\n",
  90. insn.length, dat->expected_length, dat->asm_rep);
  91. return -1;
  92. }
  93. op = get_op(dat->expected_op_str);
  94. branch = get_branch(dat->expected_branch_str);
  95. if (intel_pt_get_insn(dat->data, MAX_INSN_SIZE, x86_64, &intel_pt_insn)) {
  96. pr_debug("Intel PT failed to decode: %s\n", dat->asm_rep);
  97. return -1;
  98. }
  99. if ((int)intel_pt_insn.op != op) {
  100. pr_debug("Failed to decode 'op' value (%d vs expected %d): %s\n",
  101. intel_pt_insn.op, op, dat->asm_rep);
  102. return -1;
  103. }
  104. if ((int)intel_pt_insn.branch != branch) {
  105. pr_debug("Failed to decode 'branch' value (%d vs expected %d): %s\n",
  106. intel_pt_insn.branch, branch, dat->asm_rep);
  107. return -1;
  108. }
  109. if (intel_pt_insn.rel != dat->expected_rel) {
  110. pr_debug("Failed to decode 'rel' value (%#x vs expected %#x): %s\n",
  111. intel_pt_insn.rel, dat->expected_rel, dat->asm_rep);
  112. return -1;
  113. }
  114. pr_debug("Decoded ok: %s\n", dat->asm_rep);
  115. return 0;
  116. }
  117. static int test_data_set(struct test_data *dat_set, int x86_64)
  118. {
  119. struct test_data *dat;
  120. int ret = 0;
  121. for (dat = dat_set; dat->expected_length; dat++) {
  122. if (test_data_item(dat, x86_64))
  123. ret = -1;
  124. }
  125. return ret;
  126. }
  127. /**
  128. * test__insn_x86 - test x86 instruction decoder - new instructions.
  129. *
  130. * This function implements a test that decodes a selection of instructions and
  131. * checks the results. The Intel PT function that further categorizes
  132. * instructions (i.e. intel_pt_get_insn()) is also checked.
  133. *
  134. * The instructions are originally in insn-x86-dat-src.c which has been
  135. * processed by scripts gen-insn-x86-dat.sh and gen-insn-x86-dat.awk to produce
  136. * insn-x86-dat-32.c and insn-x86-dat-64.c which are included into this program.
  137. * i.e. to add new instructions to the test, edit insn-x86-dat-src.c, run the
  138. * gen-insn-x86-dat.sh script, make perf, and then run the test.
  139. *
  140. * If the test passes %0 is returned, otherwise %-1 is returned. Use the
  141. * verbose (-v) option to see all the instructions and whether or not they
  142. * decoded successfuly.
  143. */
  144. int test__insn_x86(int subtest __maybe_unused)
  145. {
  146. int ret = 0;
  147. if (test_data_set(test_data_32, 0))
  148. ret = -1;
  149. if (test_data_set(test_data_64, 1))
  150. ret = -1;
  151. return ret;
  152. }