hyperv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
  332. }
  333. static void stimer_mark_pending(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_cleanup(struct kvm_vcpu_hv_stimer *stimer)
  344. {
  345. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  346. trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
  347. stimer->index);
  348. hrtimer_cancel(&stimer->timer);
  349. clear_bit(stimer->index,
  350. vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
  351. stimer->msg_pending = false;
  352. stimer->exp_time = 0;
  353. }
  354. static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
  355. {
  356. struct kvm_vcpu_hv_stimer *stimer;
  357. stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
  358. trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
  359. stimer->index);
  360. stimer_mark_pending(stimer, true);
  361. return HRTIMER_NORESTART;
  362. }
  363. /*
  364. * stimer_start() assumptions:
  365. * a) stimer->count is not equal to 0
  366. * b) stimer->config has HV_STIMER_ENABLE flag
  367. */
  368. static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
  369. {
  370. u64 time_now;
  371. ktime_t ktime_now;
  372. time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
  373. ktime_now = ktime_get();
  374. if (stimer->config & HV_STIMER_PERIODIC) {
  375. if (stimer->exp_time) {
  376. if (time_now >= stimer->exp_time) {
  377. u64 remainder;
  378. div64_u64_rem(time_now - stimer->exp_time,
  379. stimer->count, &remainder);
  380. stimer->exp_time =
  381. time_now + (stimer->count - remainder);
  382. }
  383. } else
  384. stimer->exp_time = time_now + stimer->count;
  385. trace_kvm_hv_stimer_start_periodic(
  386. stimer_to_vcpu(stimer)->vcpu_id,
  387. stimer->index,
  388. time_now, stimer->exp_time);
  389. hrtimer_start(&stimer->timer,
  390. ktime_add_ns(ktime_now,
  391. 100 * (stimer->exp_time - time_now)),
  392. HRTIMER_MODE_ABS);
  393. return 0;
  394. }
  395. stimer->exp_time = stimer->count;
  396. if (time_now >= stimer->count) {
  397. /*
  398. * Expire timer according to Hypervisor Top-Level Functional
  399. * specification v4(15.3.1):
  400. * "If a one shot is enabled and the specified count is in
  401. * the past, it will expire immediately."
  402. */
  403. stimer_mark_pending(stimer, false);
  404. return 0;
  405. }
  406. trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
  407. stimer->index,
  408. time_now, stimer->count);
  409. hrtimer_start(&stimer->timer,
  410. ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
  411. HRTIMER_MODE_ABS);
  412. return 0;
  413. }
  414. static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
  415. bool host)
  416. {
  417. trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
  418. stimer->index, config, host);
  419. stimer_cleanup(stimer);
  420. if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
  421. config &= ~HV_STIMER_ENABLE;
  422. stimer->config = config;
  423. stimer_mark_pending(stimer, false);
  424. return 0;
  425. }
  426. static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
  427. bool host)
  428. {
  429. trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
  430. stimer->index, count, host);
  431. stimer_cleanup(stimer);
  432. stimer->count = count;
  433. if (stimer->count == 0)
  434. stimer->config &= ~HV_STIMER_ENABLE;
  435. else if (stimer->config & HV_STIMER_AUTOENABLE)
  436. stimer->config |= HV_STIMER_ENABLE;
  437. stimer_mark_pending(stimer, false);
  438. return 0;
  439. }
  440. static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
  441. {
  442. *pconfig = stimer->config;
  443. return 0;
  444. }
  445. static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
  446. {
  447. *pcount = stimer->count;
  448. return 0;
  449. }
  450. static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
  451. struct hv_message *src_msg)
  452. {
  453. struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
  454. struct page *page;
  455. gpa_t gpa;
  456. struct hv_message *dst_msg;
  457. int r;
  458. struct hv_message_page *msg_page;
  459. if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
  460. return -ENOENT;
  461. gpa = synic->msg_page & PAGE_MASK;
  462. page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
  463. if (is_error_page(page))
  464. return -EFAULT;
  465. msg_page = kmap_atomic(page);
  466. dst_msg = &msg_page->sint_message[sint];
  467. if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
  468. src_msg->header.message_type) != HVMSG_NONE) {
  469. dst_msg->header.message_flags.msg_pending = 1;
  470. r = -EAGAIN;
  471. } else {
  472. memcpy(&dst_msg->u.payload, &src_msg->u.payload,
  473. src_msg->header.payload_size);
  474. dst_msg->header.message_type = src_msg->header.message_type;
  475. dst_msg->header.payload_size = src_msg->header.payload_size;
  476. r = synic_set_irq(synic, sint);
  477. if (r >= 1)
  478. r = 0;
  479. else if (r == 0)
  480. r = -EFAULT;
  481. }
  482. kunmap_atomic(msg_page);
  483. kvm_release_page_dirty(page);
  484. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  485. return r;
  486. }
  487. static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
  488. {
  489. struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
  490. struct hv_message *msg = &stimer->msg;
  491. struct hv_timer_message_payload *payload =
  492. (struct hv_timer_message_payload *)&msg->u.payload;
  493. payload->expiration_time = stimer->exp_time;
  494. payload->delivery_time = get_time_ref_counter(vcpu->kvm);
  495. return synic_deliver_msg(vcpu_to_synic(vcpu),
  496. HV_STIMER_SINT(stimer->config), msg);
  497. }
  498. static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
  499. {
  500. int r;
  501. stimer->msg_pending = true;
  502. r = stimer_send_msg(stimer);
  503. trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
  504. stimer->index, r);
  505. if (!r) {
  506. stimer->msg_pending = false;
  507. if (!(stimer->config & HV_STIMER_PERIODIC))
  508. stimer->config &= ~HV_STIMER_ENABLE;
  509. }
  510. }
  511. void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
  512. {
  513. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  514. struct kvm_vcpu_hv_stimer *stimer;
  515. u64 time_now, exp_time;
  516. int i;
  517. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  518. if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
  519. stimer = &hv_vcpu->stimer[i];
  520. if (stimer->config & HV_STIMER_ENABLE) {
  521. exp_time = stimer->exp_time;
  522. if (exp_time) {
  523. time_now =
  524. get_time_ref_counter(vcpu->kvm);
  525. if (time_now >= exp_time)
  526. stimer_expiration(stimer);
  527. }
  528. if ((stimer->config & HV_STIMER_ENABLE) &&
  529. stimer->count)
  530. stimer_start(stimer);
  531. else
  532. stimer_cleanup(stimer);
  533. }
  534. }
  535. }
  536. void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
  537. {
  538. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  539. int i;
  540. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  541. stimer_cleanup(&hv_vcpu->stimer[i]);
  542. }
  543. static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
  544. {
  545. struct hv_message *msg = &stimer->msg;
  546. struct hv_timer_message_payload *payload =
  547. (struct hv_timer_message_payload *)&msg->u.payload;
  548. memset(&msg->header, 0, sizeof(msg->header));
  549. msg->header.message_type = HVMSG_TIMER_EXPIRED;
  550. msg->header.payload_size = sizeof(*payload);
  551. payload->timer_index = stimer->index;
  552. payload->expiration_time = 0;
  553. payload->delivery_time = 0;
  554. }
  555. static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
  556. {
  557. memset(stimer, 0, sizeof(*stimer));
  558. stimer->index = timer_index;
  559. hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  560. stimer->timer.function = stimer_timer_callback;
  561. stimer_prepare_msg(stimer);
  562. }
  563. void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
  564. {
  565. struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
  566. int i;
  567. synic_init(&hv_vcpu->synic);
  568. bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
  569. for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
  570. stimer_init(&hv_vcpu->stimer[i], i);
  571. }
  572. int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
  573. {
  574. /*
  575. * Hyper-V SynIC auto EOI SINT's are
  576. * not compatible with APICV, so deactivate APICV
  577. */
  578. kvm_vcpu_deactivate_apicv(vcpu);
  579. vcpu_to_synic(vcpu)->active = true;
  580. return 0;
  581. }
  582. static bool kvm_hv_msr_partition_wide(u32 msr)
  583. {
  584. bool r = false;
  585. switch (msr) {
  586. case HV_X64_MSR_GUEST_OS_ID:
  587. case HV_X64_MSR_HYPERCALL:
  588. case HV_X64_MSR_REFERENCE_TSC:
  589. case HV_X64_MSR_TIME_REF_COUNT:
  590. case HV_X64_MSR_CRASH_CTL:
  591. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  592. case HV_X64_MSR_RESET:
  593. r = true;
  594. break;
  595. }
  596. return r;
  597. }
  598. static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
  599. u32 index, u64 *pdata)
  600. {
  601. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  602. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  603. return -EINVAL;
  604. *pdata = hv->hv_crash_param[index];
  605. return 0;
  606. }
  607. static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
  608. {
  609. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  610. *pdata = hv->hv_crash_ctl;
  611. return 0;
  612. }
  613. static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
  614. {
  615. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  616. if (host)
  617. hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
  618. if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
  619. vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
  620. hv->hv_crash_param[0],
  621. hv->hv_crash_param[1],
  622. hv->hv_crash_param[2],
  623. hv->hv_crash_param[3],
  624. hv->hv_crash_param[4]);
  625. /* Send notification about crash to user space */
  626. kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
  627. }
  628. return 0;
  629. }
  630. static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
  631. u32 index, u64 data)
  632. {
  633. struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
  634. if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
  635. return -EINVAL;
  636. hv->hv_crash_param[index] = data;
  637. return 0;
  638. }
  639. static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
  640. bool host)
  641. {
  642. struct kvm *kvm = vcpu->kvm;
  643. struct kvm_hv *hv = &kvm->arch.hyperv;
  644. switch (msr) {
  645. case HV_X64_MSR_GUEST_OS_ID:
  646. hv->hv_guest_os_id = data;
  647. /* setting guest os id to zero disables hypercall page */
  648. if (!hv->hv_guest_os_id)
  649. hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  650. break;
  651. case HV_X64_MSR_HYPERCALL: {
  652. u64 gfn;
  653. unsigned long addr;
  654. u8 instructions[4];
  655. /* if guest os id is not set hypercall should remain disabled */
  656. if (!hv->hv_guest_os_id)
  657. break;
  658. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  659. hv->hv_hypercall = data;
  660. break;
  661. }
  662. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  663. addr = gfn_to_hva(kvm, gfn);
  664. if (kvm_is_error_hva(addr))
  665. return 1;
  666. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  667. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  668. if (__copy_to_user((void __user *)addr, instructions, 4))
  669. return 1;
  670. hv->hv_hypercall = data;
  671. mark_page_dirty(kvm, gfn);
  672. break;
  673. }
  674. case HV_X64_MSR_REFERENCE_TSC: {
  675. u64 gfn;
  676. HV_REFERENCE_TSC_PAGE tsc_ref;
  677. memset(&tsc_ref, 0, sizeof(tsc_ref));
  678. hv->hv_tsc_page = data;
  679. if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
  680. break;
  681. gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
  682. if (kvm_write_guest(
  683. kvm,
  684. gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
  685. &tsc_ref, sizeof(tsc_ref)))
  686. return 1;
  687. mark_page_dirty(kvm, gfn);
  688. break;
  689. }
  690. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  691. return kvm_hv_msr_set_crash_data(vcpu,
  692. msr - HV_X64_MSR_CRASH_P0,
  693. data);
  694. case HV_X64_MSR_CRASH_CTL:
  695. return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
  696. case HV_X64_MSR_RESET:
  697. if (data == 1) {
  698. vcpu_debug(vcpu, "hyper-v reset requested\n");
  699. kvm_make_request(KVM_REQ_HV_RESET, vcpu);
  700. }
  701. break;
  702. default:
  703. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  704. msr, data);
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. /* Calculate cpu time spent by current task in 100ns units */
  710. static u64 current_task_runtime_100ns(void)
  711. {
  712. cputime_t utime, stime;
  713. task_cputime_adjusted(current, &utime, &stime);
  714. return div_u64(cputime_to_nsecs(utime + stime), 100);
  715. }
  716. static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  717. {
  718. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  719. switch (msr) {
  720. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  721. u64 gfn;
  722. unsigned long addr;
  723. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  724. hv->hv_vapic = data;
  725. if (kvm_lapic_enable_pv_eoi(vcpu, 0))
  726. return 1;
  727. break;
  728. }
  729. gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
  730. addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
  731. if (kvm_is_error_hva(addr))
  732. return 1;
  733. if (__clear_user((void __user *)addr, PAGE_SIZE))
  734. return 1;
  735. hv->hv_vapic = data;
  736. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  737. if (kvm_lapic_enable_pv_eoi(vcpu,
  738. gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
  739. return 1;
  740. break;
  741. }
  742. case HV_X64_MSR_EOI:
  743. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  744. case HV_X64_MSR_ICR:
  745. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  746. case HV_X64_MSR_TPR:
  747. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  748. case HV_X64_MSR_VP_RUNTIME:
  749. if (!host)
  750. return 1;
  751. hv->runtime_offset = data - current_task_runtime_100ns();
  752. break;
  753. case HV_X64_MSR_SCONTROL:
  754. case HV_X64_MSR_SVERSION:
  755. case HV_X64_MSR_SIEFP:
  756. case HV_X64_MSR_SIMP:
  757. case HV_X64_MSR_EOM:
  758. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  759. return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
  760. case HV_X64_MSR_STIMER0_CONFIG:
  761. case HV_X64_MSR_STIMER1_CONFIG:
  762. case HV_X64_MSR_STIMER2_CONFIG:
  763. case HV_X64_MSR_STIMER3_CONFIG: {
  764. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  765. return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
  766. data, host);
  767. }
  768. case HV_X64_MSR_STIMER0_COUNT:
  769. case HV_X64_MSR_STIMER1_COUNT:
  770. case HV_X64_MSR_STIMER2_COUNT:
  771. case HV_X64_MSR_STIMER3_COUNT: {
  772. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  773. return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
  774. data, host);
  775. }
  776. default:
  777. vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
  778. msr, data);
  779. return 1;
  780. }
  781. return 0;
  782. }
  783. static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  784. {
  785. u64 data = 0;
  786. struct kvm *kvm = vcpu->kvm;
  787. struct kvm_hv *hv = &kvm->arch.hyperv;
  788. switch (msr) {
  789. case HV_X64_MSR_GUEST_OS_ID:
  790. data = hv->hv_guest_os_id;
  791. break;
  792. case HV_X64_MSR_HYPERCALL:
  793. data = hv->hv_hypercall;
  794. break;
  795. case HV_X64_MSR_TIME_REF_COUNT:
  796. data = get_time_ref_counter(kvm);
  797. break;
  798. case HV_X64_MSR_REFERENCE_TSC:
  799. data = hv->hv_tsc_page;
  800. break;
  801. case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
  802. return kvm_hv_msr_get_crash_data(vcpu,
  803. msr - HV_X64_MSR_CRASH_P0,
  804. pdata);
  805. case HV_X64_MSR_CRASH_CTL:
  806. return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
  807. case HV_X64_MSR_RESET:
  808. data = 0;
  809. break;
  810. default:
  811. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  812. return 1;
  813. }
  814. *pdata = data;
  815. return 0;
  816. }
  817. static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  818. {
  819. u64 data = 0;
  820. struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
  821. switch (msr) {
  822. case HV_X64_MSR_VP_INDEX: {
  823. int r;
  824. struct kvm_vcpu *v;
  825. kvm_for_each_vcpu(r, v, vcpu->kvm) {
  826. if (v == vcpu) {
  827. data = r;
  828. break;
  829. }
  830. }
  831. break;
  832. }
  833. case HV_X64_MSR_EOI:
  834. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  835. case HV_X64_MSR_ICR:
  836. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  837. case HV_X64_MSR_TPR:
  838. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  839. case HV_X64_MSR_APIC_ASSIST_PAGE:
  840. data = hv->hv_vapic;
  841. break;
  842. case HV_X64_MSR_VP_RUNTIME:
  843. data = current_task_runtime_100ns() + hv->runtime_offset;
  844. break;
  845. case HV_X64_MSR_SCONTROL:
  846. case HV_X64_MSR_SVERSION:
  847. case HV_X64_MSR_SIEFP:
  848. case HV_X64_MSR_SIMP:
  849. case HV_X64_MSR_EOM:
  850. case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
  851. return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
  852. case HV_X64_MSR_STIMER0_CONFIG:
  853. case HV_X64_MSR_STIMER1_CONFIG:
  854. case HV_X64_MSR_STIMER2_CONFIG:
  855. case HV_X64_MSR_STIMER3_CONFIG: {
  856. int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
  857. return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
  858. pdata);
  859. }
  860. case HV_X64_MSR_STIMER0_COUNT:
  861. case HV_X64_MSR_STIMER1_COUNT:
  862. case HV_X64_MSR_STIMER2_COUNT:
  863. case HV_X64_MSR_STIMER3_COUNT: {
  864. int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
  865. return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
  866. pdata);
  867. }
  868. default:
  869. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  870. return 1;
  871. }
  872. *pdata = data;
  873. return 0;
  874. }
  875. int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
  876. {
  877. if (kvm_hv_msr_partition_wide(msr)) {
  878. int r;
  879. mutex_lock(&vcpu->kvm->lock);
  880. r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
  881. mutex_unlock(&vcpu->kvm->lock);
  882. return r;
  883. } else
  884. return kvm_hv_set_msr(vcpu, msr, data, host);
  885. }
  886. int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  887. {
  888. if (kvm_hv_msr_partition_wide(msr)) {
  889. int r;
  890. mutex_lock(&vcpu->kvm->lock);
  891. r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
  892. mutex_unlock(&vcpu->kvm->lock);
  893. return r;
  894. } else
  895. return kvm_hv_get_msr(vcpu, msr, pdata);
  896. }
  897. bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  898. {
  899. return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  900. }
  901. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  902. {
  903. u64 param, ingpa, outgpa, ret;
  904. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  905. bool fast, longmode;
  906. /*
  907. * hypercall generates UD from non zero cpl and real mode
  908. * per HYPER-V spec
  909. */
  910. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  911. kvm_queue_exception(vcpu, UD_VECTOR);
  912. return 0;
  913. }
  914. longmode = is_64_bit_mode(vcpu);
  915. if (!longmode) {
  916. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  917. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  918. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  919. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  920. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  921. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  922. }
  923. #ifdef CONFIG_X86_64
  924. else {
  925. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  926. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  927. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  928. }
  929. #endif
  930. code = param & 0xffff;
  931. fast = (param >> 16) & 0x1;
  932. rep_cnt = (param >> 32) & 0xfff;
  933. rep_idx = (param >> 48) & 0xfff;
  934. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  935. switch (code) {
  936. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  937. kvm_vcpu_on_spin(vcpu);
  938. break;
  939. default:
  940. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  941. break;
  942. }
  943. ret = res | (((u64)rep_done & 0xfff) << 32);
  944. if (longmode) {
  945. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  946. } else {
  947. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  948. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  949. }
  950. return 1;
  951. }