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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. /* Check for presence of a hypervisor (not necessarily Hyper-V) */
  137. x86_features ( &features );
  138. if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
  139. DBGC ( hv, "HV %p not running in a hypervisor\n", hv );
  140. return -ENODEV;
  141. }
  142. /* Check that hypervisor is Hyper-V */
  143. cpuid ( HV_CPUID_INTERFACE_ID, &interface_id, &discard_ebx,
  144. &discard_ecx, &discard_edx );
  145. if ( interface_id != HV_INTERFACE_ID ) {
  146. DBGC ( hv, "HV %p not running in Hyper-V (interface ID "
  147. "%#08x)\n", hv, interface_id );
  148. return -ENODEV;
  149. }
  150. return 0;
  151. }
  152. /**
  153. * Map hypercall page
  154. *
  155. * @v hv Hyper-V hypervisor
  156. * @ret rc Return status code
  157. */
  158. static int hv_map_hypercall ( struct hv_hypervisor *hv ) {
  159. union {
  160. struct {
  161. uint32_t ebx;
  162. uint32_t ecx;
  163. uint32_t edx;
  164. } __attribute__ (( packed ));
  165. char text[ 13 /* "bbbbccccdddd" + NUL */ ];
  166. } vendor_id;
  167. uint32_t build;
  168. uint32_t version;
  169. uint32_t discard_eax;
  170. uint32_t discard_ecx;
  171. uint32_t discard_edx;
  172. uint64_t guest_os_id;
  173. uint64_t hypercall;
  174. /* Report guest OS identity */
  175. guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
  176. if ( guest_os_id != 0 ) {
  177. DBGC ( hv, "HV %p guest OS ID MSR already set to %#08llx\n",
  178. hv, guest_os_id );
  179. return -EBUSY;
  180. }
  181. guest_os_id = HV_GUEST_OS_ID_IPXE;
  182. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  183. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  184. /* Get hypervisor system identity (for debugging) */
  185. cpuid ( HV_CPUID_VENDOR_ID, &discard_eax, &vendor_id.ebx,
  186. &vendor_id.ecx, &vendor_id.edx );
  187. vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
  188. cpuid ( HV_CPUID_HYPERVISOR_ID, &build, &version, &discard_ecx,
  189. &discard_edx );
  190. DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
  191. vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
  192. /* Map hypercall page */
  193. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  194. hypercall &= ( PAGE_SIZE - 1 );
  195. hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
  196. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  197. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  198. return 0;
  199. }
  200. /**
  201. * Unmap hypercall page
  202. *
  203. * @v hv Hyper-V hypervisor
  204. */
  205. static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
  206. uint64_t hypercall;
  207. uint64_t guest_os_id;
  208. /* Unmap the hypercall page */
  209. hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
  210. hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
  211. DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
  212. wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
  213. /* Reset the guest OS identity */
  214. guest_os_id = 0;
  215. DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
  216. wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
  217. }
  218. /**
  219. * Map synthetic interrupt controller
  220. *
  221. * @v hv Hyper-V hypervisor
  222. * @ret rc Return status code
  223. */
  224. static int hv_map_synic ( struct hv_hypervisor *hv ) {
  225. uint64_t simp;
  226. uint64_t siefp;
  227. uint64_t scontrol;
  228. /* Map SynIC message page */
  229. simp = rdmsr ( HV_X64_MSR_SIMP );
  230. simp &= ( PAGE_SIZE - 1 );
  231. simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
  232. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  233. wrmsr ( HV_X64_MSR_SIMP, simp );
  234. /* Map SynIC event page */
  235. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  236. siefp &= ( PAGE_SIZE - 1 );
  237. siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
  238. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  239. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  240. /* Enable SynIC */
  241. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  242. scontrol |= HV_SCONTROL_ENABLE;
  243. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  244. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  245. return 0;
  246. }
  247. /**
  248. * Unmap synthetic interrupt controller
  249. *
  250. * @v hv Hyper-V hypervisor
  251. */
  252. static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
  253. uint64_t scontrol;
  254. uint64_t siefp;
  255. uint64_t simp;
  256. /* Disable SynIC */
  257. scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
  258. scontrol &= ~HV_SCONTROL_ENABLE;
  259. DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
  260. wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
  261. /* Unmap SynIC event page */
  262. siefp = rdmsr ( HV_X64_MSR_SIEFP );
  263. siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
  264. DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
  265. wrmsr ( HV_X64_MSR_SIEFP, siefp );
  266. /* Unmap SynIC message page */
  267. simp = rdmsr ( HV_X64_MSR_SIMP );
  268. simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
  269. DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
  270. wrmsr ( HV_X64_MSR_SIMP, simp );
  271. }
  272. /**
  273. * Enable synthetic interrupt
  274. *
  275. * @v hv Hyper-V hypervisor
  276. * @v sintx Synthetic interrupt number
  277. */
  278. void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  279. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  280. uint64_t sint;
  281. /* Enable synthetic interrupt
  282. *
  283. * We have to enable the interrupt, otherwise messages will
  284. * not be delivered (even though the documentation implies
  285. * that polling for messages is possible). We enable AutoEOI
  286. * and hook the interrupt to the obsolete IRQ13 (FPU
  287. * exception) vector, which will be implemented as a no-op.
  288. */
  289. sint = rdmsr ( msr );
  290. sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
  291. sint |= ( HV_SINT_AUTO_EOI |
  292. HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
  293. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  294. wrmsr ( msr, sint );
  295. }
  296. /**
  297. * Disable synthetic interrupt
  298. *
  299. * @v hv Hyper-V hypervisor
  300. * @v sintx Synthetic interrupt number
  301. */
  302. void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
  303. unsigned long msr = HV_X64_MSR_SINT ( sintx );
  304. uint64_t sint;
  305. /* Disable synthetic interrupt */
  306. sint = rdmsr ( msr );
  307. sint &= ~HV_SINT_AUTO_EOI;
  308. sint |= HV_SINT_MASKED;
  309. DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
  310. wrmsr ( msr, sint );
  311. }
  312. /**
  313. * Post message
  314. *
  315. * @v hv Hyper-V hypervisor
  316. * @v id Connection ID
  317. * @v type Message type
  318. * @v data Message
  319. * @v len Length of message
  320. * @ret rc Return status code
  321. */
  322. int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
  323. unsigned int type, const void *data, size_t len ) {
  324. struct hv_post_message *msg = &hv->message->posted;
  325. int status;
  326. int rc;
  327. /* Sanity check */
  328. assert ( len <= sizeof ( msg->data ) );
  329. /* Construct message */
  330. memset ( msg, 0, sizeof ( *msg ) );
  331. msg->id = cpu_to_le32 ( id );
  332. msg->type = cpu_to_le32 ( type );
  333. msg->len = cpu_to_le32 ( len );
  334. memcpy ( msg->data, data, len );
  335. DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
  336. hv, id, type );
  337. DBGC2_HDA ( hv, 0, msg->data, len );
  338. /* Post message */
  339. if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
  340. rc = -EHV ( status );
  341. DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
  342. hv, id, strerror ( rc ) );
  343. return rc;
  344. }
  345. return 0;
  346. }
  347. /**
  348. * Wait for received message
  349. *
  350. * @v hv Hyper-V hypervisor
  351. * @v sintx Synthetic interrupt number
  352. * @ret rc Return status code
  353. */
  354. int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
  355. struct hv_message *msg = &hv->message->received;
  356. struct hv_message *src = &hv->synic.message[sintx];
  357. unsigned int retries;
  358. size_t len;
  359. /* Wait for message to arrive */
  360. for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
  361. /* Check for message */
  362. if ( src->type ) {
  363. /* Copy message */
  364. memset ( msg, 0, sizeof ( *msg ) );
  365. len = src->len;
  366. assert ( len <= sizeof ( *msg ) );
  367. memcpy ( msg, src,
  368. ( offsetof ( typeof ( *msg ), data ) + len ) );
  369. DBGC2 ( hv, "HV %p SINT%d received message type "
  370. "%#08x:\n", hv, sintx,
  371. le32_to_cpu ( msg->type ) );
  372. DBGC2_HDA ( hv, 0, msg->data, len );
  373. /* Consume message */
  374. src->type = 0;
  375. return 0;
  376. }
  377. /* Trigger message delivery */
  378. wrmsr ( HV_X64_MSR_EOM, 0 );
  379. /* Delay */
  380. mdelay ( 1 );
  381. }
  382. DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
  383. hv, sintx );
  384. return -ETIMEDOUT;
  385. }
  386. /**
  387. * Signal event
  388. *
  389. * @v hv Hyper-V hypervisor
  390. * @v id Connection ID
  391. * @v flag Flag number
  392. * @ret rc Return status code
  393. */
  394. int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
  395. unsigned int flag ) {
  396. struct hv_signal_event *event = &hv->message->signalled;
  397. int status;
  398. int rc;
  399. /* Construct event */
  400. memset ( event, 0, sizeof ( *event ) );
  401. event->id = cpu_to_le32 ( id );
  402. event->flag = cpu_to_le16 ( flag );
  403. /* Signal event */
  404. if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
  405. rc = -EHV ( status );
  406. DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
  407. hv, id, strerror ( rc ) );
  408. return rc;
  409. }
  410. return 0;
  411. }
  412. /**
  413. * Probe root device
  414. *
  415. * @v rootdev Root device
  416. * @ret rc Return status code
  417. */
  418. static int hv_probe ( struct root_device *rootdev ) {
  419. struct hv_hypervisor *hv;
  420. int rc;
  421. /* Allocate and initialise structure */
  422. hv = zalloc ( sizeof ( *hv ) );
  423. if ( ! hv ) {
  424. rc = -ENOMEM;
  425. goto err_alloc;
  426. }
  427. /* Check we are running in Hyper-V */
  428. if ( ( rc = hv_check_hv ( hv ) ) != 0 )
  429. goto err_check_hv;
  430. /* Allocate pages */
  431. if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
  432. &hv->synic.event, NULL ) ) != 0 )
  433. goto err_alloc_pages;
  434. /* Allocate message buffer */
  435. if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
  436. goto err_alloc_message;
  437. /* Map hypercall page */
  438. if ( ( rc = hv_map_hypercall ( hv ) ) != 0 )
  439. goto err_map_hypercall;
  440. /* Map synthetic interrupt controller */
  441. if ( ( rc = hv_map_synic ( hv ) ) != 0 )
  442. goto err_map_synic;
  443. /* Probe Hyper-V devices */
  444. if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
  445. goto err_vmbus_probe;
  446. rootdev_set_drvdata ( rootdev, hv );
  447. return 0;
  448. vmbus_remove ( hv, &rootdev->dev );
  449. err_vmbus_probe:
  450. hv_unmap_synic ( hv );
  451. err_map_synic:
  452. hv_unmap_hypercall ( hv );
  453. err_map_hypercall:
  454. hv_free_message ( hv );
  455. err_alloc_message:
  456. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  457. NULL );
  458. err_alloc_pages:
  459. err_check_hv:
  460. free ( hv );
  461. err_alloc:
  462. return rc;
  463. }
  464. /**
  465. * Remove root device
  466. *
  467. * @v rootdev Root device
  468. */
  469. static void hv_remove ( struct root_device *rootdev ) {
  470. struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
  471. vmbus_remove ( hv, &rootdev->dev );
  472. hv_unmap_synic ( hv );
  473. hv_unmap_hypercall ( hv );
  474. hv_free_message ( hv );
  475. hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
  476. NULL );
  477. free ( hv );
  478. }
  479. /** Hyper-V root device driver */
  480. static struct root_driver hv_root_driver = {
  481. .probe = hv_probe,
  482. .remove = hv_remove,
  483. };
  484. /** Hyper-V root device */
  485. struct root_device hv_root_device __root_device = {
  486. .dev = { .name = "Hyper-V" },
  487. .driver = &hv_root_driver,
  488. };
  489. /* Drag in netvsc driver */
  490. REQUIRE_OBJECT ( netvsc );