pmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Kernel-based Virtual Machine -- Performane Monitoring Unit support
  3. *
  4. * Copyright 2011 Red Hat, Inc. and/or its affiliates.
  5. *
  6. * Authors:
  7. * Avi Kivity <avi@redhat.com>
  8. * Gleb Natapov <gleb@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2. See
  11. * the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kvm_host.h>
  16. #include <linux/perf_event.h>
  17. #include "x86.h"
  18. #include "cpuid.h"
  19. #include "lapic.h"
  20. static struct kvm_arch_event_perf_mapping {
  21. u8 eventsel;
  22. u8 unit_mask;
  23. unsigned event_type;
  24. bool inexact;
  25. } arch_events[] = {
  26. /* Index must match CPUID 0x0A.EBX bit vector */
  27. [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES },
  28. [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS },
  29. [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES },
  30. [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES },
  31. [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES },
  32. [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  33. [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES },
  34. [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES },
  35. };
  36. /* mapping between fixed pmc index and arch_events array */
  37. int fixed_pmc_events[] = {1, 0, 7};
  38. static bool pmc_is_gp(struct kvm_pmc *pmc)
  39. {
  40. return pmc->type == KVM_PMC_GP;
  41. }
  42. static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
  43. {
  44. struct kvm_pmu *pmu = &pmc->vcpu->arch.pmu;
  45. return pmu->counter_bitmask[pmc->type];
  46. }
  47. static inline bool pmc_enabled(struct kvm_pmc *pmc)
  48. {
  49. struct kvm_pmu *pmu = &pmc->vcpu->arch.pmu;
  50. return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
  51. }
  52. static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
  53. u32 base)
  54. {
  55. if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
  56. return &pmu->gp_counters[msr - base];
  57. return NULL;
  58. }
  59. static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
  60. {
  61. int base = MSR_CORE_PERF_FIXED_CTR0;
  62. if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
  63. return &pmu->fixed_counters[msr - base];
  64. return NULL;
  65. }
  66. static inline struct kvm_pmc *get_fixed_pmc_idx(struct kvm_pmu *pmu, int idx)
  67. {
  68. return get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + idx);
  69. }
  70. static struct kvm_pmc *global_idx_to_pmc(struct kvm_pmu *pmu, int idx)
  71. {
  72. if (idx < X86_PMC_IDX_FIXED)
  73. return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + idx, MSR_P6_EVNTSEL0);
  74. else
  75. return get_fixed_pmc_idx(pmu, idx - X86_PMC_IDX_FIXED);
  76. }
  77. void kvm_deliver_pmi(struct kvm_vcpu *vcpu)
  78. {
  79. if (vcpu->arch.apic)
  80. kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
  81. }
  82. static void trigger_pmi(struct irq_work *irq_work)
  83. {
  84. struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu,
  85. irq_work);
  86. struct kvm_vcpu *vcpu = container_of(pmu, struct kvm_vcpu,
  87. arch.pmu);
  88. kvm_deliver_pmi(vcpu);
  89. }
  90. static void kvm_perf_overflow(struct perf_event *perf_event,
  91. struct perf_sample_data *data,
  92. struct pt_regs *regs)
  93. {
  94. struct kvm_pmc *pmc = perf_event->overflow_handler_context;
  95. struct kvm_pmu *pmu = &pmc->vcpu->arch.pmu;
  96. __set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
  97. }
  98. static void kvm_perf_overflow_intr(struct perf_event *perf_event,
  99. struct perf_sample_data *data, struct pt_regs *regs)
  100. {
  101. struct kvm_pmc *pmc = perf_event->overflow_handler_context;
  102. struct kvm_pmu *pmu = &pmc->vcpu->arch.pmu;
  103. if (!test_and_set_bit(pmc->idx, (unsigned long *)&pmu->reprogram_pmi)) {
  104. kvm_perf_overflow(perf_event, data, regs);
  105. kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
  106. /*
  107. * Inject PMI. If vcpu was in a guest mode during NMI PMI
  108. * can be ejected on a guest mode re-entry. Otherwise we can't
  109. * be sure that vcpu wasn't executing hlt instruction at the
  110. * time of vmexit and is not going to re-enter guest mode until,
  111. * woken up. So we should wake it, but this is impossible from
  112. * NMI context. Do it from irq work instead.
  113. */
  114. if (!kvm_is_in_guest())
  115. irq_work_queue(&pmc->vcpu->arch.pmu.irq_work);
  116. else
  117. kvm_make_request(KVM_REQ_PMI, pmc->vcpu);
  118. }
  119. }
  120. static u64 read_pmc(struct kvm_pmc *pmc)
  121. {
  122. u64 counter, enabled, running;
  123. counter = pmc->counter;
  124. if (pmc->perf_event)
  125. counter += perf_event_read_value(pmc->perf_event,
  126. &enabled, &running);
  127. /* FIXME: Scaling needed? */
  128. return counter & pmc_bitmask(pmc);
  129. }
  130. static void stop_counter(struct kvm_pmc *pmc)
  131. {
  132. if (pmc->perf_event) {
  133. pmc->counter = read_pmc(pmc);
  134. perf_event_release_kernel(pmc->perf_event);
  135. pmc->perf_event = NULL;
  136. }
  137. }
  138. static void reprogram_counter(struct kvm_pmc *pmc, u32 type,
  139. unsigned config, bool exclude_user, bool exclude_kernel,
  140. bool intr)
  141. {
  142. struct perf_event *event;
  143. struct perf_event_attr attr = {
  144. .type = type,
  145. .size = sizeof(attr),
  146. .pinned = true,
  147. .exclude_idle = true,
  148. .exclude_host = 1,
  149. .exclude_user = exclude_user,
  150. .exclude_kernel = exclude_kernel,
  151. .config = config,
  152. };
  153. attr.sample_period = (-pmc->counter) & pmc_bitmask(pmc);
  154. event = perf_event_create_kernel_counter(&attr, -1, current,
  155. intr ? kvm_perf_overflow_intr :
  156. kvm_perf_overflow, pmc);
  157. if (IS_ERR(event)) {
  158. printk_once("kvm: pmu event creation failed %ld\n",
  159. PTR_ERR(event));
  160. return;
  161. }
  162. pmc->perf_event = event;
  163. clear_bit(pmc->idx, (unsigned long*)&pmc->vcpu->arch.pmu.reprogram_pmi);
  164. }
  165. static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select,
  166. u8 unit_mask)
  167. {
  168. int i;
  169. for (i = 0; i < ARRAY_SIZE(arch_events); i++)
  170. if (arch_events[i].eventsel == event_select
  171. && arch_events[i].unit_mask == unit_mask
  172. && (pmu->available_event_types & (1 << i)))
  173. break;
  174. if (i == ARRAY_SIZE(arch_events))
  175. return PERF_COUNT_HW_MAX;
  176. return arch_events[i].event_type;
  177. }
  178. static void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
  179. {
  180. unsigned config, type = PERF_TYPE_RAW;
  181. u8 event_select, unit_mask;
  182. if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
  183. printk_once("kvm pmu: pin control bit is ignored\n");
  184. pmc->eventsel = eventsel;
  185. stop_counter(pmc);
  186. if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_enabled(pmc))
  187. return;
  188. event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT;
  189. unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
  190. if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE |
  191. ARCH_PERFMON_EVENTSEL_INV |
  192. ARCH_PERFMON_EVENTSEL_CMASK))) {
  193. config = find_arch_event(&pmc->vcpu->arch.pmu, event_select,
  194. unit_mask);
  195. if (config != PERF_COUNT_HW_MAX)
  196. type = PERF_TYPE_HARDWARE;
  197. }
  198. if (type == PERF_TYPE_RAW)
  199. config = eventsel & X86_RAW_EVENT_MASK;
  200. reprogram_counter(pmc, type, config,
  201. !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
  202. !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
  203. eventsel & ARCH_PERFMON_EVENTSEL_INT);
  204. }
  205. static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx)
  206. {
  207. unsigned en = en_pmi & 0x3;
  208. bool pmi = en_pmi & 0x8;
  209. stop_counter(pmc);
  210. if (!en || !pmc_enabled(pmc))
  211. return;
  212. reprogram_counter(pmc, PERF_TYPE_HARDWARE,
  213. arch_events[fixed_pmc_events[idx]].event_type,
  214. !(en & 0x2), /* exclude user */
  215. !(en & 0x1), /* exclude kernel */
  216. pmi);
  217. }
  218. static inline u8 fixed_en_pmi(u64 ctrl, int idx)
  219. {
  220. return (ctrl >> (idx * 4)) & 0xf;
  221. }
  222. static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
  223. {
  224. int i;
  225. for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
  226. u8 en_pmi = fixed_en_pmi(data, i);
  227. struct kvm_pmc *pmc = get_fixed_pmc_idx(pmu, i);
  228. if (fixed_en_pmi(pmu->fixed_ctr_ctrl, i) == en_pmi)
  229. continue;
  230. reprogram_fixed_counter(pmc, en_pmi, i);
  231. }
  232. pmu->fixed_ctr_ctrl = data;
  233. }
  234. static void reprogram_idx(struct kvm_pmu *pmu, int idx)
  235. {
  236. struct kvm_pmc *pmc = global_idx_to_pmc(pmu, idx);
  237. if (!pmc)
  238. return;
  239. if (pmc_is_gp(pmc))
  240. reprogram_gp_counter(pmc, pmc->eventsel);
  241. else {
  242. int fidx = idx - X86_PMC_IDX_FIXED;
  243. reprogram_fixed_counter(pmc,
  244. fixed_en_pmi(pmu->fixed_ctr_ctrl, fidx), fidx);
  245. }
  246. }
  247. static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data)
  248. {
  249. int bit;
  250. u64 diff = pmu->global_ctrl ^ data;
  251. pmu->global_ctrl = data;
  252. for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
  253. reprogram_idx(pmu, bit);
  254. }
  255. bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr)
  256. {
  257. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  258. int ret;
  259. switch (msr) {
  260. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  261. case MSR_CORE_PERF_GLOBAL_STATUS:
  262. case MSR_CORE_PERF_GLOBAL_CTRL:
  263. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  264. ret = pmu->version > 1;
  265. break;
  266. default:
  267. ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)
  268. || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0)
  269. || get_fixed_pmc(pmu, msr);
  270. break;
  271. }
  272. return ret;
  273. }
  274. int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
  275. {
  276. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  277. struct kvm_pmc *pmc;
  278. switch (index) {
  279. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  280. *data = pmu->fixed_ctr_ctrl;
  281. return 0;
  282. case MSR_CORE_PERF_GLOBAL_STATUS:
  283. *data = pmu->global_status;
  284. return 0;
  285. case MSR_CORE_PERF_GLOBAL_CTRL:
  286. *data = pmu->global_ctrl;
  287. return 0;
  288. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  289. *data = pmu->global_ovf_ctrl;
  290. return 0;
  291. default:
  292. if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
  293. (pmc = get_fixed_pmc(pmu, index))) {
  294. *data = read_pmc(pmc);
  295. return 0;
  296. } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
  297. *data = pmc->eventsel;
  298. return 0;
  299. }
  300. }
  301. return 1;
  302. }
  303. int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
  304. {
  305. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  306. struct kvm_pmc *pmc;
  307. switch (index) {
  308. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  309. if (pmu->fixed_ctr_ctrl == data)
  310. return 0;
  311. if (!(data & 0xfffffffffffff444ull)) {
  312. reprogram_fixed_counters(pmu, data);
  313. return 0;
  314. }
  315. break;
  316. case MSR_CORE_PERF_GLOBAL_STATUS:
  317. break; /* RO MSR */
  318. case MSR_CORE_PERF_GLOBAL_CTRL:
  319. if (pmu->global_ctrl == data)
  320. return 0;
  321. if (!(data & pmu->global_ctrl_mask)) {
  322. global_ctrl_changed(pmu, data);
  323. return 0;
  324. }
  325. break;
  326. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  327. if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) {
  328. pmu->global_status &= ~data;
  329. pmu->global_ovf_ctrl = data;
  330. return 0;
  331. }
  332. break;
  333. default:
  334. if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
  335. (pmc = get_fixed_pmc(pmu, index))) {
  336. data = (s64)(s32)data;
  337. pmc->counter += data - read_pmc(pmc);
  338. return 0;
  339. } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
  340. if (data == pmc->eventsel)
  341. return 0;
  342. if (!(data & 0xffffffff00200000ull)) {
  343. reprogram_gp_counter(pmc, data);
  344. return 0;
  345. }
  346. }
  347. }
  348. return 1;
  349. }
  350. int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data)
  351. {
  352. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  353. bool fast_mode = pmc & (1u << 31);
  354. bool fixed = pmc & (1u << 30);
  355. struct kvm_pmc *counters;
  356. u64 ctr;
  357. pmc &= ~(3u << 30);
  358. if (!fixed && pmc >= pmu->nr_arch_gp_counters)
  359. return 1;
  360. if (fixed && pmc >= pmu->nr_arch_fixed_counters)
  361. return 1;
  362. counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
  363. ctr = read_pmc(&counters[pmc]);
  364. if (fast_mode)
  365. ctr = (u32)ctr;
  366. *data = ctr;
  367. return 0;
  368. }
  369. void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu)
  370. {
  371. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  372. struct kvm_cpuid_entry2 *entry;
  373. unsigned bitmap_len;
  374. pmu->nr_arch_gp_counters = 0;
  375. pmu->nr_arch_fixed_counters = 0;
  376. pmu->counter_bitmask[KVM_PMC_GP] = 0;
  377. pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
  378. pmu->version = 0;
  379. entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
  380. if (!entry)
  381. return;
  382. pmu->version = entry->eax & 0xff;
  383. if (!pmu->version)
  384. return;
  385. pmu->nr_arch_gp_counters = min((int)(entry->eax >> 8) & 0xff,
  386. X86_PMC_MAX_GENERIC);
  387. pmu->counter_bitmask[KVM_PMC_GP] =
  388. ((u64)1 << ((entry->eax >> 16) & 0xff)) - 1;
  389. bitmap_len = (entry->eax >> 24) & 0xff;
  390. pmu->available_event_types = ~entry->ebx & ((1ull << bitmap_len) - 1);
  391. if (pmu->version == 1) {
  392. pmu->nr_arch_fixed_counters = 0;
  393. } else {
  394. pmu->nr_arch_fixed_counters = min((int)(entry->edx & 0x1f),
  395. X86_PMC_MAX_FIXED);
  396. pmu->counter_bitmask[KVM_PMC_FIXED] =
  397. ((u64)1 << ((entry->edx >> 5) & 0xff)) - 1;
  398. }
  399. pmu->global_ctrl = ((1 << pmu->nr_arch_gp_counters) - 1) |
  400. (((1ull << pmu->nr_arch_fixed_counters) - 1) << X86_PMC_IDX_FIXED);
  401. pmu->global_ctrl_mask = ~pmu->global_ctrl;
  402. }
  403. void kvm_pmu_init(struct kvm_vcpu *vcpu)
  404. {
  405. int i;
  406. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  407. memset(pmu, 0, sizeof(*pmu));
  408. for (i = 0; i < X86_PMC_MAX_GENERIC; i++) {
  409. pmu->gp_counters[i].type = KVM_PMC_GP;
  410. pmu->gp_counters[i].vcpu = vcpu;
  411. pmu->gp_counters[i].idx = i;
  412. }
  413. for (i = 0; i < X86_PMC_MAX_FIXED; i++) {
  414. pmu->fixed_counters[i].type = KVM_PMC_FIXED;
  415. pmu->fixed_counters[i].vcpu = vcpu;
  416. pmu->fixed_counters[i].idx = i + X86_PMC_IDX_FIXED;
  417. }
  418. init_irq_work(&pmu->irq_work, trigger_pmi);
  419. kvm_pmu_cpuid_update(vcpu);
  420. }
  421. void kvm_pmu_reset(struct kvm_vcpu *vcpu)
  422. {
  423. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  424. int i;
  425. irq_work_sync(&pmu->irq_work);
  426. for (i = 0; i < X86_PMC_MAX_GENERIC; i++) {
  427. struct kvm_pmc *pmc = &pmu->gp_counters[i];
  428. stop_counter(pmc);
  429. pmc->counter = pmc->eventsel = 0;
  430. }
  431. for (i = 0; i < X86_PMC_MAX_FIXED; i++)
  432. stop_counter(&pmu->fixed_counters[i]);
  433. pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =
  434. pmu->global_ovf_ctrl = 0;
  435. }
  436. void kvm_pmu_destroy(struct kvm_vcpu *vcpu)
  437. {
  438. kvm_pmu_reset(vcpu);
  439. }
  440. void kvm_handle_pmu_event(struct kvm_vcpu *vcpu)
  441. {
  442. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  443. u64 bitmask;
  444. int bit;
  445. bitmask = pmu->reprogram_pmi;
  446. for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) {
  447. struct kvm_pmc *pmc = global_idx_to_pmc(pmu, bit);
  448. if (unlikely(!pmc || !pmc->perf_event)) {
  449. clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi);
  450. continue;
  451. }
  452. reprogram_idx(pmu, bit);
  453. }
  454. }