-
Notifications
You must be signed in to change notification settings - Fork 4
/
linux-v6.5-rc2-d192f5382.patch
418 lines (389 loc) · 20 KB
/
linux-v6.5-rc2-d192f5382.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
From 0f5f72608fbc40b24735194c08363e78552e3565 Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Sat, 22 Jul 2023 21:34:29 -0700
Subject: [PATCH 1/1] linux-v6.5-rc2-d192f5382
Signed-off-by: Dongli Zhang <[email protected]>
---
arch/x86/events/amd/core.c | 21 ++++++
arch/x86/include/asm/kvm_host.h | 17 +++++
arch/x86/include/asm/perf_event.h | 10 +++
arch/x86/kvm/cpuid.c | 24 +++++++
arch/x86/kvm/pmu.h | 8 +++
arch/x86/kvm/reverse_cpuid.h | 7 ++
arch/x86/kvm/svm/pmu.c | 103 ++++++++++++++++++++++++++++++
arch/x86/kvm/svm/svm.c | 7 ++
arch/x86/kvm/x86.c | 7 ++
9 files changed, 204 insertions(+)
diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index abadd5f23425..a601c76e2ea0 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -1440,6 +1440,27 @@ __init int amd_pmu_init(void)
if (boot_cpu_data.x86 < 6)
return -ENODEV;
+ /*
+ * 默认的配置.
+ *
+ * 1233 static __initconst const struct x86_pmu amd_pmu = {
+ * 1234 .name = "AMD",
+ * 1235 .handle_irq = amd_pmu_handle_irq,
+ * 1236 .disable_all = amd_pmu_disable_all,
+ * 1237 .enable_all = amd_pmu_enable_all,
+ * 1238 .enable = amd_pmu_enable_event,
+ * 1239 .disable = amd_pmu_disable_event,
+ * 1240 .hw_config = amd_pmu_hw_config,
+ * 1241 .schedule_events = x86_schedule_events,
+ * 1242 .eventsel = MSR_K7_EVNTSEL0,
+ * 1243 .perfctr = MSR_K7_PERFCTR0,
+ * 1244 .addr_offset = amd_pmu_addr_offset,
+ * 1245 .event_map = amd_pmu_event_map,
+ * 1246 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
+ * 1247 .num_counters = AMD64_NUM_COUNTERS,
+ * 1248 .add = amd_pmu_add_event,
+ * 1249 .del = amd_pmu_del_event,
+ */
x86_pmu = amd_pmu;
ret = amd_core_pmu_init();
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 28bd38303d70..ad80771a7aac 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -514,7 +514,24 @@ struct kvm_pmc {
#define KVM_AMD_PMC_MAX_GENERIC 6
struct kvm_pmu {
u8 version;
+ /*
+ * 在以下修改kvm_pmu->nr_arch_gp_counters:
+ * - arch/x86/kvm/svm/pmu.c|196| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ * - arch/x86/kvm/svm/pmu.c|198| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
+ * - arch/x86/kvm/svm/pmu.c|200| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ * - arch/x86/kvm/svm/pmu.c|203| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(unsigned int , pmu->nr_arch_gp_counters, kvm_pmu_cap.num_counters_gp);
+ * - arch/x86/kvm/vmx/pmu_intel.c|465| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|498| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(int , eax.split.num_counters,
+ */
unsigned nr_arch_gp_counters;
+ /*
+ * 在以下修改kvm_pmu->nr_arch_fixed_counters:
+ * - arch/x86/kvm/svm/pmu.c|216| <<amd_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|466| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|509| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|511| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = min3(ARRAY_SIZE(fixed_pmc_events),
+ * (size_t) edx.split.num_counters_fixed, (size_t)kvm_pmu_cap.num_counters_fixed);
+ */
unsigned nr_arch_fixed_counters;
unsigned available_event_types;
u64 fixed_ctr_ctrl;
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index 85a9fd5a3ec3..3ef5060f5d6b 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -238,6 +238,16 @@ union cpuid_0x80000022_ebx {
struct x86_pmu_capability {
int version;
+ /*
+ * AMD SVM:
+ * 5011 if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
+ * 5012 kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ * 5013 kvm_pmu_cap.num_counters_gp);
+ *
+ * 在以下使用x86_pmu_capability->num_counters_gp:
+ * - arch/x86/kvm/pmu.h|211| <<kvm_init_pmu_capability>> kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp, pmu_ops->MAX_NR_GP_COUNTERS);
+ * - arch/x86/kvm/svm/svm.c|5012| <<svm_set_cpu_caps>> kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ */
int num_counters_gp;
int num_counters_fixed;
int bit_width_gp;
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 7f4d13383cf2..1ba377941488 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -526,6 +526,13 @@ static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
}
+/*
+ * 在以下调用kvm_cpu_cap_init_kvm_defined():
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ */
static __always_inline
void kvm_cpu_cap_init_kvm_defined(enum kvm_only_cpuid_leafs leaf, u32 mask)
{
@@ -1237,6 +1244,13 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
union cpuid_0x80000022_ebx ebx;
entry->ecx = entry->edx = 0;
+ /*
+ * 在以下调用kvm_cpu_cap_init_kvm_defined():
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ */
if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {
entry->eax = entry->ebx;
break;
@@ -1244,6 +1258,16 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
cpuid_entry_override(entry, CPUID_8000_0022_EAX);
+ /*
+ * AMD SVM:
+ * 5011 if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
+ * 5012 kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ * 5013 kvm_pmu_cap.num_counters_gp);
+ *
+ * 在以下设置x86_pmu_capability->num_counters_gp:
+ * - arch/x86/kvm/pmu.h|211| <<kvm_init_pmu_capability>> kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp, pmu_ops->MAX_NR_GP_COUNTERS);
+ * - arch/x86/kvm/svm/svm.c|5012| <<svm_set_cpu_caps>> kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ */
if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 7d9ba301c090..4bfd52f8d11a 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -172,6 +172,10 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
extern struct x86_pmu_capability kvm_pmu_cap;
+/*
+ * called by:
+ * - arch/x86/kvm/x86.c|9497| <<__kvm_x86_vendor_init>> kvm_init_pmu_capability(ops->pmu_ops);
+ */
static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
{
bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
@@ -208,6 +212,10 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
}
kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+ /*
+ * AMD的KVM最多6个
+ * #define KVM_AMD_PMC_MAX_GENERIC 6
+ */
kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
pmu_ops->MAX_NR_GP_COUNTERS);
kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h
index 56cbdb24400a..d1fa36b0443f 100644
--- a/arch/x86/kvm/reverse_cpuid.h
+++ b/arch/x86/kvm/reverse_cpuid.h
@@ -113,6 +113,13 @@ static __always_inline u32 __feature_translate(int x86_feature)
return KVM_X86_FEATURE_SGX_EDECCSSA;
else if (x86_feature == X86_FEATURE_CONSTANT_TSC)
return KVM_X86_FEATURE_CONSTANT_TSC;
+ /*
+ * 在以下调用kvm_cpu_cap_init_kvm_defined():
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ */
else if (x86_feature == X86_FEATURE_PERFMON_V2)
return KVM_X86_FEATURE_PERFMON_V2;
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index cef5a3d0abd0..eec2985c49a1 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -20,6 +20,27 @@
#include "pmu.h"
#include "svm.h"
+/*
+ * 关于pmu enable:
+ *
+ * 1. 根据kernel的module param决定是不是disable了
+ * 2. 根据ioctl决定是不是支持cap (不支持可能是全局disable了)
+ * 3. 如果支持cap, 就disable
+ *
+ * host_cpu_vendor_fms()可获得vendor, 如果和vendor不一致, 直接disable pmu
+ * CPUState vs. first_cpu
+ *
+ * 关于pmu reset
+ *
+ * 1. 查看cpuid是否支持X86_FEATURE_PERFMON_V2.
+ * 如果支持, 根据0x80000022获取寄存器的数目.
+ * 此外, 3个global的也支持!
+ * 寄存器的base address和MSR_F15H_PERF_CTR5的一样(KVM svm其实限制了最多6个)
+ * 2. 如果不是ver=2, 如果支持X86_FEATURE_PERFCTR_CORE,
+ * 就是6个寄存器
+ * 3. 默认只支持4个
+ */
+
enum pmu_type {
PMU_TYPE_COUNTER = 0,
PMU_TYPE_EVNTSEL,
@@ -27,6 +48,15 @@ enum pmu_type {
static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
{
+ /*
+ * 在以下修改kvm_pmu->nr_arch_gp_counters:
+ * - arch/x86/kvm/svm/pmu.c|196| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ * - arch/x86/kvm/svm/pmu.c|198| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
+ * - arch/x86/kvm/svm/pmu.c|200| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ * - arch/x86/kvm/svm/pmu.c|203| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(unsigned int , pmu->nr_arch_gp_counters, kvm_pmu_cap.num_counters_gp);
+ * - arch/x86/kvm/vmx/pmu_intel.c|465| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|498| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(int , eax.split.num_counters,
+ */
unsigned int num_counters = pmu->nr_arch_gp_counters;
if (pmc_idx >= num_counters)
@@ -35,6 +65,12 @@ static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)];
}
+/*
+ * enum pmu_type {
+ * PMU_TYPE_COUNTER = 0,
+ * PMU_TYPE_EVNTSEL,
+ * };
+ */
static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
enum pmu_type type)
{
@@ -84,6 +120,15 @@ static bool amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
idx &= ~(3u << 30);
+ /*
+ * 在以下修改kvm_pmu->nr_arch_gp_counters:
+ * - arch/x86/kvm/svm/pmu.c|196| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ * - arch/x86/kvm/svm/pmu.c|198| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
+ * - arch/x86/kvm/svm/pmu.c|200| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ * - arch/x86/kvm/svm/pmu.c|203| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(unsigned int , pmu->nr_arch_gp_counters, kvm_pmu_cap.num_counters_gp);
+ * - arch/x86/kvm/vmx/pmu_intel.c|465| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|498| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(int , eax.split.num_counters,
+ */
return idx < pmu->nr_arch_gp_counters;
}
@@ -119,6 +164,15 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
return pmu->version > 1;
default:
+ /*
+ * 在以下修改kvm_pmu->nr_arch_gp_counters:
+ * - arch/x86/kvm/svm/pmu.c|196| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ * - arch/x86/kvm/svm/pmu.c|198| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
+ * - arch/x86/kvm/svm/pmu.c|200| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ * - arch/x86/kvm/svm/pmu.c|203| <<amd_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(unsigned int , pmu->nr_arch_gp_counters, kvm_pmu_cap.num_counters_gp);
+ * - arch/x86/kvm/vmx/pmu_intel.c|465| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|498| <<intel_pmu_refresh>> pmu->nr_arch_gp_counters = min_t(int , eax.split.num_counters,
+ */
if (msr > MSR_F15H_PERF_CTR5 &&
msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters)
return pmu->version > 1;
@@ -178,12 +232,32 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return 1;
}
+/*
+ * 1. 查看cpuid是否支持X86_FEATURE_PERFMON_V2.
+ * 如果支持, 根据0x80000022获取寄存器的数目.
+ * 此外, 3个global的也支持!
+ * 寄存器的base address和MSR_F15H_PERF_CTR5的一样(KVM svm其实限制了最多6个)
+ * 2. 如果不是ver=2, 如果支持X86_FEATURE_PERFCTR_CORE,
+ * 就是6个寄存器
+ * 3. 默认只支持4个
+ *
+ * struct kvm_pmu_ops amd_pmu_ops.refresh = amd_pmu_refresh()
+ */
static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
union cpuid_0x80000022_ebx ebx;
pmu->version = 1;
+ /*
+ * 在以下调用kvm_cpu_cap_init_kvm_defined():
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ *
+ * arch/x86/kernel/cpu/scattered.c|50| <<global>> { X86_FEATURE_PERFMON_V2, CPUID_EAX, 0, 0x80000022, 0 },
+ */
if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
pmu->version = 2;
/*
@@ -192,6 +266,9 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
*/
BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
+ /*
+ * 在__do_cpuid_func()查看
+ */
ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
@@ -200,6 +277,16 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
}
+ /*
+ * AMD SVM:
+ * 5011 if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
+ * 5012 kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ * 5013 kvm_pmu_cap.num_counters_gp);
+ *
+ * 在以下使用x86_pmu_capability->num_counters_gp:
+ * - arch/x86/kvm/pmu.h|211| <<kvm_init_pmu_capability>> kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
+ * - arch/x86/kvm/svm/svm.c|5012| <<svm_set_cpu_caps>> kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
+ */
pmu->nr_arch_gp_counters = min_t(unsigned int, pmu->nr_arch_gp_counters,
kvm_pmu_cap.num_counters_gp);
@@ -213,6 +300,14 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
/* not applicable to AMD; but clean them to prevent any fall out */
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
+ /*
+ * 在以下修改kvm_pmu->nr_arch_fixed_counters:
+ * - arch/x86/kvm/svm/pmu.c|216| <<amd_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|466| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|509| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = 0;
+ * - arch/x86/kvm/vmx/pmu_intel.c|511| <<intel_pmu_refresh>> pmu->nr_arch_fixed_counters = min3(ARRAY_SIZE(fixed_pmc_events),
+ * (size_t) edx.split.num_counters_fixed, (size_t)kvm_pmu_cap.num_counters_fixed);
+ */
pmu->nr_arch_fixed_counters = 0;
bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
}
@@ -242,9 +337,17 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)
struct kvm_pmc *pmc = &pmu->gp_counters[i];
pmc_stop_counter(pmc);
+ /*
+ * 重置每一个gp_counter的sel/cnt
+ */
pmc->counter = pmc->prev_counter = pmc->eventsel = 0;
}
+ /*
+ * 同时reset:
+ * - pmu->global_ctrl
+ * - pmu->global_status
+ */
pmu->global_ctrl = pmu->global_status = 0;
}
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index d381ad424554..33dc241c41dd 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -5014,6 +5014,13 @@ static __init void svm_set_cpu_caps(void)
else
kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+ /*
+ * called by:
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ */
if (kvm_pmu_cap.version != 2 ||
!kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a6b9bea62fb8..8544c1f137ae 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7160,6 +7160,13 @@ static void kvm_probe_msr_to_save(u32 msr_index)
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
+ /*
+ * 在以下调用kvm_cpu_cap_init_kvm_defined():
+ * - arch/x86/kvm/cpuid.c|649| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI));
+ * - arch/x86/kvm/cpuid.c|657| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA));
+ * - arch/x86/kvm/cpuid.c|683| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, SF(CONSTANT_TSC));
+ * - arch/x86/kvm/cpuid.c|732| <<kvm_set_cpu_caps>> kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, F(PERFMON_V2));
+ */
if (!kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
return;
break;
--
2.34.1