Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. * Check that Gen 2 UEFI firmware is not running
  199. *
  200. * @v hv Hyper-V hypervisor
  201. * @ret rc Return status code
  202. *
  203. * We must not steal ownership from the Gen 2 UEFI firmware, since
  204. * doing so will cause an immediate crash. Avoid this by checking for
  205. * the guest OS identity known to be used by the Gen 2 UEFI firmware.
  206. */
  207. static int hv_check_uefi ( struct hv_hypervisor *hv ) {
  208. uint64_t guest_os_id;
  209. /* Check for UEFI firmware's guest OS identity */
  210. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  211. if ( guest_os_id == HV_GUEST_OS_ID_UEFI ) {
  212. DBGC ( hv, "HV %p is owned by UEFI firmware\n", hv );
  213. return -ENOTSUP;
  214. }
  215. return 0;
  216. }
  217. /**
  218. * Map hypercall page
  219. *
  220. * @v hv Hyper-V hypervisor
  221. */
  222. static void hv_map_hypercall ( struct hv_hypervisor *hv ) {
  223. union {
  224. struct {
  225. uint32_t ebx;
  226. uint32_t ecx;
  227. uint32_t edx;
  228. } __attribute__ (( packed ));
  229. char text[ 13 /* "bbbbccccdddd" + NUL */ ];
  230. } vendor_id;
  231. uint32_t build;
  232. uint32_t version;
  233. uint32_t discard_eax;
  234. uint32_t discard_ecx;
  235. uint32_t discard_edx;
  236. uint64_t guest_os_id;
  237. uint64_t hypercall;
  238. /* Report guest OS identity */
  239. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  240. if ( guest_os_id != 0 ) {
  241. DBGC ( hv, "HV %p guest OS ID MSR was %#08llx\n",
  242. hv, guest_os_id );
  243. }
  244. guest_os_id = HV_GUEST_OS_ID_IPXE;
  245. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  246. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  247. /* Get hypervisor system identity (for debugging) */
  248. cpuid ( HV_CPUID_VENDOR_ID, 0, &discard_eax, &vendor_id.ebx,
  249. &vendor_id.ecx, &vendor_id.edx );
  250. vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
  251. cpuid ( HV_CPUID_HYPERVISOR_ID, 0, &build, &version, &discard_ecx,
  252. &discard_edx );
  253. DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
  254. vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
  255. /* Map hypercall page */
  256. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  257. hypercall &= ( PAGE_SIZE - 1 );
  258. hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
  259. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  260. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  261. }
  262. /**
  263. * Unmap hypercall page
  264. *
  265. * @v hv Hyper-V hypervisor
  266. */
  267. static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
  268. uint64_t hypercall;
  269. uint64_t guest_os_id;
  270. /* Unmap the hypercall page */
  271. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  272. hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
  273. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  274. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  275. /* Reset the guest OS identity */
  276. guest_os_id = 0;
  277. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  278. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  279. }
  280. /**
  281. * Map synthetic interrupt controller
  282. *
  283. * @v hv Hyper-V hypervisor
  284. */
  285. static void hv_map_synic ( struct hv_hypervisor *hv ) {
  286. uint64_t simp;
  287. uint64_t siefp;
  288. uint64_t scontrol;
  289. /* Zero SynIC message and event pages */
  290. memset ( hv->synic.message, 0, PAGE_SIZE );
  291. memset ( hv->synic.event, 0, PAGE_SIZE );
  292. /* Map SynIC message page */
  293. simp = rdmsr ( HV_X64_MSR_SIMP );
  294. simp &= ( PAGE_SIZE - 1 );
  295. simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
  296. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  297. wrmsr ( HV_X64_MSR_SIMP, simp );
  298. /* Map SynIC event page */
  299. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  300. siefp &= ( PAGE_SIZE - 1 );
  301. siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
  302. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  303. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  304. /* Enable SynIC */
  305. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  306. scontrol |= HV_SCONTROL_ENABLE;
  307. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  308. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  309. }
  310. /**
  311. * Unmap synthetic interrupt controller, leaving SCONTROL untouched
  312. *
  313. * @v hv Hyper-V hypervisor
  314. */
  315. static void hv_unmap_synic_no_scontrol ( struct hv_hypervisor *hv ) {
  316. uint64_t siefp;
  317. uint64_t simp;
  318. /* Unmap SynIC event page */
  319. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  320. siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
  321. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  322. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  323. /* Unmap SynIC message page */
  324. simp = rdmsr ( HV_X64_MSR_SIMP );
  325. simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
  326. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  327. wrmsr ( HV_X64_MSR_SIMP, simp );
  328. }
  329. /**
  330. * Unmap synthetic interrupt controller
  331. *
  332. * @v hv Hyper-V hypervisor
  333. */
  334. static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
  335. uint64_t scontrol;
  336. /* Disable SynIC */
  337. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  338. scontrol &= ~HV_SCONTROL_ENABLE;
  339. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  340. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  341. /* Unmap SynIC event and message pages */
  342. hv_unmap_synic_no_scontrol ( hv );
  343. }
  344. /**
  345. * Enable synthetic interrupt
  346. *
  347. * @v hv Hyper-V hypervisor
  348. * @v sintx Synthetic interrupt number
  349. */
  350. void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  351. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  352. uint64_t sint;
  353. /* Enable synthetic interrupt
  354. *
  355. * We have to enable the interrupt, otherwise messages will
  356. * not be delivered (even though the documentation implies
  357. * that polling for messages is possible). We enable AutoEOI
  358. * and hook the interrupt to the obsolete IRQ13 (FPU
  359. * exception) vector, which will be implemented as a no-op.
  360. */
  361. sint = rdmsr ( msr );
  362. sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
  363. sint |= ( HV_SINT_AUTO_EOI |
  364. HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
  365. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  366. wrmsr ( msr, sint );
  367. }
  368. /**
  369. * Disable synthetic interrupt
  370. *
  371. * @v hv Hyper-V hypervisor
  372. * @v sintx Synthetic interrupt number
  373. */
  374. void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  375. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  376. uint64_t sint;
  377. /* Do nothing if interrupt is already disabled */
  378. sint = rdmsr ( msr );
  379. if ( sint & HV_SINT_MASKED )
  380. return;
  381. /* Disable synthetic interrupt */
  382. sint &= ~HV_SINT_AUTO_EOI;
  383. sint |= HV_SINT_MASKED;
  384. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  385. wrmsr ( msr, sint );
  386. }
  387. /**
  388. * Post message
  389. *
  390. * @v hv Hyper-V hypervisor
  391. * @v id Connection ID
  392. * @v type Message type
  393. * @v data Message
  394. * @v len Length of message
  395. * @ret rc Return status code
  396. */
  397. int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
  398. unsigned int type, const void *data, size_t len ) {
  399. struct hv_post_message *msg = &hv->message->posted;
  400. int status;
  401. int rc;
  402. /* Sanity check */
  403. assert ( len <= sizeof ( msg->data ) );
  404. /* Construct message */
  405. memset ( msg, 0, sizeof ( *msg ) );
  406. msg->id = cpu_to_le32 ( id );
  407. msg->type = cpu_to_le32 ( type );
  408. msg->len = cpu_to_le32 ( len );
  409. memcpy ( msg->data, data, len );
  410. DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
  411. hv, id, type );
  412. DBGC2_HDA ( hv, 0, msg->data, len );
  413. /* Post message */
  414. if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
  415. rc = -EHV ( status );
  416. DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
  417. hv, id, strerror ( rc ) );
  418. return rc;
  419. }
  420. return 0;
  421. }
  422. /**
  423. * Wait for received message
  424. *
  425. * @v hv Hyper-V hypervisor
  426. * @v sintx Synthetic interrupt number
  427. * @ret rc Return status code
  428. */
  429. int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
  430. struct hv_message *msg = &hv->message->received;
  431. struct hv_message *src = &hv->synic.message[sintx];
  432. unsigned int retries;
  433. size_t len;
  434. /* Wait for message to arrive */
  435. for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
  436. /* Check for message */
  437. if ( src->type ) {
  438. /* Copy message */
  439. memset ( msg, 0, sizeof ( *msg ) );
  440. len = src->len;
  441. assert ( len <= sizeof ( *msg ) );
  442. memcpy ( msg, src,
  443. ( offsetof ( typeof ( *msg ), data ) + len ) );
  444. DBGC2 ( hv, "HV %p SINT%d received message type "
  445. "%#08x:\n", hv, sintx,
  446. le32_to_cpu ( msg->type ) );
  447. DBGC2_HDA ( hv, 0, msg->data, len );
  448. /* Consume message */
  449. src->type = 0;
  450. return 0;
  451. }
  452. /* Trigger message delivery */
  453. wrmsr ( HV_X64_MSR_EOM, 0 );
  454. /* Delay */
  455. mdelay ( 1 );
  456. }
  457. DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
  458. hv, sintx );
  459. return -ETIMEDOUT;
  460. }
  461. /**
  462. * Signal event
  463. *
  464. * @v hv Hyper-V hypervisor
  465. * @v id Connection ID
  466. * @v flag Flag number
  467. * @ret rc Return status code
  468. */
  469. int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
  470. unsigned int flag ) {
  471. struct hv_signal_event *event = &hv->message->signalled;
  472. int status;
  473. int rc;
  474. /* Construct event */
  475. memset ( event, 0, sizeof ( *event ) );
  476. event->id = cpu_to_le32 ( id );
  477. event->flag = cpu_to_le16 ( flag );
  478. /* Signal event */
  479. if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
  480. rc = -EHV ( status );
  481. DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
  482. hv, id, strerror ( rc ) );
  483. return rc;
  484. }
  485. return 0;
  486. }
  487. /**
  488. * Probe root device
  489. *
  490. * @v rootdev Root device
  491. * @ret rc Return status code
  492. */
  493. static int hv_probe ( struct root_device *rootdev ) {
  494. struct hv_hypervisor *hv;
  495. int rc;
  496. /* Check we are running in Hyper-V */
  497. if ( ( rc = hv_check_hv() ) != 0 )
  498. goto err_check_hv;
  499. /* Allocate and initialise structure */
  500. hv = zalloc ( sizeof ( *hv ) );
  501. if ( ! hv ) {
  502. rc = -ENOMEM;
  503. goto err_alloc;
  504. }
  505. /* Check features */
  506. if ( ( rc = hv_check_features ( hv ) ) != 0 )
  507. goto err_check_features;
  508. /* Check that Gen 2 UEFI firmware is not running */
  509. if ( ( rc = hv_check_uefi ( hv ) ) != 0 )
  510. goto err_check_uefi;
  511. /* Allocate pages */
  512. if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
  513. &hv->synic.event, NULL ) ) != 0 )
  514. goto err_alloc_pages;
  515. /* Allocate message buffer */
  516. if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
  517. goto err_alloc_message;
  518. /* Map hypercall page */
  519. hv_map_hypercall ( hv );
  520. /* Map synthetic interrupt controller */
  521. hv_map_synic ( hv );
  522. /* Probe Hyper-V devices */
  523. if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
  524. goto err_vmbus_probe;
  525. rootdev_set_drvdata ( rootdev, hv );
  526. return 0;
  527. vmbus_remove ( hv, &rootdev->dev );
  528. err_vmbus_probe:
  529. hv_unmap_synic ( hv );
  530. hv_unmap_hypercall ( hv );
  531. hv_free_message ( hv );
  532. err_alloc_message:
  533. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  534. NULL );
  535. err_alloc_pages:
  536. err_check_uefi:
  537. err_check_features:
  538. free ( hv );
  539. err_alloc:
  540. err_check_hv:
  541. return rc;
  542. }
  543. /**
  544. * Remove root device
  545. *
  546. * @v rootdev Root device
  547. */
  548. static void hv_remove ( struct root_device *rootdev ) {
  549. struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
  550. vmbus_remove ( hv, &rootdev->dev );
  551. hv_unmap_synic ( hv );
  552. hv_unmap_hypercall ( hv );
  553. hv_free_message ( hv );
  554. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  555. NULL );
  556. free ( hv );
  557. rootdev_set_drvdata ( rootdev, NULL );
  558. }
  559. /** Hyper-V root device driver */
  560. static struct root_driver hv_root_driver = {
  561. .probe = hv_probe,
  562. .remove = hv_remove,
  563. };
  564. /** Hyper-V root device */
  565. struct root_device hv_root_device __root_device = {
  566. .dev = { .name = "Hyper-V" },
  567. .driver = &hv_root_driver,
  568. };
  569. /**
  570. * Quiesce system
  571. *
  572. */
  573. static void hv_quiesce ( void ) {
  574. struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
  575. unsigned int i;
  576. /* Do nothing if we are not running in Hyper-V */
  577. if ( ! hv )
  578. return;
  579. /* The "enlightened" portions of the Windows Server 2016 boot
  580. * process will not cleanly take ownership of an active
  581. * Hyper-V connection. Experimentation shows that the minimum
  582. * requirement is that we disable the SynIC message page
  583. * (i.e. zero the SIMP MSR).
  584. *
  585. * We cannot perform a full shutdown of the Hyper-V
  586. * connection. Experimentation shows that if we disable the
  587. * SynIC (i.e. zero the SCONTROL MSR) then Windows Server 2016
  588. * will enter an indefinite wait loop.
  589. *
  590. * Attempt to create a safe handover environment by resetting
  591. * all MSRs except for SCONTROL.
  592. *
  593. * Note that we do not shut down our VMBus devices, since we
  594. * may need to unquiesce the system and continue operation.
  595. */
  596. /* Disable all synthetic interrupts */
  597. for ( i = 0 ; i <= HV_SINT_MAX ; i++ )
  598. hv_disable_sint ( hv, i );
  599. /* Unmap synthetic interrupt controller, leaving SCONTROL
  600. * enabled (see above).
  601. */
  602. hv_unmap_synic_no_scontrol ( hv );
  603. /* Unmap hypercall page */
  604. hv_unmap_hypercall ( hv );
  605. DBGC ( hv, "HV %p quiesced\n", hv );
  606. }
  607. /**
  608. * Unquiesce system
  609. *
  610. */
  611. static void hv_unquiesce ( void ) {
  612. struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
  613. uint64_t simp;
  614. int rc;
  615. /* Do nothing if we are not running in Hyper-V */
  616. if ( ! hv )
  617. return;
  618. /* Experimentation shows that the "enlightened" portions of
  619. * Windows Server 2016 will break our Hyper-V connection at
  620. * some point during a SAN boot. Surprisingly it does not
  621. * change the guest OS ID MSR, but it does leave the SynIC
  622. * message page disabled.
  623. *
  624. * Our own explicit quiescing procedure will also disable the
  625. * SynIC message page. We can therefore use the SynIC message
  626. * page enable bit as a heuristic to determine when we need to
  627. * reestablish our Hyper-V connection.
  628. */
  629. simp = rdmsr ( HV_X64_MSR_SIMP );
  630. if ( simp & HV_SIMP_ENABLE )
  631. return;
  632. /* Remap hypercall page */
  633. hv_map_hypercall ( hv );
  634. /* Remap synthetic interrupt controller */
  635. hv_map_synic ( hv );
  636. /* Reset Hyper-V devices */
  637. if ( ( rc = vmbus_reset ( hv, &hv_root_device.dev ) ) != 0 ) {
  638. DBGC ( hv, "HV %p could not unquiesce: %s\n",
  639. hv, strerror ( rc ) );
  640. /* Nothing we can do */
  641. return;
  642. }
  643. }
  644. /** Hyper-V quiescer */
  645. struct quiescer hv_quiescer __quiescer = {
  646. .quiesce = hv_quiesce,
  647. .unquiesce = hv_unquiesce,
  648. };
  649. /**
  650. * Probe timer
  651. *
  652. * @ret rc Return status code
  653. */
  654. static int hv_timer_probe ( void ) {
  655. uint32_t available;
  656. uint32_t discard_ebx;
  657. uint32_t discard_ecx;
  658. uint32_t discard_edx;
  659. int rc;
  660. /* Check we are running in Hyper-V */
  661. if ( ( rc = hv_check_hv() ) != 0 )
  662. return rc;
  663. /* Check for available reference counter */
  664. cpuid ( HV_CPUID_FEATURES, 0, &available, &discard_ebx, &discard_ecx,
  665. &discard_edx );
  666. if ( ! ( available & HV_FEATURES_AVAIL_TIME_REF_COUNT_MSR ) ) {
  667. DBGC ( HV_INTERFACE_ID, "HV has no time reference counter\n" );
  668. return -ENODEV;
  669. }
  670. return 0;
  671. }
  672. /**
  673. * Get current system time in ticks
  674. *
  675. * @ret ticks Current time, in ticks
  676. */
  677. static unsigned long hv_currticks ( void ) {
  678. /* Calculate time using a combination of bit shifts and
  679. * multiplication (to avoid a 64-bit division).
  680. */
  681. return ( ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) >> HV_TIMER_SHIFT ) *
  682. ( TICKS_PER_SEC / ( HV_TIMER_HZ >> HV_TIMER_SHIFT ) ) );
  683. }
  684. /**
  685. * Delay for a fixed number of microseconds
  686. *
  687. * @v usecs Number of microseconds for which to delay
  688. */
  689. static void hv_udelay ( unsigned long usecs ) {
  690. uint32_t start;
  691. uint32_t elapsed;
  692. uint32_t threshold;
  693. /* Spin until specified number of 10MHz ticks have elapsed */
  694. start = rdmsr ( HV_X64_MSR_TIME_REF_COUNT );
  695. threshold = ( usecs * ( HV_TIMER_HZ / 1000000 ) );
  696. do {
  697. elapsed = ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) - start );
  698. } while ( elapsed < threshold );
  699. }
  700. /** Hyper-V timer */
  701. struct timer hv_timer __timer ( TIMER_PREFERRED ) = {
  702. .name = "Hyper-V",
  703. .probe = hv_timer_probe,
  704. .currticks = hv_currticks,
  705. .udelay = hv_udelay,
  706. };
  707. /* Drag in objects via hv_root_device */
  708. REQUIRING_SYMBOL ( hv_root_device );
  709. /* Drag in netvsc driver */
  710. REQUIRE_OBJECT ( netvsc );