insn-x86.c 4.6 KB

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