pmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (C) 2015 Linaro Ltd.
  3. * Author: Shannon Zhao <shannon.zhao@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/kvm_emulate.h>
  23. #include <kvm/arm_pmu.h>
  24. #include <kvm/arm_vgic.h>
  25. /**
  26. * kvm_pmu_get_counter_value - get PMU counter value
  27. * @vcpu: The vcpu pointer
  28. * @select_idx: The counter index
  29. */
  30. u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
  31. {
  32. u64 counter, reg, enabled, running;
  33. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  34. struct kvm_pmc *pmc = &pmu->pmc[select_idx];
  35. reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
  36. ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
  37. counter = vcpu_sys_reg(vcpu, reg);
  38. /* The real counter value is equal to the value of counter register plus
  39. * the value perf event counts.
  40. */
  41. if (pmc->perf_event)
  42. counter += perf_event_read_value(pmc->perf_event, &enabled,
  43. &running);
  44. return counter & pmc->bitmask;
  45. }
  46. /**
  47. * kvm_pmu_set_counter_value - set PMU counter value
  48. * @vcpu: The vcpu pointer
  49. * @select_idx: The counter index
  50. * @val: The counter value
  51. */
  52. void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
  53. {
  54. u64 reg;
  55. reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
  56. ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
  57. vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
  58. }
  59. /**
  60. * kvm_pmu_stop_counter - stop PMU counter
  61. * @pmc: The PMU counter pointer
  62. *
  63. * If this counter has been configured to monitor some event, release it here.
  64. */
  65. static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
  66. {
  67. u64 counter, reg;
  68. if (pmc->perf_event) {
  69. counter = kvm_pmu_get_counter_value(vcpu, pmc->idx);
  70. reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
  71. ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
  72. vcpu_sys_reg(vcpu, reg) = counter;
  73. perf_event_disable(pmc->perf_event);
  74. perf_event_release_kernel(pmc->perf_event);
  75. pmc->perf_event = NULL;
  76. }
  77. }
  78. /**
  79. * kvm_pmu_vcpu_reset - reset pmu state for cpu
  80. * @vcpu: The vcpu pointer
  81. *
  82. */
  83. void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
  84. {
  85. int i;
  86. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  87. for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
  88. kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
  89. pmu->pmc[i].idx = i;
  90. pmu->pmc[i].bitmask = 0xffffffffUL;
  91. }
  92. }
  93. /**
  94. * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
  95. * @vcpu: The vcpu pointer
  96. *
  97. */
  98. void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
  99. {
  100. int i;
  101. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  102. for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
  103. struct kvm_pmc *pmc = &pmu->pmc[i];
  104. if (pmc->perf_event) {
  105. perf_event_disable(pmc->perf_event);
  106. perf_event_release_kernel(pmc->perf_event);
  107. pmc->perf_event = NULL;
  108. }
  109. }
  110. }
  111. u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
  112. {
  113. u64 val = vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
  114. val &= ARMV8_PMU_PMCR_N_MASK;
  115. if (val == 0)
  116. return BIT(ARMV8_PMU_CYCLE_IDX);
  117. else
  118. return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
  119. }
  120. /**
  121. * kvm_pmu_enable_counter - enable selected PMU counter
  122. * @vcpu: The vcpu pointer
  123. * @val: the value guest writes to PMCNTENSET register
  124. *
  125. * Call perf_event_enable to start counting the perf event
  126. */
  127. void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val)
  128. {
  129. int i;
  130. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  131. struct kvm_pmc *pmc;
  132. if (!(vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
  133. return;
  134. for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
  135. if (!(val & BIT(i)))
  136. continue;
  137. pmc = &pmu->pmc[i];
  138. if (pmc->perf_event) {
  139. perf_event_enable(pmc->perf_event);
  140. if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
  141. kvm_debug("fail to enable perf event\n");
  142. }
  143. }
  144. }
  145. /**
  146. * kvm_pmu_disable_counter - disable selected PMU counter
  147. * @vcpu: The vcpu pointer
  148. * @val: the value guest writes to PMCNTENCLR register
  149. *
  150. * Call perf_event_disable to stop counting the perf event
  151. */
  152. void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val)
  153. {
  154. int i;
  155. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  156. struct kvm_pmc *pmc;
  157. if (!val)
  158. return;
  159. for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
  160. if (!(val & BIT(i)))
  161. continue;
  162. pmc = &pmu->pmc[i];
  163. if (pmc->perf_event)
  164. perf_event_disable(pmc->perf_event);
  165. }
  166. }
  167. static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
  168. {
  169. u64 reg = 0;
  170. if ((vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E)) {
  171. reg = vcpu_sys_reg(vcpu, PMOVSSET_EL0);
  172. reg &= vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
  173. reg &= vcpu_sys_reg(vcpu, PMINTENSET_EL1);
  174. reg &= kvm_pmu_valid_counter_mask(vcpu);
  175. }
  176. return reg;
  177. }
  178. /**
  179. * kvm_pmu_overflow_set - set PMU overflow interrupt
  180. * @vcpu: The vcpu pointer
  181. * @val: the value guest writes to PMOVSSET register
  182. */
  183. void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val)
  184. {
  185. u64 reg;
  186. if (val == 0)
  187. return;
  188. vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= val;
  189. reg = kvm_pmu_overflow_status(vcpu);
  190. if (reg != 0)
  191. kvm_vcpu_kick(vcpu);
  192. }
  193. static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
  194. {
  195. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  196. bool overflow;
  197. if (!kvm_arm_pmu_v3_ready(vcpu))
  198. return;
  199. overflow = !!kvm_pmu_overflow_status(vcpu);
  200. if (pmu->irq_level != overflow) {
  201. pmu->irq_level = overflow;
  202. kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  203. pmu->irq_num, overflow);
  204. }
  205. }
  206. /**
  207. * kvm_pmu_flush_hwstate - flush pmu state to cpu
  208. * @vcpu: The vcpu pointer
  209. *
  210. * Check if the PMU has overflowed while we were running in the host, and inject
  211. * an interrupt if that was the case.
  212. */
  213. void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
  214. {
  215. kvm_pmu_update_state(vcpu);
  216. }
  217. /**
  218. * kvm_pmu_sync_hwstate - sync pmu state from cpu
  219. * @vcpu: The vcpu pointer
  220. *
  221. * Check if the PMU has overflowed while we were running in the guest, and
  222. * inject an interrupt if that was the case.
  223. */
  224. void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
  225. {
  226. kvm_pmu_update_state(vcpu);
  227. }
  228. static inline struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
  229. {
  230. struct kvm_pmu *pmu;
  231. struct kvm_vcpu_arch *vcpu_arch;
  232. pmc -= pmc->idx;
  233. pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
  234. vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
  235. return container_of(vcpu_arch, struct kvm_vcpu, arch);
  236. }
  237. /**
  238. * When perf event overflows, call kvm_pmu_overflow_set to set overflow status.
  239. */
  240. static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
  241. struct perf_sample_data *data,
  242. struct pt_regs *regs)
  243. {
  244. struct kvm_pmc *pmc = perf_event->overflow_handler_context;
  245. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  246. int idx = pmc->idx;
  247. kvm_pmu_overflow_set(vcpu, BIT(idx));
  248. }
  249. /**
  250. * kvm_pmu_software_increment - do software increment
  251. * @vcpu: The vcpu pointer
  252. * @val: the value guest writes to PMSWINC register
  253. */
  254. void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
  255. {
  256. int i;
  257. u64 type, enable, reg;
  258. if (val == 0)
  259. return;
  260. enable = vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
  261. for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) {
  262. if (!(val & BIT(i)))
  263. continue;
  264. type = vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i)
  265. & ARMV8_PMU_EVTYPE_EVENT;
  266. if ((type == ARMV8_PMUV3_PERFCTR_SW_INCR)
  267. && (enable & BIT(i))) {
  268. reg = vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) + 1;
  269. reg = lower_32_bits(reg);
  270. vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg;
  271. if (!reg)
  272. kvm_pmu_overflow_set(vcpu, BIT(i));
  273. }
  274. }
  275. }
  276. /**
  277. * kvm_pmu_handle_pmcr - handle PMCR register
  278. * @vcpu: The vcpu pointer
  279. * @val: the value guest writes to PMCR register
  280. */
  281. void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
  282. {
  283. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  284. struct kvm_pmc *pmc;
  285. u64 mask;
  286. int i;
  287. mask = kvm_pmu_valid_counter_mask(vcpu);
  288. if (val & ARMV8_PMU_PMCR_E) {
  289. kvm_pmu_enable_counter(vcpu,
  290. vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask);
  291. } else {
  292. kvm_pmu_disable_counter(vcpu, mask);
  293. }
  294. if (val & ARMV8_PMU_PMCR_C)
  295. kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
  296. if (val & ARMV8_PMU_PMCR_P) {
  297. for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++)
  298. kvm_pmu_set_counter_value(vcpu, i, 0);
  299. }
  300. if (val & ARMV8_PMU_PMCR_LC) {
  301. pmc = &pmu->pmc[ARMV8_PMU_CYCLE_IDX];
  302. pmc->bitmask = 0xffffffffffffffffUL;
  303. }
  304. }
  305. static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
  306. {
  307. return (vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) &&
  308. (vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(select_idx));
  309. }
  310. /**
  311. * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
  312. * @vcpu: The vcpu pointer
  313. * @data: The data guest writes to PMXEVTYPER_EL0
  314. * @select_idx: The number of selected counter
  315. *
  316. * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
  317. * event with given hardware event number. Here we call perf_event API to
  318. * emulate this action and create a kernel perf event for it.
  319. */
  320. void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
  321. u64 select_idx)
  322. {
  323. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  324. struct kvm_pmc *pmc = &pmu->pmc[select_idx];
  325. struct perf_event *event;
  326. struct perf_event_attr attr;
  327. u64 eventsel, counter;
  328. kvm_pmu_stop_counter(vcpu, pmc);
  329. eventsel = data & ARMV8_PMU_EVTYPE_EVENT;
  330. /* Software increment event does't need to be backed by a perf event */
  331. if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR &&
  332. select_idx != ARMV8_PMU_CYCLE_IDX)
  333. return;
  334. memset(&attr, 0, sizeof(struct perf_event_attr));
  335. attr.type = PERF_TYPE_RAW;
  336. attr.size = sizeof(attr);
  337. attr.pinned = 1;
  338. attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, select_idx);
  339. attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
  340. attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
  341. attr.exclude_hv = 1; /* Don't count EL2 events */
  342. attr.exclude_host = 1; /* Don't count host events */
  343. attr.config = (select_idx == ARMV8_PMU_CYCLE_IDX) ?
  344. ARMV8_PMUV3_PERFCTR_CPU_CYCLES : eventsel;
  345. counter = kvm_pmu_get_counter_value(vcpu, select_idx);
  346. /* The initial sample period (overflow count) of an event. */
  347. attr.sample_period = (-counter) & pmc->bitmask;
  348. event = perf_event_create_kernel_counter(&attr, -1, current,
  349. kvm_pmu_perf_overflow, pmc);
  350. if (IS_ERR(event)) {
  351. pr_err_once("kvm: pmu event creation failed %ld\n",
  352. PTR_ERR(event));
  353. return;
  354. }
  355. pmc->perf_event = event;
  356. }
  357. bool kvm_arm_support_pmu_v3(void)
  358. {
  359. /*
  360. * Check if HW_PERF_EVENTS are supported by checking the number of
  361. * hardware performance counters. This could ensure the presence of
  362. * a physical PMU and CONFIG_PERF_EVENT is selected.
  363. */
  364. return (perf_num_counters() > 0);
  365. }
  366. static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
  367. {
  368. if (!kvm_arm_support_pmu_v3())
  369. return -ENODEV;
  370. /*
  371. * We currently require an in-kernel VGIC to use the PMU emulation,
  372. * because we do not support forwarding PMU overflow interrupts to
  373. * userspace yet.
  374. */
  375. if (!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))
  376. return -ENODEV;
  377. if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features) ||
  378. !kvm_arm_pmu_irq_initialized(vcpu))
  379. return -ENXIO;
  380. if (kvm_arm_pmu_v3_ready(vcpu))
  381. return -EBUSY;
  382. kvm_pmu_vcpu_reset(vcpu);
  383. vcpu->arch.pmu.ready = true;
  384. return 0;
  385. }
  386. #define irq_is_ppi(irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS)
  387. /*
  388. * For one VM the interrupt type must be same for each vcpu.
  389. * As a PPI, the interrupt number is the same for all vcpus,
  390. * while as an SPI it must be a separate number per vcpu.
  391. */
  392. static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
  393. {
  394. int i;
  395. struct kvm_vcpu *vcpu;
  396. kvm_for_each_vcpu(i, vcpu, kvm) {
  397. if (!kvm_arm_pmu_irq_initialized(vcpu))
  398. continue;
  399. if (irq_is_ppi(irq)) {
  400. if (vcpu->arch.pmu.irq_num != irq)
  401. return false;
  402. } else {
  403. if (vcpu->arch.pmu.irq_num == irq)
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409. int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  410. {
  411. switch (attr->attr) {
  412. case KVM_ARM_VCPU_PMU_V3_IRQ: {
  413. int __user *uaddr = (int __user *)(long)attr->addr;
  414. int irq;
  415. if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
  416. return -ENODEV;
  417. if (get_user(irq, uaddr))
  418. return -EFAULT;
  419. /* The PMU overflow interrupt can be a PPI or a valid SPI. */
  420. if (!(irq_is_ppi(irq) || vgic_valid_spi(vcpu->kvm, irq)))
  421. return -EINVAL;
  422. if (!pmu_irq_is_valid(vcpu->kvm, irq))
  423. return -EINVAL;
  424. if (kvm_arm_pmu_irq_initialized(vcpu))
  425. return -EBUSY;
  426. kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
  427. vcpu->arch.pmu.irq_num = irq;
  428. return 0;
  429. }
  430. case KVM_ARM_VCPU_PMU_V3_INIT:
  431. return kvm_arm_pmu_v3_init(vcpu);
  432. }
  433. return -ENXIO;
  434. }
  435. int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  436. {
  437. switch (attr->attr) {
  438. case KVM_ARM_VCPU_PMU_V3_IRQ: {
  439. int __user *uaddr = (int __user *)(long)attr->addr;
  440. int irq;
  441. if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
  442. return -ENODEV;
  443. if (!kvm_arm_pmu_irq_initialized(vcpu))
  444. return -ENXIO;
  445. irq = vcpu->arch.pmu.irq_num;
  446. return put_user(irq, uaddr);
  447. }
  448. }
  449. return -ENXIO;
  450. }
  451. int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  452. {
  453. switch (attr->attr) {
  454. case KVM_ARM_VCPU_PMU_V3_IRQ:
  455. case KVM_ARM_VCPU_PMU_V3_INIT:
  456. if (kvm_arm_support_pmu_v3() &&
  457. test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
  458. return 0;
  459. }
  460. return -ENXIO;
  461. }