xref: /openbmc/linux/arch/x86/kvm/debugfs.c (revision 96ac6d43)
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  *
4  * Copyright 2016 Red Hat, Inc. and/or its affiliates.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.  See
7  * the COPYING file in the top-level directory.
8  *
9  */
10 #include <linux/kvm_host.h>
11 #include <linux/debugfs.h>
12 #include "lapic.h"
13 
14 bool kvm_arch_has_vcpu_debugfs(void)
15 {
16 	return true;
17 }
18 
19 static int vcpu_get_timer_advance_ns(void *data, u64 *val)
20 {
21 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
22 	*val = vcpu->arch.apic->lapic_timer.timer_advance_ns;
23 	return 0;
24 }
25 
26 DEFINE_SIMPLE_ATTRIBUTE(vcpu_timer_advance_ns_fops, vcpu_get_timer_advance_ns, NULL, "%llu\n");
27 
28 static int vcpu_get_tsc_offset(void *data, u64 *val)
29 {
30 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
31 	*val = vcpu->arch.tsc_offset;
32 	return 0;
33 }
34 
35 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_offset_fops, vcpu_get_tsc_offset, NULL, "%lld\n");
36 
37 static int vcpu_get_tsc_scaling_ratio(void *data, u64 *val)
38 {
39 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
40 	*val = vcpu->arch.tsc_scaling_ratio;
41 	return 0;
42 }
43 
44 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops, vcpu_get_tsc_scaling_ratio, NULL, "%llu\n");
45 
46 static int vcpu_get_tsc_scaling_frac_bits(void *data, u64 *val)
47 {
48 	*val = kvm_tsc_scaling_ratio_frac_bits;
49 	return 0;
50 }
51 
52 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_frac_fops, vcpu_get_tsc_scaling_frac_bits, NULL, "%llu\n");
53 
54 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
55 {
56 	struct dentry *ret;
57 
58 	ret = debugfs_create_file("tsc-offset", 0444,
59 							vcpu->debugfs_dentry,
60 							vcpu, &vcpu_tsc_offset_fops);
61 	if (!ret)
62 		return -ENOMEM;
63 
64 	if (lapic_in_kernel(vcpu)) {
65 		ret = debugfs_create_file("lapic_timer_advance_ns", 0444,
66 								vcpu->debugfs_dentry,
67 								vcpu, &vcpu_timer_advance_ns_fops);
68 		if (!ret)
69 			return -ENOMEM;
70 	}
71 
72 	if (kvm_has_tsc_control) {
73 		ret = debugfs_create_file("tsc-scaling-ratio", 0444,
74 							vcpu->debugfs_dentry,
75 							vcpu, &vcpu_tsc_scaling_fops);
76 		if (!ret)
77 			return -ENOMEM;
78 		ret = debugfs_create_file("tsc-scaling-ratio-frac-bits", 0444,
79 							vcpu->debugfs_dentry,
80 							vcpu, &vcpu_tsc_scaling_frac_fops);
81 		if (!ret)
82 			return -ENOMEM;
83 
84 	}
85 
86 	return 0;
87 }
88