hyperv.c 32 KB

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