You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Hyper-V driver
  23. *
  24. */
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <byteswap.h>
  32. #include <pic8259.h>
  33. #include <ipxe/malloc.h>
  34. #include <ipxe/device.h>
  35. #include <ipxe/cpuid.h>
  36. #include <ipxe/msr.h>
  37. #include <ipxe/hyperv.h>
  38. #include <ipxe/vmbus.h>
  39. #include "hyperv.h"
  40. /** Maximum time to wait for a message response
  41. *
  42. * This is a policy decision.
  43. */
  44. #define HV_MESSAGE_MAX_WAIT_MS 1000
  45. /**
  46. * Convert a Hyper-V status code to an iPXE status code
  47. *
  48. * @v status Hyper-V status code
  49. * @ret rc iPXE status code (before negation)
  50. */
  51. #define EHV( status ) EPLATFORM ( EINFO_EPLATFORM, (status) )
  52. /**
  53. * Allocate zeroed pages
  54. *
  55. * @v hv Hyper-V hypervisor
  56. * @v ... Page addresses to fill in, terminated by NULL
  57. * @ret rc Return status code
  58. */
  59. __attribute__ (( sentinel )) int
  60. hv_alloc_pages ( struct hv_hypervisor *hv, ... ) {
  61. va_list args;
  62. void **page;
  63. int i;
  64. /* Allocate and zero pages */
  65. va_start ( args, hv );
  66. for ( i = 0 ; ( ( page = va_arg ( args, void ** ) ) != NULL ); i++ ) {
  67. *page = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
  68. if ( ! *page )
  69. goto err_alloc;
  70. memset ( *page, 0, PAGE_SIZE );
  71. }
  72. va_end ( args );
  73. return 0;
  74. err_alloc:
  75. va_end ( args );
  76. va_start ( args, hv );
  77. for ( ; i >= 0 ; i-- ) {
  78. page = va_arg ( args, void ** );
  79. free_dma ( *page, PAGE_SIZE );
  80. }
  81. va_end ( args );
  82. return -ENOMEM;
  83. }
  84. /**
  85. * Free pages
  86. *
  87. * @v hv Hyper-V hypervisor
  88. * @v ... Page addresses, terminated by NULL
  89. */
  90. __attribute__ (( sentinel )) void
  91. hv_free_pages ( struct hv_hypervisor *hv, ... ) {
  92. va_list args;
  93. void *page;
  94. va_start ( args, hv );
  95. while ( ( page = va_arg ( args, void * ) ) != NULL )
  96. free_dma ( page, PAGE_SIZE );
  97. va_end ( args );
  98. }
  99. /**
  100. * Allocate message buffer
  101. *
  102. * @v hv Hyper-V hypervisor
  103. * @ret rc Return status code
  104. */
  105. static int hv_alloc_message ( struct hv_hypervisor *hv ) {
  106. /* Allocate buffer. Must be aligned to at least 8 bytes and
  107. * must not cross a page boundary, so align on its own size.
  108. */
  109. hv->message = malloc_dma ( sizeof ( *hv->message ),
  110. sizeof ( *hv->message ) );
  111. if ( ! hv->message )
  112. return -ENOMEM;
  113. return 0;
  114. }
  115. /**
  116. * Free message buffer
  117. *
  118. * @v hv Hyper-V hypervisor
  119. */
  120. static void hv_free_message ( struct hv_hypervisor *hv ) {
  121. /* Free buffer */
  122. free_dma ( hv->message, sizeof ( *hv->message ) );
  123. }
  124. /**
  125. * Check whether or not we are running in Hyper-V
  126. *
  127. * @v hv Hyper-V hypervisor
  128. * @ret rc Return status code
  129. */
  130. static int hv_check_hv ( struct hv_hypervisor *hv ) {
  131. struct x86_features features;
  132. uint32_t interface_id;
  133. uint32_t discard_ebx;
  134. uint32_t discard_ecx;
  135. uint32_t discard_edx;
  136. uint32_t available;
  137. uint32_t permissions;
  138. /* Check for presence of a hypervisor (not necessarily Hyper-V) */
  139. x86_features ( &features );
  140. if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
  141. DBGC ( hv, "HV %p not running in a hypervisor\n", hv );
  142. return -ENODEV;
  143. }
  144. /* Check that hypervisor is Hyper-V */
  145. cpuid ( HV_CPUID_INTERFACE_ID, &interface_id, &discard_ebx,
  146. &discard_ecx, &discard_edx );
  147. if ( interface_id != HV_INTERFACE_ID ) {
  148. DBGC ( hv, "HV %p not running in Hyper-V (interface ID "
  149. "%#08x)\n", hv, interface_id );
  150. return -ENODEV;
  151. }
  152. /* Check that required features and privileges are available */
  153. cpuid ( HV_CPUID_FEATURES, &available, &permissions, &discard_ecx,
  154. &discard_edx );
  155. if ( ! ( available & HV_FEATURES_AVAIL_HYPERCALL_MSR ) ) {
  156. DBGC ( hv, "HV %p has no hypercall MSRs (features %08x:%08x)\n",
  157. hv, available, permissions );
  158. return -ENODEV;
  159. }
  160. if ( ! ( available & HV_FEATURES_AVAIL_SYNIC_MSR ) ) {
  161. DBGC ( hv, "HV %p has no SynIC MSRs (features %08x:%08x)\n",
  162. hv, available, permissions );
  163. return -ENODEV;
  164. }
  165. if ( ! ( permissions & HV_FEATURES_PERM_POST_MESSAGES ) ) {
  166. DBGC ( hv, "HV %p cannot post messages (features %08x:%08x)\n",
  167. hv, available, permissions );
  168. return -EACCES;
  169. }
  170. if ( ! ( permissions & HV_FEATURES_PERM_SIGNAL_EVENTS ) ) {
  171. DBGC ( hv, "HV %p cannot signal events (features %08x:%08x)",
  172. hv, available, permissions );
  173. return -EACCES;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * Map hypercall page
  179. *
  180. * @v hv Hyper-V hypervisor
  181. * @ret rc Return status code
  182. */
  183. static int hv_map_hypercall ( struct hv_hypervisor *hv ) {
  184. union {
  185. struct {
  186. uint32_t ebx;
  187. uint32_t ecx;
  188. uint32_t edx;
  189. } __attribute__ (( packed ));
  190. char text[ 13 /* "bbbbccccdddd" + NUL */ ];
  191. } vendor_id;
  192. uint32_t build;
  193. uint32_t version;
  194. uint32_t discard_eax;
  195. uint32_t discard_ecx;
  196. uint32_t discard_edx;
  197. uint64_t guest_os_id;
  198. uint64_t hypercall;
  199. /* Report guest OS identity */
  200. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  201. if ( guest_os_id != 0 ) {
  202. DBGC ( hv, "HV %p guest OS ID MSR already set to %#08llx\n",
  203. hv, guest_os_id );
  204. return -EBUSY;
  205. }
  206. guest_os_id = HV_GUEST_OS_ID_IPXE;
  207. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  208. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  209. /* Get hypervisor system identity (for debugging) */
  210. cpuid ( HV_CPUID_VENDOR_ID, &discard_eax, &vendor_id.ebx,
  211. &vendor_id.ecx, &vendor_id.edx );
  212. vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
  213. cpuid ( HV_CPUID_HYPERVISOR_ID, &build, &version, &discard_ecx,
  214. &discard_edx );
  215. DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
  216. vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
  217. /* Map hypercall page */
  218. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  219. hypercall &= ( PAGE_SIZE - 1 );
  220. hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
  221. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  222. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  223. return 0;
  224. }
  225. /**
  226. * Unmap hypercall page
  227. *
  228. * @v hv Hyper-V hypervisor
  229. */
  230. static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
  231. uint64_t hypercall;
  232. uint64_t guest_os_id;
  233. /* Unmap the hypercall page */
  234. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  235. hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
  236. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  237. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  238. /* Reset the guest OS identity */
  239. guest_os_id = 0;
  240. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  241. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  242. }
  243. /**
  244. * Map synthetic interrupt controller
  245. *
  246. * @v hv Hyper-V hypervisor
  247. * @ret rc Return status code
  248. */
  249. static int hv_map_synic ( struct hv_hypervisor *hv ) {
  250. uint64_t simp;
  251. uint64_t siefp;
  252. uint64_t scontrol;
  253. /* Map SynIC message page */
  254. simp = rdmsr ( HV_X64_MSR_SIMP );
  255. simp &= ( PAGE_SIZE - 1 );
  256. simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
  257. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  258. wrmsr ( HV_X64_MSR_SIMP, simp );
  259. /* Map SynIC event page */
  260. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  261. siefp &= ( PAGE_SIZE - 1 );
  262. siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
  263. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  264. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  265. /* Enable SynIC */
  266. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  267. scontrol |= HV_SCONTROL_ENABLE;
  268. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  269. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  270. return 0;
  271. }
  272. /**
  273. * Unmap synthetic interrupt controller
  274. *
  275. * @v hv Hyper-V hypervisor
  276. */
  277. static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
  278. uint64_t scontrol;
  279. uint64_t siefp;
  280. uint64_t simp;
  281. /* Disable SynIC */
  282. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  283. scontrol &= ~HV_SCONTROL_ENABLE;
  284. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  285. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  286. /* Unmap SynIC event page */
  287. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  288. siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
  289. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  290. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  291. /* Unmap SynIC message page */
  292. simp = rdmsr ( HV_X64_MSR_SIMP );
  293. simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
  294. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  295. wrmsr ( HV_X64_MSR_SIMP, simp );
  296. }
  297. /**
  298. * Enable synthetic interrupt
  299. *
  300. * @v hv Hyper-V hypervisor
  301. * @v sintx Synthetic interrupt number
  302. */
  303. void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  304. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  305. uint64_t sint;
  306. /* Enable synthetic interrupt
  307. *
  308. * We have to enable the interrupt, otherwise messages will
  309. * not be delivered (even though the documentation implies
  310. * that polling for messages is possible). We enable AutoEOI
  311. * and hook the interrupt to the obsolete IRQ13 (FPU
  312. * exception) vector, which will be implemented as a no-op.
  313. */
  314. sint = rdmsr ( msr );
  315. sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
  316. sint |= ( HV_SINT_AUTO_EOI |
  317. HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
  318. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  319. wrmsr ( msr, sint );
  320. }
  321. /**
  322. * Disable synthetic interrupt
  323. *
  324. * @v hv Hyper-V hypervisor
  325. * @v sintx Synthetic interrupt number
  326. */
  327. void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  328. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  329. uint64_t sint;
  330. /* Disable synthetic interrupt */
  331. sint = rdmsr ( msr );
  332. sint &= ~HV_SINT_AUTO_EOI;
  333. sint |= HV_SINT_MASKED;
  334. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  335. wrmsr ( msr, sint );
  336. }
  337. /**
  338. * Post message
  339. *
  340. * @v hv Hyper-V hypervisor
  341. * @v id Connection ID
  342. * @v type Message type
  343. * @v data Message
  344. * @v len Length of message
  345. * @ret rc Return status code
  346. */
  347. int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
  348. unsigned int type, const void *data, size_t len ) {
  349. struct hv_post_message *msg = &hv->message->posted;
  350. int status;
  351. int rc;
  352. /* Sanity check */
  353. assert ( len <= sizeof ( msg->data ) );
  354. /* Construct message */
  355. memset ( msg, 0, sizeof ( *msg ) );
  356. msg->id = cpu_to_le32 ( id );
  357. msg->type = cpu_to_le32 ( type );
  358. msg->len = cpu_to_le32 ( len );
  359. memcpy ( msg->data, data, len );
  360. DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
  361. hv, id, type );
  362. DBGC2_HDA ( hv, 0, msg->data, len );
  363. /* Post message */
  364. if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
  365. rc = -EHV ( status );
  366. DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
  367. hv, id, strerror ( rc ) );
  368. return rc;
  369. }
  370. return 0;
  371. }
  372. /**
  373. * Wait for received message
  374. *
  375. * @v hv Hyper-V hypervisor
  376. * @v sintx Synthetic interrupt number
  377. * @ret rc Return status code
  378. */
  379. int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
  380. struct hv_message *msg = &hv->message->received;
  381. struct hv_message *src = &hv->synic.message[sintx];
  382. unsigned int retries;
  383. size_t len;
  384. /* Wait for message to arrive */
  385. for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
  386. /* Check for message */
  387. if ( src->type ) {
  388. /* Copy message */
  389. memset ( msg, 0, sizeof ( *msg ) );
  390. len = src->len;
  391. assert ( len <= sizeof ( *msg ) );
  392. memcpy ( msg, src,
  393. ( offsetof ( typeof ( *msg ), data ) + len ) );
  394. DBGC2 ( hv, "HV %p SINT%d received message type "
  395. "%#08x:\n", hv, sintx,
  396. le32_to_cpu ( msg->type ) );
  397. DBGC2_HDA ( hv, 0, msg->data, len );
  398. /* Consume message */
  399. src->type = 0;
  400. return 0;
  401. }
  402. /* Trigger message delivery */
  403. wrmsr ( HV_X64_MSR_EOM, 0 );
  404. /* Delay */
  405. mdelay ( 1 );
  406. }
  407. DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
  408. hv, sintx );
  409. return -ETIMEDOUT;
  410. }
  411. /**
  412. * Signal event
  413. *
  414. * @v hv Hyper-V hypervisor
  415. * @v id Connection ID
  416. * @v flag Flag number
  417. * @ret rc Return status code
  418. */
  419. int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
  420. unsigned int flag ) {
  421. struct hv_signal_event *event = &hv->message->signalled;
  422. int status;
  423. int rc;
  424. /* Construct event */
  425. memset ( event, 0, sizeof ( *event ) );
  426. event->id = cpu_to_le32 ( id );
  427. event->flag = cpu_to_le16 ( flag );
  428. /* Signal event */
  429. if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
  430. rc = -EHV ( status );
  431. DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
  432. hv, id, strerror ( rc ) );
  433. return rc;
  434. }
  435. return 0;
  436. }
  437. /**
  438. * Probe root device
  439. *
  440. * @v rootdev Root device
  441. * @ret rc Return status code
  442. */
  443. static int hv_probe ( struct root_device *rootdev ) {
  444. struct hv_hypervisor *hv;
  445. int rc;
  446. /* Allocate and initialise structure */
  447. hv = zalloc ( sizeof ( *hv ) );
  448. if ( ! hv ) {
  449. rc = -ENOMEM;
  450. goto err_alloc;
  451. }
  452. /* Check we are running in Hyper-V */
  453. if ( ( rc = hv_check_hv ( hv ) ) != 0 )
  454. goto err_check_hv;
  455. /* Allocate pages */
  456. if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
  457. &hv->synic.event, NULL ) ) != 0 )
  458. goto err_alloc_pages;
  459. /* Allocate message buffer */
  460. if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
  461. goto err_alloc_message;
  462. /* Map hypercall page */
  463. if ( ( rc = hv_map_hypercall ( hv ) ) != 0 )
  464. goto err_map_hypercall;
  465. /* Map synthetic interrupt controller */
  466. if ( ( rc = hv_map_synic ( hv ) ) != 0 )
  467. goto err_map_synic;
  468. /* Probe Hyper-V devices */
  469. if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
  470. goto err_vmbus_probe;
  471. rootdev_set_drvdata ( rootdev, hv );
  472. return 0;
  473. vmbus_remove ( hv, &rootdev->dev );
  474. err_vmbus_probe:
  475. hv_unmap_synic ( hv );
  476. err_map_synic:
  477. hv_unmap_hypercall ( hv );
  478. err_map_hypercall:
  479. hv_free_message ( hv );
  480. err_alloc_message:
  481. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  482. NULL );
  483. err_alloc_pages:
  484. err_check_hv:
  485. free ( hv );
  486. err_alloc:
  487. return rc;
  488. }
  489. /**
  490. * Remove root device
  491. *
  492. * @v rootdev Root device
  493. */
  494. static void hv_remove ( struct root_device *rootdev ) {
  495. struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
  496. vmbus_remove ( hv, &rootdev->dev );
  497. hv_unmap_synic ( hv );
  498. hv_unmap_hypercall ( hv );
  499. hv_free_message ( hv );
  500. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  501. NULL );
  502. free ( hv );
  503. }
  504. /** Hyper-V root device driver */
  505. static struct root_driver hv_root_driver = {
  506. .probe = hv_probe,
  507. .remove = hv_remove,
  508. };
  509. /** Hyper-V root device */
  510. struct root_device hv_root_device __root_device = {
  511. .dev = { .name = "Hyper-V" },
  512. .driver = &hv_root_driver,
  513. };
  514. /* Drag in netvsc driver */
  515. REQUIRE_OBJECT ( netvsc );