2 * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
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!)
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.
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>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include "event-parse.h"
26 #include "trace-seq.h"
34 static void init_disassembler(void)
37 ud_set_syntax(&ud
, UD_SYN_ATT
);
40 static const char *disassemble(unsigned char *insn
, int len
, uint64_t rip
,
41 int cr0_pe
, int eflags_vm
,
58 ud_set_mode(&ud
, mode
);
59 ud_set_input_buffer(&ud
, insn
, len
);
61 return ud_insn_asm(&ud
);
66 static void init_disassembler(void)
70 static const char *disassemble(unsigned char *insn
, int len
, uint64_t rip
,
71 int cr0_pe
, int eflags_vm
,
74 static char out
[15*3+1];
77 for (i
= 0; i
< len
; ++i
)
78 sprintf(out
+ i
* 3, "%02x ", insn
[i
]);
86 #define VMX_EXIT_REASONS \
87 _ER(EXCEPTION_NMI, 0) \
88 _ER(EXTERNAL_INTERRUPT, 1) \
89 _ER(TRIPLE_FAULT, 2) \
90 _ER(PENDING_INTERRUPT, 7) \
111 _ER(IO_INSTRUCTION, 30) \
114 _ER(MWAIT_INSTRUCTION, 36) \
115 _ER(MONITOR_INSTRUCTION, 39) \
116 _ER(PAUSE_INSTRUCTION, 40) \
117 _ER(MCE_DURING_VMENTRY, 41) \
118 _ER(TPR_BELOW_THRESHOLD, 43) \
119 _ER(APIC_ACCESS, 44) \
120 _ER(EOI_INDUCED, 45) \
121 _ER(EPT_VIOLATION, 48) \
122 _ER(EPT_MISCONFIG, 49) \
124 _ER(PREEMPTION_TIMER, 52) \
127 _ER(APIC_WRITE, 56) \
133 #define SVM_EXIT_REASONS \
134 _ER(EXIT_READ_CR0, 0x000) \
135 _ER(EXIT_READ_CR3, 0x003) \
136 _ER(EXIT_READ_CR4, 0x004) \
137 _ER(EXIT_READ_CR8, 0x008) \
138 _ER(EXIT_WRITE_CR0, 0x010) \
139 _ER(EXIT_WRITE_CR3, 0x013) \
140 _ER(EXIT_WRITE_CR4, 0x014) \
141 _ER(EXIT_WRITE_CR8, 0x018) \
142 _ER(EXIT_READ_DR0, 0x020) \
143 _ER(EXIT_READ_DR1, 0x021) \
144 _ER(EXIT_READ_DR2, 0x022) \
145 _ER(EXIT_READ_DR3, 0x023) \
146 _ER(EXIT_READ_DR4, 0x024) \
147 _ER(EXIT_READ_DR5, 0x025) \
148 _ER(EXIT_READ_DR6, 0x026) \
149 _ER(EXIT_READ_DR7, 0x027) \
150 _ER(EXIT_WRITE_DR0, 0x030) \
151 _ER(EXIT_WRITE_DR1, 0x031) \
152 _ER(EXIT_WRITE_DR2, 0x032) \
153 _ER(EXIT_WRITE_DR3, 0x033) \
154 _ER(EXIT_WRITE_DR4, 0x034) \
155 _ER(EXIT_WRITE_DR5, 0x035) \
156 _ER(EXIT_WRITE_DR6, 0x036) \
157 _ER(EXIT_WRITE_DR7, 0x037) \
158 _ER(EXIT_EXCP_BASE, 0x040) \
159 _ER(EXIT_INTR, 0x060) \
160 _ER(EXIT_NMI, 0x061) \
161 _ER(EXIT_SMI, 0x062) \
162 _ER(EXIT_INIT, 0x063) \
163 _ER(EXIT_VINTR, 0x064) \
164 _ER(EXIT_CR0_SEL_WRITE, 0x065) \
165 _ER(EXIT_IDTR_READ, 0x066) \
166 _ER(EXIT_GDTR_READ, 0x067) \
167 _ER(EXIT_LDTR_READ, 0x068) \
168 _ER(EXIT_TR_READ, 0x069) \
169 _ER(EXIT_IDTR_WRITE, 0x06a) \
170 _ER(EXIT_GDTR_WRITE, 0x06b) \
171 _ER(EXIT_LDTR_WRITE, 0x06c) \
172 _ER(EXIT_TR_WRITE, 0x06d) \
173 _ER(EXIT_RDTSC, 0x06e) \
174 _ER(EXIT_RDPMC, 0x06f) \
175 _ER(EXIT_PUSHF, 0x070) \
176 _ER(EXIT_POPF, 0x071) \
177 _ER(EXIT_CPUID, 0x072) \
178 _ER(EXIT_RSM, 0x073) \
179 _ER(EXIT_IRET, 0x074) \
180 _ER(EXIT_SWINT, 0x075) \
181 _ER(EXIT_INVD, 0x076) \
182 _ER(EXIT_PAUSE, 0x077) \
183 _ER(EXIT_HLT, 0x078) \
184 _ER(EXIT_INVLPG, 0x079) \
185 _ER(EXIT_INVLPGA, 0x07a) \
186 _ER(EXIT_IOIO, 0x07b) \
187 _ER(EXIT_MSR, 0x07c) \
188 _ER(EXIT_TASK_SWITCH, 0x07d) \
189 _ER(EXIT_FERR_FREEZE, 0x07e) \
190 _ER(EXIT_SHUTDOWN, 0x07f) \
191 _ER(EXIT_VMRUN, 0x080) \
192 _ER(EXIT_VMMCALL, 0x081) \
193 _ER(EXIT_VMLOAD, 0x082) \
194 _ER(EXIT_VMSAVE, 0x083) \
195 _ER(EXIT_STGI, 0x084) \
196 _ER(EXIT_CLGI, 0x085) \
197 _ER(EXIT_SKINIT, 0x086) \
198 _ER(EXIT_RDTSCP, 0x087) \
199 _ER(EXIT_ICEBP, 0x088) \
200 _ER(EXIT_WBINVD, 0x089) \
201 _ER(EXIT_MONITOR, 0x08a) \
202 _ER(EXIT_MWAIT, 0x08b) \
203 _ER(EXIT_MWAIT_COND, 0x08c) \
204 _ER(EXIT_NPF, 0x400) \
207 #define _ER(reason, val) { #reason, val },
213 static struct str_values vmx_exit_reasons
[] = {
218 static struct str_values svm_exit_reasons
[] = {
223 static struct isa_exit_reasons
{
225 struct str_values
*strings
;
226 } isa_exit_reasons
[] = {
227 { .isa
= 1, .strings
= vmx_exit_reasons
},
228 { .isa
= 2, .strings
= svm_exit_reasons
},
232 static const char *find_exit_reason(unsigned isa
, int val
)
234 struct str_values
*strings
= NULL
;
237 for (i
= 0; isa_exit_reasons
[i
].strings
; ++i
)
238 if (isa_exit_reasons
[i
].isa
== isa
) {
239 strings
= isa_exit_reasons
[i
].strings
;
243 return "UNKNOWN-ISA";
244 for (i
= 0; strings
[i
].val
>= 0; i
++)
245 if (strings
[i
].val
== val
)
248 return strings
[i
].str
;
251 static int print_exit_reason(struct trace_seq
*s
, struct tep_record
*record
,
252 struct tep_event_format
*event
, const char *field
)
254 unsigned long long isa
;
255 unsigned long long val
;
258 if (tep_get_field_val(s
, event
, field
, record
, &val
, 1) < 0)
261 if (tep_get_field_val(s
, event
, "isa", record
, &isa
, 0) < 0)
264 reason
= find_exit_reason(isa
, val
);
266 trace_seq_printf(s
, "reason %s", reason
);
268 trace_seq_printf(s
, "reason UNKNOWN (%llu)", val
);
272 static int kvm_exit_handler(struct trace_seq
*s
, struct tep_record
*record
,
273 struct tep_event_format
*event
, void *context
)
275 unsigned long long info1
= 0, info2
= 0;
277 if (print_exit_reason(s
, record
, event
, "exit_reason") < 0)
280 tep_print_num_field(s
, " rip 0x%lx", event
, "guest_rip", record
, 1);
282 if (tep_get_field_val(s
, event
, "info1", record
, &info1
, 0) >= 0
283 && tep_get_field_val(s
, event
, "info2", record
, &info2
, 0) >= 0)
284 trace_seq_printf(s
, " info %llx %llx", info1
, info2
);
289 #define KVM_EMUL_INSN_F_CR0_PE (1 << 0)
290 #define KVM_EMUL_INSN_F_EFL_VM (1 << 1)
291 #define KVM_EMUL_INSN_F_CS_D (1 << 2)
292 #define KVM_EMUL_INSN_F_CS_L (1 << 3)
294 static int kvm_emulate_insn_handler(struct trace_seq
*s
,
295 struct tep_record
*record
,
296 struct tep_event_format
*event
, void *context
)
298 unsigned long long rip
, csbase
, len
, flags
, failed
;
303 if (tep_get_field_val(s
, event
, "rip", record
, &rip
, 1) < 0)
306 if (tep_get_field_val(s
, event
, "csbase", record
, &csbase
, 1) < 0)
309 if (tep_get_field_val(s
, event
, "len", record
, &len
, 1) < 0)
312 if (tep_get_field_val(s
, event
, "flags", record
, &flags
, 1) < 0)
315 if (tep_get_field_val(s
, event
, "failed", record
, &failed
, 1) < 0)
318 insn
= tep_get_field_raw(s
, event
, "insn", record
, &llen
, 1);
322 disasm
= disassemble(insn
, len
, rip
,
323 flags
& KVM_EMUL_INSN_F_CR0_PE
,
324 flags
& KVM_EMUL_INSN_F_EFL_VM
,
325 flags
& KVM_EMUL_INSN_F_CS_D
,
326 flags
& KVM_EMUL_INSN_F_CS_L
);
328 trace_seq_printf(s
, "%llx:%llx: %s%s", csbase
, rip
, disasm
,
329 failed
? " FAIL" : "");
334 static int kvm_nested_vmexit_inject_handler(struct trace_seq
*s
, struct tep_record
*record
,
335 struct tep_event_format
*event
, void *context
)
337 if (print_exit_reason(s
, record
, event
, "exit_code") < 0)
340 tep_print_num_field(s
, " info1 %llx", event
, "exit_info1", record
, 1);
341 tep_print_num_field(s
, " info2 %llx", event
, "exit_info2", record
, 1);
342 tep_print_num_field(s
, " int_info %llx", event
, "exit_int_info", record
, 1);
343 tep_print_num_field(s
, " int_info_err %llx", event
, "exit_int_info_err", record
, 1);
348 static int kvm_nested_vmexit_handler(struct trace_seq
*s
, struct tep_record
*record
,
349 struct tep_event_format
*event
, void *context
)
351 tep_print_num_field(s
, "rip %llx ", event
, "rip", record
, 1);
353 return kvm_nested_vmexit_inject_handler(s
, record
, event
, context
);
356 union kvm_mmu_page_role
{
367 unsigned smep_and_not_wp
:1;
368 unsigned smap_and_not_wp
:1;
369 unsigned pad_for_nice_hex_output
:8;
374 static int kvm_mmu_print_role(struct trace_seq
*s
, struct tep_record
*record
,
375 struct tep_event_format
*event
, void *context
)
377 unsigned long long val
;
378 static const char *access_str
[] = {
379 "---", "--x", "w--", "w-x", "-u-", "-ux", "wu-", "wux"
381 union kvm_mmu_page_role role
;
383 if (tep_get_field_val(s
, event
, "role", record
, &val
, 1) < 0)
386 role
.word
= (int)val
;
389 * We can only use the structure if file is of the same
392 if (tep_is_file_bigendian(event
->pevent
) ==
393 tep_is_host_bigendian(event
->pevent
)) {
395 trace_seq_printf(s
, "%u q%u%s %s%s %spae %snxe %swp%s%s%s",
398 role
.direct
? " direct" : "",
399 access_str
[role
.access
],
400 role
.invalid
? " invalid" : "",
401 role
.cr4_pae
? "" : "!",
403 role
.cr0_wp
? "" : "!",
404 role
.smep_and_not_wp
? " smep" : "",
405 role
.smap_and_not_wp
? " smap" : "",
406 role
.smm
? " smm" : "");
408 trace_seq_printf(s
, "WORD: %08x", role
.word
);
410 tep_print_num_field(s
, " root %u ", event
,
411 "root_count", record
, 1);
413 if (tep_get_field_val(s
, event
, "unsync", record
, &val
, 1) < 0)
416 trace_seq_printf(s
, "%s%c", val
? "unsync" : "sync", 0);
420 static int kvm_mmu_get_page_handler(struct trace_seq
*s
,
421 struct tep_record
*record
,
422 struct tep_event_format
*event
, void *context
)
424 unsigned long long val
;
426 if (tep_get_field_val(s
, event
, "created", record
, &val
, 1) < 0)
429 trace_seq_printf(s
, "%s ", val
? "new" : "existing");
431 if (tep_get_field_val(s
, event
, "gfn", record
, &val
, 1) < 0)
434 trace_seq_printf(s
, "sp gfn %llx ", val
);
435 return kvm_mmu_print_role(s
, record
, event
, context
);
438 #define PT_WRITABLE_SHIFT 1
439 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
441 static unsigned long long
442 process_is_writable_pte(struct trace_seq
*s
, unsigned long long *args
)
444 unsigned long pte
= args
[0];
445 return pte
& PT_WRITABLE_MASK
;
448 int TEP_PLUGIN_LOADER(struct tep_handle
*pevent
)
452 tep_register_event_handler(pevent
, -1, "kvm", "kvm_exit",
453 kvm_exit_handler
, NULL
);
455 tep_register_event_handler(pevent
, -1, "kvm", "kvm_emulate_insn",
456 kvm_emulate_insn_handler
, NULL
);
458 tep_register_event_handler(pevent
, -1, "kvm", "kvm_nested_vmexit",
459 kvm_nested_vmexit_handler
, NULL
);
461 tep_register_event_handler(pevent
, -1, "kvm", "kvm_nested_vmexit_inject",
462 kvm_nested_vmexit_inject_handler
, NULL
);
464 tep_register_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_get_page",
465 kvm_mmu_get_page_handler
, NULL
);
467 tep_register_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_sync_page",
468 kvm_mmu_print_role
, NULL
);
470 tep_register_event_handler(pevent
, -1,
471 "kvmmmu", "kvm_mmu_unsync_page",
472 kvm_mmu_print_role
, NULL
);
474 tep_register_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_zap_page",
475 kvm_mmu_print_role
, NULL
);
477 tep_register_event_handler(pevent
, -1, "kvmmmu",
478 "kvm_mmu_prepare_zap_page", kvm_mmu_print_role
,
481 tep_register_print_function(pevent
,
482 process_is_writable_pte
,
490 void TEP_PLUGIN_UNLOADER(struct tep_handle
*pevent
)
492 tep_unregister_event_handler(pevent
, -1, "kvm", "kvm_exit",
493 kvm_exit_handler
, NULL
);
495 tep_unregister_event_handler(pevent
, -1, "kvm", "kvm_emulate_insn",
496 kvm_emulate_insn_handler
, NULL
);
498 tep_unregister_event_handler(pevent
, -1, "kvm", "kvm_nested_vmexit",
499 kvm_nested_vmexit_handler
, NULL
);
501 tep_unregister_event_handler(pevent
, -1, "kvm", "kvm_nested_vmexit_inject",
502 kvm_nested_vmexit_inject_handler
, NULL
);
504 tep_unregister_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_get_page",
505 kvm_mmu_get_page_handler
, NULL
);
507 tep_unregister_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_sync_page",
508 kvm_mmu_print_role
, NULL
);
510 tep_unregister_event_handler(pevent
, -1,
511 "kvmmmu", "kvm_mmu_unsync_page",
512 kvm_mmu_print_role
, NULL
);
514 tep_unregister_event_handler(pevent
, -1, "kvmmmu", "kvm_mmu_zap_page",
515 kvm_mmu_print_role
, NULL
);
517 tep_unregister_event_handler(pevent
, -1, "kvmmmu",
518 "kvm_mmu_prepare_zap_page", kvm_mmu_print_role
,
521 tep_unregister_print_function(pevent
, process_is_writable_pte
,