hyperv.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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,
  66. u64 data, bool host)
  67. {
  68. int vector;
  69. vector = data & HV_SYNIC_SINT_VECTOR_MASK;
  70. if (vector < 16 && !host)
  71. return 1;
  72. /*
  73. * Guest may configure multiple SINTs to use the same vector, so
  74. * we maintain a bitmap of vectors handled by synic, and a
  75. * bitmap of vectors with auto-eoi behavior. The bitmaps are
  76. * updated here, and atomically queried on fast paths.
  77. */
  78. atomic64_set(&synic->sint[sint], data);
  79. if (synic_has_vector_connected(synic, vector))
  80. __set_bit(vector, synic->vec_bitmap);
  81. else
  82. __clear_bit(vector, synic->vec_bitmap);
  83. if (synic_has_vector_auto_eoi(synic, vector))
  84. __set_bit(vector, synic->auto_eoi_bitmap);
  85. else
  86. __clear_bit(vector, synic->auto_eoi_bitmap);
  87. /* Load SynIC vectors into EOI exit bitmap */
  88. kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
  89. return 0;
  90. }
  91. static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
  92. {
  93. struct kvm_vcpu *vcpu;
  94. struct kvm_vcpu_hv_synic *synic;
  95. if (vcpu_id >= atomic_read(&kvm->online_vcpus))
  96. return NULL;
  97. vcpu = kvm_get_vcpu(kvm, vcpu_id);
  98. if (!vcpu)
  99. return NULL;
  100. synic = vcpu_to_synic(vcpu);
  101. return (synic->active) ? synic : NULL;
  102. }
  103. static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
  104. u32 sint)
  105. {
  106. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  107. struct page *page;
  108. gpa_t gpa;
  109. struct hv_message *msg;
  110. struct hv_message_page *msg_page;
  111. gpa = synic->msg_page & PAGE_MASK;
  112. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  113. if (is_error_page(page)) {
  114. vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
  115. gpa);
  116. return;
  117. }
  118. msg_page = kmap_atomic(page);
  119. msg = &msg_page->sint_message[sint];
  120. msg->header.message_flags.msg_pending = 0;
  121. kunmap_atomic(msg_page);
  122. kvm_release_page_dirty(page);
  123. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  124. }
  125. static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
  126. {
  127. struct kvm *kvm = vcpu->kvm;
  128. struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
  129. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  130. struct kvm_vcpu_hv_stimer *stimer;
  131. int gsi, idx, stimers_pending;
  132. trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
  133. if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
  134. synic_clear_sint_msg_pending(synic, sint);
  135. /* Try to deliver pending Hyper-V SynIC timers messages */
  136. stimers_pending = 0;
  137. for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
  138. stimer = &hv_vcpu->stimer[idx];
  139. if (stimer->msg_pending &&
  140. (stimer->config & HV_STIMER_ENABLE) &&
  141. HV_STIMER_SINT(stimer->config) == sint) {
  142. set_bit(stimer->index,
  143. hv_vcpu->stimer_pending_bitmap);
  144. stimers_pending++;
  145. }
  146. }
  147. if (stimers_pending)
  148. kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
  149. idx = srcu_read_lock(&kvm->irq_srcu);
  150. gsi = atomic_read(&synic->sint_to_gsi[sint]);
  151. if (gsi != -1)
  152. kvm_notify_acked_gsi(kvm, gsi);
  153. srcu_read_unlock(&kvm->irq_srcu, idx);
  154. }
  155. static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
  156. {
  157. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  158. struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
  159. hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
  160. hv_vcpu->exit.u.synic.msr = msr;
  161. hv_vcpu->exit.u.synic.control = synic->control;
  162. hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
  163. hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
  164. kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
  165. }
  166. static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
  167. u32 msr, u64 data, bool host)
  168. {
  169. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  170. int ret;
  171. if (!synic->active)
  172. return 1;
  173. trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, 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, host);
  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. trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, 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. trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, 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. struct kvm_hv *hv = &kvm->arch.hyperv;
  332. struct kvm_vcpu *vcpu;
  333. u64 tsc;
  334. /*
  335. * The guest has not set up the TSC page or the clock isn't
  336. * stable, fall back to get_kvmclock_ns.
  337. */
  338. if (!hv->tsc_ref.tsc_sequence)
  339. return div_u64(get_kvmclock_ns(kvm), 100);
  340. vcpu = kvm_get_vcpu(kvm, 0);
  341. tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  342. return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
  343. + hv->tsc_ref.tsc_offset;
  344. }
  345. static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
  346. bool vcpu_kick)
  347. {
  348. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  349. set_bit(stimer->index,
  350. vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
  351. kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
  352. if (vcpu_kick)
  353. kvm_vcpu_kick(vcpu);
  354. }
  355. static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
  356. {
  357. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  358. trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
  359. stimer->index);
  360. hrtimer_cancel(&stimer->timer);
  361. clear_bit(stimer->index,
  362. vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
  363. stimer->msg_pending = false;
  364. stimer->exp_time = 0;
  365. }
  366. static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
  367. {
  368. struct kvm_vcpu_hv_stimer *stimer;
  369. stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
  370. trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
  371. stimer->index);
  372. stimer_mark_pending(stimer, true);
  373. return HRTIMER_NORESTART;
  374. }
  375. /*
  376. * stimer_start() assumptions:
  377. * a) stimer->count is not equal to 0
  378. * b) stimer->config has HV_STIMER_ENABLE flag
  379. */
  380. static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
  381. {
  382. u64 time_now;
  383. ktime_t ktime_now;
  384. time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
  385. ktime_now = ktime_get();
  386. if (stimer->config & HV_STIMER_PERIODIC) {
  387. if (stimer->exp_time) {
  388. if (time_now >= stimer->exp_time) {
  389. u64 remainder;
  390. div64_u64_rem(time_now - stimer->exp_time,
  391. stimer->count, &remainder);
  392. stimer->exp_time =
  393. time_now + (stimer->count - remainder);
  394. }
  395. } else
  396. stimer->exp_time = time_now + stimer->count;
  397. trace_kvm_hv_stimer_start_periodic(
  398. stimer_to_vcpu(stimer)->vcpu_id,
  399. stimer->index,
  400. time_now, stimer->exp_time);
  401. hrtimer_start(&stimer->timer,
  402. ktime_add_ns(ktime_now,
  403. 100 * (stimer->exp_time - time_now)),
  404. HRTIMER_MODE_ABS);
  405. return 0;
  406. }
  407. stimer->exp_time = stimer->count;
  408. if (time_now >= stimer->count) {
  409. /*
  410. * Expire timer according to Hypervisor Top-Level Functional
  411. * specification v4(15.3.1):
  412. * "If a one shot is enabled and the specified count is in
  413. * the past, it will expire immediately."
  414. */
  415. stimer_mark_pending(stimer, false);
  416. return 0;
  417. }
  418. trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
  419. stimer->index,
  420. time_now, stimer->count);
  421. hrtimer_start(&stimer->timer,
  422. ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
  423. HRTIMER_MODE_ABS);
  424. return 0;
  425. }
  426. static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
  427. bool host)
  428. {
  429. trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
  430. stimer->index, config, host);
  431. stimer_cleanup(stimer);
  432. if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
  433. config &= ~HV_STIMER_ENABLE;
  434. stimer->config = config;
  435. stimer_mark_pending(stimer, false);
  436. return 0;
  437. }
  438. static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
  439. bool host)
  440. {
  441. trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
  442. stimer->index, count, host);
  443. stimer_cleanup(stimer);
  444. stimer->count = count;
  445. if (stimer->count == 0)
  446. stimer->config &= ~HV_STIMER_ENABLE;
  447. else if (stimer->config & HV_STIMER_AUTOENABLE)
  448. stimer->config |= HV_STIMER_ENABLE;
  449. stimer_mark_pending(stimer, false);
  450. return 0;
  451. }
  452. static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
  453. {
  454. *pconfig = stimer->config;
  455. return 0;
  456. }
  457. static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
  458. {
  459. *pcount = stimer->count;
  460. return 0;
  461. }
  462. static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
  463. struct hv_message *src_msg)
  464. {
  465. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  466. struct page *page;
  467. gpa_t gpa;
  468. struct hv_message *dst_msg;
  469. int r;
  470. struct hv_message_page *msg_page;
  471. if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
  472. return -ENOENT;
  473. gpa = synic->msg_page & PAGE_MASK;
  474. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  475. if (is_error_page(page))
  476. return -EFAULT;
  477. msg_page = kmap_atomic(page);
  478. dst_msg = &msg_page->sint_message[sint];
  479. if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
  480. src_msg->header.message_type) != HVMSG_NONE) {
  481. dst_msg->header.message_flags.msg_pending = 1;
  482. r = -EAGAIN;
  483. } else {
  484. memcpy(&dst_msg->u.payload, &src_msg->u.payload,
  485. src_msg->header.payload_size);
  486. dst_msg->header.message_type = src_msg->header.message_type;
  487. dst_msg->header.payload_size = src_msg->header.payload_size;
  488. r = synic_set_irq(synic, sint);
  489. if (r >= 1)
  490. r = 0;
  491. else if (r == 0)
  492. r = -EFAULT;
  493. }
  494. kunmap_atomic(msg_page);
  495. kvm_release_page_dirty(page);
  496. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  497. return r;
  498. }
  499. static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
  500. {
  501. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  502. struct hv_message *msg = &stimer->msg;
  503. struct hv_timer_message_payload *payload =
  504. (struct hv_timer_message_payload *)&msg->u.payload;
  505. payload->expiration_time = stimer->exp_time;
  506. payload->delivery_time = get_time_ref_counter(vcpu->kvm);
  507. return synic_deliver_msg(vcpu_to_synic(vcpu),
  508. HV_STIMER_SINT(stimer->config), msg);
  509. }
  510. static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
  511. {
  512. int r;
  513. stimer->msg_pending = true;
  514. r = stimer_send_msg(stimer);
  515. trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
  516. stimer->index, r);
  517. if (!r) {
  518. stimer->msg_pending = false;
  519. if (!(stimer->config & HV_STIMER_PERIODIC))
  520. stimer->config &= ~HV_STIMER_ENABLE;
  521. }
  522. }
  523. void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
  524. {
  525. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  526. struct kvm_vcpu_hv_stimer *stimer;
  527. u64 time_now, exp_time;
  528. int i;
  529. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  530. if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
  531. stimer = &hv_vcpu->stimer[i];
  532. if (stimer->config & HV_STIMER_ENABLE) {
  533. exp_time = stimer->exp_time;
  534. if (exp_time) {
  535. time_now =
  536. get_time_ref_counter(vcpu->kvm);
  537. if (time_now >= exp_time)
  538. stimer_expiration(stimer);
  539. }
  540. if ((stimer->config & HV_STIMER_ENABLE) &&
  541. stimer->count)
  542. stimer_start(stimer);
  543. else
  544. stimer_cleanup(stimer);
  545. }
  546. }
  547. }
  548. void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
  549. {
  550. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  551. int i;
  552. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  553. stimer_cleanup(&hv_vcpu->stimer[i]);
  554. }
  555. static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
  556. {
  557. struct hv_message *msg = &stimer->msg;
  558. struct hv_timer_message_payload *payload =
  559. (struct hv_timer_message_payload *)&msg->u.payload;
  560. memset(&msg->header, 0, sizeof(msg->header));
  561. msg->header.message_type = HVMSG_TIMER_EXPIRED;
  562. msg->header.payload_size = sizeof(*payload);
  563. payload->timer_index = stimer->index;
  564. payload->expiration_time = 0;
  565. payload->delivery_time = 0;
  566. }
  567. static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
  568. {
  569. memset(stimer, 0, sizeof(*stimer));
  570. stimer->index = timer_index;
  571. hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  572. stimer->timer.function = stimer_timer_callback;
  573. stimer_prepare_msg(stimer);
  574. }
  575. void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
  576. {
  577. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  578. int i;
  579. synic_init(&hv_vcpu->synic);
  580. bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
  581. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  582. stimer_init(&hv_vcpu->stimer[i], i);
  583. }
  584. int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
  585. {
  586. /*
  587. * Hyper-V SynIC auto EOI SINT's are
  588. * not compatible with APICV, so deactivate APICV
  589. */
  590. kvm_vcpu_deactivate_apicv(vcpu);
  591. vcpu_to_synic(vcpu)->active = true;
  592. return 0;
  593. }
  594. static bool kvm_hv_msr_partition_wide(u32 msr)
  595. {
  596. bool r = false;
  597. switch (msr) {
  598. case HV_X64_MSR_GUEST_OS_ID:
  599. case HV_X64_MSR_HYPERCALL:
  600. case HV_X64_MSR_REFERENCE_TSC:
  601. case HV_X64_MSR_TIME_REF_COUNT:
  602. case HV_X64_MSR_CRASH_CTL:
  603. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  604. case HV_X64_MSR_RESET:
  605. r = true;
  606. break;
  607. }
  608. return r;
  609. }
  610. static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
  611. u32 index, u64 *pdata)
  612. {
  613. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  614. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  615. return -EINVAL;
  616. *pdata = hv->hv_crash_param[index];
  617. return 0;
  618. }
  619. static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
  620. {
  621. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  622. *pdata = hv->hv_crash_ctl;
  623. return 0;
  624. }
  625. static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
  626. {
  627. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  628. if (host)
  629. hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
  630. if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
  631. vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
  632. hv->hv_crash_param[0],
  633. hv->hv_crash_param[1],
  634. hv->hv_crash_param[2],
  635. hv->hv_crash_param[3],
  636. hv->hv_crash_param[4]);
  637. /* Send notification about crash to user space */
  638. kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
  639. }
  640. return 0;
  641. }
  642. static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
  643. u32 index, u64 data)
  644. {
  645. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  646. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  647. return -EINVAL;
  648. hv->hv_crash_param[index] = data;
  649. return 0;
  650. }
  651. /*
  652. * The kvmclock and Hyper-V TSC page use similar formulas, and converting
  653. * between them is possible:
  654. *
  655. * kvmclock formula:
  656. * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
  657. * + system_time
  658. *
  659. * Hyper-V formula:
  660. * nsec/100 = ticks * scale / 2^64 + offset
  661. *
  662. * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
  663. * By dividing the kvmclock formula by 100 and equating what's left we get:
  664. * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
  665. * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
  666. * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
  667. *
  668. * Now expand the kvmclock formula and divide by 100:
  669. * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
  670. * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
  671. * + system_time
  672. * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
  673. * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
  674. * + system_time / 100
  675. *
  676. * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
  677. * nsec/100 = ticks * scale / 2^64
  678. * - tsc_timestamp * scale / 2^64
  679. * + system_time / 100
  680. *
  681. * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
  682. * offset = system_time / 100 - tsc_timestamp * scale / 2^64
  683. *
  684. * These two equivalencies are implemented in this function.
  685. */
  686. static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
  687. HV_REFERENCE_TSC_PAGE *tsc_ref)
  688. {
  689. u64 max_mul;
  690. if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
  691. return false;
  692. /*
  693. * check if scale would overflow, if so we use the time ref counter
  694. * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
  695. * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
  696. * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
  697. */
  698. max_mul = 100ull << (32 - hv_clock->tsc_shift);
  699. if (hv_clock->tsc_to_system_mul >= max_mul)
  700. return false;
  701. /*
  702. * Otherwise compute the scale and offset according to the formulas
  703. * derived above.
  704. */
  705. tsc_ref->tsc_scale =
  706. mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
  707. hv_clock->tsc_to_system_mul,
  708. 100);
  709. tsc_ref->tsc_offset = hv_clock->system_time;
  710. do_div(tsc_ref->tsc_offset, 100);
  711. tsc_ref->tsc_offset -=
  712. mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
  713. return true;
  714. }
  715. void kvm_hv_setup_tsc_page(struct kvm *kvm,
  716. struct pvclock_vcpu_time_info *hv_clock)
  717. {
  718. struct kvm_hv *hv = &kvm->arch.hyperv;
  719. u32 tsc_seq;
  720. u64 gfn;
  721. BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
  722. BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
  723. if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
  724. return;
  725. gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
  726. /*
  727. * Because the TSC parameters only vary when there is a
  728. * change in the master clock, do not bother with caching.
  729. */
  730. if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
  731. &tsc_seq, sizeof(tsc_seq))))
  732. return;
  733. /*
  734. * While we're computing and writing the parameters, force the
  735. * guest to use the time reference count MSR.
  736. */
  737. hv->tsc_ref.tsc_sequence = 0;
  738. if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
  739. &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
  740. return;
  741. if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
  742. return;
  743. /* Ensure sequence is zero before writing the rest of the struct. */
  744. smp_wmb();
  745. if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
  746. return;
  747. /*
  748. * Now switch to the TSC page mechanism by writing the sequence.
  749. */
  750. tsc_seq++;
  751. if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
  752. tsc_seq = 1;
  753. /* Write the struct entirely before the non-zero sequence. */
  754. smp_wmb();
  755. hv->tsc_ref.tsc_sequence = tsc_seq;
  756. kvm_write_guest(kvm, gfn_to_gpa(gfn),
  757. &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
  758. }
  759. static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
  760. bool host)
  761. {
  762. struct kvm *kvm = vcpu->kvm;
  763. struct kvm_hv *hv = &kvm->arch.hyperv;
  764. switch (msr) {
  765. case HV_X64_MSR_GUEST_OS_ID:
  766. hv->hv_guest_os_id = data;
  767. /* setting guest os id to zero disables hypercall page */
  768. if (!hv->hv_guest_os_id)
  769. hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  770. break;
  771. case HV_X64_MSR_HYPERCALL: {
  772. u64 gfn;
  773. unsigned long addr;
  774. u8 instructions[4];
  775. /* if guest os id is not set hypercall should remain disabled */
  776. if (!hv->hv_guest_os_id)
  777. break;
  778. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  779. hv->hv_hypercall = data;
  780. break;
  781. }
  782. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  783. addr = gfn_to_hva(kvm, gfn);
  784. if (kvm_is_error_hva(addr))
  785. return 1;
  786. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  787. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  788. if (__copy_to_user((void __user *)addr, instructions, 4))
  789. return 1;
  790. hv->hv_hypercall = data;
  791. mark_page_dirty(kvm, gfn);
  792. break;
  793. }
  794. case HV_X64_MSR_REFERENCE_TSC:
  795. hv->hv_tsc_page = data;
  796. if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
  797. kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
  798. break;
  799. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  800. return kvm_hv_msr_set_crash_data(vcpu,
  801. msr - HV_X64_MSR_CRASH_P0,
  802. data);
  803. case HV_X64_MSR_CRASH_CTL:
  804. return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
  805. case HV_X64_MSR_RESET:
  806. if (data == 1) {
  807. vcpu_debug(vcpu, "hyper-v reset requested\n");
  808. kvm_make_request(KVM_REQ_HV_RESET, vcpu);
  809. }
  810. break;
  811. default:
  812. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  813. msr, data);
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. /* Calculate cpu time spent by current task in 100ns units */
  819. static u64 current_task_runtime_100ns(void)
  820. {
  821. cputime_t utime, stime;
  822. task_cputime_adjusted(current, &utime, &stime);
  823. return div_u64(cputime_to_nsecs(utime + stime), 100);
  824. }
  825. static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  826. {
  827. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  828. switch (msr) {
  829. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  830. u64 gfn;
  831. unsigned long addr;
  832. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  833. hv->hv_vapic = data;
  834. if (kvm_lapic_enable_pv_eoi(vcpu, 0))
  835. return 1;
  836. break;
  837. }
  838. gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
  839. addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
  840. if (kvm_is_error_hva(addr))
  841. return 1;
  842. if (__clear_user((void __user *)addr, PAGE_SIZE))
  843. return 1;
  844. hv->hv_vapic = data;
  845. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  846. if (kvm_lapic_enable_pv_eoi(vcpu,
  847. gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
  848. return 1;
  849. break;
  850. }
  851. case HV_X64_MSR_EOI:
  852. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  853. case HV_X64_MSR_ICR:
  854. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  855. case HV_X64_MSR_TPR:
  856. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  857. case HV_X64_MSR_VP_RUNTIME:
  858. if (!host)
  859. return 1;
  860. hv->runtime_offset = data - current_task_runtime_100ns();
  861. break;
  862. case HV_X64_MSR_SCONTROL:
  863. case HV_X64_MSR_SVERSION:
  864. case HV_X64_MSR_SIEFP:
  865. case HV_X64_MSR_SIMP:
  866. case HV_X64_MSR_EOM:
  867. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  868. return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
  869. case HV_X64_MSR_STIMER0_CONFIG:
  870. case HV_X64_MSR_STIMER1_CONFIG:
  871. case HV_X64_MSR_STIMER2_CONFIG:
  872. case HV_X64_MSR_STIMER3_CONFIG: {
  873. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  874. return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
  875. data, host);
  876. }
  877. case HV_X64_MSR_STIMER0_COUNT:
  878. case HV_X64_MSR_STIMER1_COUNT:
  879. case HV_X64_MSR_STIMER2_COUNT:
  880. case HV_X64_MSR_STIMER3_COUNT: {
  881. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  882. return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
  883. data, host);
  884. }
  885. default:
  886. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  887. msr, data);
  888. return 1;
  889. }
  890. return 0;
  891. }
  892. static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  893. {
  894. u64 data = 0;
  895. struct kvm *kvm = vcpu->kvm;
  896. struct kvm_hv *hv = &kvm->arch.hyperv;
  897. switch (msr) {
  898. case HV_X64_MSR_GUEST_OS_ID:
  899. data = hv->hv_guest_os_id;
  900. break;
  901. case HV_X64_MSR_HYPERCALL:
  902. data = hv->hv_hypercall;
  903. break;
  904. case HV_X64_MSR_TIME_REF_COUNT:
  905. data = get_time_ref_counter(kvm);
  906. break;
  907. case HV_X64_MSR_REFERENCE_TSC:
  908. data = hv->hv_tsc_page;
  909. break;
  910. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  911. return kvm_hv_msr_get_crash_data(vcpu,
  912. msr - HV_X64_MSR_CRASH_P0,
  913. pdata);
  914. case HV_X64_MSR_CRASH_CTL:
  915. return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
  916. case HV_X64_MSR_RESET:
  917. data = 0;
  918. break;
  919. default:
  920. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  921. return 1;
  922. }
  923. *pdata = data;
  924. return 0;
  925. }
  926. static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  927. {
  928. u64 data = 0;
  929. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  930. switch (msr) {
  931. case HV_X64_MSR_VP_INDEX: {
  932. int r;
  933. struct kvm_vcpu *v;
  934. kvm_for_each_vcpu(r, v, vcpu->kvm) {
  935. if (v == vcpu) {
  936. data = r;
  937. break;
  938. }
  939. }
  940. break;
  941. }
  942. case HV_X64_MSR_EOI:
  943. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  944. case HV_X64_MSR_ICR:
  945. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  946. case HV_X64_MSR_TPR:
  947. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  948. case HV_X64_MSR_APIC_ASSIST_PAGE:
  949. data = hv->hv_vapic;
  950. break;
  951. case HV_X64_MSR_VP_RUNTIME:
  952. data = current_task_runtime_100ns() + hv->runtime_offset;
  953. break;
  954. case HV_X64_MSR_SCONTROL:
  955. case HV_X64_MSR_SVERSION:
  956. case HV_X64_MSR_SIEFP:
  957. case HV_X64_MSR_SIMP:
  958. case HV_X64_MSR_EOM:
  959. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  960. return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
  961. case HV_X64_MSR_STIMER0_CONFIG:
  962. case HV_X64_MSR_STIMER1_CONFIG:
  963. case HV_X64_MSR_STIMER2_CONFIG:
  964. case HV_X64_MSR_STIMER3_CONFIG: {
  965. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  966. return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
  967. pdata);
  968. }
  969. case HV_X64_MSR_STIMER0_COUNT:
  970. case HV_X64_MSR_STIMER1_COUNT:
  971. case HV_X64_MSR_STIMER2_COUNT:
  972. case HV_X64_MSR_STIMER3_COUNT: {
  973. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  974. return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
  975. pdata);
  976. }
  977. default:
  978. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  979. return 1;
  980. }
  981. *pdata = data;
  982. return 0;
  983. }
  984. int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  985. {
  986. if (kvm_hv_msr_partition_wide(msr)) {
  987. int r;
  988. mutex_lock(&vcpu->kvm->lock);
  989. r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
  990. mutex_unlock(&vcpu->kvm->lock);
  991. return r;
  992. } else
  993. return kvm_hv_set_msr(vcpu, msr, data, host);
  994. }
  995. int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  996. {
  997. if (kvm_hv_msr_partition_wide(msr)) {
  998. int r;
  999. mutex_lock(&vcpu->kvm->lock);
  1000. r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
  1001. mutex_unlock(&vcpu->kvm->lock);
  1002. return r;
  1003. } else
  1004. return kvm_hv_get_msr(vcpu, msr, pdata);
  1005. }
  1006. bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  1007. {
  1008. return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  1009. }
  1010. static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
  1011. {
  1012. bool longmode;
  1013. longmode = is_64_bit_mode(vcpu);
  1014. if (longmode)
  1015. kvm_register_write(vcpu, VCPU_REGS_RAX, result);
  1016. else {
  1017. kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
  1018. kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
  1019. }
  1020. }
  1021. static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
  1022. {
  1023. struct kvm_run *run = vcpu->run;
  1024. kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
  1025. return 1;
  1026. }
  1027. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  1028. {
  1029. u64 param, ingpa, outgpa, ret;
  1030. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  1031. bool fast, longmode;
  1032. /*
  1033. * hypercall generates UD from non zero cpl and real mode
  1034. * per HYPER-V spec
  1035. */
  1036. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  1037. kvm_queue_exception(vcpu, UD_VECTOR);
  1038. return 1;
  1039. }
  1040. longmode = is_64_bit_mode(vcpu);
  1041. if (!longmode) {
  1042. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  1043. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  1044. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  1045. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  1046. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  1047. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  1048. }
  1049. #ifdef CONFIG_X86_64
  1050. else {
  1051. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  1052. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  1053. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  1054. }
  1055. #endif
  1056. code = param & 0xffff;
  1057. fast = (param >> 16) & 0x1;
  1058. rep_cnt = (param >> 32) & 0xfff;
  1059. rep_idx = (param >> 48) & 0xfff;
  1060. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  1061. /* Hypercall continuation is not supported yet */
  1062. if (rep_cnt || rep_idx) {
  1063. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  1064. goto set_result;
  1065. }
  1066. switch (code) {
  1067. case HVCALL_NOTIFY_LONG_SPIN_WAIT:
  1068. kvm_vcpu_on_spin(vcpu);
  1069. break;
  1070. case HVCALL_POST_MESSAGE:
  1071. case HVCALL_SIGNAL_EVENT:
  1072. /* don't bother userspace if it has no way to handle it */
  1073. if (!vcpu_to_synic(vcpu)->active) {
  1074. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  1075. break;
  1076. }
  1077. vcpu->run->exit_reason = KVM_EXIT_HYPERV;
  1078. vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
  1079. vcpu->run->hyperv.u.hcall.input = param;
  1080. vcpu->run->hyperv.u.hcall.params[0] = ingpa;
  1081. vcpu->run->hyperv.u.hcall.params[1] = outgpa;
  1082. vcpu->arch.complete_userspace_io =
  1083. kvm_hv_hypercall_complete_userspace;
  1084. return 0;
  1085. default:
  1086. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  1087. break;
  1088. }
  1089. set_result:
  1090. ret = res | (((u64)rep_done & 0xfff) << 32);
  1091. kvm_hv_hypercall_set_result(vcpu, ret);
  1092. return 1;
  1093. }