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.

hyperv.c 15KB

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