annotate-x86.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <string.h>
  3. #include <linux/compiler.h>
  4. #include <assert.h>
  5. #include <inttypes.h>
  6. #include "../annotate-data.h"
  7. #include "../debug.h"
  8. #include "../disasm.h"
  9. #include "../dso.h"
  10. #include "../map.h"
  11. #include "../string2.h" // strstarts
  12. #include "../symbol.h"
  13. /*
  14. * x86 instruction nmemonic table to parse disasm lines for annotate.
  15. * This table is searched twice - one for exact match and another for
  16. * match without a size suffix (b, w, l, q) in case of AT&T syntax.
  17. *
  18. * So this table should not have entries with the suffix unless it's
  19. * a complete different instruction than ones without the suffix.
  20. */
  21. static const struct ins x86__instructions[] = {
  22. { .name = "adc", .ops = &mov_ops, },
  23. { .name = "add", .ops = &mov_ops, },
  24. { .name = "addsd", .ops = &mov_ops, },
  25. { .name = "and", .ops = &mov_ops, },
  26. { .name = "andpd", .ops = &mov_ops, },
  27. { .name = "andps", .ops = &mov_ops, },
  28. { .name = "bsr", .ops = &mov_ops, },
  29. { .name = "bt", .ops = &mov_ops, },
  30. { .name = "btr", .ops = &mov_ops, },
  31. { .name = "bts", .ops = &mov_ops, },
  32. { .name = "call", .ops = &call_ops, },
  33. { .name = "cmovae", .ops = &mov_ops, },
  34. { .name = "cmovbe", .ops = &mov_ops, },
  35. { .name = "cmove", .ops = &mov_ops, },
  36. { .name = "cmp", .ops = &mov_ops, },
  37. { .name = "cmpxch", .ops = &mov_ops, },
  38. { .name = "cmpxchg", .ops = &mov_ops, },
  39. { .name = "cs", .ops = &mov_ops, },
  40. { .name = "dec", .ops = &dec_ops, },
  41. { .name = "divsd", .ops = &mov_ops, },
  42. { .name = "divss", .ops = &mov_ops, },
  43. { .name = "gs", .ops = &mov_ops, },
  44. { .name = "imul", .ops = &mov_ops, },
  45. { .name = "inc", .ops = &dec_ops, },
  46. { .name = "ja", .ops = &jump_ops, },
  47. { .name = "jae", .ops = &jump_ops, },
  48. { .name = "jb", .ops = &jump_ops, },
  49. { .name = "jbe", .ops = &jump_ops, },
  50. { .name = "jc", .ops = &jump_ops, },
  51. { .name = "jcxz", .ops = &jump_ops, },
  52. { .name = "je", .ops = &jump_ops, },
  53. { .name = "jecxz", .ops = &jump_ops, },
  54. { .name = "jg", .ops = &jump_ops, },
  55. { .name = "jge", .ops = &jump_ops, },
  56. { .name = "jl", .ops = &jump_ops, },
  57. { .name = "jle", .ops = &jump_ops, },
  58. { .name = "jmp", .ops = &jump_ops, },
  59. { .name = "jna", .ops = &jump_ops, },
  60. { .name = "jnae", .ops = &jump_ops, },
  61. { .name = "jnb", .ops = &jump_ops, },
  62. { .name = "jnbe", .ops = &jump_ops, },
  63. { .name = "jnc", .ops = &jump_ops, },
  64. { .name = "jne", .ops = &jump_ops, },
  65. { .name = "jng", .ops = &jump_ops, },
  66. { .name = "jnge", .ops = &jump_ops, },
  67. { .name = "jnl", .ops = &jump_ops, },
  68. { .name = "jnle", .ops = &jump_ops, },
  69. { .name = "jno", .ops = &jump_ops, },
  70. { .name = "jnp", .ops = &jump_ops, },
  71. { .name = "jns", .ops = &jump_ops, },
  72. { .name = "jnz", .ops = &jump_ops, },
  73. { .name = "jo", .ops = &jump_ops, },
  74. { .name = "jp", .ops = &jump_ops, },
  75. { .name = "jpe", .ops = &jump_ops, },
  76. { .name = "jpo", .ops = &jump_ops, },
  77. { .name = "jrcxz", .ops = &jump_ops, },
  78. { .name = "js", .ops = &jump_ops, },
  79. { .name = "jz", .ops = &jump_ops, },
  80. { .name = "lea", .ops = &mov_ops, },
  81. { .name = "lock", .ops = &lock_ops, },
  82. { .name = "mov", .ops = &mov_ops, },
  83. { .name = "movapd", .ops = &mov_ops, },
  84. { .name = "movaps", .ops = &mov_ops, },
  85. { .name = "movdqa", .ops = &mov_ops, },
  86. { .name = "movdqu", .ops = &mov_ops, },
  87. { .name = "movsb", .ops = &mov_ops, },
  88. { .name = "movsd", .ops = &mov_ops, },
  89. { .name = "movsl", .ops = &mov_ops, },
  90. { .name = "movss", .ops = &mov_ops, },
  91. { .name = "movsw", .ops = &mov_ops, },
  92. { .name = "movupd", .ops = &mov_ops, },
  93. { .name = "movups", .ops = &mov_ops, },
  94. { .name = "movzb", .ops = &mov_ops, },
  95. { .name = "movzl", .ops = &mov_ops, },
  96. { .name = "movzw", .ops = &mov_ops, },
  97. { .name = "mulsd", .ops = &mov_ops, },
  98. { .name = "mulss", .ops = &mov_ops, },
  99. { .name = "nop", .ops = &nop_ops, },
  100. { .name = "or", .ops = &mov_ops, },
  101. { .name = "orps", .ops = &mov_ops, },
  102. { .name = "paddq", .ops = &mov_ops, },
  103. { .name = "pand", .ops = &mov_ops, },
  104. { .name = "pcmpeqb", .ops = &mov_ops, },
  105. { .name = "por", .ops = &mov_ops, },
  106. { .name = "rcl", .ops = &mov_ops, },
  107. { .name = "ret", .ops = &ret_ops, },
  108. { .name = "sbb", .ops = &mov_ops, },
  109. { .name = "sete", .ops = &mov_ops, },
  110. { .name = "sub", .ops = &mov_ops, },
  111. { .name = "subsd", .ops = &mov_ops, },
  112. { .name = "test", .ops = &mov_ops, },
  113. { .name = "tzcnt", .ops = &mov_ops, },
  114. { .name = "ucomisd", .ops = &mov_ops, },
  115. { .name = "ucomiss", .ops = &mov_ops, },
  116. { .name = "vaddsd", .ops = &mov_ops, },
  117. { .name = "vandpd", .ops = &mov_ops, },
  118. { .name = "vmovdqa", .ops = &mov_ops, },
  119. { .name = "vmovq", .ops = &mov_ops, },
  120. { .name = "vmovsd", .ops = &mov_ops, },
  121. { .name = "vmulsd", .ops = &mov_ops, },
  122. { .name = "vorpd", .ops = &mov_ops, },
  123. { .name = "vsubsd", .ops = &mov_ops, },
  124. { .name = "vucomisd", .ops = &mov_ops, },
  125. { .name = "xadd", .ops = &mov_ops, },
  126. { .name = "xbegin", .ops = &jump_ops, },
  127. { .name = "xchg", .ops = &mov_ops, },
  128. { .name = "xor", .ops = &mov_ops, },
  129. { .name = "xorpd", .ops = &mov_ops, },
  130. { .name = "xorps", .ops = &mov_ops, },
  131. };
  132. static bool amd__ins_is_fused(const struct arch *arch, const char *ins1,
  133. const char *ins2)
  134. {
  135. if (strstr(ins2, "jmp"))
  136. return false;
  137. /* Family >= 15h supports cmp/test + branch fusion */
  138. if (arch->family >= 0x15 && (strstarts(ins1, "test") ||
  139. (strstarts(ins1, "cmp") && !strstr(ins1, "xchg")))) {
  140. return true;
  141. }
  142. /* Family >= 19h supports some ALU + branch fusion */
  143. if (arch->family >= 0x19 && (strstarts(ins1, "add") ||
  144. strstarts(ins1, "sub") || strstarts(ins1, "and") ||
  145. strstarts(ins1, "inc") || strstarts(ins1, "dec") ||
  146. strstarts(ins1, "or") || strstarts(ins1, "xor"))) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. static bool intel__ins_is_fused(const struct arch *arch, const char *ins1,
  152. const char *ins2)
  153. {
  154. if (arch->family != 6 || arch->model < 0x1e || strstr(ins2, "jmp"))
  155. return false;
  156. if (arch->model == 0x1e) {
  157. /* Nehalem */
  158. if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) ||
  159. strstr(ins1, "test")) {
  160. return true;
  161. }
  162. } else {
  163. /* Newer platform */
  164. if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) ||
  165. strstr(ins1, "test") ||
  166. strstr(ins1, "add") ||
  167. strstr(ins1, "sub") ||
  168. strstr(ins1, "and") ||
  169. strstr(ins1, "inc") ||
  170. strstr(ins1, "dec")) {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. static int x86__cpuid_parse(struct arch *arch, const char *cpuid)
  177. {
  178. unsigned int family, model, stepping;
  179. int ret;
  180. /*
  181. * cpuid = "GenuineIntel,family,model,stepping"
  182. */
  183. ret = sscanf(cpuid, "%*[^,],%u,%u,%u", &family, &model, &stepping);
  184. if (ret == 3) {
  185. arch->family = family;
  186. arch->model = model;
  187. arch->ins_is_fused = strstarts(cpuid, "AuthenticAMD") ?
  188. amd__ins_is_fused :
  189. intel__ins_is_fused;
  190. return 0;
  191. }
  192. return -1;
  193. }
  194. #ifdef HAVE_LIBDW_SUPPORT
  195. static void update_insn_state_x86(struct type_state *state,
  196. struct data_loc_info *dloc, Dwarf_Die *cu_die,
  197. struct disasm_line *dl)
  198. {
  199. struct annotated_insn_loc loc;
  200. struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE];
  201. struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET];
  202. struct type_state_reg *tsr;
  203. Dwarf_Die type_die;
  204. u32 insn_offset = dl->al.offset;
  205. int fbreg = dloc->fbreg;
  206. int fboff = 0;
  207. if (annotate_get_insn_location(dloc->arch, dl, &loc) < 0)
  208. return;
  209. if (ins__is_call(&dl->ins)) {
  210. struct symbol *func = dl->ops.target.sym;
  211. if (func == NULL)
  212. return;
  213. /* __fentry__ will preserve all registers */
  214. if (!strcmp(func->name, "__fentry__"))
  215. return;
  216. pr_debug_dtp("call [%x] %s\n", insn_offset, func->name);
  217. /* Otherwise invalidate caller-saved registers after call */
  218. for (unsigned i = 0; i < ARRAY_SIZE(state->regs); i++) {
  219. if (state->regs[i].caller_saved)
  220. state->regs[i].ok = false;
  221. }
  222. /* Update register with the return type (if any) */
  223. if (die_find_func_rettype(cu_die, func->name, &type_die)) {
  224. tsr = &state->regs[state->ret_reg];
  225. tsr->type = type_die;
  226. tsr->kind = TSR_KIND_TYPE;
  227. tsr->offset = 0;
  228. tsr->ok = true;
  229. pr_debug_dtp("call [%x] return -> reg%d",
  230. insn_offset, state->ret_reg);
  231. pr_debug_type_name(&type_die, tsr->kind);
  232. }
  233. return;
  234. }
  235. if (!strncmp(dl->ins.name, "add", 3)) {
  236. u64 imm_value = -1ULL;
  237. int offset;
  238. const char *var_name = NULL;
  239. struct map_symbol *ms = dloc->ms;
  240. u64 ip = ms->sym->start + dl->al.offset;
  241. if (!has_reg_type(state, dst->reg1))
  242. return;
  243. tsr = &state->regs[dst->reg1];
  244. tsr->copied_from = -1;
  245. if (src->imm)
  246. imm_value = src->offset;
  247. else if (has_reg_type(state, src->reg1) &&
  248. state->regs[src->reg1].kind == TSR_KIND_CONST)
  249. imm_value = state->regs[src->reg1].imm_value;
  250. else if (src->reg1 == DWARF_REG_PC) {
  251. u64 var_addr = annotate_calc_pcrel(dloc->ms, ip,
  252. src->offset, dl);
  253. if (get_global_var_info(dloc, var_addr,
  254. &var_name, &offset) &&
  255. !strcmp(var_name, "this_cpu_off") &&
  256. tsr->kind == TSR_KIND_CONST) {
  257. tsr->kind = TSR_KIND_PERCPU_BASE;
  258. tsr->offset = 0;
  259. tsr->ok = true;
  260. imm_value = tsr->imm_value;
  261. }
  262. }
  263. else
  264. return;
  265. /* Ignore add to non-pointer or non-const types */
  266. if (tsr->kind == TSR_KIND_POINTER ||
  267. (dwarf_tag(&tsr->type) == DW_TAG_pointer_type &&
  268. src->reg1 != DWARF_REG_PC && tsr->kind == TSR_KIND_TYPE && !dst->mem_ref)) {
  269. tsr->offset += imm_value;
  270. pr_debug_dtp("add [%x] offset %#"PRIx64" to reg%d",
  271. insn_offset, imm_value, dst->reg1);
  272. pr_debug_type_name(&tsr->type, tsr->kind);
  273. }
  274. if (tsr->kind == TSR_KIND_CONST)
  275. tsr->imm_value += imm_value;
  276. if (tsr->kind != TSR_KIND_PERCPU_BASE)
  277. return;
  278. if (get_global_var_type(cu_die, dloc, ip, imm_value, &offset,
  279. &type_die) && offset == 0) {
  280. /*
  281. * This is not a pointer type, but it should be treated
  282. * as a pointer.
  283. */
  284. tsr->type = type_die;
  285. tsr->kind = TSR_KIND_PERCPU_POINTER;
  286. tsr->offset = 0;
  287. tsr->ok = true;
  288. pr_debug_dtp("add [%x] percpu %#"PRIx64" -> reg%d",
  289. insn_offset, imm_value, dst->reg1);
  290. pr_debug_type_name(&tsr->type, tsr->kind);
  291. }
  292. return;
  293. }
  294. if (!strncmp(dl->ins.name, "sub", 3)) {
  295. u64 imm_value = -1ULL;
  296. if (!has_reg_type(state, dst->reg1))
  297. return;
  298. tsr = &state->regs[dst->reg1];
  299. tsr->copied_from = -1;
  300. if (src->imm)
  301. imm_value = src->offset;
  302. else if (has_reg_type(state, src->reg1) &&
  303. state->regs[src->reg1].kind == TSR_KIND_CONST)
  304. imm_value = state->regs[src->reg1].imm_value;
  305. if (tsr->kind == TSR_KIND_POINTER ||
  306. (dwarf_tag(&tsr->type) == DW_TAG_pointer_type &&
  307. src->reg1 != DWARF_REG_PC && tsr->kind == TSR_KIND_TYPE && !dst->mem_ref)) {
  308. tsr->offset -= imm_value;
  309. pr_debug_dtp("sub [%x] offset %#"PRIx64" to reg%d",
  310. insn_offset, imm_value, dst->reg1);
  311. pr_debug_type_name(&tsr->type, tsr->kind);
  312. }
  313. if (tsr->kind == TSR_KIND_CONST)
  314. tsr->imm_value -= imm_value;
  315. return;
  316. }
  317. if (!strncmp(dl->ins.name, "lea", 3)) {
  318. int sreg = src->reg1;
  319. struct type_state_reg src_tsr;
  320. if (!has_reg_type(state, sreg) ||
  321. !has_reg_type(state, dst->reg1) ||
  322. !src->mem_ref)
  323. return;
  324. src_tsr = state->regs[sreg];
  325. tsr = &state->regs[dst->reg1];
  326. tsr->copied_from = -1;
  327. tsr->ok = false;
  328. /* Case 1: Based on stack pointer or frame pointer */
  329. if (sreg == fbreg || sreg == state->stack_reg) {
  330. struct type_state_stack *stack;
  331. int offset = src->offset - fboff;
  332. stack = find_stack_state(state, offset);
  333. if (!stack)
  334. return;
  335. tsr->type = stack->type;
  336. tsr->kind = TSR_KIND_POINTER;
  337. tsr->offset = offset - stack->offset;
  338. tsr->ok = true;
  339. if (sreg == fbreg) {
  340. pr_debug_dtp("lea [%x] address of -%#x(stack) -> reg%d",
  341. insn_offset, -src->offset, dst->reg1);
  342. } else {
  343. pr_debug_dtp("lea [%x] address of %#x(reg%d) -> reg%d",
  344. insn_offset, src->offset, sreg, dst->reg1);
  345. }
  346. pr_debug_type_name(&tsr->type, tsr->kind);
  347. }
  348. /* Case 2: Based on a register holding a typed pointer */
  349. else if (src_tsr.ok && (src_tsr.kind == TSR_KIND_POINTER ||
  350. (dwarf_tag(&src_tsr.type) == DW_TAG_pointer_type &&
  351. src_tsr.kind == TSR_KIND_TYPE))) {
  352. if (src_tsr.kind == TSR_KIND_TYPE &&
  353. __die_get_real_type(&state->regs[sreg].type, &type_die) == NULL)
  354. return;
  355. if (src_tsr.kind == TSR_KIND_POINTER)
  356. type_die = state->regs[sreg].type;
  357. /* Check if the target type has a member at the new offset */
  358. if (die_get_member_type(&type_die,
  359. src->offset + src_tsr.offset, &type_die) == NULL)
  360. return;
  361. tsr->type = src_tsr.type;
  362. tsr->kind = src_tsr.kind;
  363. tsr->offset = src->offset + src_tsr.offset;
  364. tsr->ok = true;
  365. pr_debug_dtp("lea [%x] address of %s%#x(reg%d) -> reg%d",
  366. insn_offset, src->offset < 0 ? "-" : "",
  367. abs(src->offset), sreg, dst->reg1);
  368. pr_debug_type_name(&tsr->type, tsr->kind);
  369. }
  370. return;
  371. }
  372. /* Invalidate register states for other ops which may change pointers */
  373. if (has_reg_type(state, dst->reg1) && !dst->mem_ref &&
  374. dwarf_tag(&state->regs[dst->reg1].type) == DW_TAG_pointer_type) {
  375. if (!strncmp(dl->ins.name, "imul", 4) || !strncmp(dl->ins.name, "mul", 3) ||
  376. !strncmp(dl->ins.name, "idiv", 4) || !strncmp(dl->ins.name, "div", 3) ||
  377. !strncmp(dl->ins.name, "shl", 3) || !strncmp(dl->ins.name, "shr", 3) ||
  378. !strncmp(dl->ins.name, "sar", 3) || !strncmp(dl->ins.name, "and", 3) ||
  379. !strncmp(dl->ins.name, "or", 2) || !strncmp(dl->ins.name, "neg", 3) ||
  380. !strncmp(dl->ins.name, "inc", 3) || !strncmp(dl->ins.name, "dec", 3)) {
  381. pr_debug_dtp("%s [%x] invalidate reg%d\n",
  382. dl->ins.name, insn_offset, dst->reg1);
  383. state->regs[dst->reg1].ok = false;
  384. state->regs[dst->reg1].copied_from = -1;
  385. return;
  386. }
  387. if (!strncmp(dl->ins.name, "xor", 3) && dst->reg1 == src->reg1) {
  388. /* xor reg, reg clears the register */
  389. pr_debug_dtp("xor [%x] clear reg%d\n",
  390. insn_offset, dst->reg1);
  391. state->regs[dst->reg1].kind = TSR_KIND_CONST;
  392. state->regs[dst->reg1].imm_value = 0;
  393. state->regs[dst->reg1].ok = true;
  394. state->regs[dst->reg1].copied_from = -1;
  395. return;
  396. }
  397. }
  398. if (strncmp(dl->ins.name, "mov", 3))
  399. return;
  400. if (dloc->fb_cfa) {
  401. u64 ip = dloc->ms->sym->start + dl->al.offset;
  402. u64 pc = map__rip_2objdump(dloc->ms->map, ip);
  403. if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
  404. fbreg = -1;
  405. }
  406. /* Case 1. register to register or segment:offset to register transfers */
  407. if (!src->mem_ref && !dst->mem_ref) {
  408. if (!has_reg_type(state, dst->reg1))
  409. return;
  410. tsr = &state->regs[dst->reg1];
  411. tsr->copied_from = -1;
  412. if (dso__kernel(map__dso(dloc->ms->map)) &&
  413. src->segment == INSN_SEG_X86_GS && src->imm) {
  414. u64 ip = dloc->ms->sym->start + dl->al.offset;
  415. u64 var_addr;
  416. int offset;
  417. /*
  418. * In kernel, %gs points to a per-cpu region for the
  419. * current CPU. Access with a constant offset should
  420. * be treated as a global variable access.
  421. */
  422. var_addr = src->offset;
  423. if (var_addr == 40) {
  424. tsr->kind = TSR_KIND_CANARY;
  425. tsr->offset = 0;
  426. tsr->ok = true;
  427. pr_debug_dtp("mov [%x] stack canary -> reg%d\n",
  428. insn_offset, dst->reg1);
  429. return;
  430. }
  431. if (!get_global_var_type(cu_die, dloc, ip, var_addr,
  432. &offset, &type_die) ||
  433. !die_get_member_type(&type_die, offset, &type_die)) {
  434. tsr->ok = false;
  435. return;
  436. }
  437. tsr->type = type_die;
  438. tsr->kind = TSR_KIND_TYPE;
  439. tsr->offset = 0;
  440. tsr->ok = true;
  441. pr_debug_dtp("mov [%x] this-cpu addr=%#"PRIx64" -> reg%d",
  442. insn_offset, var_addr, dst->reg1);
  443. pr_debug_type_name(&tsr->type, tsr->kind);
  444. return;
  445. }
  446. if (src->imm) {
  447. tsr->kind = TSR_KIND_CONST;
  448. tsr->imm_value = src->offset;
  449. tsr->offset = 0;
  450. tsr->ok = true;
  451. pr_debug_dtp("mov [%x] imm=%#x -> reg%d\n",
  452. insn_offset, tsr->imm_value, dst->reg1);
  453. return;
  454. }
  455. if (!has_reg_type(state, src->reg1) ||
  456. !state->regs[src->reg1].ok) {
  457. tsr->ok = false;
  458. return;
  459. }
  460. tsr->type = state->regs[src->reg1].type;
  461. tsr->kind = state->regs[src->reg1].kind;
  462. tsr->imm_value = state->regs[src->reg1].imm_value;
  463. tsr->offset = state->regs[src->reg1].offset;
  464. tsr->ok = true;
  465. /* To copy back the variable type later (hopefully) */
  466. if (tsr->kind == TSR_KIND_TYPE || tsr->kind == TSR_KIND_POINTER)
  467. tsr->copied_from = src->reg1;
  468. pr_debug_dtp("mov [%x] reg%d -> reg%d",
  469. insn_offset, src->reg1, dst->reg1);
  470. pr_debug_type_name(&tsr->type, tsr->kind);
  471. }
  472. /* Case 2. memory to register transers */
  473. if (src->mem_ref && !dst->mem_ref) {
  474. int sreg = src->reg1;
  475. if (!has_reg_type(state, dst->reg1))
  476. return;
  477. tsr = &state->regs[dst->reg1];
  478. tsr->copied_from = -1;
  479. retry:
  480. /* Check stack variables with offset */
  481. if (sreg == fbreg || sreg == state->stack_reg) {
  482. struct type_state_stack *stack;
  483. int offset = src->offset - fboff;
  484. stack = find_stack_state(state, offset);
  485. if (stack == NULL) {
  486. tsr->ok = false;
  487. return;
  488. } else if (!stack->compound) {
  489. tsr->type = stack->type;
  490. tsr->kind = stack->kind;
  491. tsr->offset = stack->ptr_offset;
  492. tsr->ok = true;
  493. } else if (die_get_member_type(&stack->type,
  494. offset - stack->offset,
  495. &type_die)) {
  496. tsr->type = type_die;
  497. tsr->kind = TSR_KIND_TYPE;
  498. tsr->offset = 0;
  499. tsr->ok = true;
  500. } else {
  501. tsr->ok = false;
  502. return;
  503. }
  504. if (sreg == fbreg) {
  505. pr_debug_dtp("mov [%x] -%#x(stack) -> reg%d",
  506. insn_offset, -offset, dst->reg1);
  507. } else {
  508. pr_debug_dtp("mov [%x] %#x(reg%d) -> reg%d",
  509. insn_offset, offset, sreg, dst->reg1);
  510. }
  511. pr_debug_type_name(&tsr->type, tsr->kind);
  512. }
  513. /* And then dereference the pointer if it has one */
  514. else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
  515. state->regs[sreg].kind == TSR_KIND_TYPE &&
  516. die_deref_ptr_type(&state->regs[sreg].type,
  517. src->offset + state->regs[sreg].offset, &type_die)) {
  518. tsr->type = type_die;
  519. tsr->kind = TSR_KIND_TYPE;
  520. tsr->offset = 0;
  521. tsr->ok = true;
  522. pr_debug_dtp("mov [%x] %#x(reg%d) -> reg%d",
  523. insn_offset, src->offset, sreg, dst->reg1);
  524. pr_debug_type_name(&tsr->type, tsr->kind);
  525. }
  526. /* Handle dereference of TSR_KIND_POINTER registers */
  527. else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
  528. state->regs[sreg].kind == TSR_KIND_POINTER &&
  529. die_get_member_type(&state->regs[sreg].type,
  530. src->offset + state->regs[sreg].offset, &type_die)) {
  531. tsr->type = state->regs[sreg].type;
  532. tsr->kind = TSR_KIND_TYPE;
  533. tsr->offset = src->offset + state->regs[sreg].offset;
  534. tsr->ok = true;
  535. pr_debug_dtp("mov [%x] addr %#x(reg%d) -> reg%d",
  536. insn_offset, src->offset, sreg, dst->reg1);
  537. pr_debug_type_name(&tsr->type, tsr->kind);
  538. }
  539. /* Or check if it's a global variable */
  540. else if (sreg == DWARF_REG_PC) {
  541. struct map_symbol *ms = dloc->ms;
  542. u64 ip = ms->sym->start + dl->al.offset;
  543. u64 addr;
  544. int offset;
  545. addr = annotate_calc_pcrel(ms, ip, src->offset, dl);
  546. if (!get_global_var_type(cu_die, dloc, ip, addr, &offset,
  547. &type_die) ||
  548. !die_get_member_type(&type_die, offset, &type_die)) {
  549. tsr->ok = false;
  550. return;
  551. }
  552. tsr->type = type_die;
  553. tsr->kind = TSR_KIND_TYPE;
  554. tsr->offset = 0;
  555. tsr->ok = true;
  556. pr_debug_dtp("mov [%x] global addr=%"PRIx64" -> reg%d",
  557. insn_offset, addr, dst->reg1);
  558. pr_debug_type_name(&type_die, tsr->kind);
  559. }
  560. /* And check percpu access with base register */
  561. else if (has_reg_type(state, sreg) &&
  562. state->regs[sreg].kind == TSR_KIND_PERCPU_BASE) {
  563. u64 ip = dloc->ms->sym->start + dl->al.offset;
  564. u64 var_addr = src->offset;
  565. int offset;
  566. if (src->multi_regs) {
  567. int reg2 = (sreg == src->reg1) ? src->reg2 : src->reg1;
  568. if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
  569. state->regs[reg2].kind == TSR_KIND_CONST)
  570. var_addr += state->regs[reg2].imm_value;
  571. }
  572. /*
  573. * In kernel, %gs points to a per-cpu region for the
  574. * current CPU. Access with a constant offset should
  575. * be treated as a global variable access.
  576. */
  577. if (get_global_var_type(cu_die, dloc, ip, var_addr,
  578. &offset, &type_die) &&
  579. die_get_member_type(&type_die, offset, &type_die)) {
  580. tsr->type = type_die;
  581. tsr->kind = TSR_KIND_TYPE;
  582. tsr->offset = 0;
  583. tsr->ok = true;
  584. if (src->multi_regs) {
  585. pr_debug_dtp("mov [%x] percpu %#x(reg%d,reg%d) -> reg%d",
  586. insn_offset, src->offset, src->reg1,
  587. src->reg2, dst->reg1);
  588. } else {
  589. pr_debug_dtp("mov [%x] percpu %#x(reg%d) -> reg%d",
  590. insn_offset, src->offset, sreg, dst->reg1);
  591. }
  592. pr_debug_type_name(&tsr->type, tsr->kind);
  593. } else {
  594. tsr->ok = false;
  595. }
  596. }
  597. /* And then dereference the calculated pointer if it has one */
  598. else if (has_reg_type(state, sreg) && state->regs[sreg].ok &&
  599. state->regs[sreg].kind == TSR_KIND_PERCPU_POINTER &&
  600. die_get_member_type(&state->regs[sreg].type,
  601. src->offset, &type_die)) {
  602. tsr->type = type_die;
  603. tsr->kind = TSR_KIND_TYPE;
  604. tsr->offset = 0;
  605. tsr->ok = true;
  606. pr_debug_dtp("mov [%x] pointer %#x(reg%d) -> reg%d",
  607. insn_offset, src->offset, sreg, dst->reg1);
  608. pr_debug_type_name(&tsr->type, tsr->kind);
  609. }
  610. /* Or try another register if any */
  611. else if (src->multi_regs && sreg == src->reg1 &&
  612. src->reg1 != src->reg2) {
  613. sreg = src->reg2;
  614. goto retry;
  615. }
  616. else {
  617. int offset;
  618. const char *var_name = NULL;
  619. /* it might be per-cpu variable (in kernel) access */
  620. if (src->offset < 0) {
  621. if (get_global_var_info(dloc, (s64)src->offset,
  622. &var_name, &offset) &&
  623. !strcmp(var_name, "__per_cpu_offset")) {
  624. tsr->kind = TSR_KIND_PERCPU_BASE;
  625. tsr->offset = 0;
  626. tsr->ok = true;
  627. pr_debug_dtp("mov [%x] percpu base reg%d\n",
  628. insn_offset, dst->reg1);
  629. return;
  630. }
  631. }
  632. tsr->ok = false;
  633. }
  634. }
  635. /* Case 3. register to memory transfers */
  636. if (!src->mem_ref && dst->mem_ref) {
  637. if (!has_reg_type(state, src->reg1) ||
  638. !state->regs[src->reg1].ok)
  639. return;
  640. /* Check stack variables with offset */
  641. if (dst->reg1 == fbreg || dst->reg1 == state->stack_reg) {
  642. struct type_state_stack *stack;
  643. int offset = dst->offset - fboff;
  644. tsr = &state->regs[src->reg1];
  645. stack = find_stack_state(state, offset);
  646. if (stack) {
  647. /*
  648. * The source register is likely to hold a type
  649. * of member if it's a compound type. Do not
  650. * update the stack variable type since we can
  651. * get the member type later by using the
  652. * die_get_member_type().
  653. */
  654. if (!stack->compound)
  655. set_stack_state(stack, offset, tsr->kind,
  656. &tsr->type, tsr->offset);
  657. } else {
  658. findnew_stack_state(state, offset, tsr->kind,
  659. &tsr->type, tsr->offset);
  660. }
  661. if (dst->reg1 == fbreg) {
  662. pr_debug_dtp("mov [%x] reg%d -> -%#x(stack)",
  663. insn_offset, src->reg1, -offset);
  664. } else {
  665. pr_debug_dtp("mov [%x] reg%d -> %#x(reg%d)",
  666. insn_offset, src->reg1, offset, dst->reg1);
  667. }
  668. if (tsr->offset != 0) {
  669. pr_debug_dtp(" reg%d offset %#x ->",
  670. src->reg1, tsr->offset);
  671. }
  672. pr_debug_type_name(&tsr->type, tsr->kind);
  673. }
  674. /*
  675. * Ignore other transfers since it'd set a value in a struct
  676. * and won't change the type.
  677. */
  678. }
  679. /* Case 4. memory to memory transfers (not handled for now) */
  680. }
  681. #endif
  682. const struct arch *arch__new_x86(const struct e_machine_and_e_flags *id, const char *cpuid)
  683. {
  684. struct arch *arch = zalloc(sizeof(*arch));
  685. if (!arch)
  686. return NULL;
  687. arch->name = "x86";
  688. arch->id = *id;
  689. if (cpuid) {
  690. if (x86__cpuid_parse(arch, cpuid)) {
  691. errno = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING;
  692. return NULL;
  693. }
  694. }
  695. arch->instructions = x86__instructions;
  696. arch->nr_instructions = ARRAY_SIZE(x86__instructions);
  697. #ifndef NDEBUG
  698. {
  699. static bool sorted_check;
  700. if (!sorted_check) {
  701. for (size_t i = 0; i < arch->nr_instructions - 1; i++) {
  702. assert(strcmp(arch->instructions[i].name,
  703. arch->instructions[i + 1].name) <= 0);
  704. }
  705. sorted_check = true;
  706. }
  707. }
  708. #endif
  709. arch->sorted_instructions = true;
  710. arch->objdump.comment_char = '#';
  711. arch->objdump.register_char = '%';
  712. arch->objdump.memory_ref_char = '(';
  713. arch->objdump.imm_char = '$';
  714. arch->insn_suffix = "bwlq";
  715. #ifdef HAVE_LIBDW_SUPPORT
  716. arch->update_insn_state = update_insn_state_x86;
  717. #endif
  718. return arch;
  719. }