plugin_kvm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation;
  8. * version 2.1 of the License (not later!)
  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 Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this program; if not, see <http://www.gnu.org/licenses>
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include "event-parse.h"
  25. #ifdef HAVE_UDIS86
  26. #include <udis86.h>
  27. static ud_t ud;
  28. static void init_disassembler(void)
  29. {
  30. ud_init(&ud);
  31. ud_set_syntax(&ud, UD_SYN_ATT);
  32. }
  33. static const char *disassemble(unsigned char *insn, int len, uint64_t rip,
  34. int cr0_pe, int eflags_vm,
  35. int cs_d, int cs_l)
  36. {
  37. int mode;
  38. if (!cr0_pe)
  39. mode = 16;
  40. else if (eflags_vm)
  41. mode = 16;
  42. else if (cs_l)
  43. mode = 64;
  44. else if (cs_d)
  45. mode = 32;
  46. else
  47. mode = 16;
  48. ud_set_pc(&ud, rip);
  49. ud_set_mode(&ud, mode);
  50. ud_set_input_buffer(&ud, insn, len);
  51. ud_disassemble(&ud);
  52. return ud_insn_asm(&ud);
  53. }
  54. #else
  55. static void init_disassembler(void)
  56. {
  57. }
  58. static const char *disassemble(unsigned char *insn, int len, uint64_t rip,
  59. int cr0_pe, int eflags_vm,
  60. int cs_d, int cs_l)
  61. {
  62. static char out[15*3+1];
  63. int i;
  64. for (i = 0; i < len; ++i)
  65. sprintf(out + i * 3, "%02x ", insn[i]);
  66. out[len*3-1] = '\0';
  67. return out;
  68. }
  69. #endif
  70. #define VMX_EXIT_REASONS \
  71. _ER(EXCEPTION_NMI, 0) \
  72. _ER(EXTERNAL_INTERRUPT, 1) \
  73. _ER(TRIPLE_FAULT, 2) \
  74. _ER(PENDING_INTERRUPT, 7) \
  75. _ER(NMI_WINDOW, 8) \
  76. _ER(TASK_SWITCH, 9) \
  77. _ER(CPUID, 10) \
  78. _ER(HLT, 12) \
  79. _ER(INVD, 13) \
  80. _ER(INVLPG, 14) \
  81. _ER(RDPMC, 15) \
  82. _ER(RDTSC, 16) \
  83. _ER(VMCALL, 18) \
  84. _ER(VMCLEAR, 19) \
  85. _ER(VMLAUNCH, 20) \
  86. _ER(VMPTRLD, 21) \
  87. _ER(VMPTRST, 22) \
  88. _ER(VMREAD, 23) \
  89. _ER(VMRESUME, 24) \
  90. _ER(VMWRITE, 25) \
  91. _ER(VMOFF, 26) \
  92. _ER(VMON, 27) \
  93. _ER(CR_ACCESS, 28) \
  94. _ER(DR_ACCESS, 29) \
  95. _ER(IO_INSTRUCTION, 30) \
  96. _ER(MSR_READ, 31) \
  97. _ER(MSR_WRITE, 32) \
  98. _ER(MWAIT_INSTRUCTION, 36) \
  99. _ER(MONITOR_INSTRUCTION, 39) \
  100. _ER(PAUSE_INSTRUCTION, 40) \
  101. _ER(MCE_DURING_VMENTRY, 41) \
  102. _ER(TPR_BELOW_THRESHOLD, 43) \
  103. _ER(APIC_ACCESS, 44) \
  104. _ER(EOI_INDUCED, 45) \
  105. _ER(EPT_VIOLATION, 48) \
  106. _ER(EPT_MISCONFIG, 49) \
  107. _ER(INVEPT, 50) \
  108. _ER(PREEMPTION_TIMER, 52) \
  109. _ER(WBINVD, 54) \
  110. _ER(XSETBV, 55) \
  111. _ER(APIC_WRITE, 56) \
  112. _ER(INVPCID, 58) \
  113. _ER(PML_FULL, 62) \
  114. _ER(XSAVES, 63) \
  115. _ER(XRSTORS, 64)
  116. #define SVM_EXIT_REASONS \
  117. _ER(EXIT_READ_CR0, 0x000) \
  118. _ER(EXIT_READ_CR3, 0x003) \
  119. _ER(EXIT_READ_CR4, 0x004) \
  120. _ER(EXIT_READ_CR8, 0x008) \
  121. _ER(EXIT_WRITE_CR0, 0x010) \
  122. _ER(EXIT_WRITE_CR3, 0x013) \
  123. _ER(EXIT_WRITE_CR4, 0x014) \
  124. _ER(EXIT_WRITE_CR8, 0x018) \
  125. _ER(EXIT_READ_DR0, 0x020) \
  126. _ER(EXIT_READ_DR1, 0x021) \
  127. _ER(EXIT_READ_DR2, 0x022) \
  128. _ER(EXIT_READ_DR3, 0x023) \
  129. _ER(EXIT_READ_DR4, 0x024) \
  130. _ER(EXIT_READ_DR5, 0x025) \
  131. _ER(EXIT_READ_DR6, 0x026) \
  132. _ER(EXIT_READ_DR7, 0x027) \
  133. _ER(EXIT_WRITE_DR0, 0x030) \
  134. _ER(EXIT_WRITE_DR1, 0x031) \
  135. _ER(EXIT_WRITE_DR2, 0x032) \
  136. _ER(EXIT_WRITE_DR3, 0x033) \
  137. _ER(EXIT_WRITE_DR4, 0x034) \
  138. _ER(EXIT_WRITE_DR5, 0x035) \
  139. _ER(EXIT_WRITE_DR6, 0x036) \
  140. _ER(EXIT_WRITE_DR7, 0x037) \
  141. _ER(EXIT_EXCP_BASE, 0x040) \
  142. _ER(EXIT_INTR, 0x060) \
  143. _ER(EXIT_NMI, 0x061) \
  144. _ER(EXIT_SMI, 0x062) \
  145. _ER(EXIT_INIT, 0x063) \
  146. _ER(EXIT_VINTR, 0x064) \
  147. _ER(EXIT_CR0_SEL_WRITE, 0x065) \
  148. _ER(EXIT_IDTR_READ, 0x066) \
  149. _ER(EXIT_GDTR_READ, 0x067) \
  150. _ER(EXIT_LDTR_READ, 0x068) \
  151. _ER(EXIT_TR_READ, 0x069) \
  152. _ER(EXIT_IDTR_WRITE, 0x06a) \
  153. _ER(EXIT_GDTR_WRITE, 0x06b) \
  154. _ER(EXIT_LDTR_WRITE, 0x06c) \
  155. _ER(EXIT_TR_WRITE, 0x06d) \
  156. _ER(EXIT_RDTSC, 0x06e) \
  157. _ER(EXIT_RDPMC, 0x06f) \
  158. _ER(EXIT_PUSHF, 0x070) \
  159. _ER(EXIT_POPF, 0x071) \
  160. _ER(EXIT_CPUID, 0x072) \
  161. _ER(EXIT_RSM, 0x073) \
  162. _ER(EXIT_IRET, 0x074) \
  163. _ER(EXIT_SWINT, 0x075) \
  164. _ER(EXIT_INVD, 0x076) \
  165. _ER(EXIT_PAUSE, 0x077) \
  166. _ER(EXIT_HLT, 0x078) \
  167. _ER(EXIT_INVLPG, 0x079) \
  168. _ER(EXIT_INVLPGA, 0x07a) \
  169. _ER(EXIT_IOIO, 0x07b) \
  170. _ER(EXIT_MSR, 0x07c) \
  171. _ER(EXIT_TASK_SWITCH, 0x07d) \
  172. _ER(EXIT_FERR_FREEZE, 0x07e) \
  173. _ER(EXIT_SHUTDOWN, 0x07f) \
  174. _ER(EXIT_VMRUN, 0x080) \
  175. _ER(EXIT_VMMCALL, 0x081) \
  176. _ER(EXIT_VMLOAD, 0x082) \
  177. _ER(EXIT_VMSAVE, 0x083) \
  178. _ER(EXIT_STGI, 0x084) \
  179. _ER(EXIT_CLGI, 0x085) \
  180. _ER(EXIT_SKINIT, 0x086) \
  181. _ER(EXIT_RDTSCP, 0x087) \
  182. _ER(EXIT_ICEBP, 0x088) \
  183. _ER(EXIT_WBINVD, 0x089) \
  184. _ER(EXIT_MONITOR, 0x08a) \
  185. _ER(EXIT_MWAIT, 0x08b) \
  186. _ER(EXIT_MWAIT_COND, 0x08c) \
  187. _ER(EXIT_NPF, 0x400) \
  188. _ER(EXIT_ERR, -1)
  189. #define _ER(reason, val) { #reason, val },
  190. struct str_values {
  191. const char *str;
  192. int val;
  193. };
  194. static struct str_values vmx_exit_reasons[] = {
  195. VMX_EXIT_REASONS
  196. { NULL, -1}
  197. };
  198. static struct str_values svm_exit_reasons[] = {
  199. SVM_EXIT_REASONS
  200. { NULL, -1}
  201. };
  202. static struct isa_exit_reasons {
  203. unsigned isa;
  204. struct str_values *strings;
  205. } isa_exit_reasons[] = {
  206. { .isa = 1, .strings = vmx_exit_reasons },
  207. { .isa = 2, .strings = svm_exit_reasons },
  208. { }
  209. };
  210. static const char *find_exit_reason(unsigned isa, int val)
  211. {
  212. struct str_values *strings = NULL;
  213. int i;
  214. for (i = 0; isa_exit_reasons[i].strings; ++i)
  215. if (isa_exit_reasons[i].isa == isa) {
  216. strings = isa_exit_reasons[i].strings;
  217. break;
  218. }
  219. if (!strings)
  220. return "UNKNOWN-ISA";
  221. for (i = 0; strings[i].val >= 0; i++)
  222. if (strings[i].val == val)
  223. break;
  224. return strings[i].str;
  225. }
  226. static int print_exit_reason(struct trace_seq *s, struct pevent_record *record,
  227. struct event_format *event, const char *field)
  228. {
  229. unsigned long long isa;
  230. unsigned long long val;
  231. const char *reason;
  232. if (pevent_get_field_val(s, event, field, record, &val, 1) < 0)
  233. return -1;
  234. if (pevent_get_field_val(s, event, "isa", record, &isa, 0) < 0)
  235. isa = 1;
  236. reason = find_exit_reason(isa, val);
  237. if (reason)
  238. trace_seq_printf(s, "reason %s", reason);
  239. else
  240. trace_seq_printf(s, "reason UNKNOWN (%llu)", val);
  241. return 0;
  242. }
  243. static int kvm_exit_handler(struct trace_seq *s, struct pevent_record *record,
  244. struct event_format *event, void *context)
  245. {
  246. unsigned long long info1 = 0, info2 = 0;
  247. if (print_exit_reason(s, record, event, "exit_reason") < 0)
  248. return -1;
  249. pevent_print_num_field(s, " rip 0x%lx", event, "guest_rip", record, 1);
  250. if (pevent_get_field_val(s, event, "info1", record, &info1, 0) >= 0
  251. && pevent_get_field_val(s, event, "info2", record, &info2, 0) >= 0)
  252. trace_seq_printf(s, " info %llx %llx", info1, info2);
  253. return 0;
  254. }
  255. #define KVM_EMUL_INSN_F_CR0_PE (1 << 0)
  256. #define KVM_EMUL_INSN_F_EFL_VM (1 << 1)
  257. #define KVM_EMUL_INSN_F_CS_D (1 << 2)
  258. #define KVM_EMUL_INSN_F_CS_L (1 << 3)
  259. static int kvm_emulate_insn_handler(struct trace_seq *s,
  260. struct pevent_record *record,
  261. struct event_format *event, void *context)
  262. {
  263. unsigned long long rip, csbase, len, flags, failed;
  264. int llen;
  265. uint8_t *insn;
  266. const char *disasm;
  267. if (pevent_get_field_val(s, event, "rip", record, &rip, 1) < 0)
  268. return -1;
  269. if (pevent_get_field_val(s, event, "csbase", record, &csbase, 1) < 0)
  270. return -1;
  271. if (pevent_get_field_val(s, event, "len", record, &len, 1) < 0)
  272. return -1;
  273. if (pevent_get_field_val(s, event, "flags", record, &flags, 1) < 0)
  274. return -1;
  275. if (pevent_get_field_val(s, event, "failed", record, &failed, 1) < 0)
  276. return -1;
  277. insn = pevent_get_field_raw(s, event, "insn", record, &llen, 1);
  278. if (!insn)
  279. return -1;
  280. disasm = disassemble(insn, len, rip,
  281. flags & KVM_EMUL_INSN_F_CR0_PE,
  282. flags & KVM_EMUL_INSN_F_EFL_VM,
  283. flags & KVM_EMUL_INSN_F_CS_D,
  284. flags & KVM_EMUL_INSN_F_CS_L);
  285. trace_seq_printf(s, "%llx:%llx: %s%s", csbase, rip, disasm,
  286. failed ? " FAIL" : "");
  287. return 0;
  288. }
  289. static int kvm_nested_vmexit_inject_handler(struct trace_seq *s, struct pevent_record *record,
  290. struct event_format *event, void *context)
  291. {
  292. if (print_exit_reason(s, record, event, "exit_code") < 0)
  293. return -1;
  294. pevent_print_num_field(s, " info1 %llx", event, "exit_info1", record, 1);
  295. pevent_print_num_field(s, " info2 %llx", event, "exit_info2", record, 1);
  296. pevent_print_num_field(s, " int_info %llx", event, "exit_int_info", record, 1);
  297. pevent_print_num_field(s, " int_info_err %llx", event, "exit_int_info_err", record, 1);
  298. return 0;
  299. }
  300. static int kvm_nested_vmexit_handler(struct trace_seq *s, struct pevent_record *record,
  301. struct event_format *event, void *context)
  302. {
  303. pevent_print_num_field(s, "rip %llx ", event, "rip", record, 1);
  304. return kvm_nested_vmexit_inject_handler(s, record, event, context);
  305. }
  306. union kvm_mmu_page_role {
  307. unsigned word;
  308. struct {
  309. unsigned level:4;
  310. unsigned cr4_pae:1;
  311. unsigned quadrant:2;
  312. unsigned direct:1;
  313. unsigned access:3;
  314. unsigned invalid:1;
  315. unsigned nxe:1;
  316. unsigned cr0_wp:1;
  317. unsigned smep_and_not_wp:1;
  318. unsigned smap_and_not_wp:1;
  319. unsigned pad_for_nice_hex_output:8;
  320. unsigned smm:8;
  321. };
  322. };
  323. static int kvm_mmu_print_role(struct trace_seq *s, struct pevent_record *record,
  324. struct event_format *event, void *context)
  325. {
  326. unsigned long long val;
  327. static const char *access_str[] = {
  328. "---", "--x", "w--", "w-x", "-u-", "-ux", "wu-", "wux"
  329. };
  330. union kvm_mmu_page_role role;
  331. if (pevent_get_field_val(s, event, "role", record, &val, 1) < 0)
  332. return -1;
  333. role.word = (int)val;
  334. /*
  335. * We can only use the structure if file is of the same
  336. * endianess.
  337. */
  338. if (pevent_is_file_bigendian(event->pevent) ==
  339. pevent_is_host_bigendian(event->pevent)) {
  340. trace_seq_printf(s, "%u q%u%s %s%s %spae %snxe %swp%s%s%s",
  341. role.level,
  342. role.quadrant,
  343. role.direct ? " direct" : "",
  344. access_str[role.access],
  345. role.invalid ? " invalid" : "",
  346. role.cr4_pae ? "" : "!",
  347. role.nxe ? "" : "!",
  348. role.cr0_wp ? "" : "!",
  349. role.smep_and_not_wp ? " smep" : "",
  350. role.smap_and_not_wp ? " smap" : "",
  351. role.smm ? " smm" : "");
  352. } else
  353. trace_seq_printf(s, "WORD: %08x", role.word);
  354. pevent_print_num_field(s, " root %u ", event,
  355. "root_count", record, 1);
  356. if (pevent_get_field_val(s, event, "unsync", record, &val, 1) < 0)
  357. return -1;
  358. trace_seq_printf(s, "%s%c", val ? "unsync" : "sync", 0);
  359. return 0;
  360. }
  361. static int kvm_mmu_get_page_handler(struct trace_seq *s,
  362. struct pevent_record *record,
  363. struct event_format *event, void *context)
  364. {
  365. unsigned long long val;
  366. if (pevent_get_field_val(s, event, "created", record, &val, 1) < 0)
  367. return -1;
  368. trace_seq_printf(s, "%s ", val ? "new" : "existing");
  369. if (pevent_get_field_val(s, event, "gfn", record, &val, 1) < 0)
  370. return -1;
  371. trace_seq_printf(s, "sp gfn %llx ", val);
  372. return kvm_mmu_print_role(s, record, event, context);
  373. }
  374. #define PT_WRITABLE_SHIFT 1
  375. #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
  376. static unsigned long long
  377. process_is_writable_pte(struct trace_seq *s, unsigned long long *args)
  378. {
  379. unsigned long pte = args[0];
  380. return pte & PT_WRITABLE_MASK;
  381. }
  382. int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
  383. {
  384. init_disassembler();
  385. pevent_register_event_handler(pevent, -1, "kvm", "kvm_exit",
  386. kvm_exit_handler, NULL);
  387. pevent_register_event_handler(pevent, -1, "kvm", "kvm_emulate_insn",
  388. kvm_emulate_insn_handler, NULL);
  389. pevent_register_event_handler(pevent, -1, "kvm", "kvm_nested_vmexit",
  390. kvm_nested_vmexit_handler, NULL);
  391. pevent_register_event_handler(pevent, -1, "kvm", "kvm_nested_vmexit_inject",
  392. kvm_nested_vmexit_inject_handler, NULL);
  393. pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_get_page",
  394. kvm_mmu_get_page_handler, NULL);
  395. pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_sync_page",
  396. kvm_mmu_print_role, NULL);
  397. pevent_register_event_handler(pevent, -1,
  398. "kvmmmu", "kvm_mmu_unsync_page",
  399. kvm_mmu_print_role, NULL);
  400. pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_zap_page",
  401. kvm_mmu_print_role, NULL);
  402. pevent_register_event_handler(pevent, -1, "kvmmmu",
  403. "kvm_mmu_prepare_zap_page", kvm_mmu_print_role,
  404. NULL);
  405. pevent_register_print_function(pevent,
  406. process_is_writable_pte,
  407. PEVENT_FUNC_ARG_INT,
  408. "is_writable_pte",
  409. PEVENT_FUNC_ARG_LONG,
  410. PEVENT_FUNC_ARG_VOID);
  411. return 0;
  412. }
  413. void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
  414. {
  415. pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_exit",
  416. kvm_exit_handler, NULL);
  417. pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_emulate_insn",
  418. kvm_emulate_insn_handler, NULL);
  419. pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_nested_vmexit",
  420. kvm_nested_vmexit_handler, NULL);
  421. pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_nested_vmexit_inject",
  422. kvm_nested_vmexit_inject_handler, NULL);
  423. pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_get_page",
  424. kvm_mmu_get_page_handler, NULL);
  425. pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_sync_page",
  426. kvm_mmu_print_role, NULL);
  427. pevent_unregister_event_handler(pevent, -1,
  428. "kvmmmu", "kvm_mmu_unsync_page",
  429. kvm_mmu_print_role, NULL);
  430. pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_zap_page",
  431. kvm_mmu_print_role, NULL);
  432. pevent_unregister_event_handler(pevent, -1, "kvmmmu",
  433. "kvm_mmu_prepare_zap_page", kvm_mmu_print_role,
  434. NULL);
  435. pevent_unregister_print_function(pevent, process_is_writable_pte,
  436. "is_writable_pte");
  437. }