Skip to content

Commit 6b08cd6

Browse files
Boris OstrovskyDavid Vrabel
authored andcommitted
xen/PMU: Intercept PMU-related MSR and APIC accesses
Provide interfaces for recognizing accesses to PMU-related MSRs and LVTPC APIC and process these accesses in Xen PMU code. (The interrupt handler performs XENPMU_flush right away in the beginning since no PMU emulation is available. It will be added with a later patch). Signed-off-by: Boris Ostrovsky <[email protected]> Reviewed-by: David Vrabel <[email protected]> Signed-off-by: David Vrabel <[email protected]>
1 parent e27b72d commit 6b08cd6

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

arch/x86/xen/apic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <xen/xen.h>
88
#include <xen/interface/physdev.h>
99
#include "xen-ops.h"
10+
#include "pmu.h"
1011
#include "smp.h"
1112

1213
static unsigned int xen_io_apic_read(unsigned apic, unsigned reg)
@@ -72,8 +73,10 @@ static u32 xen_apic_read(u32 reg)
7273

7374
static void xen_apic_write(u32 reg, u32 val)
7475
{
75-
if (reg == APIC_LVTPC)
76+
if (reg == APIC_LVTPC) {
77+
(void)pmu_apic_update(reg);
7678
return;
79+
}
7780

7881
/* Warn to see if there's any stray references */
7982
WARN(1,"register: %x, value: %x\n", reg, val);

arch/x86/xen/enlighten.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,9 @@ static u64 xen_read_msr_safe(unsigned int msr, int *err)
10311031
{
10321032
u64 val;
10331033

1034+
if (pmu_msr_read(msr, &val, err))
1035+
return val;
1036+
10341037
val = native_read_msr_safe(msr, err);
10351038
switch (msr) {
10361039
case MSR_IA32_APICBASE:
@@ -1077,17 +1080,13 @@ static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
10771080
Xen console noise. */
10781081

10791082
default:
1080-
ret = native_write_msr_safe(msr, low, high);
1083+
if (!pmu_msr_write(msr, low, high, &ret))
1084+
ret = native_write_msr_safe(msr, low, high);
10811085
}
10821086

10831087
return ret;
10841088
}
10851089

1086-
unsigned long long xen_read_pmc(int counter)
1087-
{
1088-
return 0;
1089-
}
1090-
10911090
void xen_setup_shared_info(void)
10921091
{
10931092
if (!xen_feature(XENFEAT_auto_translated_physmap)) {

arch/x86/xen/pmu.c

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ static __read_mostly int amd_num_counters;
5151
/* Alias registers (0x4c1) for full-width writes to PMCs */
5252
#define MSR_PMC_ALIAS_MASK (~(MSR_IA32_PERFCTR0 ^ MSR_IA32_PMC0))
5353

54+
#define INTEL_PMC_TYPE_SHIFT 30
55+
5456
static __read_mostly int intel_num_arch_counters, intel_num_fixed_counters;
5557

5658

@@ -167,6 +169,91 @@ static int is_intel_pmu_msr(u32 msr_index, int *type, int *index)
167169
}
168170
}
169171

172+
bool pmu_msr_read(unsigned int msr, uint64_t *val, int *err)
173+
{
174+
175+
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
176+
if (is_amd_pmu_msr(msr)) {
177+
*val = native_read_msr_safe(msr, err);
178+
return true;
179+
}
180+
} else {
181+
int type, index;
182+
183+
if (is_intel_pmu_msr(msr, &type, &index)) {
184+
*val = native_read_msr_safe(msr, err);
185+
return true;
186+
}
187+
}
188+
189+
return false;
190+
}
191+
192+
bool pmu_msr_write(unsigned int msr, uint32_t low, uint32_t high, int *err)
193+
{
194+
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
195+
if (is_amd_pmu_msr(msr)) {
196+
*err = native_write_msr_safe(msr, low, high);
197+
return true;
198+
}
199+
} else {
200+
int type, index;
201+
202+
if (is_intel_pmu_msr(msr, &type, &index)) {
203+
*err = native_write_msr_safe(msr, low, high);
204+
return true;
205+
}
206+
}
207+
208+
return false;
209+
}
210+
211+
static unsigned long long xen_amd_read_pmc(int counter)
212+
{
213+
uint32_t msr;
214+
int err;
215+
216+
msr = amd_counters_base + (counter * amd_msr_step);
217+
return native_read_msr_safe(msr, &err);
218+
}
219+
220+
static unsigned long long xen_intel_read_pmc(int counter)
221+
{
222+
int err;
223+
uint32_t msr;
224+
225+
if (counter & (1<<INTEL_PMC_TYPE_SHIFT))
226+
msr = MSR_CORE_PERF_FIXED_CTR0 + (counter & 0xffff);
227+
else
228+
msr = MSR_IA32_PERFCTR0 + counter;
229+
230+
return native_read_msr_safe(msr, &err);
231+
}
232+
233+
unsigned long long xen_read_pmc(int counter)
234+
{
235+
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
236+
return xen_amd_read_pmc(counter);
237+
else
238+
return xen_intel_read_pmc(counter);
239+
}
240+
241+
int pmu_apic_update(uint32_t val)
242+
{
243+
int ret;
244+
struct xen_pmu_data *xenpmu_data = get_xenpmu_data();
245+
246+
if (!xenpmu_data) {
247+
pr_warn_once("%s: pmudata not initialized\n", __func__);
248+
return -EINVAL;
249+
}
250+
251+
xenpmu_data->pmu.l.lapic_lvtpc = val;
252+
ret = HYPERVISOR_xenpmu_op(XENPMU_lvtpc_set, NULL);
253+
254+
return ret;
255+
}
256+
170257
/* perf callbacks */
171258
static int xen_is_in_guest(void)
172259
{
@@ -239,7 +326,7 @@ static void xen_convert_regs(const struct xen_pmu_regs *xen_regs,
239326

240327
irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id)
241328
{
242-
int ret = IRQ_NONE;
329+
int err, ret = IRQ_NONE;
243330
struct pt_regs regs;
244331
const struct xen_pmu_data *xenpmu_data = get_xenpmu_data();
245332

@@ -248,6 +335,12 @@ irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id)
248335
return ret;
249336
}
250337

338+
err = HYPERVISOR_xenpmu_op(XENPMU_flush, NULL);
339+
if (err) {
340+
pr_warn_once("%s: failed hypercall, err: %d\n", __func__, err);
341+
return ret;
342+
}
343+
251344
xen_convert_regs(&xenpmu_data->pmu.r.regs, &regs,
252345
xenpmu_data->pmu.pmu_flags);
253346
if (x86_pmu.handle_irq(&regs))

arch/x86/xen/pmu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id);
77
void xen_pmu_init(int cpu);
88
void xen_pmu_finish(int cpu);
99
bool is_xen_pmu(int cpu);
10+
bool pmu_msr_read(unsigned int msr, uint64_t *val, int *err);
11+
bool pmu_msr_write(unsigned int msr, uint32_t low, uint32_t high, int *err);
12+
int pmu_apic_update(uint32_t reg);
13+
unsigned long long xen_read_pmc(int counter);
1014

1115
#endif /* __XEN_PMU_H */

include/xen/interface/xenpmu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#define XENPMU_feature_set 3
2121
#define XENPMU_init 4
2222
#define XENPMU_finish 5
23+
#define XENPMU_lvtpc_set 6
24+
#define XENPMU_flush 7
2325

2426
/* ` } */
2527

0 commit comments

Comments
 (0)