選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hyperv.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Hyper-V driver
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <byteswap.h>
  36. #include <pic8259.h>
  37. #include <ipxe/malloc.h>
  38. #include <ipxe/device.h>
  39. #include <ipxe/timer.h>
  40. #include <ipxe/quiesce.h>
  41. #include <ipxe/cpuid.h>
  42. #include <ipxe/msr.h>
  43. #include <ipxe/hyperv.h>
  44. #include <ipxe/vmbus.h>
  45. #include "hyperv.h"
  46. /** Maximum time to wait for a message response
  47. *
  48. * This is a policy decision.
  49. */
  50. #define HV_MESSAGE_MAX_WAIT_MS 1000
  51. /** Hyper-V timer frequency (fixed 10Mhz) */
  52. #define HV_TIMER_HZ 10000000
  53. /** Hyper-V timer scale factor (used to avoid 64-bit division) */
  54. #define HV_TIMER_SHIFT 18
  55. /**
  56. * Convert a Hyper-V status code to an iPXE status code
  57. *
  58. * @v status Hyper-V status code
  59. * @ret rc iPXE status code (before negation)
  60. */
  61. #define EHV( status ) EPLATFORM ( EINFO_EPLATFORM, (status) )
  62. /**
  63. * Allocate zeroed pages
  64. *
  65. * @v hv Hyper-V hypervisor
  66. * @v ... Page addresses to fill in, terminated by NULL
  67. * @ret rc Return status code
  68. */
  69. __attribute__ (( sentinel )) int
  70. hv_alloc_pages ( struct hv_hypervisor *hv, ... ) {
  71. va_list args;
  72. void **page;
  73. int i;
  74. /* Allocate and zero pages */
  75. va_start ( args, hv );
  76. for ( i = 0 ; ( ( page = va_arg ( args, void ** ) ) != NULL ); i++ ) {
  77. *page = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
  78. if ( ! *page )
  79. goto err_alloc;
  80. memset ( *page, 0, PAGE_SIZE );
  81. }
  82. va_end ( args );
  83. return 0;
  84. err_alloc:
  85. va_end ( args );
  86. va_start ( args, hv );
  87. for ( ; i >= 0 ; i-- ) {
  88. page = va_arg ( args, void ** );
  89. free_dma ( *page, PAGE_SIZE );
  90. }
  91. va_end ( args );
  92. return -ENOMEM;
  93. }
  94. /**
  95. * Free pages
  96. *
  97. * @v hv Hyper-V hypervisor
  98. * @v ... Page addresses, terminated by NULL
  99. */
  100. __attribute__ (( sentinel )) void
  101. hv_free_pages ( struct hv_hypervisor *hv, ... ) {
  102. va_list args;
  103. void *page;
  104. va_start ( args, hv );
  105. while ( ( page = va_arg ( args, void * ) ) != NULL )
  106. free_dma ( page, PAGE_SIZE );
  107. va_end ( args );
  108. }
  109. /**
  110. * Allocate message buffer
  111. *
  112. * @v hv Hyper-V hypervisor
  113. * @ret rc Return status code
  114. */
  115. static int hv_alloc_message ( struct hv_hypervisor *hv ) {
  116. /* Allocate buffer. Must be aligned to at least 8 bytes and
  117. * must not cross a page boundary, so align on its own size.
  118. */
  119. hv->message = malloc_dma ( sizeof ( *hv->message ),
  120. sizeof ( *hv->message ) );
  121. if ( ! hv->message )
  122. return -ENOMEM;
  123. return 0;
  124. }
  125. /**
  126. * Free message buffer
  127. *
  128. * @v hv Hyper-V hypervisor
  129. */
  130. static void hv_free_message ( struct hv_hypervisor *hv ) {
  131. /* Free buffer */
  132. free_dma ( hv->message, sizeof ( *hv->message ) );
  133. }
  134. /**
  135. * Check whether or not we are running in Hyper-V
  136. *
  137. * @ret rc Return status code
  138. */
  139. static int hv_check_hv ( void ) {
  140. struct x86_features features;
  141. uint32_t interface_id;
  142. uint32_t discard_ebx;
  143. uint32_t discard_ecx;
  144. uint32_t discard_edx;
  145. /* Check for presence of a hypervisor (not necessarily Hyper-V) */
  146. x86_features ( &features );
  147. if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
  148. DBGC ( HV_INTERFACE_ID, "HV not running in a hypervisor\n" );
  149. return -ENODEV;
  150. }
  151. /* Check that hypervisor is Hyper-V */
  152. cpuid ( HV_CPUID_INTERFACE_ID, 0, &interface_id, &discard_ebx,
  153. &discard_ecx, &discard_edx );
  154. if ( interface_id != HV_INTERFACE_ID ) {
  155. DBGC ( HV_INTERFACE_ID, "HV not running in Hyper-V (interface "
  156. "ID %#08x)\n", interface_id );
  157. return -ENODEV;
  158. }
  159. return 0;
  160. }
  161. /**
  162. * Check required features
  163. *
  164. * @v hv Hyper-V hypervisor
  165. * @ret rc Return status code
  166. */
  167. static int hv_check_features ( struct hv_hypervisor *hv ) {
  168. uint32_t available;
  169. uint32_t permissions;
  170. uint32_t discard_ecx;
  171. uint32_t discard_edx;
  172. /* Check that required features and privileges are available */
  173. cpuid ( HV_CPUID_FEATURES, 0, &available, &permissions, &discard_ecx,
  174. &discard_edx );
  175. if ( ! ( available & HV_FEATURES_AVAIL_HYPERCALL_MSR ) ) {
  176. DBGC ( hv, "HV %p has no hypercall MSRs (features %08x:%08x)\n",
  177. hv, available, permissions );
  178. return -ENODEV;
  179. }
  180. if ( ! ( available & HV_FEATURES_AVAIL_SYNIC_MSR ) ) {
  181. DBGC ( hv, "HV %p has no SynIC MSRs (features %08x:%08x)\n",
  182. hv, available, permissions );
  183. return -ENODEV;
  184. }
  185. if ( ! ( permissions & HV_FEATURES_PERM_POST_MESSAGES ) ) {
  186. DBGC ( hv, "HV %p cannot post messages (features %08x:%08x)\n",
  187. hv, available, permissions );
  188. return -EACCES;
  189. }
  190. if ( ! ( permissions & HV_FEATURES_PERM_SIGNAL_EVENTS ) ) {
  191. DBGC ( hv, "HV %p cannot signal events (features %08x:%08x)",
  192. hv, available, permissions );
  193. return -EACCES;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * Map hypercall page
  199. *
  200. * @v hv Hyper-V hypervisor
  201. */
  202. static void hv_map_hypercall ( struct hv_hypervisor *hv ) {
  203. union {
  204. struct {
  205. uint32_t ebx;
  206. uint32_t ecx;
  207. uint32_t edx;
  208. } __attribute__ (( packed ));
  209. char text[ 13 /* "bbbbccccdddd" + NUL */ ];
  210. } vendor_id;
  211. uint32_t build;
  212. uint32_t version;
  213. uint32_t discard_eax;
  214. uint32_t discard_ecx;
  215. uint32_t discard_edx;
  216. uint64_t guest_os_id;
  217. uint64_t hypercall;
  218. /* Report guest OS identity */
  219. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  220. if ( guest_os_id != 0 ) {
  221. DBGC ( hv, "HV %p guest OS ID MSR was %#08llx\n",
  222. hv, guest_os_id );
  223. }
  224. guest_os_id = HV_GUEST_OS_ID_IPXE;
  225. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  226. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  227. /* Get hypervisor system identity (for debugging) */
  228. cpuid ( HV_CPUID_VENDOR_ID, 0, &discard_eax, &vendor_id.ebx,
  229. &vendor_id.ecx, &vendor_id.edx );
  230. vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
  231. cpuid ( HV_CPUID_HYPERVISOR_ID, 0, &build, &version, &discard_ecx,
  232. &discard_edx );
  233. DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
  234. vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
  235. /* Map hypercall page */
  236. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  237. hypercall &= ( PAGE_SIZE - 1 );
  238. hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
  239. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  240. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  241. }
  242. /**
  243. * Unmap hypercall page
  244. *
  245. * @v hv Hyper-V hypervisor
  246. */
  247. static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
  248. uint64_t hypercall;
  249. uint64_t guest_os_id;
  250. /* Unmap the hypercall page */
  251. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  252. hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
  253. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  254. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  255. /* Reset the guest OS identity */
  256. guest_os_id = 0;
  257. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  258. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  259. }
  260. /**
  261. * Map synthetic interrupt controller
  262. *
  263. * @v hv Hyper-V hypervisor
  264. */
  265. static void hv_map_synic ( struct hv_hypervisor *hv ) {
  266. uint64_t simp;
  267. uint64_t siefp;
  268. uint64_t scontrol;
  269. /* Zero SynIC message and event pages */
  270. memset ( hv->synic.message, 0, PAGE_SIZE );
  271. memset ( hv->synic.event, 0, PAGE_SIZE );
  272. /* Map SynIC message page */
  273. simp = rdmsr ( HV_X64_MSR_SIMP );
  274. simp &= ( PAGE_SIZE - 1 );
  275. simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
  276. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  277. wrmsr ( HV_X64_MSR_SIMP, simp );
  278. /* Map SynIC event page */
  279. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  280. siefp &= ( PAGE_SIZE - 1 );
  281. siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
  282. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  283. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  284. /* Enable SynIC */
  285. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  286. scontrol |= HV_SCONTROL_ENABLE;
  287. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  288. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  289. }
  290. /**
  291. * Unmap synthetic interrupt controller, leaving SCONTROL untouched
  292. *
  293. * @v hv Hyper-V hypervisor
  294. */
  295. static void hv_unmap_synic_no_scontrol ( struct hv_hypervisor *hv ) {
  296. uint64_t siefp;
  297. uint64_t simp;
  298. /* Unmap SynIC event page */
  299. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  300. siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
  301. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  302. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  303. /* Unmap SynIC message page */
  304. simp = rdmsr ( HV_X64_MSR_SIMP );
  305. simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
  306. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  307. wrmsr ( HV_X64_MSR_SIMP, simp );
  308. }
  309. /**
  310. * Unmap synthetic interrupt controller
  311. *
  312. * @v hv Hyper-V hypervisor
  313. */
  314. static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
  315. uint64_t scontrol;
  316. /* Disable SynIC */
  317. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  318. scontrol &= ~HV_SCONTROL_ENABLE;
  319. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  320. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  321. /* Unmap SynIC event and message pages */
  322. hv_unmap_synic_no_scontrol ( hv );
  323. }
  324. /**
  325. * Enable synthetic interrupt
  326. *
  327. * @v hv Hyper-V hypervisor
  328. * @v sintx Synthetic interrupt number
  329. */
  330. void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  331. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  332. uint64_t sint;
  333. /* Enable synthetic interrupt
  334. *
  335. * We have to enable the interrupt, otherwise messages will
  336. * not be delivered (even though the documentation implies
  337. * that polling for messages is possible). We enable AutoEOI
  338. * and hook the interrupt to the obsolete IRQ13 (FPU
  339. * exception) vector, which will be implemented as a no-op.
  340. */
  341. sint = rdmsr ( msr );
  342. sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
  343. sint |= ( HV_SINT_AUTO_EOI |
  344. HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
  345. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  346. wrmsr ( msr, sint );
  347. }
  348. /**
  349. * Disable synthetic interrupt
  350. *
  351. * @v hv Hyper-V hypervisor
  352. * @v sintx Synthetic interrupt number
  353. */
  354. void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  355. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  356. uint64_t sint;
  357. /* Do nothing if interrupt is already disabled */
  358. sint = rdmsr ( msr );
  359. if ( sint & HV_SINT_MASKED )
  360. return;
  361. /* Disable synthetic interrupt */
  362. sint &= ~HV_SINT_AUTO_EOI;
  363. sint |= HV_SINT_MASKED;
  364. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  365. wrmsr ( msr, sint );
  366. }
  367. /**
  368. * Post message
  369. *
  370. * @v hv Hyper-V hypervisor
  371. * @v id Connection ID
  372. * @v type Message type
  373. * @v data Message
  374. * @v len Length of message
  375. * @ret rc Return status code
  376. */
  377. int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
  378. unsigned int type, const void *data, size_t len ) {
  379. struct hv_post_message *msg = &hv->message->posted;
  380. int status;
  381. int rc;
  382. /* Sanity check */
  383. assert ( len <= sizeof ( msg->data ) );
  384. /* Construct message */
  385. memset ( msg, 0, sizeof ( *msg ) );
  386. msg->id = cpu_to_le32 ( id );
  387. msg->type = cpu_to_le32 ( type );
  388. msg->len = cpu_to_le32 ( len );
  389. memcpy ( msg->data, data, len );
  390. DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
  391. hv, id, type );
  392. DBGC2_HDA ( hv, 0, msg->data, len );
  393. /* Post message */
  394. if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
  395. rc = -EHV ( status );
  396. DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
  397. hv, id, strerror ( rc ) );
  398. return rc;
  399. }
  400. return 0;
  401. }
  402. /**
  403. * Wait for received message
  404. *
  405. * @v hv Hyper-V hypervisor
  406. * @v sintx Synthetic interrupt number
  407. * @ret rc Return status code
  408. */
  409. int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
  410. struct hv_message *msg = &hv->message->received;
  411. struct hv_message *src = &hv->synic.message[sintx];
  412. unsigned int retries;
  413. size_t len;
  414. /* Wait for message to arrive */
  415. for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
  416. /* Check for message */
  417. if ( src->type ) {
  418. /* Copy message */
  419. memset ( msg, 0, sizeof ( *msg ) );
  420. len = src->len;
  421. assert ( len <= sizeof ( *msg ) );
  422. memcpy ( msg, src,
  423. ( offsetof ( typeof ( *msg ), data ) + len ) );
  424. DBGC2 ( hv, "HV %p SINT%d received message type "
  425. "%#08x:\n", hv, sintx,
  426. le32_to_cpu ( msg->type ) );
  427. DBGC2_HDA ( hv, 0, msg->data, len );
  428. /* Consume message */
  429. src->type = 0;
  430. return 0;
  431. }
  432. /* Trigger message delivery */
  433. wrmsr ( HV_X64_MSR_EOM, 0 );
  434. /* Delay */
  435. mdelay ( 1 );
  436. }
  437. DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
  438. hv, sintx );
  439. return -ETIMEDOUT;
  440. }
  441. /**
  442. * Signal event
  443. *
  444. * @v hv Hyper-V hypervisor
  445. * @v id Connection ID
  446. * @v flag Flag number
  447. * @ret rc Return status code
  448. */
  449. int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
  450. unsigned int flag ) {
  451. struct hv_signal_event *event = &hv->message->signalled;
  452. int status;
  453. int rc;
  454. /* Construct event */
  455. memset ( event, 0, sizeof ( *event ) );
  456. event->id = cpu_to_le32 ( id );
  457. event->flag = cpu_to_le16 ( flag );
  458. /* Signal event */
  459. if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
  460. rc = -EHV ( status );
  461. DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
  462. hv, id, strerror ( rc ) );
  463. return rc;
  464. }
  465. return 0;
  466. }
  467. /**
  468. * Probe root device
  469. *
  470. * @v rootdev Root device
  471. * @ret rc Return status code
  472. */
  473. static int hv_probe ( struct root_device *rootdev ) {
  474. struct hv_hypervisor *hv;
  475. int rc;
  476. /* Check we are running in Hyper-V */
  477. if ( ( rc = hv_check_hv() ) != 0 )
  478. goto err_check_hv;
  479. /* Allocate and initialise structure */
  480. hv = zalloc ( sizeof ( *hv ) );
  481. if ( ! hv ) {
  482. rc = -ENOMEM;
  483. goto err_alloc;
  484. }
  485. /* Check features */
  486. if ( ( rc = hv_check_features ( hv ) ) != 0 )
  487. goto err_check_features;
  488. /* Allocate pages */
  489. if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
  490. &hv->synic.event, NULL ) ) != 0 )
  491. goto err_alloc_pages;
  492. /* Allocate message buffer */
  493. if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
  494. goto err_alloc_message;
  495. /* Map hypercall page */
  496. hv_map_hypercall ( hv );
  497. /* Map synthetic interrupt controller */
  498. hv_map_synic ( hv );
  499. /* Probe Hyper-V devices */
  500. if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
  501. goto err_vmbus_probe;
  502. rootdev_set_drvdata ( rootdev, hv );
  503. return 0;
  504. vmbus_remove ( hv, &rootdev->dev );
  505. err_vmbus_probe:
  506. hv_unmap_synic ( hv );
  507. hv_unmap_hypercall ( hv );
  508. hv_free_message ( hv );
  509. err_alloc_message:
  510. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  511. NULL );
  512. err_alloc_pages:
  513. err_check_features:
  514. free ( hv );
  515. err_alloc:
  516. err_check_hv:
  517. return rc;
  518. }
  519. /**
  520. * Remove root device
  521. *
  522. * @v rootdev Root device
  523. */
  524. static void hv_remove ( struct root_device *rootdev ) {
  525. struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
  526. vmbus_remove ( hv, &rootdev->dev );
  527. hv_unmap_synic ( hv );
  528. hv_unmap_hypercall ( hv );
  529. hv_free_message ( hv );
  530. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  531. NULL );
  532. free ( hv );
  533. rootdev_set_drvdata ( rootdev, NULL );
  534. }
  535. /** Hyper-V root device driver */
  536. static struct root_driver hv_root_driver = {
  537. .probe = hv_probe,
  538. .remove = hv_remove,
  539. };
  540. /** Hyper-V root device */
  541. struct root_device hv_root_device __root_device = {
  542. .dev = { .name = "Hyper-V" },
  543. .driver = &hv_root_driver,
  544. };
  545. /**
  546. * Quiesce system
  547. *
  548. */
  549. static void hv_quiesce ( void ) {
  550. struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
  551. unsigned int i;
  552. /* Do nothing if we are not running in Hyper-V */
  553. if ( ! hv )
  554. return;
  555. /* The "enlightened" portions of the Windows Server 2016 boot
  556. * process will not cleanly take ownership of an active
  557. * Hyper-V connection. Experimentation shows that the minimum
  558. * requirement is that we disable the SynIC message page
  559. * (i.e. zero the SIMP MSR).
  560. *
  561. * We cannot perform a full shutdown of the Hyper-V
  562. * connection. Experimentation shows that if we disable the
  563. * SynIC (i.e. zero the SCONTROL MSR) then Windows Server 2016
  564. * will enter an indefinite wait loop.
  565. *
  566. * Attempt to create a safe handover environment by resetting
  567. * all MSRs except for SCONTROL.
  568. *
  569. * Note that we do not shut down our VMBus devices, since we
  570. * may need to unquiesce the system and continue operation.
  571. */
  572. /* Disable all synthetic interrupts */
  573. for ( i = 0 ; i <= HV_SINT_MAX ; i++ )
  574. hv_disable_sint ( hv, i );
  575. /* Unmap synthetic interrupt controller, leaving SCONTROL
  576. * enabled (see above).
  577. */
  578. hv_unmap_synic_no_scontrol ( hv );
  579. /* Unmap hypercall page */
  580. hv_unmap_hypercall ( hv );
  581. DBGC ( hv, "HV %p quiesced\n", hv );
  582. }
  583. /**
  584. * Unquiesce system
  585. *
  586. */
  587. static void hv_unquiesce ( void ) {
  588. struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
  589. uint64_t simp;
  590. int rc;
  591. /* Do nothing if we are not running in Hyper-V */
  592. if ( ! hv )
  593. return;
  594. /* Experimentation shows that the "enlightened" portions of
  595. * Windows Server 2016 will break our Hyper-V connection at
  596. * some point during a SAN boot. Surprisingly it does not
  597. * change the guest OS ID MSR, but it does leave the SynIC
  598. * message page disabled.
  599. *
  600. * Our own explicit quiescing procedure will also disable the
  601. * SynIC message page. We can therefore use the SynIC message
  602. * page enable bit as a heuristic to determine when we need to
  603. * reestablish our Hyper-V connection.
  604. */
  605. simp = rdmsr ( HV_X64_MSR_SIMP );
  606. if ( simp & HV_SIMP_ENABLE )
  607. return;
  608. /* Remap hypercall page */
  609. hv_map_hypercall ( hv );
  610. /* Remap synthetic interrupt controller */
  611. hv_map_synic ( hv );
  612. /* Reset Hyper-V devices */
  613. if ( ( rc = vmbus_reset ( hv, &hv_root_device.dev ) ) != 0 ) {
  614. DBGC ( hv, "HV %p could not unquiesce: %s\n",
  615. hv, strerror ( rc ) );
  616. /* Nothing we can do */
  617. return;
  618. }
  619. }
  620. /** Hyper-V quiescer */
  621. struct quiescer hv_quiescer __quiescer = {
  622. .quiesce = hv_quiesce,
  623. .unquiesce = hv_unquiesce,
  624. };
  625. /**
  626. * Probe timer
  627. *
  628. * @ret rc Return status code
  629. */
  630. static int hv_timer_probe ( void ) {
  631. uint32_t available;
  632. uint32_t discard_ebx;
  633. uint32_t discard_ecx;
  634. uint32_t discard_edx;
  635. int rc;
  636. /* Check we are running in Hyper-V */
  637. if ( ( rc = hv_check_hv() ) != 0 )
  638. return rc;
  639. /* Check for available reference counter */
  640. cpuid ( HV_CPUID_FEATURES, 0, &available, &discard_ebx, &discard_ecx,
  641. &discard_edx );
  642. if ( ! ( available & HV_FEATURES_AVAIL_TIME_REF_COUNT_MSR ) ) {
  643. DBGC ( HV_INTERFACE_ID, "HV has no time reference counter\n" );
  644. return -ENODEV;
  645. }
  646. return 0;
  647. }
  648. /**
  649. * Get current system time in ticks
  650. *
  651. * @ret ticks Current time, in ticks
  652. */
  653. static unsigned long hv_currticks ( void ) {
  654. /* Calculate time using a combination of bit shifts and
  655. * multiplication (to avoid a 64-bit division).
  656. */
  657. return ( ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) >> HV_TIMER_SHIFT ) *
  658. ( TICKS_PER_SEC / ( HV_TIMER_HZ >> HV_TIMER_SHIFT ) ) );
  659. }
  660. /**
  661. * Delay for a fixed number of microseconds
  662. *
  663. * @v usecs Number of microseconds for which to delay
  664. */
  665. static void hv_udelay ( unsigned long usecs ) {
  666. uint32_t start;
  667. uint32_t elapsed;
  668. uint32_t threshold;
  669. /* Spin until specified number of 10MHz ticks have elapsed */
  670. start = rdmsr ( HV_X64_MSR_TIME_REF_COUNT );
  671. threshold = ( usecs * ( HV_TIMER_HZ / 1000000 ) );
  672. do {
  673. elapsed = ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) - start );
  674. } while ( elapsed < threshold );
  675. }
  676. /** Hyper-V timer */
  677. struct timer hv_timer __timer ( TIMER_PREFERRED ) = {
  678. .name = "Hyper-V",
  679. .probe = hv_timer_probe,
  680. .currticks = hv_currticks,
  681. .udelay = hv_udelay,
  682. };
  683. /* Drag in objects via hv_root_device */
  684. REQUIRING_SYMBOL ( hv_root_device );
  685. /* Drag in netvsc driver */
  686. REQUIRE_OBJECT ( netvsc );