[50953] | 1 | /* $Id: GIMHv.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * GIM - Guest Interface Manager, Hyper-V implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2014-2020 Oracle Corporation
|
---|
[50953] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[57358] | 18 |
|
---|
| 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[50953] | 22 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
[63648] | 23 | #include <VBox/vmm/apic.h>
|
---|
[61632] | 24 | #include <VBox/vmm/gim.h>
|
---|
| 25 | #include <VBox/vmm/cpum.h>
|
---|
| 26 | #include <VBox/vmm/mm.h>
|
---|
| 27 | #include <VBox/vmm/ssm.h>
|
---|
| 28 | #include <VBox/vmm/hm.h>
|
---|
| 29 | #include <VBox/vmm/pdmapi.h>
|
---|
[72462] | 30 | #include <VBox/vmm/em.h>
|
---|
[50953] | 31 | #include "GIMInternal.h"
|
---|
[61632] | 32 | #include <VBox/vmm/vm.h>
|
---|
[50953] | 33 |
|
---|
[76402] | 34 | #include <VBox/err.h>
|
---|
[61632] | 35 | #include <VBox/version.h>
|
---|
| 36 |
|
---|
[50953] | 37 | #include <iprt/assert.h>
|
---|
[51333] | 38 | #include <iprt/string.h>
|
---|
[51560] | 39 | #include <iprt/mem.h>
|
---|
[57989] | 40 | #include <iprt/semaphore.h>
|
---|
[51643] | 41 | #include <iprt/spinlock.h>
|
---|
[58390] | 42 | #ifdef DEBUG_ramshankar
|
---|
| 43 | # include <iprt/udp.h>
|
---|
| 44 | #endif
|
---|
[50953] | 45 |
|
---|
[52761] | 46 |
|
---|
[57358] | 47 | /*********************************************************************************************************************************
|
---|
| 48 | * Defined Constants And Macros *
|
---|
| 49 | *********************************************************************************************************************************/
|
---|
[52761] | 50 | /**
|
---|
| 51 | * GIM Hyper-V saved-state version.
|
---|
| 52 | */
|
---|
[72193] | 53 | #define GIM_HV_SAVED_STATE_VERSION UINT32_C(4)
|
---|
| 54 | /** Saved states, priot to saving debug UDP source/destination ports. */
|
---|
| 55 | #define GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG_UDP_PORTS UINT32_C(3)
|
---|
[63648] | 56 | /** Saved states, prior to any synthetic interrupt controller support. */
|
---|
[72193] | 57 | #define GIM_HV_SAVED_STATE_VERSION_PRE_SYNIC UINT32_C(2)
|
---|
[58331] | 58 | /** Vanilla saved states, prior to any debug support. */
|
---|
[72193] | 59 | #define GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG UINT32_C(1)
|
---|
[52761] | 60 |
|
---|
[51367] | 61 | #ifdef VBOX_WITH_STATISTICS
|
---|
| 62 | # define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
| 63 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
|
---|
| 64 | #else
|
---|
| 65 | # define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
| 66 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName }
|
---|
| 67 | #endif
|
---|
[50953] | 68 |
|
---|
[57989] | 69 |
|
---|
| 70 | /*********************************************************************************************************************************
|
---|
| 71 | * Global Variables *
|
---|
| 72 | *********************************************************************************************************************************/
|
---|
[51367] | 73 | /**
|
---|
| 74 | * Array of MSR ranges supported by Hyper-V.
|
---|
| 75 | */
|
---|
| 76 | static CPUMMSRRANGE const g_aMsrRanges_HyperV[] =
|
---|
| 77 | {
|
---|
[73340] | 78 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE0_FIRST, MSR_GIM_HV_RANGE0_LAST, "Hyper-V range 0"),
|
---|
| 79 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE1_FIRST, MSR_GIM_HV_RANGE1_LAST, "Hyper-V range 1"),
|
---|
| 80 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE2_FIRST, MSR_GIM_HV_RANGE2_LAST, "Hyper-V range 2"),
|
---|
| 81 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE3_FIRST, MSR_GIM_HV_RANGE3_LAST, "Hyper-V range 3"),
|
---|
| 82 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE4_FIRST, MSR_GIM_HV_RANGE4_LAST, "Hyper-V range 4"),
|
---|
| 83 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE5_FIRST, MSR_GIM_HV_RANGE5_LAST, "Hyper-V range 5"),
|
---|
| 84 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE6_FIRST, MSR_GIM_HV_RANGE6_LAST, "Hyper-V range 6"),
|
---|
| 85 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE7_FIRST, MSR_GIM_HV_RANGE7_LAST, "Hyper-V range 7"),
|
---|
| 86 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE8_FIRST, MSR_GIM_HV_RANGE8_LAST, "Hyper-V range 8"),
|
---|
| 87 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE9_FIRST, MSR_GIM_HV_RANGE9_LAST, "Hyper-V range 9"),
|
---|
| 88 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE10_FIRST, MSR_GIM_HV_RANGE10_LAST, "Hyper-V range 10"),
|
---|
| 89 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE11_FIRST, MSR_GIM_HV_RANGE11_LAST, "Hyper-V range 11"),
|
---|
| 90 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE12_FIRST, MSR_GIM_HV_RANGE12_LAST, "Hyper-V range 12")
|
---|
[51367] | 91 | };
|
---|
[54667] | 92 | #undef GIMHV_MSRRANGE
|
---|
[50953] | 93 |
|
---|
[58313] | 94 | /**
|
---|
| 95 | * DHCP OFFER packet response to the guest (client) over the Hyper-V debug
|
---|
| 96 | * transport.
|
---|
| 97 | *
|
---|
| 98 | * - MAC: Destination: broadcast.
|
---|
| 99 | * - MAC: Source: 00:00:00:00:01 (hypervisor). It's important that it's
|
---|
| 100 | * different from the client's MAC address which is all 0's.
|
---|
| 101 | * - IP: Source: 10.0.5.1 (hypervisor)
|
---|
| 102 | * - IP: Destination: broadcast.
|
---|
| 103 | * - IP: Checksum included.
|
---|
| 104 | * - BOOTP: Client IP address: 10.0.5.5.
|
---|
| 105 | * - BOOTP: Server IP address: 10.0.5.1.
|
---|
| 106 | * - DHCP options: Subnet mask, router, lease-time, DHCP server identifier.
|
---|
| 107 | * Options are kept to a minimum required for making Windows guests happy.
|
---|
| 108 | */
|
---|
| 109 | #define GIMHV_DEBUGCLIENT_IPV4 RT_H2N_U32_C(0x0a000505) /* 10.0.5.5 */
|
---|
| 110 | #define GIMHV_DEBUGSERVER_IPV4 RT_H2N_U32_C(0x0a000501) /* 10.0.5.1 */
|
---|
| 111 | static const uint8_t g_abDhcpOffer[] =
|
---|
| 112 | {
|
---|
| 113 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x45, 0x10,
|
---|
| 114 | 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0x6a, 0xb5, 0x0a, 0x00, 0x05, 0x01, 0xff, 0xff,
|
---|
| 115 | 0xff, 0xff, 0x00, 0x43, 0x00, 0x44, 0x01, 0x14, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x00, 0x00,
|
---|
| 116 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x05, 0x05, 0x00, 0x00,
|
---|
| 117 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 118 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 119 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 120 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 121 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 122 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 123 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 124 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 125 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 126 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 127 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 128 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 129 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 130 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x02, 0x01, 0x04, 0xff,
|
---|
| 131 | 0xff, 0xff, 0x00, 0x03, 0x04, 0x0a, 0x00, 0x05, 0x01, 0x33, 0x04, 0xff, 0xff, 0xff, 0xff, 0x36,
|
---|
| 132 | 0x04, 0x0a, 0x00, 0x05, 0x01, 0xff
|
---|
| 133 | };
|
---|
[51367] | 134 |
|
---|
[58313] | 135 | /**
|
---|
| 136 | * DHCP ACK packet response to the guest (client) over the Hyper-V debug
|
---|
| 137 | * transport.
|
---|
| 138 | *
|
---|
| 139 | * - MAC: Destination: 00:00:00:00:00 (client).
|
---|
| 140 | * - IP: Destination: 10.0.5.5 (client).
|
---|
| 141 | * - Rest are mostly similar to the DHCP offer.
|
---|
| 142 | */
|
---|
| 143 | static const uint8_t g_abDhcpAck[] =
|
---|
| 144 | {
|
---|
| 145 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x45, 0x10,
|
---|
| 146 | 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0x5b, 0xb0, 0x0a, 0x00, 0x05, 0x01, 0x0a, 0x00,
|
---|
| 147 | 0x05, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x14, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x00, 0x00,
|
---|
| 148 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x05, 0x05, 0x0a, 0x00, 0x05, 0x05, 0x00, 0x00,
|
---|
| 149 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 150 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 151 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 152 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 153 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 154 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 155 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 156 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 157 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 158 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 159 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 160 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 161 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 162 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x01, 0x04, 0xff,
|
---|
| 163 | 0xff, 0xff, 0x00, 0x03, 0x04, 0x0a, 0x00, 0x05, 0x01, 0x33, 0x04, 0xff, 0xff, 0xff, 0xff, 0x36,
|
---|
| 164 | 0x04, 0x0a, 0x00, 0x05, 0x01, 0xff
|
---|
| 165 | };
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * ARP reply to the guest (client) over the Hyper-V debug transport.
|
---|
| 169 | *
|
---|
| 170 | * - MAC: Destination: 00:00:00:00:00 (client)
|
---|
| 171 | * - MAC: Source: 00:00:00:00:01 (hypervisor)
|
---|
| 172 | * - ARP: Reply: 10.0.5.1 is at Source MAC address.
|
---|
| 173 | */
|
---|
| 174 | static const uint8_t g_abArpReply[] =
|
---|
| 175 | {
|
---|
| 176 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x01,
|
---|
| 177 | 0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x05, 0x01,
|
---|
| 178 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x05, 0x05
|
---|
| 179 | };
|
---|
| 180 |
|
---|
| 181 |
|
---|
[57989] | 182 | /*********************************************************************************************************************************
|
---|
| 183 | * Internal Functions *
|
---|
| 184 | *********************************************************************************************************************************/
|
---|
[58248] | 185 | static int gimR3HvInitHypercallSupport(PVM pVM);
|
---|
| 186 | static void gimR3HvTermHypercallSupport(PVM pVM);
|
---|
[58390] | 187 | static int gimR3HvInitDebugSupport(PVM pVM);
|
---|
| 188 | static void gimR3HvTermDebugSupport(PVM pVM);
|
---|
[63648] | 189 | static DECLCALLBACK(void) gimR3HvTimerCallback(PVM pVM, PTMTIMER pTimer, void *pvUser);
|
---|
[57989] | 190 |
|
---|
[51333] | 191 | /**
|
---|
| 192 | * Initializes the Hyper-V GIM provider.
|
---|
| 193 | *
|
---|
| 194 | * @returns VBox status code.
|
---|
[58122] | 195 | * @param pVM The cross context VM structure.
|
---|
[58251] | 196 | * @param pGimCfg The GIM CFGM node.
|
---|
[51333] | 197 | */
|
---|
[58248] | 198 | VMMR3_INT_DECL(int) gimR3HvInit(PVM pVM, PCFGMNODE pGimCfg)
|
---|
[50953] | 199 | {
|
---|
| 200 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
[51333] | 201 | AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_HYPERV, VERR_INTERNAL_ERROR_5);
|
---|
[50953] | 202 |
|
---|
[51560] | 203 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
[50953] | 204 |
|
---|
| 205 | /*
|
---|
[57506] | 206 | * Read configuration.
|
---|
| 207 | */
|
---|
[58248] | 208 | PCFGMNODE pCfgHv = CFGMR3GetChild(pGimCfg, "HyperV");
|
---|
| 209 | if (pCfgHv)
|
---|
| 210 | {
|
---|
| 211 | /*
|
---|
| 212 | * Validate the Hyper-V settings.
|
---|
| 213 | */
|
---|
[63648] | 214 | int rc2 = CFGMR3ValidateConfig(pCfgHv, "/HyperV/",
|
---|
[58283] | 215 | "VendorID"
|
---|
[58390] | 216 | "|VSInterface"
|
---|
| 217 | "|HypercallDebugInterface",
|
---|
[58248] | 218 | "" /* pszValidNodes */, "GIM/HyperV" /* pszWho */, 0 /* uInstance */);
|
---|
[63648] | 219 | if (RT_FAILURE(rc2))
|
---|
| 220 | return rc2;
|
---|
[58248] | 221 | }
|
---|
[57506] | 222 |
|
---|
| 223 | /** @cfgm{/GIM/HyperV/VendorID, string, 'VBoxVBoxVBox'}
|
---|
| 224 | * The Hyper-V vendor signature, must be 12 characters. */
|
---|
| 225 | char szVendor[13];
|
---|
[63648] | 226 | int rc = CFGMR3QueryStringDef(pCfgHv, "VendorID", szVendor, sizeof(szVendor), "VBoxVBoxVBox");
|
---|
[57506] | 227 | AssertLogRelRCReturn(rc, rc);
|
---|
[62642] | 228 | AssertLogRelMsgReturn(strlen(szVendor) == 12,
|
---|
| 229 | ("The VendorID config value must be exactly 12 chars, '%s' isn't!\n", szVendor),
|
---|
| 230 | VERR_INVALID_PARAMETER);
|
---|
[57506] | 231 |
|
---|
[58008] | 232 | LogRel(("GIM: HyperV: Reporting vendor as '%s'\n", szVendor));
|
---|
[62642] | 233 | /** @todo r=bird: GIM_HV_VENDOR_MICROSOFT is 12 char and the string is max
|
---|
| 234 | * 12+terminator, so the NCmp is a little bit misleading. */
|
---|
[57989] | 235 | if (!RTStrNCmp(szVendor, GIM_HV_VENDOR_MICROSOFT, sizeof(GIM_HV_VENDOR_MICROSOFT) - 1))
|
---|
[58007] | 236 | {
|
---|
[58390] | 237 | LogRel(("GIM: HyperV: Warning! Posing as the Microsoft vendor may alter guest behaviour!\n"));
|
---|
[57989] | 238 | pHv->fIsVendorMsHv = true;
|
---|
[58007] | 239 | }
|
---|
[57989] | 240 |
|
---|
[58448] | 241 | /** @cfgm{/GIM/HyperV/VSInterface, bool, true}
|
---|
| 242 | * The Microsoft virtualization service interface (debugging). */
|
---|
| 243 | rc = CFGMR3QueryBoolDef(pCfgHv, "VSInterface", &pHv->fIsInterfaceVs, false);
|
---|
| 244 | AssertLogRelRCReturn(rc, rc);
|
---|
[58390] | 245 |
|
---|
[58448] | 246 | /** @cfgm{/GIM/HyperV/HypercallDebugInterface, bool, false}
|
---|
| 247 | * Whether we specify the guest to use hypercalls for debugging rather than MSRs. */
|
---|
| 248 | rc = CFGMR3QueryBoolDef(pCfgHv, "HypercallDebugInterface", &pHv->fDbgHypercallInterface, false);
|
---|
| 249 | AssertLogRelRCReturn(rc, rc);
|
---|
[58248] | 250 |
|
---|
[57506] | 251 | /*
|
---|
[51333] | 252 | * Determine interface capabilities based on the version.
|
---|
| 253 | */
|
---|
| 254 | if (!pVM->gim.s.u32Version)
|
---|
| 255 | {
|
---|
[51981] | 256 | /* Basic features. */
|
---|
[51560] | 257 | pHv->uBaseFeat = 0
|
---|
[79080] | 258 | //| GIM_HV_BASE_FEAT_VP_RUNTIME_MSR
|
---|
[51560] | 259 | | GIM_HV_BASE_FEAT_PART_TIME_REF_COUNT_MSR
|
---|
[79080] | 260 | //| GIM_HV_BASE_FEAT_BASIC_SYNIC_MSRS // Both required for synethetic timers
|
---|
| 261 | //| GIM_HV_BASE_FEAT_STIMER_MSRS // Both required for synethetic timers
|
---|
[51980] | 262 | | GIM_HV_BASE_FEAT_APIC_ACCESS_MSRS
|
---|
[51560] | 263 | | GIM_HV_BASE_FEAT_HYPERCALL_MSRS
|
---|
| 264 | | GIM_HV_BASE_FEAT_VP_ID_MSR
|
---|
[51686] | 265 | | GIM_HV_BASE_FEAT_VIRT_SYS_RESET_MSR
|
---|
[79080] | 266 | //| GIM_HV_BASE_FEAT_STAT_PAGES_MSR
|
---|
[51560] | 267 | | GIM_HV_BASE_FEAT_PART_REF_TSC_MSR
|
---|
[79080] | 268 | //| GIM_HV_BASE_FEAT_GUEST_IDLE_STATE_MSR
|
---|
[51560] | 269 | | GIM_HV_BASE_FEAT_TIMER_FREQ_MSRS
|
---|
[79080] | 270 | //| GIM_HV_BASE_FEAT_DEBUG_MSRS
|
---|
[51560] | 271 | ;
|
---|
[51367] | 272 |
|
---|
[51981] | 273 | /* Miscellaneous features. */
|
---|
[57989] | 274 | pHv->uMiscFeat = 0
|
---|
| 275 | //| GIM_HV_MISC_FEAT_GUEST_DEBUGGING
|
---|
| 276 | //| GIM_HV_MISC_FEAT_XMM_HYPERCALL_INPUT
|
---|
[79080] | 277 | | GIM_HV_MISC_FEAT_TIMER_FREQ
|
---|
| 278 | | GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS
|
---|
[57989] | 279 | //| GIM_HV_MISC_FEAT_DEBUG_MSRS
|
---|
[79080] | 280 | ;
|
---|
[51980] | 281 |
|
---|
[51981] | 282 | /* Hypervisor recommendations to the guest. */
|
---|
[52027] | 283 | pHv->uHyperHints = GIM_HV_HINT_MSR_FOR_SYS_RESET
|
---|
[63648] | 284 | | GIM_HV_HINT_RELAX_TIME_CHECKS
|
---|
[63849] | 285 | | GIM_HV_HINT_X2APIC_MSRS
|
---|
[63648] | 286 | ;
|
---|
[57989] | 287 |
|
---|
[72190] | 288 | /* Partition features. */
|
---|
| 289 | pHv->uPartFlags |= GIM_HV_PART_FLAGS_EXTENDED_HYPERCALLS;
|
---|
| 290 |
|
---|
[58283] | 291 | /* Expose more if we're posing as Microsoft. We can, if needed, force MSR-based Hv
|
---|
[58436] | 292 | debugging by not exposing these bits while exposing the VS interface. The better
|
---|
| 293 | way is what we do currently, via the GIM_HV_DEBUG_OPTIONS_USE_HYPERCALLS bit. */
|
---|
[58283] | 294 | if (pHv->fIsVendorMsHv)
|
---|
[57989] | 295 | {
|
---|
| 296 | pHv->uMiscFeat |= GIM_HV_MISC_FEAT_GUEST_DEBUGGING
|
---|
[72190] | 297 | | GIM_HV_MISC_FEAT_DEBUG_MSRS;
|
---|
[57989] | 298 |
|
---|
| 299 | pHv->uPartFlags |= GIM_HV_PART_FLAGS_DEBUGGING;
|
---|
| 300 | }
|
---|
[51333] | 301 | }
|
---|
| 302 |
|
---|
| 303 | /*
|
---|
[51560] | 304 | * Populate the required fields in MMIO2 region records for registering.
|
---|
| 305 | */
|
---|
[82210] | 306 | for (size_t i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
|
---|
| 307 | pHv->aMmio2Regions[i].hMmio2 = NIL_PGMMMIO2HANDLE;
|
---|
| 308 |
|
---|
[51560] | 309 | AssertCompile(GIM_HV_PAGE_SIZE == PAGE_SIZE);
|
---|
| 310 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
| 311 | pRegion->iRegion = GIM_HV_HYPERCALL_PAGE_REGION_IDX;
|
---|
[51643] | 312 | pRegion->fRCMapping = false;
|
---|
[56672] | 313 | pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
|
---|
[51560] | 314 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
[51643] | 315 | RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V hypercall page");
|
---|
[51560] | 316 |
|
---|
| 317 | pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
| 318 | pRegion->iRegion = GIM_HV_REF_TSC_PAGE_REGION_IDX;
|
---|
[51643] | 319 | pRegion->fRCMapping = false;
|
---|
[56672] | 320 | pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
|
---|
[51560] | 321 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
[51643] | 322 | RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V TSC page");
|
---|
[51560] | 323 |
|
---|
| 324 | /*
|
---|
[79079] | 325 | * Make sure the CPU ID bit are in accordance with the Hyper-V
|
---|
[51560] | 326 | * requirement and other paranoia checks.
|
---|
| 327 | * See "Requirements for implementing the Microsoft hypervisor interface" spec.
|
---|
| 328 | */
|
---|
| 329 | Assert(!(pHv->uPartFlags & ( GIM_HV_PART_FLAGS_CREATE_PART
|
---|
| 330 | | GIM_HV_PART_FLAGS_ACCESS_MEMORY_POOL
|
---|
| 331 | | GIM_HV_PART_FLAGS_ACCESS_PART_ID
|
---|
| 332 | | GIM_HV_PART_FLAGS_ADJUST_MSG_BUFFERS
|
---|
| 333 | | GIM_HV_PART_FLAGS_CREATE_PORT
|
---|
| 334 | | GIM_HV_PART_FLAGS_ACCESS_STATS
|
---|
| 335 | | GIM_HV_PART_FLAGS_CPU_MGMT
|
---|
| 336 | | GIM_HV_PART_FLAGS_CPU_PROFILER)));
|
---|
| 337 | Assert((pHv->uBaseFeat & (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR))
|
---|
| 338 | == (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR));
|
---|
[62641] | 339 | #ifdef VBOX_STRICT
|
---|
[51560] | 340 | for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
|
---|
| 341 | {
|
---|
[62641] | 342 | PCGIMMMIO2REGION pCur = &pHv->aMmio2Regions[i];
|
---|
| 343 | Assert(!pCur->fRCMapping);
|
---|
| 344 | Assert(!pCur->fMapped);
|
---|
| 345 | Assert(pCur->GCPhysPage == NIL_RTGCPHYS);
|
---|
[51560] | 346 | }
|
---|
[62641] | 347 | #endif
|
---|
[51560] | 348 |
|
---|
| 349 | /*
|
---|
[50953] | 350 | * Expose HVP (Hypervisor Present) bit to the guest.
|
---|
| 351 | */
|
---|
[61776] | 352 | CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
|
---|
[50953] | 353 |
|
---|
[51333] | 354 | /*
|
---|
| 355 | * Modify the standard hypervisor leaves for Hyper-V.
|
---|
| 356 | */
|
---|
| 357 | CPUMCPUIDLEAF HyperLeaf;
|
---|
| 358 | RT_ZERO(HyperLeaf);
|
---|
| 359 | HyperLeaf.uLeaf = UINT32_C(0x40000000);
|
---|
[63648] | 360 | if ( pHv->fIsVendorMsHv
|
---|
| 361 | && pHv->fIsInterfaceVs)
|
---|
| 362 | HyperLeaf.uEax = UINT32_C(0x40000082); /* Since we expose 0x40000082 below for the Hyper-V PV-debugging case. */
|
---|
| 363 | else
|
---|
| 364 | HyperLeaf.uEax = UINT32_C(0x40000006); /* Minimum value for Hyper-V default is 0x40000005. */
|
---|
[57989] | 365 | /*
|
---|
| 366 | * Don't report vendor as 'Microsoft Hv'[1] by default, see @bugref{7270#c152}.
|
---|
| 367 | * [1]: ebx=0x7263694d ('rciM') ecx=0x666f736f ('foso') edx=0x76482074 ('vH t')
|
---|
| 368 | */
|
---|
[57506] | 369 | {
|
---|
| 370 | uint32_t uVendorEbx;
|
---|
| 371 | uint32_t uVendorEcx;
|
---|
| 372 | uint32_t uVendorEdx;
|
---|
| 373 | uVendorEbx = ((uint32_t)szVendor[ 3]) << 24 | ((uint32_t)szVendor[ 2]) << 16 | ((uint32_t)szVendor[1]) << 8
|
---|
| 374 | | (uint32_t)szVendor[ 0];
|
---|
| 375 | uVendorEcx = ((uint32_t)szVendor[ 7]) << 24 | ((uint32_t)szVendor[ 6]) << 16 | ((uint32_t)szVendor[5]) << 8
|
---|
| 376 | | (uint32_t)szVendor[ 4];
|
---|
| 377 | uVendorEdx = ((uint32_t)szVendor[11]) << 24 | ((uint32_t)szVendor[10]) << 16 | ((uint32_t)szVendor[9]) << 8
|
---|
| 378 | | (uint32_t)szVendor[ 8];
|
---|
| 379 | HyperLeaf.uEbx = uVendorEbx;
|
---|
| 380 | HyperLeaf.uEcx = uVendorEcx;
|
---|
| 381 | HyperLeaf.uEdx = uVendorEdx;
|
---|
| 382 | }
|
---|
[51333] | 383 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 384 | AssertLogRelRCReturn(rc, rc);
|
---|
| 385 |
|
---|
| 386 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
| 387 | HyperLeaf.uEax = 0x31237648; /* 'Hv#1' */
|
---|
| 388 | HyperLeaf.uEbx = 0; /* Reserved */
|
---|
| 389 | HyperLeaf.uEcx = 0; /* Reserved */
|
---|
| 390 | HyperLeaf.uEdx = 0; /* Reserved */
|
---|
| 391 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 392 | AssertLogRelRCReturn(rc, rc);
|
---|
| 393 |
|
---|
| 394 | /*
|
---|
| 395 | * Add Hyper-V specific leaves.
|
---|
| 396 | */
|
---|
[51367] | 397 | HyperLeaf.uLeaf = UINT32_C(0x40000002); /* MBZ until MSR_GIM_HV_GUEST_OS_ID is set by the guest. */
|
---|
[51333] | 398 | HyperLeaf.uEax = 0;
|
---|
| 399 | HyperLeaf.uEbx = 0;
|
---|
| 400 | HyperLeaf.uEcx = 0;
|
---|
| 401 | HyperLeaf.uEdx = 0;
|
---|
| 402 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 403 | AssertLogRelRCReturn(rc, rc);
|
---|
| 404 |
|
---|
| 405 | HyperLeaf.uLeaf = UINT32_C(0x40000003);
|
---|
[51560] | 406 | HyperLeaf.uEax = pHv->uBaseFeat;
|
---|
| 407 | HyperLeaf.uEbx = pHv->uPartFlags;
|
---|
| 408 | HyperLeaf.uEcx = pHv->uPowMgmtFeat;
|
---|
| 409 | HyperLeaf.uEdx = pHv->uMiscFeat;
|
---|
[51333] | 410 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 411 | AssertLogRelRCReturn(rc, rc);
|
---|
| 412 |
|
---|
[51980] | 413 | HyperLeaf.uLeaf = UINT32_C(0x40000004);
|
---|
| 414 | HyperLeaf.uEax = pHv->uHyperHints;
|
---|
| 415 | HyperLeaf.uEbx = 0xffffffff;
|
---|
| 416 | HyperLeaf.uEcx = 0;
|
---|
| 417 | HyperLeaf.uEdx = 0;
|
---|
| 418 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 419 | AssertLogRelRCReturn(rc, rc);
|
---|
| 420 |
|
---|
[63648] | 421 | RT_ZERO(HyperLeaf);
|
---|
| 422 | HyperLeaf.uLeaf = UINT32_C(0x40000005);
|
---|
| 423 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 424 | AssertLogRelRCReturn(rc, rc);
|
---|
| 425 |
|
---|
| 426 | /* Leaf 0x40000006 is inserted in gimR3HvInitCompleted(). */
|
---|
| 427 |
|
---|
[58248] | 428 | if ( pHv->fIsVendorMsHv
|
---|
| 429 | && pHv->fIsInterfaceVs)
|
---|
| 430 | {
|
---|
| 431 | HyperLeaf.uLeaf = UINT32_C(0x40000080);
|
---|
| 432 | HyperLeaf.uEax = 0;
|
---|
| 433 | HyperLeaf.uEbx = 0x7263694d; /* 'rciM' */
|
---|
| 434 | HyperLeaf.uEcx = 0x666f736f; /* 'foso'*/
|
---|
| 435 | HyperLeaf.uEdx = 0x53562074; /* 'SV t' */
|
---|
| 436 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 437 | AssertLogRelRCReturn(rc, rc);
|
---|
| 438 |
|
---|
| 439 | HyperLeaf.uLeaf = UINT32_C(0x40000081);
|
---|
| 440 | HyperLeaf.uEax = 0x31235356; /* '1#SV' */
|
---|
| 441 | HyperLeaf.uEbx = 0;
|
---|
| 442 | HyperLeaf.uEcx = 0;
|
---|
| 443 | HyperLeaf.uEdx = 0;
|
---|
| 444 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 445 | AssertLogRelRCReturn(rc, rc);
|
---|
| 446 |
|
---|
| 447 | HyperLeaf.uLeaf = UINT32_C(0x40000082);
|
---|
| 448 | HyperLeaf.uEax = RT_BIT_32(1);
|
---|
| 449 | HyperLeaf.uEbx = 0;
|
---|
| 450 | HyperLeaf.uEcx = 0;
|
---|
| 451 | HyperLeaf.uEdx = 0;
|
---|
| 452 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 453 | AssertLogRelRCReturn(rc, rc);
|
---|
| 454 | }
|
---|
| 455 |
|
---|
[51333] | 456 | /*
|
---|
[51367] | 457 | * Insert all MSR ranges of Hyper-V.
|
---|
[51333] | 458 | */
|
---|
[51560] | 459 | for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_HyperV); i++)
|
---|
[51367] | 460 | {
|
---|
[63648] | 461 | int rc2 = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_HyperV[i]);
|
---|
| 462 | AssertLogRelRCReturn(rc2, rc2);
|
---|
[51367] | 463 | }
|
---|
[51560] | 464 |
|
---|
[57158] | 465 | /*
|
---|
| 466 | * Setup non-zero MSRs.
|
---|
| 467 | */
|
---|
| 468 | if (pHv->uMiscFeat & GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS)
|
---|
[62417] | 469 | pHv->uCrashCtlMsr = MSR_GIM_HV_CRASH_CTL_NOTIFY;
|
---|
[80191] | 470 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[62417] | 471 | {
|
---|
[80191] | 472 | PGIMHVCPU pHvCpu = &pVM->apCpusR3[idCpu]->gim.s.u.HvCpu;
|
---|
[63648] | 473 | for (uint8_t idxSintMsr = 0; idxSintMsr < RT_ELEMENTS(pHvCpu->auSintMsrs); idxSintMsr++)
|
---|
| 474 | pHvCpu->auSintMsrs[idxSintMsr] = MSR_GIM_HV_SINT_MASKED;
|
---|
[62417] | 475 | }
|
---|
[57158] | 476 |
|
---|
[57989] | 477 | /*
|
---|
[58283] | 478 | * Setup hypercall support.
|
---|
[57989] | 479 | */
|
---|
[58283] | 480 | rc = gimR3HvInitHypercallSupport(pVM);
|
---|
| 481 | AssertLogRelRCReturn(rc, rc);
|
---|
[57989] | 482 |
|
---|
[58390] | 483 | /*
|
---|
| 484 | * Setup debug support.
|
---|
| 485 | */
|
---|
| 486 | rc = gimR3HvInitDebugSupport(pVM);
|
---|
| 487 | AssertLogRelRCReturn(rc, rc);
|
---|
| 488 |
|
---|
[63648] | 489 | /*
|
---|
| 490 | * Setup up the per-VCPU synthetic timers.
|
---|
| 491 | */
|
---|
[64045] | 492 | if ( (pHv->uBaseFeat & GIM_HV_BASE_FEAT_STIMER_MSRS)
|
---|
| 493 | || (pHv->uBaseFeat & GIM_HV_BASE_FEAT_BASIC_SYNIC_MSRS))
|
---|
[63648] | 494 | {
|
---|
[64045] | 495 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[63648] | 496 | {
|
---|
[80191] | 497 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
[64045] | 498 | PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
|
---|
[63648] | 499 |
|
---|
[64045] | 500 | for (uint8_t idxStimer = 0; idxStimer < RT_ELEMENTS(pHvCpu->aStimers); idxStimer++)
|
---|
| 501 | {
|
---|
| 502 | PGIMHVSTIMER pHvStimer = &pHvCpu->aStimers[idxStimer];
|
---|
[63648] | 503 |
|
---|
[64045] | 504 | /* Associate the synthetic timer with its corresponding VCPU. */
|
---|
| 505 | pHvStimer->idCpu = pVCpu->idCpu;
|
---|
| 506 | pHvStimer->idxStimer = idxStimer;
|
---|
| 507 |
|
---|
| 508 | /* Create the timer and associate the context pointers. */
|
---|
| 509 | RTStrPrintf(&pHvStimer->szTimerDesc[0], sizeof(pHvStimer->szTimerDesc), "Hyper-V[%u] Timer%u", pVCpu->idCpu,
|
---|
| 510 | idxStimer);
|
---|
| 511 | rc = TMR3TimerCreateInternal(pVM, TMCLOCK_VIRTUAL_SYNC, gimR3HvTimerCallback, pHvStimer /* pvUser */,
|
---|
| 512 | pHvStimer->szTimerDesc, &pHvStimer->pTimerR3);
|
---|
| 513 | AssertLogRelRCReturn(rc, rc);
|
---|
| 514 | pHvStimer->pTimerR0 = TMTimerR0Ptr(pHvStimer->pTimerR3);
|
---|
| 515 | }
|
---|
[63648] | 516 | }
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /*
|
---|
| 520 | * Register statistics.
|
---|
| 521 | */
|
---|
| 522 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 523 | {
|
---|
[80191] | 524 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
[63648] | 525 | PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
|
---|
| 526 |
|
---|
| 527 | for (size_t idxStimer = 0; idxStimer < RT_ELEMENTS(pHvCpu->aStatStimerFired); idxStimer++)
|
---|
| 528 | {
|
---|
| 529 | int rc2 = STAMR3RegisterF(pVM, &pHvCpu->aStatStimerFired[idxStimer], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
| 530 | STAMUNIT_OCCURENCES, "Number of times the synthetic timer fired.",
|
---|
| 531 | "/GIM/HyperV/%u/Stimer%u_Fired", idCpu, idxStimer);
|
---|
| 532 | AssertLogRelRCReturn(rc2, rc2);
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[50953] | 536 | return VINF_SUCCESS;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 |
|
---|
[52699] | 540 | /**
|
---|
| 541 | * Initializes remaining bits of the Hyper-V provider.
|
---|
[52768] | 542 | *
|
---|
[52699] | 543 | * This is called after initializing HM and almost all other VMM components.
|
---|
| 544 | *
|
---|
| 545 | * @returns VBox status code.
|
---|
[58122] | 546 | * @param pVM The cross context VM structure.
|
---|
[52699] | 547 | */
|
---|
[54654] | 548 | VMMR3_INT_DECL(int) gimR3HvInitCompleted(PVM pVM)
|
---|
[52699] | 549 | {
|
---|
| 550 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
[56694] | 551 | pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
[52699] | 552 |
|
---|
| 553 | /*
|
---|
| 554 | * Determine interface capabilities based on the version.
|
---|
| 555 | */
|
---|
| 556 | if (!pVM->gim.s.u32Version)
|
---|
| 557 | {
|
---|
| 558 | /* Hypervisor capabilities; features used by the hypervisor. */
|
---|
[73389] | 559 | pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
|
---|
| 560 | pHv->uHyperCaps |= HMIsMsrBitmapActive(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
|
---|
[52699] | 561 | }
|
---|
| 562 |
|
---|
| 563 | CPUMCPUIDLEAF HyperLeaf;
|
---|
| 564 | RT_ZERO(HyperLeaf);
|
---|
| 565 | HyperLeaf.uLeaf = UINT32_C(0x40000006);
|
---|
| 566 | HyperLeaf.uEax = pHv->uHyperCaps;
|
---|
| 567 | HyperLeaf.uEbx = 0;
|
---|
| 568 | HyperLeaf.uEcx = 0;
|
---|
| 569 | HyperLeaf.uEdx = 0;
|
---|
| 570 | int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
| 571 | AssertLogRelRCReturn(rc, rc);
|
---|
| 572 |
|
---|
[63945] | 573 | /*
|
---|
| 574 | * Inform APIC whether Hyper-V compatibility mode is enabled or not.
|
---|
| 575 | * Do this here rather than on gimR3HvInit() as it gets called after APIC
|
---|
| 576 | * has finished inserting/removing the x2APIC MSR range.
|
---|
| 577 | */
|
---|
| 578 | if (pHv->uHyperHints & GIM_HV_HINT_X2APIC_MSRS)
|
---|
| 579 | APICR3HvSetCompatMode(pVM, true);
|
---|
| 580 |
|
---|
[52699] | 581 | return rc;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 |
|
---|
[51983] | 585 | /**
|
---|
| 586 | * Terminates the Hyper-V GIM provider.
|
---|
| 587 | *
|
---|
| 588 | * @returns VBox status code.
|
---|
[58122] | 589 | * @param pVM The cross context VM structure.
|
---|
[51983] | 590 | */
|
---|
[54654] | 591 | VMMR3_INT_DECL(int) gimR3HvTerm(PVM pVM)
|
---|
[51643] | 592 | {
|
---|
[54654] | 593 | gimR3HvReset(pVM);
|
---|
[58283] | 594 | gimR3HvTermHypercallSupport(pVM);
|
---|
[63648] | 595 | gimR3HvTermDebugSupport(pVM);
|
---|
| 596 |
|
---|
[64045] | 597 | PCGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 598 | if ( (pHv->uBaseFeat & GIM_HV_BASE_FEAT_STIMER_MSRS)
|
---|
| 599 | || (pHv->uBaseFeat & GIM_HV_BASE_FEAT_BASIC_SYNIC_MSRS))
|
---|
[63648] | 600 | {
|
---|
[64045] | 601 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[63648] | 602 | {
|
---|
[80191] | 603 | PGIMHVCPU pHvCpu = &pVM->apCpusR3[idCpu]->gim.s.u.HvCpu;
|
---|
[64045] | 604 | for (uint8_t idxStimer = 0; idxStimer < RT_ELEMENTS(pHvCpu->aStimers); idxStimer++)
|
---|
| 605 | {
|
---|
| 606 | PGIMHVSTIMER pHvStimer = &pHvCpu->aStimers[idxStimer];
|
---|
| 607 | TMR3TimerDestroy(pHvStimer->pTimerR3);
|
---|
| 608 | }
|
---|
[63648] | 609 | }
|
---|
| 610 | }
|
---|
| 611 |
|
---|
[51643] | 612 | return VINF_SUCCESS;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 |
|
---|
[51983] | 616 | /**
|
---|
[63648] | 617 | * Applies relocations to data and code managed by this
|
---|
| 618 | * component. This function will be called at init and
|
---|
| 619 | * whenever the VMM need to relocate it self inside the GC.
|
---|
| 620 | *
|
---|
| 621 | * @param pVM The cross context VM structure.
|
---|
| 622 | * @param offDelta Relocation delta relative to old location.
|
---|
| 623 | */
|
---|
| 624 | VMMR3_INT_DECL(void) gimR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
| 625 | {
|
---|
[80077] | 626 | RT_NOREF(pVM, offDelta);
|
---|
[63648] | 627 | }
|
---|
| 628 |
|
---|
| 629 |
|
---|
| 630 | /**
|
---|
[52768] | 631 | * This resets Hyper-V provider MSRs and unmaps whatever Hyper-V regions that
|
---|
| 632 | * the guest may have mapped.
|
---|
[51560] | 633 | *
|
---|
[52768] | 634 | * This is called when the VM is being reset.
|
---|
| 635 | *
|
---|
[58122] | 636 | * @param pVM The cross context VM structure.
|
---|
[57989] | 637 | *
|
---|
[64764] | 638 | * @thread EMT(0)
|
---|
[51560] | 639 | */
|
---|
[54654] | 640 | VMMR3_INT_DECL(void) gimR3HvReset(PVM pVM)
|
---|
[51560] | 641 | {
|
---|
[54701] | 642 | VM_ASSERT_EMT0(pVM);
|
---|
| 643 |
|
---|
[51560] | 644 | /*
|
---|
| 645 | * Unmap MMIO2 pages that the guest may have setup.
|
---|
| 646 | */
|
---|
[55439] | 647 | LogRel(("GIM: HyperV: Resetting MMIO2 regions and MSRs\n"));
|
---|
[51560] | 648 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 649 | for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
|
---|
| 650 | {
|
---|
| 651 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
|
---|
[56758] | 652 | #if 0
|
---|
[61771] | 653 | gimR3Mmio2Unmap(pVM, pRegion);
|
---|
[56758] | 654 | #else
|
---|
| 655 | pRegion->fMapped = false;
|
---|
| 656 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
| 657 | #endif
|
---|
[51560] | 658 | }
|
---|
| 659 |
|
---|
| 660 | /*
|
---|
[58283] | 661 | * Reset MSRs.
|
---|
[51560] | 662 | */
|
---|
[58390] | 663 | pHv->u64GuestOsIdMsr = 0;
|
---|
| 664 | pHv->u64HypercallMsr = 0;
|
---|
| 665 | pHv->u64TscPageMsr = 0;
|
---|
| 666 | pHv->uCrashP0Msr = 0;
|
---|
| 667 | pHv->uCrashP1Msr = 0;
|
---|
| 668 | pHv->uCrashP2Msr = 0;
|
---|
| 669 | pHv->uCrashP3Msr = 0;
|
---|
| 670 | pHv->uCrashP4Msr = 0;
|
---|
| 671 | pHv->uDbgStatusMsr = 0;
|
---|
| 672 | pHv->uDbgPendingBufferMsr = 0;
|
---|
| 673 | pHv->uDbgSendBufferMsr = 0;
|
---|
| 674 | pHv->uDbgRecvBufferMsr = 0;
|
---|
[80191] | 675 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[58283] | 676 | {
|
---|
[80191] | 677 | PGIMHVCPU pHvCpu = &pVM->apCpusR3[idCpu]->gim.s.u.HvCpu;
|
---|
[63648] | 678 | pHvCpu->uSControlMsr = 0;
|
---|
[62417] | 679 | pHvCpu->uSimpMsr = 0;
|
---|
| 680 | pHvCpu->uSiefpMsr = 0;
|
---|
| 681 | pHvCpu->uApicAssistPageMsr = 0;
|
---|
[63648] | 682 |
|
---|
| 683 | for (uint8_t idxSint = 0; idxSint < RT_ELEMENTS(pHvCpu->auSintMsrs); idxSint++)
|
---|
| 684 | pHvCpu->auSintMsrs[idxSint] = MSR_GIM_HV_SINT_MASKED;
|
---|
| 685 |
|
---|
| 686 | for (uint8_t idxStimer = 0; idxStimer < RT_ELEMENTS(pHvCpu->aStimers); idxStimer++)
|
---|
| 687 | {
|
---|
| 688 | PGIMHVSTIMER pHvStimer = &pHvCpu->aStimers[idxStimer];
|
---|
| 689 | pHvStimer->uStimerConfigMsr = 0;
|
---|
| 690 | pHvStimer->uStimerCountMsr = 0;
|
---|
| 691 | }
|
---|
[58283] | 692 | }
|
---|
[51560] | 693 | }
|
---|
| 694 |
|
---|
| 695 |
|
---|
| 696 | /**
|
---|
[58390] | 697 | * Callback for when debug data is available over the debugger connection.
|
---|
| 698 | *
|
---|
| 699 | * @param pVM The cross context VM structure.
|
---|
| 700 | */
|
---|
| 701 | static DECLCALLBACK(void) gimR3HvDebugBufAvail(PVM pVM)
|
---|
| 702 | {
|
---|
| 703 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 704 | RTGCPHYS GCPhysPendingBuffer = pHv->uDbgPendingBufferMsr;
|
---|
| 705 | if ( GCPhysPendingBuffer
|
---|
| 706 | && PGMPhysIsGCPhysNormal(pVM, GCPhysPendingBuffer))
|
---|
| 707 | {
|
---|
| 708 | uint8_t bPendingData = 1;
|
---|
| 709 | int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysPendingBuffer, &bPendingData, sizeof(bPendingData));
|
---|
| 710 | if (RT_FAILURE(rc))
|
---|
| 711 | {
|
---|
| 712 | LogRelMax(5, ("GIM: HyperV: Failed to set pending debug receive buffer at %#RGp, rc=%Rrc\n", GCPhysPendingBuffer,
|
---|
| 713 | rc));
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 |
|
---|
| 719 | /**
|
---|
| 720 | * Callback for when debug data has been read from the debugger connection.
|
---|
| 721 | *
|
---|
| 722 | * This will be invoked before signalling read of the next debug buffer.
|
---|
| 723 | *
|
---|
| 724 | * @param pVM The cross context VM structure.
|
---|
| 725 | */
|
---|
| 726 | static DECLCALLBACK(void) gimR3HvDebugBufReadCompleted(PVM pVM)
|
---|
| 727 | {
|
---|
| 728 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 729 | RTGCPHYS GCPhysPendingBuffer = pHv->uDbgPendingBufferMsr;
|
---|
| 730 | if ( GCPhysPendingBuffer
|
---|
| 731 | && PGMPhysIsGCPhysNormal(pVM, GCPhysPendingBuffer))
|
---|
| 732 | {
|
---|
| 733 | uint8_t bPendingData = 0;
|
---|
| 734 | int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysPendingBuffer, &bPendingData, sizeof(bPendingData));
|
---|
| 735 | if (RT_FAILURE(rc))
|
---|
| 736 | {
|
---|
| 737 | LogRelMax(5, ("GIM: HyperV: Failed to clear pending debug receive buffer at %#RGp, rc=%Rrc\n", GCPhysPendingBuffer,
|
---|
| 738 | rc));
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 |
|
---|
| 744 | /**
|
---|
| 745 | * Get Hyper-V debug setup parameters.
|
---|
| 746 | *
|
---|
| 747 | * @returns VBox status code.
|
---|
| 748 | * @param pVM The cross context VM structure.
|
---|
| 749 | * @param pDbgSetup Where to store the debug setup details.
|
---|
| 750 | */
|
---|
| 751 | VMMR3_INT_DECL(int) gimR3HvGetDebugSetup(PVM pVM, PGIMDEBUGSETUP pDbgSetup)
|
---|
| 752 | {
|
---|
| 753 | Assert(pDbgSetup);
|
---|
| 754 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 755 | if (pHv->fDbgEnabled)
|
---|
| 756 | {
|
---|
| 757 | pDbgSetup->pfnDbgRecvBufAvail = gimR3HvDebugBufAvail;
|
---|
| 758 | pDbgSetup->cbDbgRecvBuf = GIM_HV_PAGE_SIZE;
|
---|
| 759 | return VINF_SUCCESS;
|
---|
| 760 | }
|
---|
| 761 | return VERR_GIM_NO_DEBUG_CONNECTION;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 |
|
---|
| 765 | /**
|
---|
[51643] | 766 | * Hyper-V state-save operation.
|
---|
| 767 | *
|
---|
| 768 | * @returns VBox status code.
|
---|
[58122] | 769 | * @param pVM The cross context VM structure.
|
---|
[62641] | 770 | * @param pSSM The saved state handle.
|
---|
[51643] | 771 | */
|
---|
[54654] | 772 | VMMR3_INT_DECL(int) gimR3HvSave(PVM pVM, PSSMHANDLE pSSM)
|
---|
[51643] | 773 | {
|
---|
[62641] | 774 | PCGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
[51643] | 775 |
|
---|
[52010] | 776 | /*
|
---|
| 777 | * Save the Hyper-V SSM version.
|
---|
| 778 | */
|
---|
[52761] | 779 | SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
|
---|
[52010] | 780 |
|
---|
[51643] | 781 | /*
|
---|
| 782 | * Save per-VM MSRs.
|
---|
| 783 | */
|
---|
[62641] | 784 | SSMR3PutU64(pSSM, pHv->u64GuestOsIdMsr);
|
---|
| 785 | SSMR3PutU64(pSSM, pHv->u64HypercallMsr);
|
---|
| 786 | SSMR3PutU64(pSSM, pHv->u64TscPageMsr);
|
---|
[51643] | 787 |
|
---|
| 788 | /*
|
---|
| 789 | * Save Hyper-V features / capabilities.
|
---|
| 790 | */
|
---|
[62641] | 791 | SSMR3PutU32(pSSM, pHv->uBaseFeat);
|
---|
| 792 | SSMR3PutU32(pSSM, pHv->uPartFlags);
|
---|
| 793 | SSMR3PutU32(pSSM, pHv->uPowMgmtFeat);
|
---|
| 794 | SSMR3PutU32(pSSM, pHv->uMiscFeat);
|
---|
| 795 | SSMR3PutU32(pSSM, pHv->uHyperHints);
|
---|
| 796 | SSMR3PutU32(pSSM, pHv->uHyperCaps);
|
---|
[51643] | 797 |
|
---|
| 798 | /*
|
---|
[51686] | 799 | * Save the Hypercall region.
|
---|
[51643] | 800 | */
|
---|
[62641] | 801 | PCGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
| 802 | SSMR3PutU8(pSSM, pRegion->iRegion);
|
---|
| 803 | SSMR3PutBool(pSSM, pRegion->fRCMapping);
|
---|
| 804 | SSMR3PutU32(pSSM, pRegion->cbRegion);
|
---|
| 805 | SSMR3PutGCPhys(pSSM, pRegion->GCPhysPage);
|
---|
| 806 | SSMR3PutStrZ(pSSM, pRegion->szDescription);
|
---|
[51686] | 807 |
|
---|
| 808 | /*
|
---|
| 809 | * Save the reference TSC region.
|
---|
| 810 | */
|
---|
[62641] | 811 | pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
| 812 | SSMR3PutU8(pSSM, pRegion->iRegion);
|
---|
| 813 | SSMR3PutBool(pSSM, pRegion->fRCMapping);
|
---|
| 814 | SSMR3PutU32(pSSM, pRegion->cbRegion);
|
---|
| 815 | SSMR3PutGCPhys(pSSM, pRegion->GCPhysPage);
|
---|
| 816 | SSMR3PutStrZ(pSSM, pRegion->szDescription);
|
---|
[51686] | 817 | /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
|
---|
| 818 | uint32_t uTscSequence = 0;
|
---|
[62641] | 819 | if ( pRegion->fMapped
|
---|
| 820 | && MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
|
---|
[51643] | 821 | {
|
---|
[62641] | 822 | PCGIMHVREFTSC pRefTsc = (PCGIMHVREFTSC)pRegion->pvPageR3;
|
---|
| 823 | uTscSequence = pRefTsc->u32TscSequence;
|
---|
[51643] | 824 | }
|
---|
[58331] | 825 | SSMR3PutU32(pSSM, uTscSequence);
|
---|
[51643] | 826 |
|
---|
[58331] | 827 | /*
|
---|
| 828 | * Save debug support data.
|
---|
| 829 | */
|
---|
[62641] | 830 | SSMR3PutU64(pSSM, pHv->uDbgPendingBufferMsr);
|
---|
| 831 | SSMR3PutU64(pSSM, pHv->uDbgSendBufferMsr);
|
---|
| 832 | SSMR3PutU64(pSSM, pHv->uDbgRecvBufferMsr);
|
---|
| 833 | SSMR3PutU64(pSSM, pHv->uDbgStatusMsr);
|
---|
| 834 | SSMR3PutU32(pSSM, pHv->enmDbgReply);
|
---|
| 835 | SSMR3PutU32(pSSM, pHv->uDbgBootpXId);
|
---|
| 836 | SSMR3PutU32(pSSM, pHv->DbgGuestIp4Addr.u);
|
---|
[72193] | 837 | SSMR3PutU16(pSSM, pHv->uUdpGuestDstPort);
|
---|
| 838 | SSMR3PutU16(pSSM, pHv->uUdpGuestSrcPort);
|
---|
[58331] | 839 |
|
---|
[80191] | 840 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[58331] | 841 | {
|
---|
[80191] | 842 | PGIMHVCPU pHvCpu = &pVM->apCpusR3[idCpu]->gim.s.u.HvCpu;
|
---|
[58331] | 843 | SSMR3PutU64(pSSM, pHvCpu->uSimpMsr);
|
---|
[63648] | 844 | for (size_t idxSintMsr = 0; idxSintMsr < RT_ELEMENTS(pHvCpu->auSintMsrs); idxSintMsr++)
|
---|
| 845 | SSMR3PutU64(pSSM, pHvCpu->auSintMsrs[idxSintMsr]);
|
---|
[58331] | 846 | }
|
---|
| 847 |
|
---|
[62641] | 848 | return SSMR3PutU8(pSSM, UINT8_MAX);
|
---|
[51643] | 849 | }
|
---|
| 850 |
|
---|
| 851 |
|
---|
| 852 | /**
|
---|
| 853 | * Hyper-V state-load operation, final pass.
|
---|
| 854 | *
|
---|
| 855 | * @returns VBox status code.
|
---|
[58122] | 856 | * @param pVM The cross context VM structure.
|
---|
[62641] | 857 | * @param pSSM The saved state handle.
|
---|
[51643] | 858 | */
|
---|
[62641] | 859 | VMMR3_INT_DECL(int) gimR3HvLoad(PVM pVM, PSSMHANDLE pSSM)
|
---|
[51643] | 860 | {
|
---|
[52010] | 861 | /*
|
---|
| 862 | * Load the Hyper-V SSM version first.
|
---|
| 863 | */
|
---|
[63648] | 864 | uint32_t uHvSavedStateVersion;
|
---|
| 865 | int rc = SSMR3GetU32(pSSM, &uHvSavedStateVersion);
|
---|
[52761] | 866 | AssertRCReturn(rc, rc);
|
---|
[63648] | 867 | if ( uHvSavedStateVersion != GIM_HV_SAVED_STATE_VERSION
|
---|
[72193] | 868 | && uHvSavedStateVersion != GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG_UDP_PORTS
|
---|
[63648] | 869 | && uHvSavedStateVersion != GIM_HV_SAVED_STATE_VERSION_PRE_SYNIC
|
---|
| 870 | && uHvSavedStateVersion != GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG)
|
---|
[52761] | 871 | return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
|
---|
[62641] | 872 | N_("Unsupported Hyper-V saved-state version %u (current %u)!"),
|
---|
[63648] | 873 | uHvSavedStateVersion, GIM_HV_SAVED_STATE_VERSION);
|
---|
[52010] | 874 |
|
---|
[51643] | 875 | /*
|
---|
[56694] | 876 | * Update the TSC frequency from TM.
|
---|
| 877 | */
|
---|
| 878 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 879 | pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
| 880 |
|
---|
| 881 | /*
|
---|
[51643] | 882 | * Load per-VM MSRs.
|
---|
| 883 | */
|
---|
[52761] | 884 | SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
|
---|
| 885 | SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
|
---|
| 886 | SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
|
---|
[51643] | 887 |
|
---|
| 888 | /*
|
---|
[51982] | 889 | * Load Hyper-V features / capabilities.
|
---|
[51643] | 890 | */
|
---|
[52761] | 891 | SSMR3GetU32(pSSM, &pHv->uBaseFeat);
|
---|
| 892 | SSMR3GetU32(pSSM, &pHv->uPartFlags);
|
---|
| 893 | SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
|
---|
| 894 | SSMR3GetU32(pSSM, &pHv->uMiscFeat);
|
---|
| 895 | SSMR3GetU32(pSSM, &pHv->uHyperHints);
|
---|
| 896 | SSMR3GetU32(pSSM, &pHv->uHyperCaps);
|
---|
[51643] | 897 |
|
---|
| 898 | /*
|
---|
[51686] | 899 | * Load and enable the Hypercall region.
|
---|
[51643] | 900 | */
|
---|
| 901 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
[52761] | 902 | SSMR3GetU8(pSSM, &pRegion->iRegion);
|
---|
| 903 | SSMR3GetBool(pSSM, &pRegion->fRCMapping);
|
---|
| 904 | SSMR3GetU32(pSSM, &pRegion->cbRegion);
|
---|
| 905 | SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
|
---|
| 906 | rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
|
---|
[51686] | 907 | AssertRCReturn(rc, rc);
|
---|
[56672] | 908 |
|
---|
| 909 | if (pRegion->cbRegion != PAGE_SIZE)
|
---|
| 910 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall page region size %u invalid, expected %u"),
|
---|
| 911 | pRegion->cbRegion, PAGE_SIZE);
|
---|
| 912 |
|
---|
[58014] | 913 | if (MSR_GIM_HV_HYPERCALL_PAGE_IS_ENABLED(pHv->u64HypercallMsr))
|
---|
[51643] | 914 | {
|
---|
| 915 | Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
|
---|
[51686] | 916 | if (RT_LIKELY(pRegion->fRegistered))
|
---|
[51643] | 917 | {
|
---|
[54654] | 918 | rc = gimR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
|
---|
[51643] | 919 | if (RT_FAILURE(rc))
|
---|
| 920 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
|
---|
| 921 | pRegion->GCPhysPage, rc);
|
---|
| 922 | }
|
---|
| 923 | else
|
---|
| 924 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | /*
|
---|
[51686] | 928 | * Load and enable the reference TSC region.
|
---|
[51643] | 929 | */
|
---|
[51686] | 930 | uint32_t uTscSequence;
|
---|
[51643] | 931 | pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
[52761] | 932 | SSMR3GetU8(pSSM, &pRegion->iRegion);
|
---|
| 933 | SSMR3GetBool(pSSM, &pRegion->fRCMapping);
|
---|
| 934 | SSMR3GetU32(pSSM, &pRegion->cbRegion);
|
---|
| 935 | SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
|
---|
| 936 | SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
|
---|
| 937 | rc = SSMR3GetU32(pSSM, &uTscSequence);
|
---|
[51686] | 938 | AssertRCReturn(rc, rc);
|
---|
[56672] | 939 |
|
---|
| 940 | if (pRegion->cbRegion != PAGE_SIZE)
|
---|
| 941 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC page region size %u invalid, expected %u"),
|
---|
| 942 | pRegion->cbRegion, PAGE_SIZE);
|
---|
| 943 |
|
---|
[51643] | 944 | if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
|
---|
| 945 | {
|
---|
| 946 | Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
|
---|
| 947 | if (pRegion->fRegistered)
|
---|
| 948 | {
|
---|
[54654] | 949 | rc = gimR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
|
---|
[51643] | 950 | if (RT_FAILURE(rc))
|
---|
| 951 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
|
---|
| 952 | pRegion->GCPhysPage, rc);
|
---|
| 953 | }
|
---|
| 954 | else
|
---|
| 955 | return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
|
---|
| 956 | }
|
---|
| 957 |
|
---|
[58331] | 958 | /*
|
---|
| 959 | * Load the debug support data.
|
---|
| 960 | */
|
---|
[63648] | 961 | if (uHvSavedStateVersion > GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG)
|
---|
[58331] | 962 | {
|
---|
[58390] | 963 | SSMR3GetU64(pSSM, &pHv->uDbgPendingBufferMsr);
|
---|
| 964 | SSMR3GetU64(pSSM, &pHv->uDbgSendBufferMsr);
|
---|
| 965 | SSMR3GetU64(pSSM, &pHv->uDbgRecvBufferMsr);
|
---|
| 966 | SSMR3GetU64(pSSM, &pHv->uDbgStatusMsr);
|
---|
[81766] | 967 | SSM_GET_ENUM32_RET(pSSM, pHv->enmDbgReply, GIMHVDEBUGREPLY);
|
---|
[58390] | 968 | SSMR3GetU32(pSSM, &pHv->uDbgBootpXId);
|
---|
[58331] | 969 | rc = SSMR3GetU32(pSSM, &pHv->DbgGuestIp4Addr.u);
|
---|
| 970 | AssertRCReturn(rc, rc);
|
---|
[72193] | 971 | if (uHvSavedStateVersion > GIM_HV_SAVED_STATE_VERSION_PRE_DEBUG_UDP_PORTS)
|
---|
| 972 | {
|
---|
| 973 | rc = SSMR3GetU16(pSSM, &pHv->uUdpGuestDstPort); AssertRCReturn(rc, rc);
|
---|
| 974 | rc = SSMR3GetU16(pSSM, &pHv->uUdpGuestSrcPort); AssertRCReturn(rc, rc);
|
---|
| 975 | }
|
---|
[58331] | 976 |
|
---|
[80191] | 977 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
[58331] | 978 | {
|
---|
[80191] | 979 | PGIMHVCPU pHvCpu = &pVM->apCpusR3[idCpu]->gim.s.u.HvCpu;
|
---|
[58331] | 980 | SSMR3GetU64(pSSM, &pHvCpu->uSimpMsr);
|
---|
[63648] | 981 | if (uHvSavedStateVersion <= GIM_HV_SAVED_STATE_VERSION_PRE_SYNIC)
|
---|
| 982 | SSMR3GetU64(pSSM, &pHvCpu->auSintMsrs[GIM_HV_VMBUS_MSG_SINT]);
|
---|
| 983 | else
|
---|
| 984 | {
|
---|
| 985 | for (uint8_t idxSintMsr = 0; idxSintMsr < RT_ELEMENTS(pHvCpu->auSintMsrs); idxSintMsr++)
|
---|
| 986 | SSMR3GetU64(pSSM, &pHvCpu->auSintMsrs[idxSintMsr]);
|
---|
| 987 | }
|
---|
[58331] | 988 | }
|
---|
[61008] | 989 |
|
---|
| 990 | uint8_t bDelim;
|
---|
| 991 | rc = SSMR3GetU8(pSSM, &bDelim);
|
---|
[58331] | 992 | }
|
---|
[61008] | 993 | else
|
---|
| 994 | rc = VINF_SUCCESS;
|
---|
[58331] | 995 |
|
---|
[61008] | 996 | return rc;
|
---|
[51643] | 997 | }
|
---|
| 998 |
|
---|
| 999 |
|
---|
| 1000 | /**
|
---|
[72460] | 1001 | * Hyper-V load-done callback.
|
---|
| 1002 | *
|
---|
| 1003 | * @returns VBox status code.
|
---|
| 1004 | * @param pVM The cross context VM structure.
|
---|
| 1005 | * @param pSSM The saved state handle.
|
---|
| 1006 | */
|
---|
| 1007 | VMMR3_INT_DECL(int) gimR3HvLoadDone(PVM pVM, PSSMHANDLE pSSM)
|
---|
| 1008 | {
|
---|
| 1009 | if (RT_SUCCESS(SSMR3HandleGetStatus(pSSM)))
|
---|
| 1010 | {
|
---|
| 1011 | /*
|
---|
| 1012 | * Update EM on whether MSR_GIM_HV_GUEST_OS_ID allows hypercall instructions.
|
---|
| 1013 | */
|
---|
| 1014 | if (pVM->gim.s.u.Hv.u64GuestOsIdMsr)
|
---|
[80191] | 1015 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 1016 | EMSetHypercallInstructionsEnabled(pVM->apCpusR3[idCpu], true);
|
---|
[72460] | 1017 | else
|
---|
[80191] | 1018 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 1019 | EMSetHypercallInstructionsEnabled(pVM->apCpusR3[idCpu], false);
|
---|
[72460] | 1020 | }
|
---|
| 1021 | return VINF_SUCCESS;
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 |
|
---|
| 1025 | /**
|
---|
[62354] | 1026 | * Enables the Hyper-V APIC-assist page.
|
---|
| 1027 | *
|
---|
| 1028 | * @returns VBox status code.
|
---|
[62417] | 1029 | * @param pVCpu The cross context virtual CPU structure.
|
---|
[62359] | 1030 | * @param GCPhysApicAssistPage Where to map the APIC-assist page.
|
---|
[62354] | 1031 | */
|
---|
[62417] | 1032 | VMMR3_INT_DECL(int) gimR3HvEnableApicAssistPage(PVMCPU pVCpu, RTGCPHYS GCPhysApicAssistPage)
|
---|
[62354] | 1033 | {
|
---|
[62417] | 1034 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
[62354] | 1035 | PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
|
---|
| 1036 | AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
|
---|
| 1037 |
|
---|
| 1038 | /*
|
---|
| 1039 | * Map the APIC-assist-page at the specified address.
|
---|
| 1040 | */
|
---|
| 1041 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
| 1042 | * @bugref{7532}. Instead of the overlay style mapping, we just
|
---|
[62417] | 1043 | * rewrite guest memory directly. */
|
---|
[62354] | 1044 | size_t const cbApicAssistPage = PAGE_SIZE;
|
---|
| 1045 | void *pvApicAssist = RTMemAllocZ(cbApicAssistPage);
|
---|
| 1046 | if (RT_LIKELY(pvApicAssist))
|
---|
| 1047 | {
|
---|
| 1048 | int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysApicAssistPage, pvApicAssist, cbApicAssistPage);
|
---|
| 1049 | if (RT_SUCCESS(rc))
|
---|
| 1050 | {
|
---|
| 1051 | /** @todo Inform APIC. */
|
---|
[63648] | 1052 | LogRel(("GIM%u: HyperV: Enabled APIC-assist page at %#RGp\n", pVCpu->idCpu, GCPhysApicAssistPage));
|
---|
[62354] | 1053 | }
|
---|
| 1054 | else
|
---|
| 1055 | {
|
---|
[63648] | 1056 | LogRelFunc(("GIM%u: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", pVCpu->idCpu, rc));
|
---|
[62354] | 1057 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | RTMemFree(pvApicAssist);
|
---|
| 1061 | return rc;
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
[63648] | 1064 | LogRelFunc(("GIM%u: HyperV: Failed to alloc %u bytes\n", pVCpu->idCpu, cbApicAssistPage));
|
---|
[62354] | 1065 | return VERR_NO_MEMORY;
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 |
|
---|
| 1069 | /**
|
---|
| 1070 | * Disables the Hyper-V APIC-assist page.
|
---|
| 1071 | *
|
---|
| 1072 | * @returns VBox status code.
|
---|
[62417] | 1073 | * @param pVCpu The cross context virtual CPU structure.
|
---|
[62354] | 1074 | */
|
---|
[62417] | 1075 | VMMR3_INT_DECL(int) gimR3HvDisableApicAssistPage(PVMCPU pVCpu)
|
---|
[62354] | 1076 | {
|
---|
[63648] | 1077 | LogRel(("GIM%u: HyperV: Disabled APIC-assist page\n", pVCpu->idCpu));
|
---|
[62354] | 1078 | /** @todo inform APIC */
|
---|
| 1079 | return VINF_SUCCESS;
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 |
|
---|
| 1083 | /**
|
---|
[63648] | 1084 | * Hyper-V synthetic timer callback.
|
---|
| 1085 | *
|
---|
| 1086 | * @param pVM The cross context VM structure.
|
---|
| 1087 | * @param pTimer Pointer to timer.
|
---|
| 1088 | * @param pvUser Pointer to the synthetic timer.
|
---|
| 1089 | */
|
---|
| 1090 | static DECLCALLBACK(void) gimR3HvTimerCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
|
---|
| 1091 | {
|
---|
| 1092 | PGIMHVSTIMER pHvStimer = (PGIMHVSTIMER)pvUser;
|
---|
| 1093 | Assert(pHvStimer);
|
---|
[63650] | 1094 | Assert(TMTimerIsLockOwner(pTimer)); RT_NOREF(pTimer);
|
---|
[63648] | 1095 | Assert(pHvStimer->idCpu < pVM->cCpus);
|
---|
| 1096 |
|
---|
[80191] | 1097 | PVMCPU pVCpu = pVM->apCpusR3[pHvStimer->idCpu];
|
---|
[63648] | 1098 | PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
|
---|
| 1099 | Assert(pHvStimer->idxStimer < RT_ELEMENTS(pHvCpu->aStatStimerFired));
|
---|
| 1100 |
|
---|
| 1101 | STAM_COUNTER_INC(&pHvCpu->aStatStimerFired[pHvStimer->idxStimer]);
|
---|
| 1102 |
|
---|
| 1103 | uint64_t const uStimerConfig = pHvStimer->uStimerConfigMsr;
|
---|
| 1104 | uint16_t const idxSint = MSR_GIM_HV_STIMER_GET_SINTX(uStimerConfig);
|
---|
| 1105 | if (RT_LIKELY(idxSint < RT_ELEMENTS(pHvCpu->auSintMsrs)))
|
---|
| 1106 | {
|
---|
| 1107 | uint64_t const uSint = pHvCpu->auSintMsrs[idxSint];
|
---|
| 1108 | if (!MSR_GIM_HV_SINT_IS_MASKED(uSint))
|
---|
| 1109 | {
|
---|
| 1110 | uint8_t const uVector = MSR_GIM_HV_SINT_GET_VECTOR(uSint);
|
---|
| 1111 | bool const fAutoEoi = MSR_GIM_HV_SINT_IS_AUTOEOI(uSint);
|
---|
| 1112 | APICHvSendInterrupt(pVCpu, uVector, fAutoEoi, XAPICTRIGGERMODE_EDGE);
|
---|
| 1113 | }
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
| 1116 | /* Re-arm the timer if it's periodic. */
|
---|
| 1117 | if (MSR_GIM_HV_STIMER_IS_PERIODIC(uStimerConfig))
|
---|
| 1118 | gimHvStartStimer(pVCpu, pHvStimer);
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 |
|
---|
| 1122 | /**
|
---|
[62417] | 1123 | * Enables the Hyper-V SIEF page.
|
---|
| 1124 | *
|
---|
| 1125 | * @returns VBox status code.
|
---|
| 1126 | * @param pVCpu The cross context virtual CPU structure.
|
---|
| 1127 | * @param GCPhysSiefPage Where to map the SIEF page.
|
---|
| 1128 | */
|
---|
| 1129 | VMMR3_INT_DECL(int) gimR3HvEnableSiefPage(PVMCPU pVCpu, RTGCPHYS GCPhysSiefPage)
|
---|
| 1130 | {
|
---|
| 1131 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
| 1132 | PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
|
---|
| 1133 | AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
|
---|
| 1134 |
|
---|
| 1135 | /*
|
---|
| 1136 | * Map the SIEF page at the specified address.
|
---|
| 1137 | */
|
---|
| 1138 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
| 1139 | * @bugref{7532}. Instead of the overlay style mapping, we just
|
---|
| 1140 | * rewrite guest memory directly. */
|
---|
| 1141 | size_t const cbSiefPage = PAGE_SIZE;
|
---|
| 1142 | void *pvSiefPage = RTMemAllocZ(cbSiefPage);
|
---|
| 1143 | if (RT_LIKELY(pvSiefPage))
|
---|
| 1144 | {
|
---|
| 1145 | int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysSiefPage, pvSiefPage, cbSiefPage);
|
---|
| 1146 | if (RT_SUCCESS(rc))
|
---|
| 1147 | {
|
---|
| 1148 | /** @todo SIEF setup. */
|
---|
[63648] | 1149 | LogRel(("GIM%u: HyperV: Enabled SIEF page at %#RGp\n", pVCpu->idCpu, GCPhysSiefPage));
|
---|
[62417] | 1150 | }
|
---|
| 1151 | else
|
---|
| 1152 | {
|
---|
[63648] | 1153 | LogRelFunc(("GIM%u: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", pVCpu->idCpu, rc));
|
---|
[62417] | 1154 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | RTMemFree(pvSiefPage);
|
---|
| 1158 | return rc;
|
---|
| 1159 | }
|
---|
| 1160 |
|
---|
[63648] | 1161 | LogRelFunc(("GIM%u: HyperV: Failed to alloc %u bytes\n", pVCpu->idCpu, cbSiefPage));
|
---|
[62417] | 1162 | return VERR_NO_MEMORY;
|
---|
| 1163 | }
|
---|
| 1164 |
|
---|
| 1165 |
|
---|
| 1166 | /**
|
---|
| 1167 | * Disables the Hyper-V SIEF page.
|
---|
| 1168 | *
|
---|
| 1169 | * @returns VBox status code.
|
---|
| 1170 | * @param pVCpu The cross context virtual CPU structure.
|
---|
| 1171 | */
|
---|
| 1172 | VMMR3_INT_DECL(int) gimR3HvDisableSiefPage(PVMCPU pVCpu)
|
---|
| 1173 | {
|
---|
[63648] | 1174 | LogRel(("GIM%u: HyperV: Disabled APIC-assist page\n", pVCpu->idCpu));
|
---|
[62417] | 1175 | /** @todo SIEF teardown. */
|
---|
| 1176 | return VINF_SUCCESS;
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 |
|
---|
| 1180 | /**
|
---|
[51643] | 1181 | * Enables the Hyper-V TSC page.
|
---|
| 1182 | *
|
---|
| 1183 | * @returns VBox status code.
|
---|
[58122] | 1184 | * @param pVM The cross context VM structure.
|
---|
[54654] | 1185 | * @param GCPhysTscPage Where to map the TSC page.
|
---|
| 1186 | * @param fUseThisTscSeq Whether to set the TSC sequence number to the one
|
---|
| 1187 | * specified in @a uTscSeq.
|
---|
| 1188 | * @param uTscSeq The TSC sequence value to use. Ignored if
|
---|
| 1189 | * @a fUseThisTscSeq is false.
|
---|
| 1190 | */
|
---|
| 1191 | VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
|
---|
[51643] | 1192 | {
|
---|
| 1193 | PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
|
---|
| 1194 | PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
| 1195 | AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
|
---|
| 1196 |
|
---|
| 1197 | int rc;
|
---|
| 1198 | if (pRegion->fMapped)
|
---|
| 1199 | {
|
---|
| 1200 | /*
|
---|
| 1201 | * Is it already enabled at the given guest-address?
|
---|
| 1202 | */
|
---|
| 1203 | if (pRegion->GCPhysPage == GCPhysTscPage)
|
---|
| 1204 | return VINF_SUCCESS;
|
---|
| 1205 |
|
---|
| 1206 | /*
|
---|
| 1207 | * If it's mapped at a different address, unmap the previous address.
|
---|
| 1208 | */
|
---|
[54654] | 1209 | rc = gimR3HvDisableTscPage(pVM);
|
---|
[51643] | 1210 | AssertRC(rc);
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | /*
|
---|
| 1214 | * Map the TSC-page at the specified address.
|
---|
| 1215 | */
|
---|
| 1216 | Assert(!pRegion->fMapped);
|
---|
[56672] | 1217 |
|
---|
| 1218 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
| 1219 | * @bugref{7532}. Instead of the overlay style mapping, we just
|
---|
[62417] | 1220 | * rewrite guest memory directly. */
|
---|
[56672] | 1221 | #if 0
|
---|
[61771] | 1222 | rc = gimR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
|
---|
[51643] | 1223 | if (RT_SUCCESS(rc))
|
---|
| 1224 | {
|
---|
| 1225 | Assert(pRegion->GCPhysPage == GCPhysTscPage);
|
---|
| 1226 |
|
---|
| 1227 | /*
|
---|
| 1228 | * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
|
---|
| 1229 | * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
|
---|
| 1230 | *
|
---|
| 1231 | * Also, Hyper-V normalizes the time in 10 MHz, see:
|
---|
| 1232 | * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
|
---|
| 1233 | */
|
---|
| 1234 | PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
|
---|
| 1235 | Assert(pRefTsc);
|
---|
| 1236 |
|
---|
[56694] | 1237 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1238 | uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
|
---|
[51686] | 1239 | uint32_t u32TscSeq = 1;
|
---|
[54654] | 1240 | if ( fUseThisTscSeq
|
---|
| 1241 | && uTscSeq < UINT32_C(0xfffffffe))
|
---|
| 1242 | u32TscSeq = uTscSeq + 1;
|
---|
[51686] | 1243 | pRefTsc->u32TscSequence = u32TscSeq;
|
---|
[51659] | 1244 | pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
|
---|
[54065] | 1245 | pRefTsc->i64TscOffset = 0;
|
---|
[51643] | 1246 |
|
---|
[54065] | 1247 | LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
|
---|
| 1248 | GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
|
---|
| 1249 |
|
---|
| 1250 | TMR3CpuTickParavirtEnable(pVM);
|
---|
[51643] | 1251 | return VINF_SUCCESS;
|
---|
| 1252 | }
|
---|
| 1253 | else
|
---|
[61771] | 1254 | LogRelFunc(("gimR3Mmio2Map failed. rc=%Rrc\n", rc));
|
---|
[56672] | 1255 | return VERR_GIM_OPERATION_FAILED;
|
---|
| 1256 | #else
|
---|
| 1257 | AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_2);
|
---|
| 1258 | PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)RTMemAllocZ(PAGE_SIZE);
|
---|
| 1259 | if (RT_UNLIKELY(!pRefTsc))
|
---|
| 1260 | {
|
---|
| 1261 | LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
|
---|
| 1262 | return VERR_NO_MEMORY;
|
---|
| 1263 | }
|
---|
[51643] | 1264 |
|
---|
[56694] | 1265 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1266 | uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
|
---|
[56672] | 1267 | uint32_t u32TscSeq = 1;
|
---|
| 1268 | if ( fUseThisTscSeq
|
---|
| 1269 | && uTscSeq < UINT32_C(0xfffffffe))
|
---|
| 1270 | u32TscSeq = uTscSeq + 1;
|
---|
| 1271 | pRefTsc->u32TscSequence = u32TscSeq;
|
---|
| 1272 | pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
|
---|
| 1273 | pRefTsc->i64TscOffset = 0;
|
---|
| 1274 |
|
---|
| 1275 | rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysTscPage, pRefTsc, sizeof(*pRefTsc));
|
---|
| 1276 | if (RT_SUCCESS(rc))
|
---|
| 1277 | {
|
---|
| 1278 | LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
|
---|
| 1279 | GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
|
---|
| 1280 |
|
---|
| 1281 | pRegion->GCPhysPage = GCPhysTscPage;
|
---|
| 1282 | pRegion->fMapped = true;
|
---|
| 1283 | TMR3CpuTickParavirtEnable(pVM);
|
---|
| 1284 | }
|
---|
| 1285 | else
|
---|
| 1286 | {
|
---|
| 1287 | LogRelFunc(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
|
---|
| 1288 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
| 1289 | }
|
---|
| 1290 | RTMemFree(pRefTsc);
|
---|
| 1291 | return rc;
|
---|
| 1292 | #endif
|
---|
[51643] | 1293 | }
|
---|
| 1294 |
|
---|
| 1295 |
|
---|
| 1296 | /**
|
---|
[62417] | 1297 | * Enables the Hyper-V SIM page.
|
---|
| 1298 | *
|
---|
| 1299 | * @returns VBox status code.
|
---|
| 1300 | * @param pVCpu The cross context virtual CPU structure.
|
---|
| 1301 | * @param GCPhysSimPage Where to map the SIM page.
|
---|
| 1302 | */
|
---|
| 1303 | VMMR3_INT_DECL(int) gimR3HvEnableSimPage(PVMCPU pVCpu, RTGCPHYS GCPhysSimPage)
|
---|
| 1304 | {
|
---|
| 1305 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
| 1306 | PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
|
---|
| 1307 | AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
|
---|
| 1308 |
|
---|
| 1309 | /*
|
---|
| 1310 | * Map the SIMP page at the specified address.
|
---|
| 1311 | */
|
---|
| 1312 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
| 1313 | * @bugref{7532}. Instead of the overlay style mapping, we just
|
---|
| 1314 | * rewrite guest memory directly. */
|
---|
| 1315 | size_t const cbSimPage = PAGE_SIZE;
|
---|
| 1316 | void *pvSimPage = RTMemAllocZ(cbSimPage);
|
---|
| 1317 | if (RT_LIKELY(pvSimPage))
|
---|
| 1318 | {
|
---|
| 1319 | int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysSimPage, pvSimPage, cbSimPage);
|
---|
| 1320 | if (RT_SUCCESS(rc))
|
---|
| 1321 | {
|
---|
| 1322 | /** @todo SIM setup. */
|
---|
[63648] | 1323 | LogRel(("GIM%u: HyperV: Enabled SIM page at %#RGp\n", pVCpu->idCpu, GCPhysSimPage));
|
---|
[62417] | 1324 | }
|
---|
| 1325 | else
|
---|
| 1326 | {
|
---|
[63648] | 1327 | LogRelFunc(("GIM%u: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", pVCpu->idCpu, rc));
|
---|
[62417] | 1328 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 | RTMemFree(pvSimPage);
|
---|
| 1332 | return rc;
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
[63648] | 1335 | LogRelFunc(("GIM%u: HyperV: Failed to alloc %u bytes\n", pVCpu->idCpu, cbSimPage));
|
---|
[62417] | 1336 | return VERR_NO_MEMORY;
|
---|
| 1337 | }
|
---|
| 1338 |
|
---|
| 1339 |
|
---|
| 1340 | /**
|
---|
| 1341 | * Disables the Hyper-V SIM page.
|
---|
| 1342 | *
|
---|
| 1343 | * @returns VBox status code.
|
---|
| 1344 | * @param pVCpu The cross context virtual CPU structure.
|
---|
| 1345 | */
|
---|
| 1346 | VMMR3_INT_DECL(int) gimR3HvDisableSimPage(PVMCPU pVCpu)
|
---|
| 1347 | {
|
---|
[63648] | 1348 | LogRel(("GIM%u: HyperV: Disabled SIM page\n", pVCpu->idCpu));
|
---|
[62417] | 1349 | /** @todo SIM teardown. */
|
---|
| 1350 | return VINF_SUCCESS;
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
| 1353 |
|
---|
| 1354 |
|
---|
| 1355 | /**
|
---|
[51643] | 1356 | * Disables the Hyper-V TSC page.
|
---|
| 1357 | *
|
---|
| 1358 | * @returns VBox status code.
|
---|
[58122] | 1359 | * @param pVM The cross context VM structure.
|
---|
[51643] | 1360 | */
|
---|
[54654] | 1361 | VMMR3_INT_DECL(int) gimR3HvDisableTscPage(PVM pVM)
|
---|
[51643] | 1362 | {
|
---|
| 1363 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1364 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
| 1365 | if (pRegion->fMapped)
|
---|
| 1366 | {
|
---|
[56672] | 1367 | #if 0
|
---|
[61771] | 1368 | gimR3Mmio2Unmap(pVM, pRegion);
|
---|
[51643] | 1369 | Assert(!pRegion->fMapped);
|
---|
[56672] | 1370 | #else
|
---|
| 1371 | pRegion->fMapped = false;
|
---|
| 1372 | #endif
|
---|
[62354] | 1373 | LogRel(("GIM: HyperV: Disabled TSC page\n"));
|
---|
[54065] | 1374 |
|
---|
| 1375 | TMR3CpuTickParavirtDisable(pVM);
|
---|
[51643] | 1376 | return VINF_SUCCESS;
|
---|
| 1377 | }
|
---|
| 1378 | return VERR_GIM_PVTSC_NOT_ENABLED;
|
---|
| 1379 | }
|
---|
| 1380 |
|
---|
| 1381 |
|
---|
| 1382 | /**
|
---|
| 1383 | * Disables the Hyper-V Hypercall page.
|
---|
| 1384 | *
|
---|
| 1385 | * @returns VBox status code.
|
---|
| 1386 | */
|
---|
[54654] | 1387 | VMMR3_INT_DECL(int) gimR3HvDisableHypercallPage(PVM pVM)
|
---|
[51643] | 1388 | {
|
---|
| 1389 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1390 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
| 1391 | if (pRegion->fMapped)
|
---|
| 1392 | {
|
---|
[56672] | 1393 | #if 0
|
---|
[61771] | 1394 | gimR3Mmio2Unmap(pVM, pRegion);
|
---|
[51643] | 1395 | Assert(!pRegion->fMapped);
|
---|
[56672] | 1396 | #else
|
---|
| 1397 | pRegion->fMapped = false;
|
---|
| 1398 | #endif
|
---|
[51643] | 1399 | LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
|
---|
| 1400 | return VINF_SUCCESS;
|
---|
| 1401 | }
|
---|
| 1402 | return VERR_GIM_HYPERCALLS_NOT_ENABLED;
|
---|
| 1403 | }
|
---|
| 1404 |
|
---|
| 1405 |
|
---|
| 1406 | /**
|
---|
| 1407 | * Enables the Hyper-V Hypercall page.
|
---|
| 1408 | *
|
---|
| 1409 | * @returns VBox status code.
|
---|
[58122] | 1410 | * @param pVM The cross context VM structure.
|
---|
[51643] | 1411 | * @param GCPhysHypercallPage Where to map the hypercall page.
|
---|
| 1412 | */
|
---|
[54654] | 1413 | VMMR3_INT_DECL(int) gimR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
|
---|
[51643] | 1414 | {
|
---|
| 1415 | PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
|
---|
| 1416 | PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
| 1417 | AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
|
---|
| 1418 |
|
---|
| 1419 | if (pRegion->fMapped)
|
---|
| 1420 | {
|
---|
| 1421 | /*
|
---|
| 1422 | * Is it already enabled at the given guest-address?
|
---|
| 1423 | */
|
---|
| 1424 | if (pRegion->GCPhysPage == GCPhysHypercallPage)
|
---|
| 1425 | return VINF_SUCCESS;
|
---|
| 1426 |
|
---|
| 1427 | /*
|
---|
| 1428 | * If it's mapped at a different address, unmap the previous address.
|
---|
| 1429 | */
|
---|
[54654] | 1430 | int rc2 = gimR3HvDisableHypercallPage(pVM);
|
---|
[51643] | 1431 | AssertRC(rc2);
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 | /*
|
---|
| 1435 | * Map the hypercall-page at the specified address.
|
---|
| 1436 | */
|
---|
| 1437 | Assert(!pRegion->fMapped);
|
---|
[56672] | 1438 |
|
---|
| 1439 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
| 1440 | * @bugref{7532}. Instead of the overlay style mapping, we just
|
---|
[62417] | 1441 | * rewrite guest memory directly. */
|
---|
[56672] | 1442 | #if 0
|
---|
[61771] | 1443 | int rc = gimR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
|
---|
[51643] | 1444 | if (RT_SUCCESS(rc))
|
---|
| 1445 | {
|
---|
| 1446 | Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
|
---|
| 1447 |
|
---|
| 1448 | /*
|
---|
| 1449 | * Patch the hypercall-page.
|
---|
| 1450 | */
|
---|
[55118] | 1451 | size_t cbWritten = 0;
|
---|
| 1452 | rc = VMMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
|
---|
| 1453 | if ( RT_SUCCESS(rc)
|
---|
| 1454 | && cbWritten < PAGE_SIZE)
|
---|
[51643] | 1455 | {
|
---|
[55118] | 1456 | uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
|
---|
| 1457 | *pbLast = 0xc3; /* RET */
|
---|
[51643] | 1458 |
|
---|
[55118] | 1459 | /*
|
---|
[55129] | 1460 | * Notify VMM that hypercalls are now enabled for all VCPUs.
|
---|
[55118] | 1461 | */
|
---|
[80191] | 1462 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 1463 | VMMHypercallsEnable(pVM->apCpusR3[idCpu]);
|
---|
[55118] | 1464 |
|
---|
[58014] | 1465 | LogRel(("GIM: HyperV: Enabled hypercall page at %#RGp\n", GCPhysHypercallPage));
|
---|
[55118] | 1466 | return VINF_SUCCESS;
|
---|
[51643] | 1467 | }
|
---|
| 1468 | else
|
---|
| 1469 | {
|
---|
[55118] | 1470 | if (rc == VINF_SUCCESS)
|
---|
| 1471 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
| 1472 | LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
|
---|
[51643] | 1473 | }
|
---|
[55118] | 1474 |
|
---|
[61771] | 1475 | gimR3Mmio2Unmap(pVM, pRegion);
|
---|
[51643] | 1476 | }
|
---|
| 1477 |
|
---|
[61771] | 1478 | LogRel(("GIM: HyperV: gimR3Mmio2Map failed. rc=%Rrc\n", rc));
|
---|
[51643] | 1479 | return rc;
|
---|
[56672] | 1480 | #else
|
---|
| 1481 | AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_3);
|
---|
| 1482 | void *pvHypercallPage = RTMemAllocZ(PAGE_SIZE);
|
---|
| 1483 | if (RT_UNLIKELY(!pvHypercallPage))
|
---|
| 1484 | {
|
---|
| 1485 | LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
|
---|
| 1486 | return VERR_NO_MEMORY;
|
---|
| 1487 | }
|
---|
| 1488 |
|
---|
| 1489 | /*
|
---|
| 1490 | * Patch the hypercall-page.
|
---|
| 1491 | */
|
---|
[72469] | 1492 | size_t cbHypercall = 0;
|
---|
| 1493 | int rc = GIMQueryHypercallOpcodeBytes(pVM, pvHypercallPage, PAGE_SIZE, &cbHypercall, NULL /*puDisOpcode*/);
|
---|
[56672] | 1494 | if ( RT_SUCCESS(rc)
|
---|
[72469] | 1495 | && cbHypercall < PAGE_SIZE)
|
---|
[56672] | 1496 | {
|
---|
[72469] | 1497 | uint8_t *pbLast = (uint8_t *)pvHypercallPage + cbHypercall;
|
---|
[56672] | 1498 | *pbLast = 0xc3; /* RET */
|
---|
| 1499 |
|
---|
| 1500 | rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysHypercallPage, pvHypercallPage, PAGE_SIZE);
|
---|
| 1501 | if (RT_SUCCESS(rc))
|
---|
| 1502 | {
|
---|
| 1503 | pRegion->GCPhysPage = GCPhysHypercallPage;
|
---|
| 1504 | pRegion->fMapped = true;
|
---|
[58014] | 1505 | LogRel(("GIM: HyperV: Enabled hypercall page at %#RGp\n", GCPhysHypercallPage));
|
---|
[56672] | 1506 | }
|
---|
| 1507 | else
|
---|
| 1508 | LogRel(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed during hypercall page setup. rc=%Rrc\n", rc));
|
---|
| 1509 | }
|
---|
| 1510 | else
|
---|
| 1511 | {
|
---|
| 1512 | if (rc == VINF_SUCCESS)
|
---|
| 1513 | rc = VERR_GIM_OPERATION_FAILED;
|
---|
[72469] | 1514 | LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbHypercall=%u\n", rc, cbHypercall));
|
---|
[56672] | 1515 | }
|
---|
| 1516 |
|
---|
| 1517 | RTMemFree(pvHypercallPage);
|
---|
| 1518 | return rc;
|
---|
| 1519 | #endif
|
---|
[51643] | 1520 | }
|
---|
| 1521 |
|
---|
[57989] | 1522 |
|
---|
| 1523 | /**
|
---|
[58248] | 1524 | * Initializes Hyper-V guest hypercall support.
|
---|
[57989] | 1525 | *
|
---|
| 1526 | * @returns VBox status code.
|
---|
[58122] | 1527 | * @param pVM The cross context VM structure.
|
---|
[57989] | 1528 | */
|
---|
[58248] | 1529 | static int gimR3HvInitHypercallSupport(PVM pVM)
|
---|
[57989] | 1530 | {
|
---|
| 1531 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1532 | pHv->pbHypercallIn = (uint8_t *)RTMemAllocZ(GIM_HV_PAGE_SIZE);
|
---|
| 1533 | if (RT_LIKELY(pHv->pbHypercallIn))
|
---|
| 1534 | {
|
---|
| 1535 | pHv->pbHypercallOut = (uint8_t *)RTMemAllocZ(GIM_HV_PAGE_SIZE);
|
---|
| 1536 | if (RT_LIKELY(pHv->pbHypercallOut))
|
---|
| 1537 | return VINF_SUCCESS;
|
---|
| 1538 | RTMemFree(pHv->pbHypercallIn);
|
---|
| 1539 | }
|
---|
| 1540 | return VERR_NO_MEMORY;
|
---|
| 1541 | }
|
---|
| 1542 |
|
---|
| 1543 |
|
---|
| 1544 | /**
|
---|
[58248] | 1545 | * Terminates Hyper-V guest hypercall support.
|
---|
[57989] | 1546 | *
|
---|
[58122] | 1547 | * @param pVM The cross context VM structure.
|
---|
[57989] | 1548 | */
|
---|
[58248] | 1549 | static void gimR3HvTermHypercallSupport(PVM pVM)
|
---|
[57989] | 1550 | {
|
---|
| 1551 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1552 | RTMemFree(pHv->pbHypercallIn);
|
---|
| 1553 | pHv->pbHypercallIn = NULL;
|
---|
| 1554 |
|
---|
| 1555 | RTMemFree(pHv->pbHypercallOut);
|
---|
| 1556 | pHv->pbHypercallOut = NULL;
|
---|
| 1557 | }
|
---|
| 1558 |
|
---|
| 1559 |
|
---|
| 1560 | /**
|
---|
[58390] | 1561 | * Initializes Hyper-V guest debug support.
|
---|
| 1562 | *
|
---|
| 1563 | * @returns VBox status code.
|
---|
| 1564 | * @param pVM The cross context VM structure.
|
---|
| 1565 | */
|
---|
| 1566 | static int gimR3HvInitDebugSupport(PVM pVM)
|
---|
| 1567 | {
|
---|
| 1568 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1569 | if ( (pHv->uPartFlags & GIM_HV_PART_FLAGS_DEBUGGING)
|
---|
| 1570 | || pHv->fIsInterfaceVs)
|
---|
| 1571 | {
|
---|
| 1572 | pHv->fDbgEnabled = true;
|
---|
| 1573 | pHv->pvDbgBuffer = RTMemAllocZ(PAGE_SIZE);
|
---|
| 1574 | if (!pHv->pvDbgBuffer)
|
---|
| 1575 | return VERR_NO_MEMORY;
|
---|
| 1576 | }
|
---|
| 1577 | return VINF_SUCCESS;
|
---|
| 1578 | }
|
---|
| 1579 |
|
---|
| 1580 |
|
---|
| 1581 | /**
|
---|
| 1582 | * Terminates Hyper-V guest debug support.
|
---|
| 1583 | *
|
---|
| 1584 | * @param pVM The cross context VM structure.
|
---|
| 1585 | */
|
---|
| 1586 | static void gimR3HvTermDebugSupport(PVM pVM)
|
---|
| 1587 | {
|
---|
| 1588 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1589 | if (pHv->pvDbgBuffer)
|
---|
| 1590 | {
|
---|
| 1591 | RTMemFree(pHv->pvDbgBuffer);
|
---|
| 1592 | pHv->pvDbgBuffer = NULL;
|
---|
| 1593 | }
|
---|
| 1594 | }
|
---|
| 1595 |
|
---|
| 1596 |
|
---|
| 1597 | /**
|
---|
[57989] | 1598 | * Reads data from a debugger connection, asynchronous.
|
---|
| 1599 | *
|
---|
| 1600 | * @returns VBox status code.
|
---|
[58122] | 1601 | * @param pVM The cross context VM structure.
|
---|
[57989] | 1602 | * @param pvBuf Where to read the data.
|
---|
[58248] | 1603 | * @param cbBuf Size of the read buffer @a pvBuf, must be >= @a cbRead.
|
---|
| 1604 | * @param cbRead Number of bytes to read.
|
---|
[57989] | 1605 | * @param pcbRead Where to store how many bytes were really read.
|
---|
| 1606 | * @param cMsTimeout Timeout of the read operation in milliseconds.
|
---|
[58248] | 1607 | * @param fUdpPkt Whether the debug data returned in @a pvBuf needs to be
|
---|
| 1608 | * encapsulated in a UDP frame.
|
---|
[57989] | 1609 | *
|
---|
| 1610 | * @thread EMT.
|
---|
| 1611 | */
|
---|
[58248] | 1612 | VMMR3_INT_DECL(int) gimR3HvDebugRead(PVM pVM, void *pvBuf, uint32_t cbBuf, uint32_t cbRead, uint32_t *pcbRead,
|
---|
| 1613 | uint32_t cMsTimeout, bool fUdpPkt)
|
---|
[57989] | 1614 | {
|
---|
[58248] | 1615 | NOREF(cMsTimeout); /** @todo implement timeout. */
|
---|
[57989] | 1616 | AssertCompile(sizeof(size_t) >= sizeof(uint32_t));
|
---|
[58248] | 1617 | AssertReturn(cbBuf >= cbRead, VERR_INVALID_PARAMETER);
|
---|
| 1618 |
|
---|
[58313] | 1619 | int rc;
|
---|
| 1620 | if (!fUdpPkt)
|
---|
[58248] | 1621 | {
|
---|
[58313] | 1622 | /*
|
---|
| 1623 | * Read the raw debug data.
|
---|
| 1624 | */
|
---|
| 1625 | size_t cbReallyRead = cbRead;
|
---|
[61771] | 1626 | rc = gimR3DebugRead(pVM, pvBuf, &cbReallyRead, gimR3HvDebugBufReadCompleted);
|
---|
[58313] | 1627 | *pcbRead = (uint32_t)cbReallyRead;
|
---|
| 1628 | }
|
---|
| 1629 | else
|
---|
| 1630 | {
|
---|
| 1631 | /*
|
---|
| 1632 | * Guest requires UDP encapsulated frames.
|
---|
| 1633 | */
|
---|
| 1634 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1635 | rc = VERR_GIM_IPE_1;
|
---|
[58390] | 1636 | switch (pHv->enmDbgReply)
|
---|
[58248] | 1637 | {
|
---|
[58313] | 1638 | case GIMHVDEBUGREPLY_UDP:
|
---|
| 1639 | {
|
---|
| 1640 | size_t cbReallyRead = cbRead;
|
---|
[61771] | 1641 | rc = gimR3DebugRead(pVM, pvBuf, &cbReallyRead, gimR3HvDebugBufReadCompleted);
|
---|
[58313] | 1642 | if ( RT_SUCCESS(rc)
|
---|
| 1643 | && cbReallyRead > 0)
|
---|
| 1644 | {
|
---|
| 1645 | uint8_t abFrame[sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + sizeof(RTNETUDP)];
|
---|
| 1646 | if (cbReallyRead + sizeof(abFrame) <= cbBuf)
|
---|
| 1647 | {
|
---|
| 1648 | /*
|
---|
| 1649 | * Windows guests pumps ethernet frames over the Hyper-V debug connection as
|
---|
| 1650 | * explained in gimR3HvHypercallPostDebugData(). Here, we reconstruct the packet
|
---|
| 1651 | * with the guest's self-chosen IP ARP address we saved in pHv->DbgGuestAddr.
|
---|
| 1652 | *
|
---|
| 1653 | * Note! We really need to pass the minimum IPv4 header length. The Windows 10 guest
|
---|
| 1654 | * is -not- happy if we include the IPv4 options field, i.e. using sizeof(RTNETIPV4)
|
---|
| 1655 | * instead of RTNETIPV4_MIN_LEN.
|
---|
| 1656 | */
|
---|
| 1657 | RT_ZERO(abFrame);
|
---|
| 1658 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)&abFrame[0];
|
---|
| 1659 | PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
|
---|
| 1660 | PRTNETUDP pUdpHdr = (PRTNETUDP) ((uint8_t *)pIpHdr + RTNETIPV4_MIN_LEN);
|
---|
[58248] | 1661 |
|
---|
[58313] | 1662 | /* Ethernet */
|
---|
| 1663 | pEthHdr->EtherType = RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4);
|
---|
| 1664 | /* IPv4 */
|
---|
| 1665 | pIpHdr->ip_v = 4;
|
---|
| 1666 | pIpHdr->ip_hl = RTNETIPV4_MIN_LEN / sizeof(uint32_t);
|
---|
| 1667 | pIpHdr->ip_tos = 0;
|
---|
| 1668 | pIpHdr->ip_len = RT_H2N_U16((uint16_t)cbReallyRead + sizeof(RTNETUDP) + RTNETIPV4_MIN_LEN);
|
---|
| 1669 | pIpHdr->ip_id = 0;
|
---|
| 1670 | pIpHdr->ip_off = 0;
|
---|
| 1671 | pIpHdr->ip_ttl = 255;
|
---|
| 1672 | pIpHdr->ip_p = RTNETIPV4_PROT_UDP;
|
---|
| 1673 | pIpHdr->ip_sum = 0;
|
---|
| 1674 | pIpHdr->ip_src.u = 0;
|
---|
[58331] | 1675 | pIpHdr->ip_dst.u = pHv->DbgGuestIp4Addr.u;
|
---|
[58313] | 1676 | pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
|
---|
| 1677 | /* UDP */
|
---|
[58390] | 1678 | pUdpHdr->uh_dport = pHv->uUdpGuestSrcPort;
|
---|
| 1679 | pUdpHdr->uh_sport = pHv->uUdpGuestDstPort;
|
---|
[58313] | 1680 | pUdpHdr->uh_ulen = RT_H2N_U16_C((uint16_t)cbReallyRead + sizeof(*pUdpHdr));
|
---|
[58248] | 1681 |
|
---|
[58313] | 1682 | /* Make room by moving the payload and prepending the headers. */
|
---|
| 1683 | uint8_t *pbData = (uint8_t *)pvBuf;
|
---|
| 1684 | memmove(pbData + sizeof(abFrame), pbData, cbReallyRead);
|
---|
| 1685 | memcpy(pbData, &abFrame[0], sizeof(abFrame));
|
---|
[58248] | 1686 |
|
---|
[58313] | 1687 | /* Update the adjusted sizes. */
|
---|
| 1688 | cbReallyRead += sizeof(abFrame);
|
---|
| 1689 | }
|
---|
| 1690 | else
|
---|
| 1691 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
| 1692 | }
|
---|
| 1693 | *pcbRead = (uint32_t)cbReallyRead;
|
---|
| 1694 | break;
|
---|
| 1695 | }
|
---|
| 1696 |
|
---|
| 1697 | case GIMHVDEBUGREPLY_ARP_REPLY:
|
---|
| 1698 | {
|
---|
| 1699 | uint32_t const cbArpReplyPkt = sizeof(g_abArpReply);
|
---|
| 1700 | if (cbBuf >= cbArpReplyPkt)
|
---|
| 1701 | {
|
---|
| 1702 | memcpy(pvBuf, g_abArpReply, cbArpReplyPkt);
|
---|
| 1703 | rc = VINF_SUCCESS;
|
---|
| 1704 | *pcbRead = cbArpReplyPkt;
|
---|
[58390] | 1705 | pHv->enmDbgReply = GIMHVDEBUGREPLY_ARP_REPLY_SENT;
|
---|
[58313] | 1706 | }
|
---|
| 1707 | else
|
---|
| 1708 | {
|
---|
| 1709 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
| 1710 | *pcbRead = 0;
|
---|
| 1711 | }
|
---|
| 1712 | break;
|
---|
| 1713 | }
|
---|
| 1714 |
|
---|
| 1715 | case GIMHVDEBUGREPLY_DHCP_OFFER:
|
---|
| 1716 | {
|
---|
| 1717 | uint32_t const cbDhcpOfferPkt = sizeof(g_abDhcpOffer);
|
---|
| 1718 | if (cbBuf >= cbDhcpOfferPkt)
|
---|
| 1719 | {
|
---|
| 1720 | memcpy(pvBuf, g_abDhcpOffer, cbDhcpOfferPkt);
|
---|
| 1721 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)pvBuf;
|
---|
| 1722 | PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
|
---|
| 1723 | PRTNETUDP pUdpHdr = (PRTNETUDP) ((uint8_t *)pIpHdr + RTNETIPV4_MIN_LEN);
|
---|
| 1724 | PRTNETBOOTP pBootpHdr = (PRTNETBOOTP) (pUdpHdr + 1);
|
---|
[58390] | 1725 | pBootpHdr->bp_xid = pHv->uDbgBootpXId;
|
---|
[58313] | 1726 |
|
---|
| 1727 | rc = VINF_SUCCESS;
|
---|
| 1728 | *pcbRead = cbDhcpOfferPkt;
|
---|
[58390] | 1729 | pHv->enmDbgReply = GIMHVDEBUGREPLY_DHCP_OFFER_SENT;
|
---|
[58313] | 1730 | LogRel(("GIM: HyperV: Debug DHCP offered IP address %RTnaipv4, transaction Id %#x\n", pBootpHdr->bp_yiaddr,
|
---|
[58390] | 1731 | RT_N2H_U32(pHv->uDbgBootpXId)));
|
---|
[58313] | 1732 | }
|
---|
| 1733 | else
|
---|
| 1734 | {
|
---|
| 1735 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
| 1736 | *pcbRead = 0;
|
---|
| 1737 | }
|
---|
| 1738 | break;
|
---|
| 1739 | }
|
---|
| 1740 |
|
---|
| 1741 | case GIMHVDEBUGREPLY_DHCP_ACK:
|
---|
| 1742 | {
|
---|
| 1743 | uint32_t const cbDhcpAckPkt = sizeof(g_abDhcpAck);
|
---|
| 1744 | if (cbBuf >= cbDhcpAckPkt)
|
---|
| 1745 | {
|
---|
| 1746 | memcpy(pvBuf, g_abDhcpAck, cbDhcpAckPkt);
|
---|
[58315] | 1747 | PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)pvBuf;
|
---|
| 1748 | PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
|
---|
| 1749 | PRTNETUDP pUdpHdr = (PRTNETUDP) ((uint8_t *)pIpHdr + RTNETIPV4_MIN_LEN);
|
---|
[58316] | 1750 | PRTNETBOOTP pBootpHdr = (PRTNETBOOTP) (pUdpHdr + 1);
|
---|
[58390] | 1751 | pBootpHdr->bp_xid = pHv->uDbgBootpXId;
|
---|
[58313] | 1752 |
|
---|
| 1753 | rc = VINF_SUCCESS;
|
---|
| 1754 | *pcbRead = cbDhcpAckPkt;
|
---|
[58390] | 1755 | pHv->enmDbgReply = GIMHVDEBUGREPLY_DHCP_ACK_SENT;
|
---|
[58313] | 1756 | LogRel(("GIM: HyperV: Debug DHCP acknowledged IP address %RTnaipv4, transaction Id %#x\n",
|
---|
[58390] | 1757 | pBootpHdr->bp_yiaddr, RT_N2H_U32(pHv->uDbgBootpXId)));
|
---|
[58313] | 1758 | }
|
---|
| 1759 | else
|
---|
| 1760 | {
|
---|
| 1761 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
| 1762 | *pcbRead = 0;
|
---|
| 1763 | }
|
---|
| 1764 | break;
|
---|
| 1765 | }
|
---|
| 1766 |
|
---|
| 1767 | case GIMHVDEBUGREPLY_ARP_REPLY_SENT:
|
---|
| 1768 | case GIMHVDEBUGREPLY_DHCP_OFFER_SENT:
|
---|
| 1769 | case GIMHVDEBUGREPLY_DHCP_ACK_SENT:
|
---|
| 1770 | {
|
---|
| 1771 | rc = VINF_SUCCESS;
|
---|
| 1772 | *pcbRead = 0;
|
---|
| 1773 | break;
|
---|
| 1774 | }
|
---|
| 1775 |
|
---|
| 1776 | default:
|
---|
| 1777 | {
|
---|
[58390] | 1778 | AssertMsgFailed(("GIM: HyperV: Invalid/unimplemented debug reply type %u\n", pHv->enmDbgReply));
|
---|
[58313] | 1779 | rc = VERR_INTERNAL_ERROR_2;
|
---|
| 1780 | }
|
---|
[58248] | 1781 | }
|
---|
[58313] | 1782 | Assert(rc != VERR_GIM_IPE_1);
|
---|
[58390] | 1783 |
|
---|
| 1784 | #ifdef DEBUG_ramshankar
|
---|
| 1785 | if ( rc == VINF_SUCCESS
|
---|
| 1786 | && *pcbRead > 0)
|
---|
| 1787 | {
|
---|
| 1788 | RTSOCKET hSocket;
|
---|
| 1789 | int rc2 = RTUdpCreateClientSocket("localhost", 52000, NULL, &hSocket);
|
---|
| 1790 | if (RT_SUCCESS(rc2))
|
---|
| 1791 | {
|
---|
| 1792 | size_t cbTmpWrite = *pcbRead;
|
---|
| 1793 | RTSocketWriteNB(hSocket, pvBuf, *pcbRead, &cbTmpWrite); NOREF(cbTmpWrite);
|
---|
| 1794 | RTSocketClose(hSocket);
|
---|
| 1795 | }
|
---|
| 1796 | }
|
---|
| 1797 | #endif
|
---|
[58248] | 1798 | }
|
---|
[58390] | 1799 |
|
---|
[57989] | 1800 | return rc;
|
---|
| 1801 | }
|
---|
| 1802 |
|
---|
| 1803 |
|
---|
| 1804 | /**
|
---|
| 1805 | * Writes data to the debugger connection, asynchronous.
|
---|
| 1806 | *
|
---|
| 1807 | * @returns VBox status code.
|
---|
[58122] | 1808 | * @param pVM The cross context VM structure.
|
---|
[58248] | 1809 | * @param pvData Pointer to the data to be written.
|
---|
| 1810 | * @param cbWrite Size of the write buffer @a pvData.
|
---|
| 1811 | * @param pcbWritten Where to store the number of bytes written.
|
---|
| 1812 | * @param fUdpPkt Whether the debug data in @a pvData is encapsulated in a
|
---|
| 1813 | * UDP frame.
|
---|
[57989] | 1814 | *
|
---|
| 1815 | * @thread EMT.
|
---|
| 1816 | */
|
---|
[58248] | 1817 | VMMR3_INT_DECL(int) gimR3HvDebugWrite(PVM pVM, void *pvData, uint32_t cbWrite, uint32_t *pcbWritten, bool fUdpPkt)
|
---|
[57989] | 1818 | {
|
---|
[58248] | 1819 | Assert(cbWrite > 0);
|
---|
| 1820 |
|
---|
| 1821 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 1822 | bool fIgnorePkt = false;
|
---|
| 1823 | uint8_t *pbData = (uint8_t *)pvData;
|
---|
| 1824 | if (fUdpPkt)
|
---|
| 1825 | {
|
---|
[58390] | 1826 | #ifdef DEBUG_ramshankar
|
---|
| 1827 | RTSOCKET hSocket;
|
---|
| 1828 | int rc2 = RTUdpCreateClientSocket("localhost", 52000, NULL, &hSocket);
|
---|
| 1829 | if (RT_SUCCESS(rc2))
|
---|
| 1830 | {
|
---|
| 1831 | size_t cbTmpWrite = cbWrite;
|
---|
| 1832 | RTSocketWriteNB(hSocket, pbData, cbWrite, &cbTmpWrite); NOREF(cbTmpWrite);
|
---|
| 1833 | RTSocketClose(hSocket);
|
---|
| 1834 | }
|
---|
| 1835 | #endif
|
---|
[58248] | 1836 | /*
|
---|
| 1837 | * Windows guests sends us ethernet frames over the Hyper-V debug connection.
|
---|
| 1838 | * It sends DHCP/ARP queries with zero'd out MAC addresses and requires fudging up the
|
---|
| 1839 | * packets somewhere.
|
---|
| 1840 | *
|
---|
| 1841 | * The Microsoft WinDbg debugger talks UDP and thus only expects the actual debug
|
---|
| 1842 | * protocol payload.
|
---|
| 1843 | *
|
---|
[58313] | 1844 | * If the guest is configured with the "nodhcp" option it sends ARP queries with
|
---|
| 1845 | * a self-chosen IP and after a couple of attempts of receiving no replies, the guest
|
---|
| 1846 | * picks its own IP address. After this, the guest starts sending the UDP packets
|
---|
| 1847 | * we require. We thus ignore the initial ARP packets until the guest eventually
|
---|
| 1848 | * starts talking UDP. Then we can finally feed the UDP payload over the debug
|
---|
| 1849 | * connection.
|
---|
[58337] | 1850 | *
|
---|
| 1851 | * When 'kdvm.dll' is the debug transport in the guest (Windows 7), it doesn't bother
|
---|
| 1852 | * with this DHCP/ARP phase. It starts sending debug data in a UDP frame right away.
|
---|
[58248] | 1853 | */
|
---|
| 1854 | if (cbWrite > sizeof(RTNETETHERHDR))
|
---|
| 1855 | {
|
---|
| 1856 | PCRTNETETHERHDR pEtherHdr = (PCRTNETETHERHDR)pbData;
|
---|
| 1857 | if (pEtherHdr->EtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4))
|
---|
| 1858 | {
|
---|
| 1859 | if (cbWrite > sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN)
|
---|
| 1860 | {
|
---|
| 1861 | size_t const cbMaxIpHdr = cbWrite - sizeof(RTNETETHERHDR) - sizeof(RTNETUDP) - 1;
|
---|
| 1862 | size_t const cbMaxIpPkt = cbWrite - sizeof(RTNETETHERHDR);
|
---|
| 1863 | PCRTNETIPV4 pIp4Hdr = (PCRTNETIPV4)(pbData + sizeof(RTNETETHERHDR));
|
---|
| 1864 | bool const fValidIp4 = RTNetIPv4IsHdrValid(pIp4Hdr, cbMaxIpHdr, cbMaxIpPkt, false /*fChecksum*/);
|
---|
| 1865 | if ( fValidIp4
|
---|
| 1866 | && pIp4Hdr->ip_p == RTNETIPV4_PROT_UDP)
|
---|
| 1867 | {
|
---|
| 1868 | uint32_t const cbIpHdr = pIp4Hdr->ip_hl * 4;
|
---|
| 1869 | uint32_t const cbMaxUdpPkt = cbWrite - sizeof(RTNETETHERHDR) - cbIpHdr;
|
---|
| 1870 | PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t *)pIp4Hdr + cbIpHdr);
|
---|
| 1871 | if ( pUdpHdr->uh_ulen > RT_H2N_U16(sizeof(RTNETUDP))
|
---|
| 1872 | && pUdpHdr->uh_ulen <= RT_H2N_U16((uint16_t)cbMaxUdpPkt))
|
---|
| 1873 | {
|
---|
| 1874 | /*
|
---|
[58313] | 1875 | * Check for DHCP.
|
---|
[58248] | 1876 | */
|
---|
[58390] | 1877 | bool fBuggyPkt = false;
|
---|
[58313] | 1878 | size_t const cbUdpPkt = cbMaxIpPkt - cbIpHdr;
|
---|
| 1879 | if ( pUdpHdr->uh_dport == RT_N2H_U16_C(RTNETIPV4_PORT_BOOTPS)
|
---|
[58390] | 1880 | && pUdpHdr->uh_sport == RT_N2H_U16_C(RTNETIPV4_PORT_BOOTPC))
|
---|
[58248] | 1881 | {
|
---|
[58313] | 1882 | PCRTNETBOOTP pDhcpPkt = (PCRTNETBOOTP)(pUdpHdr + 1);
|
---|
| 1883 | uint8_t bMsgType;
|
---|
[58390] | 1884 | if ( cbMaxIpPkt >= cbIpHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
|
---|
| 1885 | && RTNetIPv4IsDHCPValid(pUdpHdr, pDhcpPkt, cbUdpPkt - sizeof(*pUdpHdr), &bMsgType))
|
---|
[58313] | 1886 | {
|
---|
| 1887 | switch (bMsgType)
|
---|
| 1888 | {
|
---|
| 1889 | case RTNET_DHCP_MT_DISCOVER:
|
---|
[58390] | 1890 | pHv->enmDbgReply = GIMHVDEBUGREPLY_DHCP_OFFER;
|
---|
| 1891 | pHv->uDbgBootpXId = pDhcpPkt->bp_xid;
|
---|
[58313] | 1892 | break;
|
---|
| 1893 | case RTNET_DHCP_MT_REQUEST:
|
---|
[58390] | 1894 | pHv->enmDbgReply = GIMHVDEBUGREPLY_DHCP_ACK;
|
---|
| 1895 | pHv->uDbgBootpXId = pDhcpPkt->bp_xid;
|
---|
[58313] | 1896 | break;
|
---|
| 1897 | default:
|
---|
| 1898 | LogRelMax(5, ("GIM: HyperV: Debug DHCP MsgType %#x not implemented! Packet dropped\n",
|
---|
| 1899 | bMsgType));
|
---|
| 1900 | break;
|
---|
| 1901 | }
|
---|
[58390] | 1902 | fIgnorePkt = true;
|
---|
[58313] | 1903 | }
|
---|
[58390] | 1904 | else if ( pIp4Hdr->ip_src.u == GIMHV_DEBUGCLIENT_IPV4
|
---|
| 1905 | && pIp4Hdr->ip_dst.u == 0)
|
---|
| 1906 | {
|
---|
| 1907 | /*
|
---|
| 1908 | * Windows 8.1 seems to be sending malformed BOOTP packets at the final stage of the
|
---|
| 1909 | * debugger sequence. It appears that a previously sent DHCP request buffer wasn't cleared
|
---|
| 1910 | * in the guest and they re-use it instead of sending a zero destination+source port packet
|
---|
| 1911 | * as expected below.
|
---|
| 1912 | *
|
---|
| 1913 | * We workaround Microsoft's bug here, or at least, I'm classifying it as a bug to
|
---|
| 1914 | * preserve my own sanity, see @bugref{8006#c54}.
|
---|
| 1915 | */
|
---|
| 1916 | fBuggyPkt = true;
|
---|
| 1917 | }
|
---|
[58313] | 1918 | }
|
---|
[58390] | 1919 |
|
---|
| 1920 | if ( ( !pUdpHdr->uh_dport
|
---|
| 1921 | && !pUdpHdr->uh_sport)
|
---|
| 1922 | || fBuggyPkt)
|
---|
[58313] | 1923 | {
|
---|
| 1924 | /*
|
---|
| 1925 | * Extract the UDP payload and pass it to the debugger and record the guest IP address.
|
---|
[58390] | 1926 | *
|
---|
| 1927 | * Hyper-V sends UDP debugger packets with source and destination port as 0 except in the
|
---|
[61559] | 1928 | * aforementioned buggy case. The buggy packet case requires us to remember the ports and
|
---|
[58390] | 1929 | * reply to them, otherwise the guest won't receive the replies we sent with port 0.
|
---|
[58313] | 1930 | */
|
---|
[58248] | 1931 | uint32_t const cbFrameHdr = sizeof(RTNETETHERHDR) + cbIpHdr + sizeof(RTNETUDP);
|
---|
| 1932 | pbData += cbFrameHdr;
|
---|
| 1933 | cbWrite -= cbFrameHdr;
|
---|
[58390] | 1934 | pHv->DbgGuestIp4Addr.u = pIp4Hdr->ip_src.u;
|
---|
| 1935 | pHv->uUdpGuestDstPort = pUdpHdr->uh_dport;
|
---|
| 1936 | pHv->uUdpGuestSrcPort = pUdpHdr->uh_sport;
|
---|
| 1937 | pHv->enmDbgReply = GIMHVDEBUGREPLY_UDP;
|
---|
[58248] | 1938 | }
|
---|
| 1939 | else
|
---|
| 1940 | {
|
---|
[58390] | 1941 | LogFlow(("GIM: HyperV: Ignoring UDP packet SourcePort=%u DstPort=%u\n", pUdpHdr->uh_sport,
|
---|
| 1942 | pUdpHdr->uh_dport));
|
---|
[58248] | 1943 | fIgnorePkt = true;
|
---|
| 1944 | }
|
---|
| 1945 | }
|
---|
| 1946 | else
|
---|
| 1947 | {
|
---|
| 1948 | LogFlow(("GIM: HyperV: Ignoring malformed UDP packet. cbMaxUdpPkt=%u UdpPkt.len=%u\n", cbMaxUdpPkt,
|
---|
| 1949 | RT_N2H_U16(pUdpHdr->uh_ulen)));
|
---|
| 1950 | fIgnorePkt = true;
|
---|
| 1951 | }
|
---|
| 1952 | }
|
---|
| 1953 | else
|
---|
| 1954 | {
|
---|
| 1955 | LogFlow(("GIM: HyperV: Ignoring non-IP / non-UDP packet. fValidIp4=%RTbool Proto=%u\n", fValidIp4,
|
---|
| 1956 | pIp4Hdr->ip_p));
|
---|
| 1957 | fIgnorePkt = true;
|
---|
| 1958 | }
|
---|
| 1959 | }
|
---|
| 1960 | else
|
---|
| 1961 | {
|
---|
| 1962 | LogFlow(("GIM: HyperV: Ignoring IPv4 packet; too short to be valid UDP. cbWrite=%u\n", cbWrite));
|
---|
| 1963 | fIgnorePkt = true;
|
---|
| 1964 | }
|
---|
| 1965 | }
|
---|
[58313] | 1966 | else if (pEtherHdr->EtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_ARP))
|
---|
| 1967 | {
|
---|
| 1968 | /*
|
---|
| 1969 | * Check for targetted ARP query.
|
---|
| 1970 | */
|
---|
| 1971 | PCRTNETARPHDR pArpHdr = (PCRTNETARPHDR)(pbData + sizeof(RTNETETHERHDR));
|
---|
| 1972 | if ( pArpHdr->ar_hlen == sizeof(RTMAC)
|
---|
| 1973 | && pArpHdr->ar_plen == sizeof(RTNETADDRIPV4)
|
---|
| 1974 | && pArpHdr->ar_htype == RT_H2N_U16(RTNET_ARP_ETHER)
|
---|
| 1975 | && pArpHdr->ar_ptype == RT_H2N_U16(RTNET_ETHERTYPE_IPV4))
|
---|
| 1976 | {
|
---|
| 1977 | uint16_t uArpOp = pArpHdr->ar_oper;
|
---|
| 1978 | if (uArpOp == RT_H2N_U16_C(RTNET_ARPOP_REQUEST))
|
---|
| 1979 | {
|
---|
| 1980 | PCRTNETARPIPV4 pArpPkt = (PCRTNETARPIPV4)pArpHdr;
|
---|
| 1981 | bool fGratuitous = pArpPkt->ar_spa.u == pArpPkt->ar_tpa.u;
|
---|
| 1982 | if ( !fGratuitous
|
---|
| 1983 | && pArpPkt->ar_spa.u == GIMHV_DEBUGCLIENT_IPV4
|
---|
| 1984 | && pArpPkt->ar_tpa.u == GIMHV_DEBUGSERVER_IPV4)
|
---|
| 1985 | {
|
---|
[58390] | 1986 | pHv->enmDbgReply = GIMHVDEBUGREPLY_ARP_REPLY;
|
---|
[58313] | 1987 | }
|
---|
| 1988 | }
|
---|
| 1989 | }
|
---|
| 1990 | fIgnorePkt = true;
|
---|
| 1991 | }
|
---|
[58248] | 1992 | else
|
---|
| 1993 | {
|
---|
| 1994 | LogFlow(("GIM: HyperV: Ignoring non-IP packet. Ethertype=%#x\n", RT_N2H_U16(pEtherHdr->EtherType)));
|
---|
| 1995 | fIgnorePkt = true;
|
---|
| 1996 | }
|
---|
| 1997 | }
|
---|
| 1998 | }
|
---|
| 1999 |
|
---|
| 2000 | if (!fIgnorePkt)
|
---|
| 2001 | {
|
---|
| 2002 | AssertCompile(sizeof(size_t) >= sizeof(uint32_t));
|
---|
| 2003 | size_t cbWriteBuf = cbWrite;
|
---|
[61771] | 2004 | int rc = gimR3DebugWrite(pVM, pbData, &cbWriteBuf);
|
---|
[58248] | 2005 | if ( RT_SUCCESS(rc)
|
---|
| 2006 | && cbWriteBuf == cbWrite)
|
---|
| 2007 | *pcbWritten = (uint32_t)cbWriteBuf;
|
---|
| 2008 | else
|
---|
| 2009 | *pcbWritten = 0;
|
---|
| 2010 | }
|
---|
| 2011 | else
|
---|
| 2012 | *pcbWritten = cbWrite;
|
---|
| 2013 |
|
---|
| 2014 | return VINF_SUCCESS;
|
---|
[57989] | 2015 | }
|
---|
| 2016 |
|
---|
| 2017 |
|
---|
| 2018 | /**
|
---|
| 2019 | * Performs the HvPostDebugData hypercall.
|
---|
| 2020 | *
|
---|
| 2021 | * @returns VBox status code.
|
---|
[58122] | 2022 | * @param pVM The cross context VM structure.
|
---|
[57989] | 2023 | * @param prcHv Where to store the result of the hypercall operation.
|
---|
| 2024 | *
|
---|
| 2025 | * @thread EMT.
|
---|
| 2026 | */
|
---|
[58283] | 2027 | VMMR3_INT_DECL(int) gimR3HvHypercallPostDebugData(PVM pVM, int *prcHv)
|
---|
[57989] | 2028 | {
|
---|
| 2029 | AssertPtr(pVM);
|
---|
| 2030 | AssertPtr(prcHv);
|
---|
| 2031 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 2032 | int rcHv = GIM_HV_STATUS_OPERATION_DENIED;
|
---|
| 2033 |
|
---|
| 2034 | /*
|
---|
| 2035 | * Grab the parameters.
|
---|
| 2036 | */
|
---|
| 2037 | PGIMHVDEBUGPOSTIN pIn = (PGIMHVDEBUGPOSTIN)pHv->pbHypercallIn;
|
---|
| 2038 | AssertPtrReturn(pIn, VERR_GIM_IPE_1);
|
---|
| 2039 | uint32_t cbWrite = pIn->cbWrite;
|
---|
| 2040 | uint32_t fFlags = pIn->fFlags;
|
---|
| 2041 | uint8_t *pbData = ((uint8_t *)pIn) + sizeof(PGIMHVDEBUGPOSTIN);
|
---|
| 2042 |
|
---|
| 2043 | PGIMHVDEBUGPOSTOUT pOut = (PGIMHVDEBUGPOSTOUT)pHv->pbHypercallOut;
|
---|
| 2044 |
|
---|
| 2045 | /*
|
---|
| 2046 | * Perform the hypercall.
|
---|
| 2047 | */
|
---|
| 2048 | #if 0
|
---|
| 2049 | /* Currently disabled as Windows 10 guest passes us undocumented flags. */
|
---|
| 2050 | if (fFlags & ~GIM_HV_DEBUG_POST_OPTIONS_MASK))
|
---|
| 2051 | rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
|
---|
[62641] | 2052 | #else
|
---|
| 2053 | RT_NOREF1(fFlags);
|
---|
[57989] | 2054 | #endif
|
---|
| 2055 | if (cbWrite > GIM_HV_DEBUG_MAX_DATA_SIZE)
|
---|
| 2056 | rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
|
---|
| 2057 | else if (!cbWrite)
|
---|
[58248] | 2058 | {
|
---|
[57989] | 2059 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
[58248] | 2060 | pOut->cbPending = 0;
|
---|
| 2061 | }
|
---|
[57989] | 2062 | else if (cbWrite > 0)
|
---|
| 2063 | {
|
---|
[58248] | 2064 | uint32_t cbWritten = 0;
|
---|
| 2065 | int rc2 = gimR3HvDebugWrite(pVM, pbData, cbWrite, &cbWritten, pHv->fIsVendorMsHv /*fUdpPkt*/);
|
---|
| 2066 | if ( RT_SUCCESS(rc2)
|
---|
| 2067 | && cbWritten == cbWrite)
|
---|
[57989] | 2068 | {
|
---|
[58248] | 2069 | pOut->cbPending = 0;
|
---|
| 2070 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
[57989] | 2071 | }
|
---|
| 2072 | else
|
---|
[58248] | 2073 | rcHv = GIM_HV_STATUS_INSUFFICIENT_BUFFER;
|
---|
[57989] | 2074 | }
|
---|
| 2075 |
|
---|
| 2076 | /*
|
---|
| 2077 | * Update the guest memory with result.
|
---|
| 2078 | */
|
---|
[58283] | 2079 | int rc = PGMPhysSimpleWriteGCPhys(pVM, pHv->GCPhysHypercallOut, pHv->pbHypercallOut, sizeof(GIMHVDEBUGPOSTOUT));
|
---|
[57989] | 2080 | if (RT_FAILURE(rc))
|
---|
| 2081 | {
|
---|
| 2082 | LogRelMax(10, ("GIM: HyperV: HvPostDebugData failed to update guest memory. rc=%Rrc\n", rc));
|
---|
| 2083 | rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
|
---|
| 2084 | }
|
---|
[61544] | 2085 | else
|
---|
| 2086 | Assert(rc == VINF_SUCCESS);
|
---|
[57989] | 2087 |
|
---|
| 2088 | *prcHv = rcHv;
|
---|
| 2089 | return rc;
|
---|
| 2090 | }
|
---|
| 2091 |
|
---|
| 2092 |
|
---|
| 2093 | /**
|
---|
| 2094 | * Performs the HvRetrieveDebugData hypercall.
|
---|
| 2095 | *
|
---|
| 2096 | * @returns VBox status code.
|
---|
[58122] | 2097 | * @param pVM The cross context VM structure.
|
---|
[57989] | 2098 | * @param prcHv Where to store the result of the hypercall operation.
|
---|
| 2099 | *
|
---|
| 2100 | * @thread EMT.
|
---|
| 2101 | */
|
---|
[58283] | 2102 | VMMR3_INT_DECL(int) gimR3HvHypercallRetrieveDebugData(PVM pVM, int *prcHv)
|
---|
[57989] | 2103 | {
|
---|
| 2104 | AssertPtr(pVM);
|
---|
| 2105 | AssertPtr(prcHv);
|
---|
| 2106 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 2107 | int rcHv = GIM_HV_STATUS_OPERATION_DENIED;
|
---|
| 2108 |
|
---|
| 2109 | /*
|
---|
| 2110 | * Grab the parameters.
|
---|
| 2111 | */
|
---|
| 2112 | PGIMHVDEBUGRETRIEVEIN pIn = (PGIMHVDEBUGRETRIEVEIN)pHv->pbHypercallIn;
|
---|
| 2113 | AssertPtrReturn(pIn, VERR_GIM_IPE_1);
|
---|
| 2114 | uint32_t cbRead = pIn->cbRead;
|
---|
| 2115 | uint32_t fFlags = pIn->fFlags;
|
---|
| 2116 | uint64_t uTimeout = pIn->u64Timeout;
|
---|
| 2117 | uint32_t cMsTimeout = (fFlags & GIM_HV_DEBUG_RETREIVE_LOOP) ? (uTimeout * 100) / RT_NS_1MS_64 : 0;
|
---|
| 2118 |
|
---|
| 2119 | PGIMHVDEBUGRETRIEVEOUT pOut = (PGIMHVDEBUGRETRIEVEOUT)pHv->pbHypercallOut;
|
---|
| 2120 | AssertPtrReturn(pOut, VERR_GIM_IPE_2);
|
---|
| 2121 | uint32_t *pcbReallyRead = &pOut->cbRead;
|
---|
| 2122 | uint32_t *pcbRemainingRead = &pOut->cbRemaining;
|
---|
| 2123 | void *pvData = ((uint8_t *)pOut) + sizeof(GIMHVDEBUGRETRIEVEOUT);
|
---|
| 2124 |
|
---|
| 2125 | /*
|
---|
| 2126 | * Perform the hypercall.
|
---|
| 2127 | */
|
---|
| 2128 | *pcbReallyRead = 0;
|
---|
| 2129 | *pcbRemainingRead = cbRead;
|
---|
| 2130 | #if 0
|
---|
| 2131 | /* Currently disabled as Windows 10 guest passes us undocumented flags. */
|
---|
| 2132 | if (fFlags & ~GIM_HV_DEBUG_RETREIVE_OPTIONS_MASK)
|
---|
| 2133 | rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
|
---|
| 2134 | #endif
|
---|
| 2135 | if (cbRead > GIM_HV_DEBUG_MAX_DATA_SIZE)
|
---|
| 2136 | rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
|
---|
| 2137 | else if (fFlags & GIM_HV_DEBUG_RETREIVE_TEST_ACTIVITY)
|
---|
| 2138 | rcHv = GIM_HV_STATUS_SUCCESS; /** @todo implement this. */
|
---|
| 2139 | else if (!cbRead)
|
---|
| 2140 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
| 2141 | else if (cbRead > 0)
|
---|
| 2142 | {
|
---|
[58248] | 2143 | int rc2 = gimR3HvDebugRead(pVM, pvData, GIM_HV_PAGE_SIZE, cbRead, pcbReallyRead, cMsTimeout,
|
---|
| 2144 | pHv->fIsVendorMsHv /*fUdpPkt*/);
|
---|
[57989] | 2145 | Assert(*pcbReallyRead <= cbRead);
|
---|
| 2146 | if ( RT_SUCCESS(rc2)
|
---|
| 2147 | && *pcbReallyRead > 0)
|
---|
| 2148 | {
|
---|
[58248] | 2149 | *pcbRemainingRead = cbRead - *pcbReallyRead;
|
---|
[57989] | 2150 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
| 2151 | }
|
---|
| 2152 | else
|
---|
| 2153 | rcHv = GIM_HV_STATUS_NO_DATA;
|
---|
| 2154 | }
|
---|
| 2155 |
|
---|
| 2156 | /*
|
---|
| 2157 | * Update the guest memory with result.
|
---|
| 2158 | */
|
---|
[58283] | 2159 | int rc = PGMPhysSimpleWriteGCPhys(pVM, pHv->GCPhysHypercallOut, pHv->pbHypercallOut,
|
---|
| 2160 | sizeof(GIMHVDEBUGRETRIEVEOUT) + *pcbReallyRead);
|
---|
[57989] | 2161 | if (RT_FAILURE(rc))
|
---|
| 2162 | {
|
---|
| 2163 | LogRelMax(10, ("GIM: HyperV: HvRetrieveDebugData failed to update guest memory. rc=%Rrc\n", rc));
|
---|
| 2164 | rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
|
---|
| 2165 | }
|
---|
[61544] | 2166 | else
|
---|
| 2167 | Assert(rc == VINF_SUCCESS);
|
---|
[57989] | 2168 |
|
---|
| 2169 | *prcHv = rcHv;
|
---|
| 2170 | return rc;
|
---|
| 2171 | }
|
---|
| 2172 |
|
---|
[72190] | 2173 |
|
---|
| 2174 | /**
|
---|
| 2175 | * Performs the HvExtCallQueryCapabilities extended hypercall.
|
---|
| 2176 | *
|
---|
| 2177 | * @returns VBox status code.
|
---|
| 2178 | * @param pVM The cross context VM structure.
|
---|
| 2179 | * @param prcHv Where to store the result of the hypercall operation.
|
---|
| 2180 | *
|
---|
| 2181 | * @thread EMT.
|
---|
| 2182 | */
|
---|
| 2183 | VMMR3_INT_DECL(int) gimR3HvHypercallExtQueryCap(PVM pVM, int *prcHv)
|
---|
| 2184 | {
|
---|
| 2185 | AssertPtr(pVM);
|
---|
| 2186 | AssertPtr(prcHv);
|
---|
| 2187 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 2188 |
|
---|
| 2189 | /*
|
---|
| 2190 | * Grab the parameters.
|
---|
| 2191 | */
|
---|
| 2192 | PGIMHVEXTQUERYCAP pOut = (PGIMHVEXTQUERYCAP)pHv->pbHypercallOut;
|
---|
| 2193 |
|
---|
| 2194 | /*
|
---|
| 2195 | * Perform the hypercall.
|
---|
| 2196 | */
|
---|
| 2197 | pOut->fCapabilities = GIM_HV_EXT_HYPERCALL_CAP_ZERO_MEM;
|
---|
| 2198 |
|
---|
| 2199 | /*
|
---|
| 2200 | * Update the guest memory with result.
|
---|
| 2201 | */
|
---|
| 2202 | int rcHv;
|
---|
| 2203 | int rc = PGMPhysSimpleWriteGCPhys(pVM, pHv->GCPhysHypercallOut, pHv->pbHypercallOut, sizeof(GIMHVEXTQUERYCAP));
|
---|
| 2204 | if (RT_SUCCESS(rc))
|
---|
| 2205 | {
|
---|
| 2206 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
| 2207 | LogRel(("GIM: HyperV: Queried extended hypercall capabilities %#RX64 at %#RGp\n", pOut->fCapabilities,
|
---|
| 2208 | pHv->GCPhysHypercallOut));
|
---|
| 2209 | }
|
---|
| 2210 | else
|
---|
| 2211 | {
|
---|
| 2212 | rcHv = GIM_HV_STATUS_OPERATION_DENIED;
|
---|
| 2213 | LogRelMax(10, ("GIM: HyperV: HvHypercallExtQueryCap failed to update guest memory. rc=%Rrc\n", rc));
|
---|
| 2214 | rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
|
---|
| 2215 | }
|
---|
| 2216 |
|
---|
| 2217 | *prcHv = rcHv;
|
---|
| 2218 | return rc;
|
---|
| 2219 | }
|
---|
| 2220 |
|
---|
| 2221 |
|
---|
| 2222 | /**
|
---|
| 2223 | * Performs the HvExtCallGetBootZeroedMemory extended hypercall.
|
---|
| 2224 | *
|
---|
| 2225 | * @returns VBox status code.
|
---|
| 2226 | * @param pVM The cross context VM structure.
|
---|
| 2227 | * @param prcHv Where to store the result of the hypercall operation.
|
---|
| 2228 | *
|
---|
| 2229 | * @thread EMT.
|
---|
| 2230 | */
|
---|
| 2231 | VMMR3_INT_DECL(int) gimR3HvHypercallExtGetBootZeroedMem(PVM pVM, int *prcHv)
|
---|
| 2232 | {
|
---|
| 2233 | AssertPtr(pVM);
|
---|
| 2234 | AssertPtr(prcHv);
|
---|
| 2235 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
| 2236 |
|
---|
| 2237 | /*
|
---|
| 2238 | * Grab the parameters.
|
---|
| 2239 | */
|
---|
| 2240 | PGIMHVEXTGETBOOTZEROMEM pOut = (PGIMHVEXTGETBOOTZEROMEM)pHv->pbHypercallOut;
|
---|
| 2241 |
|
---|
| 2242 | /*
|
---|
| 2243 | * Perform the hypercall.
|
---|
| 2244 | */
|
---|
| 2245 | uint32_t const cRanges = PGMR3PhysGetRamRangeCount(pVM);
|
---|
| 2246 | pOut->cPages = 0;
|
---|
| 2247 | for (uint32_t iRange = 0; iRange < cRanges; iRange++)
|
---|
| 2248 | {
|
---|
| 2249 | RTGCPHYS GCPhysStart;
|
---|
| 2250 | RTGCPHYS GCPhysEnd;
|
---|
| 2251 | int rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, NULL /* fIsMmio */);
|
---|
| 2252 | if (RT_FAILURE(rc))
|
---|
| 2253 | {
|
---|
| 2254 | LogRelMax(10, ("GIM: HyperV: HvHypercallExtGetBootZeroedMem: PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n",
|
---|
| 2255 | iRange, rc));
|
---|
| 2256 | *prcHv = GIM_HV_STATUS_OPERATION_DENIED;
|
---|
| 2257 | return rc;
|
---|
| 2258 | }
|
---|
| 2259 |
|
---|
| 2260 | RTGCPHYS const cbRange = RT_ALIGN(GCPhysEnd - GCPhysStart + 1, PAGE_SIZE);
|
---|
| 2261 | pOut->cPages += cbRange >> GIM_HV_PAGE_SHIFT;
|
---|
| 2262 | if (iRange == 0)
|
---|
| 2263 | pOut->GCPhysStart = GCPhysStart;
|
---|
| 2264 | }
|
---|
| 2265 |
|
---|
| 2266 | /*
|
---|
| 2267 | * Update the guest memory with result.
|
---|
| 2268 | */
|
---|
| 2269 | int rcHv;
|
---|
| 2270 | int rc = PGMPhysSimpleWriteGCPhys(pVM, pHv->GCPhysHypercallOut, pHv->pbHypercallOut, sizeof(GIMHVEXTGETBOOTZEROMEM));
|
---|
| 2271 | if (RT_SUCCESS(rc))
|
---|
| 2272 | {
|
---|
| 2273 | LogRel(("GIM: HyperV: Queried boot zeroed guest memory range (starting at %#RGp spanning %u pages) at %#RGp\n",
|
---|
| 2274 | pOut->GCPhysStart, pOut->cPages, pHv->GCPhysHypercallOut));
|
---|
| 2275 | rcHv = GIM_HV_STATUS_SUCCESS;
|
---|
| 2276 | }
|
---|
| 2277 | else
|
---|
| 2278 | {
|
---|
| 2279 | rcHv = GIM_HV_STATUS_OPERATION_DENIED;
|
---|
| 2280 | LogRelMax(10, ("GIM: HyperV: HvHypercallExtGetBootZeroedMem failed to update guest memory. rc=%Rrc\n", rc));
|
---|
| 2281 | rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
|
---|
| 2282 | }
|
---|
| 2283 |
|
---|
| 2284 | *prcHv = rcHv;
|
---|
| 2285 | return rc;
|
---|
| 2286 | }
|
---|
| 2287 |
|
---|