pmu.c 13 KB

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