Debian Codegraph @

tools/xentrace/xenalyze.c

raw
1 /*
2 * xenalyze.c: Analyzing xentrace output
3 *
4 * Written by George Dunlap.
5 *
6 * Copyright (c) 2006-2007, XenSource Inc.
7 * Copyright (c) 2007-2008, Citrix Systems R&D Ltd, UK
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; If not, see <http://www.gnu.org/licenses/>.
20 */
21 #define _XOPEN_SOURCE 600
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <argp.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <xen/trace.h>
34 #include "analyze.h"
35 #include "mread.h"
36 #include "pv.h"
37 #include <errno.h>
38 #include <strings.h>
39 #include <string.h>
40 #include <assert.h>
41
42 struct mread_ctrl;
43
44
45 #define DEFAULT_CPU_HZ 2400000000LL
46 #define QHZ_FROM_HZ(_hz) (((_hz) << 10)/ 1000000000)
47
48 #define ADDR_SPACE_BITS 48
49 #define DEFAULT_SAMPLE_SIZE 1024
50 #define DEFAULT_SAMPLE_MAX 1024*1024*32
51 #define DEFAULT_INTERVAL_LENGTH 1000
52
53 struct array_struct {
54 unsigned long long *values;
55 int count;
56 };
57
58 #define warn_once(_x...) \
59 do { \
60 static int _w=1; \
61 if ( _w ) { \
62 _w=0; \
63 fprintf(warn, ##_x); \
64 } \
65 } while(0) \
66
67 /* -- Global variables -- */
68 struct {
69 int fd;
70 struct mread_ctrl *mh;
71 struct symbol_struct * symbols;
72 char * symbol_file;
73 char * trace_file;
74 int output_defined;
75 off_t file_size;
76 struct {
77 off_t update_offset;
78 int pipe[2];
79 FILE* out;
80 int pid;
81 } progress;
82 } G = {
83 .fd=-1,
84 .symbols = NULL,
85 .symbol_file = NULL,
86 .trace_file = NULL,
87 .output_defined = 0,
88 .file_size = 0,
89 .progress = { .update_offset = 0 },
90 };
91
92 /*
93 Kinds of errors:
94 Unexpected values
95 - RIP with information in high bits (not all 0 or 1)
96 - exit reason too high
97 Unexpected record layout
98 - x64 bit set in PIO,PV_PTWR_EMULATION_PAE,
99 - Unknown minor type (PV_PTWR_EMULATION, RUNSTATE_CHANGE
100 - Wrong record size
101 - More than one bit set in evt.main field
102 Unexpected sequences
103 - wake tsc tracking
104 - TSC dependency loop
105 - Mismatch between non-running old event states
106 - Runstate continue while running on another pcpu
107 - lost_record_end seen in non-lost pcpu
108 - Unexpected non-CPU_CHANGE record during new_pcpu scan
109 - record tsc < interval start tsc
110 - lost_record tsc !> order tsc
111 Limited resources
112 - interrupt interval slots
113 - record cpu > MAX_CPUS
114 Algorithm asserts
115 - Duplicate CR3/domain values
116 - Logic holes
117 - domain runstates
118 - runstate / tsc skew
119 - vcpu_{prev,next}_update p->current{==,!=}null
120 - vcpu start conditions
121 - lost_cpu count higher than # of seen cpus / < 0
122 - lost cpu has non-null p->current
123 Symbol file
124 -file doesn't open
125 -file not ordered
126 System
127 - short read
128 - malloc failed
129 Args
130 - Invalid cpu_hz value / suffix
131 - No trace file
132 - Can't open trace file
133 */
134 enum error_level {
135 ERR_NONE=0,
136 ERR_STRICT, /* Be unreasonably picky */
137 ERR_WARN, /* Something midly unexpected */
138 ERR_SANITY, /* Sanity checks: RIP with info in high bits */
139 ERR_RECORD, /* Something that keeps you from processing the record */
140 ERR_FILE, /* Probably caused by a corrupt file */
141 ERR_LIMIT, /* Exceeded limits; data will be lost */
142 ERR_MAX_TOLERABLE=ERR_LIMIT,
143 /* -- Unrecoverable past this point -- */
144 ERR_ASSERT, /* Algoritm assert */
145 ERR_SYSTEM, /* System error: cannot allocate memory, short read, &c */
146 };
147
148 int verbosity = 5;
149
150 struct {
151 unsigned
152 scatterplot_interrupt_eip:1,
153 scatterplot_unpin_promote:1,
154 scatterplot_cr3_switch:1,
155 scatterplot_wake_to_halt:1,
156 scatterplot_io:1,
157 scatterplot_vmexit_eip:1,
158 scatterplot_runstate:1,
159 scatterplot_runstate_time:1,
160 scatterplot_pcpu:1,
161 scatterplot_extint_cycles:1,
162 scatterplot_rdtsc:1,
163 scatterplot_irq:1,
164 histogram_interrupt_eip:1,
165 interval_mode:1,
166 dump_all:1,
167 dump_raw_process:1,
168 dump_raw_reads:1,
169 dump_no_processing:1,
170 dump_ipi_latency:1,
171 dump_trace_volume_on_lost_record:1,
172 dump_show_power_states:1,
173 with_cr3_enumeration:1,
174 with_pio_enumeration:1,
175 with_mmio_enumeration:1,
176 with_interrupt_eip_enumeration:1,
177 show_default_domain_summary:1,
178 mmio_enumeration_skip_vga:1,
179 progress:1,
180 svm_mode:1,
181 summary:1,
182 report_pcpu:1,
183 tsc_loop_fatal:1,
184 summary_info;
185 long long cpu_qhz, cpu_hz;
186 int scatterplot_interrupt_vector;
187 int scatterplot_extint_cycles_vector;
188 int scatterplot_io_port;
189 int histogram_interrupt_vector;
190 unsigned long long histogram_interrupt_increment;
191 int interrupt_eip_enumeration_vector;
192 int default_guest_paging_levels;
193 int sample_size, sample_max;
194 enum error_level tolerance; /* Tolerate up to this level of error */
195 struct {
196 tsc_t cycles;
197 /* Used if interval is specified in seconds to delay calculating
198 * time_interval until all arguments have been processed (specifically,
199 * cpu_hz). */
200 unsigned msec;
201 enum {
202 INTERVAL_CR3_SCHEDULE_TIME,
203 INTERVAL_CR3_SCHEDULE_ORDERED,
204 INTERVAL_CR3_SHORT_SUMMARY,
205 INTERVAL_DOMAIN_TOTAL_TIME,
206 INTERVAL_DOMAIN_SHORT_SUMMARY,
207 INTERVAL_DOMAIN_GUEST_INTERRUPT,
208 INTERVAL_DOMAIN_GRANT_MAPS
209 } output;
210 enum {
211 INTERVAL_MODE_CUSTOM,
212 INTERVAL_MODE_ARRAY,
213 INTERVAL_MODE_LIST
214 } mode;
215 enum {
216 INTERVAL_CHECK_NONE,
217 INTERVAL_CHECK_CR3,
218 INTERVAL_CHECK_DOMAIN
219 } check;
220 /* Options for specific interval output types */
221 union {
222 struct array_struct array;
223 };
224 int count;
225 } interval;
226 } opt = {
227 .scatterplot_interrupt_eip=0,
228 .scatterplot_unpin_promote=0,
229 .scatterplot_cr3_switch=0,
230 .scatterplot_wake_to_halt=0,
231 .scatterplot_vmexit_eip=0,
232 .scatterplot_runstate=0,
233 .scatterplot_runstate_time=0,
234 .scatterplot_pcpu=0,
235 .scatterplot_extint_cycles=0,
236 .scatterplot_rdtsc=0,
237 .scatterplot_irq=0,
238 .histogram_interrupt_eip=0,
239 .dump_all = 0,
240 .dump_raw_process = 0,
241 .dump_raw_reads = 0,
242 .dump_no_processing = 0,
243 .dump_ipi_latency = 0,
244 .dump_trace_volume_on_lost_record = 0,
245 .dump_show_power_states = 0,
246 .with_cr3_enumeration = 0,
247 .with_pio_enumeration = 1,
248 .with_mmio_enumeration = 0,
249 .with_interrupt_eip_enumeration = 0,
250 .show_default_domain_summary = 0,
251 .mmio_enumeration_skip_vga = 1,
252 .progress = 0,
253 .svm_mode = 0,
254 .summary = 0,
255 .report_pcpu = 0,
256 .tsc_loop_fatal = 0,
257 .cpu_hz = DEFAULT_CPU_HZ,
258 /* Pre-calculate a multiplier that makes the rest of the
259 * calculations easier */
260 .cpu_qhz = QHZ_FROM_HZ(DEFAULT_CPU_HZ),
261 .default_guest_paging_levels = 2,
262 .sample_size = DEFAULT_SAMPLE_SIZE,
263 .sample_max = DEFAULT_SAMPLE_MAX,
264 .tolerance = ERR_SANITY,
265 .interval = { .msec = DEFAULT_INTERVAL_LENGTH },
266 };
267
268 FILE *warn = NULL;
269
270 /* -- Summary data -- */
271 struct cycle_framework {
272 tsc_t first_tsc, last_tsc, total_cycles;
273 };
274
275 struct interval_element {
276 int count;
277 long long cycles;
278 long long instructions;
279 };
280
281 struct cycle_summary {
282 int event_count, count, sample_size;
283 unsigned long long cycles;
284 long long *sample;
285 struct interval_element interval;
286 };
287
288 /* -- Symbol list information -- */
289 #define SYMBOL_ENTRIES_PER_STRUCT 1023
290 #define SYMBOL_NAME_SIZE 124
291 struct symbol_struct {
292 int count;
293 struct {
294 unsigned long long addr;
295 char name[SYMBOL_NAME_SIZE];
296 } symbols[SYMBOL_ENTRIES_PER_STRUCT];
297 struct symbol_struct *next;
298 };
299
300 void error(enum error_level l, struct record_info *ri);
301
302 void parse_symbol_file(char *fn) {
303 unsigned long long last_addr = 0;
304 FILE * symbol_file;
305 struct symbol_struct ** p=&G.symbols;
306
307 if((symbol_file=fopen(fn, "rb"))==NULL) {
308 fprintf(stderr, "Could not open symbol file %s\n", fn);
309 perror("open");
310 error(ERR_SYSTEM, NULL);
311 }
312 while(!feof(symbol_file)) {
313 /* Allocate a new struct if we need it */
314 if(!*p) {
315 *p = malloc(sizeof(**p));
316 if(!*p) {
317 fprintf(stderr, "Malloc failed!\n");
318 error(ERR_SYSTEM, NULL);
319 }
320 (*p)->count=0;
321 (*p)->next=NULL;
322 }
323
324 /* FIXME -- use regexp. This won't work for symbols with spaces (yes they exist) */
325 (*p)->symbols[(*p)->count].addr = 0xDEADBEEF;
326 if ( fscanf(symbol_file, "%llx %" STR(SYMBOL_NAME_SIZE) "s",
327 &(*p)->symbols[(*p)->count].addr,
328 (*p)->symbols[(*p)->count].name) == 0 )
329 break;
330
331
332 if( ((*p)->symbols[(*p)->count].addr > 0)
333 && ((*p)->symbols[(*p)->count].addr < last_addr) ) {
334 fprintf(stderr, "Symbol file not properly ordered: %llx %s < %llx!\n",
335 (*p)->symbols[(*p)->count].addr,
336 (*p)->symbols[(*p)->count].name,
337 last_addr);
338 /* Could be recovered from; just free existing strings and set symbols to NULL */
339 error(ERR_ASSERT, NULL);
340 } else
341 last_addr = (*p)->symbols[(*p)->count].addr;
342
343 (*p)->count++;
344
345 /* If this struct is full, point to the next. It will be allocated
346 if needed. */
347 if((*p)->count == SYMBOL_ENTRIES_PER_STRUCT) {
348 p=&((*p)->next);
349 }
350 }
351
352 fclose(symbol_file);
353 }
354
355 /* WARNING not thread safe */
356 char * find_symbol(unsigned long long addr) {
357 struct symbol_struct * p=G.symbols;
358 int i;
359 const char * lastname="ZERO";
360 unsigned long long offset=addr;
361 static char name[144];
362
363 if(!p) {
364 name[0]=0;
365 return name;
366 }
367
368 while(1) {
369 if(!p)
370 goto finish;
371 for(i=0; i<p->count; i++) {
372 if(p->symbols[i].addr > addr)
373 goto finish;
374 else {
375 lastname=p->symbols[i].name;
376 offset=addr - p->symbols[i].addr;
377 }
378 }
379 p=p->next;
380 }
381 finish:
382 snprintf(name, 144, "(%s +%llx)",
383 lastname, offset);
384 return name;
385 }
386
387 /* -- Eip list data -- */
388 enum {
389 EIP_LIST_TYPE_NONE=0,
390 EIP_LIST_TYPE_MAX
391 };
392
393 struct eip_list_struct {
394 struct eip_list_struct *next;
395 unsigned long long eip;
396 struct cycle_summary summary;
397 int type;
398 void * extra;
399 };
400
401 struct {
402 void (*update)(struct eip_list_struct *, void *);
403 void (*new)(struct eip_list_struct *, void *);
404 void (*dump)(struct eip_list_struct *);
405 } eip_list_type[EIP_LIST_TYPE_MAX] = {
406 [EIP_LIST_TYPE_NONE] = {
407 .update=NULL,
408 .new=NULL,
409 .dump=NULL },
410 };
411
412
413 /* --- HVM class of events --- */
414
415 /*
416 * -- Algorithms --
417 *
418 * Interrupt Wake-to-halt detection
419 *
420 * Purpose: To correlate device interrupts to vcpu runtime.
421 *
422 * Diagram:
423 * ...
424 * blocked -> runnable <- set to waking
425 * ...
426 * runnable -> running
427 * inj_virq A <- Note "waking" interrupt
428 * vmenter <- Start tsc of "wake-to-halt" interval.
429 Turn off 'waking'.
430 * ...
431 * inj_virq B <- Note alternate interrupt
432 * vmenter <- Start tsc of "interrupt-to-halt" interval
433 * ...
434 * vmexit <- End tsc of "x-to-halt" interval
435 * running -> blocked <- Process
436 *
437 * The "waking" interrupts we want to sub-classify into
438 * "wake-only" (when interrupt was the only interrupt from wake to halt) and
439 * "wake-all" (whether this was the only interrupt or not).
440 */
441
442 /* VMX data */
443 #define EXIT_REASON_EXCEPTION_NMI 0
444 #define EXIT_REASON_EXTERNAL_INTERRUPT 1
445 #define EXIT_REASON_TRIPLE_FAULT 2
446 #define EXIT_REASON_INIT 3
447 #define EXIT_REASON_SIPI 4
448 #define EXIT_REASON_IO_SMI 5
449 #define EXIT_REASON_OTHER_SMI 6
450 #define EXIT_REASON_PENDING_VIRT_INTR 7
451 #define EXIT_REASON_PENDING_VIRT_NMI 8
452 #define EXIT_REASON_TASK_SWITCH 9
453 #define EXIT_REASON_CPUID 10
454 #define EXIT_REASON_GETSEC 11
455 #define EXIT_REASON_HLT 12
456 #define EXIT_REASON_INVD 13
457 #define EXIT_REASON_INVLPG 14
458 #define EXIT_REASON_RDPMC 15
459 #define EXIT_REASON_RDTSC 16
460 #define EXIT_REASON_RSM 17
461 #define EXIT_REASON_VMCALL 18
462 #define EXIT_REASON_VMCLEAR 19
463 #define EXIT_REASON_VMLAUNCH 20
464 #define EXIT_REASON_VMPTRLD 21
465 #define EXIT_REASON_VMPTRST 22
466 #define EXIT_REASON_VMREAD 23
467 #define EXIT_REASON_VMRESUME 24
468 #define EXIT_REASON_VMWRITE 25
469 #define EXIT_REASON_VMXOFF 26
470 #define EXIT_REASON_VMXON 27
471 #define EXIT_REASON_CR_ACCESS 28
472 #define EXIT_REASON_DR_ACCESS 29
473 #define EXIT_REASON_IO_INSTRUCTION 30
474 #define EXIT_REASON_MSR_READ 31
475 #define EXIT_REASON_MSR_WRITE 32
476 #define EXIT_REASON_INVALID_GUEST_STATE 33
477 #define EXIT_REASON_MSR_LOADING 34
478 #define EXIT_REASON_MWAIT_INSTRUCTION 36
479 #define EXIT_REASON_MONITOR_TRAP_FLAG 37
480 #define EXIT_REASON_MONITOR_INSTRUCTION 39
481 #define EXIT_REASON_PAUSE_INSTRUCTION 40
482 #define EXIT_REASON_MCE_DURING_VMENTRY 41
483 #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
484 #define EXIT_REASON_APIC_ACCESS 44
485 #define EXIT_REASON_ACCESS_GDTR_OR_IDTR 46
486 #define EXIT_REASON_ACCESS_LDTR_OR_TR 47
487 #define EXIT_REASON_EPT_VIOLATION 48
488 #define EXIT_REASON_EPT_MISCONFIG 49
489 #define EXIT_REASON_INVEPT 50
490 #define EXIT_REASON_RDTSCP 51
491 #define EXIT_REASON_VMX_PREEMPTION_TIMER_EXPIRED 52
492 #define EXIT_REASON_INVVPID 53
493 #define EXIT_REASON_WBINVD 54
494 #define EXIT_REASON_XSETBV 55
495
496 #define HVM_VMX_EXIT_REASON_MAX (EXIT_REASON_XSETBV+1)
497
498 const char * hvm_vmx_exit_reason_name[HVM_VMX_EXIT_REASON_MAX] = {
499 [EXIT_REASON_EXCEPTION_NMI]="EXCEPTION_NMI",
500 [EXIT_REASON_EXTERNAL_INTERRUPT]="EXTERNAL_INTERRUPT",
501 [EXIT_REASON_TRIPLE_FAULT]="TRIPLE_FAULT",
502 [EXIT_REASON_INIT]="INIT",
503 [EXIT_REASON_SIPI]="SIPI",
504 [EXIT_REASON_IO_SMI]="IO_SMI",
505 [EXIT_REASON_OTHER_SMI]="OTHER_SMI",
506 [EXIT_REASON_PENDING_VIRT_INTR]="PENDING_VIRT_INTR",
507 [EXIT_REASON_PENDING_VIRT_NMI]="PENDING_VIRT_NMI",
508 [EXIT_REASON_TASK_SWITCH]="TASK_SWITCH",
509 [EXIT_REASON_CPUID]="CPUID",
510 [EXIT_REASON_GETSEC]="GETSEC",
511 [EXIT_REASON_HLT]="HLT",
512 [EXIT_REASON_INVD]="INVD",
513 [EXIT_REASON_INVLPG]="INVLPG",
514 [EXIT_REASON_RDPMC]="RDPMC",
515 [EXIT_REASON_RDTSC]="RDTSC",
516 [EXIT_REASON_RSM]="RSM",
517 [EXIT_REASON_VMCALL]="VMCALL",
518 [EXIT_REASON_VMCLEAR]="VMCLEAR",
519 [EXIT_REASON_VMLAUNCH]="VMLAUNCH",
520 [EXIT_REASON_VMPTRLD]="VMPTRLD",
521 [EXIT_REASON_VMPTRST]="VMPTRST",
522 [EXIT_REASON_VMREAD]="VMREAD",
523 [EXIT_REASON_VMRESUME]="VMRESUME",
524 [EXIT_REASON_VMWRITE]="VMWRITE",
525 [EXIT_REASON_VMXOFF]="VMXOFF",
526 [EXIT_REASON_VMXON]="VMXON",
527 [EXIT_REASON_CR_ACCESS]="CR_ACCESS",
528 [EXIT_REASON_DR_ACCESS]="DR_ACCESS",
529 [EXIT_REASON_IO_INSTRUCTION]="IO_INSTRUCTION",
530 [EXIT_REASON_MSR_READ]="MSR_READ",
531 [EXIT_REASON_MSR_WRITE]="MSR_WRITE",
532 [EXIT_REASON_INVALID_GUEST_STATE]="INVALID_GUEST_STATE",
533 [EXIT_REASON_MSR_LOADING]="MSR_LOADING",
534 [EXIT_REASON_MWAIT_INSTRUCTION]="MWAIT_INSTRUCTION",
535 [EXIT_REASON_MONITOR_TRAP_FLAG]="MONITOR_TRAP_FLAG",
536 [EXIT_REASON_MONITOR_INSTRUCTION]="MONITOR_INSTRUCTION",
537 [EXIT_REASON_PAUSE_INSTRUCTION]="PAUSE_INSTRUCTION",
538 [EXIT_REASON_MCE_DURING_VMENTRY]="MCE_DURING_VMENTRY",
539 [EXIT_REASON_TPR_BELOW_THRESHOLD]="TPR_BELOW_THRESHOLD",
540 [EXIT_REASON_APIC_ACCESS]="APIC_ACCESS",
541 [EXIT_REASON_EPT_VIOLATION]="EPT_VIOLATION",
542 [EXIT_REASON_EPT_MISCONFIG]="EPT_MISCONFIG",
543 [EXIT_REASON_INVEPT]="INVEPT",
544 [EXIT_REASON_RDTSCP]="RDTSCP",
545 [EXIT_REASON_VMX_PREEMPTION_TIMER_EXPIRED]="VMX_PREEMPTION_TIMER_EXPIRED",
546 [EXIT_REASON_INVVPID]="INVVPID",
547 [EXIT_REASON_WBINVD]="WBINVD",
548 [EXIT_REASON_XSETBV]="XSETBV",
549 };
550
551 /* SVM data */
552 enum VMEXIT_EXITCODE
553 {
554 /* control register read exitcodes */
555 VMEXIT_CR0_READ = 0,
556 VMEXIT_CR1_READ = 1,
557 VMEXIT_CR2_READ = 2,
558 VMEXIT_CR3_READ = 3,
559 VMEXIT_CR4_READ = 4,
560 VMEXIT_CR5_READ = 5,
561 VMEXIT_CR6_READ = 6,
562 VMEXIT_CR7_READ = 7,
563 VMEXIT_CR8_READ = 8,
564 VMEXIT_CR9_READ = 9,
565 VMEXIT_CR10_READ = 10,
566 VMEXIT_CR11_READ = 11,
567 VMEXIT_CR12_READ = 12,
568 VMEXIT_CR13_READ = 13,
569 VMEXIT_CR14_READ = 14,
570 VMEXIT_CR15_READ = 15,
571
572 /* control register write exitcodes */
573 VMEXIT_CR0_WRITE = 16,
574 VMEXIT_CR1_WRITE = 17,
575 VMEXIT_CR2_WRITE = 18,
576 VMEXIT_CR3_WRITE = 19,
577 VMEXIT_CR4_WRITE = 20,
578 VMEXIT_CR5_WRITE = 21,
579 VMEXIT_CR6_WRITE = 22,
580 VMEXIT_CR7_WRITE = 23,
581 VMEXIT_CR8_WRITE = 24,
582 VMEXIT_CR9_WRITE = 25,
583 VMEXIT_CR10_WRITE = 26,
584 VMEXIT_CR11_WRITE = 27,
585 VMEXIT_CR12_WRITE = 28,
586 VMEXIT_CR13_WRITE = 29,
587 VMEXIT_CR14_WRITE = 30,
588 VMEXIT_CR15_WRITE = 31,
589
590 /* debug register read exitcodes */
591 VMEXIT_DR0_READ = 32,
592 VMEXIT_DR1_READ = 33,
593 VMEXIT_DR2_READ = 34,
594 VMEXIT_DR3_READ = 35,
595 VMEXIT_DR4_READ = 36,
596 VMEXIT_DR5_READ = 37,
597 VMEXIT_DR6_READ = 38,
598 VMEXIT_DR7_READ = 39,
599 VMEXIT_DR8_READ = 40,
600 VMEXIT_DR9_READ = 41,
601 VMEXIT_DR10_READ = 42,
602 VMEXIT_DR11_READ = 43,
603 VMEXIT_DR12_READ = 44,
604 VMEXIT_DR13_READ = 45,
605 VMEXIT_DR14_READ = 46,
606 VMEXIT_DR15_READ = 47,
607
608 /* debug register write exitcodes */
609 VMEXIT_DR0_WRITE = 48,
610 VMEXIT_DR1_WRITE = 49,
611 VMEXIT_DR2_WRITE = 50,
612 VMEXIT_DR3_WRITE = 51,
613 VMEXIT_DR4_WRITE = 52,
614 VMEXIT_DR5_WRITE = 53,
615 VMEXIT_DR6_WRITE = 54,
616 VMEXIT_DR7_WRITE = 55,
617 VMEXIT_DR8_WRITE = 56,
618 VMEXIT_DR9_WRITE = 57,
619 VMEXIT_DR10_WRITE = 58,
620 VMEXIT_DR11_WRITE = 59,
621 VMEXIT_DR12_WRITE = 60,
622 VMEXIT_DR13_WRITE = 61,
623 VMEXIT_DR14_WRITE = 62,
624 VMEXIT_DR15_WRITE = 63,
625
626 /* processor exception exitcodes (VMEXIT_EXCP[0-31]) */
627 VMEXIT_EXCEPTION_DE = 64, /* divide-by-zero-error */
628 VMEXIT_EXCEPTION_DB = 65, /* debug */
629 VMEXIT_EXCEPTION_NMI = 66, /* non-maskable-interrupt */
630 VMEXIT_EXCEPTION_BP = 67, /* breakpoint */
631 VMEXIT_EXCEPTION_OF = 68, /* overflow */
632 VMEXIT_EXCEPTION_BR = 69, /* bound-range */
633 VMEXIT_EXCEPTION_UD = 70, /* invalid-opcode*/
634 VMEXIT_EXCEPTION_NM = 71, /* device-not-available */
635 VMEXIT_EXCEPTION_DF = 72, /* double-fault */
636 VMEXIT_EXCEPTION_09 = 73, /* unsupported (reserved) */
637 VMEXIT_EXCEPTION_TS = 74, /* invalid-tss */
638 VMEXIT_EXCEPTION_NP = 75, /* segment-not-present */
639 VMEXIT_EXCEPTION_SS = 76, /* stack */
640 VMEXIT_EXCEPTION_GP = 77, /* general-protection */
641 VMEXIT_EXCEPTION_PF = 78, /* page-fault */
642 VMEXIT_EXCEPTION_15 = 79, /* reserved */
643 VMEXIT_EXCEPTION_MF = 80, /* x87 floating-point exception-pending */
644 VMEXIT_EXCEPTION_AC = 81, /* alignment-check */
645 VMEXIT_EXCEPTION_MC = 82, /* machine-check */
646 VMEXIT_EXCEPTION_XF = 83, /* simd floating-point */
647
648 /* exceptions 20-31 (exitcodes 84-95) are reserved */
649
650 /* ...and the rest of the #VMEXITs */
651 VMEXIT_INTR = 96,
652 VMEXIT_NMI = 97,
653 VMEXIT_SMI = 98,
654 VMEXIT_INIT = 99,
655 VMEXIT_VINTR = 100,
656 VMEXIT_CR0_SEL_WRITE = 101,
657 VMEXIT_IDTR_READ = 102,
658 VMEXIT_GDTR_READ = 103,
659 VMEXIT_LDTR_READ = 104,
660 VMEXIT_TR_READ = 105,
661 VMEXIT_IDTR_WRITE = 106,
662 VMEXIT_GDTR_WRITE = 107,
663 VMEXIT_LDTR_WRITE = 108,
664 VMEXIT_TR_WRITE = 109,
665 VMEXIT_RDTSC = 110,
666 VMEXIT_RDPMC = 111,
667 VMEXIT_PUSHF = 112,
668 VMEXIT_POPF = 113,
669 VMEXIT_CPUID = 114,
670 VMEXIT_RSM = 115,
671 VMEXIT_IRET = 116,
672 VMEXIT_SWINT = 117,
673 VMEXIT_INVD = 118,
674 VMEXIT_PAUSE = 119,
675 VMEXIT_HLT = 120,
676 VMEXIT_INVLPG = 121,
677 VMEXIT_INVLPGA = 122,
678 VMEXIT_IOIO = 123,
679 VMEXIT_MSR = 124,
680 VMEXIT_TASK_SWITCH = 125,
681 VMEXIT_FERR_FREEZE = 126,
682 VMEXIT_SHUTDOWN = 127,
683 VMEXIT_VMRUN = 128,
684 VMEXIT_VMMCALL = 129,
685 VMEXIT_VMLOAD = 130,
686 VMEXIT_VMSAVE = 131,
687 VMEXIT_STGI = 132,
688 VMEXIT_CLGI = 133,
689 VMEXIT_SKINIT = 134,
690 VMEXIT_RDTSCP = 135,
691 VMEXIT_ICEBP = 136,
692 VMEXIT_WBINVD = 137,
693 VMEXIT_MONITOR = 138,
694 VMEXIT_MWAIT = 139,
695 VMEXIT_MWAIT_CONDITIONAL= 140,
696 VMEXIT_NPF = 1024, /* nested paging fault */
697 VMEXIT_INVALID = -1
698 };
699
700 #define HVM_SVM_EXIT_REASON_MAX 1025
701 const char * hvm_svm_exit_reason_name[HVM_SVM_EXIT_REASON_MAX] = {
702 /* 0-15 */
703 "VMEXIT_CR0_READ",
704 "VMEXIT_CR1_READ",
705 "VMEXIT_CR2_READ",
706 "VMEXIT_CR3_READ",
707 "VMEXIT_CR4_READ",
708 "VMEXIT_CR5_READ",
709 "VMEXIT_CR6_READ",
710 "VMEXIT_CR7_READ",
711 "VMEXIT_CR8_READ",
712 "VMEXIT_CR9_READ",
713 "VMEXIT_CR10_READ",
714 "VMEXIT_CR11_READ",
715 "VMEXIT_CR12_READ",
716 "VMEXIT_CR13_READ",
717 "VMEXIT_CR14_READ",
718 "VMEXIT_CR15_READ",
719 /* 16-31 */
720 "VMEXIT_CR0_WRITE",
721 "VMEXIT_CR1_WRITE",
722 "VMEXIT_CR2_WRITE",
723 "VMEXIT_CR3_WRITE",
724 "VMEXIT_CR4_WRITE",
725 "VMEXIT_CR5_WRITE",
726 "VMEXIT_CR6_WRITE",
727 "VMEXIT_CR7_WRITE",
728 "VMEXIT_CR8_WRITE",
729 "VMEXIT_CR9_WRITE",
730 "VMEXIT_CR10_WRITE",
731 "VMEXIT_CR11_WRITE",
732 "VMEXIT_CR12_WRITE",
733 "VMEXIT_CR13_WRITE",
734 "VMEXIT_CR14_WRITE",
735 "VMEXIT_CR15_WRITE",
736 /* 32-47 */
737 "VMEXIT_DR0_READ",
738 "VMEXIT_DR1_READ",
739 "VMEXIT_DR2_READ",
740 "VMEXIT_DR3_READ",
741 "VMEXIT_DR4_READ",
742 "VMEXIT_DR5_READ",
743 "VMEXIT_DR6_READ",
744 "VMEXIT_DR7_READ",
745 "VMEXIT_DR8_READ",
746 "VMEXIT_DR9_READ",
747 "VMEXIT_DR10_READ",
748 "VMEXIT_DR11_READ",
749 "VMEXIT_DR12_READ",
750 "VMEXIT_DR13_READ",
751 "VMEXIT_DR14_READ",
752 "VMEXIT_DR15_READ",
753 /* 48-63 */
754 "VMEXIT_DR0_WRITE",
755 "VMEXIT_DR1_WRITE",
756 "VMEXIT_DR2_WRITE",
757 "VMEXIT_DR3_WRITE",
758 "VMEXIT_DR4_WRITE",
759 "VMEXIT_DR5_WRITE",
760 "VMEXIT_DR6_WRITE",
761 "VMEXIT_DR7_WRITE",
762 "VMEXIT_DR8_WRITE",
763 "VMEXIT_DR9_WRITE",
764 "VMEXIT_DR10_WRITE",
765 "VMEXIT_DR11_WRITE",
766 "VMEXIT_DR12_WRITE",
767 "VMEXIT_DR13_WRITE",
768 "VMEXIT_DR14_WRITE",
769 "VMEXIT_DR15_WRITE",
770 /* 64-83 */
771 "VMEXIT_EXCEPTION_DE",
772 "VMEXIT_EXCEPTION_DB",
773 "VMEXIT_EXCEPTION_NMI",
774 "VMEXIT_EXCEPTION_BP",
775 "VMEXIT_EXCEPTION_OF",
776 "VMEXIT_EXCEPTION_BR",
777 "VMEXIT_EXCEPTION_UD",
778 "VMEXIT_EXCEPTION_NM",
779 "VMEXIT_EXCEPTION_DF",
780 "VMEXIT_EXCEPTION_09",
781 "VMEXIT_EXCEPTION_TS",
782 "VMEXIT_EXCEPTION_NP",
783 "VMEXIT_EXCEPTION_SS",
784 "VMEXIT_EXCEPTION_GP",
785 "VMEXIT_EXCEPTION_PF",
786 "VMEXIT_EXCEPTION_15",
787 "VMEXIT_EXCEPTION_MF",
788 "VMEXIT_EXCEPTION_AC",
789 "VMEXIT_EXCEPTION_MC",
790 "VMEXIT_EXCEPTION_XF",
791 /* 84-95 */
792 "VMEXIT_EXCEPTION_20",
793 "VMEXIT_EXCEPTION_21",
794 "VMEXIT_EXCEPTION_22",
795 "VMEXIT_EXCEPTION_23",
796 "VMEXIT_EXCEPTION_24",
797 "VMEXIT_EXCEPTION_25",
798 "VMEXIT_EXCEPTION_26",
799 "VMEXIT_EXCEPTION_27",
800 "VMEXIT_EXCEPTION_28",
801 "VMEXIT_EXCEPTION_29",
802 "VMEXIT_EXCEPTION_30",
803 "VMEXIT_EXCEPTION_31",
804 /* 96-99 */
805 "VMEXIT_INTR",
806 "VMEXIT_NMI",
807 "VMEXIT_SMI",
808 "VMEXIT_INIT",
809 /* 100-109 */
810 "VMEXIT_VINTR",
811 "VMEXIT_CR0_SEL_WRITE",
812 "VMEXIT_IDTR_READ",
813 "VMEXIT_GDTR_READ",
814 "VMEXIT_LDTR_READ",
815 "VMEXIT_TR_READ",
816 "VMEXIT_IDTR_WRITE",
817 "VMEXIT_GDTR_WRITE",
818 "VMEXIT_LDTR_WRITE",
819 "VMEXIT_TR_WRITE",
820 /* 110-119 */
821 "VMEXIT_RDTSC",
822 "VMEXIT_RDPMC",
823 "VMEXIT_PUSHF",
824 "VMEXIT_POPF",
825 "VMEXIT_CPUID",
826 "VMEXIT_RSM",
827 "VMEXIT_IRET",
828 "VMEXIT_SWINT",
829 "VMEXIT_INVD",
830 "VMEXIT_PAUSE",
831 /* 120-129 */
832 "VMEXIT_HLT",
833 "VMEXIT_INVLPG",
834 "VMEXIT_INVLPGA",
835 "VMEXIT_IOIO",
836 "VMEXIT_MSR",
837 "VMEXIT_TASK_SWITCH",
838 "VMEXIT_FERR_FREEZE",
839 "VMEXIT_SHUTDOWN",
840 "VMEXIT_VMRUN",
841 "VMEXIT_VMMCALL",
842 /* 130-139 */
843 "VMEXIT_VMLOAD",
844 "VMEXIT_VMSAVE",
845 "VMEXIT_STGI",
846 "VMEXIT_CLGI",
847 "VMEXIT_SKINIT",
848 "VMEXIT_RDTSCP",
849 "VMEXIT_ICEBP",
850 "VMEXIT_WBINVD",
851 "VMEXIT_MONITOR",
852 "VMEXIT_MWAIT",
853 /* 140 */
854 "VMEXIT_MWAIT_CONDITIONAL",
855 [VMEXIT_NPF] = "VMEXIT_NPF", /* nested paging fault */
856 };
857
858
859 #if ( HVM_VMX_EXIT_REASON_MAX > HVM_SVM_EXIT_REASON_MAX )
860 # define HVM_EXIT_REASON_MAX HVM_VMX_EXIT_REASON_MAX
861 # error - Strange!
862 #else
863 # define HVM_EXIT_REASON_MAX HVM_SVM_EXIT_REASON_MAX
864 #endif
865
866 /* General hvm information */
867 #define SPURIOUS_APIC_VECTOR 0xff
868 #define ERROR_APIC_VECTOR 0xfe
869 #define INVALIDATE_TLB_VECTOR 0xfd
870 #define EVENT_CHECK_VECTOR 0xfc
871 #define CALL_FUNCTION_VECTOR 0xfb
872 #define THERMAL_APIC_VECTOR 0xfa
873 #define LOCAL_TIMER_VECTOR 0xf9
874
875 #define EXTERNAL_INTERRUPT_MAX 256
876
877 /* Stringify numbers */
878 const char * hvm_extint_vector_name[EXTERNAL_INTERRUPT_MAX] = {
879 [SPURIOUS_APIC_VECTOR] = "SPURIOS_APIC",
880 [ERROR_APIC_VECTOR] = "ERROR_APIC",
881 [INVALIDATE_TLB_VECTOR]= "INVALIDATE_TLB",
882 [EVENT_CHECK_VECTOR]= "EVENT_CHECK",
883 [CALL_FUNCTION_VECTOR]= "CALL_FUNCTION",
884 [THERMAL_APIC_VECTOR]= "THERMAL_APIC",
885 [LOCAL_TIMER_VECTOR] = "LOCAL_TIMER",
886 };
887
888 #define HVM_TRAP_MAX 20
889
890 const char * hvm_trap_name[HVM_TRAP_MAX] = {
891 [0] = "Divide",
892 [1] = "RESERVED",
893 [2] = "NMI",
894 [3] = "Breakpoint",
895 [4] = "Overflow",
896 [5] = "BOUND",
897 [6] = "Invalid Op",
898 [7] = "Coprocessor not present",
899 [8] = "Double Fault",
900 [9] = "Coprocessor segment overrun",
901 [10] = "TSS",
902 [11] = "Segment not present",
903 [12] = "Stack-segment fault",
904 [13] = "GP",
905 [14] = "Page fault",
906 [15] = "RESERVED",
907 [16] = "FPU",
908 [17] = "Alignment check",
909 [18] = "Machine check",
910 [19] = "SIMD",
911 };
912
913
914 enum {
915 HVM_EVENT_HANDLER_NONE = 0,
916 HVM_EVENT_HANDLER_PF_XEN = 1,
917 HVM_EVENT_HANDLER_PF_INJECT,
918 HVM_EVENT_HANDLER_INJ_EXC,
919 HVM_EVENT_HANDLER_INJ_VIRQ,
920 HVM_EVENT_HANDLER_REINJ_VIRQ,
921 HVM_EVENT_HANDLER_IO_READ,
922 HVM_EVENT_HANDLER_IO_WRITE,
923 HVM_EVENT_HANDLER_CR_READ, /* 8 */
924 HVM_EVENT_HANDLER_CR_WRITE,
925 HVM_EVENT_HANDLER_DR_READ,
926 HVM_EVENT_HANDLER_DR_WRITE,
927 HVM_EVENT_HANDLER_MSR_READ,
928 HVM_EVENT_HANDLER_MSR_WRITE,
929 HVM_EVENT_HANDLER_CPUID,
930 HVM_EVENT_HANDLER_INTR,
931 HVM_EVENT_HANDLER_NMI, /* 16 */
932 HVM_EVENT_HANDLER_SMI,
933 HVM_EVENT_HANDLER_VMCALL,
934 HVM_EVENT_HANDLER_HLT,
935 HVM_EVENT_HANDLER_INVLPG,
936 HVM_EVENT_HANDLER_MCE,
937 HVM_EVENT_HANDLER_IO_ASSIST,
938 HVM_EVENT_HANDLER_MMIO_ASSIST,
939 HVM_EVENT_HANDLER_CLTS,
940 HVM_EVENT_HANDLER_LMSW,
941 HVM_EVENT_RDTSC,
942 HVM_EVENT_INTR_WINDOW=0x20, /* Oops... skipped 0x1b-1f */
943 HVM_EVENT_NPF,
944 HVM_EVENT_REALMODE_EMULATE,
945 HVM_EVENT_TRAP,
946 HVM_EVENT_TRAP_DEBUG,
947 HVM_EVENT_VLAPIC,
948 HVM_EVENT_HANDLER_MAX
949 };
950 const char * hvm_event_handler_name[HVM_EVENT_HANDLER_MAX] = {
951 "(no handler)",
952 "pf_xen",
953 "pf_inject",
954 "inj_exc",
955 "inj_virq",
956 "reinj_virq",
957 "io_read",
958 "io_write",
959 "cr_read", /* 8 */
960 "cr_write",
961 "dr_read",
962 "dr_write",
963 "msr_read",
964 "msr_write",
965 "cpuid",
966 "intr",
967 "nmi", /* 16 */
968 "smi",
969 "vmcall",
970 "hlt",
971 "invlpg",
972 "mce",
973 "io_assist",
974 "mmio_assist",
975 "clts", /* 24 */
976 "lmsw",
977 "rdtsc",
978 [HVM_EVENT_INTR_WINDOW]="intr_window",
979 "npf",
980 "realmode_emulate",
981 "trap",
982 "trap_debug",
983 "vlapic"
984 };
985
986 enum {
987 HVM_VOL_VMENTRY,
988 HVM_VOL_VMEXIT,
989 HVM_VOL_HANDLER,
990 HVM_VOL_EMUL,
991 HVM_VOL_MAX
992 };
993
994 enum {
995 GUEST_INTERRUPT_CASE_NONE,
996 /* This interrupt woke, no other interrupts until halt */
997 GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ALONE,
998 /* This interrupt woke, maybe another interrupt before halt */
999 GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ANY,
1000 /* Time from interrupt (running) to halt */
1001 GUEST_INTERRUPT_CASE_INTERRUPT_TO_HALT,
1002 GUEST_INTERRUPT_CASE_MAX,
1003 };
1004
1005 const char *guest_interrupt_case_name[] = {
1006 [GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ALONE]="wake to halt alone",
1007 /* This interrupt woke, maybe another interrupt before halt */
1008 [GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ANY] ="wake to halt any ",
1009 /* Time from interrupt (running) to halt */
1010 [GUEST_INTERRUPT_CASE_INTERRUPT_TO_HALT] ="intr to halt ",
1011 };
1012
1013 const char *hvm_vol_name[HVM_VOL_MAX] = {
1014 [HVM_VOL_VMENTRY]="vmentry",
1015 [HVM_VOL_VMEXIT] ="vmexit",
1016 [HVM_VOL_HANDLER]="handler",
1017 [HVM_VOL_EMUL]="emul",
1018 };
1019
1020 enum {
1021 HYPERCALL_set_trap_table = 0,
1022 HYPERCALL_mmu_update,
1023 HYPERCALL_set_gdt,
1024 HYPERCALL_stack_switch,
1025 HYPERCALL_set_callbacks,
1026 HYPERCALL_fpu_taskswitch,
1027 HYPERCALL_sched_op_compat,
1028 HYPERCALL_platform_op,
1029 HYPERCALL_set_debugreg,
1030 HYPERCALL_get_debugreg,
1031 HYPERCALL_update_descriptor,
1032 HYPERCALL_memory_op=12,
1033 HYPERCALL_multicall,
1034 HYPERCALL_update_va_mapping,
1035 HYPERCALL_set_timer_op,
1036 HYPERCALL_event_channel_op_compat,
1037 HYPERCALL_xen_version,
1038 HYPERCALL_console_io,
1039 HYPERCALL_physdev_op_compat,
1040 HYPERCALL_grant_table_op,
1041 HYPERCALL_vm_assist,
1042 HYPERCALL_update_va_mapping_otherdomain,
1043 HYPERCALL_iret,
1044 HYPERCALL_vcpu_op,
1045 HYPERCALL_set_segment_base,
1046 HYPERCALL_mmuext_op,
1047 HYPERCALL_acm_op,
1048 HYPERCALL_nmi_op,
1049 HYPERCALL_sched_op,
1050 HYPERCALL_callback_op,
1051 HYPERCALL_xenoprof_op,
1052 HYPERCALL_event_channel_op,
1053 HYPERCALL_physdev_op,
1054 HYPERCALL_hvm_op,
1055 HYPERCALL_sysctl,
1056 HYPERCALL_domctl,
1057 HYPERCALL_kexec_op,
1058 HYPERCALL_MAX
1059 };
1060
1061 const char *hypercall_name[HYPERCALL_MAX] = {
1062 [HYPERCALL_set_trap_table]="set_trap_table",
1063 [HYPERCALL_mmu_update]="mmu_update",
1064 [HYPERCALL_set_gdt]="set_gdt",
1065 [HYPERCALL_stack_switch]="stack_switch",
1066 [HYPERCALL_set_callbacks]="set_callbacks",
1067 [HYPERCALL_fpu_taskswitch]="fpu_taskswitch",
1068 [HYPERCALL_sched_op_compat]="sched_op(compat)",
1069 [HYPERCALL_platform_op]="platform_op",
1070 [HYPERCALL_set_debugreg]="set_debugreg",
1071 [HYPERCALL_get_debugreg]="get_debugreg",
1072 [HYPERCALL_update_descriptor]="update_descriptor",
1073 [HYPERCALL_memory_op]="memory_op",
1074 [HYPERCALL_multicall]="multicall",
1075 [HYPERCALL_update_va_mapping]="update_va_mapping",
1076 [HYPERCALL_set_timer_op]="set_timer_op",
1077 [HYPERCALL_event_channel_op_compat]="evtchn_op(compat)",
1078 [HYPERCALL_xen_version]="xen_version",
1079 [HYPERCALL_console_io]="console_io",
1080 [HYPERCALL_physdev_op_compat]="physdev_op(compat)",
1081 [HYPERCALL_grant_table_op]="grant_table_op",
1082 [HYPERCALL_vm_assist]="vm_assist",
1083 [HYPERCALL_update_va_mapping_otherdomain]="update_va_mapping_otherdomain",
1084 [HYPERCALL_iret]="iret",
1085 [HYPERCALL_vcpu_op]="vcpu_op",
1086 [HYPERCALL_set_segment_base]="set_segment_base",
1087 [HYPERCALL_mmuext_op]="mmuext_op",
1088 [HYPERCALL_acm_op]="acm_op",
1089 [HYPERCALL_nmi_op]="nmi_op",
1090 [HYPERCALL_sched_op]="sched_op",
1091 [HYPERCALL_callback_op]="callback_op",
1092 [HYPERCALL_xenoprof_op]="xenoprof_op",
1093 [HYPERCALL_event_channel_op]="evtchn_op",
1094 [HYPERCALL_physdev_op]="physdev_op",
1095 [HYPERCALL_hvm_op]="hvm_op",
1096 [HYPERCALL_sysctl]="sysctl",
1097 [HYPERCALL_domctl]="domctl",
1098 [HYPERCALL_kexec_op]="kexec_op"
1099 };
1100
1101 enum {
1102 PF_XEN_EMUL_LVL_0,
1103 PF_XEN_EMUL_LVL_1,
1104 PF_XEN_EMUL_LVL_2,
1105 PF_XEN_EMUL_LVL_3,
1106 PF_XEN_EMUL_LVL_4,
1107 PF_XEN_EMUL_EARLY_UNSHADOW,
1108 PF_XEN_EMUL_SET_CHANGED,
1109 PF_XEN_EMUL_SET_UNCHANGED,
1110 PF_XEN_EMUL_SET_FLUSH,
1111 PF_XEN_EMUL_SET_ERROR,
1112 PF_XEN_EMUL_PROMOTE,
1113 PF_XEN_EMUL_DEMOTE,
1114 PF_XEN_EMUL_PREALLOC_UNPIN,
1115 PF_XEN_EMUL_PREALLOC_UNHOOK,
1116 PF_XEN_EMUL_MAX,
1117 };
1118
1119 const char * pf_xen_emul_name[PF_XEN_EMUL_MAX] = {
1120 [PF_XEN_EMUL_LVL_0]="non-linmap",
1121 [PF_XEN_EMUL_LVL_1]="linmap l1",
1122 [PF_XEN_EMUL_LVL_2]="linmap l2",
1123 [PF_XEN_EMUL_LVL_3]="linmap l3",
1124 [PF_XEN_EMUL_LVL_4]="linmap l4",
1125 [PF_XEN_EMUL_EARLY_UNSHADOW]="early unshadow",
1126 [PF_XEN_EMUL_SET_UNCHANGED]="set unchanged",
1127 [PF_XEN_EMUL_SET_CHANGED]="set changed",
1128 [PF_XEN_EMUL_SET_FLUSH]="set changed",
1129 [PF_XEN_EMUL_SET_ERROR]="set changed",
1130 [PF_XEN_EMUL_PROMOTE]="promote",
1131 [PF_XEN_EMUL_DEMOTE]="demote",
1132 [PF_XEN_EMUL_PREALLOC_UNPIN]="unpin",
1133 [PF_XEN_EMUL_PREALLOC_UNHOOK]="unhook",
1134 };
1135
1136 /* Rio only */
1137 enum {
1138 PF_XEN_NON_EMUL_VA_USER,
1139 PF_XEN_NON_EMUL_VA_KERNEL,
1140 PF_XEN_NON_EMUL_EIP_USER,
1141 PF_XEN_NON_EMUL_EIP_KERNEL,
1142 PF_XEN_NON_EMUL_MAX,
1143 };
1144
1145 const char * pf_xen_non_emul_name[PF_XEN_NON_EMUL_MAX] = {
1146 [PF_XEN_NON_EMUL_VA_USER]="va user",
1147 [PF_XEN_NON_EMUL_VA_KERNEL]="va kernel",
1148 [PF_XEN_NON_EMUL_EIP_USER]="eip user",
1149 [PF_XEN_NON_EMUL_EIP_KERNEL]="eip kernel",
1150 };
1151
1152 enum {
1153 PF_XEN_FIXUP_PREALLOC_UNPIN,
1154 PF_XEN_FIXUP_PREALLOC_UNHOOK,
1155 PF_XEN_FIXUP_UNSYNC,
1156 PF_XEN_FIXUP_OOS_ADD,
1157 PF_XEN_FIXUP_OOS_EVICT,
1158 PF_XEN_FIXUP_PROMOTE,
1159 PF_XEN_FIXUP_UPDATE_ONLY,
1160 PF_XEN_FIXUP_WRMAP,
1161 PF_XEN_FIXUP_BRUTE_FORCE,
1162 PF_XEN_FIXUP_MAX,
1163 };
1164
1165 const char * pf_xen_fixup_name[PF_XEN_FIXUP_MAX] = {
1166 [PF_XEN_FIXUP_PREALLOC_UNPIN] = "unpin",
1167 [PF_XEN_FIXUP_PREALLOC_UNHOOK] = "unhook",
1168 [PF_XEN_FIXUP_UNSYNC] = "unsync",
1169 [PF_XEN_FIXUP_OOS_ADD] = "oos-add",
1170 [PF_XEN_FIXUP_OOS_EVICT] = "oos-evict",
1171 [PF_XEN_FIXUP_PROMOTE] = "promote",
1172 [PF_XEN_FIXUP_UPDATE_ONLY] = "update",
1173 [PF_XEN_FIXUP_WRMAP] = "wrmap",
1174 [PF_XEN_FIXUP_BRUTE_FORCE] = "wrmap-bf",
1175 };
1176
1177 enum {
1178 PF_XEN_NOT_SHADOW = 1,
1179 PF_XEN_FAST_PROPAGATE,
1180 PF_XEN_FAST_MMIO,
1181 PF_XEN_FALSE_FAST_PATH,
1182 PF_XEN_MMIO,
1183 PF_XEN_FIXUP,
1184 PF_XEN_DOMF_DYING,
1185 PF_XEN_EMULATE,
1186 PF_XEN_EMULATE_UNSHADOW_USER,
1187 PF_XEN_EMULATE_UNSHADOW_EVTINJ,
1188 PF_XEN_EMULATE_UNSHADOW_UNHANDLED,
1189 PF_XEN_LAST_FAULT=PF_XEN_EMULATE_UNSHADOW_UNHANDLED,
1190 PF_XEN_NON_EMULATE,
1191 PF_XEN_NO_HANDLER,
1192 PF_XEN_MAX,
1193 };
1194
1195 #define SHADOW_WRMAP_BF 12
1196 #define SHADOW_PREALLOC_UNPIN 13
1197 #define SHADOW_RESYNC_FULL 14
1198 #define SHADOW_RESYNC_ONLY 15
1199
1200 const char * pf_xen_name[PF_XEN_MAX] = {
1201 [PF_XEN_NOT_SHADOW]="propagate",
1202 [PF_XEN_FAST_PROPAGATE]="fast propagate",
1203 [PF_XEN_FAST_MMIO]="fast mmio",
1204 [PF_XEN_FALSE_FAST_PATH]="false fast path",
1205 [PF_XEN_MMIO]="mmio",
1206 [PF_XEN_FIXUP]="fixup",
1207 [PF_XEN_DOMF_DYING]="dom dying",
1208 [PF_XEN_EMULATE]="emulate",
1209 [PF_XEN_EMULATE_UNSHADOW_USER]="unshadow:user-mode",
1210 [PF_XEN_EMULATE_UNSHADOW_EVTINJ]="unshadow:evt inj",
1211 [PF_XEN_EMULATE_UNSHADOW_UNHANDLED]="unshadow:unhandled instr",
1212 [PF_XEN_NON_EMULATE]="fixup|mmio",
1213 [PF_XEN_NO_HANDLER]="(no handler)",
1214 };
1215
1216 #define CORR_VA_INVALID (0ULL-1)
1217
1218 enum {
1219 NONPF_MMIO_APIC,
1220 NONPF_MMIO_NPF,
1221 NONPF_MMIO_UNKNOWN,
1222 NONPF_MMIO_MAX
1223 };
1224
1225 struct mmio_info {
1226 unsigned long long gpa;
1227 unsigned long long va; /* Filled only by shadow */
1228 unsigned data;
1229 unsigned data_valid:1, is_write:1;
1230 };
1231
1232 struct pf_xen_extra {
1233 unsigned long long va;
1234 union {
1235 unsigned flags;
1236 struct {
1237 unsigned flag_set_ad:1,
1238 flag_set_a:1,
1239 flag_shadow_l1_get_ref:1,
1240 flag_shadow_l1_put_ref:1,
1241 flag_l2_propagate:1,
1242 flag_set_changed:1,
1243 flag_set_flush:1,
1244 flag_set_error:1,
1245 flag_demote:1,
1246 flag_promote:1,
1247 flag_wrmap:1,
1248 flag_wrmap_guess_found:1,
1249 flag_wrmap_brute_force:1,
1250 flag_early_unshadow:1,
1251 flag_emulation_2nd_pt_written:1,
1252 flag_emulation_last_failed:1,
1253 flag_emulate_full_pt:1,
1254 flag_prealloc_unhook:1,
1255 flag_unsync:1,
1256 flag_oos_fixup_add:1,
1257 flag_oos_fixup_evict:1;
1258 };
1259 }; /* Miami + ; fixup & emulate */
1260 unsigned int error_code; /* Rio only */
1261
1262 /* Calculated */
1263 int pf_case; /* Rio */
1264
1265 /* MMIO only */
1266 unsigned long long gpa;
1267 unsigned int data;
1268
1269 /* Emulate only */
1270 unsigned long long gl1e; /* Miami + */
1271 unsigned long long wval; /* Miami */
1272 unsigned long long corresponding_va;
1273 unsigned int pt_index[5], pt_is_lo;
1274 int pt_level;
1275
1276 /* Other */
1277 unsigned long long gfn;
1278
1279 /* Flags */
1280 unsigned corr_valid:1,
1281 corr_is_kernel:1,
1282 va_is_kernel:1;
1283 };
1284
1285 struct pcpu_info;
1286
1287 #define GUEST_INTERRUPT_MAX 350
1288 #define FAKE_VECTOR 349
1289 #define CR_MAX 9
1290 #define RESYNCS_MAX 17
1291 #define PF_XEN_FIXUP_UNSYNC_RESYNC_MAX 2
1292
1293 struct hvm_data;
1294
1295 struct hvm_summary_handler_node {
1296 void (*handler)(struct hvm_data *, void* data);
1297 void *data;
1298 struct hvm_summary_handler_node *next;
1299 };
1300
1301 struct hvm_data {
1302 /* Summary information */
1303 int init;
1304 int vmexit_valid;
1305 int summary_info;
1306 struct vcpu_data *v; /* up-pointer */
1307
1308 /* SVM / VMX compatibility. FIXME - should be global */
1309 const char ** exit_reason_name;
1310 int exit_reason_max;
1311 struct hvm_summary_handler_node *exit_reason_summary_handler_list[HVM_EXIT_REASON_MAX];
1312
1313 /* Information about particular exit reasons */
1314 struct {
1315 struct cycle_summary exit_reason[HVM_EXIT_REASON_MAX];
1316 int extint[EXTERNAL_INTERRUPT_MAX+1];
1317 int *extint_histogram;
1318 struct cycle_summary trap[HVM_TRAP_MAX];
1319 struct cycle_summary pf_xen[PF_XEN_MAX];
1320 struct cycle_summary pf_xen_emul[PF_XEN_EMUL_MAX];
1321 struct cycle_summary pf_xen_emul_early_unshadow[5];
1322 struct cycle_summary pf_xen_non_emul[PF_XEN_NON_EMUL_MAX];
1323 struct cycle_summary pf_xen_fixup[PF_XEN_FIXUP_MAX];
1324 struct cycle_summary pf_xen_fixup_unsync_resync[PF_XEN_FIXUP_UNSYNC_RESYNC_MAX+1];
1325 struct cycle_summary cr_write[CR_MAX];
1326 struct cycle_summary cr3_write_resyncs[RESYNCS_MAX+1];
1327 struct cycle_summary vmcall[HYPERCALL_MAX+1];
1328 struct cycle_summary generic[HVM_EVENT_HANDLER_MAX];
1329 struct cycle_summary mmio[NONPF_MMIO_MAX];
1330 struct hvm_gi_struct {
1331 int count;
1332 struct cycle_summary runtime[GUEST_INTERRUPT_CASE_MAX];
1333 /* OK, not summary info, but still... */
1334 int is_wake;
1335 tsc_t start_tsc;
1336 } guest_interrupt[GUEST_INTERRUPT_MAX + 1];
1337 /* IPI Latency */
1338 struct cycle_summary ipi_latency;
1339 int ipi_count[256];
1340 struct {
1341 struct io_address *mmio, *pio;
1342 } io;
1343 } summary;
1344
1345 /* In-flight accumulation information */
1346 struct {
1347 union {
1348 struct {
1349 unsigned port:31,
1350 is_write:1;
1351 unsigned int val;
1352 } io;
1353 struct pf_xen_extra pf_xen;
1354 struct {
1355 unsigned cr;
1356 unsigned long long val;
1357 int repromote;
1358 } cr_write;
1359 struct {
1360 unsigned addr;
1361 unsigned long long val;
1362 } msr;
1363 struct {
1364 unsigned int event;
1365 uint32_t d[4];
1366 } generic;
1367 struct {
1368 unsigned eax;
1369 } vmcall;
1370 struct {
1371 unsigned vec;
1372 } intr;
1373 };
1374 /* MMIO gets its separate area, since many exits may use it */
1375 struct mmio_info mmio;
1376 }inflight;
1377 int resyncs;
1378 void (*post_process)(struct hvm_data *);
1379 tsc_t exit_tsc, arc_cycles, entry_tsc;
1380 unsigned long long rip;
1381 unsigned exit_reason, event_handler;
1382 unsigned int short_summary_done:1, prealloc_unpin:1, wrmap_bf:1;
1383
1384 /* Immediate processing */
1385 void *d;
1386
1387 /* Wake-to-halt detection. See comment above. */
1388 struct {
1389 unsigned waking:1;
1390 /* Wake vector: keep track of time from vmentry until:
1391 next halt, or next interrupt */
1392 int vector, interrupts, interrupts_wanting_tsc;
1393 } w2h;
1394
1395 /* Historical info */
1396 tsc_t last_rdtsc;
1397 };
1398
1399 enum {
1400 HVM_SHORT_SUMMARY_EMULATE,
1401 HVM_SHORT_SUMMARY_UNSYNC,
1402 HVM_SHORT_SUMMARY_FIXUP,
1403 HVM_SHORT_SUMMARY_MMIO,
1404 HVM_SHORT_SUMMARY_PROPAGATE,
1405 HVM_SHORT_SUMMARY_CR3,
1406 HVM_SHORT_SUMMARY_VMCALL,
1407 HVM_SHORT_SUMMARY_INTERRUPT,
1408 HVM_SHORT_SUMMARY_HLT,
1409 HVM_SHORT_SUMMARY_OTHER,
1410 HVM_SHORT_SUMMARY_MAX,
1411 };
1412
1413 const char *hvm_short_summary_name[HVM_SHORT_SUMMARY_MAX] = {
1414 [HVM_SHORT_SUMMARY_EMULATE] ="emulate",
1415 [HVM_SHORT_SUMMARY_UNSYNC] ="unsync",
1416 [HVM_SHORT_SUMMARY_FIXUP] ="fixup",
1417 [HVM_SHORT_SUMMARY_MMIO] ="mmio",
1418 [HVM_SHORT_SUMMARY_PROPAGATE]="propagate",
1419 [HVM_SHORT_SUMMARY_CR3] ="cr3",
1420 [HVM_SHORT_SUMMARY_VMCALL] ="vmcall",
1421 [HVM_SHORT_SUMMARY_INTERRUPT]="intr",
1422 [HVM_SHORT_SUMMARY_HLT] ="hlt",
1423 [HVM_SHORT_SUMMARY_OTHER] ="other",
1424 };
1425
1426 struct hvm_short_summary_struct {
1427 struct cycle_summary s[HVM_SHORT_SUMMARY_MAX];
1428 };
1429
1430 void init_hvm_data(struct hvm_data *h, struct vcpu_data *v) {
1431 int i;
1432
1433 if(h->init)
1434 return;
1435
1436 h->v = v;
1437
1438 h->init = 1;
1439
1440 if(opt.histogram_interrupt_eip) {
1441 int count = ((1ULL<<ADDR_SPACE_BITS)/opt.histogram_interrupt_increment);
1442 size_t size = count * sizeof(int);
1443 h->summary.extint_histogram = malloc(size);
1444 if(h->summary.extint_histogram)
1445 bzero(h->summary.extint_histogram, size);
1446 else {
1447 fprintf(stderr, "FATAL: Could not allocate %zd bytes for interrupt histogram!\n",
1448 size);
1449 error(ERR_SYSTEM, NULL);
1450 }
1451
1452 }
1453 for(i=0; i<GUEST_INTERRUPT_MAX+1; i++)
1454 h->summary.guest_interrupt[i].count=0;
1455 }
1456
1457 void set_hvm_exit_reason_data(struct hvm_data *h, unsigned event) {
1458 if (((event & ~TRC_64_FLAG) == TRC_HVM_SVM_EXIT) ||
1459 opt.svm_mode) {
1460 opt.svm_mode = 1;
1461 h->exit_reason_max = HVM_SVM_EXIT_REASON_MAX;
1462 h->exit_reason_name = hvm_svm_exit_reason_name;
1463 } else {
1464 h->exit_reason_max = HVM_VMX_EXIT_REASON_MAX;
1465 h->exit_reason_name = hvm_vmx_exit_reason_name;
1466 }
1467 }
1468
1469 /* PV data */
1470 enum {
1471 PV_HYPERCALL=1,
1472 PV_TRAP=3,
1473 PV_PAGE_FAULT,
1474 PV_FORCED_INVALID_OP,
1475 PV_EMULATE_PRIVOP,
1476 PV_EMULATE_4GB,
1477 PV_MATH_STATE_RESTORE,
1478 PV_PAGING_FIXUP,
1479 PV_GDT_LDT_MAPPING_FAULT,
1480 PV_PTWR_EMULATION,
1481 PV_PTWR_EMULATION_PAE,
1482 PV_HYPERCALL_V2 = 13,
1483 PV_HYPERCALL_SUBCALL = 14,
1484 PV_MAX
1485 };
1486
1487 const char *pv_name[PV_MAX] = {
1488 [PV_HYPERCALL]="hypercall",
1489 [PV_TRAP]="trap",
1490 [PV_PAGE_FAULT]="page_fault",
1491 [PV_FORCED_INVALID_OP]="forced_invalid_op",
1492 [PV_EMULATE_PRIVOP]="emulate privop",
1493 [PV_EMULATE_4GB]="emulate 4g",
1494 [PV_MATH_STATE_RESTORE]="math state restore",
1495 [PV_PAGING_FIXUP]="paging fixup",
1496 [PV_GDT_LDT_MAPPING_FAULT]="gdt/ldt mapping fault",
1497 [PV_PTWR_EMULATION]="ptwr",
1498 [PV_PTWR_EMULATION_PAE]="ptwr(pae)",
1499 [PV_HYPERCALL_V2]="hypercall",
1500 [PV_HYPERCALL_SUBCALL]="hypercall (subcall)",
1501 };
1502
1503 #define PV_HYPERCALL_MAX 56
1504 #define PV_TRAP_MAX 20
1505
1506 struct pv_data {
1507 unsigned summary_info:1;
1508 int count[PV_MAX];
1509 int hypercall_count[PV_HYPERCALL_MAX];
1510 int trap_count[PV_TRAP_MAX];
1511 };
1512
1513 /* Sched data */
1514 enum {
1515 RUNSTATE_RUNNING=0,
1516 RUNSTATE_RUNNABLE,
1517 RUNSTATE_BLOCKED,
1518 RUNSTATE_OFFLINE,
1519 RUNSTATE_LOST,
1520 RUNSTATE_QUEUED,
1521 RUNSTATE_INIT,
1522 RUNSTATE_MAX
1523 };
1524
1525 int runstate_graph[RUNSTATE_MAX] =
1526 {
1527 [RUNSTATE_BLOCKED]=0,
1528 [RUNSTATE_OFFLINE]=1,
1529 [RUNSTATE_RUNNABLE]=2,
1530 [RUNSTATE_RUNNING]=3,
1531 [RUNSTATE_LOST]=-1,
1532 [RUNSTATE_QUEUED]=-2,
1533 [RUNSTATE_INIT]=-2,
1534 };
1535
1536 const char * runstate_name[RUNSTATE_MAX]={
1537 [RUNSTATE_RUNNING]= "running",
1538 [RUNSTATE_RUNNABLE]="runnable",
1539 [RUNSTATE_BLOCKED]= "blocked", /* to be blocked */
1540 [RUNSTATE_OFFLINE]= "offline",
1541 [RUNSTATE_QUEUED]= "queued",
1542 [RUNSTATE_INIT]= "init",
1543 [RUNSTATE_LOST]= "lost",
1544 };
1545
1546 enum {
1547 RUNNABLE_STATE_INVALID,
1548 RUNNABLE_STATE_WAKE,
1549 RUNNABLE_STATE_PREEMPT,
1550 RUNNABLE_STATE_OTHER,
1551 RUNNABLE_STATE_MAX
1552 };
1553
1554 const char * runnable_state_name[RUNNABLE_STATE_MAX]={
1555 [RUNNABLE_STATE_INVALID]="invalid", /* Should never show up */
1556 [RUNNABLE_STATE_WAKE]="wake",
1557 [RUNNABLE_STATE_PREEMPT]="preempt",
1558 [RUNNABLE_STATE_OTHER]="other",
1559 };
1560
1561 /* Memory data */
1562 enum {
1563 MEM_PAGE_GRANT_MAP = 1,
1564 MEM_PAGE_GRANT_UNMAP,
1565 MEM_PAGE_GRANT_TRANSFER,
1566 MEM_SET_P2M_ENTRY,
1567 MEM_DECREASE_RESERVATION,
1568 MEM_POD_POPULATE = 16,
1569 MEM_POD_ZERO_RECLAIM,
1570 MEM_POD_SUPERPAGE_SPLINTER,
1571 MEM_MAX
1572 };
1573
1574 const char *mem_name[MEM_MAX] = {
1575 [MEM_PAGE_GRANT_MAP] = "grant-map",
1576 [MEM_PAGE_GRANT_UNMAP] = "grant-unmap",
1577 [MEM_PAGE_GRANT_TRANSFER] = "grant-transfer",
1578 [MEM_SET_P2M_ENTRY] = "set-p2m",
1579 [MEM_DECREASE_RESERVATION] = "decrease-reservation",
1580 [MEM_POD_POPULATE] = "pod-populate",
1581 [MEM_POD_ZERO_RECLAIM] = "pod-zero-reclaim",
1582 [MEM_POD_SUPERPAGE_SPLINTER] = "pod-superpage-splinter",
1583 };
1584
1585 /* Per-unit information. */
1586
1587 struct cr3_value_struct {
1588 struct cr3_value_struct * next;
1589 struct cr3_value_struct * gnext;
1590 unsigned long long gmfn;
1591 int cr3_id;
1592 unsigned long long first_time, last_time, run_time;
1593 struct cycle_summary total_time, guest_time, hv_time;
1594 int switch_count, flush_count;
1595
1596 struct hvm_short_summary_struct hvm;
1597
1598 struct {
1599 int now;
1600 int count;
1601 } prealloc_unpin;
1602
1603 struct {
1604 unsigned callback:1;
1605 unsigned flush_count, switch_count;
1606 unsigned fixup_user, emulate_corr_user;
1607 } destroy;
1608 };
1609
1610 #ifndef MAX_CPUS
1611 #define MAX_CPUS 256
1612 #endif
1613 typedef uint32_t cpu_mask_t;
1614
1615 #define IDLE_DOMAIN 32767
1616 #define DEFAULT_DOMAIN 32768
1617
1618 #define MAX_VLAPIC_LIST 8
1619 struct vlapic_struct {
1620 struct {
1621 struct outstanding_ipi {
1622 tsc_t first_tsc;
1623 int vec, count;
1624 int injected, valid;
1625 } list[MAX_VLAPIC_LIST];
1626 } outstanding;
1627 };
1628
1629 struct vcpu_data {
1630 int vid;
1631 struct domain_data *d; /* up-pointer */
1632 unsigned activated:1, delayed_init:1;
1633
1634 int guest_paging_levels;
1635
1636 /* Schedule info */
1637 struct {
1638 int state;
1639 int runnable_state; /* Only valid when state==RUNSTATE_RUNNABLE */
1640 tsc_t tsc;
1641 /* TSC skew detection/correction */
1642 struct last_oldstate_struct {
1643 int wrong, actual, pid;
1644 tsc_t tsc;
1645 } last_oldstate;
1646 /* Performance counters */
1647 unsigned long long p1_start, p2_start;
1648 } runstate;
1649 struct pcpu_info *p;
1650 tsc_t pcpu_tsc;
1651
1652 /* Hardware tracking */
1653 struct {
1654 long long val;
1655 tsc_t start_time;
1656 struct cr3_value_struct *data;
1657 } cr3;
1658
1659 /* IPI latency tracking */
1660 struct vlapic_struct vlapic;
1661
1662 /* Summary info */
1663 struct cycle_framework f;
1664 struct cycle_summary runstates[RUNSTATE_MAX];
1665 struct cycle_summary runnable_states[RUNNABLE_STATE_MAX];
1666 struct cycle_summary cpu_affinity_all,
1667 cpu_affinity_pcpu[MAX_CPUS];
1668 enum {
1669 VCPU_DATA_NONE=0,
1670 VCPU_DATA_HVM,
1671 VCPU_DATA_PV
1672 } data_type;
1673 union {
1674 struct hvm_data hvm;
1675 struct pv_data pv;
1676 };
1677 };
1678
1679 enum {
1680 DOMAIN_RUNSTATE_BLOCKED=0,
1681 DOMAIN_RUNSTATE_PARTIAL_RUN,
1682 DOMAIN_RUNSTATE_FULL_RUN,
1683 DOMAIN_RUNSTATE_PARTIAL_CONTENTION,
1684 DOMAIN_RUNSTATE_CONCURRENCY_HAZARD,
1685 DOMAIN_RUNSTATE_FULL_CONTENTION,
1686 DOMAIN_RUNSTATE_LOST,
1687 DOMAIN_RUNSTATE_MAX
1688 };
1689
1690 const char * domain_runstate_name[] = {
1691 [DOMAIN_RUNSTATE_BLOCKED]="blocked",
1692 [DOMAIN_RUNSTATE_PARTIAL_RUN]="partial run",
1693 [DOMAIN_RUNSTATE_FULL_RUN]="full run",
1694 [DOMAIN_RUNSTATE_PARTIAL_CONTENTION]="partial contention",
1695 [DOMAIN_RUNSTATE_CONCURRENCY_HAZARD]="concurrency_hazard",
1696 [DOMAIN_RUNSTATE_FULL_CONTENTION]="full_contention",
1697 [DOMAIN_RUNSTATE_LOST]="lost",
1698 };
1699
1700 enum {
1701 POD_RECLAIM_CONTEXT_UNKNOWN=0,
1702 POD_RECLAIM_CONTEXT_FAULT,
1703 POD_RECLAIM_CONTEXT_BALLOON,
1704 POD_RECLAIM_CONTEXT_MAX
1705 };
1706
1707 const char * pod_reclaim_context_name[] = {
1708 [POD_RECLAIM_CONTEXT_UNKNOWN]="unknown",
1709 [POD_RECLAIM_CONTEXT_FAULT]="fault",
1710 [POD_RECLAIM_CONTEXT_BALLOON]="balloon",
1711 };
1712
1713 #define POD_ORDER_MAX 4
1714
1715 struct domain_data {
1716 struct domain_data *next;
1717 int did;
1718 struct vcpu_data *vcpu[MAX_CPUS];
1719
1720 int max_vid;
1721
1722 int runstate;
1723 tsc_t runstate_tsc;
1724 struct cycle_summary total_time;
1725 struct cycle_summary runstates[DOMAIN_RUNSTATE_MAX];
1726 struct cr3_value_struct *cr3_value_head;
1727 struct eip_list_struct *emulate_eip_list;
1728 struct eip_list_struct *interrupt_eip_list;
1729
1730 int guest_interrupt[GUEST_INTERRUPT_MAX+1];
1731 struct hvm_short_summary_struct hvm_short;
1732 struct {
1733 int done[MEM_MAX];
1734 int done_interval[MEM_MAX];
1735
1736 int done_for[MEM_MAX];
1737 int done_for_interval[MEM_MAX];
1738 } memops;
1739
1740 struct {
1741 int reclaim_order[POD_ORDER_MAX];
1742 int reclaim_context[POD_RECLAIM_CONTEXT_MAX];
1743 int reclaim_context_order[POD_RECLAIM_CONTEXT_MAX][POD_ORDER_MAX];
1744 /* FIXME: Do a full cycle summary */
1745 int populate_order[POD_ORDER_MAX];
1746 } pod;
1747 };
1748
1749 struct domain_data * domain_list=NULL;
1750
1751 struct domain_data default_domain;
1752
1753 enum {
1754 TOPLEVEL_GEN=0,
1755 TOPLEVEL_SCHED,
1756 TOPLEVEL_DOM0OP,
1757 TOPLEVEL_HVM,
1758 TOPLEVEL_MEM,
1759 TOPLEVEL_PV,
1760 TOPLEVEL_SHADOW,
1761 TOPLEVEL_HW,
1762 TOPLEVEL_MAX=TOPLEVEL_HW+1,
1763 };
1764
1765 const char * toplevel_name[TOPLEVEL_MAX] = {
1766 [TOPLEVEL_GEN]="gen",
1767 [TOPLEVEL_SCHED]="sched",
1768 [TOPLEVEL_DOM0OP]="dom0op",
1769 [TOPLEVEL_HVM]="hvm",
1770 [TOPLEVEL_MEM]="mem",
1771 [TOPLEVEL_PV]="pv",
1772 [TOPLEVEL_SHADOW]="shadow",
1773 [TOPLEVEL_HW]="hw",
1774 };
1775
1776 struct trace_volume {
1777 unsigned long long toplevel[TOPLEVEL_MAX];
1778 unsigned long long sched_verbose;
1779 unsigned long long hvm[HVM_VOL_MAX];
1780 } volume;
1781
1782 #define UPDATE_VOLUME(_p,_x,_s) \
1783 do { \
1784 (_p)->volume.total._x += _s; \
1785 (_p)->volume.last_buffer._x += _s; \
1786 } while(0)
1787
1788 void volume_clear(struct trace_volume *vol)
1789 {
1790 bzero(vol, sizeof(*vol));
1791 }
1792
1793 void volume_summary(struct trace_volume *vol)
1794 {
1795 int j, k;
1796 for(j=0; j<TOPLEVEL_MAX; j++)
1797 if(vol->toplevel[j]) {
1798 printf(" %-6s: %10lld\n",
1799 toplevel_name[j], vol->toplevel[j]);
1800 switch(j) {
1801 case TOPLEVEL_SCHED:
1802 if(vol->sched_verbose)
1803 printf(" +-verbose: %10lld\n",
1804 vol->sched_verbose);
1805 break;
1806 case TOPLEVEL_HVM:
1807 for(k=0; k<HVM_VOL_MAX; k++) {
1808 if(vol->hvm[k])
1809 printf(" +-%-7s: %10lld\n",
1810 hvm_vol_name[k], vol->hvm[k]);
1811 }
1812
1813 break;
1814 }
1815 }
1816 }
1817
1818 struct pcpu_info {
1819 /* Information about this pcpu */
1820 unsigned active:1, summary:1;
1821 int pid;
1822
1823 /* Information related to scanning thru the file */
1824 tsc_t first_tsc, last_tsc, order_tsc;
1825 off_t file_offset;
1826 off_t next_cpu_change_offset;
1827 struct record_info ri;
1828 int last_cpu_change_pid;
1829 int power_state;
1830
1831 /* Information related to tsc skew detection / correction */
1832 struct {
1833 tsc_t offset;
1834 cpu_mask_t downstream; /* To detect cycles in dependencies */
1835 } tsc_skew;
1836
1837 /* Information related to domain tracking */
1838 struct vcpu_data * current;
1839 struct {
1840 unsigned active:1,
1841 domain_valid:1,
1842 seen_valid_schedule:1; /* Seen an actual schedule since lost records */
1843 unsigned did:16,vid:16;
1844 tsc_t tsc;
1845 } lost_record;
1846
1847 /* Record volume */
1848 struct {
1849 tsc_t buffer_first_tsc,
1850 buffer_dom0_runstate_tsc,
1851 buffer_dom0_runstate_cycles[RUNSTATE_MAX];
1852 int buffer_dom0_runstate;
1853 unsigned buffer_size;
1854 struct trace_volume total, last_buffer;
1855 } volume;
1856
1857 /* Time report */
1858 struct {
1859 tsc_t tsc;
1860 struct cycle_summary idle, running, lost;
1861 } time;
1862 };
1863
1864 void __fill_in_record_info(struct pcpu_info *p);
1865
1866 #define INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX 10
1867
1868 struct {
1869 int max_active_pcpu;
1870 off_t last_epoch_offset;
1871 int early_eof;
1872 int lost_cpus;
1873 tsc_t now;
1874 struct cycle_framework f;
1875 tsc_t buffer_trace_virq_tsc;
1876 struct pcpu_info pcpu[MAX_CPUS];
1877
1878 struct {
1879 int id;
1880 /* Invariant: head null => tail null; head !null => tail valid */
1881 struct cr3_value_struct *head, **tail;
1882 } cr3;
1883
1884 struct {
1885 tsc_t start_tsc;
1886 /* Information about specific interval output types */
1887 union {
1888 struct {
1889 struct interval_element ** values;
1890 int count;
1891 } array;
1892 struct {
1893 struct interval_list *head, **tail;
1894 } list;
1895 struct cr3_value_struct *cr3;
1896 struct {
1897 struct domain_data *d;
1898 int guest_vector[INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX];
1899 } domain;
1900 };
1901 } interval;
1902 } P = { 0 };
1903
1904 /* Function prototypes */
1905 char * pcpu_string(int pcpu);
1906 void pcpu_string_draw(struct pcpu_info *p);
1907 void process_generic(struct record_info *ri);
1908 void dump_generic(FILE *f, struct record_info *ri);
1909 ssize_t __read_record(struct trace_record *rec, off_t offset);
1910 void error(enum error_level l, struct record_info *ri);
1911 void update_io_address(struct io_address ** list, unsigned int pa, int dir,
1912 tsc_t arc_cycles, unsigned int va);
1913 int check_extra_words(struct record_info *ri, int expected_size, const char *record);
1914 int vcpu_set_data_type(struct vcpu_data *v, int type);
1915
1916 void cpumask_init(cpu_mask_t *c) {
1917 *c = 0UL;
1918 }
1919
1920 void cpumask_clear(cpu_mask_t *c, int cpu) {
1921 *c &= ~(1UL << cpu);
1922 }
1923
1924 void cpumask_set(cpu_mask_t *c, int cpu) {
1925 *c |= (1UL << cpu);
1926 }
1927
1928 int cpumask_isset(const cpu_mask_t *c, int cpu) {
1929 if(*c & (1UL<<cpu))
1930 return 1;
1931 else
1932 return 0;
1933 }
1934
1935 void cpumask_union(cpu_mask_t *d, const cpu_mask_t *s) {
1936 *d |= *s;
1937 }
1938
1939 /* -- Time code -- */
1940
1941 void cycles_to_time(unsigned long long c, struct time_struct *t) {
1942 t->time = ((c - P.f.first_tsc) << 10) / opt.cpu_qhz;
1943 t->s = t->time / 1000000000;
1944 t->ns = t->time - (t->s * 1000000000);
1945 }
1946
1947 void abs_cycles_to_time(unsigned long long ac, struct time_struct *t) {
1948 if(ac > P.f.first_tsc) {
1949 /* t->time = ((ac - P.f.first_tsc) * 1000) / (opt.cpu_hz / 1000000 ); */
1950 /* t->s = t->time / 1000000000; */
1951 /* t->ns = t->time % 1000000000; */
1952 t->time = ((ac - P.f.first_tsc) << 10) / opt.cpu_qhz;
1953 t->s = t->time / 1000000000;
1954 t->ns = t->time - (t->s * 1000000000);
1955 } else {
1956 t->time = t->s = t->ns = 0;
1957 }
1958 }
1959
1960 tsc_t abs_cycles_to_global(unsigned long long ac) {
1961 if(ac > P.f.first_tsc)
1962 return ac - P.f.first_tsc;
1963 else
1964 return 0;
1965 }
1966
1967 void scatterplot_vs_time(tsc_t atsc, long long y) {
1968 struct time_struct t;
1969
1970 abs_cycles_to_time(atsc, &t);
1971
1972 printf("%u.%09u %lld\n", t.s, t.ns, y);
1973 }
1974
1975 /* -- Summary Code -- */
1976
1977 /* With compliments to "Numerical Recipes in C", which provided the algorithm
1978 * and basic template for this function. */
1979 long long percentile(long long * A, int N, int ple) {
1980 int I, J, L, R, K;
1981
1982 long long X, W;
1983
1984 /* No samples! */
1985 if ( N == 0 )
1986 return 0;
1987
1988 /* Find K, the element # we want */
1989 K=N*ple/100;
1990
1991 /* Set the left and right boundaries of the current search space */
1992 L=0; R=N-1;
1993
1994 while(L < R) {
1995 /* X: The value to order everything higher / lower than */
1996 X=A[K];
1997
1998 /* Starting at the left and the right... */
1999 I=L;
2000 J=R;
2001
2002 do {
2003 /* Find the first element on the left that is out-of-order w/ X */
2004 while(A[I]<X)
2005 I++;
2006 /* Find the first element on the right that is out-of-order w/ X */
2007 while(X<A[J])
2008 J--;
2009
2010 /* If we found something out-of-order */
2011 if(I<=J) {
2012 /* Switch the values */
2013 W=A[I];
2014 A[I]=A[J];
2015 A[J]=W;
2016
2017 /* And move on */
2018 I++; J--;
2019 }
2020 } while (I <= J); /* Keep going until our pointers meet or pass */
2021
2022 /* Re-adjust L and R, based on which element we're looking for */
2023 if(J<K)
2024 L=I;
2025 if(K<I)
2026 R=J;
2027 }
2028
2029 return A[K];
2030 }
2031
2032 float weighted_percentile(float * A, /* values */
2033 unsigned long long * w, /* weights */
2034 int N, /* total */
2035 int ple) /* percentile */
2036 {
2037 int L, R, I, J, K;
2038 unsigned long long L_weight, R_weight, I_weight, J_weight,
2039 K_weight, N_weight;
2040
2041 float X, t1;
2042 unsigned long long t2;
2043
2044 /* Calculate total weight */
2045 N_weight=0;
2046
2047 for(I=0; I<N; I++) {
2048 assert(w[I]!=0);
2049 N_weight += w[I];
2050 }
2051
2052 /* Find K_weight, the target weight we want */
2053 K_weight = N_weight * ple / 100;
2054
2055 /* Set the left and right boundaries of the current search space */
2056 L=0;
2057 L_weight = 0;
2058 R=N-1;
2059 R_weight = N_weight - w[R];
2060
2061 /* Search between L and R, narrowing down until we're done */
2062 while(L < R) {
2063 /* Chose an ordering value from right in the middle */
2064 K = (L + R) >> 1;
2065 /* X: The value to order everything higher / lower than */
2066 X=A[K];
2067
2068 /* Starting at the left and the right... */
2069 I=L; I_weight = L_weight;
2070 J=R; J_weight = R_weight;
2071
2072 do {
2073 /* Find the first element on the left that is out-of-order w/ X */
2074 while(A[I]<X) {
2075 I_weight += w[I];
2076 I++;
2077 }
2078 /* Find the first element on the right that is out-of-order w/ X */
2079 while(X<A[J]) {
2080 J_weight -= w[J];
2081 J--;
2082 }
2083
2084 /* If we actually found something... */
2085 if(I<=J) {
2086 /* Switch the values */
2087 t1=A[I];
2088 A[I]=A[J];
2089 A[J]=t1;
2090
2091 t2=w[I];
2092 w[I]=w[J];
2093 w[J]=t2;
2094
2095 /* And move in */
2096 I_weight += w[I];
2097 I++;
2098
2099 J_weight -= w[J];
2100 J--;
2101 }
2102 } while (I <= J); /* Keep going until our pointers meet or pass */
2103
2104 /* Re-adjust L and R, based on which element we're looking for */
2105 if(J_weight<K_weight) {
2106 L=I;
2107 L_weight = I_weight;
2108 }
2109 if(K_weight<I_weight) {
2110 R=J;
2111 R_weight = J_weight;
2112 }
2113 }
2114
2115 return A[L];
2116 }
2117
2118 long long self_weighted_percentile(long long * A,
2119 int N, /* total */
2120 int ple) /* percentile */
2121 {
2122 int L, R, I, J, K;
2123 long long L_weight, R_weight, I_weight, J_weight,
2124 K_weight, N_weight;
2125
2126 long long X, t1;
2127
2128 /* Calculate total weight */
2129 N_weight=0;
2130
2131 for(I=0; I<N; I++) {
2132 if(A[I] < 0)
2133 fprintf(warn, "%s: Value %lld less than zero!\n",
2134 __func__, A[I]);
2135 assert(A[I]!=0);
2136 N_weight += A[I];
2137 }
2138
2139 /* Find K_weight, the target weight we want */
2140 K_weight = N_weight * ple / 100;
2141
2142 /* Set the left and right boundaries of the current search space */
2143 L=0;
2144 L_weight = 0;
2145 R=N-1;
2146 R_weight = N_weight - A[R];
2147
2148 /* Search between L and R, narrowing down until we're done */
2149 while(L < R) {
2150 /* Chose an ordering value from right in the middle */
2151 K = (L + R) >> 1;
2152 /* X: The value to order everything higher / lower than */
2153 X=A[K];
2154
2155 /* Starting at the left and the right... */
2156 I=L; I_weight = L_weight;
2157 J=R; J_weight = R_weight;
2158
2159 do {
2160 /* Find the first element on the left that is out-of-order w/ X */
2161 while(A[I]<X) {
2162 I_weight += A[I];
2163 I++;
2164 }
2165 /* Find the first element on the right that is out-of-order w/ X */
2166 while(X<A[J]) {
2167 J_weight -= A[J];
2168 J--;
2169 }
2170
2171 /* If we actually found something... */
2172 if(I<=J) {
2173 /* Switch the values */
2174 t1=A[I];
2175 A[I]=A[J];
2176 A[J]=t1;
2177
2178 /* And move in */
2179 I_weight += A[I];
2180 I++;
2181
2182 J_weight -= A[J];
2183 J--;
2184 }
2185 } while (I <= J); /* Keep going until our pointers meet or pass */
2186
2187 /* Re-adjust L and R, based on which element we're looking for */
2188 if(J_weight<K_weight) {
2189 L=I;
2190 L_weight = I_weight;
2191 }
2192 if(K_weight<I_weight) {
2193 R=J;
2194 R_weight = J_weight;
2195 }
2196 }
2197
2198 return A[L];
2199 }
2200
2201 static inline double __cycles_percent(long long cycles, long long total) {
2202 return (double)(cycles*100) / total;
2203 }
2204
2205 static inline double __summary_percent(struct cycle_summary *s,
2206 struct cycle_framework *f) {
2207 return __cycles_percent(s->cycles, f->total_cycles);
2208 }
2209
2210 static inline double summary_percent_global(struct cycle_summary *s) {
2211 return __summary_percent(s, &P.f);
2212 }
2213
2214 static inline void update_cycles(struct cycle_summary *s, long long c) {
2215 s->event_count++;
2216
2217 if (!c)
2218 return;
2219
2220 if(opt.sample_size) {
2221 if (s->count >= s->sample_size
2222 && (s->count == 0
2223 || opt.sample_max == 0
2224 || s->sample_size < opt.sample_max)) {
2225 int new_size;
2226 void * new_sample = NULL;
2227
2228 new_size = s->sample_size << 1;
2229
2230 if (new_size == 0)
2231 new_size = opt.sample_size;
2232
2233 if (opt.sample_max != 0 && new_size > opt.sample_max)
2234 new_size = opt.sample_max;
2235
2236 new_sample = realloc(s->sample, sizeof(*s->sample) * new_size);
2237
2238 if (new_sample) {
2239 s->sample = new_sample;
2240 s->sample_size = new_size;
2241 }
2242 }
2243
2244 if (s->count < s->sample_size) {
2245 s->sample[s->count]=c;
2246 } else {
2247 /*
2248 * If we run out of space for samples, start taking only a
2249 * subset of samples.
2250 */
2251 int lap, index;
2252 lap = (s->count/s->sample_size)+1;
2253 index =s->count % s->sample_size;
2254 if((index - (lap/3))%lap == 0) {
2255 s->sample[index]=c;
2256 }
2257 }
2258 }
2259 s->count++;
2260 s->cycles += c;
2261
2262 s->interval.count++;
2263 s->interval.cycles += c;
2264 }
2265
2266 static inline void clear_interval_cycles(struct interval_element *e) {
2267 e->cycles = 0;
2268 e->count = 0;
2269 e->instructions = 0;
2270 }
2271
2272 static inline void print_cpu_affinity(struct cycle_summary *s, const char *p) {
2273 if(s->count) {
2274 long long avg;
2275
2276 avg = s->cycles / s->count;
2277
2278 if ( opt.sample_size ) {
2279 long long p5, p50, p95;
2280 int data_size = s->count;
2281 if(data_size > s->sample_size)
2282 data_size = s->sample_size;
2283
2284 p50 = percentile(s->sample, data_size, 50);
2285 p5 = percentile(s->sample, data_size, 5);
2286 p95 = percentile(s->sample, data_size, 95);
2287
2288 printf("%s: %7d %6lld {%6lld|%6lld|%6lld}\n",
2289 p, s->count, avg, p5, p50, p95);
2290 } else {
2291 printf("%s: %7d %6lld\n",
2292 p, s->count, avg);
2293 }
2294 }
2295 }
2296
2297 static inline void print_cycle_percent_summary(struct cycle_summary *s,
2298 tsc_t total, char *p) {
2299 if(s->count) {
2300 long long avg;
2301 double percent, seconds;
2302
2303 avg = s->cycles / s->count;
2304
2305 seconds = ((double)s->cycles) / opt.cpu_hz;
2306
2307 percent = ((double)(s->cycles * 100)) / total;
2308
2309 if ( opt.sample_size ) {
2310 long long p5, p50, p95;
2311 int data_size = s->count;
2312
2313 if(data_size > s->sample_size)
2314 data_size = s->sample_size;
2315
2316 p50 = self_weighted_percentile(s->sample, data_size, 50);
2317 p5 = self_weighted_percentile(s->sample, data_size, 5);
2318 p95 = self_weighted_percentile(s->sample, data_size, 95);
2319
2320 printf("%s: %7d %5.2lfs %5.2lf%% %6lld {%6lld|%6lld|%6lld}\n",
2321 p, s->count,
2322 seconds,
2323 percent,
2324 avg, p5, p50, p95);
2325 } else {
2326 printf("%s: %7d %5.2lfs %5.2lf%% %6lld\n",
2327 p, s->count,
2328 seconds,
2329 percent,
2330 avg);
2331 }
2332 }
2333 }
2334
2335 static inline void print_cycle_summary(struct cycle_summary *s,
2336 const char *p) {
2337 if(s->count) {
2338 long long avg;
2339
2340 avg = s->cycles / s->count;
2341
2342 if ( opt.sample_size ) {
2343 long long p5, p50, p95;
2344 int data_size = s->count;
2345
2346 if(data_size > s->sample_size)
2347 data_size = s->sample_size;
2348
2349 p50 = self_weighted_percentile(s->sample, data_size, 50);
2350 p5 = self_weighted_percentile(s->sample, data_size, 5);
2351 p95 = self_weighted_percentile(s->sample, data_size, 95);
2352
2353 printf("%s: %7d %5.2lfs %6lld {%6lld|%6lld|%6lld}\n",
2354 p, s->count, ((double)s->cycles)/opt.cpu_hz,
2355 avg, p5, p50, p95);
2356 } else {
2357 printf("%s: %7d %5.2lfs %6lld\n",
2358 p, s->count, ((double)s->cycles)/opt.cpu_hz, avg);
2359 }
2360 }
2361 }
2362
2363 #define PRINT_SUMMARY(_s, _p...) \
2364 do { \
2365 if((_s).event_count) { \
2366 if ( opt.sample_size ) { \
2367 unsigned long long p5, p50, p95; \
2368 int data_size=(_s).count; \
2369 if(data_size > (_s).sample_size) \
2370 data_size=(_s).sample_size; \
2371 p50=percentile((_s).sample, data_size, 50); \
2372 p5=percentile((_s).sample, data_size, 5); \
2373 p95=percentile((_s).sample, data_size, 95); \
2374 printf(_p); \
2375 printf(" %7d %5.2lfs %5.2lf%% %5lld cyc {%5lld|%5lld|%5lld}\n", \
2376 (_s).event_count, \
2377 ((double)(_s).cycles)/opt.cpu_hz, \
2378 summary_percent_global(&(_s)), \
2379 (_s).count ? (_s).cycles / (_s).count:0, \
2380 p5, p50, p95); \
2381 } else { \
2382 printf(_p); \
2383 printf(" %7d %5.2lfs %5.2lf%% %5lld cyc\n", \
2384 (_s).event_count, \
2385 ((double)(_s).cycles)/opt.cpu_hz, \
2386 summary_percent_global(&(_s)), \
2387 (_s).count ? (_s).cycles / (_s).count:0); \
2388 } \
2389 } \
2390 } while(0)
2391
2392 #define INTERVAL_DESC_MAX 31
2393 struct interval_list {
2394 struct interval_element *elem;
2395 struct interval_list *next;
2396 char desc[INTERVAL_DESC_MAX+1]; /* +1 for the null terminator */
2397 };
2398
2399 void __interval_cycle_percent_output(struct interval_element *e, tsc_t cycles) {
2400 printf(" %.02lf",
2401 __cycles_percent(e->cycles, cycles));
2402 clear_interval_cycles(e);
2403 }
2404
2405 void interval_cycle_percent_output(struct interval_element *e) {
2406 __interval_cycle_percent_output(e, opt.interval.cycles);
2407 }
2408
2409 void interval_time_output(void) {
2410 struct time_struct t;
2411 abs_cycles_to_time(P.interval.start_tsc, &t);
2412
2413 printf("%u.%09u", t.s, t.ns);
2414 }
2415
2416 void interval_table_output(void) {
2417 int i;
2418
2419 interval_time_output();
2420
2421 if(opt.interval.mode == INTERVAL_MODE_ARRAY) {
2422 for(i=0; i<P.interval.array.count; i++) {
2423 struct interval_element *e = P.interval.array.values[i];
2424 if(e) {
2425 interval_cycle_percent_output(e);
2426 } else {
2427 printf(" 0.0");
2428 }
2429 }
2430 } else if(opt.interval.mode == INTERVAL_MODE_LIST) {
2431 struct interval_list *p;
2432 for(p = P.interval.list.head; p; p = p->next)
2433 interval_cycle_percent_output(p->elem);
2434 }
2435 printf("\n");
2436 }
2437
2438 void interval_table_tail(void) {
2439 struct interval_list *p;
2440
2441 printf("time");
2442
2443 for(p=P.interval.list.head; p; p = p->next)
2444 printf(" %s", p->desc);
2445
2446 printf("\n");
2447 }
2448
2449 void interval_table_alloc(int count) {
2450 P.interval.array.count = count;
2451 P.interval.array.values = malloc(count * sizeof(struct interval_list *));
2452
2453 if(!P.interval.array.values) {
2454 fprintf(stderr, "Malloc failed!\n");
2455 error(ERR_SYSTEM, NULL);
2456 }
2457
2458 bzero(P.interval.array.values, count*sizeof(struct interval_list *));
2459 }
2460
2461 void interval_list_add(struct interval_element *e, char *desc) {
2462 struct interval_list *p;
2463
2464 fprintf(warn, "%s: Adding element '%s'\n", __func__, desc);
2465
2466 if((p=malloc(sizeof(*p)))==NULL) {
2467 fprintf(stderr, "malloc() failed.\n");
2468 error(ERR_SYSTEM, NULL);
2469 }
2470
2471 bzero(p, sizeof(*p));
2472
2473 p->elem = e;
2474 strncpy(p->desc, desc, INTERVAL_DESC_MAX);
2475
2476 p->next=NULL;
2477
2478 if(P.interval.list.head)
2479 *P.interval.list.tail = p;
2480 else
2481 P.interval.list.head = p;
2482 P.interval.list.tail = &p->next;
2483 }
2484
2485 void interval_cr3_schedule_time_header(void) {
2486 if( opt.interval.mode == INTERVAL_MODE_ARRAY ) {
2487 int i;
2488
2489 printf("time");
2490 for(i=0; i<opt.interval.array.count; i++) {
2491 printf(" %llx", opt.interval.array.values[i]);
2492 }
2493 printf("\n");
2494 }
2495 /* Can't see into the future, so no header if cr3 values are
2496 not specified. */
2497 }
2498
2499 void interval_cr3_value_check(struct cr3_value_struct *cr3) {
2500 if( opt.interval.mode == INTERVAL_MODE_ARRAY ) {
2501 int i;
2502
2503 for(i=0; i<opt.interval.array.count; i++) {
2504 if(cr3->gmfn == opt.interval.array.values[i]) {
2505 if(P.interval.array.values[i]) {
2506 fprintf(stderr, "Fatal: duplicate cr3 value %llx!\n",
2507 cr3->gmfn);
2508 error(ERR_ASSERT, NULL);
2509 }
2510 fprintf(stderr, "%s: found gmfn %llx\n",
2511 __func__, cr3->gmfn);
2512
2513 P.interval.array.values[i] = &cr3->total_time.interval;
2514 }
2515 }
2516 } else if(opt.interval.mode == INTERVAL_MODE_LIST) {
2517 char desc[32];
2518 snprintf(desc, 32, "%llx", cr3->gmfn);
2519 interval_list_add(&cr3->total_time.interval, desc);
2520 } else {
2521 /* Custom */
2522 if(cr3->gmfn == opt.interval.array.values[0])
2523 P.interval.cr3 = cr3;
2524 }
2525 }
2526
2527 int cr3_time_compare(const void *_a, const void *_b) {
2528 struct cr3_value_struct *a=*(typeof(&a))_a;
2529 struct cr3_value_struct *b=*(typeof(&a))_b;
2530
2531 if(a->total_time.interval.cycles < b->total_time.interval.cycles)
2532 return 1;
2533 else if(b->total_time.interval.cycles == a->total_time.interval.cycles) {
2534 if(a->total_time.interval.count < b->total_time.interval.count)
2535 return 1;
2536 else if(a->total_time.interval.count == b->total_time.interval.count)
2537 return 0;
2538 else
2539 return -1;
2540 } else
2541 return -1;
2542 }
2543
2544 void interval_cr3_schedule_ordered_output(void) {
2545 struct cr3_value_struct *p;
2546 int i;
2547
2548 struct cr3_value_struct **qsort_array;
2549 int N=0;
2550
2551 for(p=P.cr3.head; p; p=p->gnext)
2552 N++;
2553
2554 if(!N)
2555 return;
2556
2557 qsort_array = malloc(N * sizeof(struct eip_list_struct *));
2558
2559 for(i=0, p=P.cr3.head; p; p=p->gnext, i++)
2560 qsort_array[i]=p;
2561
2562 qsort(qsort_array, N, sizeof(struct eip_list_struct *),
2563 cr3_time_compare);
2564
2565 interval_time_output();
2566
2567 for(i=0; i<N; i++) {
2568 p = qsort_array[i];
2569 /* Rounding down means this will get ..1]% */
2570 if(p->total_time.interval.cycles > 0) {
2571 printf(" %8llx: %.02lf %c\n",
2572 p->gmfn,
2573 __cycles_percent(p->total_time.interval.cycles,
2574 opt.interval.cycles),
2575 (p->first_time > P.interval.start_tsc)?'*':' ');
2576 }
2577 clear_interval_cycles(&p->total_time.interval);
2578 }
2579
2580 free(qsort_array);
2581 }
2582
2583 void interval_cr3_short_summary_header(void) {
2584 int i;
2585
2586 printf("time guest");
2587 for(i=0; i<HVM_SHORT_SUMMARY_MAX; i++)
2588 printf(" %s", hvm_short_summary_name[i]);
2589 printf("\n");
2590 }
2591
2592 void interval_cr3_short_summary_output(void) {
2593 struct cycle_summary *hss_array;
2594 int i;
2595
2596 if(P.interval.cr3) {
2597 struct cr3_value_struct *p = P.interval.cr3;
2598
2599 interval_time_output();
2600
2601 hss_array = p->hvm.s;
2602
2603 printf(" %.02lf",
2604 __cycles_percent(p->total_time.interval.cycles,
2605 opt.interval.cycles));
2606
2607 for(i=0; i<HVM_SHORT_SUMMARY_MAX; i++)
2608 __interval_cycle_percent_output(&hss_array[i].interval,
2609 p->total_time.interval.cycles);
2610
2611 clear_interval_cycles(&p->total_time.interval);
2612
2613 printf("\n");
2614 }
2615 }
2616
2617 void interval_domain_value_check(struct domain_data *d) {
2618 if( opt.interval.mode == INTERVAL_MODE_ARRAY ) {
2619 int i;
2620
2621 for(i=0; i<opt.interval.array.count; i++) {
2622 if(d->did == opt.interval.array.values[i]) {
2623 if(P.interval.array.values[i]) {
2624 fprintf(stderr, "Fatal: duplicate domain value %d!\n",
2625 d->did);
2626 error(ERR_ASSERT, NULL);
2627 }
2628
2629 P.interval.array.values[i] = &d->total_time.interval;
2630 }
2631 }
2632 } else if(opt.interval.mode == INTERVAL_MODE_LIST) {
2633 char desc[32];
2634 snprintf(desc, 32, "%d", d->did);
2635 interval_list_add(&d->total_time.interval, desc);
2636 } else {
2637 if(d->did == opt.interval.array.values[0])
2638 P.interval.domain.d = d;
2639 }
2640 }
2641
2642 void interval_domain_short_summary_header(void) {
2643 int i;
2644
2645 printf("time running");
2646 for(i=0; i<HVM_SHORT_SUMMARY_MAX; i++)
2647 printf(" %s", hvm_short_summary_name[i]);
2648 printf("\n");
2649 }
2650
2651 void interval_domain_short_summary_output(void) {
2652
2653 if(P.interval.domain.d) {
2654 struct domain_data *d;
2655 int i;
2656
2657 d=P.interval.domain.d;
2658
2659 interval_time_output();
2660
2661 interval_cycle_percent_output(&d->total_time.interval);
2662
2663 for(i=0; i<HVM_SHORT_SUMMARY_MAX; i++)
2664 interval_cycle_percent_output(&d->hvm_short.s[i].interval);
2665
2666 printf("\n");
2667 }
2668 }
2669
2670 void interval_domain_guest_interrupt(struct hvm_data *h, int vector) {
2671 struct domain_data *d = h->v->d;
2672 int i;
2673
2674 /* Check to see if this vector is in the "print list" */
2675 for(i=0; i<INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX; i++) {
2676 if(P.interval.domain.guest_vector[i] == 0) {
2677 P.interval.domain.guest_vector[i] = vector;
2678 break;
2679 }
2680 if(P.interval.domain.guest_vector[i] == vector)
2681 break;
2682 }
2683
2684 if(i == INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX) {
2685 fprintf(stderr, "FATAL: used up all %d guest interrupt slots!\n",
2686 INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX);
2687 error(ERR_LIMIT, NULL);
2688 } else {
2689 d->guest_interrupt[vector]++;
2690 }
2691 }
2692
2693 void interval_domain_guest_interrupt_tail(void) {
2694 int i;
2695
2696 printf("time running");
2697 for(i=0; i<INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX; i++) {
2698 if(P.interval.domain.guest_vector[i] == 0)
2699 break;
2700 printf(" %d", P.interval.domain.guest_vector[i]);
2701 }
2702 printf("\n");
2703 }
2704
2705 void interval_domain_guest_interrupt_output(void) {
2706
2707 if(P.interval.domain.d) {
2708 struct domain_data *d;
2709 int i;
2710
2711 d=P.interval.domain.d;
2712
2713 interval_time_output();
2714
2715 for(i=0; i<INTERVAL_DOMAIN_GUEST_INTERRUPT_MAX; i++) {
2716 int v = P.interval.domain.guest_vector[i];
2717
2718 if(v == 0)
2719 break;
2720
2721 printf(" %d", d->guest_interrupt[v]);
2722
2723 d->guest_interrupt[v]=0;
2724 }
2725
2726 printf("\n");
2727 }
2728
2729 }
2730
2731 void interval_domain_grant_maps_output(void) {
2732
2733 if(P.interval.domain.d) {
2734 struct domain_data *d;
2735
2736 d=P.interval.domain.d;
2737
2738 interval_time_output();
2739
2740 printf(" %d", d->memops.done_for_interval[MEM_PAGE_GRANT_MAP]);
2741
2742 d->memops.done_for_interval[MEM_PAGE_GRANT_MAP] = 0;
2743
2744 printf("\n");
2745 }
2746 }
2747
2748 /* General interval gateways */
2749
2750 void interval_callback(void) {
2751 /* First, see if we're in generic mode. */
2752 switch(opt.interval.mode) {
2753 case INTERVAL_MODE_LIST:
2754 case INTERVAL_MODE_ARRAY:
2755 interval_table_output();
2756 return;
2757 default:
2758 break;
2759 }
2760
2761 switch(opt.interval.output) {
2762 case INTERVAL_CR3_SCHEDULE_ORDERED:
2763 interval_cr3_schedule_ordered_output();
2764 break;
2765 case INTERVAL_CR3_SHORT_SUMMARY:
2766 interval_cr3_short_summary_output();
2767 break;
2768 case INTERVAL_DOMAIN_SHORT_SUMMARY:
2769 interval_domain_short_summary_output();
2770 break;
2771 case INTERVAL_DOMAIN_GUEST_INTERRUPT:
2772 interval_domain_guest_interrupt_output();
2773 break;
2774 case INTERVAL_DOMAIN_GRANT_MAPS:
2775 interval_domain_grant_maps_output();
2776 break;
2777 default:
2778 break;
2779 }
2780 }
2781
2782 void interval_header(void) {
2783 switch(opt.interval.output) {
2784 case INTERVAL_CR3_SHORT_SUMMARY:
2785 interval_cr3_short_summary_header();
2786 break;
2787 case INTERVAL_DOMAIN_SHORT_SUMMARY:
2788 interval_domain_short_summary_header();
2789 break;
2790 default:
2791 break;
2792 }
2793 }
2794
2795 void interval_tail(void) {
2796 if(opt.interval.mode == INTERVAL_MODE_LIST) {
2797 interval_table_tail();
2798 return;
2799 }
2800
2801 switch(opt.interval.output) {
2802 case INTERVAL_DOMAIN_GUEST_INTERRUPT:
2803 interval_domain_guest_interrupt_tail();
2804 break;
2805 default:
2806 break;
2807 }
2808 }
2809
2810 /* -- Eip list data -- */
2811
2812 void update_eip(struct eip_list_struct **head, unsigned long long eip,
2813 unsigned long long cycles, int type, void * extra) {
2814 struct eip_list_struct *p, **last=head;
2815
2816 for(p=*head; p; last = (&p->next), p=p->next)
2817 if(p->eip >= eip)
2818 break;
2819
2820 if(!p || p->eip != eip) {
2821 p=malloc(sizeof(*p));
2822 if(!p) {
2823 perror("malloc failed");
2824 error(ERR_SYSTEM, NULL);
2825 }
2826
2827 bzero(p, sizeof(*p));
2828
2829 p->eip=eip;
2830 p->type = type;
2831
2832 if(eip_list_type[type].new) {
2833 eip_list_type[type].new(p, extra);
2834 }
2835 p->next = *last;
2836 *last=p;
2837 } else if(p->type != type) {
2838 fprintf(stderr, "WARNING, mixed types! %d %d\n", p->type, type);
2839 } else if(eip_list_type[type].update) {
2840 eip_list_type[type].update(p, extra);
2841 }
2842
2843 update_cycles(&p->summary, cycles);
2844 }
2845
2846 int eip_compare(const void *_a, const void *_b) {
2847 struct eip_list_struct *a=*(typeof(&a))_a;
2848 struct eip_list_struct *b=*(typeof(&a))_b;
2849
2850 if(a->summary.cycles < b->summary.cycles)
2851 return 1;
2852 else if(b->summary.cycles == a->summary.cycles) {
2853 if(a->summary.count < b->summary.count)
2854 return 1;
2855 else if(a->summary.count == b->summary.count)
2856 return 0;
2857 else
2858 return -1;
2859 } else
2860 return -1;
2861 }
2862
2863 void dump_eip(struct eip_list_struct *head) {
2864 struct eip_list_struct *p;
2865 int i;
2866 int total = 0;
2867
2868 struct eip_list_struct **qsort_array;
2869 int N=0;
2870
2871 for(p=head; p; p=p->next)
2872 {
2873 total += p->summary.event_count;
2874 N++;
2875 }
2876
2877 if(!N)
2878 return;
2879
2880 qsort_array = malloc(N * sizeof(struct eip_list_struct *));
2881
2882 for(i=0, p=head; p; p=p->next, i++)
2883 qsort_array[i]=p;
2884
2885 qsort(qsort_array, N, sizeof(struct eip_list_struct *),
2886 eip_compare);
2887
2888 /* WARNING: don't use N after this point unless you copy this variable */
2889 #if 0
2890 if(opt.summary_eip_limit && opt.summary_eip_limit < N)
2891 N=opt.summary_eip_limit;
2892 #endif
2893
2894 printf(" Total samples: %d\n", total);
2895
2896 for(i=0; i<N; i++) {
2897 p = qsort_array[i];
2898 if ( p->summary.cycles )
2899 PRINT_SUMMARY(p->summary, " %12llx%-45s: ",
2900 p->eip,
2901 find_symbol(p->eip));
2902 else
2903 {
2904 printf(" %12llx%-45s: ",
2905 p->eip,
2906 find_symbol(p->eip));
2907 printf(" %7d %5.2lf%%\n",
2908 p->summary.event_count,
2909 ((double)p->summary.event_count*100)/total);
2910 }
2911
2912
2913 if(eip_list_type[p->type].dump) {
2914 eip_list_type[p->type].dump(p);
2915 }
2916 }
2917
2918 free(qsort_array);
2919 }
2920
2921 /* -- HVM code -- */
2922 struct hvm_pf_xen_record {
2923 //unsigned vcpu:16, domain:16;
2924 union {
2925 struct {
2926 unsigned long long va;
2927 unsigned int error_code;
2928 } x64;
2929 struct {
2930 unsigned int va;
2931 unsigned int error_code;
2932 } x32;
2933 };
2934 };
2935
2936 void hvm_update_short_summary(struct hvm_data *h, int element) {
2937 struct vcpu_data *v = h->v;
2938
2939 if(v->cr3.data)
2940 update_cycles(&v->cr3.data->hvm.s[element], h->arc_cycles);
2941
2942 update_cycles(&v->d->hvm_short.s[element], h->arc_cycles);
2943
2944 h->short_summary_done=1;
2945 }
2946
2947 void hvm_short_summary(struct hvm_short_summary_struct *hss,
2948 tsc_t total, const char *prefix) {
2949 char desc[80];
2950 int i;
2951
2952 for(i=0; i<HVM_SHORT_SUMMARY_MAX; i++) {
2953 snprintf(desc, 80, "%s%s", prefix, hvm_short_summary_name[i]);
2954 print_cycle_percent_summary(hss->s + i, total, desc);
2955 }
2956 }
2957
2958 /* Wrapper to try to make sure this is only called once per
2959 * call site, rather than walking through the list each time */
2960 #define hvm_set_summary_handler(_h, _s, _d) \
2961 do { \
2962 static int done=0; \
2963 int ret; \
2964 if(!done) { \
2965 if ((ret=__hvm_set_summary_handler(_h, _s, _d))) \
2966 fprintf(stderr, "%s: hvm_set_summary_handler returned %d\n", \
2967 __func__, ret); \
2968 done=1; \
2969 } \
2970 } while(0)
2971
2972 int __hvm_set_summary_handler(struct hvm_data *h, void (*s)(struct hvm_data *h, void*d), void*d) {
2973 /* Set summary handler */
2974 if(h->exit_reason < h->exit_reason_max)
2975 {
2976 struct hvm_summary_handler_node *p, **q;
2977
2978 /* Find the end of the list, checking to make sure there are no
2979 * duplicates along the way */
2980 q=&h->exit_reason_summary_handler_list[h->exit_reason];
2981 p = *q;
2982 while(p)
2983 {
2984 if(p->handler == s && p->data == d)
2985 {
2986 fprintf(stderr, "%s: Unexpected duplicate handler %p,%p\n",
2987 __func__, s, d);
2988 error(ERR_STRICT, NULL);
2989 return -EBUSY;
2990 }
2991 q=&p->next;
2992 p=*q;
2993 }
2994
2995 assert(p==NULL);
2996
2997 /* Insert the new handler */
2998 p=malloc(sizeof(*p));
2999 if (!p) {
3000 fprintf(stderr, "%s: Malloc failed!\n", __func__);
3001 error(ERR_SYSTEM, NULL);
3002 }
3003 p->handler=s;
3004 p->data = d;
3005 p->next=*q;
3006 *q=p;
3007 return 0;
3008 }
3009 return -EINVAL;
3010 }
3011
3012 void hvm_generic_postprocess(struct hvm_data *h);
3013
3014 static int hvm_set_postprocess(struct hvm_data *h, void (*s)(struct hvm_data *h))
3015 {
3016 if ( h->post_process == NULL
3017 || h->post_process == hvm_generic_postprocess )
3018 {
3019 h->post_process = s;
3020 return 0;
3021 }
3022 else
3023 return 1;
3024 }
3025
3026 #define SIGN_EXTENDED_BITS (~((1ULL<<48)-1))
3027 #define HIGH_BIT(_v) ((_v) & (1ULL<<47))
3028 static inline int is_valid_addr64(unsigned long long va)
3029 {
3030 if(HIGH_BIT(va))
3031 return ((va & SIGN_EXTENDED_BITS) == SIGN_EXTENDED_BITS);
3032 else
3033 return ((va & SIGN_EXTENDED_BITS) == 0);
3034 }
3035
3036 void hvm_pf_xen_summary(struct hvm_data *h, void *d) {
3037 int i,j, k;
3038
3039 printf(" page_fault\n");
3040 for(i=0; i<PF_XEN_MAX; i++)
3041 {
3042 if( pf_xen_name[i] )
3043 {
3044 PRINT_SUMMARY(h->summary.pf_xen[i],
3045 " %-25s ", pf_xen_name[i]);
3046 }
3047 else
3048 {
3049 PRINT_SUMMARY(h->summary.pf_xen[i],
3050 " [%23d] ", i);
3051 }
3052 switch(i){
3053 case PF_XEN_NON_EMULATE:
3054 for(j=0; j<PF_XEN_NON_EMUL_MAX; j++)
3055 PRINT_SUMMARY(h->summary.pf_xen_non_emul[j],
3056 " *%-13s ", pf_xen_non_emul_name[j]);
3057 break;
3058 case PF_XEN_EMULATE:
3059 for(j=0; j<PF_XEN_EMUL_MAX; j++) {
3060 PRINT_SUMMARY(h->summary.pf_xen_emul[j],
3061 " *%-13s ", pf_xen_emul_name[j]);
3062 if(j == PF_XEN_EMUL_EARLY_UNSHADOW) {
3063 int k;
3064 for(k=0; k<5; k++) {
3065 PRINT_SUMMARY(h->summary.pf_xen_emul_early_unshadow[k],
3066 " +[%d] ", k);
3067 }
3068 }
3069 }
3070 break;
3071 case PF_XEN_FIXUP:
3072 for(j=0; j<PF_XEN_FIXUP_MAX; j++) {
3073 PRINT_SUMMARY(h->summary.pf_xen_fixup[j],
3074 " *%-13s ", pf_xen_fixup_name[j]);
3075 if(j == PF_XEN_FIXUP_UNSYNC ) {
3076 for(k=0; k<PF_XEN_FIXUP_UNSYNC_RESYNC_MAX; k++) {
3077 PRINT_SUMMARY(h->summary.pf_xen_fixup_unsync_resync[k],
3078 " +[%3d] ", k);
3079 }
3080 PRINT_SUMMARY(h->summary.pf_xen_fixup_unsync_resync[k],
3081 " +[max] ");
3082 }
3083 }
3084 break;
3085 }
3086 }
3087 }
3088
3089 void pf_preprocess(struct pf_xen_extra *e, int guest_paging_levels)
3090 {
3091 switch(guest_paging_levels) {
3092 /* Select a subfield of _bits bits starting at bit _shift from _x */
3093 #define _SUBFIELD(_bits, _shift, _x) \
3094 (((_x)>>(_shift)) & ((1ULL<<(_bits))-1))
3095 case 4:
3096 /* Verify sign-extension */
3097 if((HIGH_BIT(e->va)
3098 &&((e->va & SIGN_EXTENDED_BITS) != SIGN_EXTENDED_BITS))
3099 || (!HIGH_BIT(e->va)
3100 && ((e->va & SIGN_EXTENDED_BITS) != 0))) {
3101 fprintf(warn, "Strange, va %llx not properly sign extended for 4-level pagetables\n",
3102 e->va);
3103 }
3104 e->pt_index[4]=_SUBFIELD(9,39,e->va);
3105 e->pt_index[3]=_SUBFIELD(9,30,e->va);
3106 e->pt_index[2]=_SUBFIELD(9,21,e->va);
3107 e->pt_index[1]=_SUBFIELD(9,12,e->va);
3108 /* These are only useful for the linear-pagetable code */
3109 e->pt_index[0]=_SUBFIELD(9,3,e->va);
3110 if(e->va & 0x4)
3111 e->pt_is_lo=0;
3112 break;
3113 case 3:
3114 e->pt_index[3]=_SUBFIELD(2,30,e->va);
3115 e->pt_index[2]=_SUBFIELD(9,21,e->va);
3116 e->pt_index[1]=_SUBFIELD(9,12,e->va);
3117 /* These are only useful for the linear-pagetable code */
3118 e->pt_index[0]=_SUBFIELD(9,3,e->va);
3119 if(e->va & 0x4)
3120 e->pt_is_lo=0;
3121 break;
3122 case 2:
3123 e->pt_index[2]=_SUBFIELD(10,22,e->va);
3124 e->pt_index[1]=_SUBFIELD(10,12,e->va);
3125 /* This is only useful for the linear pagetable code */
3126 e->pt_index[0]=_SUBFIELD(10,2,e->va);
3127 break;
3128 case 0:
3129 break;
3130 default:
3131 fprintf(warn, "Don't know how to handle %d-level pagetables\n",
3132 guest_paging_levels);
3133 }
3134
3135 e->corresponding_va = CORR_VA_INVALID;
3136 e->pt_level = 0;
3137
3138 /* Detect accesses to Windows linear pagetables */
3139 switch(guest_paging_levels)
3140 {
3141 case 2:
3142 if(e->pt_index[2] == 768) {
3143 if(e->pt_index[1] == 768)
3144 {
3145 e->pt_level = 2;
3146 e->corresponding_va=((1UL<<22)-1)
3147 | e->pt_index[0]<<22;
3148 }
3149 else
3150 {
3151 e->pt_level = 1;
3152 e->corresponding_va = ((1UL<<12)-1)
3153 | e->pt_index[1]<<22
3154 | e->pt_index[0]<<12;
3155 }
3156 }
3157 break;
3158 case 3:
3159 if(e->pt_index[3]==3 && (e->pt_index[2]>>2==0))
3160 {
3161 if(e->pt_index[2]==3 && e->pt_index[1]>>2==0)
3162 {
3163 if(e->pt_index[1] == 3 && e->pt_index[0]>>2==0)
3164 {
3165 e->pt_level = 3;
3166 e->corresponding_va=((1UL<<30)-1)
3167 | e->pt_index[0]<<30;
3168 }
3169 else
3170 {
3171 e->pt_level = 2;
3172 e->corresponding_va=((1UL<<21)-1)
3173 | e->pt_index[1]<<30
3174 | e->pt_index[2]<<21;
3175 }
3176 }
3177 else
3178 {
3179 e->pt_level = 1;
3180 e->corresponding_va = ((1UL<<12)-1)
3181 | e->pt_index[0]<<12
3182 | e->pt_index[1]<<21
3183 | e->pt_index[2]<<30;
3184 }
3185 }
3186 break;
3187 case 4:
3188 if(e->pt_index[4] == 0x1ed)
3189 {
3190 if(e->pt_index[3] == 0x1ed)
3191 {
3192 if(e->pt_index[2] == 0x1ed)
3193 {
3194 if(e->pt_index[1] == 0x1ed)
3195 {
3196 e->pt_level = 4;
3197 e->corresponding_va = ((1ULL<<39)-1)
3198 | (unsigned long long)e->pt_index[0]<<39;
3199 }
3200 else
3201 {
3202 e->pt_level = 3;
3203 e->corresponding_va = ((1ULL<<30)-1)
3204 | (unsigned long long)e->pt_index[0]<<30
3205 | (unsigned long long)e->pt_index[1]<<39;
3206 }
3207 }
3208 else
3209 {
3210 e->pt_level = 2;
3211 e->corresponding_va = ((1ULL<<21)-1)
3212 | (unsigned long long)e->pt_index[0]<<21
3213 | (unsigned long long)e->pt_index[1]<<30
3214 | (unsigned long long)e->pt_index[2]<<39;
3215 }
3216 }
3217 else
3218 {
3219 e->pt_level = 1;
3220 e->corresponding_va = ((1ULL<<12)-1)
3221 | (unsigned long long)e->pt_index[0]<<12
3222 | (unsigned long long)e->pt_index[1]<<21
3223 | (unsigned long long)e->pt_index[2]<<30
3224 | (unsigned long long)e->pt_index[3]<<39;
3225 }
3226
3227 if(HIGH_BIT(e->corresponding_va))
3228 e->corresponding_va |= SIGN_EXTENDED_BITS;
3229 }
3230 break;
3231 default:
3232 break;
3233 }
3234 }
3235
3236 void hvm_pf_xen_preprocess(unsigned event, struct hvm_data *h) {
3237 struct pf_xen_extra *e = &h->inflight.pf_xen;
3238 struct mmio_info *m = &h->inflight.mmio;
3239 struct hvm_pf_xen_record *r = (typeof(r))h->d;
3240
3241 if(event == TRC_HVM_PF_XEN64)
3242 {
3243 if(!is_valid_addr64(r->x64.va))
3244 fprintf(warn, "%s: invalid va %llx",
3245 __func__, r->x64.va);
3246 e->va = r->x64.va;
3247 e->error_code = r->x64.error_code;
3248 }
3249 else
3250 {
3251 e->va = r->x32.va;
3252 e->error_code = r->x32.error_code;
3253 }
3254
3255 if(m->data_valid)
3256 e->pf_case = PF_XEN_MMIO;
3257 else
3258 {
3259 pf_preprocess(e, h->v->guest_paging_levels);
3260
3261 /* On rio traces, we try to infer emulation by looking for accesses
3262 in the linear pagetable */
3263 if(e->pt_level > 0)
3264 e->pf_case = PF_XEN_EMULATE;
3265 else
3266 e->pf_case = PF_XEN_NON_EMULATE;
3267 }
3268 }
3269
3270 static inline int is_kernel(int paging_levels, unsigned long long va) {
3271 switch(paging_levels) {
3272 case 2:
3273 case 3:
3274 if(va & 0x80000000)
3275 return 1;
3276 else
3277 return 0;
3278 break;
3279 case 4:
3280 if(HIGH_BIT(va))
3281 return 1;
3282 else return 0;
3283 default:
3284 return 0;
3285 }
3286
3287 }
3288
3289 void hvm_pf_xen_postprocess(struct hvm_data *h) {
3290 struct pf_xen_extra *e = &h->inflight.pf_xen;
3291
3292 if(opt.summary_info) {
3293 if(e->pf_case)
3294 update_cycles(&h->summary.pf_xen[e->pf_case],
3295 h->arc_cycles);
3296 else
3297 fprintf(warn, "Strange, pf_case 0!\n");
3298 switch(e->pf_case)
3299 {
3300 case PF_XEN_EMULATE:
3301 update_eip(&h->v->d->emulate_eip_list,
3302 h->rip,
3303 h->arc_cycles,
3304 0, NULL);
3305 break;
3306 case PF_XEN_NON_EMULATE:
3307 if(is_kernel(h->v->guest_paging_levels, h->rip))
3308 update_cycles(&h->summary.pf_xen_non_emul[PF_XEN_NON_EMUL_EIP_KERNEL],
3309 h->arc_cycles);
3310 else
3311 update_cycles(&h->summary.pf_xen_non_emul[PF_XEN_NON_EMUL_EIP_USER],
3312 h->arc_cycles);
3313 if(is_kernel(h->v->guest_paging_levels, e->va))
3314 update_cycles(&h->summary.pf_xen_non_emul[PF_XEN_NON_EMUL_VA_KERNEL],
3315 h->arc_cycles);
3316
3317 else
3318 update_cycles(&h->summary.pf_xen_non_emul[PF_XEN_NON_EMUL_VA_USER],
3319 h->arc_cycles);
3320 }
3321
3322 /* Set summary handler */
3323 hvm_set_summary_handler(h, hvm_pf_xen_summary, NULL);
3324 }
3325 }
3326
3327 void hvm_pf_xen_process(struct record_info *ri, struct hvm_data *h) {
3328 struct pf_xen_extra *e = &h->inflight.pf_xen;
3329
3330 if(ri->event == TRC_HVM_PF_XEN64
3331 && h->v->guest_paging_levels != 4)
3332 fprintf(warn, "Strange, PF_XEN64 but guest_paging_levels %d!\n",
3333 h->v->guest_paging_levels);
3334 else if(ri->event == TRC_HVM_PF_XEN
3335 && h->v->guest_paging_levels == 4)
3336 fprintf(warn, "Strange, PF_XEN but guest_paging_levels %d!\n",
3337 h->v->guest_paging_levels);
3338
3339 hvm_pf_xen_preprocess(ri->event, h);
3340
3341 if(opt.dump_all)
3342 {
3343 if(e->pf_case == PF_XEN_EMULATE)
3344 printf("]%s pf_xen:emulate va %llx ec %x level %d corr %llx e->pt_index[%d %d %d %d %d]\n",
3345 ri->dump_header, e->va, e->error_code,
3346 e->pt_level, e->corresponding_va,
3347 e->pt_index[0], e->pt_index[1], e->pt_index[2],
3348 e->pt_index[3],
3349 e->pt_index[4]);
3350 else
3351 printf("]%s pf_xen va %llx ec %x e->pt_index[%d %d %d %d %d]\n",
3352 ri->dump_header, e->va, e->error_code,
3353 e->pt_index[0], e->pt_index[1], e->pt_index[2],
3354 e->pt_index[3],
3355 e->pt_index[4]);
3356 }
3357
3358 if ( hvm_set_postprocess(h, hvm_pf_xen_postprocess) )
3359 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
3360 }
3361
3362 const char * hvm_vlapic_icr_dest_shorthand_name[4] = {
3363 "dest_field", "self", "all-inc", "all-exc"
3364 };
3365
3366 void hvm_vlapic_vmentry_cleanup(struct vcpu_data *v, tsc_t tsc)
3367 {
3368 int i;
3369
3370 struct vlapic_struct *vla = &v->vlapic;
3371
3372 for(i=0; i<MAX_VLAPIC_LIST; i++)
3373 {
3374 unsigned long long lat=0;
3375 struct outstanding_ipi *o = vla->outstanding.list + i;
3376
3377 if(!(o->valid && o->injected))
3378 continue;
3379
3380 if(tsc >= o->first_tsc)
3381 lat = tsc - o->first_tsc;
3382 else
3383 fprintf(warn, "Strange, vec %d first_tsc %lld > ri->tsc %lld!\n",
3384 o->vec, o->first_tsc, tsc);
3385
3386 if(opt.dump_ipi_latency
3387 || (opt.dump_all && o->count > 1)) {
3388 struct time_struct t;
3389 cycles_to_time(lat, &t);
3390 printf(" [vla] d%dv%d vec %d ipis %d, latency %lld (%u.%09u s)\n",
3391 v->d->did, v->vid, o->vec, o->count, lat,
3392 t.s, t.ns);
3393 }
3394
3395 #if 0
3396 /* FIXME: make general somehow */
3397 if(opt.summary_info)
3398 {
3399 update_cycles(&h->summary.ipi_latency, lat);
3400 h->summary.ipi_count[vla->outstanding_ipis]++;
3401 }
3402 #endif
3403
3404 o->vec = o->count = o->injected = o->valid = o->first_tsc = 0;
3405 }
3406 }
3407
3408 void hvm_vlapic_clear(struct vlapic_struct *vla)
3409 {
3410 bzero(vla, sizeof(*vla));
3411 }
3412
3413 struct outstanding_ipi *find_vec(struct vlapic_struct *vla, int vec)
3414 {
3415 struct outstanding_ipi *o = NULL;
3416 int i;
3417
3418 /* Find the entry for this vector, or the first empty one. */
3419 for(i=0; i<MAX_VLAPIC_LIST; i++)
3420 {
3421 if(vla->outstanding.list[i].valid && vla->outstanding.list[i].vec == vec)
3422 {
3423 o = vla->outstanding.list + i;
3424 break;
3425 } else if(!vla->outstanding.list[i].valid && !o)
3426 o = vla->outstanding.list + i;
3427 }
3428
3429 if(o && !o->valid) {
3430 o->vec = vec;
3431 o->valid = 1;
3432 }
3433
3434 return o;
3435 }
3436
3437 void ipi_send(struct vcpu_data *ov, int vec)
3438 {
3439 struct vlapic_struct *vla;
3440 struct outstanding_ipi *o = NULL;
3441
3442 if(ov->runstate.state == RUNSTATE_LOST) {
3443 if(opt.dump_all)
3444 fprintf(warn, "%s: v%d in state RUNSTATE_LOST, not counting ipi\n",
3445 __func__, ov->vid);
3446 return;
3447 }
3448
3449 vla = &ov->vlapic;
3450
3451 o = find_vec(vla, vec);
3452
3453 if(!o)
3454 {
3455 fprintf(warn, "%s: Couldn't find an open slot!\n",
3456 __func__);
3457 return;
3458 }
3459
3460 if(!o->first_tsc)
3461 o->first_tsc = P.now;
3462
3463 if(opt.dump_all && o->count == 0 && o->injected)
3464 printf(" [vla] Pre-injection\n");
3465
3466 o->count++;
3467
3468 if((opt.dump_all)
3469 #if 0
3470 && (ov->runstate.state != RUNSTATE_RUNNING
3471 || ov->hvm.vmexit_valid)
3472 #endif
3473 )
3474 printf(" [vla] d%dv%d vec %d state %s (outstanding ipis %d)\n",
3475 ov->d->did, ov->vid,
3476 o->vec,
3477 runstate_name[ov->runstate.state],
3478 o->count);
3479 }
3480
3481 void hvm_vlapic_icr_handler(struct hvm_data *h)
3482 {
3483 struct mmio_info *m = &h->inflight.mmio;
3484 union {
3485 unsigned int val;
3486 struct {
3487 unsigned vec:8,
3488 delivery_mode:3,
3489 dest_mode:1,
3490 delivery_status:1,
3491 _res1:1,
3492 level:1,
3493 trigger:1,
3494 _res2:2,
3495 dest_shorthand:2;
3496 };
3497 } icr = { .val = m->data };
3498
3499 if(m->is_write) {
3500 if(opt.dump_all) {
3501 printf(" [vla] d%dv%d icr vec %d %s\n",
3502 h->v->d->did, h->v->vid,
3503 icr.vec,
3504 hvm_vlapic_icr_dest_shorthand_name[icr.dest_shorthand]);
3505 }
3506
3507 if(icr.dest_shorthand == 3)
3508 {
3509 struct vcpu_data *ov, *v = h->v;
3510 struct domain_data *d = v->d;
3511 int i;
3512
3513 for(i=0; i<MAX_CPUS; i++)
3514 {
3515 ov = d->vcpu[i];
3516 if(!ov || ov == v)
3517 continue;
3518
3519 ipi_send(ov, icr.vec);
3520
3521 }
3522 } else if(icr.dest_shorthand != 1) {
3523 #if 0
3524 fprintf(warn, "Strange, vlapic icr %s vec %d!\n",
3525 hvm_vlapic_icr_dest_shorthand_name[icr.dest_shorthand],
3526 icr.vec);
3527 #endif
3528 }
3529 } else {
3530 /* Read */
3531 if(opt.dump_all) {
3532 printf(" [vla] d%dv%d icr status %s\n",
3533 h->v->d->did, h->v->vid,
3534 icr.delivery_status?"pending":"idle");
3535 }
3536 }
3537
3538 }
3539
3540 void hvm_vlapic_inject(struct vcpu_data *v, int vec)
3541 {
3542 struct vlapic_struct *vla = &v->vlapic;
3543 struct outstanding_ipi *o = NULL;
3544
3545 o = find_vec(vla, vec);
3546
3547 if(o) {
3548 if(opt.dump_all)
3549 printf(" [vla] d%dv%d vec %d injecting\n",
3550 v->d->did, v->vid, vec);
3551 o->injected=1;
3552 } else {
3553 fprintf(stderr, "%s: Couldn't find an open ipi slot!\n",
3554 __func__);
3555 }
3556 }
3557
3558 void hvm_vlapic_eoi_handler(struct hvm_data *h) {
3559 if(opt.dump_all)
3560 printf(" [vla] d%dv%d eoi\n",
3561 h->v->d->did, h->v->vid);
3562 }
3563
3564 void hvm_vlapic_handler(struct hvm_data *h)
3565 {
3566 struct mmio_info *m = &h->inflight.mmio;
3567
3568 switch(m->gpa) {
3569 case 0xfee00300:
3570 hvm_vlapic_icr_handler(h);
3571 break;
3572 case 0xfee000b0:
3573 hvm_vlapic_eoi_handler(h);
3574 break;
3575 }
3576
3577 }
3578
3579 /* Also called by shadow_mmio_postprocess */
3580 #define MMIO_VGA_START (0xa0000)
3581 #define MMIO_VGA_END (0xbffff)
3582 void enumerate_mmio(struct hvm_data *h)
3583 {
3584 struct mmio_info *m = &h->inflight.mmio;
3585
3586 /* Skip vga area */
3587 if ( opt.mmio_enumeration_skip_vga
3588 && m->gpa >= MMIO_VGA_START
3589 && m->gpa < MMIO_VGA_END)
3590 {
3591 warn_once("WARNING: Not enumerationg MMIO in VGA range. Use --mmio-enumeration-skip-vga=0 to override.\n");
3592 return;
3593 }
3594
3595 if ( m->data_valid )
3596 update_io_address(&h->summary.io.mmio, m->gpa, m->is_write, h->arc_cycles, m->va);
3597 }
3598
3599 void hvm_mmio_summary(struct hvm_data *h, void *data)
3600 {
3601 long reason=(long)data;
3602
3603 PRINT_SUMMARY(h->summary.mmio[reason],
3604 " mmio ");
3605 }
3606
3607 void hvm_mmio_assist_postprocess(struct hvm_data *h)
3608 {
3609 long reason;
3610
3611 switch(h->exit_reason)
3612 {
3613 case VMEXIT_NPF:
3614 case EXIT_REASON_EPT_VIOLATION:
3615 reason=NONPF_MMIO_NPF;
3616 hvm_set_summary_handler(h, hvm_mmio_summary, (void *)reason);
3617 break;
3618 case EXIT_REASON_APIC_ACCESS:
3619 reason=NONPF_MMIO_APIC;
3620 hvm_set_summary_handler(h, hvm_mmio_summary, (void *)reason);
3621 break;
3622 default:
3623 {
3624 static int warned = 0;
3625 if (!warned)
3626 {
3627 fprintf(warn, "%s: Strange, MMIO with unexpected exit reason %d\n",
3628 __func__, h->exit_reason);
3629 warned=1;
3630 }
3631 reason=NONPF_MMIO_UNKNOWN;
3632 hvm_set_summary_handler(h, hvm_mmio_summary, (void *)reason);
3633 break;
3634 }
3635 }
3636
3637 if(opt.summary_info)
3638 {
3639 update_cycles(&h->summary.mmio[reason],
3640 h->arc_cycles);
3641 }
3642
3643 if ( opt.with_mmio_enumeration )
3644 enumerate_mmio(h);
3645 }
3646
3647 #define HVM_IO_ASSIST_WRITE 0x200
3648 void hvm_mmio_assist_process(struct record_info *ri, struct hvm_data *h)
3649 {
3650 struct mmio_info *m = &h->inflight.mmio;
3651 union {
3652 struct {
3653 unsigned int gpa;
3654 unsigned int data;
3655 } x32;
3656 struct {
3657 unsigned long long gpa;
3658 unsigned int data;
3659 } x64;
3660 } *r = (typeof(r))h->d;
3661
3662 union {
3663 unsigned event;
3664 struct {
3665 unsigned minor:8,
3666 x64:1,
3667 write:2;
3668 };
3669 } mevt = { .event = ri->event };
3670
3671 if(mevt.x64) {
3672 m->gpa = r->x64.gpa;
3673 m->data = r->x64.data;
3674 if(ri->extra_words*(sizeof(unsigned int))==sizeof(r->x64))
3675 m->data_valid=1;
3676 } else {
3677 m->gpa = r->x32.gpa;
3678 m->data = r->x32.data;
3679 if(ri->extra_words*(sizeof(unsigned int))==sizeof(r->x32))
3680 m->data_valid=1;
3681 }
3682
3683 m->is_write = mevt.write;
3684
3685 if(opt.dump_all)
3686 {
3687 if(m->data_valid)
3688 printf("]%s mmio_assist %c gpa %llx data %x\n",
3689 ri->dump_header,
3690 mevt.write?'w':'r',
3691 m->gpa, m->data);
3692 else
3693 printf("]%s mmio_assist %c gpa %llx (no data)\n", ri->dump_header,
3694 mevt.write?'w':'r', m->gpa);
3695 }
3696
3697 if((m->gpa & 0xfffff000) == 0xfee00000)
3698 hvm_vlapic_handler(h);
3699
3700 /* Catch MMIOs that don't go through the shadow code; tolerate
3701 * failures to set (probably shadow_mmio) */
3702 hvm_set_postprocess(h, hvm_mmio_assist_postprocess);
3703 }
3704
3705 void hvm_inj_virq_process(struct record_info *ri, struct hvm_data *h) {
3706 struct {
3707 int vector, fake;
3708 } *r = (typeof(r))h->d;
3709
3710 if(opt.dump_all) {
3711 printf(" %s inj_virq vec %u %s\n",
3712 ri->dump_header,
3713 r->vector, r->fake?"fake":"real");
3714 }
3715
3716 if(opt.summary_info)
3717 {
3718 int vector = r->vector;
3719
3720 if(vector >= GUEST_INTERRUPT_MAX)
3721 vector = GUEST_INTERRUPT_MAX;
3722 h->summary.guest_interrupt[vector].count++;
3723
3724 if(opt.interval.output == INTERVAL_DOMAIN_GUEST_INTERRUPT)
3725 interval_domain_guest_interrupt(h, vector);
3726 }
3727
3728 /* If we're waking, make this the wake vector */
3729 if(r->vector < GUEST_INTERRUPT_MAX ) {
3730 int vector = r->vector;
3731 if ( h->w2h.waking && h->w2h.vector == 0 ) {
3732 if(h->summary.guest_interrupt[vector].start_tsc) {
3733 fprintf(warn, "Strange, d%dv%d waking && wake_vector 0 but vec %d start_tsc %lld!\n",
3734 h->v->d->did, h->v->vid,
3735 vector,
3736 h->summary.guest_interrupt[vector].start_tsc);
3737 error(ERR_WARN, NULL);
3738 }
3739 if(h->w2h.interrupts)
3740 fprintf(warn, "Strange, waking && wake_vector 0 but interrupts_this_wait_to_halt %d!\n",
3741 h->w2h.interrupts);
3742
3743 if(opt.dump_all)
3744 printf(" [w2h] d%dv%d Setting wake_vector %d\n",
3745 h->v->d->did, h->v->vid, vector);
3746
3747 /* In svm mode, vector information is invalid */
3748 if ( opt.svm_mode && r->fake )
3749 h->w2h.vector = FAKE_VECTOR;
3750 else
3751 h->w2h.vector = vector;
3752 h->summary.guest_interrupt[vector].is_wake = 1;
3753 }
3754
3755 if( h->summary.guest_interrupt[vector].start_tsc == 0 ) {
3756 /* Note that we want start_tsc set at the next vmentry */
3757 h->summary.guest_interrupt[vector].start_tsc = 1;
3758 h->w2h.interrupts_wanting_tsc++;
3759 h->w2h.interrupts++;
3760
3761 if(opt.dump_all)
3762 printf(" [w2h] d%dv%d Starting vec %d\n",
3763 h->v->d->did, h->v->vid, vector);
3764 }
3765 }
3766
3767 hvm_vlapic_inject(h->v, r->vector);
3768 }
3769
3770 /* I/O Handling */
3771 struct io_address {
3772 struct io_address *next;
3773 unsigned int pa;
3774 unsigned int va;
3775 struct cycle_summary summary[2];
3776 };
3777
3778 void update_io_address(struct io_address ** list, unsigned int pa, int dir,
3779 tsc_t arc_cycles, unsigned int va) {
3780 struct io_address *p, *q=NULL;
3781
3782 /* Keep list in order */
3783 for(p=*list; p && (p->pa != pa) && (p->pa < pa); q=p, p=p->next);
3784
3785 /* If we didn't find it, make a new element. */
3786 if(!p || (p->pa != pa)) {
3787 if((p=malloc(sizeof(*p)))==NULL) {
3788 fprintf(stderr, "malloc() failed.\n");
3789 error(ERR_SYSTEM, NULL);
3790 }
3791
3792 bzero(p, sizeof(*p));
3793
3794 p->pa=pa;
3795 p->va=va;
3796
3797 /* If we stopped in the middle or at the end, add it in */
3798 if(q) {
3799 p->next=q->next;
3800 q->next=p;
3801 } else {
3802 /* Otherwise, we stopped after the first element; put it at the beginning */
3803 p->next = *list;
3804 *list = p;
3805 }
3806 }
3807 update_cycles(&p->summary[dir], arc_cycles);
3808 }
3809
3810 void hvm_io_address_summary(struct io_address *list, const char * s) {
3811 if(!list)
3812 return;
3813
3814 printf("%s\n", s);
3815
3816 for(; list; list=list->next) {
3817 if ( list->va )
3818 {
3819 PRINT_SUMMARY(list->summary[0], "%8x@%8x:[r] ", list->pa, list->va);
3820 PRINT_SUMMARY(list->summary[1], "%8x@%8x:[w] ", list->pa, list->va);
3821 }
3822 else
3823 {
3824 PRINT_SUMMARY(list->summary[0], "%8x:[r] ", list->pa);
3825 PRINT_SUMMARY(list->summary[1], "%8x:[w] ", list->pa);
3826 }
3827 }
3828 }
3829
3830 void hvm_io_write_postprocess(struct hvm_data *h)
3831 {
3832 if(opt.with_pio_enumeration)
3833 update_io_address(&h->summary.io.pio, h->inflight.io.port, 1, h->arc_cycles, 0);
3834 }
3835
3836 void hvm_io_read_postprocess(struct hvm_data *h)
3837 {
3838 if(opt.with_pio_enumeration)
3839 update_io_address(&h->summary.io.pio, h->inflight.io.port, 0, h->arc_cycles, 0);
3840 if(opt.scatterplot_io && h->inflight.io.port == opt.scatterplot_io_port)
3841 scatterplot_vs_time(h->exit_tsc, P.now - h->exit_tsc);
3842 }
3843
3844 void hvm_io_assist_process(struct record_info *ri, struct hvm_data *h)
3845 {
3846 union {
3847 struct {
3848 unsigned int port;
3849 unsigned int data;
3850 } x32;
3851 } *r = (typeof(r))h->d;
3852
3853 union {
3854 unsigned event;
3855 struct {
3856 unsigned minor:8,
3857 x64:1,
3858 write:2;
3859 };
3860 } mevt = { .event = ri->event };
3861
3862 if(mevt.x64) {
3863 fprintf(stderr, "FATAL: Unexpected 64-bit PIO\n");
3864 error(ERR_RECORD, ri);
3865 return;
3866 }
3867
3868 h->inflight.io.port = r->x32.port;
3869 h->inflight.io.val = r->x32.data;
3870
3871 if(mevt.write) {
3872 h->inflight.io.is_write = 1;
3873 if ( hvm_set_postprocess(h, hvm_io_write_postprocess) )
3874 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
3875 } else {
3876 h->inflight.io.is_write = 0;
3877 if ( hvm_set_postprocess(h, hvm_io_read_postprocess) )
3878 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
3879 }
3880
3881 if(opt.dump_all)
3882 {
3883 printf(" %s io %s port %x val %x\n",
3884 ri->dump_header,
3885 mevt.write?"write":"read",
3886 r->x32.port,
3887 r->x32.data);
3888 }
3889 }
3890
3891 /* cr_write */
3892 /* CR3 list */
3893 void cr3_switch(unsigned long long val, struct hvm_data *h) {
3894 struct vcpu_data *v = h->v;
3895 /* Really only need absolute tsc here. Later change to global time. */
3896 unsigned long long now = P.now;
3897 unsigned long long gmfn = val >> 12;
3898
3899 if ( !h->init )
3900 return;
3901
3902 if(opt.with_cr3_enumeration) {
3903 if(v->cr3.data) {
3904 struct cr3_value_struct *cur = v->cr3.data;
3905 unsigned long long cycles = now - v->cr3.start_time;
3906
3907 if(opt.summary_info)
3908 update_cycles(&cur->total_time, cycles);
3909
3910 cur->last_time = now;
3911 }
3912
3913 if(gmfn) {
3914 struct cr3_value_struct *p, **last=&v->d->cr3_value_head;
3915
3916 /* Always add to tail, so that we get consistent interval
3917 ouptut as the number of cr3s grow */
3918 for(p=*last; p; last = (&p->next), p=p->next)
3919 if(p->gmfn == gmfn)
3920 break;
3921
3922 if(!p) {
3923 if((p=malloc(sizeof(*p)))==NULL) {
3924 fprintf(stderr, "malloc() failed.\n");
3925 error(ERR_SYSTEM, NULL);
3926 }
3927
3928 bzero(p, sizeof(*p));
3929
3930 p->gmfn = gmfn;
3931 p->cr3_id = P.cr3.id;
3932 p->first_time = now;
3933
3934 p->next=*last;
3935 *last=p;
3936
3937 p->gnext = NULL;
3938 if(P.cr3.head)
3939 *P.cr3.tail = p;
3940 else
3941 P.cr3.head = p;
3942 P.cr3.tail = &p->gnext;
3943
3944 P.cr3.id++;
3945
3946 /* Add to the interval list if appropriate */
3947 if(opt.interval.check == INTERVAL_CHECK_CR3
3948 && v->d->did != DEFAULT_DOMAIN)
3949 interval_cr3_value_check(p);
3950 }
3951
3952 if(p->prealloc_unpin.now) {
3953 fprintf(warn, "Re-promoting previously unpinned cr3 %llx!\n",
3954 p->gmfn);
3955 p->prealloc_unpin.now = 0;
3956 h->inflight.cr_write.repromote = 1;
3957 }
3958
3959 /* Accounting for new toplevel */
3960 v->cr3.start_time = now;
3961 p->switch_count++;
3962 if(p->destroy.callback)
3963 p->destroy.switch_count++;
3964 v->cr3.data = p;
3965 } else {
3966 v->cr3.data = NULL;
3967 }
3968
3969 if (opt.scatterplot_cr3_switch) {
3970 scatterplot_vs_time(h->exit_tsc,
3971 v->cr3.data ? (v->cr3.data->cr3_id) : 0);
3972 }
3973 } else {
3974 if (opt.scatterplot_cr3_switch)
3975 scatterplot_vs_time(h->exit_tsc, gmfn);
3976 }
3977
3978 v->cr3.val = val;
3979 };
3980
3981 void cr3_prealloc_unpin(struct vcpu_data *v, unsigned long long gmfn) {
3982 struct cr3_value_struct *cr3;
3983
3984 /* Look for it in the list */
3985 for(cr3 = v->d->cr3_value_head; cr3; cr3=cr3->next)
3986 if(cr3->gmfn == gmfn)
3987 break;
3988
3989 if(!cr3)
3990 return;
3991
3992 if(cr3->prealloc_unpin.now)
3993 fprintf(warn, "Strange, gmfn %llx multiple unpins w/o access!\n",
3994 gmfn);
3995
3996 cr3->prealloc_unpin.now = 1;
3997 cr3->prealloc_unpin.count++;
3998
3999 if(opt.dump_all)
4000 printf(" cr3 %llx unpinned %d times\n",
4001 gmfn, cr3->prealloc_unpin.count);
4002 }
4003
4004 int cr3_compare_start(const void *_a, const void *_b) {
4005 struct cr3_value_struct *a=*(typeof(&a))_a;
4006 struct cr3_value_struct *b=*(typeof(&a))_b;
4007
4008 if(a->first_time > b->first_time)
4009 return 1;
4010 else if(b->first_time == a->first_time)
4011 return 0;
4012 else
4013 return -1;
4014 }
4015
4016 void cr3_dump_list(struct cr3_value_struct *head){
4017 struct cr3_value_struct *p;
4018 struct cr3_value_struct **qsort_array;
4019 int i, N=0;
4020
4021 if(!head)
4022 return;
4023
4024 /* Count the number of elements */
4025 for(p=head; p; p=p->next)
4026 N++;
4027
4028 /* Alloc a struct of the right size */
4029 qsort_array = malloc(N * sizeof(struct eip_list_struct *));
4030
4031 /* Point the array into it */
4032 for(i=0, p=head; p; p=p->next, i++)
4033 qsort_array[i]=p;
4034
4035 /* Sort the array by time */
4036 qsort(qsort_array, N, sizeof(struct eip_list_struct *),
4037 cr3_compare_start);
4038
4039 /* WARNING: don't use N after this point unless you copy this variable */
4040 #if 0
4041 if(opt.summary_eip_limit && opt.summary_eip_limit < N)
4042 N=opt.summary_eip_limit;
4043 #endif
4044
4045 /* Now print the results */
4046 printf(" cr3 values:\n");
4047 for(i=0; i<N; i++) {
4048 char desc[30];
4049 struct time_struct first, last;
4050
4051 p = qsort_array[i];
4052
4053 abs_cycles_to_time(p->first_time, &first);
4054 abs_cycles_to_time(p->last_time, &last);
4055
4056
4057 snprintf(desc, 30, " %8llx (id %d)", p->gmfn, p->cr3_id);
4058 print_cycle_summary(&p->total_time, desc);
4059 snprintf(desc, 30, " guest");
4060 print_cycle_percent_summary(&p->guest_time, p->run_time, desc);
4061 snprintf(desc, 30, " hv ");
4062 print_cycle_percent_summary(&p->hv_time, p->run_time, desc);
4063
4064 hvm_short_summary(&p->hvm, p->run_time, " + ");
4065 printf(" Seen: %4u.%09u-%4u.%09u switch %d flush %d\n",
4066 first.s, first.ns,
4067 last.s, last.ns,
4068 p->switch_count, p->flush_count);
4069 if(p->destroy.callback)
4070 printf(" destroy: flush %u switch %u fixup %u emulate %u\n",
4071 p->destroy.flush_count,
4072 p->destroy.switch_count,
4073 p->destroy.fixup_user,
4074 p->destroy.emulate_corr_user);
4075 }
4076
4077 free(qsort_array);
4078 }
4079
4080 void hvm_cr3_write_summary(struct hvm_data *h) {
4081 int j;
4082
4083 for(j=0; j<RESYNCS_MAX; j++)
4084 PRINT_SUMMARY(h->summary.cr3_write_resyncs[j],
4085 " *[%3d] ", j);
4086 PRINT_SUMMARY(h->summary.cr3_write_resyncs[j],
4087 " *[MAX] ");
4088 }
4089
4090 void hvm_cr_write_summary(struct hvm_data *h, void *data)
4091 {
4092 long cr=(long)data;
4093
4094 PRINT_SUMMARY(h->summary.cr_write[cr],
4095 " cr%ld ", cr);
4096 if ( cr==3 )
4097 hvm_cr3_write_summary(h);
4098 }
4099
4100 void hvm_cr_write_postprocess(struct hvm_data *h)
4101 {
4102 if(h->inflight.cr_write.cr == 3) {
4103 struct vcpu_data *v = h->v;
4104 unsigned long long new_val = h->inflight.cr_write.val;
4105 unsigned long long oval;
4106 int flush=0;
4107
4108 if(v->cr3.val) {
4109 oval = v->cr3.val;
4110
4111 if(new_val == oval) {
4112 if(v->cr3.data) {
4113 v->cr3.data->flush_count++;
4114 if(v->cr3.data->destroy.callback)
4115 v->cr3.data->destroy.flush_count++;
4116 }
4117 flush=1;
4118 }
4119 }
4120
4121 if(opt.summary_info) {
4122 int resyncs = h->resyncs;
4123
4124 if(resyncs > RESYNCS_MAX)
4125 resyncs = RESYNCS_MAX;
4126
4127 update_cycles(&h->summary.cr3_write_resyncs[resyncs],
4128 h->arc_cycles);
4129
4130 update_cycles(&h->summary.cr_write[3],
4131 h->arc_cycles);
4132
4133 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_CR3);
4134 }
4135
4136 if(!flush)
4137 cr3_switch(new_val, h);
4138 } else {
4139 if(opt.summary_info)
4140 {
4141 if(h->inflight.cr_write.cr < CR_MAX)
4142 update_cycles(&h->summary.cr_write[h->inflight.cr_write.cr],
4143 h->arc_cycles);
4144
4145 }
4146 }
4147
4148 /* Set summary handler */
4149 /* FIXME - deal with cr_read_summary */
4150 if(h->exit_reason < h->exit_reason_max)
4151 {
4152 /* Want a different "set" for each cr */
4153 switch(h->inflight.cr_write.cr)
4154 {
4155 #define case_cr(_x) \
4156 case (_x): \
4157 hvm_set_summary_handler(h, hvm_cr_write_summary, (void *)(_x)); \
4158 break
4159 case_cr(0);
4160 case_cr(1);
4161 case_cr(2);
4162 case_cr(3);
4163 case_cr(4);
4164 case_cr(5);
4165 case_cr(6);
4166 case_cr(7);
4167 case_cr(8);
4168 case_cr(9);
4169 case_cr(10);
4170 case_cr(11);
4171 case_cr(12);
4172 case_cr(13);
4173 case_cr(14);
4174 case_cr(15);
4175 #undef case_cr
4176 default:
4177 fprintf(stderr, "Unexpected cr: %d\n", h->inflight.cr_write.cr);
4178 error(ERR_SANITY, NULL);
4179 break;
4180 }
4181 }
4182 }
4183
4184 void hvm_cr_write_process(struct record_info *ri, struct hvm_data *h)
4185 {
4186 union {
4187 struct {
4188 unsigned cr;
4189 unsigned int val;
4190 } x32;
4191 struct {
4192 unsigned cr;
4193 unsigned long long val;
4194 } __attribute__((packed)) x64;
4195 } *r = (typeof(r))h->d;
4196 unsigned cr;
4197 unsigned long long val;
4198
4199 if(ri->event & TRC_64_FLAG) {
4200 h->inflight.cr_write.cr = cr = r->x64.cr;
4201 h->inflight.cr_write.val = val = r->x64.val;
4202 } else {
4203 h->inflight.cr_write.cr = cr = r->x32.cr;
4204 h->inflight.cr_write.val = val = r->x32.val;
4205 }
4206
4207 /* In vmx, in real mode, cr accesses may cause EXNMI vmexits.
4208 * Account them under that heading; otherwise, complain */
4209 if ( hvm_set_postprocess(h, hvm_cr_write_postprocess) )
4210 fprintf(warn, "%s: Strange, h->postprocess already set!\n",
4211 __func__);
4212
4213 if(opt.dump_all)
4214 {
4215 if(cr == 3 && h->v->cr3.val) {
4216 printf("]%s cr_write cr3 val %llx oval %llx %s\n",
4217 ri->dump_header,
4218 val,
4219 h->v->cr3.val,
4220 (h->v->cr3.val == val)?"flush":"switch");
4221 } else {
4222 printf(" %s cr_write cr%d val %llx\n",
4223 ri->dump_header,
4224 cr, val);
4225
4226 }
4227 }
4228
4229 }
4230
4231 /* msr_write */
4232 void hvm_msr_write_summary(struct hvm_data *h, void *d)
4233 {
4234 }
4235
4236 void hvm_msr_write_postprocess(struct hvm_data *h)
4237 {
4238 if(opt.summary_info) {
4239 }
4240
4241 /* Set summary handler */
4242 hvm_set_summary_handler(h, hvm_msr_write_summary, NULL);
4243 }
4244
4245 void hvm_msr_write_process(struct record_info *ri, struct hvm_data *h)
4246 {
4247 struct {
4248 unsigned int addr;
4249 unsigned long long val;
4250 } __attribute__((packed)) *r = (typeof(r))h->d;
4251
4252 if(check_extra_words(ri, sizeof(*r), "msr_write"))
4253 return;
4254
4255 h->inflight.msr.addr = r->addr;
4256 h->inflight.msr.val = r->val;
4257
4258 if(opt.dump_all)
4259 {
4260 printf(" %s msr_write addr %x val %llx\n",
4261 ri->dump_header,
4262 r->addr, r->val);
4263 }
4264
4265 if ( hvm_set_postprocess(h, hvm_msr_write_postprocess) )
4266 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
4267 }
4268
4269 /* msr_read */
4270 void hvm_msr_read_summary(struct hvm_data *h, void *d)
4271 {
4272 }
4273
4274 void hvm_msr_read_postprocess(struct hvm_data *h)
4275 {
4276 if(opt.summary_info) {
4277 }
4278
4279 /* Set summary handler */
4280 hvm_set_summary_handler(h, hvm_msr_read_summary, NULL);
4281 }
4282
4283 void hvm_msr_read_process(struct record_info *ri, struct hvm_data *h)
4284 {
4285 struct {
4286 unsigned int addr;
4287 unsigned long long val;
4288 } __attribute__((packed)) *r = (typeof(r))h->d;
4289
4290 if(check_extra_words(ri, sizeof(*r), "msr_read"))
4291 return;
4292
4293 h->inflight.msr.addr = r->addr;
4294 h->inflight.msr.val = r->val;
4295
4296 if(opt.dump_all)
4297 {
4298 printf(" %s msr_read addr %x val %llx\n",
4299 ri->dump_header,
4300 r->addr, r->val);
4301 }
4302
4303 if ( hvm_set_postprocess(h, hvm_msr_read_postprocess) )
4304 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
4305 }
4306
4307 void hvm_vmcall_summary(struct hvm_data *h, void *d)
4308 {
4309 int i;
4310
4311 for ( i=0; i<HYPERCALL_MAX ; i++)
4312 {
4313 PRINT_SUMMARY(h->summary.vmcall[i],
4314 " [%10s] ", hypercall_name[i]);
4315 }
4316 PRINT_SUMMARY(h->summary.vmcall[HYPERCALL_MAX],
4317 " [%10s] ", "max");
4318 }
4319
4320 void hvm_vmcall_postprocess(struct hvm_data *h)
4321 {
4322 unsigned eax = h->inflight.vmcall.eax ;
4323
4324 if(opt.summary)
4325 {
4326 if ( eax < HYPERCALL_MAX )
4327 update_cycles(&h->summary.vmcall[eax],
4328 h->arc_cycles);
4329 else
4330 update_cycles(&h->summary.vmcall[HYPERCALL_MAX],
4331 h->arc_cycles);
4332 hvm_set_summary_handler(h, hvm_vmcall_summary, NULL);
4333 }
4334 }
4335
4336 void hvm_vmcall_process(struct record_info *ri, struct hvm_data *h)
4337 {
4338 struct {
4339 unsigned int eax;
4340 } *r = (typeof(r))h->d;
4341
4342 if(opt.dump_all) {
4343 if(r->eax < HYPERCALL_MAX)
4344 printf(" %s vmcall %2x (%s)\n",
4345 ri->dump_header,
4346 r->eax,
4347 hypercall_name[r->eax]);
4348 else
4349 printf(" %s vmcall %2x\n",
4350 ri->dump_header,
4351 r->eax);
4352 }
4353
4354 h->inflight.vmcall.eax = r->eax;
4355
4356 if ( hvm_set_postprocess(h, hvm_vmcall_postprocess) )
4357 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
4358 }
4359
4360 void hvm_inj_exc_process(struct record_info *ri, struct hvm_data *h)
4361 {
4362 struct {
4363 unsigned vec, ec;
4364 } *r = (typeof(r))h->d;
4365
4366 if ( opt.dump_all )
4367 {
4368 if(r->vec < HVM_TRAP_MAX)
4369 printf(" %3u.%09u %s inj_exc trap %s ec %x\n",
4370 ri->t.s, ri->t.ns, pcpu_string(ri->cpu),
4371 hvm_trap_name[r->vec], r->ec);
4372 else
4373 printf(" %3u.%09u %s inj_exc trap %u ec %x\n",
4374 ri->t.s, ri->t.ns, pcpu_string(ri->cpu),
4375 r->vec, r->ec);
4376 }
4377
4378 }
4379
4380 void hvm_intr_summary(struct hvm_data *h, void *d)
4381 {
4382 int i;
4383
4384 for(i=0; i<EXTERNAL_INTERRUPT_MAX; i++)
4385 if(h->summary.extint[i])
4386 {
4387 if(hvm_extint_vector_name[i])
4388 printf(" %10s(%3d): %d\n",
4389 hvm_extint_vector_name[i],
4390 i,
4391 h->summary.extint[i]);
4392 else
4393 printf(" [%3d]: %d\n",
4394 i,
4395 h->summary.extint[i]);
4396 }
4397 if(h->summary.extint[EXTERNAL_INTERRUPT_MAX])
4398 printf(" Other: : %d\n",
4399 h->summary.extint[EXTERNAL_INTERRUPT_MAX]);
4400 }
4401
4402
4403 void hvm_intr_process(struct record_info *ri, struct hvm_data *h)
4404 {
4405 unsigned vec = *(unsigned *)h->d;
4406
4407 /* Vector is difficult to get in SVM mode */
4408 if ( opt.svm_mode )
4409 vec = 0;
4410
4411 if( (h->rip >> ADDR_SPACE_BITS) != 00
4412 && (h->rip >> ADDR_SPACE_BITS) != ((0ULL-1)>> ADDR_SPACE_BITS) ) {
4413 fprintf(stderr, "Unexpected rip %llx (shift %llx)\n",
4414 h->rip,
4415 h->rip >> ADDR_SPACE_BITS);
4416 error(ERR_RECORD, NULL);
4417 /* Can process with strange rip */
4418 }
4419
4420 h->inflight.intr.vec = vec;
4421
4422 if ( opt.dump_all )
4423 {
4424 if ( vec < EXTERNAL_INTERRUPT_MAX &&
4425 hvm_extint_vector_name[vec] )
4426 printf(" %s intr vec %s(%x)\n",
4427 ri->dump_header,
4428 hvm_extint_vector_name[vec],
4429 vec);
4430 else
4431 printf(" %s intr vec %x\n",
4432 ri->dump_header, vec);
4433 }
4434
4435 if(opt.scatterplot_interrupt_eip
4436 && vec == opt.scatterplot_interrupt_vector)
4437 {
4438 struct time_struct t;
4439 /* Truncate to 40 bits */
4440 unsigned long long rip = h->rip & ((1ULL << ADDR_SPACE_BITS)-1);
4441
4442 /* Want absolute tsc to global tsc */
4443 abs_cycles_to_time(h->exit_tsc, &t);
4444 printf("d%dv%d %u.%09u %lld\n",
4445 h->v->d->did, h->v->vid,
4446 t.s, t.ns,
4447 rip);
4448 }
4449
4450 if(opt.histogram_interrupt_eip
4451 && vec == opt.histogram_interrupt_vector)
4452 {
4453 /* Truncate to 40 bits */
4454 unsigned long long rip = h->rip & ((1ULL << ADDR_SPACE_BITS)-1);
4455 unsigned index = rip / opt.histogram_interrupt_increment;
4456
4457 h->summary.extint_histogram[index]++;
4458 }
4459
4460 if(opt.with_interrupt_eip_enumeration
4461 && vec == opt.interrupt_eip_enumeration_vector)
4462 {
4463 /* Truncate to 40 bits */
4464 unsigned long long rip = h->rip & ((1ULL << ADDR_SPACE_BITS)-1);
4465
4466 /* Want absolute tsc to global tsc */
4467 update_eip(&h->v->d->interrupt_eip_list, rip, 0, 0, NULL);
4468 }
4469
4470 /* Disable generic postprocessing */
4471 /* FIXME: Do the summary stuff in a post-processor */
4472 h->post_process = NULL;
4473
4474 if(opt.summary_info) {
4475 if(opt.summary)
4476 hvm_set_summary_handler(h, hvm_intr_summary, NULL);
4477
4478 if(vec < EXTERNAL_INTERRUPT_MAX)
4479 h->summary.extint[vec]++;
4480 else
4481 h->summary.extint[EXTERNAL_INTERRUPT_MAX]++;
4482 }
4483 }
4484
4485
4486 void hvm_intr_window_process(struct record_info *ri, struct hvm_data *h)
4487 {
4488 struct {
4489 uint32_t vector;
4490 uint32_t source;
4491 int32_t intr;
4492 } *r = (typeof(r))h->d;
4493
4494 const char *intsrc_name[] = {
4495 "none",
4496 "pic",
4497 "lapic",
4498 "nmi",
4499 "mce",
4500 "vector"
4501 };
4502
4503 if ( opt.dump_all )
4504 {
4505 printf(" %s intr_window vec %u src %u(%s) ",
4506 ri->dump_header,
4507 (unsigned)r->vector,
4508 (unsigned)r->source,
4509 r->source < 6 ? intsrc_name[r->source]: "?");
4510
4511 if ( r->intr > 0 )
4512 printf("intr %x\n",
4513 (unsigned)r->intr);
4514 else
4515 printf("intr #\n");
4516 }
4517 }
4518
4519 void hvm_pf_inject_process(struct record_info *ri, struct hvm_data *h)
4520 {
4521 union {
4522 struct {
4523 unsigned ec;
4524 unsigned int cr2;
4525 } x32;
4526 struct {
4527 unsigned ec;
4528 unsigned long long cr2;
4529 } __attribute__((packed)) x64;
4530 } *r = (typeof(r))h->d;
4531 unsigned int ec;
4532 unsigned long long cr2;
4533 int is_64 = 0;
4534
4535 if(ri->event & TRC_64_FLAG) {
4536 is_64 = 1;
4537 cr2 = r->x64.cr2;
4538 ec = r->x64.ec;
4539 } else {
4540 cr2 = r->x32.cr2;
4541 ec = r->x32.ec;
4542 }
4543
4544 if ( opt.dump_all )
4545 {
4546 printf(" %3u.%09u %s pf_inject%s guest_cr2 %llx guest_ec %x\n",
4547 ri->t.s, ri->t.ns, pcpu_string(ri->cpu),
4548 is_64?"64":"",
4549 cr2, ec);
4550 }
4551 }
4552
4553 void hvm_generic_postprocess_init(struct record_info *ri, struct hvm_data *h);
4554
4555 void hvm_npf_process(struct record_info *ri, struct hvm_data *h)
4556 {
4557 struct {
4558 uint64_t gpa;
4559 uint64_t mfn;
4560 uint32_t qualification;
4561 uint32_t p2mt;
4562 } *r = (typeof(r))h->d;
4563
4564 if ( opt.dump_all )
4565 printf(" %s npf gpa %llx q %x mfn %llx t %d\n",
4566 ri->dump_header,
4567 (unsigned long long)r->gpa, r->qualification,
4568 (unsigned long long)r->mfn, r->p2mt);
4569
4570 if ( opt.summary_info )
4571 hvm_generic_postprocess_init(ri, h);
4572 }
4573
4574 void hvm_rdtsc_process(struct record_info *ri, struct hvm_data *h)
4575 {
4576 struct {
4577 unsigned long long tsc;
4578 } *r = (typeof(r))h->d;
4579
4580 if ( opt.dump_all )
4581 printf(" %s rdtsc %llx %lld %s\n",
4582 ri->dump_header,
4583 (unsigned long long)r->tsc,
4584 (unsigned long long)r->tsc,
4585 h->last_rdtsc > r->tsc ? "BACKWARDS" : "");
4586
4587 if ( opt.scatterplot_rdtsc )
4588 {
4589 struct time_struct t;
4590
4591 abs_cycles_to_time(ri->tsc, &t);
4592
4593 printf("%dv%d %u.%09u %llu\n",
4594 h->v->d->did, h->v->vid,
4595 t.s, t.ns,
4596 r->tsc);
4597 }
4598
4599 h->last_rdtsc = r->tsc;
4600 }
4601
4602 void hvm_generic_summary(struct hvm_data *h, void *data)
4603 {
4604 long evt = (long)data;
4605
4606 assert(evt < HVM_EVENT_HANDLER_MAX);
4607
4608 PRINT_SUMMARY(h->summary.generic[evt],
4609 " %s ", hvm_event_handler_name[evt]);
4610
4611 }
4612
4613 void hvm_generic_postprocess_init(struct record_info *ri, struct hvm_data *h)
4614 {
4615 if ( h->post_process != hvm_generic_postprocess )
4616 fprintf(warn, "%s: Strange, h->postprocess set!\n",
4617 __func__);
4618 h->inflight.generic.event = ri->event;
4619 bcopy(h->d, h->inflight.generic.d, sizeof(unsigned int) * 4);
4620 }
4621
4622 void hvm_generic_postprocess(struct hvm_data *h)
4623 {
4624 long evt = 0;
4625 static unsigned registered[HVM_EVENT_HANDLER_MAX] = { 0 };
4626
4627 if ( h->inflight.generic.event )
4628 evt = (h->inflight.generic.event - TRC_HVM_HANDLER)
4629 & ~(TRC_64_FLAG|HVM_IO_ASSIST_WRITE);
4630 else {
4631 static unsigned warned[HVM_EXIT_REASON_MAX] = { 0 };
4632 /* Some exits we don't expect a handler; just return */
4633 if(opt.svm_mode)
4634 {
4635 switch(h->exit_reason)
4636 {
4637 case VMEXIT_VINTR: /* Equivalent of PENDING_VIRT_INTR */
4638 case VMEXIT_PAUSE:
4639 return;
4640 default:
4641 break;
4642 }
4643 }
4644 else
4645 {
4646 switch(h->exit_reason)
4647 {
4648 /* These just need us to go through the return path */
4649 case EXIT_REASON_PENDING_VIRT_INTR:
4650 case EXIT_REASON_TPR_BELOW_THRESHOLD:
4651 case EXIT_REASON_PAUSE_INSTRUCTION:
4652 /* Not much to log now; may need later */
4653 case EXIT_REASON_WBINVD:
4654 return;
4655 default:
4656 break;
4657 }
4658 }
4659 if ( !warned[h->exit_reason] )
4660 {
4661 /* If we aren't a known exception, warn and log results */
4662 fprintf(warn, "%s: Strange, exit %x(%s) missing a handler\n",
4663 __func__, h->exit_reason,
4664 (h->exit_reason > h->exit_reason_max)
4665 ? "[clipped]"
4666 : h->exit_reason_name[h->exit_reason]);
4667 warned[h->exit_reason]=1;
4668
4669 /*
4670 * NB that we don't return here; the result will be that `evt`
4671 * will be "0", and there will be a "(no handler)" entry for the
4672 * given VMEXIT.
4673 *
4674 * This does mean that if two different exits have no HVM
4675 * handlers, that only the first one will accumulate data;
4676 * if accumulating a separate "(no handler)" data for more
4677 * than one exit reason is needed, we'll have to make Yet
4678 * Another Array. But for now, since we try to avoid
4679 * it happening, just tolerate it.
4680 */
4681 }
4682 }
4683
4684 if ( evt >= HVM_EVENT_HANDLER_MAX || evt < 0)
4685 {
4686 fprintf(warn, "%s: invalid hvm event %lx(%x)\n",
4687 __func__, evt, h->inflight.generic.event);
4688 error(ERR_RECORD, NULL);
4689 return;
4690 }
4691
4692 if(opt.summary_info) {
4693 /* NB that h->exit_reason may be 0, so we offset by 1 */
4694 if ( registered[evt] )
4695 {
4696 static unsigned warned[HVM_EXIT_REASON_MAX] = { 0 };
4697 if ( registered[evt] != h->exit_reason+1 )
4698 {
4699 if ( !warned[h->exit_reason] )
4700 {
4701 fprintf(warn, "%s: HVM evt %lx in %x and %x!\n",
4702 __func__, evt, registered[evt]-1, h->exit_reason);
4703 warned[h->exit_reason]=1;
4704 }
4705 return;
4706 }
4707 }
4708 else
4709 {
4710 int ret;
4711 if((ret=__hvm_set_summary_handler(h, hvm_generic_summary, (void *)evt)))
4712 fprintf(stderr, "%s: hvm_set_summary_handler returned %d\n",
4713 __func__, ret);
4714 registered[evt]=h->exit_reason+1;
4715 }
4716
4717 update_cycles(&h->summary.generic[evt],
4718 h->arc_cycles);
4719
4720 /* HLT checked at hvm_vmexit_close() */
4721 }
4722 }
4723
4724 void hvm_generic_dump(struct record_info *ri, const char * prefix)
4725 {
4726 struct {
4727 unsigned vcpu:16, domain:16;
4728 unsigned d[4];
4729 } *cr = (typeof(cr))ri->d;
4730
4731 const char *evt_string;
4732 char evt_number[256];
4733 int i, evt, is_64 = 0;
4734
4735 evt = ri->event - TRC_HVM_HANDLER;
4736
4737 if(evt & TRC_64_FLAG) {
4738 evt &= ~(TRC_64_FLAG);
4739 is_64=1;
4740 }
4741
4742 if(evt < HVM_EVENT_HANDLER_MAX)
4743 {
4744 evt_string = hvm_event_handler_name[evt];
4745 }
4746 else
4747 {
4748 snprintf(evt_number, 256, "hvm_handler %d", evt);
4749 evt_string = evt_number;
4750 }
4751
4752 printf("%s%s %s%s [",
4753 prefix,
4754 ri->dump_header,
4755 evt_string,
4756 is_64?"64":"");
4757
4758 for(i=0; i<ri->extra_words; i++) {
4759 printf(" %x", ri->d[i]);
4760 }
4761
4762 printf(" ]\n");
4763 }
4764
4765 void hvm_handler_process(struct record_info *ri, struct hvm_data *h) {
4766 /* Wait for first vmexit to initialize */
4767 if(!h->init)
4768 {
4769 if(opt.dump_all)
4770 hvm_generic_dump(ri,"!");
4771 return;
4772 }
4773
4774 h->d = ri->d;
4775
4776 /* Handle things that don't need a vmexit */
4777 switch(ri->event) {
4778 default:
4779 goto needs_vmexit;
4780 /* Records about changing guest state */
4781 case TRC_HVM_PF_INJECT:
4782 case TRC_HVM_PF_INJECT64:
4783 hvm_pf_inject_process(ri, h);
4784 break;
4785 case TRC_HVM_REINJ_VIRQ:
4786 if ( opt.dump_all )
4787 {
4788 printf(" %3u.%09u %s inj_virq vec %u\n",
4789 ri->t.s, ri->t.ns, pcpu_string(ri->cpu),
4790 *(unsigned*)h->d);
4791 }
4792 break;
4793 case TRC_HVM_INJ_EXC:
4794 hvm_inj_exc_process(ri, h);
4795 break;
4796 case TRC_HVM_INJ_VIRQ:
4797 hvm_inj_virq_process(ri, h);
4798 break;
4799 case TRC_HVM_INTR_WINDOW:
4800 hvm_intr_window_process(ri, h);
4801 break;
4802 case TRC_HVM_OP_DESTROY_PROC:
4803 if(h->v->cr3.data) {
4804 struct cr3_value_struct *cur = h->v->cr3.data;
4805 if(cur->destroy.callback)
4806 fprintf(warn, "Strange, double callback for cr3 gmfn %llx!\n",
4807 cur->gmfn);
4808 cur->destroy.callback = 1;
4809 } else if(opt.with_cr3_enumeration) {
4810 fprintf(warn, "Warning: destroy_proc: don't know current cr3\n");
4811 }
4812 if ( opt.dump_all )
4813 {
4814 printf(" %3u.%09u %s destroy_proc cur_cr3 %llx\n",
4815 ri->t.s, ri->t.ns, pcpu_string(ri->cpu), h->v->cr3.val);
4816 }
4817 break;
4818 }
4819
4820 return;
4821
4822 needs_vmexit:
4823 /* Wait for the next vmexit */
4824 if(!h->vmexit_valid)
4825 {
4826 if(opt.dump_all)
4827 hvm_generic_dump(ri,"!");
4828 return;
4829 }
4830
4831 /* Keep generic "event handler" info */
4832 h->event_handler = ri->event - TRC_HVM_HANDLER;
4833
4834 switch(ri->event) {
4835 /* Records adding to the vmexit reason */
4836 case TRC_HVM_INTR:
4837 hvm_intr_process(ri, h);
4838 break;
4839 case TRC_HVM_PF_XEN:
4840 case TRC_HVM_PF_XEN64:
4841 hvm_pf_xen_process(ri, h);
4842 break;
4843 case TRC_HVM_IOPORT_READ:
4844 case TRC_HVM_IOPORT_WRITE:
4845 hvm_io_assist_process(ri, h);
4846 break;
4847 case TRC_HVM_IOMEM_READ:
4848 case TRC_HVM_IOMEM_WRITE:
4849 case TRC_HVM_IOMEM_READ|TRC_64_FLAG:
4850 case TRC_HVM_IOMEM_WRITE|TRC_64_FLAG:
4851 hvm_mmio_assist_process(ri, h);
4852 break;
4853 case TRC_HVM_CR_WRITE:
4854 case TRC_HVM_CR_WRITE64:
4855 hvm_cr_write_process(ri, h);
4856 break;
4857 case TRC_HVM_MSR_WRITE:
4858 hvm_msr_write_process(ri, h);
4859 break;
4860 case TRC_HVM_MSR_READ:
4861 hvm_msr_read_process(ri, h);
4862 break;
4863 case TRC_HVM_VMMCALL:
4864 hvm_vmcall_process(ri, h);
4865 break;
4866 case TRC_HVM_NPF:
4867 hvm_npf_process(ri, h);
4868 break;
4869 case TRC_HVM_RDTSC:
4870 hvm_rdtsc_process(ri, h);
4871 break;
4872 case TRC_HVM_DR_READ:
4873 case TRC_HVM_DR_WRITE:
4874 case TRC_HVM_CPUID:
4875 case TRC_HVM_SMI:
4876 case TRC_HVM_HLT:
4877 case TRC_HVM_INVLPG:
4878 case TRC_HVM_INVLPG64:
4879 case TRC_HVM_MCE:
4880 case TRC_HVM_CLTS:
4881 case TRC_HVM_LMSW:
4882 case TRC_HVM_LMSW64:
4883 case TRC_HVM_NMI:
4884 case TRC_HVM_REALMODE_EMULATE:
4885 case TRC_HVM_TRAP:
4886 case TRC_HVM_TRAP_DEBUG:
4887 case TRC_HVM_CR_READ:
4888 case TRC_HVM_CR_READ64:
4889 default:
4890 if(opt.dump_all)
4891 hvm_generic_dump(ri, "]");
4892 if(opt.summary_info)
4893 hvm_generic_postprocess_init(ri, h);
4894 break;
4895 }
4896 }
4897
4898 void vcpu_next_update(struct pcpu_info *p, struct vcpu_data *next, tsc_t tsc);
4899 void vcpu_prev_update(struct pcpu_info *p, struct vcpu_data *prev,
4900 tsc_t tsc, int new_runstate);
4901 struct vcpu_data * vcpu_find(int did, int vid);
4902 void lose_vcpu(struct vcpu_data *v, tsc_t tsc);
4903
4904 int domain_runstate(struct domain_data *d) {
4905 int i;
4906 int runstates[RUNSTATE_MAX];
4907 int ret=-1;
4908 int max_vcpus = 0;
4909
4910 if(d->did == DEFAULT_DOMAIN)
4911 return 0;
4912
4913 for(i=0; i<RUNSTATE_MAX; i++)
4914 runstates[i]=0;
4915
4916 for(i=0; i<=d->max_vid; i++)
4917 if(d->vcpu[i] && d->vcpu[i]->runstate.state != RUNSTATE_INIT) {
4918 max_vcpus++;
4919 runstates[d->vcpu[i]->runstate.state]++;
4920 }
4921
4922 if(runstates[RUNSTATE_LOST] == max_vcpus)
4923 ret=DOMAIN_RUNSTATE_LOST;
4924 else if(runstates[RUNSTATE_RUNNING])
4925 {
4926 if(runstates[RUNSTATE_RUNNABLE])
4927 ret=DOMAIN_RUNSTATE_CONCURRENCY_HAZARD;
4928 else if(runstates[RUNSTATE_BLOCKED]||runstates[RUNSTATE_OFFLINE])
4929 ret= DOMAIN_RUNSTATE_PARTIAL_RUN;
4930 else
4931 ret= DOMAIN_RUNSTATE_FULL_RUN;
4932 }
4933 else if(runstates[RUNSTATE_RUNNABLE])
4934 {
4935 if(runstates[RUNSTATE_BLOCKED]||runstates[RUNSTATE_OFFLINE])
4936 ret= DOMAIN_RUNSTATE_PARTIAL_CONTENTION;
4937 else
4938 ret= DOMAIN_RUNSTATE_FULL_CONTENTION;
4939 }
4940 else if(runstates[RUNSTATE_BLOCKED]||runstates[RUNSTATE_OFFLINE])
4941 {
4942 ret= DOMAIN_RUNSTATE_BLOCKED;
4943 } else {
4944 fprintf(warn, "Strange, no meaningful runstates for d%d!\n",
4945 d->did);
4946 }
4947
4948 if ( ret < 0 )
4949 {
4950 printf(" Max vid: %d (max_vcpus %d)\n", d->max_vid, max_vcpus);
4951 for(i=0; i<=d->max_vid; i++)
4952 if(d->vcpu[i])
4953 fprintf(warn, " v%d: %s\n",
4954 i, runstate_name[d->vcpu[i]->runstate.state]);
4955
4956 for(i=0; i<RUNSTATE_MAX; i++)
4957 fprintf(warn, " %s: %d\n",
4958 runstate_name[i], runstates[i]);
4959 }
4960
4961 if(ret >= 0)
4962 return ret;
4963
4964 error(ERR_ASSERT, NULL);
4965 return -1; /* Never happens */
4966 }
4967
4968 static inline void runstate_update(struct vcpu_data *v, int new_runstate,
4969 tsc_t tsc)
4970 {
4971 struct domain_data *d = v->d;
4972
4973 if ( opt.scatterplot_runstate )
4974 {
4975 struct time_struct t;
4976
4977 abs_cycles_to_time(tsc, &t);
4978
4979 printf("%dv%d %u.%09u %d\n",
4980 d->did, v->vid,
4981 t.s, t.ns,
4982 runstate_graph[v->runstate.state]);
4983 printf("%dv%d %u.%09u %d\n",
4984 d->did, v->vid,
4985 t.s, t.ns,
4986 runstate_graph[new_runstate]);
4987 }
4988
4989 if(v->runstate.tsc > 0 && v->runstate.tsc < tsc) {
4990 update_cycles(v->runstates + v->runstate.state, tsc - v->runstate.tsc);
4991
4992 if ( opt.scatterplot_runstate_time )
4993 {
4994 struct time_struct t, dt;
4995
4996 abs_cycles_to_time(tsc, &t);
4997 cycles_to_time(tsc - v->runstate.tsc, &dt);
4998
4999 printf("%dv%d %u.%09u %u.%09u\n",
5000 d->did, v->vid,
5001 t.s, t.ns,
5002 dt.s, dt.ns);
5003 }
5004
5005 if(v->runstate.state == RUNSTATE_RUNNING)
5006 update_cycles(&v->d->total_time, tsc - v->runstate.tsc);
5007
5008 if(v->runstate.state == RUNSTATE_RUNNABLE)
5009 update_cycles(v->runnable_states + v->runstate.runnable_state, tsc - v->runstate.tsc);
5010
5011 /* How much did dom0 run this buffer? */
5012 if(v->d->did == 0) {
5013 int i;
5014 for(i=0; i<MAX_CPUS; i++) {
5015 struct pcpu_info * p = P.pcpu + i;
5016 tsc_t start_tsc;
5017 if(!p->active)
5018 continue;
5019 start_tsc = (p->volume.buffer_first_tsc > v->runstate.tsc) ?
5020 p->volume.buffer_first_tsc :
5021 v->runstate.tsc;
5022 p->volume.buffer_dom0_runstate_cycles[v->runstate.state]
5023 += tsc - start_tsc;
5024 #if 0
5025 printf(" - updated p%d dom0_runstate %s to %lld cycles (+%lld)\n",
5026 p->pid, runstate_name[v->runstate.state],
5027 p->volume.buffer_dom0_runstate_cycles[v->runstate.state],
5028 tsc - start_tsc);
5029 #endif
5030 p->volume.buffer_dom0_runstate = new_runstate;
5031 p->volume.buffer_dom0_runstate_tsc = tsc;
5032 }
5033 }
5034 }
5035
5036 /* Detect "runnable" states */
5037 if ( new_runstate == RUNSTATE_RUNNABLE )
5038 {
5039 switch(v->runstate.state)
5040 {
5041 case RUNSTATE_RUNNING:
5042 v->runstate.runnable_state=RUNNABLE_STATE_PREEMPT;
5043 break;
5044 case RUNSTATE_BLOCKED:
5045 case RUNSTATE_OFFLINE:
5046 v->runstate.runnable_state=RUNNABLE_STATE_WAKE;
5047 break;
5048 default:
5049 v->runstate.runnable_state=RUNNABLE_STATE_OTHER;
5050 break;
5051 }
5052 } else
5053 v->runstate.runnable_state=RUNNABLE_STATE_INVALID;
5054
5055 v->runstate.state = new_runstate;
5056 v->runstate.tsc = tsc;
5057
5058 /* Determine the domain runstate */
5059 if(d->runstate_tsc > 0 && d->runstate_tsc < tsc)
5060 update_cycles(d->runstates + d->runstate, tsc - d->runstate_tsc);
5061
5062 d->runstate = domain_runstate(d);
5063
5064 d->runstate_tsc = tsc;
5065 }
5066
5067 void hvm_vmexit_process(struct record_info *ri, struct hvm_data *h,
5068 struct vcpu_data *v) {
5069 struct {
5070 union {
5071 struct {
5072 unsigned int exit_reason;
5073 unsigned long long rip;
5074 } __attribute__((packed)) x64;
5075 struct {
5076 unsigned int exit_reason;
5077 unsigned int eip;
5078 } x32;
5079 };
5080 } *r;
5081
5082 if ( ri->event & TRC_64_FLAG )
5083 {
5084 if (check_extra_words(ri, sizeof(r->x64), "vmexit"))
5085 return;
5086 }
5087 else
5088 {
5089 if (check_extra_words(ri, sizeof(r->x32), "vmexit"))
5090 return;
5091 }
5092
5093 r = (typeof(r))ri->d;
5094
5095 if(!h->exit_reason_name)
5096 set_hvm_exit_reason_data(h, ri->event);
5097
5098 h->vmexit_valid=1;
5099 bzero(&h->inflight, sizeof(h->inflight));
5100
5101 if(ri->event & TRC_64_FLAG) {
5102 if(v->guest_paging_levels != 4)
5103 {
5104 if ( verbosity >= 6 )
5105 fprintf(warn, "%s: VMEXIT64, but guest_paging_levels %d. Switching to 4.\n",
5106 __func__, v->guest_paging_levels);
5107 v->guest_paging_levels = 4;
5108 }
5109 if(!is_valid_addr64(r->x64.rip))
5110 fprintf(warn, "%s: invalid va %llx\n",
5111 __func__, r->x64.rip);
5112 h->rip = r->x64.rip;
5113 h->exit_reason = r->x64.exit_reason;
5114 } else {
5115 if(v->guest_paging_levels == 4)
5116 {
5117 int new_paging_levels = opt.default_guest_paging_levels;
5118
5119 if(new_paging_levels == 4)
5120 new_paging_levels = 2; /* Wild guess */
5121
5122 if ( verbosity >= 6 )
5123 fprintf(warn, "%s: VMEXIT, but guest_paging_levels %d. Switching to %d(default).\n",
5124 __func__, v->guest_paging_levels, new_paging_levels);
5125
5126 v->guest_paging_levels = new_paging_levels;
5127 }
5128 h->rip = r->x32.eip;
5129 h->exit_reason = r->x32.exit_reason;
5130 }
5131
5132 if(opt.scatterplot_vmexit_eip)
5133 scatterplot_vs_time(ri->tsc, h->rip);
5134
5135 if(h->exit_reason > h->exit_reason_max)
5136 {
5137 fprintf(warn, "h->exit_reason %x > exit_reason_max %x!\n",
5138 (unsigned int)h->exit_reason,
5139 (unsigned int)h->exit_reason_max);
5140 error(ERR_RECORD, ri);
5141 return;
5142 }
5143
5144 if(opt.dump_all) {
5145 if ( h->exit_reason < h->exit_reason_max
5146 && h->exit_reason_name[h->exit_reason] != NULL)
5147 printf("]%s vmexit exit_reason %s eip %llx%s\n",
5148 ri->dump_header,
5149 h->exit_reason_name[h->exit_reason],
5150 h->rip,
5151 find_symbol(h->rip));
5152 else
5153 printf("]%s vmexit exit_reason %x eip %llx%s\n",
5154 ri->dump_header,
5155 h->exit_reason,
5156 h->rip,
5157 find_symbol(h->rip));
5158 }
5159
5160 if(h->v->cr3.data && h->entry_tsc) {
5161 update_cycles(&h->v->cr3.data->guest_time,
5162 ri->tsc - h->entry_tsc);
5163 h->v->cr3.data->run_time += (ri->tsc - h->entry_tsc);
5164 }
5165
5166 h->exit_tsc = ri->tsc;
5167 h->entry_tsc = 0;
5168 h->resyncs = 0;
5169 h->prealloc_unpin = 0;
5170 h->wrmap_bf = 0;
5171 h->short_summary_done = 0;
5172
5173 h->post_process = hvm_generic_postprocess;
5174 h->inflight.generic.event = 0;
5175 }
5176
5177 void hvm_close_vmexit(struct hvm_data *h, tsc_t tsc) {
5178
5179 if(h->exit_tsc) {
5180 if(h->exit_tsc > tsc)
5181 h->arc_cycles = 0;
5182 else {
5183 h->arc_cycles = tsc - h->exit_tsc;
5184
5185 if(opt.summary_info) {
5186 update_cycles(&h->summary.exit_reason[h->exit_reason],
5187 h->arc_cycles);
5188 h->summary_info = 1;
5189 }
5190
5191 if ( opt.scatterplot_extint_cycles
5192 && h->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT
5193 && h->inflight.intr.vec == opt.scatterplot_extint_cycles_vector )
5194 {
5195 struct time_struct t;
5196
5197 abs_cycles_to_time(tsc, &t);
5198
5199 printf("d%dv%d %u.%09u %lld\n",
5200 h->v->d->did,
5201 h->v->vid,
5202 t.s, t.ns,
5203 h->arc_cycles);
5204 }
5205 }
5206 }
5207
5208 if(h->post_process)
5209 (h->post_process)(h);
5210
5211 if(h->arc_cycles) {
5212 if(opt.summary_info && !h->short_summary_done) {
5213 switch(h->event_handler) {
5214 case HVM_EVENT_HANDLER_VMCALL:
5215 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_VMCALL);
5216 break;
5217 case HVM_EVENT_HANDLER_INTR:
5218 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_INTERRUPT);
5219 break;
5220 case HVM_EVENT_HANDLER_HLT:
5221 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_HLT);
5222 break;
5223 default:
5224 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_OTHER);
5225 break;
5226 }
5227 }
5228
5229
5230 if(h->v->cr3.data) {
5231 h->v->cr3.data->run_time += h->arc_cycles;
5232
5233 if(opt.summary_info)
5234 update_cycles(&h->v->cr3.data->hv_time,
5235 h->arc_cycles);
5236 }
5237 }
5238
5239 h->exit_tsc = 0;
5240 h->vmexit_valid = 0;
5241 h->post_process = NULL;
5242
5243 }
5244
5245 void hvm_vmentry_process(struct record_info *ri, struct hvm_data *h) {
5246 if(!h->init)
5247 {
5248 if(opt.dump_all)
5249 printf("!%s vmentry\n",
5250 ri->dump_header);
5251 return;
5252 }
5253
5254 /* Vista bug
5255 * This has to be done here because irqs are injected on the path out
5256 * to vmexit. */
5257 hvm_vlapic_vmentry_cleanup(h->v, ri->tsc);
5258
5259 if(h->w2h.waking && opt.dump_all)
5260 printf(" [w2h] d%dv%d Finishing waking\n",
5261 h->v->d->did, h->v->vid);
5262
5263 h->w2h.waking = 0;
5264
5265 if ( h->w2h.interrupts_wanting_tsc ) {
5266 int i;
5267 for(i=0; i<GUEST_INTERRUPT_MAX; i++)
5268 {
5269 if ( h->summary.guest_interrupt[i].start_tsc == 1 )
5270 {
5271 if(opt.dump_all)
5272 printf(" [w2h] d%dv%d Setting vec %d tsc to %lld\n",
5273 h->v->d->did, h->v->vid, i, ri->tsc);
5274 h->summary.guest_interrupt[i].start_tsc = ri->tsc;
5275 h->w2h.interrupts_wanting_tsc--;
5276 if ( h->w2h.interrupts_wanting_tsc == 0 )
5277 break;
5278 }
5279 }
5280 }
5281
5282 if(!h->vmexit_valid)
5283 {
5284 if(opt.dump_all)
5285 printf("!%s vmentry\n",
5286 ri->dump_header);
5287 return;
5288 }
5289
5290 if(opt.dump_all) {
5291 unsigned long long arc_cycles = ri->tsc - h->exit_tsc;
5292 printf("]%s vmentry cycles %lld %s\n",
5293 ri->dump_header, arc_cycles, (arc_cycles>10000)?"!":"");
5294 }
5295
5296 hvm_close_vmexit(h, ri->tsc);
5297 h->entry_tsc = ri->tsc;
5298 }
5299
5300 void hvm_process(struct pcpu_info *p)
5301 {
5302 struct record_info *ri = &p->ri;
5303 struct vcpu_data *v = p->current;
5304 struct hvm_data *h = &v->hvm;
5305
5306 assert(p->current);
5307
5308 /* HVM_EMUL types show up in all contexts */
5309 if(ri->evt.sub != 0x4) {
5310 if(vcpu_set_data_type(p->current, VCPU_DATA_HVM))
5311 return;
5312 }
5313
5314 switch ( ri->evt.sub ) {
5315 case 2: /* HVM_HANDLER */
5316 UPDATE_VOLUME(p, hvm[HVM_VOL_HANDLER], ri->size);
5317 hvm_handler_process(ri, h);
5318 break;
5319 case 4: /* HVM_EMUL */
5320 UPDATE_VOLUME(p, hvm[HVM_VOL_EMUL], ri->size);
5321 warn_once("WARNING: We don't yet analyze HVM_EMUL events.\n");
5322 /* FIXME: Collect analysis on this */
5323 break;
5324 default:
5325 switch(ri->event) {
5326 case TRC_HVM_VMX_EXIT:
5327 case TRC_HVM_VMX_EXIT64:
5328 case TRC_HVM_SVM_EXIT:
5329 case TRC_HVM_SVM_EXIT64:
5330 UPDATE_VOLUME(p, hvm[HVM_VOL_VMEXIT], ri->size);
5331 hvm_vmexit_process(ri, h, v);
5332 break;
5333 case TRC_HVM_VMENTRY:
5334 UPDATE_VOLUME(p, hvm[HVM_VOL_VMENTRY], ri->size);
5335 hvm_vmentry_process(ri, &p->current->hvm);
5336 break;
5337 default:
5338 fprintf(warn, "Unknown hvm event: %x\n", ri->event);
5339 }
5340 }
5341 }
5342
5343 void hvm_summary(struct hvm_data *h) {
5344 int i;
5345
5346 if(!h->summary_info)
5347 return;
5348
5349 printf("Exit reasons:\n");
5350 for(i=0; i<h->exit_reason_max; i++) {
5351 struct hvm_summary_handler_node *p;
5352
5353 if ( h->exit_reason_name[i] )
5354 PRINT_SUMMARY(h->summary.exit_reason[i],
5355 " %-20s ", h->exit_reason_name[i]);
5356 else
5357 PRINT_SUMMARY(h->summary.exit_reason[i],
5358 " %20d ", i);
5359
5360 p=h->exit_reason_summary_handler_list[i];
5361 while(p)
5362 {
5363 p->handler(h, p->data);
5364 p=p->next;
5365 }
5366 }
5367
5368 printf("Guest interrupt counts:\n");
5369 for(i=0; i<GUEST_INTERRUPT_MAX; i++)
5370 if(h->summary.guest_interrupt[i].count) {
5371 int j;
5372 printf(" [%3d] %d\n",
5373 i, h->summary.guest_interrupt[i].count);
5374 for(j=1; j<GUEST_INTERRUPT_CASE_MAX; j++) {
5375 char desc[80];
5376 snprintf(desc, 80, " * %s", guest_interrupt_case_name[j]);
5377 print_cycle_summary(h->summary.guest_interrupt[i].runtime+j, desc);
5378 }
5379 }
5380 if(h->summary.guest_interrupt[i].count)
5381 printf(" [%d+] %d\n",
5382 i, h->summary.guest_interrupt[i].count);
5383
5384 if(opt.histogram_interrupt_eip)
5385 {
5386 unsigned max = ((1ULL<<ADDR_SPACE_BITS)/opt.histogram_interrupt_increment);
5387 printf("Interrupt eip histogram:\n");
5388 for(i=0; i<max; i++)
5389 if(h->summary.extint_histogram[i])
5390 {
5391 printf("[%llx-%llx]: %d\n",
5392 opt.histogram_interrupt_increment * i,
5393 (opt.histogram_interrupt_increment * (i+1)) - 1,
5394 h->summary.extint_histogram[i]);
5395 }
5396 }
5397
5398 PRINT_SUMMARY(h->summary.ipi_latency,
5399 "IPI latency \n");
5400 for(i=0; i<256; i++)
5401 if(h->summary.ipi_count[i])
5402 printf(" [%3d] %10d\n",
5403 i, h->summary.ipi_count[i]);
5404 hvm_io_address_summary(h->summary.io.pio, "IO address summary:");
5405 hvm_io_address_summary(h->summary.io.mmio, "MMIO address summary:");
5406 }
5407
5408 /* ---- Shadow records ---- */
5409 union shadow_event
5410 {
5411 unsigned event;
5412 struct {
5413 unsigned minor:8,
5414 paging_levels:4;
5415 };
5416 };
5417
5418 /* WARNING - not thread safe */
5419 #define FLAGSTRING(_name, _char) \
5420 if(e->flag_ ## _name) \
5421 flagstring[i] = _char; \
5422 i++;
5423
5424 char * flag_string(struct pf_xen_extra *e)
5425 {
5426 static char flagstring[32];
5427 int i=0;
5428
5429 for(i=0; i<32; i++)
5430 flagstring[i]='-';
5431
5432 i=0;
5433
5434 if(e->flag_set_ad)
5435 flagstring[i]='d';
5436 else if(e->flag_set_a)
5437 flagstring[i]='a';
5438 i++;
5439
5440 FLAGSTRING(shadow_l1_get_ref, 'g');
5441 FLAGSTRING(shadow_l1_put_ref, 'p');
5442 //FLAGSTRING(l2_propagate, '2');
5443 FLAGSTRING(demote, 'D');
5444 FLAGSTRING(promote, 'P');
5445 FLAGSTRING(wrmap, 'w');
5446 FLAGSTRING(wrmap_guess_found, 'G');
5447 //FLAGSTRING(wrmap_brute_force, 'b');
5448 FLAGSTRING(early_unshadow, 'e');
5449 FLAGSTRING(prealloc_unhook, 'H');
5450 FLAGSTRING(unsync, 'u');
5451 FLAGSTRING(oos_fixup_add, 'a');
5452 FLAGSTRING(oos_fixup_evict, 'x');
5453
5454 flagstring[i]=0;
5455
5456 return flagstring;
5457 }
5458
5459 void shadow_emulate_postprocess(struct hvm_data *h)
5460 {
5461 struct pf_xen_extra *e = &h->inflight.pf_xen;
5462
5463 if ( opt.summary_info )
5464 {
5465 update_eip(&h->v->d->emulate_eip_list,
5466 h->rip,
5467 h->arc_cycles,
5468 0, NULL);
5469 update_cycles(&h->summary.pf_xen[PF_XEN_EMULATE], h->arc_cycles);
5470 update_cycles(&h->summary.pf_xen_emul[e->pt_level], h->arc_cycles);
5471 if(h->prealloc_unpin)
5472 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_PREALLOC_UNPIN], h->arc_cycles);
5473 if(e->flag_prealloc_unhook)
5474 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_PREALLOC_UNHOOK], h->arc_cycles);
5475 if(e->flag_early_unshadow)
5476 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_EARLY_UNSHADOW], h->arc_cycles);
5477 if(e->flag_set_changed)
5478 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_SET_CHANGED], h->arc_cycles);
5479 else
5480 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_SET_UNCHANGED], h->arc_cycles);
5481 if(e->flag_set_flush)
5482 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_SET_FLUSH], h->arc_cycles);
5483 if(e->flag_set_error)
5484 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_SET_ERROR], h->arc_cycles);
5485 if(e->flag_promote)
5486 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_PROMOTE], h->arc_cycles);
5487 if(e->flag_demote)
5488 update_cycles(&h->summary.pf_xen_emul[PF_XEN_EMUL_DEMOTE], h->arc_cycles);
5489 /* more summary info */
5490
5491 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_EMULATE);
5492 }
5493
5494 if(opt.scatterplot_unpin_promote) {
5495 if(e->flag_early_unshadow)
5496 scatterplot_vs_time(h->exit_tsc, -10);
5497 if(h->prealloc_unpin)
5498 scatterplot_vs_time(h->exit_tsc, 0);
5499 if(e->flag_promote) {
5500 if(opt.with_cr3_enumeration) {
5501 if(h->v->cr3.data)
5502 scatterplot_vs_time(h->exit_tsc, h->v->cr3.data->cr3_id);
5503 } else
5504 scatterplot_vs_time(h->exit_tsc, 2);
5505 }
5506 }
5507
5508
5509 }
5510
5511 void shadow_emulate_process(struct record_info *ri, struct hvm_data *h)
5512 {
5513 struct pf_xen_extra *e = &h->inflight.pf_xen;
5514 union {
5515 /* for PAE, guest_l1e may be 64 while guest_va may be 32;
5516 so put it first for alignment sake. */
5517 struct {
5518 unsigned gl1e, write_val;
5519 unsigned va;
5520 unsigned flags:29, emulation_count:3;
5521 } gpl2;
5522 struct {
5523 unsigned long long gl1e, write_val;
5524 unsigned va;
5525 unsigned flags:29, emulation_count:3;
5526 } gpl3;
5527 struct {
5528 unsigned long long gl1e, write_val;
5529 unsigned long long va;
5530 unsigned flags:29, emulation_count:3;
5531 } gpl4;
5532 } *r = (typeof(r))ri->d;
5533
5534 union shadow_event sevt = { .event = ri->event };
5535 int rec_gpl = sevt.paging_levels + 2;
5536
5537 if ( rec_gpl != h->v->guest_paging_levels )
5538 {
5539 fprintf(warn, "%s: record paging levels %d, guest paging levels %d. Switching.\n",
5540 __func__, rec_gpl, h->v->guest_paging_levels);
5541 h->v->guest_paging_levels = rec_gpl;
5542 }
5543
5544 /* Fill in extended information */
5545 switch(rec_gpl)
5546 {
5547 case 2:
5548 if(sizeof(r->gpl2) != ri->extra_words * 4)
5549 {
5550 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5551 __func__, sizeof(r->gpl2), h->v->guest_paging_levels,
5552 ri->extra_words * 4);
5553
5554 error(ERR_RECORD, ri);
5555 return;
5556 }
5557 e->va = r->gpl2.va;
5558 e->flags = r->gpl2.flags;
5559 e->gl1e = r->gpl2.gl1e;
5560 e->wval = r->gpl2.write_val;
5561 break;
5562 case 3:
5563 if(sizeof(r->gpl3) != ri->extra_words * 4)
5564 {
5565 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5566 __func__, sizeof(r->gpl3), h->v->guest_paging_levels,
5567 ri->extra_words * 4);
5568 error(ERR_RECORD, ri);
5569 return;
5570 }
5571 e->va = r->gpl3.va;
5572 e->flags = r->gpl3.flags;
5573 e->gl1e = r->gpl3.gl1e;
5574 e->wval = r->gpl3.write_val;
5575 break;
5576 case 4:
5577 if(sizeof(r->gpl4) != ri->extra_words * 4)
5578 {
5579 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5580 __func__, sizeof(r->gpl4), h->v->guest_paging_levels,
5581 ri->extra_words * 4);
5582 error(ERR_RECORD, ri);
5583 return;
5584 }
5585 e->va = r->gpl4.va;
5586 e->flags = r->gpl4.flags;
5587 e->gl1e = r->gpl4.gl1e;
5588 e->wval = r->gpl4.write_val;
5589 break;
5590 }
5591
5592 pf_preprocess(e,rec_gpl);
5593
5594 if(opt.dump_all)
5595 printf("]%s emulate va %llx gl1e %8llx wval %8llx flags %s(%x) pt_level %d corr %8llx\n",
5596 ri->dump_header,
5597 e->va,
5598 e->gl1e, e->wval,
5599 flag_string(e), e->flags,
5600 e->pt_level, e->corresponding_va);
5601
5602 if ( hvm_set_postprocess(h, shadow_emulate_postprocess) )
5603 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
5604 }
5605
5606 struct shadow_emulate_other {
5607 unsigned long long gfn, va;
5608 };
5609
5610 #define SHADOW_OTHER_LOGS_GFN_NOT_GMFN 1
5611
5612 void shadow_parse_other(struct record_info *ri,
5613 struct shadow_emulate_other *o,
5614 struct hvm_data *h) {
5615 union {
5616 /* for PAE, guest_l1e may be 64 while guest_va may be 32;
5617 so put it first for alignment sake. */
5618 #if SHADOW_OTHER_LOGS_GFN_NOT_GMFN
5619 /* D'OH! Accidentally used mfn_t in the struct, so gmfns are always
5620 64-bit... :-/ */
5621 struct {
5622 unsigned int gfn, va;
5623 } gpl2;
5624 #endif
5625 struct {
5626 unsigned long long gfn;
5627 unsigned int va;
5628 } gpl3;
5629 struct {
5630 unsigned long long gfn, va;
5631 } gpl4;
5632 } *r = (typeof(r))ri->d;
5633
5634
5635 union shadow_event sevt = { .event = ri->event };
5636 int rec_gpl = sevt.paging_levels + 2;
5637
5638 if ( rec_gpl != h->v->guest_paging_levels )
5639 {
5640 fprintf(warn, "%s: record paging levels %d, guest paging levels %d. Switching.\n",
5641 __func__, rec_gpl, h->v->guest_paging_levels);
5642 h->v->guest_paging_levels = rec_gpl;
5643 }
5644
5645 switch(rec_gpl)
5646 {
5647 #if SHADOW_OTHER_LOGS_GFN_NOT_GMFN
5648 case 2:
5649 if(sizeof(r->gpl2) != ri->extra_words * 4)
5650 {
5651 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5652 __func__, sizeof(r->gpl2), rec_gpl,
5653 ri->extra_words * 4);
5654 error(ERR_RECORD, ri);
5655 return;
5656 }
5657 o->va = r->gpl2.va;
5658 o->gfn = r->gpl2.gfn;
5659 break;
5660 #else
5661 case 2:
5662 /* FALLTHRU */
5663 #endif
5664 case 3:
5665 if(sizeof(r->gpl3) != ri->extra_words * 4)
5666 {
5667 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5668 __func__, sizeof(r->gpl3), rec_gpl,
5669 ri->extra_words * 4);
5670 error(ERR_RECORD, ri);
5671 return;
5672 }
5673 o->va = r->gpl3.va;
5674 o->gfn = r->gpl3.gfn;
5675 break;
5676 case 4:
5677 if(sizeof(r->gpl4) != ri->extra_words * 4)
5678 {
5679 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5680 __func__, sizeof(r->gpl4), rec_gpl,
5681 ri->extra_words * 4);
5682 error(ERR_RECORD, ri);
5683 return;
5684 }
5685 o->va = r->gpl4.va;
5686 o->gfn = r->gpl4.gfn;
5687 break;
5688 }
5689 }
5690
5691 #if 0
5692 void shadow_unsync_postprocess(struct hvm_data *h)
5693 {
5694 struct pf_xen_extra *e = &h->inflight.pf_xen;
5695
5696 if(h->resyncs > 1)
5697 fprintf(warn, "Strange, %d resyncs for an unsync!\n",
5698 h->resyncs);
5699
5700 if(opt.summary_info) {
5701 update_cycles(&h->summary.pf_xen[PF_XEN_EMULATE_UNSYNC],
5702 h->arc_cycles);
5703 if(h->resyncs <= 1)
5704 update_cycles(&h->summary.pf_xen_unsync[h->resyncs],
5705 h->arc_cycles);
5706 }
5707 }
5708
5709
5710 void shadow_unsync_process(struct record_info *ri, struct hvm_data *h)
5711 {
5712 struct pf_xen_extra *e = &h->inflight.pf_xen;
5713 struct shadow_emulate_other r;
5714
5715 shadow_parse_other(ri, &r, h);
5716
5717 e->gmfn = r.gmfn;
5718 e->va = r.va;
5719
5720 pf_preprocess(e, h->v->guest_paging_levels);
5721
5722 if(opt.dump_all)
5723 printf("]%s shadow unsync gmfn %llx va %llx pt_level %d corr %llx\n",
5724 ri->dump_header,
5725 e->gmfn,
5726 e->va,
5727 e->pt_level,
5728 e->corresponding_va);
5729
5730 if ( hvm_set_postprocess(h, shadow_unsync_postprocess) )
5731 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
5732 }
5733 #endif
5734
5735 void shadow_fault_generic_postprocess(struct hvm_data *h);
5736
5737 void shadow_emulate_other_process(struct record_info *ri, struct hvm_data *h)
5738 {
5739 struct pf_xen_extra *e = &h->inflight.pf_xen;
5740 struct shadow_emulate_other r;
5741 union shadow_event sevt = { .event = ri->event };
5742
5743 shadow_parse_other(ri, &r, h);
5744
5745 e->gfn = r.gfn;
5746 e->va = r.va;
5747 e->pf_case = sevt.minor;
5748
5749 pf_preprocess(e, h->v->guest_paging_levels);
5750
5751 if(opt.dump_all)
5752 printf("]%s shadow %s gfn %llx va %llx\n",
5753 ri->dump_header,
5754 pf_xen_name[sevt.minor],
5755 e->gfn,
5756 e->va);
5757
5758 if ( hvm_set_postprocess(h, shadow_fault_generic_postprocess) )
5759 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
5760 }
5761
5762 void shadow_fixup_postprocess(struct hvm_data *h)
5763 {
5764 struct pf_xen_extra *e = &h->inflight.pf_xen;
5765
5766 if ( opt.summary_info )
5767 {
5768 update_cycles(&h->summary.pf_xen[PF_XEN_FIXUP], h->arc_cycles);
5769 if(h->prealloc_unpin) {
5770 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_PREALLOC_UNPIN], h->arc_cycles);
5771 }
5772 if(e->flag_unsync) {
5773 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_UNSYNC], h->arc_cycles);
5774 if(h->resyncs < PF_XEN_FIXUP_UNSYNC_RESYNC_MAX)
5775 update_cycles(&h->summary.pf_xen_fixup_unsync_resync[h->resyncs],
5776 h->arc_cycles);
5777 else
5778 update_cycles(&h->summary.pf_xen_fixup_unsync_resync[PF_XEN_FIXUP_UNSYNC_RESYNC_MAX],
5779 h->arc_cycles);
5780 }
5781 if(e->flag_oos_fixup_add)
5782 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_OOS_ADD], h->arc_cycles);
5783 if(e->flag_oos_fixup_evict)
5784 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_OOS_EVICT], h->arc_cycles);
5785 if(e->flag_promote)
5786 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_PROMOTE], h->arc_cycles);
5787 if(e->flag_wrmap) {
5788 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_WRMAP], h->arc_cycles);
5789 if(e->flag_wrmap_brute_force || h->wrmap_bf)
5790 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_BRUTE_FORCE], h->arc_cycles);
5791 } else if(e->flag_wrmap_brute_force || h->wrmap_bf) {
5792 fprintf(warn, "Strange: wrmap_bf but not wrmap!\n");
5793 }
5794
5795
5796 if(!(e->flag_promote || h->prealloc_unpin || e->flag_unsync))
5797 update_cycles(&h->summary.pf_xen_fixup[PF_XEN_FIXUP_UPDATE_ONLY], h->arc_cycles);
5798 /* more summary info */
5799
5800 if(e->flag_unsync)
5801 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_UNSYNC);
5802 else
5803 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_FIXUP);
5804 }
5805
5806 if(opt.scatterplot_unpin_promote) {
5807 if(h->prealloc_unpin)
5808 scatterplot_vs_time(h->exit_tsc, 0);
5809 if(e->flag_promote) {
5810 if(opt.with_cr3_enumeration) {
5811 if(h->v->cr3.data)
5812 scatterplot_vs_time(h->exit_tsc, h->v->cr3.data->cr3_id);
5813 } else
5814 scatterplot_vs_time(h->exit_tsc, 2);
5815 }
5816 }
5817 }
5818
5819 void shadow_fixup_process(struct record_info *ri, struct hvm_data *h)
5820 {
5821 struct pf_xen_extra *e = &h->inflight.pf_xen;
5822 union {
5823 /* for PAE, guest_l1e may be 64 while guest_va may be 32;
5824 so put it first for alignment sake. */
5825 struct {
5826 unsigned int gl1e, va, flags;
5827 } gpl2;
5828 struct {
5829 unsigned long long gl1e;
5830 unsigned int va, flags;
5831 } gpl3;
5832 struct {
5833 unsigned long long gl1e, va;
5834 unsigned int flags;
5835 } gpl4;
5836 } *r = (typeof(r))ri->d;
5837 union shadow_event sevt = { .event = ri->event };
5838 int rec_gpl = sevt.paging_levels + 2;
5839
5840 if ( rec_gpl != h->v->guest_paging_levels )
5841 {
5842 fprintf(warn, "%s: record paging levels %d, guest paging levels %d. Switching.\n",
5843 __func__, rec_gpl, h->v->guest_paging_levels);
5844 h->v->guest_paging_levels = rec_gpl;
5845 }
5846
5847 switch(rec_gpl)
5848 {
5849 case 2:
5850 if(sizeof(r->gpl2) != ri->extra_words * 4)
5851 {
5852 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5853 __func__, sizeof(r->gpl2), h->v->guest_paging_levels,
5854 ri->extra_words * 4);
5855 error(ERR_RECORD, ri);
5856 return;
5857 }
5858 e->va = r->gpl2.va;
5859 e->flags = r->gpl2.flags;
5860 e->gl1e = r->gpl2.gl1e;
5861 break;
5862 case 3:
5863 if(sizeof(r->gpl3) != ri->extra_words * 4)
5864 {
5865 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5866 __func__, sizeof(r->gpl3), h->v->guest_paging_levels,
5867 ri->extra_words * 4);
5868 error(ERR_RECORD, ri);
5869 return;
5870 }
5871 e->va = r->gpl3.va;
5872 e->flags = r->gpl3.flags;
5873 e->gl1e = r->gpl3.gl1e;
5874 break;
5875 case 4:
5876 if(sizeof(r->gpl4) != ri->extra_words * 4)
5877 {
5878 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5879 __func__, sizeof(r->gpl4), h->v->guest_paging_levels,
5880 ri->extra_words * 4);
5881 error(ERR_RECORD, ri);
5882 return;
5883 }
5884 e->va = r->gpl4.va;
5885 e->flags = r->gpl4.flags;
5886 e->gl1e = r->gpl4.gl1e;
5887 break;
5888 }
5889
5890 pf_preprocess(e,rec_gpl);
5891
5892 if(opt.dump_all)
5893 {
5894 if ( e->flag_unsync )
5895 printf("]%s fixup:unsync va %llx gl1e %llx corr %llx flags (%x)%s\n",
5896 ri->dump_header,
5897 e->va, e->gl1e,
5898 e->corresponding_va,
5899 e->flags,
5900 flag_string(e));
5901 else
5902 printf("]%s fixup va %llx gl1e %llx flags (%x)%s\n",
5903 ri->dump_header,
5904 e->va, e->gl1e, e->flags,
5905 flag_string(e));
5906 }
5907
5908 if ( hvm_set_postprocess(h, shadow_fixup_postprocess) )
5909 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
5910 }
5911
5912 void shadow_mmio_postprocess(struct hvm_data *h)
5913 {
5914 struct pf_xen_extra *e = &h->inflight.pf_xen;
5915 if ( opt.summary_info )
5916 {
5917 if(e->pf_case)
5918 update_cycles(&h->summary.pf_xen[e->pf_case],
5919 h->arc_cycles);
5920 else
5921 fprintf(warn, "Strange, pf_case 0!\n");
5922
5923 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_MMIO);
5924 }
5925
5926 if(opt.with_mmio_enumeration)
5927 enumerate_mmio(h);
5928 }
5929
5930 void shadow_mmio_process(struct record_info *ri, struct hvm_data *h)
5931 {
5932 struct pf_xen_extra *e = &h->inflight.pf_xen;
5933 struct mmio_info *m = &h->inflight.mmio;
5934 union {
5935 /* for PAE, guest_l1e may be 64 while guest_va may be 32;
5936 so put it first for alignment sake. */
5937 struct {
5938 unsigned int va;
5939 } gpl2;
5940 struct {
5941 unsigned long long va;
5942 } gpl4;
5943 } *r = (typeof(r))ri->d;
5944 union shadow_event sevt = { .event = ri->event };
5945 int rec_gpl = sevt.paging_levels + 2;
5946
5947 if ( rec_gpl != h->v->guest_paging_levels )
5948 {
5949 fprintf(warn, "%s: record paging levels %d, guest paging levels %d. Switching.\n",
5950 __func__, rec_gpl, h->v->guest_paging_levels);
5951 h->v->guest_paging_levels = rec_gpl;
5952 }
5953
5954 switch(rec_gpl)
5955 {
5956 case 2:
5957 case 3:
5958 if(sizeof(r->gpl2) != ri->extra_words * 4)
5959 {
5960 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5961 __func__, sizeof(r->gpl2), h->v->guest_paging_levels,
5962 ri->extra_words * 4);
5963 error(ERR_RECORD, ri);
5964 return;
5965 }
5966 e->va = m->va = r->gpl2.va;
5967 break;
5968 case 4:
5969 if(sizeof(r->gpl4) != ri->extra_words * 4)
5970 {
5971 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
5972 __func__, sizeof(r->gpl4), h->v->guest_paging_levels,
5973 ri->extra_words * 4);
5974 error(ERR_RECORD, ri);
5975 return;
5976 }
5977 e->va = m->va = r->gpl4.va;
5978 break;
5979 }
5980
5981 if(opt.dump_all)
5982 printf("]%s %smmio va %llx\n",
5983 ri->dump_header,
5984 (e->pf_case==PF_XEN_FAST_MMIO)?"fast ":"",
5985 e->va);
5986
5987 if ( hvm_set_postprocess(h, shadow_mmio_postprocess) )
5988 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
5989 }
5990
5991 void shadow_propagate_postprocess(struct hvm_data *h)
5992 {
5993 struct pf_xen_extra *e = &h->inflight.pf_xen;
5994
5995 if ( opt.summary_info )
5996 {
5997 if(e->pf_case)
5998 update_cycles(&h->summary.pf_xen[e->pf_case],
5999 h->arc_cycles);
6000 else
6001 fprintf(warn, "Strange, pf_case 0!\n");
6002
6003 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_PROPAGATE);
6004 }
6005 }
6006
6007 void shadow_propagate_process(struct record_info *ri, struct hvm_data *h)
6008 {
6009 struct pf_xen_extra *e = &h->inflight.pf_xen;
6010 union {
6011 /* for PAE, guest_l1e may be 64 while guest_va may be 32;
6012 so put it first for alignment sake. */
6013 struct {
6014 unsigned int gl1e, va, flags;
6015 } gpl2;
6016 struct {
6017 unsigned long long gl1e;
6018 unsigned int va, flags;
6019 } gpl3;
6020 struct {
6021 unsigned long long gl1e, va;
6022 unsigned int flags;
6023 } gpl4;
6024 } *r = (typeof(r))ri->d;
6025 union shadow_event sevt = { .event = ri->event };
6026 int rec_gpl = sevt.paging_levels + 2;
6027
6028 if ( rec_gpl != h->v->guest_paging_levels )
6029 {
6030 fprintf(warn, "%s: record paging levels %d, guest paging levels %d. Switching.\n",
6031 __func__, rec_gpl, h->v->guest_paging_levels);
6032 h->v->guest_paging_levels = rec_gpl;
6033 }
6034
6035 switch(rec_gpl)
6036 {
6037 case 2:
6038 if(sizeof(r->gpl2) != ri->extra_words * 4)
6039 {
6040 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
6041 __func__, sizeof(r->gpl2), h->v->guest_paging_levels,
6042 ri->extra_words * 4);
6043 error(ERR_RECORD, ri);
6044 return;
6045 }
6046 e->va = r->gpl2.va;
6047 e->flags = r->gpl2.flags;
6048 e->gl1e = r->gpl2.gl1e;
6049 break;
6050 case 3:
6051 if(sizeof(r->gpl3) != ri->extra_words * 4)
6052 {
6053 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
6054 __func__, sizeof(r->gpl3), h->v->guest_paging_levels,
6055 ri->extra_words * 4);
6056 error(ERR_RECORD, ri);
6057 return;
6058 }
6059 e->va = r->gpl3.va;
6060 e->flags = r->gpl3.flags;
6061 e->gl1e = r->gpl3.gl1e;
6062 break;
6063 case 4:
6064 if(sizeof(r->gpl4) != ri->extra_words * 4)
6065 {
6066 fprintf(warn, "%s: expected %zd bytes for %d-level guest, got %d!\n",
6067 __func__, sizeof(r->gpl4), h->v->guest_paging_levels,
6068 ri->extra_words * 4);
6069 error(ERR_RECORD, ri);
6070 return;
6071 }
6072 e->va = r->gpl4.va;
6073 e->flags = r->gpl4.flags;
6074 e->gl1e = r->gpl4.gl1e;
6075 break;
6076 }
6077
6078 if(opt.dump_all)
6079 printf("]%s propagate va %llx gl1e %llx flags (%x)%s\n",
6080 ri->dump_header,
6081 e->va, e->gl1e, e->flags,
6082 flag_string(e));
6083
6084 if ( hvm_set_postprocess(h, shadow_propagate_postprocess) )
6085 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
6086 }
6087
6088 void shadow_fault_generic_dump(unsigned int event, uint32_t *d,
6089 const char *prefix, const char * dump_header)
6090 {
6091 const char *evt_string;
6092 char evt_number[10];
6093 union shadow_event sevt = { .event = event };
6094 int i;
6095
6096 if(sevt.minor < PF_XEN_MAX && pf_xen_name[sevt.minor])
6097 {
6098 evt_string = pf_xen_name[sevt.minor];
6099 }
6100 else
6101 {
6102 snprintf(evt_number, 10, "%d", sevt.minor);
6103 evt_string = evt_number;
6104 }
6105
6106 printf("%s%s shadow %s gl %d [",
6107 prefix,
6108 dump_header,
6109 evt_string,
6110 sevt.paging_levels);
6111
6112 for(i=0; i<4; i++)
6113 {
6114 printf(" %x", d[i]);
6115 }
6116
6117 printf(" ]\n");
6118 }
6119
6120 void shadow_fault_generic_postprocess(struct hvm_data *h)
6121 {
6122 struct pf_xen_extra *e = &h->inflight.pf_xen;
6123 if ( e->pf_case < PF_XEN_NOT_SHADOW || e->pf_case > PF_XEN_LAST_FAULT )
6124 {
6125 fprintf(warn, "%s: Strange, unexpected case %d\n",
6126 __func__, e->pf_case);
6127 return;
6128 }
6129
6130 if(opt.summary_info) {
6131 update_cycles(&h->summary.pf_xen[e->pf_case],
6132 h->arc_cycles);
6133
6134 hvm_update_short_summary(h, HVM_SHORT_SUMMARY_PROPAGATE);
6135 }
6136 }
6137
6138 void shadow_fault_generic_process(struct record_info *ri, struct hvm_data *h)
6139 {
6140 union shadow_event sevt = { .event = ri->event };
6141
6142 /* pf-case traces, vs others */
6143 h->inflight.generic.event = ri->event;
6144 bcopy(ri->d, h->inflight.generic.d, sizeof(unsigned int) * 4);
6145
6146 if(opt.dump_all)
6147 shadow_fault_generic_dump(h->inflight.generic.event,
6148 h->inflight.generic.d,
6149 "]", ri->dump_header);
6150
6151 h->inflight.pf_xen.pf_case = sevt.minor;
6152 if ( hvm_set_postprocess(h, shadow_fault_generic_postprocess) )
6153 fprintf(warn, "%s: Strange, postprocess already set\n", __func__);
6154 }
6155
6156 void shadow_resync_process(struct record_info *ri, struct hvm_data *h)
6157 {
6158 struct {
6159 unsigned long long gfn;
6160 } *r = (typeof(r))ri->d;
6161
6162 if(opt.dump_all)
6163 printf(" %s oos resync %s gfn %llx\n",
6164 ri->dump_header,
6165 (ri->event == TRC_SHADOW_RESYNC_FULL)?"full":"only",
6166 r->gfn);
6167
6168 h->resyncs++;
6169 }
6170
6171 void shadow_prealloc_unpin_process(struct record_info *ri, struct hvm_data *h) {
6172 struct {
6173 unsigned long long gfn;
6174 } *r = (typeof(r))ri->d;
6175
6176 if(opt.dump_all)
6177 printf(" %s prealloc-unpin gfn %llx\n",
6178 ri->dump_header, r->gfn);
6179
6180 if(h->prealloc_unpin)
6181 fprintf(warn, "Strange, more than one prealloc_unpin per arc!\n");
6182
6183 h->prealloc_unpin = 1;
6184
6185 if(opt.with_cr3_enumeration)
6186 cr3_prealloc_unpin(h->v, r->gfn);
6187 }
6188
6189 void shadow_wrmap_bf_process(struct record_info *ri, struct hvm_data *h) {
6190 struct {
6191 unsigned long long gfn;
6192 } *r = (typeof(r))ri->d;
6193
6194 if(opt.dump_all)
6195 printf(" %s wrmap-bf gfn %llx\n",
6196 ri->dump_header, r->gfn);
6197
6198 h->wrmap_bf = 1;
6199 }
6200
6201 void shadow_process(struct pcpu_info *p)
6202 {
6203 struct record_info *ri = &p->ri;
6204 struct hvm_data *h;
6205
6206 union shadow_event sevt = { .event = ri->event };
6207
6208 int gpl = sevt.paging_levels + 2;
6209
6210 assert(p->current);
6211 if(vcpu_set_data_type(p->current, VCPU_DATA_HVM))
6212 return;
6213
6214 h = &p->current->hvm;
6215
6216 if(!h->init || !h->vmexit_valid)
6217 {
6218 if(opt.dump_all)
6219 shadow_fault_generic_dump(ri->event,
6220 ri->d,
6221 "!", ri->dump_header);
6222 return;
6223 }
6224
6225 if(sevt.minor <= PF_XEN_NOT_SHADOW) {
6226 if(p->current->guest_paging_levels != gpl)
6227 {
6228 fprintf(warn, "%s: Changing guest paging levels to %d\n",
6229 __func__, gpl);
6230 p->current->guest_paging_levels = gpl;
6231 }
6232 }
6233
6234 if(sevt.minor <= PF_XEN_LAST_FAULT) {
6235 h->inflight.pf_xen.pf_case = sevt.minor;
6236 if(opt.summary) {
6237 hvm_set_summary_handler(h, hvm_pf_xen_summary, NULL);
6238 }
6239 }
6240
6241 /* FIXME - mask out paging levels */
6242 switch(sevt.minor)
6243 {
6244 case PF_XEN_NOT_SHADOW:
6245 shadow_propagate_process(ri, h);
6246 break;
6247 case PF_XEN_EMULATE:
6248 shadow_emulate_process(ri, h);
6249 break;
6250 case PF_XEN_FIXUP:
6251 shadow_fixup_process(ri, h);
6252 break;
6253 case PF_XEN_MMIO:
6254 case PF_XEN_FAST_MMIO:
6255 shadow_mmio_process(ri, h);
6256 break;
6257 case PF_XEN_EMULATE_UNSHADOW_USER:
6258 case PF_XEN_EMULATE_UNSHADOW_EVTINJ:
6259 case PF_XEN_EMULATE_UNSHADOW_UNHANDLED:
6260 shadow_emulate_other_process(ri, h);
6261 break;
6262 #if 0
6263 case PF_XEN_EMULATE_UNSYNC:
6264 shadow_unsync_process(ri, h);
6265 break;
6266 #endif
6267 case SHADOW_RESYNC_FULL:
6268 case SHADOW_RESYNC_ONLY:
6269 shadow_resync_process(ri, h);
6270 break;
6271 case SHADOW_PREALLOC_UNPIN:
6272 shadow_prealloc_unpin_process(ri, h);
6273 break;
6274 case SHADOW_WRMAP_BF:
6275 shadow_wrmap_bf_process(ri, h);
6276 break;
6277 default:
6278 if(sevt.minor <= PF_XEN_LAST_FAULT) {
6279 shadow_fault_generic_process(ri, h);
6280 } else {
6281 warn_once("Warning: processing shadow as generic\n");
6282 process_generic(ri);
6283 }
6284 break;
6285 }
6286 }
6287
6288 /* ---- PV guests ---- */
6289 union pv_event {
6290 unsigned event;
6291 struct {
6292 unsigned minor:8,
6293 x64:1,
6294 unused1:3,
6295 sub:4,
6296 main:12,
6297 unused:4;
6298 };
6299 };
6300
6301 void pv_hypercall_process(struct record_info *ri, struct pv_data *pv) {
6302 union {
6303 struct {
6304 uint32_t eip, eax;
6305 } x32;
6306 struct {
6307 uint64_t eip;
6308 uint32_t eax;
6309 } x64;
6310 } * r = (typeof(r)) ri->d;
6311 union pv_event pevt = { .event = ri->event };
6312 unsigned long long eip;
6313 unsigned int eax;
6314
6315 if(pevt.x64) {
6316 eip = r->x64.eip;
6317 eax = r->x64.eax;
6318 } else {
6319 eip = r->x32.eip;
6320 eax = r->x32.eax;
6321 }
6322
6323 if(opt.summary_info) {
6324 if(eax < PV_HYPERCALL_MAX)
6325 pv->hypercall_count[eax]++;
6326 }
6327
6328 if(opt.dump_all) {
6329 if(eax < HYPERCALL_MAX)
6330 printf(" %s hypercall %2x (%s) eip %llx\n",
6331 ri->dump_header, eax,
6332 hypercall_name[eax], eip);
6333 else
6334 printf(" %s hypercall %x eip %llx\n",
6335 ri->dump_header, eax, eip);
6336 }
6337 }
6338
6339 void pv_trap_process(struct record_info *ri, struct pv_data *pv) {
6340 union {
6341 struct {
6342 unsigned int eip;
6343 unsigned trapnr:15,
6344 use_error_code:1,
6345 error_code:16;
6346 } x32;
6347 struct {
6348 unsigned long long eip;
6349 unsigned trapnr:15,
6350 use_error_code:1,
6351 error_code:16;
6352 } x64;
6353 } * r = (typeof(r)) ri->d;
6354 union pv_event pevt = { .event = ri->event };
6355 unsigned long long eip;
6356 unsigned trapnr, use_ec, ec;
6357
6358 if(pevt.x64) {
6359 eip = r->x64.eip;
6360 trapnr = r->x64.trapnr;
6361 use_ec = r->x64.use_error_code;
6362 ec = r->x64.error_code;
6363 } else {
6364 eip = r->x32.eip;
6365 trapnr = r->x32.trapnr;
6366 use_ec = r->x32.use_error_code;
6367 ec = r->x32.error_code;
6368 }
6369
6370 if(opt.summary_info) {
6371 if(trapnr < PV_TRAP_MAX)
6372 pv->trap_count[trapnr]++;
6373 }
6374
6375 if(opt.dump_all) {
6376 printf(" %s trap %x eip %llx",
6377 ri->dump_header, trapnr, eip);
6378 if(use_ec)
6379 printf(" ec %x\n", ec);
6380 else
6381 printf("\n");
6382 }
6383 }
6384
6385 void pv_ptwr_emulation_process(struct record_info *ri, struct pv_data *pv) {
6386 union pv_event pevt = { .event = ri->event };
6387 union {
6388 /* gpl2 is deprecated */
6389 struct {
6390 unsigned long long pte;
6391 unsigned int addr, eip;
6392 } gpl3;
6393 struct {
6394 unsigned long long pte;
6395 unsigned long long addr, eip;
6396 } gpl4;
6397 } *r = (typeof(r))ri->d;
6398 struct {
6399 unsigned long long pte, addr, eip;
6400 } e;
6401
6402 switch ( pevt.minor ) {
6403 case PV_PTWR_EMULATION_PAE:
6404 if ( pevt.x64 )
6405 {
6406 fprintf(warn, "Strange: PV_PTWR_EMULATION, but x64! %x\n",
6407 ri->event);
6408 error(ERR_RECORD, ri);
6409 }
6410 e.pte = r->gpl3.pte;
6411 e.addr = r->gpl3.addr;
6412 e.eip = r->gpl3.eip;
6413 break;
6414 case PV_PTWR_EMULATION:
6415 if ( !pevt.x64 )
6416 {
6417 fprintf(warn, "Strange: PV_PTWR_EMULATION, but !x64! %x\n",
6418 ri->event);
6419 error(ERR_RECORD, ri);
6420 }
6421 e.pte = r->gpl4.pte;
6422 e.addr = r->gpl4.addr;
6423 e.eip = r->gpl4.eip;
6424 break;
6425 default:
6426 fprintf(warn, "ERROR: Unknown PV_PTRW minor type %d!\n",
6427 pevt.minor);
6428 error(ERR_RECORD, ri);
6429 return;
6430 }
6431
6432 if ( opt.dump_all )
6433 {
6434 printf(" %s ptwr l1e %llx eip %llx addr %llx\n",
6435 ri->dump_header,
6436 e.pte, e.eip, e.addr);
6437 }
6438 }
6439
6440 void pv_generic_process(struct record_info *ri, struct pv_data *pv) {
6441 union pv_event pevt = { .event = ri->event };
6442 if ( opt.dump_all ) {
6443 if(pevt.minor < PV_MAX && pv_name[pevt.minor])
6444 printf(" %s %s",
6445 ri->dump_header,
6446 pv_name[pevt.minor]);
6447 else
6448 printf(" %s PV-%d ",
6449 ri->dump_header, pevt.minor);
6450 if (ri->extra_words) {
6451 int i;
6452 printf("[ ");
6453 for(i=0; i<ri->extra_words; i++) {
6454 printf("%x ", (unsigned)ri->d[i]);
6455 }
6456 printf("]");
6457
6458 }
6459 printf("\n");
6460 }
6461 }
6462
6463 void pv_summary(struct pv_data *pv) {
6464 int i, j;
6465
6466 if(!pv->summary_info)
6467 return;
6468
6469 printf("PV events:\n");
6470 for(i=0; i<PV_MAX; i++) {
6471 int count;
6472
6473 count = pv->count[i];
6474 if (i == PV_HYPERCALL_V2)
6475 count += pv->count[PV_HYPERCALL_SUBCALL];
6476
6477 if (count == 0)
6478 continue;
6479
6480 printf(" %s %d\n", pv_name[i], count);
6481
6482 switch(i) {
6483 case PV_HYPERCALL:
6484 case PV_HYPERCALL_V2:
6485 for(j=0; j<PV_HYPERCALL_MAX; j++) {
6486 if(pv->hypercall_count[j])
6487 printf(" %-29s[%2d]: %6d\n",
6488 hypercall_name[j],
6489 j,
6490 pv->hypercall_count[j]);
6491 }
6492 break;
6493 case PV_TRAP:
6494 for(j=0; j<PV_TRAP_MAX; j++) {
6495 if(pv->trap_count[j])
6496 printf(" [%d] %d\n",
6497 j, pv->trap_count[j]);
6498 }
6499 break;
6500 }
6501 }
6502 }
6503
6504 static const char *grant_table_op_str[] = {
6505 "map_grant_ref", "unmap_grant_ref", "setup_table", "dump_table",
6506 "transfer", "copy", "query_size", "unmap_and_replace",
6507 "set_version", "get_status_frames", "get_version", "swap_grant_ref",
6508 };
6509
6510 static const char *vcpu_op_str[] = {
6511 "initialise", "up", "down", "is_up", "get_runstate_info",
6512 "register_runstate_memory_area", "set_periodic_timer",
6513 "stop_periodic_timer", "set_singleshot_timer", "stop_singleshot_timer",
6514 "register_vcpu_info", "send_nmi", "get_physid",
6515 "register_vcpu_time_memory_area", "register_runstate_phys_area",
6516 "register_vcpu_time_phys_area",
6517 };
6518
6519 static const char *sched_op_str[] = {
6520 "yield", "block", "shutdown", "poll", "remote_shutdown", "shutdown_code",
6521 "watchdog",
6522 };
6523
6524 static const char *cmd_to_str(const char *strings[], size_t n, uint32_t cmd)
6525 {
6526 static char buf[32];
6527
6528 if (cmd < n)
6529 return strings[cmd];
6530
6531 snprintf(buf, sizeof(buf), "unknown (%d)", cmd);
6532 return buf;
6533 }
6534
6535 #define CMD_TO_STR(op) \
6536 static const char * op ## _to_str(uint32_t cmd) { \
6537 return cmd_to_str(op ## _str, ARRAY_SIZE(op ## _str), cmd); \
6538 }
6539
6540 CMD_TO_STR(grant_table_op);
6541 CMD_TO_STR(vcpu_op);
6542 CMD_TO_STR(sched_op);
6543
6544 void pv_hypercall_gather_args(const struct record_info *ri, uint64_t *args)
6545 {
6546 int i, word;
6547
6548 /* Missing arguments are zeroed. */
6549 memset(args, 0, 6 * sizeof(uint64_t));
6550
6551 for (i = 0, word = 1; i < 6 && word < ri->extra_words; i++) {
6552 int present = pv_hypercall_arg_present(ri, i);
6553
6554 switch (present) {
6555 case ARG_32BIT:
6556 args[i] = ri->d[word];
6557 break;
6558 case ARG_64BIT:
6559 args[i] = ((uint64_t)ri->d[word + 1] << 32) | ri->d[word];
6560 break;
6561 }
6562
6563 /* Skip over any words for this argument. */
6564 word += present;
6565 }
6566 }
6567
6568 static void pv_hypercall_print_args(const struct record_info *ri)
6569 {
6570 int i, word;
6571
6572 for (i = 0, word = 1; i < 6 && word < ri->extra_words; i++) {
6573 int present = pv_hypercall_arg_present(ri, i);
6574
6575 switch (present) {
6576 case ARG_MISSING:
6577 printf(" ??");
6578 break;
6579 case ARG_32BIT:
6580 printf(" %08x", ri->d[word]);
6581 break;
6582 case ARG_64BIT:
6583 printf(" %016"PRIu64"", ((uint64_t)ri->d[word + 1] << 32) | ri->d[word]);
6584 break;
6585 }
6586
6587 word += present;
6588 }
6589 }
6590
6591 void pv_hypercall_v2_process(struct record_info *ri, struct pv_data *pv,
6592 const char *indent)
6593 {
6594 int op = pv_hypercall_op(ri);
6595
6596 if(opt.summary_info) {
6597 if(op < PV_HYPERCALL_MAX)
6598 pv->hypercall_count[op]++;
6599 }
6600
6601 if(opt.dump_all) {
6602 uint64_t args[6];
6603
6604 if(op < HYPERCALL_MAX)
6605 printf(" %s%s hypercall %2x (%s)",
6606 ri->dump_header, indent, op, hypercall_name[op]);
6607 else
6608 printf(" %s%s hypercall %2x",
6609 ri->dump_header, indent, op);
6610
6611 switch(op) {
6612 case HYPERCALL_mmu_update:
6613 pv_hypercall_gather_args(ri, args);
6614 printf(" %d updates%s", (uint32_t)args[1] & ~MMU_UPDATE_PREEMPTED,
6615 (args[1] & MMU_UPDATE_PREEMPTED) ? " (preempted)" : "");
6616 break;
6617 case HYPERCALL_multicall:
6618 pv_hypercall_gather_args(ri, args);
6619 printf(" %d calls", (uint32_t)args[1]);
6620 break;
6621 case HYPERCALL_grant_table_op:
6622 pv_hypercall_gather_args(ri, args);
6623 printf(" %s %d ops", grant_table_op_to_str(args[0]), (uint32_t)args[2]);
6624 break;
6625 case HYPERCALL_vcpu_op:
6626 pv_hypercall_gather_args(ri, args);
6627 printf(" %s vcpu %d", vcpu_op_to_str(args[0]), (uint32_t)args[1]);
6628 break;
6629 case HYPERCALL_mmuext_op:
6630 pv_hypercall_gather_args(ri, args);
6631 printf(" %d ops", (uint32_t)args[1]);
6632 break;
6633 case HYPERCALL_sched_op:
6634 pv_hypercall_gather_args(ri, args);
6635 printf(" %s", sched_op_to_str(args[0]));
6636 break;
6637 default:
6638 pv_hypercall_print_args(ri);
6639 break;
6640 }
6641 printf("\n");
6642 }
6643 }
6644
6645 void pv_process(struct pcpu_info *p)
6646 {
6647 struct record_info *ri = &p->ri;
6648 struct vcpu_data *v = p->current;
6649 struct pv_data *pv = &v->pv;
6650
6651 union pv_event pevt = { .event = ri->event };
6652
6653 if(vcpu_set_data_type(p->current, VCPU_DATA_PV))
6654 return;
6655
6656 if(opt.summary_info) {
6657 pv->summary_info=1;
6658
6659 if(pevt.minor == PV_PTWR_EMULATION_PAE)
6660 pv->count[PV_PTWR_EMULATION]++;
6661 else
6662 pv->count[pevt.minor]++;
6663 }
6664
6665 switch(pevt.minor)
6666 {
6667 case PV_HYPERCALL:
6668 pv_hypercall_process(ri, pv);
6669 break;
6670 case PV_TRAP:
6671 pv_trap_process(ri, pv);
6672 break;
6673 case PV_PTWR_EMULATION:
6674 case PV_PTWR_EMULATION_PAE:
6675 pv_ptwr_emulation_process(ri, pv);
6676 break;
6677 case PV_HYPERCALL_V2:
6678 pv_hypercall_v2_process(ri, pv, "");
6679 break;
6680 case PV_HYPERCALL_SUBCALL:
6681 pv_hypercall_v2_process(ri, pv, " ");
6682 break;
6683 default:
6684 pv_generic_process(ri, pv);
6685 break;
6686 }
6687 }
6688
6689 /* ---- Schedule ---- */
6690 struct vcpu_data * vcpu_create(struct domain_data *d, int vid)
6691 {
6692 struct vcpu_data *v;
6693
6694 assert(d->vcpu[vid] == NULL);
6695
6696 fprintf(warn, "Creating vcpu %d for dom %d\n", vid, d->did);
6697
6698 if((v=malloc(sizeof(*v)))==NULL)
6699 {
6700 fprintf(stderr, "%s: malloc %zd failed!\n", __func__, sizeof(*d));
6701 error(ERR_SYSTEM, NULL);
6702 }
6703
6704 bzero(v, sizeof(*v));
6705
6706 v->vid = vid;
6707 v->d = d;
6708 v->p = NULL;
6709 v->runstate.state = RUNSTATE_INIT;
6710 v->runstate.last_oldstate.wrong = RUNSTATE_INIT;
6711
6712 d->vcpu[vid] = v;
6713
6714 assert(v == v->d->vcpu[v->vid]);
6715
6716 if(vid > d->max_vid)
6717 d->max_vid = vid;
6718
6719 return v;
6720 }
6721
6722 /* Called by both domain_create and sched_default_domain_init */
6723 void domain_init(struct domain_data *d, int did)
6724 {
6725 bzero(d, sizeof(*d));
6726
6727 d->did = did;
6728 d->next = NULL;
6729
6730 if(opt.interval.check == INTERVAL_CHECK_DOMAIN)
6731 interval_domain_value_check(d);
6732 }
6733
6734 struct domain_data * domain_create(int did)
6735 {
6736 struct domain_data *d;
6737
6738 fprintf(warn, "Creating domain %d\n", did);
6739
6740 if((d=malloc(sizeof(*d)))==NULL)
6741 {
6742 fprintf(stderr, "%s: malloc %zd failed!\n", __func__, sizeof(*d));
6743 error(ERR_SYSTEM, NULL);
6744 }
6745
6746 /* Initialize domain & vcpus */
6747 domain_init(d, did);
6748
6749 return d;
6750 }
6751
6752 struct domain_data * domain_find(int did)
6753 {
6754 struct domain_data *d, *n, **q;
6755
6756 /* Look for domain, keeping track of the last pointer so we can add
6757 a domain if we need to. */
6758 for ( d = domain_list, q=&domain_list ;
6759 d && (d->did < did) ;
6760 q = &d->next, d=d->next ) ;
6761
6762 if(d && d->did == did)
6763 return d;
6764
6765 /* Make a new domain */
6766 n = domain_create(did);
6767
6768 /* Insert it into the list */
6769 n->next = d;
6770 *q = n;
6771
6772 return n;
6773 }
6774
6775 struct vcpu_data * vcpu_find(int did, int vid)
6776 {
6777 struct domain_data *d;
6778 struct vcpu_data *v;
6779
6780 d = domain_find(did);
6781
6782 v = d->vcpu[vid];
6783
6784 if(!v)
6785 v = vcpu_create(d, vid);
6786
6787 return v;
6788 }
6789
6790 void pcpu_runstate_update(struct pcpu_info *p, tsc_t tsc)
6791 {
6792 if ( p->time.tsc )
6793 {
6794 if ( p->current->d->did == IDLE_DOMAIN )
6795 update_cycles(&p->time.idle, tsc - p->time.tsc);
6796 else
6797 update_cycles(&p->time.running, tsc - p->time.tsc);
6798 p->time.tsc = 0;
6799 }
6800 }
6801
6802 void vcpu_prev_update(struct pcpu_info *p, struct vcpu_data *prev,
6803 tsc_t tsc, int new_runstate)
6804 {
6805 assert(prev == prev->d->vcpu[prev->vid]);
6806
6807 if(prev->p != p)
6808 {
6809 fprintf(warn, "Strange, sched_switch on pcpu %d, prev->pcpu %d!\n",
6810 p->pid, prev->p->pid);
6811 prev->runstate.tsc = 0;
6812 goto set;
6813 }
6814
6815 //assert(p->current);
6816
6817 if ( !p->current )
6818 {
6819 fprintf(warn, "%s: FATAL: p->current NULL!\n", __func__);
6820 error(ERR_ASSERT, NULL);
6821 }
6822
6823 if(p->current != prev)
6824 {
6825 fprintf(warn, "Strange, sched_switch prev d%dv%d, pcpu %d current d%dv%d!\n",
6826 prev->d->did, prev->vid,
6827 p->pid, p->current->d->did, p->current->vid);
6828 prev->runstate.tsc = 0;
6829 goto set;
6830 }
6831
6832 if(prev->runstate.state != RUNSTATE_RUNNING)
6833 {
6834 fprintf(warn, "Strange, prev d%dv%d not running!\n",
6835 prev->d->did, prev->vid);
6836 prev->runstate.tsc = 0;
6837 goto set;
6838 }
6839
6840 set:
6841 pcpu_runstate_update(p, tsc);
6842 p->current = NULL;
6843 pcpu_string_draw(p);
6844 runstate_update(prev, new_runstate, tsc);
6845 }
6846
6847 void vcpu_next_update(struct pcpu_info *p, struct vcpu_data *next, tsc_t tsc)
6848 {
6849 assert(next == next->d->vcpu[next->vid]);
6850 //assert(p->current == NULL);
6851
6852 if ( p->current != NULL )
6853 {
6854 if ( p->lost_record.seen_valid_schedule == 0 )
6855 {
6856 fprintf(warn, "%s: p->current non-NULL, but seen_valid_schedule 0. Ignoring.\n",
6857 __func__);
6858 runstate_update(p->current, RUNSTATE_LOST, tsc);
6859 p->current = NULL;
6860 }
6861 else
6862 {
6863 fprintf(warn, "%s: FATAL: p->current not NULL! (d%dv%d, runstate %s)\n",
6864 __func__,
6865 p->current->d->did,
6866 p->current->vid,
6867 runstate_name[p->current->runstate.state]);
6868 error(ERR_ASSERT, NULL);
6869 }
6870 }
6871
6872 if(next->activated)
6873 {
6874 /* We may get lost records at start-of-day, so ignore
6875 setting runstate of default vcpus */
6876 if(next->runstate.state == RUNSTATE_RUNNING
6877 && next->d->did != DEFAULT_DOMAIN)
6878 {
6879 fprintf(warn, "Strange, next d%dv%d already running on proc %d!\n",
6880 next->d->did, next->vid,
6881 next->p->pid);
6882 next->runstate.tsc = 0;
6883 }
6884
6885 /* If we're moving from one pcpu to another, record change & update tsc */
6886 if(next->p != p) {
6887 if(next->pcpu_tsc)
6888 {
6889 update_cycles(&next->cpu_affinity_all, tsc - next->pcpu_tsc);
6890 update_cycles(&next->cpu_affinity_pcpu[p->pid], tsc - next->pcpu_tsc);
6891 }
6892 next->pcpu_tsc = tsc;
6893 }
6894 }
6895 else
6896 {
6897 next->guest_paging_levels = opt.default_guest_paging_levels;
6898 next->activated = 1;
6899 next->pcpu_tsc = tsc;
6900 }
6901
6902 runstate_update(next, RUNSTATE_RUNNING, tsc);
6903
6904 if ( opt.scatterplot_pcpu
6905 && next->d->did != IDLE_DOMAIN
6906 && next->d->did != DEFAULT_DOMAIN )
6907 {
6908 struct time_struct t;
6909
6910 abs_cycles_to_time(tsc, &t);
6911
6912 if ( next->p )
6913 printf("%dv%d %u.%09u %d\n",
6914 next->d->did, next->vid,
6915 t.s, t.ns,
6916 next->p->pid);
6917 printf("%dv%d %u.%09u %d\n",
6918 next->d->did, next->vid,
6919 t.s, t.ns,
6920 p->pid);
6921 }
6922
6923 next->p = p;
6924 p->current = next;
6925 pcpu_string_draw(p);
6926 p->time.tsc = tsc;
6927 p->lost_record.seen_valid_schedule = 1;
6928 }
6929
6930 /*
6931 * If the vcpu in question is in state INIT, we're fixing up from something
6932 * like start-of-day. Update what we can.
6933 */
6934 void vcpu_start(struct pcpu_info *p, struct vcpu_data *v,
6935 int old_runstate, int new_runstate, tsc_t ri_tsc) {
6936 tsc_t tsc;
6937
6938 /*
6939 *
6940 * Cases:
6941 * running -> running:
6942 * v -> running, using p->first_tsc
6943 * {runnable, blocked} -> running:
6944 * v -> running, using ri->tsc
6945 * running -> {runnable, blocked}:
6946 * Leave v INIT, but clear p->current in case another vcpu is scheduled
6947 * blocked -> runnable:
6948 * Leave INIT, and also leave p->current, since we still don't know who's scheduled here
6949 */
6950
6951 /*
6952 * NB that a vcpu won't come out of INIT until it starts running somewhere.
6953 * If this event is pcpu that has already seen a scheduling event, p->current
6954 * should be null; if this is the first scheduling event on this pcpu,
6955 * p->current should be the default domain.
6956 */
6957 if( old_runstate == RUNSTATE_RUNNING ) {
6958 if ( !p->current || p->current->d->did != DEFAULT_DOMAIN) {
6959 fprintf(stderr, "Strange, p->current not default domain!\n");
6960 error(ERR_FILE, NULL);
6961 return;
6962
6963 }
6964
6965 if(!p->first_tsc) {
6966 fprintf(stderr, "Strange, p%d first_tsc 0!\n", p->pid);
6967 error(ERR_FILE, NULL);
6968 }
6969
6970 if(p->first_tsc <= p->current->runstate.tsc) {
6971 fprintf(stderr, "Strange, first_tsc %llx < default_domain runstate tsc %llx!\n",
6972 p->first_tsc,
6973 p->current->runstate.tsc);
6974 error(ERR_FILE, NULL);
6975 }
6976
6977 /* Change default domain to 'queued' */
6978 runstate_update(p->current, RUNSTATE_QUEUED, p->first_tsc);
6979
6980 /*
6981 * Set current to NULL, so that if another vcpu (not in INIT)
6982 * is scheduled here, we don't trip over the check in
6983 * vcpu_next_update()
6984 */
6985 p->current = NULL;
6986 }
6987
6988 /* TSC skew at start-of-day is hard to deal with. Don't
6989 * bring a vcpu out of INIT until it's seen to be actually
6990 * running somewhere. */
6991 if ( new_runstate != RUNSTATE_RUNNING ) {
6992 if ( !v->delayed_init ) {
6993 fprintf(warn, "First schedule for d%dv%d doesn't take us into a running state; leaving in INIT\n",
6994 v->d->did, v->vid);
6995 v->delayed_init = 1;
6996 }
6997
6998 return;
6999 } else if ( v->delayed_init ) {
7000 fprintf(warn, "d%dv%d RUNSTATE_RUNNING detected, leaving INIT",
7001 v->d->did, v->vid);
7002 v->delayed_init = 0;
7003 }
7004
7005 tsc = ri_tsc;
7006 if ( old_runstate == RUNSTATE_RUNNING ) {
7007 /* FIXME: Copy over data from the default domain this interval */
7008 fprintf(warn, "Using first_tsc for d%dv%d (%lld cycles)\n",
7009 v->d->did, v->vid, p->last_tsc - p->first_tsc);
7010
7011 tsc = p->first_tsc;
7012 }
7013
7014 /* Simulate the time since the first tsc */
7015 runstate_update(v, RUNSTATE_RUNNING, tsc);
7016 p->time.tsc = tsc;
7017 p->current = v;
7018 pcpu_string_draw(p);
7019 v->p = p;
7020 }
7021
7022 void sched_runstate_process(struct pcpu_info *p)
7023 {
7024 enum {
7025 CHANGE=0,
7026 CONTINUE
7027 } type;
7028 struct vcpu_data *v;
7029 struct record_info *ri = &p->ri;
7030 struct {
7031 unsigned vcpu:16, dom:16;
7032 unsigned long long p1, p2;
7033 } __attribute__((packed)) * r = (typeof(r))ri->d;
7034 union {
7035 unsigned int event;
7036 struct {
7037 unsigned lo:4,
7038 new_runstate:4,
7039 old_runstate:4,
7040 sub:4,
7041 main:12,
7042 unused:4;
7043 };
7044 } _sevt = { .event = ri->event };
7045 struct {
7046 int new_runstate, old_runstate;
7047 } sevt;
7048 int perfctrs;
7049 struct last_oldstate_struct last_oldstate;
7050
7051 switch(_sevt.lo)
7052 {
7053 case 1:
7054 type = CHANGE;
7055 sevt.new_runstate = _sevt.new_runstate;
7056 sevt.old_runstate = _sevt.old_runstate;
7057 break;
7058 case 2:
7059 type = CONTINUE;
7060 sevt.new_runstate = sevt.old_runstate = RUNSTATE_RUNNING;
7061 break;
7062 default:
7063 fprintf(warn, "FATAL: Unexpected runstate change type %d!\n",
7064 _sevt.lo);
7065 error(ERR_RECORD, NULL);
7066 return;
7067 }
7068
7069 perfctrs = (ri->extra_words == 5);
7070
7071 if(opt.dump_all) {
7072 if( perfctrs ) {
7073 printf(" %s %s {%lld,%lld} d%uv%u %s->%s\n",
7074 ri->dump_header,
7075 type?"runstate_continue":"runstate_change",
7076 r->p1, r->p2,
7077 r->dom, r->vcpu,
7078 runstate_name[sevt.old_runstate],
7079 runstate_name[sevt.new_runstate]);
7080 } else {
7081 printf(" %s %s d%uv%u %s->%s\n",
7082 ri->dump_header,
7083 type?"runstate_continue":"runstate_change",
7084 r->dom, r->vcpu,
7085 runstate_name[sevt.old_runstate],
7086 runstate_name[sevt.new_runstate]);
7087 }
7088 }
7089
7090 /* Sanity check: expected transitions */
7091 if ( type == CHANGE )
7092 {
7093 if( (sevt.new_runstate == RUNSTATE_RUNNING
7094 && sevt.old_runstate != RUNSTATE_RUNNABLE)
7095 || (sevt.new_runstate == RUNSTATE_BLOCKED
7096 && sevt.old_runstate == RUNSTATE_RUNNABLE ) )
7097 {
7098 fprintf(warn, "Strange, d%dv%d unexpected runstate transition %s->%s\n",
7099 r->dom, r->vcpu,
7100 runstate_name[sevt.old_runstate],
7101 runstate_name[sevt.new_runstate]);
7102 }
7103 }
7104
7105 if(r->vcpu > MAX_CPUS)
7106 {
7107 fprintf(warn, "%s: vcpu %u > MAX_VCPUS %d!\n",
7108 __func__, r->vcpu, MAX_CPUS);
7109 return;
7110 }
7111
7112 v = vcpu_find(r->dom, r->vcpu);
7113
7114 /* We want last_oldstate reset every time; so copy the last one and use
7115 * that locally, clobbering the one in the vcpu struct. If it needs to
7116 * be reset, it will be reset below. */
7117 last_oldstate = v->runstate.last_oldstate;
7118 v->runstate.last_oldstate.wrong = RUNSTATE_INIT;
7119
7120 /* Handle all "start-of-day" issues in one place. This can be
7121 * done before any of the other tracks or sanity checks. */
7122 if ( v->runstate.state == RUNSTATE_INIT ) {
7123 vcpu_start(p, v, sevt.old_runstate, sevt.new_runstate, ri->tsc);
7124 return;
7125 }
7126
7127 /* Close vmexits when the putative reason for blocking / &c stops.
7128 * This way, we don't account cpu contention to some other overhead. */
7129 if(sevt.new_runstate == RUNSTATE_RUNNABLE
7130 && v->data_type == VCPU_DATA_HVM
7131 && v->hvm.vmexit_valid) {
7132 hvm_close_vmexit(&v->hvm, ri->tsc);
7133 }
7134
7135 /* Track waking state */
7136 if ( v->data_type == VCPU_DATA_HVM && v->runstate.state != RUNSTATE_LOST ) {
7137 if ( sevt.new_runstate == RUNSTATE_RUNNABLE
7138 && sevt.old_runstate == RUNSTATE_BLOCKED )
7139 {
7140 /* Hmm... want to make sure we're not in some weird
7141 vmexit state... have to look later. */
7142 if(opt.dump_all)
7143 printf(" [w2h] d%dv%d Setting waking\n", v->d->did, v->vid);
7144 v->hvm.w2h.waking = 1;
7145 }
7146 else if ( sevt.new_runstate != RUNSTATE_RUNNING
7147 || sevt.old_runstate != RUNSTATE_RUNNABLE )
7148 {
7149 if( v->hvm.w2h.waking
7150 && sevt.old_runstate == RUNSTATE_RUNNING
7151 && sevt.new_runstate != RUNSTATE_OFFLINE )
7152 {
7153 /* NB: This is printed a lot unnecessairly when there is TSC skew */
7154 if ( sevt.old_runstate != v->runstate.state )
7155 fprintf(warn, "Strange, unexpected waking transition for d%dv%d: %s -> %s\n",
7156 v->d->did, v->vid,
7157 runstate_name[sevt.old_runstate],
7158 runstate_name[sevt.new_runstate]);
7159 v->hvm.w2h.waking = 0;
7160 }
7161
7162 /* Close wake-to-halt summary */
7163 /* FIXME: Need to think about handling preemption. */
7164 if (sevt.new_runstate == RUNSTATE_BLOCKED
7165 && sevt.old_runstate == RUNSTATE_RUNNING
7166 && v->hvm.w2h.interrupts ) {
7167 int i;
7168 for(i=0; i<GUEST_INTERRUPT_MAX; i++) {
7169 struct hvm_gi_struct *g=v->hvm.summary.guest_interrupt + i;
7170 tsc_t start_tsc = g->start_tsc;
7171 if(start_tsc) {
7172 tsc_t t = (start_tsc == 1) ? 0 : ri->tsc - start_tsc;
7173 if(opt.dump_all)
7174 printf(" [w2h] Halting vec %d is_wake %d time %lld\n",
7175 i,
7176 g->is_wake,
7177 t);
7178
7179 if(opt.scatterplot_wake_to_halt
7180 && t
7181 && g->is_wake)
7182 scatterplot_vs_time(ri->tsc, t);
7183
7184 if(opt.summary && t) {
7185 if(g->is_wake) {
7186 if(v->hvm.w2h.interrupts==1)
7187 update_cycles(&g->runtime[GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ALONE],
7188 t);
7189 update_cycles(&g->runtime[GUEST_INTERRUPT_CASE_WAKE_TO_HALT_ANY],
7190 t);
7191 } else {
7192 update_cycles(&g->runtime[GUEST_INTERRUPT_CASE_INTERRUPT_TO_HALT],
7193 t);
7194 }
7195 }
7196 g->start_tsc = 0;
7197 g->is_wake = 0;
7198 }
7199 }
7200 v->hvm.w2h.interrupts = 0;
7201 v->hvm.w2h.vector = 0;
7202 }
7203 }
7204 }
7205
7206 /* Sanity checks / tsc skew detection */
7207 if( v->runstate.state != sevt.old_runstate
7208 && v->runstate.state != RUNSTATE_INIT )
7209 {
7210 if(v->runstate.state == RUNSTATE_LOST) {
7211 if( sevt.new_runstate == RUNSTATE_RUNNING )
7212 goto update;
7213 else if(opt.dump_all)
7214 fprintf(warn, "%s: d%dv%d in runstate lost, not updating to %s\n",
7215 __func__, v->d->did, v->vid,
7216 runstate_name[sevt.new_runstate]);
7217 goto no_update;
7218 } else if (last_oldstate.wrong == sevt.new_runstate
7219 && last_oldstate.actual == sevt.old_runstate) {
7220 tsc_t lag, old_offset;
7221 struct pcpu_info *p2;
7222
7223 if(ri->tsc < last_oldstate.tsc) {
7224 fprintf(warn, "WARNING: new tsc %lld < detected runstate tsc %lld! Not updating\n",
7225 ri->tsc, last_oldstate.tsc);
7226 goto no_update;
7227 }
7228
7229 p2 = P.pcpu + last_oldstate.pid;
7230
7231 lag = ri->tsc
7232 - last_oldstate.tsc;
7233
7234 old_offset = p2->tsc_skew.offset;
7235
7236 cpumask_union(&p2->tsc_skew.downstream, &p->tsc_skew.downstream);
7237 cpumask_set(&p2->tsc_skew.downstream, p->pid);
7238
7239 if(cpumask_isset(&p2->tsc_skew.downstream, p2->pid)) {
7240 if ( opt.tsc_loop_fatal )
7241 {
7242 fprintf(stderr, "FATAL: tsc skew dependency loop detected!\n");
7243 error(ERR_FILE, NULL);
7244 }
7245 else
7246 {
7247 int i;
7248 fprintf(warn, "Tsc skew dependency loop detected! Resetting...\n");
7249 for ( i=0; i<=P.max_active_pcpu; i++)
7250 {
7251 struct pcpu_info *p = P.pcpu + i;
7252
7253 p->tsc_skew.offset = 0;
7254 cpumask_init(&p->tsc_skew.downstream);
7255 }
7256 goto no_update;
7257 }
7258 }
7259
7260 p2->tsc_skew.offset += lag * 2;
7261
7262 fprintf(warn, "TSC skew detected p%d->p%d, %lld cycles. Changing p%d offset from %lld to %lld\n",
7263 p->pid, p2->pid, lag,
7264 p2->pid,
7265 old_offset,
7266 p2->tsc_skew.offset);
7267
7268 goto no_update;
7269 } else {
7270 fprintf(warn, "runstate_change old_runstate %s, d%dv%d runstate %s. Possible tsc skew.\n",
7271 runstate_name[sevt.old_runstate],
7272 v->d->did, v->vid,
7273 runstate_name[v->runstate.state]);
7274
7275 v->runstate.last_oldstate.wrong = sevt.old_runstate;
7276 v->runstate.last_oldstate.actual = v->runstate.state;
7277 v->runstate.last_oldstate.tsc = ri->tsc;
7278 v->runstate.last_oldstate.pid = p->pid;
7279
7280 if ( v->runstate.state == RUNSTATE_RUNNING )
7281 {
7282 fprintf(warn, " Not updating.\n");
7283 goto no_update;
7284 }
7285 goto update;
7286 }
7287 fprintf(stderr, "FATAL: Logic hole in %s\n", __func__);
7288 error(ERR_ASSERT, NULL);
7289 }
7290
7291 update:
7292 /* Actually update the runstate. Special things to do if we're starting
7293 * or stopping actually running on a physical cpu. */
7294 if ( type == CONTINUE )
7295 {
7296 /* Continue running. First, do some sanity checks */
7297 if ( v->runstate.state == RUNSTATE_LOST ) {
7298 fprintf(warn, "WARNING: continue with d%dv%d in RUNSTATE_LOST. Resetting current.\n",
7299 v->d->did, v->vid);
7300 if ( p->current )
7301 vcpu_prev_update(p, p->current, ri->tsc, RUNSTATE_LOST);
7302 vcpu_next_update(p, v, ri->tsc);
7303 }
7304 else if( v->runstate.state != RUNSTATE_RUNNING ) {
7305 /* This should never happen. */
7306 fprintf(warn, "FATAL: sevt.old_runstate running, but d%dv%d runstate %s!\n",
7307 v->d->did, v->vid, runstate_name[v->runstate.state]);
7308 error(ERR_FILE, NULL);
7309 } else if ( v->p != p ) {
7310 fprintf(warn, "FATAL: continue on p%d, but d%dv%d p%d!\n",
7311 p->pid, v->d->did, v->vid,
7312 v->p ? v->p->pid : -1);
7313 error(ERR_FILE, NULL);
7314 }
7315
7316 runstate_update(v, RUNSTATE_RUNNING, ri->tsc);
7317 }
7318 else if ( sevt.old_runstate == RUNSTATE_RUNNING
7319 || v->runstate.state == RUNSTATE_RUNNING )
7320 {
7321 /*
7322 * Cases:
7323 * old running, v running:
7324 * normal (prev update p, lost record check)
7325 * v running, old ! running:
7326 * tsc skew (prev update v->p, lost record check)
7327 * old running, v init:
7328 start-of-day (fake update, prev p, lost record)
7329 * old running, v !{running,init}:
7330 * # (should never happen)
7331 */
7332 if( sevt.old_runstate == RUNSTATE_RUNNING ) {
7333 if( v->runstate.state != RUNSTATE_RUNNING
7334 && v->runstate.state != RUNSTATE_LOST ) {
7335 /* This should never happen. */
7336 fprintf(warn, "FATAL: sevt.old_runstate running, but d%dv%d runstate %s!\n",
7337 v->d->did, v->vid, runstate_name[v->runstate.state]);
7338 error(ERR_FILE, NULL);
7339 }
7340
7341 vcpu_prev_update(p, v, ri->tsc, sevt.new_runstate);
7342 } else {
7343 vcpu_prev_update(v->p, v, ri->tsc, sevt.new_runstate);
7344 }
7345
7346 if(P.lost_cpus && v->d->did != IDLE_DOMAIN) {
7347 if(opt.dump_all)
7348 fprintf(warn, "%s: %d lost cpus, setting d%dv%d runstate to RUNSTATE_LOST\n",
7349 __func__, P.lost_cpus, v->d->did, v->vid);
7350 lose_vcpu(v, ri->tsc);
7351 }
7352 }
7353 else if ( sevt.new_runstate == RUNSTATE_RUNNING )
7354 {
7355 if(perfctrs) {
7356 v->runstate.p1_start = r->p1;
7357 v->runstate.p2_start = r->p2;
7358 }
7359
7360 vcpu_next_update(p, v, ri->tsc);
7361 }
7362 else
7363 {
7364 runstate_update(v, sevt.new_runstate, ri->tsc);
7365 }
7366
7367 no_update:
7368 return;
7369 }
7370
7371 void dump_sched_switch(struct record_info *ri)
7372 {
7373 struct {
7374 unsigned int prev_dom, prev_vcpu, next_dom, next_vcpu;
7375 } *r = (typeof(r))ri->d;
7376
7377 printf(" %s sched_switch prev d%uv%u next d%uv%u\n",
7378 ri->dump_header, r->prev_dom, r->prev_vcpu,
7379 r->next_dom, r->next_vcpu);
7380 }
7381
7382 void sched_switch_process(struct pcpu_info *p)
7383 {
7384 struct vcpu_data *prev, *next;
7385 struct record_info *ri = &p->ri;
7386 struct {
7387 unsigned int prev_dom, prev_vcpu, next_dom, next_vcpu;
7388 } * r = (typeof(r))ri->d;
7389
7390 if(opt.dump_all)
7391 dump_sched_switch(ri);
7392
7393 if(r->prev_vcpu > MAX_CPUS)
7394 {
7395 fprintf(warn, "%s: prev_vcpu %u > MAX_VCPUS %d!\n",
7396 __func__, r->prev_vcpu, MAX_CPUS);
7397 return;
7398 }
7399
7400 if(r->next_vcpu > MAX_CPUS)
7401 {
7402 fprintf(warn, "%s: next_vcpu %u > MAX_VCPUS %d!\n",
7403 __func__, r->next_vcpu, MAX_CPUS);
7404 return;
7405 }
7406
7407 prev = vcpu_find(r->prev_dom, r->prev_vcpu);
7408 next = vcpu_find(r->next_dom, r->next_vcpu);
7409
7410 vcpu_prev_update(p, prev, ri->tsc, RUNSTATE_QUEUED); /* FIXME */
7411
7412 vcpu_next_update(p, next, ri->tsc);
7413 }
7414
7415 void sched_default_vcpu_activate(struct pcpu_info *p)
7416 {
7417 struct vcpu_data *v = default_domain.vcpu[p->pid];
7418
7419 if(!v)
7420 v = vcpu_create(&default_domain, p->pid);
7421
7422 assert(v == v->d->vcpu[v->vid]);
7423
7424 v->activated = 1;
7425 v->guest_paging_levels = opt.default_guest_paging_levels;
7426 v->p = p;
7427 v->runstate.state = RUNSTATE_RUNNING;
7428
7429 p->current = v;
7430 pcpu_string_draw(p);
7431 }
7432
7433 void sched_default_domain_init(void)
7434 {
7435 struct domain_data *d = &default_domain;
7436
7437 domain_init(d, DEFAULT_DOMAIN);
7438 }
7439
7440 void runstate_clear(tsc_t * runstate_cycles)
7441 {
7442 int i;
7443 for(i=0; i<RUNSTATE_MAX; i++)
7444 runstate_cycles[i]=0;
7445 }
7446
7447 void runstate_summary(tsc_t * runstate_cycles)
7448 {
7449 int i;
7450 for(i=0; i<RUNSTATE_MAX; i++)
7451 if(runstate_cycles[i]) {
7452 struct time_struct t;
7453 cycles_to_time(runstate_cycles[i], &t);
7454 printf(" %s: %u.%09u s\n",
7455 runstate_name[i], t.s, t.ns);
7456 }
7457 }
7458
7459 void sched_summary_vcpu(struct vcpu_data *v)
7460 {
7461 int i;
7462 char desc[30];
7463
7464 /* FIXME: Update all records like this */
7465 if ( v->pcpu_tsc )
7466 {
7467 update_cycles(&v->cpu_affinity_all, P.f.last_tsc - v->pcpu_tsc);
7468 update_cycles(&v->cpu_affinity_pcpu[v->p->pid], P.f.last_tsc - v->pcpu_tsc);
7469 }
7470
7471 printf(" Runstates:\n");
7472 for(i=0; i<RUNSTATE_MAX; i++) {
7473 snprintf(desc,30, " %8s", runstate_name[i]);
7474 print_cycle_summary(v->runstates+i, desc);
7475 if ( i==RUNSTATE_RUNNABLE )
7476 {
7477 int j;
7478 for(j=0; j<RUNNABLE_STATE_MAX; j++) {
7479 if ( j == RUNNABLE_STATE_INVALID )
7480 continue;
7481 snprintf(desc,30, " %8s", runnable_state_name[j]);
7482 print_cycle_summary(v->runnable_states+j, desc);
7483 }
7484 }
7485 }
7486 print_cpu_affinity(&v->cpu_affinity_all, " cpu affinity");
7487 for ( i = 0; i < MAX_CPUS ; i++)
7488 {
7489 snprintf(desc,30, " [%d]", i);
7490 print_cpu_affinity(v->cpu_affinity_pcpu+i, desc);
7491 }
7492 }
7493
7494 void sched_summary_domain(struct domain_data *d)
7495 {
7496 int i;
7497 char desc[30];
7498
7499 printf(" Runstates:\n");
7500 for(i=0; i<DOMAIN_RUNSTATE_MAX; i++) {
7501 snprintf(desc,30, " %8s", domain_runstate_name[i]);
7502 print_cycle_summary(d->runstates+i, desc);
7503 }
7504 }
7505
7506 void dump_sched_vcpu_action(struct record_info *ri, const char *action)
7507 {
7508 struct {
7509 unsigned int domid, vcpuid;
7510 } *r = (typeof(r))ri->d;
7511
7512 printf(" %s %s d%uv%u\n", ri->dump_header, action, r->domid, r->vcpuid);
7513 }
7514
7515 void sched_process(struct pcpu_info *p)
7516 {
7517 struct record_info *ri = &p->ri;
7518
7519 if(ri->evt.sub == 0xf) {
7520 switch(ri->event)
7521 {
7522 case TRC_SCHED_SWITCH:
7523 sched_switch_process(p);
7524 break;
7525 default:
7526 process_generic(&p->ri);
7527 }
7528 return;
7529 }
7530
7531 if(ri->evt.sub == 1) {
7532 /* TRC_SCHED_MIN */
7533 sched_runstate_process(p);
7534 } else if (ri->evt.sub == 8) {
7535 /* TRC_SCHED_VERBOSE */
7536 switch(ri->event)
7537 {
7538 case TRC_SCHED_DOM_ADD:
7539 if(opt.dump_all) {
7540 struct {
7541 unsigned int domid;
7542 } *r = (typeof(r))ri->d;
7543
7544 printf(" %s sched_init_domain d%u\n", ri->dump_header, r->domid);
7545 }
7546 break;
7547 case TRC_SCHED_DOM_REM:
7548 if(opt.dump_all) {
7549 struct {
7550 unsigned int domid, vcpuid;
7551 } *r = (typeof(r))ri->d;
7552
7553 printf(" %s sched_destroy_domain d%u\n", ri->dump_header, r->domid);
7554 }
7555 break;
7556 case TRC_SCHED_SLEEP:
7557 if(opt.dump_all)
7558 dump_sched_vcpu_action(ri, "vcpu_sleep");
7559 break;
7560 case TRC_SCHED_WAKE:
7561 if(opt.dump_all)
7562 dump_sched_vcpu_action(ri, "vcpu_wake");
7563 break;
7564 case TRC_SCHED_YIELD:
7565 if(opt.dump_all)
7566 dump_sched_vcpu_action(ri, "vcpu_yield");
7567 break;
7568 case TRC_SCHED_BLOCK:
7569 if(opt.dump_all)
7570 dump_sched_vcpu_action(ri, "vcpu_block");
7571 break;
7572 case TRC_SCHED_SHUTDOWN:
7573 case TRC_SCHED_SHUTDOWN_CODE:
7574 if(opt.dump_all) {
7575 struct {
7576 unsigned int domid, vcpuid, reason;
7577 } *r = (typeof(r))ri->d;
7578
7579 printf(" %s %s d%uv%u, reason = %u\n", ri->dump_header,
7580 ri->event == TRC_SCHED_SHUTDOWN ? "sched_shutdown" :
7581 "sched_shutdown_code", r->domid, r->vcpuid, r->reason);
7582 }
7583 break;
7584 case TRC_SCHED_ADJDOM:
7585 if(opt.dump_all) {
7586 struct {
7587 unsigned int domid;
7588 } *r = (typeof(r))ri->d;
7589
7590 printf(" %s sched_adjust d%u\n", ri->dump_header, r->domid);
7591 }
7592 break;
7593 case TRC_SCHED_SWITCH:
7594 if(opt.dump_all)
7595 dump_sched_switch(ri);
7596 break;
7597 case TRC_SCHED_SWITCH_INFPREV:
7598 if(opt.dump_all) {
7599 struct {
7600 unsigned int domid, vcpuid, runtime;
7601 } *r = (typeof(r))ri->d;
7602
7603 printf(" %s sched_switch prev d%uv%d, run for %u.%uus\n",
7604 ri->dump_header, r->domid, r->vcpuid,
7605 r->runtime / 1000, r->runtime % 1000);
7606 }
7607 break;
7608 case TRC_SCHED_SWITCH_INFNEXT:
7609 if(opt.dump_all)
7610 {
7611 struct {
7612 unsigned int domid, vcpuid, rsince;
7613 int slice;
7614 } *r = (typeof(r))ri->d;
7615
7616 printf(" %s sched_switch next d%uv%u", ri->dump_header,
7617 r->domid, r->vcpuid);
7618 if ( r->rsince != 0 )
7619 printf(", was runnable for %u.%uus", r->rsince / 1000,
7620 r->rsince % 1000);
7621 if ( r->slice > 0 )
7622 printf(", next slice %u.%uus", r->slice / 1000,
7623 r->slice % 1000);
7624 printf("\n");
7625 }
7626 break;
7627 case TRC_SCHED_SWITCH_INFCONT:
7628 if(opt.dump_all)
7629 {
7630 struct {
7631 unsigned int domid, vcpuid, rsince;
7632 int slice;
7633 } *r = (typeof(r))ri->d;
7634
7635 printf(" %s sched_switch continue d%uv%u, run for %u.%uus",
7636 ri->dump_header, r->domid, r->vcpuid,
7637 r->rsince / 1000, r->rsince % 1000);
7638 if ( r->slice > 0 )
7639 printf(", next slice %u.%uus", r->slice / 1000,
7640 r->slice % 1000);
7641 printf("\n");
7642 }
7643 break;
7644 case TRC_SCHED_CTL:
7645 case TRC_SCHED_S_TIMER_FN:
7646 case TRC_SCHED_T_TIMER_FN:
7647 case TRC_SCHED_DOM_TIMER_FN:
7648 break;
7649 default:
7650 process_generic(&p->ri);
7651 }
7652 } else if(ri->evt.sub == 2) {
7653 /* TRC_SCHED_CLASS */
7654 switch(ri->event)
7655 {
7656 /* CREDIT (TRC_CSCHED_xxx) */
7657 case TRC_SCHED_CLASS_EVT(CSCHED, 1): /* SCHED_TASKLET */
7658 if(opt.dump_all)
7659 printf(" %s csched:sched_tasklet\n", ri->dump_header);
7660 break;
7661 case TRC_SCHED_CLASS_EVT(CSCHED, 2): /* ACCOUNT_START */
7662 case TRC_SCHED_CLASS_EVT(CSCHED, 3): /* ACCOUNT_STOP */
7663 if(opt.dump_all) {
7664 struct {
7665 unsigned int domid, vcpuid, actv_cnt;
7666 } *r = (typeof(r))ri->d;
7667
7668 printf(" %s csched:acct_%s d%uv%u, active_vcpus %u\n",
7669 ri->dump_header,
7670 ri->event == TRC_SCHED_CLASS_EVT(CSCHED, 2) ?
7671 "start" : "stop",
7672 r->domid, r->vcpuid,
7673 r->actv_cnt);
7674 }
7675 break;
7676 case TRC_SCHED_CLASS_EVT(CSCHED, 4): /* STOLEN_VCPU */
7677 if(opt.dump_all) {
7678 struct {
7679 unsigned int peer_cpu, domid, vcpuid;
7680 } *r = (typeof(r))ri->d;
7681
7682 printf(" %s csched:stolen_vcpu d%uv%u from cpu %u\n",
7683 ri->dump_header, r->domid, r->vcpuid, r->peer_cpu);
7684 }
7685 break;
7686 case TRC_SCHED_CLASS_EVT(CSCHED, 5): /* PICKED_CPU */
7687 if(opt.dump_all) {
7688 struct {
7689 unsigned int domid, vcpuid, cpu;
7690 } *r = (typeof(r))ri->d;
7691
7692 printf(" %s csched:pick_cpu %u for d%uv%u\n",
7693 ri->dump_header, r->cpu, r->domid, r->vcpuid);
7694 }
7695 break;
7696 case TRC_SCHED_CLASS_EVT(CSCHED, 6): /* TICKLE */
7697 if(opt.dump_all) {
7698 struct {
7699 unsigned int cpu;
7700 } *r = (typeof(r))ri->d;
7701
7702 printf(" %s csched:runq_tickle, cpu %u\n",
7703 ri->dump_header, r->cpu);
7704 }
7705 break;
7706 case TRC_SCHED_CLASS_EVT(CSCHED, 7): /* BOOST_START */
7707 if(opt.dump_all) {
7708 struct {
7709 unsigned int domid, vcpuid;
7710 } *r = (typeof(r))ri->d;
7711
7712 printf(" %s csched: d%uv%u boosted\n",
7713 ri->dump_header, r->domid, r->vcpuid);
7714 }
7715 break;
7716 case TRC_SCHED_CLASS_EVT(CSCHED, 8): /* BOOST_END */
7717 if(opt.dump_all) {
7718 struct {
7719 unsigned int domid, vcpuid;
7720 } *r = (typeof(r))ri->d;
7721
7722 printf(" %s csched: d%uv%u unboosted\n",
7723 ri->dump_header, r->domid, r->vcpuid);
7724 }
7725 break;
7726 case TRC_SCHED_CLASS_EVT(CSCHED, 9): /* SCHEDULE */
7727 if(opt.dump_all) {
7728 struct {
7729 unsigned int cpu:16, tasklet:8, idle:8;
7730 } *r = (typeof(r))ri->d;
7731
7732 printf(" %s csched:schedule cpu %u%s%s\n",
7733 ri->dump_header, r->cpu,
7734 r->tasklet ? ", tasklet scheduled" : "",
7735 r->idle ? ", idle" : ", busy");
7736 }
7737 break;
7738 case TRC_SCHED_CLASS_EVT(CSCHED, 10): /* RATELIMIT */
7739 if(opt.dump_all) {
7740 struct {
7741 unsigned int vcpuid:16, domid:16;
7742 unsigned int runtime;
7743 } *r = (typeof(r))ri->d;
7744
7745 printf(" %s csched:ratelimit, d%uv%u run only %u.%uus\n",
7746 ri->dump_header, r->domid, r->vcpuid,
7747 r->runtime / 1000, r->runtime % 1000);
7748 }
7749 break;
7750 case TRC_SCHED_CLASS_EVT(CSCHED, 11): /* STEAL_CHECK */
7751 if(opt.dump_all) {
7752 struct {
7753 unsigned int peer_cpu, check;
7754 } *r = (typeof(r))ri->d;
7755
7756 printf(" %s csched:load_balance %s %u\n",
7757 ri->dump_header, r->check ? "checking" : "skipping",
7758 r->peer_cpu);
7759 }
7760 break;
7761 /* CREDIT 2 (TRC_CSCHED2_xxx) */
7762 case TRC_SCHED_CLASS_EVT(CSCHED2, 1): /* TICK */
7763 case TRC_SCHED_CLASS_EVT(CSCHED2, 4): /* CREDIT_ADD */
7764 break;
7765 case TRC_SCHED_CLASS_EVT(CSCHED2, 2): /* RUNQ_POS */
7766 if(opt.dump_all) {
7767 struct {
7768 unsigned int vcpuid:16, domid:16, pos;
7769 } *r = (typeof(r))ri->d;
7770
7771 printf(" %s csched2:runq_insert d%uv%u, position %u\n",
7772 ri->dump_header, r->domid, r->vcpuid, r->pos);
7773 }
7774 break;
7775 case TRC_SCHED_CLASS_EVT(CSCHED2, 3): /* CREDIT_BURN */
7776 if(opt.dump_all) {
7777 struct {
7778 unsigned int vcpuid:16, domid:16;
7779 int credit, budget, delta;
7780 } *r = (typeof(r))ri->d;
7781
7782 printf(" %s csched2:burn_credits d%uv%u, credit = %d, ",
7783 ri->dump_header, r->domid, r->vcpuid, r->credit);
7784 if ( r->budget != INT_MIN )
7785 printf("budget = %d, ", r->budget);
7786 printf("delta = %d\n", r->delta);
7787 }
7788 break;
7789 case TRC_SCHED_CLASS_EVT(CSCHED2, 5): /* TICKLE_CHECK */
7790 if(opt.dump_all) {
7791 struct {
7792 unsigned int vcpuid:16, domid:16;
7793 int credit, score;
7794 } *r = (typeof(r))ri->d;
7795
7796 printf(" %s csched2:tickle_check d%uv%u, credit = %d, score = %d\n\n",
7797 ri->dump_header, r->domid, r->vcpuid,
7798 r->credit, r->score);
7799 }
7800 break;
7801 case TRC_SCHED_CLASS_EVT(CSCHED2, 6): /* TICKLE */
7802 if(opt.dump_all) {
7803 struct {
7804 unsigned int cpu:16;
7805 } *r = (typeof(r))ri->d;
7806
7807 printf(" %s csched2:runq_tickle cpu %u\n",
7808 ri->dump_header, r->cpu);
7809 }
7810 break;
7811 case TRC_SCHED_CLASS_EVT(CSCHED2, 7): /* CREDIT_RESET */
7812 if(opt.dump_all) {
7813 struct {
7814 unsigned int vcpuid:16, domid:16;
7815 int credit_start, credit_end;
7816 } *r = (typeof(r))ri->d;
7817
7818 printf(" %s csched2:reset_credits d%uv%u, "
7819 "credit_start = %d, credit_end = %d\n",
7820 ri->dump_header, r->domid, r->vcpuid,
7821 r->credit_start, r->credit_end);
7822 }
7823 break;
7824 case TRC_SCHED_CLASS_EVT(CSCHED2, 8): /* SCHED_TASKLET */
7825 if(opt.dump_all)
7826 printf(" %s csched2:sched_tasklet\n", ri->dump_header);
7827 break;
7828 case TRC_SCHED_CLASS_EVT(CSCHED2, 9): /* UPDATE_LOAD */
7829 if(opt.dump_all)
7830 printf(" %s csched2:update_load\n", ri->dump_header);
7831 break;
7832 case TRC_SCHED_CLASS_EVT(CSCHED2, 10): /* RUNQ_ASSIGN */
7833 if(opt.dump_all) {
7834 struct {
7835 unsigned int vcpuid:16, domid:16;
7836 unsigned int rqi:16;
7837 } *r = (typeof(r))ri->d;
7838
7839 printf(" %s csched2:runq_assign d%uv%u on rq# %u\n",
7840 ri->dump_header, r->domid, r->vcpuid, r->rqi);
7841 }
7842 break;
7843 case TRC_SCHED_CLASS_EVT(CSCHED2, 11): /* UPDATE_VCPU_LOAD */
7844 if(opt.dump_all) {
7845 struct {
7846 uint64_t vcpuload;
7847 unsigned int vcpuid:16, domid:16;
7848 unsigned int shift;
7849 } *r = (typeof(r))ri->d;
7850 double vcpuload;
7851
7852 vcpuload = (r->vcpuload * 100.0) / (1ULL << r->shift);
7853
7854 printf(" %s csched2:update_vcpu_load d%uv%u, "
7855 "vcpu_load = %4.3f%% (%"PRIu64")\n",
7856 ri->dump_header, r->domid, r->vcpuid, vcpuload,
7857 r->vcpuload);
7858 }
7859 break;
7860 case TRC_SCHED_CLASS_EVT(CSCHED2, 12): /* UPDATE_RUNQ_LOAD */
7861 if(opt.dump_all) {
7862 struct {
7863 uint64_t rq_avgload, b_avgload;
7864 unsigned int rq_load:16, rq_id:8, shift:8;
7865 } *r = (typeof(r))ri->d;
7866 double avgload, b_avgload;
7867
7868 avgload = (r->rq_avgload * 100.0) / (1ULL << r->shift);
7869 b_avgload = (r->b_avgload * 100.0) / (1ULL << r->shift);
7870
7871 printf(" %s csched2:update_rq_load rq# %u, load = %u, "
7872 "avgload = %4.3f%% (%"PRIu64"), "
7873 "b_avgload = %4.3f%% (%"PRIu64")\n",
7874 ri->dump_header, r->rq_id, r->rq_load,
7875 avgload, r->rq_avgload, b_avgload, r->b_avgload);
7876 }
7877 break;
7878 case TRC_SCHED_CLASS_EVT(CSCHED2, 13): /* TICKLE_NEW */
7879 if (opt.dump_all) {
7880 struct {
7881 unsigned int vcpuid:16, domid:16, processor;
7882 int credit;
7883 } *r = (typeof(r))ri->d;
7884
7885 printf(" %s csched2:runq_tickle_new d%uv%u, "
7886 "processor = %u, credit = %d\n",
7887 ri->dump_header, r->domid, r->vcpuid,
7888 r->processor, r->credit);
7889 }
7890 break;
7891 case TRC_SCHED_CLASS_EVT(CSCHED2, 14): /* RUNQ_MAX_WEIGHT */
7892 if (opt.dump_all) {
7893 struct {
7894 unsigned rqi:16, max_weight:16;
7895 } *r = (typeof(r))ri->d;
7896
7897 printf(" %s csched2:update_max_weight rq# %u, max_weight = %u\n",
7898 ri->dump_header, r->rqi, r->max_weight);
7899 }
7900 break;
7901 case TRC_SCHED_CLASS_EVT(CSCHED2, 15): /* MIGRATE */
7902 if (opt.dump_all) {
7903 struct {
7904 unsigned vcpuid:16, domid:16;
7905 unsigned rqi:16, trqi:16;
7906 } *r = (typeof(r))ri->d;
7907
7908 printf(" %s csched2:migrate d%uv%u rq# %u --> rq# %u\n",
7909 ri->dump_header, r->domid, r->vcpuid, r->rqi, r->trqi);
7910 }
7911 break;
7912 case TRC_SCHED_CLASS_EVT(CSCHED2, 16): /* LOAD_CHECK */
7913 if (opt.dump_all) {
7914 struct {
7915 unsigned lrqi:16, orqi:16;
7916 unsigned load_delta;
7917 } *r = (typeof(r))ri->d;
7918
7919 printf(" %s csched2:load_balance_check lrq# %u, orq# %u, "
7920 "delta = %u\n",
7921 ri->dump_header, r->lrqi, r->orqi, r->load_delta);
7922 }
7923 break;
7924 case TRC_SCHED_CLASS_EVT(CSCHED2, 17): /* LOAD_BALANCE */
7925 if (opt.dump_all) {
7926 struct {
7927 uint64_t lb_avgload, ob_avgload;
7928 unsigned lrqi:16, orqi:16;
7929 } *r = (typeof(r))ri->d;
7930
7931 printf(" %s csched2:load_balance_begin lrq# %u, "
7932 "avg_load = %"PRIu64" -- orq# %u, avg_load = %"PRIu64"\n",
7933 ri->dump_header, r->lrqi, r->lb_avgload,
7934 r->orqi, r->ob_avgload);
7935 }
7936 break;
7937 case TRC_SCHED_CLASS_EVT(CSCHED2, 19): /* PICKED_CPU */
7938 if (opt.dump_all) {
7939 struct {
7940 uint64_t b_avgload;
7941 unsigned vcpuid:16, domid:16;
7942 unsigned rqi:16, cpu:16;
7943 } *r = (typeof(r))ri->d;
7944
7945 printf(" %s csched2:picked_cpu d%uv%u, rq# %u, cpu %u\n",
7946 ri->dump_header, r->domid, r->vcpuid, r->rqi, r->cpu);
7947 }
7948 break;
7949 case TRC_SCHED_CLASS_EVT(CSCHED2, 20): /* RUNQ_CANDIDATE */
7950 if (opt.dump_all) {
7951 struct {
7952 unsigned vcpuid:16, domid:16;
7953 unsigned tickled_cpu;
7954 int credit;
7955 } *r = (typeof(r))ri->d;
7956
7957 printf(" %s csched2:runq_candidate d%uv%u, credit = %d, ",
7958 ri->dump_header, r->domid, r->vcpuid, r->credit);
7959 if (r->tickled_cpu == (unsigned)-1)
7960 printf("no cpu was tickled\n");
7961 else
7962 printf("cpu %u was tickled\n", r->tickled_cpu);
7963 }
7964 break;
7965 case TRC_SCHED_CLASS_EVT(CSCHED2, 21): /* SCHEDULE */
7966 if (opt.dump_all) {
7967 struct {
7968 unsigned cpu:16, rqi:16;
7969 unsigned tasklet:8, idle:8, smt_idle:8, tickled:8;
7970 } *r = (typeof(r))ri->d;
7971
7972 printf(" %s csched2:schedule cpu %u, rq# %u%s%s%s%s\n",
7973 ri->dump_header, r->cpu, r->rqi,
7974 r->tasklet ? ", tasklet scheduled" : "",
7975 r->idle ? ", idle" : ", busy",
7976 r->idle ? (r->smt_idle ? ", SMT idle" : ", SMT busy") : "",
7977 r->tickled ? ", tickled" : ", not tickled");
7978 }
7979 break;
7980 case TRC_SCHED_CLASS_EVT(CSCHED2, 22): /* RATELIMIT */
7981 if (opt.dump_all) {
7982 struct {
7983 unsigned int vcpuid:16, domid:16;
7984 unsigned int runtime;
7985 } *r = (typeof(r))ri->d;
7986
7987 printf(" %s csched2:ratelimit, d%uv%u run only %u.%uus\n",
7988 ri->dump_header, r->domid, r->vcpuid,
7989 r->runtime / 1000, r->runtime % 1000);
7990 }
7991 break;
7992 case TRC_SCHED_CLASS_EVT(CSCHED2, 23): /* RUNQ_CAND_CHECK */
7993 if(opt.dump_all) {
7994 struct {
7995 unsigned int vcpuid:16, domid:16;
7996 } *r = (typeof(r))ri->d;
7997
7998 printf(" %s csched2:runq_cand_check d%uv%u\n",
7999 ri->dump_header, r->domid, r->vcpuid);
8000 }
8001 break;
8002 /* RTDS (TRC_RTDS_xxx) */
8003 case TRC_SCHED_CLASS_EVT(RTDS, 1): /* TICKLE */
8004 if(opt.dump_all) {
8005 struct {
8006 unsigned int cpu:16;
8007 } *r = (typeof(r))ri->d;
8008
8009 printf(" %s rtds:runq_tickle cpu %u\n",
8010 ri->dump_header, r->cpu);
8011 }
8012 break;
8013 case TRC_SCHED_CLASS_EVT(RTDS, 2): /* RUNQ_PICK */
8014 if(opt.dump_all) {
8015 struct {
8016 unsigned int vcpuid:16, domid:16;
8017 uint64_t cur_dl, cur_bg;
8018 } __attribute__((packed)) *r = (typeof(r))ri->d;
8019
8020 printf(" %s rtds:runq_pick d%uv%u, deadline = %"PRIu64", "
8021 "budget = %"PRIu64"\n", ri->dump_header,
8022 r->domid, r->vcpuid, r->cur_dl, r->cur_bg);
8023 }
8024 break;
8025 case TRC_SCHED_CLASS_EVT(RTDS, 3): /* BUDGET_BURN */
8026 if(opt.dump_all) {
8027 struct {
8028 unsigned int vcpuid:16, domid:16;
8029 uint64_t cur_bg;
8030 int delta;
8031 } __attribute__((packed)) *r = (typeof(r))ri->d;
8032
8033 printf(" %s rtds:burn_budget d%uv%u, budget = %"PRIu64", "
8034 "delta = %d\n", ri->dump_header, r->domid,
8035 r->vcpuid, r->cur_bg, r->delta);
8036 }
8037 break;
8038 case TRC_SCHED_CLASS_EVT(RTDS, 4): /* BUDGET_REPLENISH */
8039 if(opt.dump_all) {
8040 struct {
8041 unsigned int vcpuid:16, domid:16;
8042 uint64_t cur_dl, cur_bg;
8043 } __attribute__((packed)) *r = (typeof(r))ri->d;
8044
8045 printf(" %s rtds:repl_budget d%uv%u, deadline = %"PRIu64", "
8046 "budget = %"PRIu64"\n", ri->dump_header,
8047 r->domid, r->vcpuid, r->cur_dl, r->cur_bg);
8048 }
8049 break;
8050 case TRC_SCHED_CLASS_EVT(RTDS, 5): /* SCHED_TASKLET */
8051 if(opt.dump_all)
8052 printf(" %s rtds:sched_tasklet\n", ri->dump_header);
8053 break;
8054 case TRC_SCHED_CLASS_EVT(RTDS, 6): /* SCHEDULE */
8055 if (opt.dump_all) {
8056 struct {
8057 unsigned cpu:16, tasklet:8, idle:4, tickled:4;
8058 } __attribute__((packed)) *r = (typeof(r))ri->d;
8059
8060 printf(" %s rtds:schedule cpu %u, %s%s%s\n",
8061 ri->dump_header, r->cpu,
8062 r->tasklet ? ", tasklet scheduled" : "",
8063 r->idle ? ", idle" : ", busy",
8064 r->tickled ? ", tickled" : ", not tickled");
8065 }
8066 break;
8067 case TRC_SCHED_CLASS_EVT(SNULL, 1): /* PICKED_CPU */
8068 if (opt.dump_all) {
8069 struct {
8070 uint16_t vcpuid, domid;
8071 uint32_t new_cpu;
8072 } *r = (typeof(r))ri->d;
8073
8074 printf(" %s null:picked_cpu d%uv%u, cpu %u\n",
8075 ri->dump_header, r->domid, r->vcpuid, r->new_cpu);
8076 }
8077 break;
8078 case TRC_SCHED_CLASS_EVT(SNULL, 2): /* VCPU_ASSIGN */
8079 if (opt.dump_all) {
8080 struct {
8081 uint16_t vcpuid, domid;
8082 uint32_t cpu;
8083 } *r = (typeof(r))ri->d;
8084
8085 printf(" %s null:vcpu_assign d%uv%u to cpu %u\n",
8086 ri->dump_header, r->domid, r->vcpuid, r->cpu);
8087 }
8088 break;
8089 case TRC_SCHED_CLASS_EVT(SNULL, 3): /* VCPU_DEASSIGN */
8090 if (opt.dump_all) {
8091 struct {
8092 uint16_t vcpuid, domid;
8093 uint32_t cpu;
8094 } *r = (typeof(r))ri->d;
8095
8096 printf(" %s null:vcpu_deassign d%uv%u from cpu %u\n",
8097 ri->dump_header, r->domid, r->vcpuid, r->cpu);
8098 }
8099 break;
8100 case TRC_SCHED_CLASS_EVT(SNULL, 4): /* MIGRATE */
8101 if (opt.dump_all) {
8102 struct {
8103 uint16_t vcpuid, domid;
8104 uint16_t cpu, new_cpu;
8105 } *r = (typeof(r))ri->d;
8106
8107 printf(" %s null:migrate d%uv%u, cpu %u, new_cpu %u\n",
8108 ri->dump_header, r->domid, r->vcpuid,
8109 r->cpu, r->new_cpu);
8110 }
8111 break;
8112 case TRC_SCHED_CLASS_EVT(SNULL, 5): /* SCHEDULE */
8113 if (opt.dump_all) {
8114 struct {
8115 uint16_t tasklet, cpu;
8116 int16_t vcpuid, domid;
8117 } *r = (typeof(r))ri->d;
8118
8119 printf(" %s null:schedule cpu %u%s",
8120 ri->dump_header, r->cpu,
8121 r->tasklet ? ", tasklet scheduled" : "");
8122 if (r->vcpuid != -1)
8123 printf(", vcpu d%uv%d\n", r->domid, r->vcpuid);
8124 else
8125 printf(", no vcpu\n");
8126 }
8127 break;
8128 case TRC_SCHED_CLASS_EVT(SNULL, 6): /* TASKLET */
8129 if (opt.dump_all)
8130 printf(" %s null:sched_tasklet\n", ri->dump_header);
8131 break;
8132 default:
8133 process_generic(ri);
8134 }
8135 } else {
8136 UPDATE_VOLUME(p, sched_verbose, ri->size);
8137 process_generic(&p->ri);
8138 }
8139 }
8140
8141 /* ---- Memory ---- */
8142 void mem_summary_domain(struct domain_data *d) {
8143 int i, j;
8144
8145 printf(" Grant table ops:\n");
8146
8147 printf(" Done by:\n");
8148 for(i=0; i<MEM_MAX; i++)
8149 if(d->memops.done[i])
8150 printf(" %-14s: %d\n",
8151 mem_name[i],
8152 d->memops.done[i]);
8153
8154 printf(" Done for:\n");
8155 for(i=0; i<MEM_MAX; i++)
8156 if(d->memops.done_for[i])
8157 printf(" %-14s: %d\n",
8158 mem_name[i],
8159 d->memops.done_for[i]);
8160
8161 printf(" Populate-on-demand:\n");
8162 printf(" Populated:\n");
8163 for(i=0; i<4; i++)
8164 {
8165 if ( d->pod.populate_order[i] )
8166 printf(" [%d] %d\n", i,
8167 d->pod.populate_order[i]);
8168 }
8169 printf(" Reclaim order:\n");
8170 for(i=0; i<4; i++)
8171 {
8172 if ( d->pod.reclaim_order[i] )
8173 printf(" [%d] %d\n", i,
8174 d->pod.reclaim_order[i]);
8175 }
8176 printf(" Reclaim contexts:\n");
8177 for(j=0; j<POD_RECLAIM_CONTEXT_MAX; j++)
8178 {
8179 if ( d->pod.reclaim_context[j] )
8180 {
8181 printf(" * [%s] %d\n",
8182 pod_reclaim_context_name[j],
8183 d->pod.reclaim_context[j]);
8184 for(i=0; i<4; i++)
8185 {
8186 if ( d->pod.reclaim_context_order[j][i] )
8187 printf(" [%d] %d\n", i,
8188 d->pod.reclaim_context_order[j][i]);
8189 }
8190 }
8191 }
8192 }
8193
8194 int p2m_canonical_order(int order)
8195 {
8196 if ( order % 9
8197 || (order / 9) > 2 )
8198 {
8199 fprintf(warn, "%s: Strange, non-canonical order %d\n",
8200 __func__, order);
8201 order = 4;
8202 } else {
8203 order /= 9;
8204 }
8205 return order;
8206 }
8207
8208 void mem_pod_zero_reclaim_process(struct pcpu_info *p)
8209 {
8210 struct record_info *ri = &p->ri;
8211 int context = POD_RECLAIM_CONTEXT_UNKNOWN;
8212 struct vcpu_data *v = p->current;
8213
8214 struct {
8215 uint64_t gfn, mfn;
8216 uint32_t d, order;
8217 } *r = (typeof(r))ri->d;
8218
8219 if ( v && v->hvm.vmexit_valid )
8220 {
8221 switch(v->hvm.exit_reason)
8222 {
8223 case EXIT_REASON_EPT_VIOLATION:
8224 case EXIT_REASON_EXCEPTION_NMI:
8225 context = POD_RECLAIM_CONTEXT_FAULT;
8226 break;
8227 case EXIT_REASON_VMCALL:
8228 context = POD_RECLAIM_CONTEXT_BALLOON;
8229 break;
8230 }
8231 }
8232
8233 if ( opt.dump_all )
8234 {
8235 printf(" %s pod_zero_reclaim d%d o%d g %llx m %llx ctx %s\n",
8236 ri->dump_header,
8237 r->d, r->order,
8238 (unsigned long long)r->gfn, (unsigned long long)r->mfn,
8239 pod_reclaim_context_name[context]);
8240
8241 }
8242
8243 if ( opt.summary_info )
8244 {
8245 struct domain_data *d;
8246
8247 if ( v && (d=v->d) )
8248 {
8249 int order;
8250
8251 order = p2m_canonical_order(r->order);
8252
8253 d->pod.reclaim_order[order]++;
8254 d->pod.reclaim_context[context]++;
8255 d->pod.reclaim_context_order[context][order]++;
8256 }
8257 }
8258 }
8259
8260 void mem_pod_populate_process(struct pcpu_info *p)
8261 {
8262 struct record_info *ri = &p->ri;
8263
8264 struct {
8265 uint64_t gfn, mfn;
8266 uint32_t d, order;
8267 } *r = (typeof(r))ri->d;
8268
8269 if ( opt.dump_all )
8270 {
8271 printf(" %s pod_populate d%d o%d g %llx m %llx\n",
8272 ri->dump_header,
8273 r->d, r->order,
8274 (unsigned long long)r->gfn, (unsigned long long)r->mfn);
8275 }
8276
8277 if ( opt.summary_info )
8278 {
8279 struct vcpu_data *v = p->current;
8280 struct domain_data *d;
8281
8282 if ( v && (d=v->d) )
8283 {
8284 int order;
8285
8286 order = p2m_canonical_order(r->order);
8287
8288 d->pod.populate_order[order]++;
8289 }
8290 }
8291 }
8292
8293 void mem_pod_superpage_splinter_process(struct pcpu_info *p)
8294 {
8295 struct record_info *ri = &p->ri;
8296
8297 struct {
8298 uint64_t gfn;
8299 uint32_t d, order;
8300 } *r = (typeof(r))ri->d;
8301
8302 if ( opt.dump_all )
8303 {
8304 printf(" %s pod_spage_splinter d%d o%d g %"PRIx64"\n",
8305 ri->dump_header,
8306 r->d, r->order, r->gfn);
8307 }
8308 }
8309
8310 void mem_page_grant(struct pcpu_info *p)
8311 {
8312 struct record_info *ri = &p->ri;
8313
8314 struct {
8315 unsigned domain;
8316 } *r = (typeof(r))ri->d;
8317 union pv_event pevt = { .event = ri->event };
8318
8319 if ( opt.dump_all )
8320 {
8321 printf(" %s %s domain %u\n", ri->dump_header, mem_name[pevt.minor], r->domain);
8322 }
8323 }
8324 void mem_set_p2m_entry_process(struct pcpu_info *p)
8325 {
8326 struct record_info *ri = &p->ri;
8327
8328 struct {
8329 uint64_t gfn, mfn;
8330 uint32_t p2mt;
8331 uint16_t d, order;
8332 } *r = (typeof(r))ri->d;
8333
8334 if ( opt.dump_all )
8335 {
8336 printf(" %s set_p2m_entry d%u o%u t %u g %llx m %llx\n",
8337 ri->dump_header,
8338 r->d, r->order,
8339 r->p2mt,
8340 (unsigned long long)r->gfn, (unsigned long long)r->mfn);
8341 }
8342 }
8343
8344 void mem_decrease_reservation_process(struct pcpu_info *p)
8345 {
8346 struct record_info *ri = &p->ri;
8347
8348 struct {
8349 uint64_t gfn;
8350 uint32_t d, order;
8351 } *r = (typeof(r))ri->d;
8352
8353 if ( opt.dump_all )
8354 {
8355 printf(" %s decrease_reservation d%d o%d g %llx\n",
8356 ri->dump_header,
8357 r->d, r->order,
8358 (unsigned long long)r->gfn);
8359 }
8360 }
8361
8362 void mem_process(struct pcpu_info *p) {
8363 struct record_info *ri = &p->ri;
8364 struct {
8365 int dom;
8366 } *r = (typeof(r))ri->d;
8367
8368 int minor = ri->evt.minor;
8369
8370 switch ( minor )
8371 {
8372 case MEM_PAGE_GRANT_MAP:
8373 case MEM_PAGE_GRANT_UNMAP:
8374 case MEM_PAGE_GRANT_TRANSFER:
8375 mem_page_grant(p);
8376 break;
8377 case MEM_SET_P2M_ENTRY:
8378 mem_set_p2m_entry_process(p);
8379 break;
8380 case MEM_DECREASE_RESERVATION:
8381 mem_decrease_reservation_process(p);
8382 break;
8383 case MEM_POD_POPULATE:
8384 mem_pod_populate_process(p);
8385 break;
8386 case MEM_POD_ZERO_RECLAIM:
8387 mem_pod_zero_reclaim_process(p);
8388 break;
8389 case MEM_POD_SUPERPAGE_SPLINTER:
8390 mem_pod_superpage_splinter_process(p);
8391 break;
8392 default:
8393 if(opt.dump_all) {
8394 dump_generic(stdout, ri);
8395 }
8396
8397 if(opt.summary_info && minor < MEM_MAX) {
8398 struct domain_data *d;
8399
8400 if(p->current) {
8401 if (p->current->d) {
8402 p->current->d->memops.done[minor]++;
8403 p->current->d->memops.done_interval[minor]++;
8404 }
8405 if((d=domain_find(r->dom))) {
8406 d->memops.done_for[minor]++;
8407 d->memops.done_for_interval[minor]++;
8408 }
8409 }
8410 }
8411 break;
8412 }
8413
8414 }
8415
8416 /* ---- PM ---- */
8417 #define CSTATE_MAX 5
8418 #define CSTATE_INVALID ((CSTATE_MAX)+1)
8419 void pm_process(struct pcpu_info *p) {
8420 struct record_info *ri = &p->ri;
8421
8422 switch ( ri->event )
8423 {
8424 case TRC_PM_FREQ_CHANGE:
8425 if (opt.dump_all )
8426 printf(" %s pm_freq_change o%d n%d\n",
8427 ri->dump_header,
8428 ri->d[0],
8429 ri->d[1]);
8430 break;
8431 case TRC_PM_IDLE_ENTRY:
8432 if (opt.dump_all )
8433 printf(" %s pm_idle_start c%d\n",
8434 ri->dump_header,
8435 ri->d[0]);
8436 if ( ri->d[0] <= CSTATE_MAX )
8437 {
8438 p->power_state=ri->d[0];
8439 pcpu_string_draw(p);
8440 }
8441 break;
8442 case TRC_PM_IDLE_EXIT:
8443 if (opt.dump_all )
8444 printf(" %s pm_idle_end c%d\n",
8445 ri->dump_header,
8446 ri->d[0]);
8447 if ( p->power_state != ri->d[0]
8448 && p->power_state != CSTATE_INVALID )
8449 printf("Strange, pm_idle_end %d, power_state %d!\n",
8450 ri->d[0], p->power_state);
8451 p->power_state = 0;
8452 pcpu_string_draw(p);
8453 break;
8454 default:
8455 if(opt.dump_all) {
8456 dump_generic(stdout, ri);
8457 }
8458 break;
8459 }
8460
8461 }
8462
8463 /*
8464 * IRQ related stuff
8465 */
8466
8467 #define MAX_VECTOR 256
8468 int global_vector_used[256] = {0};
8469 struct pci_dev {
8470 uint8_t bus;
8471 uint8_t devfn;
8472 int vector_used[MAX_VECTOR];
8473 struct pci_dev *next;
8474 } *pdev_list;
8475
8476 #define MAX_IRQ 512
8477 struct irq_desc {
8478 enum {
8479 IRQ_NONE,
8480 IRQ_MSI,
8481 IRQ_GSI
8482 } type;
8483 struct pci_dev *dev;
8484 } irq_table[MAX_IRQ];
8485
8486 struct pci_dev * pdev_find(uint8_t bus, uint8_t devfn)
8487 {
8488 struct pci_dev *d, *n, **q;
8489
8490 /* Look for domain, keeping track of the last pointer so we can add
8491 a domain if we need to. */
8492 for ( d = pdev_list, q=&pdev_list ;
8493 d && ( (d->bus < bus)
8494 || (d->bus == bus && d->devfn < devfn) ) ;
8495 q = &d->next, d=d->next ) ;
8496
8497 if(d && d->bus == bus && d->devfn == devfn)
8498 return d;
8499
8500 /* Make a new domain */
8501 fprintf(warn, "Creating pdev %02x:%02x.%x\n", bus, devfn>>4, devfn&3);
8502
8503 if((n=malloc(sizeof(*n)))==NULL)
8504 {
8505 fprintf(stderr, "%s: malloc %zd failed!\n", __func__, sizeof(*n));
8506 error(ERR_SYSTEM, NULL);
8507 }
8508
8509 bzero(n, sizeof(*n));
8510
8511 n->bus=bus;
8512 n->devfn=devfn;
8513
8514 /* Insert it into the list */
8515 n->next = d;
8516 *q = n;
8517
8518 return n;
8519 }
8520
8521 void irq_process(struct pcpu_info *p) {
8522 struct record_info *ri = &p->ri;
8523
8524 switch ( ri->event )
8525 {
8526 case TRC_HW_IRQ_BIND_VECTOR:
8527 {
8528 struct {
8529 int irq, vec;
8530 unsigned mask[4];
8531 } *r = (typeof(r))ri->d;
8532 if ( opt.dump_all )
8533 {
8534 printf(" %s irq_bind_vector irq %x vec %x mask %04x %04x %04x %04x\n",
8535 ri->dump_header,
8536 r->irq, r->vec,
8537 r->mask[3],
8538 r->mask[2],
8539 r->mask[1],
8540 r->mask[0]);
8541 }
8542 break;
8543 }
8544 case TRC_HW_IRQ_HANDLED:
8545 {
8546 struct {
8547 int irq, start_tsc, end_tsc;
8548 } *r = (typeof(r))ri->d;
8549 int arctime;
8550
8551 arctime = r->end_tsc - r->start_tsc;
8552 if ( opt.dump_all )
8553 {
8554 printf(" %s irq_handled irq %x %d (%d,%d)\n",
8555 ri->dump_header,
8556 r->irq, arctime, r->start_tsc, r->end_tsc);
8557 }
8558 if ( opt.scatterplot_irq )
8559 {
8560 struct time_struct t;
8561
8562 abs_cycles_to_time(ri->tsc, &t);
8563
8564 printf("i%x %u.%09u %d\n",
8565 (unsigned)r->irq,
8566 t.s, t.ns,
8567 p->pid);
8568 }
8569 break;
8570 }
8571 case TRC_HW_IRQ_ASSIGN_VECTOR:
8572 {
8573 struct {
8574 int irq, vec;
8575 unsigned mask[4];
8576 } *r = (typeof(r))ri->d;
8577 if ( opt.dump_all )
8578 {
8579 printf(" %s irq_assign_vector irq %x vec %x mask %04x %04x %04x %04x\n",
8580 ri->dump_header,
8581 r->irq, r->vec,
8582 r->mask[3],
8583 r->mask[2],
8584 r->mask[1],
8585 r->mask[0]);
8586 }
8587 if ( r->irq < MAX_IRQ
8588 && r->vec < MAX_VECTOR )
8589 {
8590 if ( irq_table[r->irq].type == IRQ_MSI )
8591 {
8592 if(global_vector_used[r->vec])
8593 fprintf(warn, " Vector collision on global table!\n");
8594 global_vector_used[r->vec]=1;
8595 }
8596 if( irq_table[r->irq].dev )
8597 {
8598 struct pci_dev * pdev=irq_table[r->irq].dev;
8599
8600 if(pdev->vector_used[r->vec])
8601 fprintf(warn, " Vector collision on %02x.%02x!\n",
8602 pdev->bus, pdev->devfn);
8603 pdev->vector_used[r->vec]=1;
8604 }
8605 }
8606 break;
8607 }
8608 case TRC_HW_IRQ_MOVE_CLEANUP_DELAY:
8609 {
8610 struct {
8611 int irq, vec, cpu;
8612 } *r = (typeof(r))ri->d;
8613
8614 if ( opt.dump_all )
8615 {
8616 printf(" %s irq_move_cleanup_delay irq %x vec %x cpu %d\n",
8617 ri->dump_header,
8618 r->irq, r->vec, r->cpu);
8619 }
8620 break;
8621 }
8622 case TRC_HW_IRQ_MOVE_CLEANUP:
8623 {
8624 struct {
8625 int irq;
8626 int vec;
8627 int cpu;
8628 } *r = (typeof(r))ri->d;
8629
8630 if ( opt.dump_all )
8631 {
8632 printf(" %s irq_move_cleanup irq %x vec %x cpu %d\n",
8633 ri->dump_header,
8634 r->irq, r->vec, r->cpu);
8635 }
8636 if ( r->irq < MAX_IRQ
8637 && r->vec < MAX_VECTOR )
8638 {
8639 if ( irq_table[r->irq].type == IRQ_MSI )
8640 {
8641 if(!global_vector_used[r->vec])
8642 fprintf(warn," Strange, cleanup on non-used vector\n");
8643 global_vector_used[r->vec]=0;
8644 }
8645 if ( irq_table[r->irq].dev )
8646 {
8647 struct pci_dev * pdev=irq_table[r->irq].dev;
8648
8649 if(!pdev->vector_used[r->vec])
8650 fprintf(warn," Strange, cleanup on non-used vector\n");
8651 pdev->vector_used[r->vec]=0;
8652 }
8653 }
8654 break;
8655 }
8656 case TRC_HW_IRQ_UNMAPPED_VECTOR:
8657 {
8658 struct {
8659 int vec;
8660 } *r = (typeof(r))ri->d;
8661
8662 if ( opt.dump_all )
8663 {
8664 printf(" %s irq_unmapped_vector vec %x\n",
8665 ri->dump_header,
8666 r->vec);
8667 }
8668 break;
8669 }
8670 case TRC_HW_IRQ_CLEAR_VECTOR:
8671 case TRC_HW_IRQ_MOVE_FINISH :
8672 default:
8673 if(opt.dump_all) {
8674 dump_generic(stdout, ri);
8675 }
8676 break;
8677 }
8678 }
8679
8680 #define TRC_HW_SUB_PM 1
8681 #define TRC_HW_SUB_IRQ 2
8682 void hw_process(struct pcpu_info *p)
8683 {
8684 struct record_info *ri = &p->ri;
8685
8686 switch(ri->evt.sub)
8687 {
8688 case TRC_HW_SUB_PM:
8689 pm_process(p);
8690 break;
8691 case TRC_HW_SUB_IRQ:
8692 irq_process(p);
8693 break;
8694 }
8695
8696 }
8697
8698 #define TRC_DOM0_SUB_DOMOPS 1
8699 void dom0_process(struct pcpu_info *p)
8700 {
8701 struct record_info *ri = &p->ri;
8702
8703 switch(ri->evt.sub)
8704 {
8705 case TRC_DOM0_SUB_DOMOPS:
8706 if(opt.dump_all) {
8707 struct {
8708 unsigned int domid;
8709 } *r = (typeof(r))ri->d;
8710
8711 printf(" %s %s domain d%u\n", ri->dump_header,
8712 ri->event == TRC_DOM0_DOM_ADD ? "creating" : "destroying",
8713 r->domid);
8714 }
8715 break;
8716 default:
8717 process_generic(&p->ri);
8718 }
8719 }
8720
8721 /* ---- Base ----- */
8722 void dump_generic(FILE * f, struct record_info *ri)
8723 {
8724 int i;
8725
8726 fprintf(f, "]%s %7x(%x:%x:%x) %u [",
8727 ri->dump_header,
8728 ri->event,
8729 ri->evt.main,
8730 ri->evt.sub,
8731 ri->evt.minor,
8732 ri->extra_words);
8733
8734 for(i=0; i<ri->extra_words; i++) {
8735 fprintf(f, " %x", ri->d[i]);
8736 }
8737
8738 fprintf(f, " ]\n");
8739 }
8740
8741 void dump_raw(const char * s, struct record_info *ri)
8742 {
8743 int i;
8744
8745 if(ri->rec.cycle_flag)
8746 printf("%s %7x %d %14lld [",
8747 s, ri->event, ri->extra_words, ri->tsc);
8748 else
8749 printf("%s %7x %d %14s [",
8750 s, ri->event, ri->extra_words, "-");
8751
8752 for(i=0; i<7; i++) {
8753 if ( i < ri->extra_words )
8754 printf(" %8x", ri->d[i]);
8755 else
8756 printf(" ");
8757 }
8758
8759 printf(" ] | ");
8760
8761 for (i=0; i<8; i++) {
8762 printf(" %08x", ri->rec.raw[i]);
8763 }
8764
8765 printf(" |\n");
8766 }
8767
8768 void error(enum error_level l, struct record_info *ri)
8769 {
8770 if ( l > opt.tolerance )
8771 {
8772 if ( ri )
8773 dump_generic(warn, ri);
8774 exit(1);
8775 }
8776 }
8777
8778 int check_extra_words(struct record_info *ri,
8779 int expected_size,
8780 const char *record)
8781 {
8782 static int off_by_one = 0;
8783 int expected_extra = expected_size / sizeof(unsigned int);
8784
8785 if(ri->extra_words != expected_extra
8786 && !(off_by_one && ri->extra_words == expected_extra + 1) )
8787 {
8788 if ( !off_by_one && ri->extra_words == expected_extra + 1 )
8789 {
8790 fprintf(warn, "Detected off-by-one bug; relaxing expectations\n");
8791 off_by_one=1;
8792 }
8793 else {
8794 fprintf(warn, "ERROR: %s extra_words %d, expected %d!\n",
8795 record,
8796 ri->extra_words, expected_extra);
8797 error(ERR_RECORD, ri);
8798 return 1;
8799 }
8800 }
8801 return 0;
8802 }
8803
8804 void process_generic(struct record_info *ri) {
8805
8806 error(ERR_STRICT, ri);
8807
8808 if(opt.dump_all) {
8809 dump_generic(stdout, ri);
8810 }
8811 }
8812
8813 int vcpu_set_data_type(struct vcpu_data *v, int type)
8814 {
8815 if (v->data_type == VCPU_DATA_NONE )
8816 {
8817 v->data_type = type;
8818 switch(type)
8819 {
8820 case VCPU_DATA_HVM:
8821 init_hvm_data(&v->hvm, v);
8822 break;
8823 default:
8824 break;
8825 }
8826 }
8827 else
8828 assert(v->data_type == type);
8829 return 0;
8830 }
8831
8832
8833 void lose_vcpu(struct vcpu_data *v, tsc_t tsc)
8834 {
8835 if(v->data_type == VCPU_DATA_HVM)
8836 v->hvm.vmexit_valid=0;
8837 runstate_update(v, RUNSTATE_LOST, tsc);
8838 hvm_vlapic_clear(&v->vlapic);
8839
8840 if(v->data_type == VCPU_DATA_HVM) {
8841 int i;
8842 if(opt.dump_all)
8843 printf(" [w2h] Clearing w2h state for d%dv%d\n",
8844 v->d->did, v->vid);
8845 v->hvm.w2h.interrupts=0;
8846 v->hvm.w2h.vector=0;
8847 v->hvm.w2h.waking = 0;
8848 for(i=0; i<GUEST_INTERRUPT_MAX; i++) {
8849 if(opt.dump_all && v->hvm.summary.guest_interrupt[i].start_tsc) {
8850 printf(" Interrupt %d clearing start_tsc %lld\n",
8851 i, v->hvm.summary.guest_interrupt[i].start_tsc);
8852 }
8853 v->hvm.summary.guest_interrupt[i].start_tsc = 0;
8854 }
8855 }
8856 }
8857
8858 struct lost_record_struct {
8859 int lost_records;
8860 unsigned did:16,vid:16;
8861 tsc_t first_tsc;
8862 };
8863
8864 void process_lost_records(struct pcpu_info *p)
8865 {
8866 struct record_info *ri = &p->ri;
8867 struct lost_record_struct *r = (typeof(r))ri->d;
8868 tsc_t first_tsc; /* TSC of first record that was lost */
8869
8870 /* Sanity checks */
8871 if(ri->extra_words != 4)
8872 {
8873 fprintf(warn, "FATAL: Lost record has unexpected extra words %d!\n",
8874 ri->extra_words);
8875 error(ERR_RECORD, ri);
8876 return;
8877 }
8878
8879 first_tsc = r->first_tsc;
8880
8881 if(opt.dump_all)
8882 {
8883 if(p->current)
8884 printf(" %s lost_records count %d d%uv%u (cur d%dv%d) first_tsc %lld\n",
8885 ri->dump_header, r->lost_records,
8886 r->did, r->vid,
8887 p->current->d->did, p->current->vid,
8888 r->first_tsc);
8889 else
8890 printf(" %s lost_records count %d d%uv%u (cur X) first_tsc %lld\n",
8891 ri->dump_header, r->lost_records,
8892 r->did, r->vid,
8893 r->first_tsc);
8894 }
8895
8896 #if 0
8897 if(opt.dump_trace_volume_on_lost_record)
8898 volume_summary(&p->volume.last_buffer);
8899 #endif
8900
8901 if ( p->current ) {
8902
8903 hvm_vlapic_clear(&p->current->vlapic);
8904 if(p->current->data_type == VCPU_DATA_HVM) {
8905 p->current->hvm.vmexit_valid=0;
8906 cr3_switch(0, &p->current->hvm);
8907 }
8908
8909 /* We may lose scheduling records; so we need to:
8910 * - Point all records until now to the next schedule in the
8911 * "default" domain
8912 * - Make sure there are no warnings / strangeness with the
8913 * current vcpu (if it gets scheduled elsewhere).
8914 */
8915 vcpu_prev_update(p, p->current, first_tsc, RUNSTATE_LOST);
8916 }
8917 #if 0
8918 vcpu_next_update(p, default_domain.vcpu[p->pid], first_tsc);
8919 if(p->current->data_type == VCPU_DATA_HVM) {
8920 p->current->hvm.vmexit_valid=0;
8921 }
8922 #endif
8923
8924 /* The lost record trace is processed early -- i.e.,
8925 * After the last good record, rather than when the next
8926 * record is processed. Between the time it's processed and
8927 * the time it actually went in, the vcpu may be scheduled on
8928 * other processors. So we can't switch vcpus until the first
8929 * TSC'd record after the lost record. */
8930 if(!p->lost_record.active) {
8931 P.lost_cpus++;
8932 if(P.lost_cpus > P.max_active_pcpu + 1) {
8933 fprintf(warn, "ERROR: P.lost_cpus %d > P.max_active_pcpu + 1 %d!\n",
8934 P.lost_cpus, P.max_active_pcpu + 1);
8935 error(ERR_ASSERT, NULL);
8936 }
8937 } else
8938 fprintf(warn, "Strange, lost record for pcpu %d, but lost_record still active!\n",
8939 p->pid);
8940
8941 p->lost_record.active = 1;
8942 p->lost_record.tsc = first_tsc;
8943 pcpu_string_draw(p);
8944
8945 {
8946 /* Any vcpu which is not actively running may be scheduled on the
8947 * lost cpu. To avoid mis-accounting, we need to reset */
8948 struct domain_data *d;
8949 int i;
8950 for(d=domain_list ; d; d=d->next)
8951 {
8952 if(d->did != DEFAULT_DOMAIN) {
8953 for(i=0; i<MAX_CPUS; i++)
8954 if(d->vcpu[i] &&
8955 d->vcpu[i]->runstate.state != RUNSTATE_RUNNING) {
8956 if(opt.dump_all)
8957 fprintf(warn, "%s: setting d%dv%d to RUNSTATE_LOST\n",
8958 __func__, d->did, i);
8959 lose_vcpu(d->vcpu[i], first_tsc);
8960 }
8961 }
8962 }
8963 }
8964
8965 p->lost_record.domain_valid=1;
8966 p->lost_record.did=r->did;
8967 p->lost_record.vid=r->vid;
8968 }
8969
8970
8971 void process_lost_records_end(struct pcpu_info *p)
8972 {
8973 struct record_info *ri = &p->ri;
8974 struct lost_record_struct *r = (typeof(r))ri->d;
8975
8976 if(!p->lost_record.active) {
8977 fprintf(warn, "FATAL: lost_records_end but pid %d not lost!\n",
8978 p->pid);
8979 error(ERR_FILE, NULL);
8980 return;
8981 }
8982
8983 /* Lost records. If this is the first record on a pcpu after the loss,
8984 * Update the information. */
8985 if(ri->tsc > p->lost_record.tsc)
8986 {
8987 if(opt.dump_all)
8988 printf(" %s lost_records end ---\n",
8989 pcpu_string(p->pid));
8990
8991 update_cycles(&p->time.lost, ri->tsc - p->lost_record.tsc);
8992
8993 if(p->lost_record.domain_valid) {
8994 int did = p->lost_record.did,
8995 vid = p->lost_record.vid;
8996
8997 if(opt.dump_all)
8998 printf(" %s lost_records end d%dv%d---\n",
8999 pcpu_string(p->pid),
9000 did, vid);
9001 if(p->current)
9002 {
9003 fprintf(warn, "FATAL: lost_record valid (d%dv%d), but current d%dv%d!\n",
9004 did, vid,
9005 p->current->d->did, p->current->vid);
9006 error(ERR_FILE, NULL);
9007 return;
9008 }
9009
9010 if(opt.dump_all)
9011 fprintf(warn, "Changing p%d current to d%dv%d\n",
9012 p->pid, did, vid);
9013 vcpu_next_update(p,
9014 vcpu_find(did, vid),
9015 ri->tsc);
9016 p->lost_record.domain_valid=0;
9017 p->lost_record.seen_valid_schedule=0; /* Let next vcpu_next_update know that
9018 this one was inferred */
9019 } else {
9020 if(opt.dump_all)
9021 printf(" %s lost_records end (domain invalid)---\n",
9022 pcpu_string(p->pid));
9023 }
9024
9025
9026 p->lost_record.active = 0;
9027 pcpu_string_draw(p);
9028 P.lost_cpus--;
9029 if(P.lost_cpus < 0) {
9030 fprintf(warn, "ERROR: lost_cpus fell below 0 for pcpu %d!\n",
9031 p->pid);
9032 error(ERR_ASSERT, NULL);
9033 }
9034 }
9035 }
9036
9037 void base_process(struct pcpu_info *p) {
9038 struct record_info *ri = &p->ri;
9039 switch(ri->event)
9040 {
9041 case TRC_TRACE_WRAP_BUFFER:
9042 break;
9043 case TRC_LOST_RECORDS:
9044 process_lost_records(p);
9045 break;
9046 case TRC_LOST_RECORDS_END:
9047 process_lost_records_end(p);
9048 break;
9049 default:
9050 process_generic(ri);
9051 }
9052 }
9053
9054
9055
9056 /* Non-compat only */
9057 void record_order_insert(struct pcpu_info *new);
9058 void record_order_remove(struct pcpu_info *rem);
9059 void record_order_bubble(struct pcpu_info *last);
9060
9061 struct cpu_change_data {
9062 int cpu;
9063 unsigned window_size;
9064 };
9065
9066 void activate_early_eof(void) {
9067 struct pcpu_info *p;
9068 int i;
9069
9070 fprintf(warn, "Short cpu_change window, activating early_eof\n");
9071
9072 P.early_eof = 1;
9073
9074 for(i=0; i<=P.max_active_pcpu; i++) {
9075 p = P.pcpu + i;
9076 if(p->active && p->file_offset > P.last_epoch_offset) {
9077 fprintf(warn, " deactivating pid %d\n",
9078 p->pid);
9079 p->active = 0;
9080 }
9081 }
9082 }
9083
9084 off_t scan_for_new_pcpu(off_t offset) {
9085 ssize_t r;
9086 struct trace_record rec;
9087 struct cpu_change_data *cd;
9088
9089 r=__read_record(&rec, offset);
9090
9091 if(r==0)
9092 return 0;
9093
9094 if(rec.event != TRC_TRACE_CPU_CHANGE
9095 || rec.cycle_flag)
9096 {
9097 fprintf(stderr, "%s: Unexpected record event %x!\n",
9098 __func__, rec.event);
9099 error(ERR_ASSERT, NULL); /* Actually file, but can't recover */
9100 }
9101
9102 cd = (typeof(cd))rec.u.notsc.data;
9103
9104 if ( cd->cpu > MAX_CPUS )
9105 {
9106 fprintf(stderr, "%s: cpu %d exceeds MAX_CPU %d!\n",
9107 __func__, cd->cpu, MAX_CPUS);
9108 /* FIXME: Figure out if we could handle this more gracefully */
9109 error(ERR_ASSERT, NULL);
9110 }
9111
9112 if(cd->cpu > P.max_active_pcpu || !P.pcpu[cd->cpu].active) {
9113 struct pcpu_info *p = P.pcpu + cd->cpu;
9114
9115 fprintf(warn, "%s: Activating pcpu %d at offset %lld\n",
9116 __func__, cd->cpu, (unsigned long long)offset);
9117
9118 p->active = 1;
9119 /* Process this cpu_change record first */
9120 p->ri.rec = rec;
9121 p->ri.size = r;
9122 __fill_in_record_info(p);
9123
9124 p->file_offset = offset;
9125 p->next_cpu_change_offset = offset;
9126
9127 record_order_insert(p);
9128
9129 offset += r + cd->window_size;
9130
9131 sched_default_vcpu_activate(p);
9132
9133 if ( cd->cpu > P.max_active_pcpu )
9134 P.max_active_pcpu = cd->cpu;
9135
9136 return offset;
9137 } else {
9138 return 0;
9139 }
9140 }
9141
9142 /*
9143 * Conceptually, when we reach a cpu_change record that's not for our pcpu,
9144 * we want to scan forward through the file until we reach one that's for us.
9145 * However, looping through involves reading the file, which we'd rather
9146 * do in one place. Because cpu_change records don't include a tsc,
9147 * the same pcpu will be processed repeatedly until the cpu_change
9148 * equals p->pid.
9149 *
9150 * There are two additional things we need to do in this algorithm:
9151 * + Detect new pcpus as they come online
9152 * + De-activate pcpus which don't have any more records
9153 *
9154 * Detecting new pcpus which are less than P.max_active_pcpu is straight-
9155 * forward: when max_active_pcpu is searching for its next cpu window,
9156 * it will pass by the new cpu's window, and can activate it then.
9157 *
9158 * Detecting new pcpus greater than P.max_active_pcpu is a little harder;
9159 * When max_active_pcpu is scanning for its next cpu window, after it's found
9160 * it, we need to scan one more window forward to see if its' an already-active
9161 * pcpu; if not, activate it.
9162 *
9163 * We also need to deal with truncated files, where records from one pcpu may
9164 * be present but not from another pcpu due to lack of disk space. The best
9165 * thing to do is to find the last "epoch" and essentially truncate the file
9166 * to that.
9167 */
9168 void deactivate_pcpu(struct pcpu_info *p)
9169 {
9170 if ( p->current )
9171 {
9172 pcpu_runstate_update(p, p->last_tsc);
9173
9174 fprintf(warn, "%s: setting d%dv%d to state LOST\n",
9175 __func__, p->current->d->did,
9176 p->current->vid);
9177 lose_vcpu(p->current, p->last_tsc);
9178 }
9179 p->active = 0;
9180
9181 record_order_remove(p);
9182
9183 if ( p->pid == P.max_active_pcpu )
9184 {
9185 int i, max_active_pcpu = -1;
9186 for(i=0; i<=P.max_active_pcpu; i++)
9187 {
9188 if(!P.pcpu[i].active)
9189 continue;
9190
9191 max_active_pcpu = i;
9192 }
9193 P.max_active_pcpu = max_active_pcpu;
9194 fprintf(warn, "%s: Setting max_active_pcpu to %d\n",
9195 __func__, max_active_pcpu);
9196 }
9197
9198 }
9199
9200 /* Helper function to process tsc-related record info */
9201 void process_record_tsc(tsc_t order_tsc, struct record_info *ri)
9202 {
9203 /* Find the first tsc set */
9204 if(ri->tsc && ri->tsc >= P.f.first_tsc) {
9205 /* We use the order_tsc to account for the second processing of
9206 * a lost record. */
9207 tsc_t tsc = order_tsc;
9208
9209 if(P.f.first_tsc == 0) {
9210 P.f.first_tsc = tsc;
9211 if ( opt.interval_mode ) {
9212 P.interval.start_tsc = tsc;
9213 }
9214 } else {
9215 if ( opt.interval_mode ) {
9216 if(P.interval.start_tsc > tsc) {
9217 fprintf(warn, "FATAL: order_tsc %lld < interval.start_tsc %lld!\n",
9218 tsc, P.interval.start_tsc);
9219 error(ERR_FILE, NULL);
9220 } else {
9221 while ( tsc - P.interval.start_tsc > opt.interval.cycles ) {
9222 interval_callback();
9223 P.interval.start_tsc += opt.interval.cycles;
9224 }
9225 }
9226 }
9227 }
9228
9229 P.f.last_tsc=tsc;
9230
9231 P.f.total_cycles = P.f.last_tsc - P.f.first_tsc;
9232
9233 P.now = tsc;
9234 }
9235 }
9236
9237 /* Standardized part of dump output */
9238 void create_dump_header(struct record_info *ri, struct pcpu_info *p)
9239 {
9240 char * c;
9241 int len, r;
9242
9243 len = DUMP_HEADER_MAX;
9244 c = ri->dump_header;
9245
9246 abs_cycles_to_time(ri->tsc, &ri->t);
9247
9248 if ( ri->t.time )
9249 {
9250 r=snprintf(c, len, "%3u.%09u", ri->t.s, ri->t.ns);
9251 c+=r;
9252 len-=r;
9253 }
9254 else
9255 {
9256 r=snprintf(c,
9257 len,
9258 " ");
9259 c+=r;
9260 len-=r;
9261 }
9262
9263 r = snprintf(c, len, " %s", pcpu_string(ri->cpu));
9264 c+=r;
9265 len-=r;
9266
9267 if ( p->current )
9268 {
9269 r = snprintf(c, len, " d%dv%d", p->current->d->did, p->current->vid);
9270 c+=r;
9271 len-=r;
9272 }
9273 else
9274 {
9275 r = snprintf(c, len, " d?v?");
9276 c+=r;
9277 len-=r;
9278 }
9279 }
9280
9281 int find_toplevel_event(struct record_info *ri)
9282 {
9283 int toplevel=0, i, count;
9284
9285 for(i=0, count=0; i<TOPLEVEL_MAX; i++)
9286 if(ri->evt.main & (1UL<<i))
9287 {
9288 toplevel=i;
9289 count++;
9290 }
9291
9292 /* Sanity check: One and only one bit should be set */
9293 if(count != 1)
9294 {
9295 fprintf(warn, "FATAL: unexpected number bits(%d) in evt.main! event %x main %x sub %x minor %x\n",
9296 count,
9297 ri->event,
9298 ri->evt.main, ri->evt.sub, ri->evt.minor);
9299 error(ERR_RECORD, NULL);
9300 return -1;
9301 }
9302
9303 return toplevel;
9304 }
9305
9306
9307 void process_cpu_change(struct pcpu_info *p) {
9308 struct record_info *ri = &p->ri;
9309 struct cpu_change_data *r = (typeof(r))ri->d;
9310
9311 if(opt.dump_all && verbosity >= 6) {
9312 printf("]%s cpu_change this-cpu %u record-cpu %u window_size %u(0x%08x)\n",
9313 ri->dump_header, p->pid, r->cpu, r->window_size,
9314 r->window_size);
9315 }
9316
9317 /* File sanity check */
9318 if(p->file_offset != p->next_cpu_change_offset) {
9319 fprintf(warn, "Strange, pcpu %d expected offset %llx, actual %llx!\n",
9320 p->pid, (unsigned long long)p->next_cpu_change_offset,
9321 (unsigned long long)p->file_offset);
9322 }
9323
9324 if(r->cpu > MAX_CPUS)
9325 {
9326 fprintf(stderr, "FATAL: cpu %d > MAX_CPUS %d.\n",
9327 r->cpu, MAX_CPUS);
9328 /* Actually file, but takes some work to skip */
9329 error(ERR_ASSERT, NULL);
9330 }
9331
9332 /* Detect beginning of new "epoch" while scanning thru file */
9333 if((p->last_cpu_change_pid > r->cpu)
9334 && (p->file_offset > P.last_epoch_offset)) {
9335 P.last_epoch_offset = p->file_offset;
9336 }
9337
9338 /* If that pcpu has never been activated, activate it. */
9339 if(!P.pcpu[r->cpu].active && P.pcpu[r->cpu].file_offset == 0)
9340 {
9341 struct pcpu_info * p2 = P.pcpu + r->cpu;
9342
9343 p2->active = 1;
9344 if(r->cpu > P.max_active_pcpu)
9345 P.max_active_pcpu = r->cpu;
9346
9347 /* Taking this record as the first record should make everything
9348 * run swimmingly. */
9349 p2->ri = *ri;
9350 p2->ri.cpu = r->cpu;
9351 p2->ri.d = p2->ri.rec.u.notsc.data;
9352 p2->file_offset = p->file_offset;
9353 p2->next_cpu_change_offset = p->file_offset;
9354
9355 fprintf(warn, "%s: Activating pcpu %d at offset %lld\n",
9356 __func__, r->cpu, (unsigned long long)p->file_offset);
9357
9358 record_order_insert(p2);
9359
9360 sched_default_vcpu_activate(p2);
9361 }
9362
9363 p->last_cpu_change_pid = r->cpu;
9364
9365 /* If this isn't the cpu we're looking for, skip the whole bunch */
9366 if(p->pid != r->cpu)
9367 {
9368 p->file_offset += ri->size + r->window_size;
9369 p->next_cpu_change_offset = p->file_offset;
9370
9371 if(p->file_offset > G.file_size) {
9372 activate_early_eof();
9373 } else if(P.early_eof && p->file_offset > P.last_epoch_offset) {
9374 fprintf(warn, "%s: early_eof activated, pcpu %d past last_epoch_offset %llx, deactivating.\n",
9375 __func__, p->pid, (unsigned long long)P.last_epoch_offset);
9376 deactivate_pcpu(p);
9377 }
9378 }
9379 else
9380 {
9381 /* Track information about dom0 scheduling and records */
9382 if(opt.dump_trace_volume_on_lost_record) {
9383 tsc_t cycles;
9384 struct time_struct t;
9385
9386 /* Update dom0 runstates */
9387 cycles = (p->volume.buffer_first_tsc > p->volume.buffer_dom0_runstate_tsc) ?
9388 p->volume.buffer_first_tsc :
9389 p->volume.buffer_dom0_runstate_tsc;
9390 p->volume.buffer_dom0_runstate_cycles[p->volume.buffer_dom0_runstate]
9391 += ri->tsc - cycles;
9392
9393 printf(" - updated p%d dom0_runstate %s to %lld cycles (+%lld)\n",
9394 p->pid, runstate_name[p->volume.buffer_dom0_runstate],
9395 p->volume.buffer_dom0_runstate_cycles[p->volume.buffer_dom0_runstate],
9396 ri->tsc - cycles);
9397
9398 /* print info */
9399 cycles = ri->tsc - p->volume.buffer_first_tsc;
9400 cycles_to_time(cycles, &t);
9401 printf("Buffer time: %u.%09u (%lld cycles)\n",
9402 t.s, t.ns, cycles);
9403 if(p->volume.buffer_size)
9404 printf("Rate: %lld cycles / byte\n",
9405 cycles / p->volume.buffer_size);
9406 if(P.buffer_trace_virq_tsc)
9407 {
9408 cycles = ri->tsc - P.buffer_trace_virq_tsc;
9409 cycles_to_time(cycles, &t);
9410 printf("trace_virq latency: %u.%09u (%lld cycles)\n",
9411 t.s, t.ns, cycles);
9412 P.buffer_trace_virq_tsc = 0;
9413 }
9414 else
9415 {
9416 printf("No trace_virq record found.\n");
9417 }
9418 printf("Dom0 runstates this buffer:\n");
9419 runstate_summary(p->volume.buffer_dom0_runstate_cycles);
9420 volume_summary(&p->volume.last_buffer);
9421
9422 /* reset info */
9423 p->volume.buffer_first_tsc = 0;
9424 p->volume.buffer_size = r->window_size;
9425 runstate_clear(p->volume.buffer_dom0_runstate_cycles);
9426 volume_clear(&p->volume.last_buffer);
9427 }
9428
9429 p->file_offset += ri->size;
9430 p->next_cpu_change_offset = p->file_offset + r->window_size;
9431
9432 if(p->next_cpu_change_offset > G.file_size)
9433 activate_early_eof();
9434 else if(p->pid == P.max_active_pcpu)
9435 scan_for_new_pcpu(p->next_cpu_change_offset);
9436
9437 }
9438 }
9439
9440 struct tl_assert_mask {
9441 unsigned p_current:1,
9442 not_idle_domain:1;
9443 int vcpu_data_mode;
9444 };
9445 static struct tl_assert_mask tl_assert_checks[TOPLEVEL_MAX] = {
9446 [TRC_HVM_MAIN]={ .p_current=1, .not_idle_domain=1, .vcpu_data_mode=VCPU_DATA_HVM },
9447 [TRC_SHADOW_MAIN]={ .p_current=1, .not_idle_domain=1, .vcpu_data_mode=VCPU_DATA_HVM },
9448 [TRC_PV_MAIN]={ .p_current=1, .not_idle_domain=1, .vcpu_data_mode=VCPU_DATA_PV },
9449 };
9450
9451 /* There are a lot of common assumptions for the various processing
9452 * routines. Check them all in one place, doing something else if
9453 * they don't pass. */
9454 int toplevel_assert_check(int toplevel, struct record_info *ri, struct pcpu_info *p)
9455 {
9456 struct tl_assert_mask mask;
9457 bool is_hvm_emul = (toplevel == TOPLEVEL_HVM) && (ri->evt.sub == 0x4);
9458
9459 mask = tl_assert_checks[toplevel];
9460
9461 if (mask.p_current && p->current == NULL)
9462 {
9463 fprintf(warn, "WARNING: p->current null! Not processing\n");
9464 goto fail;
9465 }
9466
9467 if( mask.not_idle_domain && !is_hvm_emul)
9468 {
9469 /* Can't do this check w/o first doing above check */
9470 assert(mask.p_current);
9471
9472 if ( p->current->d->did == IDLE_DOMAIN) {
9473 fprintf(warn, "WARNING: Unexpected record for idle domain! Not processing\n");
9474 goto fail;
9475 }
9476 }
9477
9478 if ( mask.vcpu_data_mode )
9479 {
9480 struct vcpu_data *v;
9481 assert(mask.p_current);
9482
9483 v = p->current;
9484
9485 if ( ! (v->data_type == VCPU_DATA_NONE
9486 || v->data_type == mask.vcpu_data_mode
9487 || is_hvm_emul) )
9488 {
9489 /* This may happen for track_dirty_vram, which causes a SHADOW_WRMAP_BF trace f/ dom0 */
9490 fprintf(warn, "WARNING: Unexpected vcpu data type for d%dv%d on proc %d! Expected %d got %d. Not processing\n",
9491 v->d->did, v->vid, p->pid,
9492 mask.vcpu_data_mode,
9493 v->data_type);
9494 goto fail;
9495 }
9496 }
9497
9498 return 1;
9499
9500 fail:
9501 dump_generic(warn, &p->ri);
9502 return 0;
9503 }
9504
9505 void process_record(struct pcpu_info *p) {
9506 struct record_info *ri = &p->ri;
9507 int toplevel;
9508
9509 /* Process only TRC_TRACE_CPU_CHANGE */
9510 if(ri->event == TRC_TRACE_CPU_CHANGE) {
9511 process_cpu_change(p);
9512 return;
9513 }
9514
9515 if ( opt.dump_no_processing )
9516 goto out;
9517
9518 p->summary = 1;
9519
9520 if( opt.dump_raw_process )
9521 dump_raw("* ", ri);
9522
9523 process_record_tsc(p->order_tsc, ri);
9524
9525 if(opt.dump_all)
9526 create_dump_header(ri, p);
9527
9528
9529 toplevel = find_toplevel_event(ri);
9530 if ( toplevel < 0 )
9531 return;
9532
9533 /* Unify toplevel assertions */
9534 if ( toplevel_assert_check(toplevel, ri, p) )
9535 {
9536 switch(toplevel) {
9537 case TRC_GEN_MAIN:
9538 base_process(p);
9539 break;
9540 case TRC_SCHED_MAIN:
9541 sched_process(p);
9542 break;
9543 case TRC_HVM_MAIN:
9544 hvm_process(p);
9545 break;
9546 case TRC_SHADOW_MAIN:
9547 shadow_process(p);
9548 break;
9549 case TRC_PV_MAIN:
9550 pv_process(p);
9551 break;
9552 case TRC_MEM_MAIN:
9553 mem_process(p);
9554 break;
9555 case TRC_HW_MAIN:
9556 hw_process(p);
9557 break;
9558 case TRC_DOM0OP_MAIN:
9559 dom0_process(p);
9560 break;
9561 default:
9562 process_generic(ri);
9563 }
9564 }
9565
9566 UPDATE_VOLUME(p, toplevel[toplevel], ri->size);
9567
9568 if(!p->volume.buffer_first_tsc)
9569 p->volume.buffer_first_tsc = ri->tsc;
9570
9571 out:
9572 /* Lost records gets processed twice */
9573 if(ri->event != TRC_LOST_RECORDS)
9574 p->file_offset += ri->size;
9575 }
9576
9577 static inline ssize_t get_rec_size(struct trace_record *rec) {
9578 ssize_t s;
9579
9580 s = sizeof(uint32_t);
9581
9582 if(rec->cycle_flag)
9583 s += sizeof(tsc_t);
9584
9585 s += rec->extra_words * sizeof(uint32_t);
9586
9587 return s;
9588 }
9589
9590 #define STDIN 0
9591
9592 void progress_child_exec(void) {
9593 fclose(stdin);
9594 dup2(G.progress.pipe[0], STDIN);
9595
9596 execlp("zenity", "zenity", "--progress", "--auto-close", "--title",
9597 "Analyzing", "--text", G.trace_file, "--auto-kill", NULL);
9598 }
9599
9600 void progress_init(void) {
9601 int pid;
9602
9603 if (pipe(G.progress.pipe) < 0)
9604 perror("pipe");
9605
9606 if(!(pid = fork())) {
9607 progress_child_exec();
9608
9609 fprintf(stderr, "%s: exec failed (%s), disabling progress bar\n",
9610 __func__, strerror(errno));
9611 opt.progress = 0;
9612 exit(1);
9613 } else if( pid < 0 ) {
9614 fprintf(stderr, "%s: could not fork: %s, disabling progress bar\n",
9615 __func__, strerror(errno));
9616 opt.progress = 0;
9617 }
9618
9619 if( (G.progress.out = fdopen(G.progress.pipe[1], "w")) == NULL ) {
9620 fprintf(stderr, "%s: could not fdopen pipe: %s, disabling progress bar\n",
9621 __func__, strerror(errno));
9622 opt.progress = 0;
9623 }
9624
9625 }
9626
9627 void progress_update(off_t offset) {
9628 long long p;
9629
9630 p = ( offset * 100 ) / G.file_size;
9631
9632 fprintf(G.progress.out, "%lld\n", p);
9633 fflush(G.progress.out);
9634
9635 p += 1;
9636
9637 G.progress.update_offset = ( G.file_size * p ) / 100;
9638
9639 #if 0
9640 fprintf(stderr, "Progress: %lld %% Next update_offset: %lld\n",
9641 p-1,
9642 G.progress.update_offset);
9643 #endif
9644 }
9645
9646 void progress_finish(void) {
9647 int pid;
9648
9649 fprintf(G.progress.out, "100\n");
9650 fflush(G.progress.out);
9651 fclose(G.progress.out);
9652
9653 wait(NULL);
9654
9655 if(!(pid = fork())) {
9656 /* Child */
9657 char text[128];
9658
9659 snprintf(text, 128, "Finished analyzing %s",
9660 G.trace_file);
9661 execlp("zenity", "zenity", "--info", "--text", text, NULL);
9662 }
9663 }
9664
9665 ssize_t __read_record(struct trace_record *rec, off_t offset)
9666 {
9667 ssize_t r, rsize;
9668
9669 r=mread64(G.mh, rec, sizeof(*rec), offset);
9670
9671 if(r < 0) {
9672 /* Read error */
9673 perror("read");
9674 fprintf(stderr, "offset %llx\n", (unsigned long long)offset);
9675 return 0;
9676 } else if(r==0) {
9677 /* End-of-file */
9678 return 0;
9679 } else if(r < sizeof(uint32_t)) {
9680 /* Full header not read */
9681 fprintf(stderr, "%s: short read (%zd bytes)\n",
9682 __func__, r);
9683 error(ERR_SYSTEM, NULL);
9684 }
9685
9686 rsize=get_rec_size(rec);
9687
9688 if(r < rsize) {
9689 /* Full record not read */
9690 fprintf(stderr, "%s: short read (%zd, expected %zd)\n",
9691 __func__, r, rsize);
9692 return 0;
9693 }
9694
9695 return rsize;
9696 }
9697
9698 void __fill_in_record_info(struct pcpu_info *p)
9699 {
9700 struct record_info *ri;
9701 tsc_t tsc=0;
9702
9703 ri = &p->ri;
9704
9705 ri->event = ri->rec.event;
9706 ri->extra_words = ri->rec.extra_words;
9707
9708 if(ri->rec.cycle_flag) {
9709 tsc = (((tsc_t)ri->rec.u.tsc.tsc_hi) << 32)
9710 | ri->rec.u.tsc.tsc_lo;
9711
9712 tsc += p->tsc_skew.offset;
9713
9714 ri->tsc = tsc;
9715 ri->d = ri->rec.u.tsc.data;
9716
9717 if(p->first_tsc == 0)
9718 p->first_tsc = tsc;
9719
9720 /* We process lost record twice: once at the first_tsc,
9721 once at the time it was placed in the log */
9722 if(ri->event == TRC_LOST_RECORDS && ri->extra_words == 4) {
9723 struct lost_record_struct *r = (typeof(r))ri->d;
9724 p->order_tsc = r->first_tsc + p->tsc_skew.offset;
9725 } else
9726 p->order_tsc = tsc;
9727
9728 p->last_tsc = tsc;
9729 } else {
9730 ri->tsc = p->last_tsc;
9731 ri->d = ri->rec.u.notsc.data;
9732 }
9733
9734 if ( opt.dump_raw_reads ) {
9735 char s[256];
9736 snprintf(s, 256, "R p%2d o%016llx ",
9737 p->pid, (unsigned long long)p->file_offset);
9738 dump_raw(s, ri);
9739 }
9740
9741 /* Updated tracing uses CPU_CHANGE. If we hit one of these,
9742 * it will process very next (since the tsc isn't updated), and
9743 * we'll skip forward appropriately. */
9744 ri->cpu = p->pid;
9745 }
9746
9747 ssize_t read_record(struct pcpu_info * p) {
9748 off_t * offset;
9749 struct record_info *ri;
9750
9751 offset = &p->file_offset;
9752 ri = &p->ri;
9753
9754 ri->size = __read_record(&ri->rec, *offset);
9755 if(ri->size)
9756 {
9757 __fill_in_record_info(p);
9758 }
9759 else
9760 {
9761 fprintf(warn, "%s: read returned zero, deactivating pcpu %d\n",
9762 __func__, p->pid);
9763 deactivate_pcpu(p);
9764 }
9765
9766 return ri->size;
9767 }
9768
9769 /*
9770 * This function gets called for every record when doing dump. Try to
9771 * make it efficient by changing the minimum amount from the last
9772 * call. Do this by:
9773 * - Keeping track of the last pcpu called, so we can just set that to -
9774 * - Keeping track of how many pcpus we've "drawn", and only "drawing" new ones
9775 * - Updating the current one
9776 *
9777 * FIXME: Need to deal with pcpu states changing...
9778 *
9779 * WARNING not thread-safe
9780 */
9781
9782 char __pcpu_string[MAX_CPUS+1] = { 0 };
9783 void pcpu_string_draw(struct pcpu_info *p)
9784 {
9785 char *s = __pcpu_string;
9786 int i=p->pid;
9787
9788 if(p->lost_record.active)
9789 s[i]='l';
9790 else if (!p->current)
9791 s[i]=' ';
9792 else if (p->current->d->did == DEFAULT_DOMAIN)
9793 s[i]='.';
9794 else if (p->current->d->did == IDLE_DOMAIN)
9795 {
9796 if ( opt.dump_show_power_states )
9797 s[i]=p->power_state+'0';
9798 else
9799 s[i]='-';
9800 }
9801 else
9802 s[i]='|';
9803 }
9804
9805 char * pcpu_string(int pcpu)
9806 {
9807 char *s = __pcpu_string;
9808 static int max_active_pcpu=-1, last_pcpu=-1;
9809
9810 assert(P.max_active_pcpu < MAX_CPUS);
9811 assert(pcpu <= P.max_active_pcpu);
9812
9813 if(last_pcpu >= 0)
9814 pcpu_string_draw(P.pcpu+last_pcpu);
9815
9816 if(P.max_active_pcpu > max_active_pcpu)
9817 {
9818 int i;
9819 for(i=max_active_pcpu + 1; i<= P.max_active_pcpu; i++)
9820 pcpu_string_draw(P.pcpu+i);
9821 max_active_pcpu=P.max_active_pcpu;
9822 }
9823
9824 s[pcpu]='x';
9825 last_pcpu = pcpu;
9826
9827 return s;
9828 }
9829
9830 /* Null terminated */
9831 struct pcpu_info *record_order[MAX_CPUS+1] = { 0 };
9832
9833 /* In the case of identical tsc values, the old algorithm would favor the
9834 * pcpu with the lowest number. By default the new algorithm favors the
9835 * pcpu which has been processed most recently.
9836 *
9837 * I think the second way is better; but it's good to be able to use the
9838 * old ordering, at very lest to verify that there are no (other) ordering
9839 * differences. Enabling the below flag will cause the insertion / bubble
9840 * routines to order by pcpu id as well as tsc, preserving the old order. */
9841 //#define PRESERVE_PCPU_ORDERING
9842
9843 /* Steady state:
9844 * + Entire list is in order, except (potentially) for the first entry
9845 * + last is pointing to the first entry.
9846 */
9847 void record_order_bubble(struct pcpu_info *last)
9848 {
9849 int i;
9850
9851 /* Find the pcpu to "bubble". This is usually the
9852 * first one, but if other pcpus have been activated, it may
9853 * not be. */
9854 for(i=0; record_order[i] && record_order[i]!=last; i++);
9855
9856 assert(record_order[i]);
9857
9858 /* Now bubble it down */
9859 for( ;
9860 record_order[i+1]
9861 && ( record_order[i+1]->order_tsc < last->order_tsc
9862 #ifdef PRESERVE_PCPU_ORDERING
9863 || ( record_order[i+1]->order_tsc == last->order_tsc
9864 && record_order[i+1]->pid < last->pid )
9865 #endif
9866 ) ;
9867 i++)
9868 record_order[i]=record_order[i+1];
9869 record_order[i]=last;
9870 }
9871
9872 void record_order_insert(struct pcpu_info *new)
9873 {
9874 int i;
9875 struct pcpu_info *p=NULL, *t=NULL;
9876
9877 /* Sanity check: Make sure it's not already in there */
9878 for(i=0; record_order[i]; i++)
9879 assert(record_order[i]!=new);
9880
9881 /* Find where to insert it */
9882 for(i=0;
9883 record_order[i]
9884 && ( record_order[i]->order_tsc < new->order_tsc
9885 #ifdef PRESERVE_PCPU_ORDERING
9886 || ( record_order[i]->order_tsc == new->order_tsc
9887 && record_order[i]->pid < new->pid )
9888 #endif
9889 ) ;
9890 i++)
9891 ;
9892
9893 /* And insert it */
9894 for( p=new; p ; i++)
9895 {
9896 t=record_order[i];
9897 record_order[i]=p;
9898 p=t;
9899 }
9900 }
9901
9902 void record_order_remove(struct pcpu_info *rem)
9903 {
9904 int i;
9905
9906 /* Find where the record is */
9907 for(i=0; record_order[i] && record_order[i]!=rem; i++)
9908 ;
9909
9910 /* Sanity check: Make sure it's actually there! */
9911 assert(record_order[i]);
9912
9913 /* And move everyone forward */
9914 for(; (record_order[i]=record_order[i+1]); i++)
9915 ;
9916 }
9917
9918 struct pcpu_info * choose_next_record(void)
9919 {
9920 struct pcpu_info *min_p=NULL;
9921
9922 min_p=record_order[0];
9923
9924 if(opt.progress && min_p && min_p->file_offset >= G.progress.update_offset)
9925 progress_update(min_p->file_offset);
9926
9927 /* If there are active pcpus, make sure we chose one */
9928 assert(min_p || (P.max_active_pcpu==-1));
9929
9930 return min_p;
9931 }
9932
9933 void process_records(void) {
9934 while(1) {
9935 struct pcpu_info *p = NULL;
9936
9937 if(!(p=choose_next_record()))
9938 return;
9939
9940 process_record(p);
9941
9942 /* Lost records gets processed twice. */
9943 if(p->ri.event == TRC_LOST_RECORDS) {
9944 p->ri.event = TRC_LOST_RECORDS_END;
9945 if(p->ri.tsc > p->order_tsc)
9946 p->order_tsc = p->ri.tsc;
9947 else {
9948 fprintf(warn, "Strange, lost_record ri->tsc %lld !> p->order_tsc %lld!\n",
9949 p->ri.tsc, p->order_tsc);
9950 error(ERR_FILE, NULL);
9951 }
9952 }
9953 else
9954 read_record(p);
9955
9956 /* Update this pcpu in the processing order */
9957 if ( p->active )
9958 record_order_bubble(p);
9959 }
9960 }
9961
9962 void vcpu_summary(struct vcpu_data *v)
9963 {
9964 printf("-- v%d --\n", v->vid);
9965 sched_summary_vcpu(v);
9966 switch(v->data_type) {
9967 case VCPU_DATA_HVM:
9968 hvm_summary(&v->hvm);
9969 break;
9970 case VCPU_DATA_PV:
9971 pv_summary(&v->pv);
9972 break;
9973 default:
9974 break;
9975 }
9976 }
9977
9978 void domain_summary(void)
9979 {
9980 struct domain_data * d;
9981 int i;
9982
9983 if(opt.show_default_domain_summary) {
9984 d = &default_domain;
9985 printf("|-- Default domain --|\n");
9986
9987 for( i = 0; i < MAX_CPUS ; i++ )
9988 {
9989 if(d->vcpu[i])
9990 vcpu_summary(d->vcpu[i]);
9991 }
9992 }
9993
9994 for ( d = domain_list ; d ; d=d->next )
9995 {
9996 int i;
9997 printf("|-- Domain %d --|\n", d->did);
9998
9999 sched_summary_domain(d);
10000
10001 mem_summary_domain(d);
10002
10003 for( i = 0; i < MAX_CPUS ; i++ )
10004 {
10005 if(d->vcpu[i])
10006 vcpu_summary(d->vcpu[i]);
10007 }
10008
10009 printf("Emulate eip list\n");
10010 dump_eip(d->emulate_eip_list);
10011
10012 if ( opt.with_interrupt_eip_enumeration )
10013 {
10014 printf("Interrupt eip list (vector %d)\n",
10015 opt.interrupt_eip_enumeration_vector);
10016 dump_eip(d->interrupt_eip_list);
10017 }
10018
10019 cr3_dump_list(d->cr3_value_head);
10020 }
10021 }
10022
10023 char * stringify_cpu_hz(long long cpu_hz);
10024
10025 void summary(void) {
10026 int i;
10027 printf("Total time: %.2lf seconds (using cpu speed %s)\n",
10028 ((double)(P.f.total_cycles))/opt.cpu_hz,
10029 stringify_cpu_hz(opt.cpu_hz));
10030 printf("--- Log volume summary ---\n");
10031 for(i=0; i<MAX_CPUS; i++)
10032 {
10033 struct pcpu_info *p = P.pcpu+i;
10034 if(!p->summary)
10035 continue;
10036 printf(" - cpu %d -\n", i);
10037 volume_summary(&p->volume.total);
10038 }
10039 domain_summary();
10040 }
10041
10042 void report_pcpu(void) {
10043 int i, active=0;
10044
10045 for(i=0; i<MAX_CPUS; i++)
10046 {
10047 struct pcpu_info *p = P.pcpu+i;
10048 if(!p->summary)
10049 continue;
10050 printf("pcpu %d\n", i);
10051
10052 print_cycle_summary(&p->time.running, " running");
10053 print_cycle_summary(&p->time.idle, " idle");
10054 print_cycle_summary(&p->time.lost, " lost");
10055
10056 if ( p->time.running.count )
10057 active++;
10058 }
10059 printf("Total active cpus: %d\n", active);
10060
10061 }
10062
10063 void init_pcpus(void) {
10064 int i=0;
10065 off_t offset = 0;
10066
10067 for(i=0; i<MAX_CPUS; i++)
10068 {
10069 P.pcpu[i].pid=i;
10070 P.pcpu[i].lost_record.seen_valid_schedule=1;
10071 P.pcpu[i].power_state=CSTATE_INVALID;
10072 }
10073
10074 P.max_active_pcpu = -1;
10075
10076 sched_default_domain_init();
10077
10078 /* Scan through the cpu_change recs until we see a duplicate */
10079 do {
10080 offset = scan_for_new_pcpu(offset);
10081
10082 if(!offset) {
10083 fprintf(warn, "%s: through first trace write, done for now.\n",
10084 __func__);
10085 }
10086 } while(offset);
10087
10088 }
10089
10090 enum {
10091 OPT_NULL=0,
10092 /* Dumping info */
10093 OPT_DUMP_RAW_READS,
10094 OPT_DUMP_RAW_PROCESS,
10095 OPT_DUMP_NO_PROCESSING,
10096 OPT_DUMP_IPI_LATENCY,
10097 OPT_DUMP_TRACE_VOLUME_ON_LOST_RECORD,
10098 OPT_DUMP_SHOW_POWER_STATES,
10099 /* Extra tracking functionality */
10100 OPT_WITH_CR3_ENUMERATION,
10101 OPT_WITH_PIO_ENUMERATION,
10102 OPT_WITH_MMIO_ENUMERATION,
10103 OPT_WITH_INTERRUPT_EIP_ENUMERATION,
10104 OPT_SCATTERPLOT_INTERRUPT_EIP,
10105 OPT_SCATTERPLOT_UNPIN_PROMOTE,
10106 OPT_SCATTERPLOT_CR3_SWITCH,
10107 OPT_SCATTERPLOT_WAKE_TO_HALT,
10108 OPT_SCATTERPLOT_IO,
10109 OPT_SCATTERPLOT_VMEXIT_EIP,
10110 OPT_SCATTERPLOT_RUNSTATE,
10111 OPT_SCATTERPLOT_RUNSTATE_TIME,
10112 OPT_SCATTERPLOT_PCPU,
10113 OPT_SCATTERPLOT_EXTINT_CYCLES,
10114 OPT_SCATTERPLOT_RDTSC,
10115 OPT_SCATTERPLOT_IRQ,
10116 OPT_HISTOGRAM_INTERRUPT_EIP,
10117 /* Interval options */
10118 OPT_INTERVAL_CR3_SCHEDULE_TIME,
10119 OPT_INTERVAL_CR3_SCHEDULE_TIME_ALL,
10120 OPT_INTERVAL_CR3_SCHEDULE_ORDERED,
10121 OPT_INTERVAL_CR3_SHORT_SUMMARY,
10122 OPT_INTERVAL_DOMAIN_TOTAL_TIME,
10123 OPT_INTERVAL_DOMAIN_TOTAL_TIME_ALL,
10124 OPT_INTERVAL_DOMAIN_SHORT_SUMMARY,
10125 OPT_INTERVAL_DOMAIN_GUEST_INTERRUPT,
10126 OPT_INTERVAL_DOMAIN_GRANT_MAPS,
10127 /* Summary info */
10128 OPT_SHOW_DEFAULT_DOMAIN_SUMMARY,
10129 OPT_MMIO_ENUMERATION_SKIP_VGA,
10130 OPT_SAMPLE_SIZE,
10131 OPT_SAMPLE_MAX,
10132 OPT_REPORT_PCPU,
10133 /* Guest info */
10134 OPT_DEFAULT_GUEST_PAGING_LEVELS,
10135 OPT_SYMBOL_FILE,
10136 /* Hardware info */
10137 OPT_SVM_MODE,
10138 OPT_CPU_HZ,
10139 /* Misc */
10140 OPT_PROGRESS,
10141 OPT_TOLERANCE,
10142 OPT_TSC_LOOP_FATAL,
10143 /* Specific letters */
10144 OPT_DUMP_ALL='a',
10145 OPT_INTERVAL_LENGTH='i',
10146 OPT_SUMMARY='s',
10147 };
10148
10149 enum {
10150 OPT_GROUP_SUMMARY=1,
10151 OPT_GROUP_DUMP,
10152 OPT_GROUP_INTERVAL,
10153 OPT_GROUP_EXTRA,
10154 OPT_GROUP_GUEST,
10155 OPT_GROUP_HARDWARE
10156 };
10157
10158 #define xstr(x) str(x)
10159 #define str(x) #x
10160
10161 #define GHZ 1000000000LL
10162 #define MHZ 1000000LL
10163 #define KHZ 1000LL
10164
10165 void parse_cpu_hz(char * arg) {
10166 float hz_base;
10167 char * next_ptr;
10168
10169 hz_base=strtof(arg, &next_ptr);
10170 if(next_ptr == arg) {
10171 fprintf(stderr, "Invalid cpu_hz %s\n", arg);
10172 exit(1);
10173 }
10174 switch(*next_ptr) {
10175 case '\0':
10176 opt.cpu_hz=(long long)hz_base;
10177 break;
10178 case 'G':
10179 opt.cpu_hz= hz_base * GHZ;
10180 break;
10181 case 'M':
10182 opt.cpu_hz=hz_base * MHZ;
10183 break;
10184 case 'K':
10185 opt.cpu_hz=hz_base * KHZ;
10186 break;
10187 default:
10188 fprintf(stderr, "Unknown suffix %c\n", *next_ptr);
10189 exit(1);
10190 }
10191 /* Just a convenient pre-calculation */
10192 opt.cpu_qhz = QHZ_FROM_HZ(opt.cpu_hz);
10193 }
10194
10195 /* WARNING not thread-safe */
10196 char * stringify_cpu_hz(long long cpu_hz) {
10197 static char cpu_string[20], suffix;
10198 float hz;
10199
10200 if(cpu_hz > GHZ) {
10201 hz = (float)cpu_hz / GHZ;
10202 suffix = 'G';
10203 } else if(cpu_hz > MHZ) {
10204 hz = (float)cpu_hz / MHZ;
10205 suffix = 'M';
10206 } else if(cpu_hz > KHZ) {
10207 hz = (float)cpu_hz / KHZ;
10208 suffix = 'k';
10209 } else {
10210 hz = cpu_hz;
10211 suffix = ' ';
10212 }
10213
10214 snprintf(cpu_string, 20, "%1.2lf %cHz", hz, suffix);
10215
10216 return cpu_string;
10217 }
10218
10219 int parse_array(char *arg, struct array_struct *a) {
10220 char *p, *q;
10221 int n=1, i;
10222
10223 /* Count the number of commas (and thus the number of elements) */
10224 for(p=arg; *p; p++)
10225 if(*p == ',')
10226 n++;
10227
10228 fprintf(warn, "%s: Found %d elements\n", __func__, n);
10229 fflush(warn);
10230 a->count = n;
10231 a->values = malloc(n * sizeof(unsigned long long));
10232
10233 if(!a->values) {
10234 fprintf(stderr, "Malloc failed!\n");
10235 error(ERR_SYSTEM, NULL);
10236 }
10237
10238 /* Now parse the elements */
10239 p = q = arg;
10240 for(i=0; i<n; i++) {
10241 a->values[i] = strtoull(p, &q, 0);
10242 if(p == q) {
10243 fprintf(stderr, "Bad format: %s\n", q);
10244 return -1;
10245 }
10246 fprintf(warn, "%s: Found element 0x%llx (%lld)\n",
10247 __func__, a->values[i],
10248 a->values[i]);
10249 fflush(warn);
10250 if(*q == ',')
10251 q++;
10252 else if(*q != '\0') {
10253 fprintf(stderr, "Bad format: %s\n", q);
10254 return -1;
10255 }
10256 p=q;
10257 }
10258
10259 return n;
10260 }
10261
10262 error_t cmd_parser(int key, char *arg, struct argp_state *state)
10263 {
10264 switch (key)
10265 {
10266 /* Dump group */
10267 case OPT_DUMP_ALL:
10268 opt.dump_all = 1;
10269 G.output_defined = 1;
10270 break;
10271 case OPT_DUMP_RAW_READS:
10272 opt.dump_raw_reads = 1;
10273 G.output_defined = 1;
10274 break;
10275 case OPT_DUMP_NO_PROCESSING:
10276 opt.dump_no_processing = 1;
10277 opt.dump_raw_reads = 1;
10278 G.output_defined = 1;
10279 break;
10280 case OPT_DUMP_RAW_PROCESS:
10281 opt.dump_raw_process = 1;
10282 G.output_defined = 1;
10283 break;
10284 case OPT_DUMP_IPI_LATENCY:
10285 opt.dump_ipi_latency = 1;
10286 break;
10287 case OPT_DUMP_TRACE_VOLUME_ON_LOST_RECORD:
10288 opt.dump_trace_volume_on_lost_record = 1;
10289 break;
10290 case OPT_DUMP_SHOW_POWER_STATES:
10291 opt.dump_show_power_states = 1;
10292 break;
10293 /* Extra group */
10294 case OPT_WITH_CR3_ENUMERATION:
10295 opt.with_cr3_enumeration=1;
10296 break;
10297 case OPT_WITH_PIO_ENUMERATION:
10298 opt.with_pio_enumeration=1;
10299 break;
10300 case OPT_WITH_MMIO_ENUMERATION:
10301 opt.with_mmio_enumeration=1;
10302 break;
10303 case OPT_SHOW_DEFAULT_DOMAIN_SUMMARY:
10304 opt.show_default_domain_summary=1;
10305 break;
10306 case OPT_SAMPLE_SIZE:
10307 {
10308 char * inval;
10309 opt.sample_size = (int)strtol(arg, &inval, 0);
10310 if( inval == arg )
10311 argp_usage(state);
10312 break;
10313 }
10314 case OPT_SAMPLE_MAX:
10315 {
10316 char * inval;
10317 opt.sample_max = (int)strtol(arg, &inval, 0);
10318 if( inval == arg )
10319 argp_usage(state);
10320 break;
10321 }
10322 case OPT_MMIO_ENUMERATION_SKIP_VGA:
10323 {
10324 char * inval;
10325 opt.mmio_enumeration_skip_vga = (int)strtol(arg, &inval, 0);
10326 if( inval == arg )
10327 argp_usage(state);
10328 break;
10329 }
10330 case OPT_SCATTERPLOT_INTERRUPT_EIP:
10331 {
10332 char * inval;
10333 G.output_defined = 1;
10334 opt.scatterplot_interrupt_eip=1;
10335 opt.scatterplot_interrupt_vector = (int)strtol(arg, &inval, 0);
10336 if( inval == arg )
10337 argp_usage(state);
10338 }
10339 break;
10340 case OPT_WITH_INTERRUPT_EIP_ENUMERATION:
10341 {
10342 char * inval;
10343 opt.with_interrupt_eip_enumeration=1;
10344 opt.interrupt_eip_enumeration_vector = (int)strtol(arg, &inval, 0);
10345 if( inval == arg )
10346 argp_usage(state);
10347 }
10348 break;
10349 case OPT_SCATTERPLOT_UNPIN_PROMOTE:
10350 G.output_defined = 1;
10351 opt.scatterplot_unpin_promote=1;
10352 break;
10353 case OPT_SCATTERPLOT_CR3_SWITCH:
10354 G.output_defined = 1;
10355 opt.scatterplot_cr3_switch=1;
10356 break;
10357 case OPT_SCATTERPLOT_WAKE_TO_HALT:
10358 G.output_defined = 1;
10359 opt.scatterplot_wake_to_halt=1;
10360 break;
10361 case OPT_SCATTERPLOT_VMEXIT_EIP:
10362 G.output_defined = 1;
10363 opt.scatterplot_vmexit_eip=1;
10364 break;
10365 case OPT_SCATTERPLOT_EXTINT_CYCLES:
10366 {
10367 char * inval;
10368 G.output_defined = 1;
10369 opt.scatterplot_extint_cycles=1;
10370 opt.scatterplot_extint_cycles_vector = (int)strtol(arg, &inval, 0);
10371 if( inval == arg )
10372 argp_usage(state);
10373 }
10374 break;
10375 case OPT_SCATTERPLOT_RDTSC:
10376 G.output_defined = 1;
10377 opt.scatterplot_rdtsc=1;
10378 break;
10379 case OPT_SCATTERPLOT_IRQ:
10380 G.output_defined = 1;
10381 opt.scatterplot_irq=1;
10382 break;
10383 case OPT_SCATTERPLOT_IO:
10384 {
10385 char * inval;
10386 G.output_defined = 1;
10387 opt.scatterplot_io=1;
10388 opt.scatterplot_io_port = (int)strtol(arg, &inval, 0);
10389 if( inval == arg )
10390 argp_usage(state);
10391 }
10392 break;
10393 case OPT_SCATTERPLOT_RUNSTATE:
10394 G.output_defined = 1;
10395 opt.scatterplot_runstate=1;
10396 break;
10397 case OPT_SCATTERPLOT_RUNSTATE_TIME:
10398 G.output_defined = 1;
10399 opt.scatterplot_runstate_time=1;
10400 break;
10401 case OPT_SCATTERPLOT_PCPU:
10402 G.output_defined = 1;
10403 opt.scatterplot_pcpu=1;
10404 break;
10405 case OPT_HISTOGRAM_INTERRUPT_EIP:
10406 {
10407 char * inval, *p;
10408
10409 opt.histogram_interrupt_eip=1;
10410 opt.histogram_interrupt_vector = (int)strtol(arg, &inval, 0);
10411
10412 if( inval == arg )
10413 argp_usage(state);
10414
10415 p = inval;
10416
10417 if(*p == ',')
10418 opt.histogram_interrupt_increment = (unsigned long long)strtoull(p+1, &inval, 0);
10419 else
10420 opt.histogram_interrupt_increment = 0x1000000;
10421
10422 printf("Making histogram of eips at interrupt %d, increment %llx\n",
10423 opt.histogram_interrupt_vector,
10424 opt.histogram_interrupt_increment);
10425 }
10426 break;
10427
10428 case OPT_INTERVAL_LENGTH:
10429 {
10430 char * inval;
10431
10432 opt.interval.msec = (unsigned) (strtof(arg, &inval) * 1000);
10433
10434 if ( inval == arg )
10435 argp_usage(state);
10436
10437 break;
10438 }
10439
10440 case OPT_INTERVAL_CR3_SCHEDULE_TIME:
10441 {
10442 if(parse_array(arg, &opt.interval.array) < 0)
10443 goto usage;
10444 interval_table_alloc(opt.interval.array.count);
10445 opt.interval.output = INTERVAL_CR3_SCHEDULE_TIME;
10446 opt.interval.check = INTERVAL_CHECK_CR3;
10447 opt.interval.mode = INTERVAL_MODE_ARRAY;
10448 opt.interval_mode = 1;
10449 opt.summary_info = 1;
10450 opt.with_cr3_enumeration = 1;
10451 G.output_defined = 1;
10452 break;
10453 usage:
10454 fprintf(stderr, "Invalid input for cr3_schedule_time\n");
10455 argp_usage(state);
10456 break;
10457 }
10458
10459 case OPT_INTERVAL_CR3_SCHEDULE_TIME_ALL:
10460 opt.interval.output = INTERVAL_CR3_SCHEDULE_TIME;
10461 opt.interval.check = INTERVAL_CHECK_CR3;
10462 opt.interval.mode = INTERVAL_MODE_LIST;
10463 opt.interval_mode = 1;
10464 opt.summary_info = 1;
10465 opt.with_cr3_enumeration = 1;
10466 G.output_defined = 1;
10467 break;
10468
10469 case OPT_INTERVAL_CR3_SCHEDULE_ORDERED:
10470 opt.interval.output = INTERVAL_CR3_SCHEDULE_ORDERED;
10471 opt.interval.check = INTERVAL_CHECK_CR3;
10472 opt.interval_mode = 1;
10473 opt.summary_info = 1;
10474 opt.with_cr3_enumeration = 1;
10475 G.output_defined = 1;
10476 break;
10477
10478 case OPT_INTERVAL_CR3_SHORT_SUMMARY:
10479 {
10480 if(parse_array(arg, &opt.interval.array) < 0
10481 || opt.interval.array.count != 1)
10482 goto usage;
10483 opt.interval.output = INTERVAL_CR3_SHORT_SUMMARY;
10484 opt.interval.check = INTERVAL_CHECK_CR3;
10485 opt.interval_mode = 1;
10486 opt.summary_info = 1;
10487 opt.with_cr3_enumeration = 1;
10488 G.output_defined = 1;
10489 break;
10490 }
10491
10492 case OPT_INTERVAL_DOMAIN_TOTAL_TIME:
10493 {
10494 if(parse_array(arg, &opt.interval.array) < 0)
10495 goto idtt_usage;
10496 interval_table_alloc(opt.interval.array.count);
10497 opt.interval.output = INTERVAL_DOMAIN_TOTAL_TIME;
10498 opt.interval.check = INTERVAL_CHECK_DOMAIN;
10499 opt.interval.mode = INTERVAL_MODE_ARRAY;
10500 opt.interval_mode = 1;
10501 opt.summary_info = 1;
10502 G.output_defined = 1;
10503 break;
10504 idtt_usage:
10505 fprintf(stderr, "Invalid input for domain_total_time\n");
10506 argp_usage(state);
10507 break;
10508 }
10509
10510 case OPT_INTERVAL_DOMAIN_TOTAL_TIME_ALL:
10511 opt.interval.output = INTERVAL_DOMAIN_TOTAL_TIME;
10512 opt.interval.check = INTERVAL_CHECK_DOMAIN;
10513 opt.interval.mode = INTERVAL_MODE_LIST;
10514 opt.interval_mode = 1;
10515 opt.summary_info = 1;
10516 G.output_defined = 1;
10517 break;
10518
10519 case OPT_INTERVAL_DOMAIN_SHORT_SUMMARY:
10520 {
10521 if((parse_array(arg, &opt.interval.array) < 0)
10522 || opt.interval.array.count != 1)
10523 argp_usage(state);
10524
10525 opt.interval.output = INTERVAL_DOMAIN_SHORT_SUMMARY;
10526 opt.interval.check = INTERVAL_CHECK_DOMAIN;
10527 opt.interval_mode = 1;
10528 opt.summary_info = 1;
10529 G.output_defined = 1;
10530 break;
10531 }
10532
10533 case OPT_INTERVAL_DOMAIN_GUEST_INTERRUPT:
10534 {
10535 if((parse_array(arg, &opt.interval.array) < 0)
10536 || opt.interval.array.count != 1)
10537 argp_usage(state);
10538
10539 opt.interval.output = INTERVAL_DOMAIN_GUEST_INTERRUPT;
10540 opt.interval.check = INTERVAL_CHECK_DOMAIN;
10541 opt.interval_mode = 1;
10542 opt.summary_info = 1;
10543 G.output_defined = 1;
10544 break;
10545 }
10546
10547 case OPT_INTERVAL_DOMAIN_GRANT_MAPS:
10548 {
10549 if((parse_array(arg, &opt.interval.array) < 0)
10550 || opt.interval.array.count != 1)
10551 argp_usage(state);
10552
10553 opt.interval.output = INTERVAL_DOMAIN_GRANT_MAPS;
10554 opt.interval.check = INTERVAL_CHECK_DOMAIN;
10555 opt.interval_mode = 1;
10556 opt.summary_info = 1;
10557 G.output_defined = 1;
10558 break;
10559 }
10560
10561 /* Summary group */
10562 case OPT_SUMMARY:
10563 opt.summary = 1;
10564 opt.summary_info = 1;
10565 G.output_defined = 1;
10566 break;
10567 case OPT_REPORT_PCPU:
10568 opt.report_pcpu = 1;
10569 //opt.summary_info = 1;
10570 G.output_defined = 1;
10571 break;
10572 /* Guest info group */
10573 case OPT_DEFAULT_GUEST_PAGING_LEVELS:
10574 {
10575 char *inval;
10576 opt.default_guest_paging_levels = (int)strtol(arg, &inval, 0);
10577 if ( inval == arg )
10578 argp_usage(state);
10579 }
10580 break;
10581 case OPT_SYMBOL_FILE:
10582 /* FIXME - strcpy */
10583 G.symbol_file = arg;
10584 break;
10585 /* Hardware info group */
10586 case OPT_SVM_MODE:
10587 opt.svm_mode = 1;
10588 break;
10589 case OPT_CPU_HZ:
10590 parse_cpu_hz(arg);
10591 break;
10592 break;
10593
10594 case OPT_TOLERANCE:
10595 {
10596 char * inval;
10597
10598 opt.tolerance = (int)strtol(arg, &inval, 0);
10599
10600 if( inval == arg )
10601 argp_usage(state);
10602
10603 if ( opt.tolerance > ERR_MAX_TOLERABLE )
10604 {
10605 fprintf(stderr, "ERROR: Max tolerable error %d\n",
10606 ERR_MAX_TOLERABLE);
10607 exit(1);
10608 }
10609
10610 printf("Tolerating errors at or below %d\n",
10611 opt.tolerance);
10612 }
10613 break;
10614
10615 case OPT_PROGRESS:
10616 opt.progress = 1;
10617 break;
10618
10619 case OPT_TSC_LOOP_FATAL:
10620 opt.tsc_loop_fatal = 1;
10621 break;
10622
10623 case ARGP_KEY_ARG:
10624 {
10625 /* FIXME - strcpy */
10626 if (state->arg_num == 0)
10627 G.trace_file = arg;
10628 else
10629 argp_usage(state);
10630 }
10631 break;
10632 case ARGP_KEY_END:
10633 {
10634 if(opt.interval_mode) {
10635 opt.interval.cycles = ( opt.interval.msec * opt.cpu_hz ) / 1000 ;
10636 interval_header();
10637 }
10638
10639 if(!G.output_defined)
10640 {
10641 fprintf(stderr, "No output defined, using summary.\n");
10642 opt.summary = 1;
10643 opt.summary_info = 1;
10644 }
10645 fprintf(stderr, "Using %s hardware-assisted virtualization.\n",
10646 opt.svm_mode?"SVM":"VMX");
10647 }
10648 break;
10649
10650 default:
10651 return ARGP_ERR_UNKNOWN;
10652 }
10653
10654 return 0;
10655 }
10656
10657 const struct argp_option cmd_opts[] = {
10658 /* Dump group */
10659 { .name = "dump-all",
10660 .key = OPT_DUMP_ALL,
10661 .group = OPT_GROUP_DUMP,
10662 .doc = "Dump all records as they come in.", },
10663
10664 { .name = "dump-raw-reads",
10665 .key = OPT_DUMP_RAW_READS,
10666 .group = OPT_GROUP_DUMP,
10667 .doc = "Dump raw data as it's read from disk. Useful mainly for debugging the analysis tool.", },
10668
10669 { .name = "dump-no-processing",
10670 .key = OPT_DUMP_NO_PROCESSING,
10671 .group = OPT_GROUP_DUMP,
10672 .doc = "Don't do any processing on records other than cpu changes. Implies dump-raw-reads (or you wouldn't get anything).", },
10673
10674 { .name = "dump-raw-process",
10675 .key = OPT_DUMP_RAW_PROCESS,
10676 .group = OPT_GROUP_DUMP,
10677 .doc = "Dump raw data as it's processed. Useful mainly for debugging the analysis tool.", },
10678
10679 { .name = "dump-ipi-latency",
10680 .key = OPT_DUMP_IPI_LATENCY,
10681 .group = OPT_GROUP_DUMP,
10682 .doc = "Dump IPI latency info as IPIs are delivered (vector 0xd1 only).", },
10683
10684 { .name = "dump-trace-volume-on-lost-record",
10685 .key = OPT_DUMP_TRACE_VOLUME_ON_LOST_RECORD,
10686 .group = OPT_GROUP_DUMP,
10687 .doc = "Dump the volume of trace types in the previous cpu buffer when a lost record is created.", },
10688
10689 { .name = "dump-show-power-states",
10690 .key = OPT_DUMP_SHOW_POWER_STATES,
10691 .group = OPT_GROUP_DUMP,
10692 .doc = "Show the power-state of the physical cpu when dumping output.", },
10693
10694 /* Extra processing group */
10695 { .name = "with-cr3-enumeration",
10696 .key = OPT_WITH_CR3_ENUMERATION,
10697 .group = OPT_GROUP_EXTRA,
10698 .doc = "Keep track of per-cr3 values", },
10699
10700 { .name = "with-pio-enumeration",
10701 .key = OPT_WITH_PIO_ENUMERATION,
10702 .group = OPT_GROUP_EXTRA,
10703 .doc = "Report summary info on indiviaul IO addresses", },
10704
10705 { .name = "with-mmio-enumeration",
10706 .key = OPT_WITH_MMIO_ENUMERATION,
10707 .group = OPT_GROUP_EXTRA,
10708 .doc = "Report summary info on indiviaul MMIO addresses.", },
10709
10710 { .name = "with-interrupt-eip-enumeration",
10711 .key = OPT_WITH_INTERRUPT_EIP_ENUMERATION,
10712 .arg = "vector",
10713 .group = OPT_GROUP_EXTRA,
10714 .doc = "Report a summary on eips interrupted by specified vector.", },
10715
10716 { .name = "scatterplot-interrupt-eip",
10717 .key = OPT_SCATTERPLOT_INTERRUPT_EIP,
10718 .arg = "vector",
10719 .group = OPT_GROUP_EXTRA,
10720 .doc = "Output scatterplot of eips as a function of time.", },
10721
10722 { .name = "scatterplot-extint-cycles",
10723 .key = OPT_SCATTERPLOT_EXTINT_CYCLES,
10724 .arg = "vector",
10725 .group = OPT_GROUP_EXTRA,
10726 .doc = "Output a scatterplot of vmexit cycles for external interrupts of the given vector as a function of time.", },
10727
10728 { .name = "scatterplot-unpin-promote",
10729 .key = OPT_SCATTERPLOT_UNPIN_PROMOTE,
10730 .group = OPT_GROUP_EXTRA,
10731 .doc = "Output scatterplot of unpins and promotions. If " \
10732 "--with-cr3-enumeration is included, promotions include current cr3.", },
10733
10734 { .name = "scatterplot-cr3-switch",
10735 .key = OPT_SCATTERPLOT_CR3_SWITCH,
10736 .group = OPT_GROUP_EXTRA,
10737 .doc = "Output scatterplot of cr3 switches.", },
10738
10739 { .name = "scatterplot-wake-to-halt",
10740 .key = OPT_SCATTERPLOT_WAKE_TO_HALT,
10741 .group = OPT_GROUP_EXTRA,
10742 .doc = "Output scatterplot of wake-to-halt.", },
10743
10744 { .name = "scatterplot-vmexit-eip",
10745 .key = OPT_SCATTERPLOT_VMEXIT_EIP,
10746 .group = OPT_GROUP_EXTRA,
10747 .doc = "Output scatterplot of vmexit eips.", },
10748
10749 { .name = "scatterplot-io",
10750 .key = OPT_SCATTERPLOT_IO,
10751 .arg = "port",
10752 .group = OPT_GROUP_EXTRA,
10753 .doc = "Output scatterplot of io latencies for givein address as a function of time.", },
10754
10755 { .name = "scatterplot-runstate",
10756 .key = OPT_SCATTERPLOT_RUNSTATE,
10757 .group = OPT_GROUP_EXTRA,
10758 .doc = "Output scatterplot of runstate.", },
10759
10760 { .name = "scatterplot-runstate-time",
10761 .key = OPT_SCATTERPLOT_RUNSTATE_TIME,
10762 .group = OPT_GROUP_EXTRA,
10763 .doc = "Output scatterplot of time in a runstate.", },
10764
10765 { .name = "scatterplot-pcpu",
10766 .key = OPT_SCATTERPLOT_PCPU,
10767 .group = OPT_GROUP_EXTRA,
10768 .doc = "Output scatterplot of which pcpu vcpus are run on.", },
10769
10770 { .name = "scatterplot-rdtsc",
10771 .key = OPT_SCATTERPLOT_RDTSC,
10772 .group = OPT_GROUP_EXTRA,
10773 .doc = "Output scatterplot of rdtsc values.", },
10774
10775 { .name = "scatterplot-irq",
10776 .key = OPT_SCATTERPLOT_IRQ,
10777 .group = OPT_GROUP_EXTRA,
10778 .doc = "Output scatterplot of irqs on pcpus.", },
10779
10780 { .name = "histogram-interrupt-eip",
10781 .key = OPT_HISTOGRAM_INTERRUPT_EIP,
10782 .arg = "vector[,increment]",
10783 .group = OPT_GROUP_EXTRA,
10784 .doc = "Output histograms of eips.", },
10785
10786 { .name = "interval",
10787 .key = OPT_INTERVAL_LENGTH,
10788 .arg = "sec",
10789 .group = OPT_GROUP_INTERVAL,
10790 .doc = "Interval length to do time-based graphs, in seconds", },
10791
10792 { .name = "interval-cr3-schedule-time",
10793 .key = OPT_INTERVAL_CR3_SCHEDULE_TIME,
10794 .arg = "gmfn[,gmfn...]",
10795 .group = OPT_GROUP_INTERVAL,
10796 .doc = "Print a csv with the listed cr3 value(s) every interval.", },
10797
10798 { .name = "interval-cr3-schedule-time-all",
10799 .key = OPT_INTERVAL_CR3_SCHEDULE_TIME_ALL,
10800 .group = OPT_GROUP_INTERVAL,
10801 .doc = "Print a csv with all cr3 values every interval.", },
10802
10803 { .name = "interval-cr3-schedule-ordered",
10804 .key = OPT_INTERVAL_CR3_SCHEDULE_ORDERED,
10805 .group = OPT_GROUP_INTERVAL,
10806 .doc = "Print summary with the top 10 cr3 values every interval.", },
10807
10808 { .name = "interval-cr3-short-summary",
10809 .key = OPT_INTERVAL_CR3_SHORT_SUMMARY,
10810 .arg = "gmfn",
10811 .group = OPT_GROUP_INTERVAL,
10812 .doc = "Print a csv with the hvm short summary of cr3 value every interval.", },
10813
10814 { .name = "interval-domain-total-time",
10815 .key = OPT_INTERVAL_DOMAIN_TOTAL_TIME,
10816 .arg = "domain[,domain...]",
10817 .group = OPT_GROUP_INTERVAL,
10818 .doc = "Print a csv with the listed domain(s) total runtime every interval.", },
10819
10820 { .name = "interval-domain-total-time-all",
10821 .key = OPT_INTERVAL_DOMAIN_TOTAL_TIME_ALL,
10822 .group = OPT_GROUP_INTERVAL,
10823 .doc = "Print a csv with all domains every interval.", },
10824
10825 { .name = "interval-domain-short-summary",
10826 .key = OPT_INTERVAL_DOMAIN_SHORT_SUMMARY,
10827 .arg = "domain-id",
10828 .group = OPT_GROUP_INTERVAL,
10829 .doc = "Print a csv with the hvm short summary of given domain every interval.", },
10830
10831 { .name = "interval-domain-guest-interrupt",
10832 .key = OPT_INTERVAL_DOMAIN_GUEST_INTERRUPT,
10833 .arg = "domain-id",
10834 .group = OPT_GROUP_INTERVAL,
10835 .doc = "Print a csv with the guest interrupt count of given domain every interval.", },
10836
10837 { .name = "interval-domain-grant-maps",
10838 .key = OPT_INTERVAL_DOMAIN_GRANT_MAPS,
10839 .arg = "domain-id",
10840 .group = OPT_GROUP_INTERVAL,
10841 .doc = "Print a csv with the grant maps done on behalf of a given domain every interval.", },
10842
10843 /* Summary group */
10844 { .name = "show-default-domain-summary",
10845 .key = OPT_SHOW_DEFAULT_DOMAIN_SUMMARY,
10846 .group = OPT_GROUP_SUMMARY,
10847 .doc = "Show default domain information on summary", },
10848
10849 { .name = "mmio-enumeration-skip-vga",
10850 .key = OPT_MMIO_ENUMERATION_SKIP_VGA,
10851 .arg = "[0|1]",
10852 .group = OPT_GROUP_SUMMARY,
10853 .doc = "Control whether we enumerate MMIO accesses to the VGA area, which can be extremely high during boot. Default: 0", },
10854
10855 { .name = "sample-size",
10856 .key = OPT_SAMPLE_SIZE,
10857 .arg = "size",
10858 .group = OPT_GROUP_SUMMARY,
10859 .doc = "Start with [size] samples for percentile purposes. Enter 0 to" \
10860 "disable. Default 1024.", },
10861
10862 { .name = "sample-max",
10863 .key = OPT_SAMPLE_MAX,
10864 .arg = "size",
10865 .group = OPT_GROUP_SUMMARY,
10866 .doc = "Do not allow sample to grow beyond [size] samples for percentile"\
10867 " purposes. Enter 0 for no limit.", },
10868
10869 { .name = "summary",
10870 .key = OPT_SUMMARY,
10871 .group = OPT_GROUP_SUMMARY,
10872 .doc = "Output a summary", },
10873
10874 { .name = "report-pcpu",
10875 .key = OPT_REPORT_PCPU,
10876 .group = OPT_GROUP_SUMMARY,
10877 .doc = "Report utilization for pcpus", },
10878
10879 /* Guest info */
10880 { .name = "default-guest-paging-levels",
10881 .key = OPT_DEFAULT_GUEST_PAGING_LEVELS,
10882 .group = OPT_GROUP_GUEST,
10883 .arg = "L",
10884 .doc = "Default guest paging levels. Mainly necessary for Rio, as Miami traces include guest paging levels where appropriate.", },
10885
10886 { .name = "symbol-file",
10887 .key = OPT_SYMBOL_FILE,
10888 .group = OPT_GROUP_GUEST,
10889 .arg = "filename",
10890 .doc = "A symbol file for interpreting guest eips.", },
10891
10892 /* Hardware info */
10893 { .name = "cpu-hz",
10894 .key = OPT_CPU_HZ,
10895 .group = OPT_GROUP_HARDWARE,
10896 .arg = "HZ",
10897 .doc = "Cpu speed of the tracing host, used to convert tsc into seconds.", },
10898
10899 { .name = "progress",
10900 .key = OPT_PROGRESS,
10901 .doc = "Progress dialog. Requires the zenity (GTK+) executable.", },
10902
10903 { .name = "tsc-loop-fatal",
10904 .key = OPT_TSC_LOOP_FATAL,
10905 .doc = "Stop processing and exit if tsc skew tracking detects a dependency loop.", },
10906
10907 { .name = "tolerance",
10908 .key = OPT_TOLERANCE,
10909 .arg = "errlevel",
10910 .doc = "Sets tolerance for errors found in the file. Default is 3; max is 6.", },
10911
10912
10913 { 0 },
10914 };
10915
10916 const struct argp parser_def = {
10917 .options = cmd_opts,
10918 .parser = cmd_parser,
10919 .args_doc = "[trace file]",
10920 .doc = "",
10921 };
10922
10923 int main(int argc, char *argv[]) {
10924 /* Start with warn at stderr. */
10925 warn = stderr;
10926
10927 argp_parse(&parser_def, argc, argv, 0, NULL, NULL);
10928
10929 if (G.trace_file == NULL)
10930 exit(1);
10931
10932 if ( (G.fd = open(G.trace_file, O_RDONLY)) < 0) {
10933 perror("open");
10934 error(ERR_SYSTEM, NULL);
10935 } else {
10936 struct stat s;
10937 fstat(G.fd, &s);
10938 G.file_size = s.st_size;
10939 }
10940
10941 if ( (G.mh = mread_init(G.fd)) == NULL )
10942 perror("mread");
10943
10944 if (G.symbol_file != NULL)
10945 parse_symbol_file(G.symbol_file);
10946
10947 if(opt.dump_all)
10948 warn = stdout;
10949
10950 init_pcpus();
10951
10952 if(opt.progress)
10953 progress_init();
10954
10955 process_records();
10956
10957 if(opt.interval_mode)
10958 interval_tail();
10959
10960 if(opt.summary)
10961 summary();
10962
10963 if(opt.report_pcpu)
10964 report_pcpu();
10965
10966 if(opt.progress)
10967 progress_finish();
10968
10969 return 0;
10970 }
10971 /*
10972 * Local variables:
10973 * mode: C
10974 * c-set-style: "BSD"
10975 * c-basic-offset: 4
10976 * tab-width: 4
10977 * indent-tabs-mode: nil
10978 * End:
10979 */