PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/linux/arch/x86/kvm/pmu.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 533 lines | 427 code | 82 blank | 24 comment | 71 complexity | 72a0ec76c4e7bc4a842b1e0feceb431c MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. pmc->eventsel = eventsel;
  182. stop_counter(pmc);
  183. if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_enabled(pmc))
  184. return;
  185. event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT;
  186. unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
  187. if (!(event_select & (ARCH_PERFMON_EVENTSEL_EDGE |
  188. ARCH_PERFMON_EVENTSEL_INV |
  189. ARCH_PERFMON_EVENTSEL_CMASK))) {
  190. config = find_arch_event(&pmc->vcpu->arch.pmu, event_select,
  191. unit_mask);
  192. if (config != PERF_COUNT_HW_MAX)
  193. type = PERF_TYPE_HARDWARE;
  194. }
  195. if (type == PERF_TYPE_RAW)
  196. config = eventsel & X86_RAW_EVENT_MASK;
  197. reprogram_counter(pmc, type, config,
  198. !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
  199. !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
  200. eventsel & ARCH_PERFMON_EVENTSEL_INT);
  201. }
  202. static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx)
  203. {
  204. unsigned en = en_pmi & 0x3;
  205. bool pmi = en_pmi & 0x8;
  206. stop_counter(pmc);
  207. if (!en || !pmc_enabled(pmc))
  208. return;
  209. reprogram_counter(pmc, PERF_TYPE_HARDWARE,
  210. arch_events[fixed_pmc_events[idx]].event_type,
  211. !(en & 0x2), /* exclude user */
  212. !(en & 0x1), /* exclude kernel */
  213. pmi);
  214. }
  215. static inline u8 fixed_en_pmi(u64 ctrl, int idx)
  216. {
  217. return (ctrl >> (idx * 4)) & 0xf;
  218. }
  219. static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
  220. {
  221. int i;
  222. for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
  223. u8 en_pmi = fixed_en_pmi(data, i);
  224. struct kvm_pmc *pmc = get_fixed_pmc_idx(pmu, i);
  225. if (fixed_en_pmi(pmu->fixed_ctr_ctrl, i) == en_pmi)
  226. continue;
  227. reprogram_fixed_counter(pmc, en_pmi, i);
  228. }
  229. pmu->fixed_ctr_ctrl = data;
  230. }
  231. static void reprogram_idx(struct kvm_pmu *pmu, int idx)
  232. {
  233. struct kvm_pmc *pmc = global_idx_to_pmc(pmu, idx);
  234. if (!pmc)
  235. return;
  236. if (pmc_is_gp(pmc))
  237. reprogram_gp_counter(pmc, pmc->eventsel);
  238. else {
  239. int fidx = idx - X86_PMC_IDX_FIXED;
  240. reprogram_fixed_counter(pmc,
  241. fixed_en_pmi(pmu->fixed_ctr_ctrl, fidx), fidx);
  242. }
  243. }
  244. static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data)
  245. {
  246. int bit;
  247. u64 diff = pmu->global_ctrl ^ data;
  248. pmu->global_ctrl = data;
  249. for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
  250. reprogram_idx(pmu, bit);
  251. }
  252. bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr)
  253. {
  254. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  255. int ret;
  256. switch (msr) {
  257. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  258. case MSR_CORE_PERF_GLOBAL_STATUS:
  259. case MSR_CORE_PERF_GLOBAL_CTRL:
  260. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  261. ret = pmu->version > 1;
  262. break;
  263. default:
  264. ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)
  265. || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0)
  266. || get_fixed_pmc(pmu, msr);
  267. break;
  268. }
  269. return ret;
  270. }
  271. int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
  272. {
  273. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  274. struct kvm_pmc *pmc;
  275. switch (index) {
  276. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  277. *data = pmu->fixed_ctr_ctrl;
  278. return 0;
  279. case MSR_CORE_PERF_GLOBAL_STATUS:
  280. *data = pmu->global_status;
  281. return 0;
  282. case MSR_CORE_PERF_GLOBAL_CTRL:
  283. *data = pmu->global_ctrl;
  284. return 0;
  285. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  286. *data = pmu->global_ovf_ctrl;
  287. return 0;
  288. default:
  289. if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
  290. (pmc = get_fixed_pmc(pmu, index))) {
  291. *data = read_pmc(pmc);
  292. return 0;
  293. } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
  294. *data = pmc->eventsel;
  295. return 0;
  296. }
  297. }
  298. return 1;
  299. }
  300. int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
  301. {
  302. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  303. struct kvm_pmc *pmc;
  304. switch (index) {
  305. case MSR_CORE_PERF_FIXED_CTR_CTRL:
  306. if (pmu->fixed_ctr_ctrl == data)
  307. return 0;
  308. if (!(data & 0xfffffffffffff444)) {
  309. reprogram_fixed_counters(pmu, data);
  310. return 0;
  311. }
  312. break;
  313. case MSR_CORE_PERF_GLOBAL_STATUS:
  314. break; /* RO MSR */
  315. case MSR_CORE_PERF_GLOBAL_CTRL:
  316. if (pmu->global_ctrl == data)
  317. return 0;
  318. if (!(data & pmu->global_ctrl_mask)) {
  319. global_ctrl_changed(pmu, data);
  320. return 0;
  321. }
  322. break;
  323. case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  324. if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) {
  325. pmu->global_status &= ~data;
  326. pmu->global_ovf_ctrl = data;
  327. return 0;
  328. }
  329. break;
  330. default:
  331. if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
  332. (pmc = get_fixed_pmc(pmu, index))) {
  333. data = (s64)(s32)data;
  334. pmc->counter += data - read_pmc(pmc);
  335. return 0;
  336. } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
  337. if (data == pmc->eventsel)
  338. return 0;
  339. if (!(data & 0xffffffff00200000ull)) {
  340. reprogram_gp_counter(pmc, data);
  341. return 0;
  342. }
  343. }
  344. }
  345. return 1;
  346. }
  347. int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data)
  348. {
  349. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  350. bool fast_mode = pmc & (1u << 31);
  351. bool fixed = pmc & (1u << 30);
  352. struct kvm_pmc *counters;
  353. u64 ctr;
  354. pmc &= (3u << 30) - 1;
  355. if (!fixed && pmc >= pmu->nr_arch_gp_counters)
  356. return 1;
  357. if (fixed && pmc >= pmu->nr_arch_fixed_counters)
  358. return 1;
  359. counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
  360. ctr = read_pmc(&counters[pmc]);
  361. if (fast_mode)
  362. ctr = (u32)ctr;
  363. *data = ctr;
  364. return 0;
  365. }
  366. void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu)
  367. {
  368. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  369. struct kvm_cpuid_entry2 *entry;
  370. unsigned bitmap_len;
  371. pmu->nr_arch_gp_counters = 0;
  372. pmu->nr_arch_fixed_counters = 0;
  373. pmu->counter_bitmask[KVM_PMC_GP] = 0;
  374. pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
  375. pmu->version = 0;
  376. entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
  377. if (!entry)
  378. return;
  379. pmu->version = entry->eax & 0xff;
  380. if (!pmu->version)
  381. return;
  382. pmu->nr_arch_gp_counters = min((int)(entry->eax >> 8) & 0xff,
  383. X86_PMC_MAX_GENERIC);
  384. pmu->counter_bitmask[KVM_PMC_GP] =
  385. ((u64)1 << ((entry->eax >> 16) & 0xff)) - 1;
  386. bitmap_len = (entry->eax >> 24) & 0xff;
  387. pmu->available_event_types = ~entry->ebx & ((1ull << bitmap_len) - 1);
  388. if (pmu->version == 1) {
  389. pmu->global_ctrl = (1 << pmu->nr_arch_gp_counters) - 1;
  390. return;
  391. }
  392. pmu->nr_arch_fixed_counters = min((int)(entry->edx & 0x1f),
  393. X86_PMC_MAX_FIXED);
  394. pmu->counter_bitmask[KVM_PMC_FIXED] =
  395. ((u64)1 << ((entry->edx >> 5) & 0xff)) - 1;
  396. pmu->global_ctrl_mask = ~(((1 << pmu->nr_arch_gp_counters) - 1)
  397. | (((1ull << pmu->nr_arch_fixed_counters) - 1)
  398. << X86_PMC_IDX_FIXED));
  399. }
  400. void kvm_pmu_init(struct kvm_vcpu *vcpu)
  401. {
  402. int i;
  403. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  404. memset(pmu, 0, sizeof(*pmu));
  405. for (i = 0; i < X86_PMC_MAX_GENERIC; i++) {
  406. pmu->gp_counters[i].type = KVM_PMC_GP;
  407. pmu->gp_counters[i].vcpu = vcpu;
  408. pmu->gp_counters[i].idx = i;
  409. }
  410. for (i = 0; i < X86_PMC_MAX_FIXED; i++) {
  411. pmu->fixed_counters[i].type = KVM_PMC_FIXED;
  412. pmu->fixed_counters[i].vcpu = vcpu;
  413. pmu->fixed_counters[i].idx = i + X86_PMC_IDX_FIXED;
  414. }
  415. init_irq_work(&pmu->irq_work, trigger_pmi);
  416. kvm_pmu_cpuid_update(vcpu);
  417. }
  418. void kvm_pmu_reset(struct kvm_vcpu *vcpu)
  419. {
  420. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  421. int i;
  422. irq_work_sync(&pmu->irq_work);
  423. for (i = 0; i < X86_PMC_MAX_GENERIC; i++) {
  424. struct kvm_pmc *pmc = &pmu->gp_counters[i];
  425. stop_counter(pmc);
  426. pmc->counter = pmc->eventsel = 0;
  427. }
  428. for (i = 0; i < X86_PMC_MAX_FIXED; i++)
  429. stop_counter(&pmu->fixed_counters[i]);
  430. pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =
  431. pmu->global_ovf_ctrl = 0;
  432. }
  433. void kvm_pmu_destroy(struct kvm_vcpu *vcpu)
  434. {
  435. kvm_pmu_reset(vcpu);
  436. }
  437. void kvm_handle_pmu_event(struct kvm_vcpu *vcpu)
  438. {
  439. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  440. u64 bitmask;
  441. int bit;
  442. bitmask = pmu->reprogram_pmi;
  443. for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) {
  444. struct kvm_pmc *pmc = global_idx_to_pmc(pmu, bit);
  445. if (unlikely(!pmc || !pmc->perf_event)) {
  446. clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi);
  447. continue;
  448. }
  449. reprogram_idx(pmu, bit);
  450. }
  451. }