hyperv.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * KVM Microsoft Hyper-V emulation
  3. *
  4. * derived from arch/x86/kvm/x86.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  10. * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
  11. *
  12. * Authors:
  13. * Avi Kivity <avi@qumranet.com>
  14. * Yaniv Kamay <yaniv@qumranet.com>
  15. * Amit Shah <amit.shah@qumranet.com>
  16. * Ben-Ami Yassour <benami@il.ibm.com>
  17. * Andrey Smetanin <asmetanin@virtuozzo.com>
  18. *
  19. * This work is licensed under the terms of the GNU GPL, version 2. See
  20. * the COPYING file in the top-level directory.
  21. *
  22. */
  23. #include "x86.h"
  24. #include "lapic.h"
  25. #include "ioapic.h"
  26. #include "hyperv.h"
  27. #include <linux/kvm_host.h>
  28. #include <linux/highmem.h>
  29. #include <asm/apicdef.h>
  30. #include <trace/events/kvm.h>
  31. #include "trace.h"
  32. static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
  33. {
  34. return atomic64_read(&synic->sint[sint]);
  35. }
  36. static inline int synic_get_sint_vector(u64 sint_value)
  37. {
  38. if (sint_value & HV_SYNIC_SINT_MASKED)
  39. return -1;
  40. return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
  41. }
  42. static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
  43. int vector)
  44. {
  45. int i;
  46. for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
  47. if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
  48. return true;
  49. }
  50. return false;
  51. }
  52. static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
  53. int vector)
  54. {
  55. int i;
  56. u64 sint_value;
  57. for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
  58. sint_value = synic_read_sint(synic, i);
  59. if (synic_get_sint_vector(sint_value) == vector &&
  60. sint_value & HV_SYNIC_SINT_AUTO_EOI)
  61. return true;
  62. }
  63. return false;
  64. }
  65. static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data)
  66. {
  67. int vector;
  68. vector = data & HV_SYNIC_SINT_VECTOR_MASK;
  69. if (vector < 16)
  70. return 1;
  71. /*
  72. * Guest may configure multiple SINTs to use the same vector, so
  73. * we maintain a bitmap of vectors handled by synic, and a
  74. * bitmap of vectors with auto-eoi behavior. The bitmaps are
  75. * updated here, and atomically queried on fast paths.
  76. */
  77. atomic64_set(&synic->sint[sint], data);
  78. if (synic_has_vector_connected(synic, vector))
  79. __set_bit(vector, synic->vec_bitmap);
  80. else
  81. __clear_bit(vector, synic->vec_bitmap);
  82. if (synic_has_vector_auto_eoi(synic, vector))
  83. __set_bit(vector, synic->auto_eoi_bitmap);
  84. else
  85. __clear_bit(vector, synic->auto_eoi_bitmap);
  86. /* Load SynIC vectors into EOI exit bitmap */
  87. kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
  88. return 0;
  89. }
  90. static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
  91. {
  92. struct kvm_vcpu *vcpu;
  93. struct kvm_vcpu_hv_synic *synic;
  94. if (vcpu_id >= atomic_read(&kvm->online_vcpus))
  95. return NULL;
  96. vcpu = kvm_get_vcpu(kvm, vcpu_id);
  97. if (!vcpu)
  98. return NULL;
  99. synic = vcpu_to_synic(vcpu);
  100. return (synic->active) ? synic : NULL;
  101. }
  102. static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
  103. u32 sint)
  104. {
  105. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  106. struct page *page;
  107. gpa_t gpa;
  108. struct hv_message *msg;
  109. struct hv_message_page *msg_page;
  110. gpa = synic->msg_page & PAGE_MASK;
  111. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  112. if (is_error_page(page)) {
  113. vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
  114. gpa);
  115. return;
  116. }
  117. msg_page = kmap_atomic(page);
  118. msg = &msg_page->sint_message[sint];
  119. msg->header.message_flags.msg_pending = 0;
  120. kunmap_atomic(msg_page);
  121. kvm_release_page_dirty(page);
  122. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  123. }
  124. static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
  125. {
  126. struct kvm *kvm = vcpu->kvm;
  127. struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
  128. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  129. struct kvm_vcpu_hv_stimer *stimer;
  130. int gsi, idx, stimers_pending;
  131. vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
  132. if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
  133. synic_clear_sint_msg_pending(synic, sint);
  134. /* Try to deliver pending Hyper-V SynIC timers messages */
  135. stimers_pending = 0;
  136. for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
  137. stimer = &hv_vcpu->stimer[idx];
  138. if (stimer->msg_pending &&
  139. (stimer->config & HV_STIMER_ENABLE) &&
  140. HV_STIMER_SINT(stimer->config) == sint) {
  141. set_bit(stimer->index,
  142. hv_vcpu->stimer_pending_bitmap);
  143. stimers_pending++;
  144. }
  145. }
  146. if (stimers_pending)
  147. kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
  148. idx = srcu_read_lock(&kvm->irq_srcu);
  149. gsi = atomic_read(&synic->sint_to_gsi[sint]);
  150. if (gsi != -1)
  151. kvm_notify_acked_gsi(kvm, gsi);
  152. srcu_read_unlock(&kvm->irq_srcu, idx);
  153. }
  154. static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
  155. {
  156. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  157. struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
  158. hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
  159. hv_vcpu->exit.u.synic.msr = msr;
  160. hv_vcpu->exit.u.synic.control = synic->control;
  161. hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
  162. hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
  163. kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
  164. }
  165. static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
  166. u32 msr, u64 data, bool host)
  167. {
  168. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  169. int ret;
  170. if (!synic->active)
  171. return 1;
  172. vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
  173. msr, data, host);
  174. ret = 0;
  175. switch (msr) {
  176. case HV_X64_MSR_SCONTROL:
  177. synic->control = data;
  178. if (!host)
  179. synic_exit(synic, msr);
  180. break;
  181. case HV_X64_MSR_SVERSION:
  182. if (!host) {
  183. ret = 1;
  184. break;
  185. }
  186. synic->version = data;
  187. break;
  188. case HV_X64_MSR_SIEFP:
  189. if (data & HV_SYNIC_SIEFP_ENABLE)
  190. if (kvm_clear_guest(vcpu->kvm,
  191. data & PAGE_MASK, PAGE_SIZE)) {
  192. ret = 1;
  193. break;
  194. }
  195. synic->evt_page = data;
  196. if (!host)
  197. synic_exit(synic, msr);
  198. break;
  199. case HV_X64_MSR_SIMP:
  200. if (data & HV_SYNIC_SIMP_ENABLE)
  201. if (kvm_clear_guest(vcpu->kvm,
  202. data & PAGE_MASK, PAGE_SIZE)) {
  203. ret = 1;
  204. break;
  205. }
  206. synic->msg_page = data;
  207. if (!host)
  208. synic_exit(synic, msr);
  209. break;
  210. case HV_X64_MSR_EOM: {
  211. int i;
  212. for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
  213. kvm_hv_notify_acked_sint(vcpu, i);
  214. break;
  215. }
  216. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  217. ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data);
  218. break;
  219. default:
  220. ret = 1;
  221. break;
  222. }
  223. return ret;
  224. }
  225. static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
  226. {
  227. int ret;
  228. if (!synic->active)
  229. return 1;
  230. ret = 0;
  231. switch (msr) {
  232. case HV_X64_MSR_SCONTROL:
  233. *pdata = synic->control;
  234. break;
  235. case HV_X64_MSR_SVERSION:
  236. *pdata = synic->version;
  237. break;
  238. case HV_X64_MSR_SIEFP:
  239. *pdata = synic->evt_page;
  240. break;
  241. case HV_X64_MSR_SIMP:
  242. *pdata = synic->msg_page;
  243. break;
  244. case HV_X64_MSR_EOM:
  245. *pdata = 0;
  246. break;
  247. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  248. *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
  249. break;
  250. default:
  251. ret = 1;
  252. break;
  253. }
  254. return ret;
  255. }
  256. int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
  257. {
  258. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  259. struct kvm_lapic_irq irq;
  260. int ret, vector;
  261. if (sint >= ARRAY_SIZE(synic->sint))
  262. return -EINVAL;
  263. vector = synic_get_sint_vector(synic_read_sint(synic, sint));
  264. if (vector < 0)
  265. return -ENOENT;
  266. memset(&irq, 0, sizeof(irq));
  267. irq.dest_id = kvm_apic_id(vcpu->arch.apic);
  268. irq.dest_mode = APIC_DEST_PHYSICAL;
  269. irq.delivery_mode = APIC_DM_FIXED;
  270. irq.vector = vector;
  271. irq.level = 1;
  272. ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
  273. vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
  274. return ret;
  275. }
  276. int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
  277. {
  278. struct kvm_vcpu_hv_synic *synic;
  279. synic = synic_get(kvm, vcpu_id);
  280. if (!synic)
  281. return -EINVAL;
  282. return synic_set_irq(synic, sint);
  283. }
  284. void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
  285. {
  286. struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
  287. int i;
  288. vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
  289. for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
  290. if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
  291. kvm_hv_notify_acked_sint(vcpu, i);
  292. }
  293. static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
  294. {
  295. struct kvm_vcpu_hv_synic *synic;
  296. synic = synic_get(kvm, vcpu_id);
  297. if (!synic)
  298. return -EINVAL;
  299. if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
  300. return -EINVAL;
  301. atomic_set(&synic->sint_to_gsi[sint], gsi);
  302. return 0;
  303. }
  304. void kvm_hv_irq_routing_update(struct kvm *kvm)
  305. {
  306. struct kvm_irq_routing_table *irq_rt;
  307. struct kvm_kernel_irq_routing_entry *e;
  308. u32 gsi;
  309. irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
  310. lockdep_is_held(&kvm->irq_lock));
  311. for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
  312. hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
  313. if (e->type == KVM_IRQ_ROUTING_HV_SINT)
  314. kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
  315. e->hv_sint.sint, gsi);
  316. }
  317. }
  318. }
  319. static void synic_init(struct kvm_vcpu_hv_synic *synic)
  320. {
  321. int i;
  322. memset(synic, 0, sizeof(*synic));
  323. synic->version = HV_SYNIC_VERSION_1;
  324. for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
  325. atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
  326. atomic_set(&synic->sint_to_gsi[i], -1);
  327. }
  328. }
  329. static u64 get_time_ref_counter(struct kvm *kvm)
  330. {
  331. return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
  332. }
  333. static void stimer_mark_expired(struct kvm_vcpu_hv_stimer *stimer,
  334. bool vcpu_kick)
  335. {
  336. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  337. set_bit(stimer->index,
  338. vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
  339. kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
  340. if (vcpu_kick)
  341. kvm_vcpu_kick(vcpu);
  342. }
  343. static void stimer_stop(struct kvm_vcpu_hv_stimer *stimer)
  344. {
  345. hrtimer_cancel(&stimer->timer);
  346. }
  347. static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
  348. {
  349. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  350. stimer_stop(stimer);
  351. clear_bit(stimer->index,
  352. vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
  353. stimer->msg_pending = false;
  354. }
  355. static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
  356. {
  357. struct kvm_vcpu_hv_stimer *stimer;
  358. stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
  359. stimer_mark_expired(stimer, true);
  360. return HRTIMER_NORESTART;
  361. }
  362. static void stimer_restart(struct kvm_vcpu_hv_stimer *stimer)
  363. {
  364. u64 time_now;
  365. ktime_t ktime_now;
  366. u64 remainder;
  367. time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
  368. ktime_now = ktime_get();
  369. div64_u64_rem(time_now - stimer->exp_time, stimer->count, &remainder);
  370. stimer->exp_time = time_now + (stimer->count - remainder);
  371. hrtimer_start(&stimer->timer,
  372. ktime_add_ns(ktime_now,
  373. 100 * (stimer->exp_time - time_now)),
  374. HRTIMER_MODE_ABS);
  375. }
  376. static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
  377. {
  378. u64 time_now;
  379. ktime_t ktime_now;
  380. time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
  381. ktime_now = ktime_get();
  382. if (stimer->config & HV_STIMER_PERIODIC) {
  383. if (stimer->count == 0)
  384. return -EINVAL;
  385. stimer->exp_time = time_now + stimer->count;
  386. hrtimer_start(&stimer->timer,
  387. ktime_add_ns(ktime_now, 100 * stimer->count),
  388. HRTIMER_MODE_ABS);
  389. return 0;
  390. }
  391. stimer->exp_time = stimer->count;
  392. if (time_now >= stimer->count) {
  393. /*
  394. * Expire timer according to Hypervisor Top-Level Functional
  395. * specification v4(15.3.1):
  396. * "If a one shot is enabled and the specified count is in
  397. * the past, it will expire immediately."
  398. */
  399. stimer_mark_expired(stimer, false);
  400. return 0;
  401. }
  402. hrtimer_start(&stimer->timer,
  403. ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
  404. HRTIMER_MODE_ABS);
  405. return 0;
  406. }
  407. static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
  408. bool host)
  409. {
  410. if (stimer->count == 0 || HV_STIMER_SINT(config) == 0)
  411. config &= ~HV_STIMER_ENABLE;
  412. stimer->config = config;
  413. stimer_cleanup(stimer);
  414. if (stimer->config & HV_STIMER_ENABLE)
  415. if (stimer_start(stimer))
  416. return 1;
  417. return 0;
  418. }
  419. static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
  420. bool host)
  421. {
  422. stimer->count = count;
  423. stimer_cleanup(stimer);
  424. if (stimer->count == 0)
  425. stimer->config &= ~HV_STIMER_ENABLE;
  426. else if (stimer->config & HV_STIMER_AUTOENABLE) {
  427. stimer->config |= HV_STIMER_ENABLE;
  428. if (stimer_start(stimer))
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
  434. {
  435. *pconfig = stimer->config;
  436. return 0;
  437. }
  438. static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
  439. {
  440. *pcount = stimer->count;
  441. return 0;
  442. }
  443. static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
  444. struct hv_message *src_msg)
  445. {
  446. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  447. struct page *page;
  448. gpa_t gpa;
  449. struct hv_message *dst_msg;
  450. int r;
  451. struct hv_message_page *msg_page;
  452. if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
  453. return -ENOENT;
  454. gpa = synic->msg_page & PAGE_MASK;
  455. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  456. if (is_error_page(page))
  457. return -EFAULT;
  458. msg_page = kmap_atomic(page);
  459. dst_msg = &msg_page->sint_message[sint];
  460. if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
  461. src_msg->header.message_type) != HVMSG_NONE) {
  462. dst_msg->header.message_flags.msg_pending = 1;
  463. r = -EAGAIN;
  464. } else {
  465. memcpy(&dst_msg->u.payload, &src_msg->u.payload,
  466. src_msg->header.payload_size);
  467. dst_msg->header.message_type = src_msg->header.message_type;
  468. dst_msg->header.payload_size = src_msg->header.payload_size;
  469. r = synic_set_irq(synic, sint);
  470. if (r >= 1)
  471. r = 0;
  472. else if (r == 0)
  473. r = -EFAULT;
  474. }
  475. kunmap_atomic(msg_page);
  476. kvm_release_page_dirty(page);
  477. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  478. return r;
  479. }
  480. static void stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
  481. {
  482. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  483. struct hv_message *msg = &stimer->msg;
  484. struct hv_timer_message_payload *payload =
  485. (struct hv_timer_message_payload *)&msg->u.payload;
  486. int r;
  487. stimer->msg_pending = true;
  488. payload->expiration_time = stimer->exp_time;
  489. payload->delivery_time = get_time_ref_counter(vcpu->kvm);
  490. r = synic_deliver_msg(vcpu_to_synic(vcpu),
  491. HV_STIMER_SINT(stimer->config), msg);
  492. if (!r)
  493. stimer->msg_pending = false;
  494. }
  495. static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
  496. {
  497. stimer_send_msg(stimer);
  498. if (!(stimer->config & HV_STIMER_PERIODIC))
  499. stimer->config &= ~HV_STIMER_ENABLE;
  500. else
  501. stimer_restart(stimer);
  502. }
  503. void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
  504. {
  505. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  506. struct kvm_vcpu_hv_stimer *stimer;
  507. u64 time_now;
  508. int i;
  509. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  510. if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
  511. stimer = &hv_vcpu->stimer[i];
  512. if (stimer->config & HV_STIMER_ENABLE) {
  513. time_now = get_time_ref_counter(vcpu->kvm);
  514. if (time_now >= stimer->exp_time)
  515. stimer_expiration(stimer);
  516. }
  517. }
  518. }
  519. void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
  520. {
  521. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  522. int i;
  523. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  524. stimer_cleanup(&hv_vcpu->stimer[i]);
  525. }
  526. static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
  527. {
  528. struct hv_message *msg = &stimer->msg;
  529. struct hv_timer_message_payload *payload =
  530. (struct hv_timer_message_payload *)&msg->u.payload;
  531. memset(&msg->header, 0, sizeof(msg->header));
  532. msg->header.message_type = HVMSG_TIMER_EXPIRED;
  533. msg->header.payload_size = sizeof(*payload);
  534. payload->timer_index = stimer->index;
  535. payload->expiration_time = 0;
  536. payload->delivery_time = 0;
  537. }
  538. static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
  539. {
  540. memset(stimer, 0, sizeof(*stimer));
  541. stimer->index = timer_index;
  542. hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  543. stimer->timer.function = stimer_timer_callback;
  544. stimer_prepare_msg(stimer);
  545. }
  546. void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
  547. {
  548. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  549. int i;
  550. synic_init(&hv_vcpu->synic);
  551. bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
  552. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  553. stimer_init(&hv_vcpu->stimer[i], i);
  554. }
  555. int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
  556. {
  557. /*
  558. * Hyper-V SynIC auto EOI SINT's are
  559. * not compatible with APICV, so deactivate APICV
  560. */
  561. kvm_vcpu_deactivate_apicv(vcpu);
  562. vcpu_to_synic(vcpu)->active = true;
  563. return 0;
  564. }
  565. static bool kvm_hv_msr_partition_wide(u32 msr)
  566. {
  567. bool r = false;
  568. switch (msr) {
  569. case HV_X64_MSR_GUEST_OS_ID:
  570. case HV_X64_MSR_HYPERCALL:
  571. case HV_X64_MSR_REFERENCE_TSC:
  572. case HV_X64_MSR_TIME_REF_COUNT:
  573. case HV_X64_MSR_CRASH_CTL:
  574. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  575. case HV_X64_MSR_RESET:
  576. r = true;
  577. break;
  578. }
  579. return r;
  580. }
  581. static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
  582. u32 index, u64 *pdata)
  583. {
  584. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  585. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  586. return -EINVAL;
  587. *pdata = hv->hv_crash_param[index];
  588. return 0;
  589. }
  590. static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
  591. {
  592. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  593. *pdata = hv->hv_crash_ctl;
  594. return 0;
  595. }
  596. static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
  597. {
  598. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  599. if (host)
  600. hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
  601. if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
  602. vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
  603. hv->hv_crash_param[0],
  604. hv->hv_crash_param[1],
  605. hv->hv_crash_param[2],
  606. hv->hv_crash_param[3],
  607. hv->hv_crash_param[4]);
  608. /* Send notification about crash to user space */
  609. kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
  610. }
  611. return 0;
  612. }
  613. static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
  614. u32 index, u64 data)
  615. {
  616. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  617. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  618. return -EINVAL;
  619. hv->hv_crash_param[index] = data;
  620. return 0;
  621. }
  622. static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
  623. bool host)
  624. {
  625. struct kvm *kvm = vcpu->kvm;
  626. struct kvm_hv *hv = &kvm->arch.hyperv;
  627. switch (msr) {
  628. case HV_X64_MSR_GUEST_OS_ID:
  629. hv->hv_guest_os_id = data;
  630. /* setting guest os id to zero disables hypercall page */
  631. if (!hv->hv_guest_os_id)
  632. hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  633. break;
  634. case HV_X64_MSR_HYPERCALL: {
  635. u64 gfn;
  636. unsigned long addr;
  637. u8 instructions[4];
  638. /* if guest os id is not set hypercall should remain disabled */
  639. if (!hv->hv_guest_os_id)
  640. break;
  641. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  642. hv->hv_hypercall = data;
  643. break;
  644. }
  645. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  646. addr = gfn_to_hva(kvm, gfn);
  647. if (kvm_is_error_hva(addr))
  648. return 1;
  649. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  650. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  651. if (__copy_to_user((void __user *)addr, instructions, 4))
  652. return 1;
  653. hv->hv_hypercall = data;
  654. mark_page_dirty(kvm, gfn);
  655. break;
  656. }
  657. case HV_X64_MSR_REFERENCE_TSC: {
  658. u64 gfn;
  659. HV_REFERENCE_TSC_PAGE tsc_ref;
  660. memset(&tsc_ref, 0, sizeof(tsc_ref));
  661. hv->hv_tsc_page = data;
  662. if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
  663. break;
  664. gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
  665. if (kvm_write_guest(
  666. kvm,
  667. gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
  668. &tsc_ref, sizeof(tsc_ref)))
  669. return 1;
  670. mark_page_dirty(kvm, gfn);
  671. break;
  672. }
  673. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  674. return kvm_hv_msr_set_crash_data(vcpu,
  675. msr - HV_X64_MSR_CRASH_P0,
  676. data);
  677. case HV_X64_MSR_CRASH_CTL:
  678. return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
  679. case HV_X64_MSR_RESET:
  680. if (data == 1) {
  681. vcpu_debug(vcpu, "hyper-v reset requested\n");
  682. kvm_make_request(KVM_REQ_HV_RESET, vcpu);
  683. }
  684. break;
  685. default:
  686. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  687. msr, data);
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. /* Calculate cpu time spent by current task in 100ns units */
  693. static u64 current_task_runtime_100ns(void)
  694. {
  695. cputime_t utime, stime;
  696. task_cputime_adjusted(current, &utime, &stime);
  697. return div_u64(cputime_to_nsecs(utime + stime), 100);
  698. }
  699. static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  700. {
  701. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  702. switch (msr) {
  703. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  704. u64 gfn;
  705. unsigned long addr;
  706. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  707. hv->hv_vapic = data;
  708. if (kvm_lapic_enable_pv_eoi(vcpu, 0))
  709. return 1;
  710. break;
  711. }
  712. gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
  713. addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
  714. if (kvm_is_error_hva(addr))
  715. return 1;
  716. if (__clear_user((void __user *)addr, PAGE_SIZE))
  717. return 1;
  718. hv->hv_vapic = data;
  719. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  720. if (kvm_lapic_enable_pv_eoi(vcpu,
  721. gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
  722. return 1;
  723. break;
  724. }
  725. case HV_X64_MSR_EOI:
  726. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  727. case HV_X64_MSR_ICR:
  728. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  729. case HV_X64_MSR_TPR:
  730. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  731. case HV_X64_MSR_VP_RUNTIME:
  732. if (!host)
  733. return 1;
  734. hv->runtime_offset = data - current_task_runtime_100ns();
  735. break;
  736. case HV_X64_MSR_SCONTROL:
  737. case HV_X64_MSR_SVERSION:
  738. case HV_X64_MSR_SIEFP:
  739. case HV_X64_MSR_SIMP:
  740. case HV_X64_MSR_EOM:
  741. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  742. return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
  743. case HV_X64_MSR_STIMER0_CONFIG:
  744. case HV_X64_MSR_STIMER1_CONFIG:
  745. case HV_X64_MSR_STIMER2_CONFIG:
  746. case HV_X64_MSR_STIMER3_CONFIG: {
  747. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  748. return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
  749. data, host);
  750. }
  751. case HV_X64_MSR_STIMER0_COUNT:
  752. case HV_X64_MSR_STIMER1_COUNT:
  753. case HV_X64_MSR_STIMER2_COUNT:
  754. case HV_X64_MSR_STIMER3_COUNT: {
  755. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  756. return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
  757. data, host);
  758. }
  759. default:
  760. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  761. msr, data);
  762. return 1;
  763. }
  764. return 0;
  765. }
  766. static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  767. {
  768. u64 data = 0;
  769. struct kvm *kvm = vcpu->kvm;
  770. struct kvm_hv *hv = &kvm->arch.hyperv;
  771. switch (msr) {
  772. case HV_X64_MSR_GUEST_OS_ID:
  773. data = hv->hv_guest_os_id;
  774. break;
  775. case HV_X64_MSR_HYPERCALL:
  776. data = hv->hv_hypercall;
  777. break;
  778. case HV_X64_MSR_TIME_REF_COUNT:
  779. data = get_time_ref_counter(kvm);
  780. break;
  781. case HV_X64_MSR_REFERENCE_TSC:
  782. data = hv->hv_tsc_page;
  783. break;
  784. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  785. return kvm_hv_msr_get_crash_data(vcpu,
  786. msr - HV_X64_MSR_CRASH_P0,
  787. pdata);
  788. case HV_X64_MSR_CRASH_CTL:
  789. return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
  790. case HV_X64_MSR_RESET:
  791. data = 0;
  792. break;
  793. default:
  794. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  795. return 1;
  796. }
  797. *pdata = data;
  798. return 0;
  799. }
  800. static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  801. {
  802. u64 data = 0;
  803. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  804. switch (msr) {
  805. case HV_X64_MSR_VP_INDEX: {
  806. int r;
  807. struct kvm_vcpu *v;
  808. kvm_for_each_vcpu(r, v, vcpu->kvm) {
  809. if (v == vcpu) {
  810. data = r;
  811. break;
  812. }
  813. }
  814. break;
  815. }
  816. case HV_X64_MSR_EOI:
  817. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  818. case HV_X64_MSR_ICR:
  819. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  820. case HV_X64_MSR_TPR:
  821. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  822. case HV_X64_MSR_APIC_ASSIST_PAGE:
  823. data = hv->hv_vapic;
  824. break;
  825. case HV_X64_MSR_VP_RUNTIME:
  826. data = current_task_runtime_100ns() + hv->runtime_offset;
  827. break;
  828. case HV_X64_MSR_SCONTROL:
  829. case HV_X64_MSR_SVERSION:
  830. case HV_X64_MSR_SIEFP:
  831. case HV_X64_MSR_SIMP:
  832. case HV_X64_MSR_EOM:
  833. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  834. return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
  835. case HV_X64_MSR_STIMER0_CONFIG:
  836. case HV_X64_MSR_STIMER1_CONFIG:
  837. case HV_X64_MSR_STIMER2_CONFIG:
  838. case HV_X64_MSR_STIMER3_CONFIG: {
  839. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  840. return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
  841. pdata);
  842. }
  843. case HV_X64_MSR_STIMER0_COUNT:
  844. case HV_X64_MSR_STIMER1_COUNT:
  845. case HV_X64_MSR_STIMER2_COUNT:
  846. case HV_X64_MSR_STIMER3_COUNT: {
  847. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  848. return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
  849. pdata);
  850. }
  851. default:
  852. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  853. return 1;
  854. }
  855. *pdata = data;
  856. return 0;
  857. }
  858. int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  859. {
  860. if (kvm_hv_msr_partition_wide(msr)) {
  861. int r;
  862. mutex_lock(&vcpu->kvm->lock);
  863. r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
  864. mutex_unlock(&vcpu->kvm->lock);
  865. return r;
  866. } else
  867. return kvm_hv_set_msr(vcpu, msr, data, host);
  868. }
  869. int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  870. {
  871. if (kvm_hv_msr_partition_wide(msr)) {
  872. int r;
  873. mutex_lock(&vcpu->kvm->lock);
  874. r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
  875. mutex_unlock(&vcpu->kvm->lock);
  876. return r;
  877. } else
  878. return kvm_hv_get_msr(vcpu, msr, pdata);
  879. }
  880. bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  881. {
  882. return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  883. }
  884. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  885. {
  886. u64 param, ingpa, outgpa, ret;
  887. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  888. bool fast, longmode;
  889. /*
  890. * hypercall generates UD from non zero cpl and real mode
  891. * per HYPER-V spec
  892. */
  893. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  894. kvm_queue_exception(vcpu, UD_VECTOR);
  895. return 0;
  896. }
  897. longmode = is_64_bit_mode(vcpu);
  898. if (!longmode) {
  899. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  900. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  901. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  902. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  903. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  904. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  905. }
  906. #ifdef CONFIG_X86_64
  907. else {
  908. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  909. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  910. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  911. }
  912. #endif
  913. code = param & 0xffff;
  914. fast = (param >> 16) & 0x1;
  915. rep_cnt = (param >> 32) & 0xfff;
  916. rep_idx = (param >> 48) & 0xfff;
  917. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  918. switch (code) {
  919. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  920. kvm_vcpu_on_spin(vcpu);
  921. break;
  922. default:
  923. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  924. break;
  925. }
  926. ret = res | (((u64)rep_done & 0xfff) << 32);
  927. if (longmode) {
  928. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  929. } else {
  930. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  931. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  932. }
  933. return 1;
  934. }