[23] | 1 | /* $Id: IOMAll.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * IOM - Input / Output Monitor - Any Context.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[96407] | 7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[81383] | 32 | #define LOG_GROUP LOG_GROUP_IOM_IOPORT
|
---|
[35346] | 33 | #include <VBox/vmm/iom.h>
|
---|
| 34 | #include <VBox/vmm/mm.h>
|
---|
[1] | 35 | #include <VBox/param.h>
|
---|
| 36 | #include "IOMInternal.h"
|
---|
[80268] | 37 | #include <VBox/vmm/vmcc.h>
|
---|
[35346] | 38 | #include <VBox/vmm/vmm.h>
|
---|
| 39 | #include <VBox/vmm/selm.h>
|
---|
| 40 | #include <VBox/vmm/trpm.h>
|
---|
| 41 | #include <VBox/vmm/pdmdev.h>
|
---|
| 42 | #include <VBox/vmm/pgm.h>
|
---|
| 43 | #include <VBox/vmm/cpum.h>
|
---|
[1] | 44 | #include <VBox/err.h>
|
---|
| 45 | #include <VBox/log.h>
|
---|
| 46 | #include <iprt/assert.h>
|
---|
[76382] | 47 | #include <iprt/string.h>
|
---|
[37424] | 48 | #include "IOMInline.h"
|
---|
[1] | 49 |
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Reads an I/O port register.
|
---|
| 53 | *
|
---|
[7724] | 54 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
[3145] | 55 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
[2504] | 56 | * @retval VINF_SUCCESS Success.
|
---|
[7724] | 57 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
[3145] | 58 | * status code must be passed on to EM.
|
---|
[56403] | 59 | * @retval VINF_IOM_R3_IOPORT_READ Defer the read to ring-3. (R0/RC only)
|
---|
[1] | 60 | *
|
---|
[58122] | 61 | * @param pVM The cross context VM structure.
|
---|
[58123] | 62 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
[1] | 63 | * @param Port The port to read.
|
---|
| 64 | * @param pu32Value Where to store the value read.
|
---|
| 65 | * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
|
---|
| 66 | */
|
---|
[80641] | 67 | VMMDECL(VBOXSTRICTRC) IOMIOPortRead(PVMCC pVM, PVMCPU pVCpu, RTIOPORT Port, uint32_t *pu32Value, size_t cbValue)
|
---|
[1] | 68 | {
|
---|
[82378] | 69 | STAM_COUNTER_INC(&pVM->iom.s.StatIoPortIn);
|
---|
[60847] | 70 | Assert(pVCpu->iom.s.PendingIOPortWrite.cbValue == 0);
|
---|
| 71 |
|
---|
[37443] | 72 | /** @todo should initialize *pu32Value here because it can happen that some
|
---|
| 73 | * handle is buggy and doesn't handle all cases. */
|
---|
[80641] | 74 |
|
---|
| 75 | /* For lookups we need to share lock IOM. */
|
---|
[45311] | 76 | int rc2 = IOM_LOCK_SHARED(pVM);
|
---|
[90426] | 77 | if (RT_SUCCESS(rc2))
|
---|
| 78 | { /* likely */ }
|
---|
[19472] | 79 | #ifndef IN_RING3
|
---|
[90426] | 80 | else if (rc2 == VERR_SEM_BUSY)
|
---|
[40280] | 81 | return VINF_IOM_R3_IOPORT_READ;
|
---|
[19472] | 82 | #endif
|
---|
[90426] | 83 | else
|
---|
| 84 | AssertMsgFailedReturn(("rc2=%Rrc\n", rc2), rc2);
|
---|
[19472] | 85 |
|
---|
[80641] | 86 | /*
|
---|
| 87 | * Get the entry for the current context.
|
---|
| 88 | */
|
---|
[80679] | 89 | uint16_t offPort;
|
---|
| 90 | CTX_SUFF(PIOMIOPORTENTRY) pRegEntry = iomIoPortGetEntry(pVM, Port, &offPort, &pVCpu->iom.s.idxIoPortLastRead);
|
---|
[80641] | 91 | if (pRegEntry)
|
---|
| 92 | {
|
---|
[1] | 93 | #ifdef VBOX_WITH_STATISTICS
|
---|
[80679] | 94 | PIOMIOPORTSTATSENTRY pStats = iomIoPortGetStats(pVM, pRegEntry, offPort);
|
---|
[80641] | 95 | #endif
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * Found an entry, get the data so we can leave the IOM lock.
|
---|
| 99 | */
|
---|
[81136] | 100 | uint16_t const fFlags = pRegEntry->fFlags;
|
---|
[80960] | 101 | PFNIOMIOPORTNEWIN pfnInCallback = pRegEntry->pfnInCallback;
|
---|
| 102 | PPDMDEVINS pDevIns = pRegEntry->pDevIns;
|
---|
[80641] | 103 | #ifndef IN_RING3
|
---|
| 104 | if ( pfnInCallback
|
---|
| 105 | && pDevIns
|
---|
| 106 | && pRegEntry->cPorts > 0)
|
---|
| 107 | { /* likely */ }
|
---|
| 108 | else
|
---|
| 109 | {
|
---|
| 110 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
| 111 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 112 | return VINF_IOM_R3_IOPORT_READ;
|
---|
| 113 | }
|
---|
| 114 | #endif
|
---|
| 115 | void *pvUser = pRegEntry->pvUser;
|
---|
| 116 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 117 | AssertPtr(pDevIns);
|
---|
| 118 | AssertPtr(pfnInCallback);
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | * Call the device.
|
---|
| 122 | */
|
---|
[90346] | 123 | VBOXSTRICTRC rcStrict = PDMCritSectEnter(pVM, pDevIns->CTX_SUFF(pCritSectRo), VINF_IOM_R3_IOPORT_READ);
|
---|
[80641] | 124 | if (rcStrict == VINF_SUCCESS)
|
---|
| 125 | {
|
---|
| 126 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
[81136] | 127 | rcStrict = pfnInCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? Port : offPort, pu32Value, (unsigned)cbValue);
|
---|
[80641] | 128 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
[90346] | 129 | PDMCritSectLeave(pVM, pDevIns->CTX_SUFF(pCritSectRo));
|
---|
[80641] | 130 |
|
---|
| 131 | #ifndef IN_RING3
|
---|
[82378] | 132 | if (rcStrict == VINF_IOM_R3_IOPORT_READ)
|
---|
[80641] | 133 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
[82378] | 134 | else
|
---|
[80641] | 135 | #endif
|
---|
| 136 | {
|
---|
[82378] | 137 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(In));
|
---|
| 138 | STAM_COUNTER_INC(&iomIoPortGetStats(pVM, pRegEntry, 0)->Total);
|
---|
| 139 | if (rcStrict == VERR_IOM_IOPORT_UNUSED)
|
---|
[80641] | 140 | {
|
---|
[82378] | 141 | /* make return value */
|
---|
| 142 | rcStrict = VINF_SUCCESS;
|
---|
| 143 | switch (cbValue)
|
---|
| 144 | {
|
---|
| 145 | case 1: *(uint8_t *)pu32Value = 0xff; break;
|
---|
| 146 | case 2: *(uint16_t *)pu32Value = 0xffff; break;
|
---|
| 147 | case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
|
---|
| 148 | default:
|
---|
| 149 | AssertMsgFailedReturn(("Invalid I/O port size %d. Port=%d\n", cbValue, Port), VERR_IOM_INVALID_IOPORT_SIZE);
|
---|
| 150 | }
|
---|
[80641] | 151 | }
|
---|
[82378] | 152 | }
|
---|
[80641] | 153 | Log3(("IOMIOPortRead: Port=%RTiop *pu32=%08RX32 cb=%d rc=%Rrc\n", Port, *pu32Value, cbValue, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
[82378] | 154 | STAM_COUNTER_INC(&iomIoPortGetStats(pVM, pRegEntry, 0)->Total);
|
---|
[80641] | 155 | }
|
---|
| 156 | else
|
---|
| 157 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
| 158 | return rcStrict;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[1] | 161 | /*
|
---|
| 162 | * Ok, no handler for this port.
|
---|
| 163 | */
|
---|
[82311] | 164 | IOM_UNLOCK_SHARED(pVM);
|
---|
[1] | 165 | switch (cbValue)
|
---|
| 166 | {
|
---|
| 167 | case 1: *(uint8_t *)pu32Value = 0xff; break;
|
---|
| 168 | case 2: *(uint16_t *)pu32Value = 0xffff; break;
|
---|
[12772] | 169 | case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
|
---|
[1] | 170 | default:
|
---|
[82311] | 171 | AssertMsgFailedReturn(("Invalid I/O port size %d. Port=%d\n", cbValue, Port), VERR_IOM_INVALID_IOPORT_SIZE);
|
---|
[1] | 172 | }
|
---|
| 173 | Log3(("IOMIOPortRead: Port=%RTiop *pu32=%08RX32 cb=%d rc=VINF_SUCCESS\n", Port, *pu32Value, cbValue));
|
---|
| 174 | return VINF_SUCCESS;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Reads the string buffer of an I/O port register.
|
---|
| 180 | *
|
---|
[7724] | 181 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
[3145] | 182 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
[56403] | 183 | * @retval VINF_SUCCESS Success or no string I/O callback in
|
---|
| 184 | * this context.
|
---|
[7724] | 185 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
[3145] | 186 | * status code must be passed on to EM.
|
---|
[56403] | 187 | * @retval VINF_IOM_R3_IOPORT_READ Defer the read to ring-3. (R0/RC only)
|
---|
[1] | 188 | *
|
---|
[58122] | 189 | * @param pVM The cross context VM structure.
|
---|
[58123] | 190 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
[58126] | 191 | * @param uPort The port to read.
|
---|
[56413] | 192 | * @param pvDst Pointer to the destination buffer.
|
---|
[1] | 193 | * @param pcTransfers Pointer to the number of transfer units to read, on return remaining transfer units.
|
---|
| 194 | * @param cb Size of the transfer unit (1, 2 or 4 bytes).
|
---|
[22493] | 195 | */
|
---|
[80641] | 196 | VMM_INT_DECL(VBOXSTRICTRC) IOMIOPortReadString(PVMCC pVM, PVMCPU pVCpu, RTIOPORT uPort,
|
---|
[56415] | 197 | void *pvDst, uint32_t *pcTransfers, unsigned cb)
|
---|
[1] | 198 | {
|
---|
[82378] | 199 | STAM_COUNTER_INC(&pVM->iom.s.StatIoPortInS);
|
---|
[60847] | 200 | Assert(pVCpu->iom.s.PendingIOPortWrite.cbValue == 0);
|
---|
| 201 |
|
---|
[80641] | 202 | /* For lookups we need to share lock IOM. */
|
---|
[45311] | 203 | int rc2 = IOM_LOCK_SHARED(pVM);
|
---|
[90426] | 204 | if (RT_SUCCESS(rc2))
|
---|
| 205 | { /* likely */ }
|
---|
[19472] | 206 | #ifndef IN_RING3
|
---|
[90426] | 207 | else if (rc2 == VERR_SEM_BUSY)
|
---|
[40280] | 208 | return VINF_IOM_R3_IOPORT_READ;
|
---|
[19472] | 209 | #endif
|
---|
[90426] | 210 | else
|
---|
| 211 | AssertMsgFailedReturn(("rc2=%Rrc\n", rc2), rc2);
|
---|
[19472] | 212 |
|
---|
[56415] | 213 | const uint32_t cRequestedTransfers = *pcTransfers;
|
---|
| 214 | Assert(cRequestedTransfers > 0);
|
---|
| 215 |
|
---|
[80641] | 216 | /*
|
---|
| 217 | * Get the entry for the current context.
|
---|
| 218 | */
|
---|
[80679] | 219 | uint16_t offPort;
|
---|
| 220 | CTX_SUFF(PIOMIOPORTENTRY) pRegEntry = iomIoPortGetEntry(pVM, uPort, &offPort, &pVCpu->iom.s.idxIoPortLastReadStr);
|
---|
[80641] | 221 | if (pRegEntry)
|
---|
| 222 | {
|
---|
[1] | 223 | #ifdef VBOX_WITH_STATISTICS
|
---|
[80679] | 224 | PIOMIOPORTSTATSENTRY pStats = iomIoPortGetStats(pVM, pRegEntry, offPort);
|
---|
[80641] | 225 | #endif
|
---|
| 226 |
|
---|
| 227 | /*
|
---|
| 228 | * Found an entry, get the data so we can leave the IOM lock.
|
---|
| 229 | */
|
---|
[81136] | 230 | uint16_t const fFlags = pRegEntry->fFlags;
|
---|
[80960] | 231 | PFNIOMIOPORTNEWINSTRING pfnInStrCallback = pRegEntry->pfnInStrCallback;
|
---|
| 232 | PFNIOMIOPORTNEWIN pfnInCallback = pRegEntry->pfnInCallback;
|
---|
| 233 | PPDMDEVINS pDevIns = pRegEntry->pDevIns;
|
---|
[80641] | 234 | #ifndef IN_RING3
|
---|
| 235 | if ( pfnInCallback
|
---|
| 236 | && pDevIns
|
---|
| 237 | && pRegEntry->cPorts > 0)
|
---|
| 238 | { /* likely */ }
|
---|
| 239 | else
|
---|
| 240 | {
|
---|
| 241 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
| 242 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 243 | return VINF_IOM_R3_IOPORT_READ;
|
---|
| 244 | }
|
---|
| 245 | #endif
|
---|
| 246 | void *pvUser = pRegEntry->pvUser;
|
---|
| 247 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 248 | AssertPtr(pDevIns);
|
---|
| 249 | AssertPtr(pfnInCallback);
|
---|
| 250 |
|
---|
| 251 | /*
|
---|
| 252 | * Call the device.
|
---|
| 253 | */
|
---|
[90346] | 254 | VBOXSTRICTRC rcStrict = PDMCritSectEnter(pVM, pDevIns->CTX_SUFF(pCritSectRo), VINF_IOM_R3_IOPORT_READ);
|
---|
[80641] | 255 | if (rcStrict == VINF_SUCCESS)
|
---|
| 256 | {
|
---|
| 257 | /*
|
---|
| 258 | * First using the string I/O callback.
|
---|
| 259 | */
|
---|
| 260 | if (pfnInStrCallback)
|
---|
| 261 | {
|
---|
| 262 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
[81136] | 263 | rcStrict = pfnInStrCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? uPort : offPort,
|
---|
| 264 | (uint8_t *)pvDst, pcTransfers, cb);
|
---|
[80641] | 265 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /*
|
---|
| 269 | * Then doing the single I/O fallback.
|
---|
| 270 | */
|
---|
| 271 | if ( *pcTransfers > 0
|
---|
| 272 | && rcStrict == VINF_SUCCESS)
|
---|
| 273 | {
|
---|
| 274 | pvDst = (uint8_t *)pvDst + (cRequestedTransfers - *pcTransfers) * cb;
|
---|
| 275 | do
|
---|
| 276 | {
|
---|
| 277 | uint32_t u32Value = 0;
|
---|
| 278 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
[81136] | 279 | rcStrict = pfnInCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? uPort : offPort, &u32Value, cb);
|
---|
[80641] | 280 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfIn), a);
|
---|
| 281 | if (rcStrict == VERR_IOM_IOPORT_UNUSED)
|
---|
| 282 | {
|
---|
| 283 | u32Value = UINT32_MAX;
|
---|
| 284 | rcStrict = VINF_SUCCESS;
|
---|
| 285 | }
|
---|
| 286 | if (IOM_SUCCESS(rcStrict))
|
---|
| 287 | {
|
---|
| 288 | switch (cb)
|
---|
| 289 | {
|
---|
| 290 | case 4: *(uint32_t *)pvDst = u32Value; pvDst = (uint8_t *)pvDst + 4; break;
|
---|
| 291 | case 2: *(uint16_t *)pvDst = (uint16_t)u32Value; pvDst = (uint8_t *)pvDst + 2; break;
|
---|
| 292 | case 1: *(uint8_t *)pvDst = (uint8_t )u32Value; pvDst = (uint8_t *)pvDst + 1; break;
|
---|
| 293 | default: AssertFailed();
|
---|
| 294 | }
|
---|
| 295 | *pcTransfers -= 1;
|
---|
| 296 | }
|
---|
| 297 | } while ( *pcTransfers > 0
|
---|
| 298 | && rcStrict == VINF_SUCCESS);
|
---|
| 299 | }
|
---|
[90346] | 300 | PDMCritSectLeave(pVM, pDevIns->CTX_SUFF(pCritSectRo));
|
---|
[80641] | 301 |
|
---|
| 302 | #ifdef VBOX_WITH_STATISTICS
|
---|
| 303 | # ifndef IN_RING3
|
---|
[82378] | 304 | if (rcStrict == VINF_IOM_R3_IOPORT_READ)
|
---|
[80641] | 305 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
[82378] | 306 | else
|
---|
[80641] | 307 | # endif
|
---|
[82378] | 308 | {
|
---|
| 309 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(In));
|
---|
| 310 | STAM_COUNTER_INC(&iomIoPortGetStats(pVM, pRegEntry, 0)->Total);
|
---|
| 311 | }
|
---|
[80641] | 312 | #endif
|
---|
| 313 | Log3(("IOMIOPortReadStr: uPort=%RTiop pvDst=%p pcTransfer=%p:{%#x->%#x} cb=%d rc=%Rrc\n",
|
---|
| 314 | uPort, pvDst, pcTransfers, cRequestedTransfers, *pcTransfers, cb, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
| 315 | }
|
---|
| 316 | #ifndef IN_RING3
|
---|
| 317 | else
|
---|
| 318 | STAM_COUNTER_INC(&pStats->InRZToR3);
|
---|
| 319 | #endif
|
---|
| 320 | return rcStrict;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[1] | 323 | /*
|
---|
| 324 | * Ok, no handler for this port.
|
---|
| 325 | */
|
---|
[82311] | 326 | IOM_UNLOCK_SHARED(pVM);
|
---|
[58354] | 327 | *pcTransfers = 0;
|
---|
| 328 | memset(pvDst, 0xff, cRequestedTransfers * cb);
|
---|
| 329 | Log3(("IOMIOPortReadStr: uPort=%RTiop (unused) pvDst=%p pcTransfer=%p:{%#x->%#x} cb=%d rc=VINF_SUCCESS\n",
|
---|
[56415] | 330 | uPort, pvDst, pcTransfers, cRequestedTransfers, *pcTransfers, cb));
|
---|
[1] | 331 | return VINF_SUCCESS;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 |
|
---|
[60847] | 335 | #ifndef IN_RING3
|
---|
[1] | 336 | /**
|
---|
[60847] | 337 | * Defers a pending I/O port write to ring-3.
|
---|
| 338 | *
|
---|
| 339 | * @returns VINF_IOM_R3_IOPORT_COMMIT_WRITE
|
---|
| 340 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
| 341 | * @param Port The port to write to.
|
---|
| 342 | * @param u32Value The value to write.
|
---|
| 343 | * @param cbValue The size of the value (1, 2, 4).
|
---|
| 344 | */
|
---|
| 345 | static VBOXSTRICTRC iomIOPortRing3WritePending(PVMCPU pVCpu, RTIOPORT Port, uint32_t u32Value, size_t cbValue)
|
---|
| 346 | {
|
---|
[60854] | 347 | Log5(("iomIOPortRing3WritePending: %#x LB %u -> %RTiop\n", u32Value, cbValue, Port));
|
---|
[60847] | 348 | AssertReturn(pVCpu->iom.s.PendingIOPortWrite.cbValue == 0, VERR_IOM_IOPORT_IPE_1);
|
---|
| 349 | pVCpu->iom.s.PendingIOPortWrite.IOPort = Port;
|
---|
| 350 | pVCpu->iom.s.PendingIOPortWrite.u32Value = u32Value;
|
---|
| 351 | pVCpu->iom.s.PendingIOPortWrite.cbValue = (uint32_t)cbValue;
|
---|
| 352 | VMCPU_FF_SET(pVCpu, VMCPU_FF_IOM);
|
---|
| 353 | return VINF_IOM_R3_IOPORT_COMMIT_WRITE;
|
---|
| 354 | }
|
---|
| 355 | #endif
|
---|
| 356 |
|
---|
| 357 |
|
---|
| 358 | /**
|
---|
[1] | 359 | * Writes to an I/O port register.
|
---|
| 360 | *
|
---|
[7724] | 361 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
[3145] | 362 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
[2504] | 363 | * @retval VINF_SUCCESS Success.
|
---|
[7724] | 364 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
[3145] | 365 | * status code must be passed on to EM.
|
---|
[56403] | 366 | * @retval VINF_IOM_R3_IOPORT_WRITE Defer the write to ring-3. (R0/RC only)
|
---|
[1] | 367 | *
|
---|
[58122] | 368 | * @param pVM The cross context VM structure.
|
---|
[58123] | 369 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
[1] | 370 | * @param Port The port to write to.
|
---|
| 371 | * @param u32Value The value to write.
|
---|
| 372 | * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
|
---|
| 373 | */
|
---|
[80641] | 374 | VMMDECL(VBOXSTRICTRC) IOMIOPortWrite(PVMCC pVM, PVMCPU pVCpu, RTIOPORT Port, uint32_t u32Value, size_t cbValue)
|
---|
[1] | 375 | {
|
---|
[82378] | 376 | STAM_COUNTER_INC(&pVM->iom.s.StatIoPortOut);
|
---|
[60847] | 377 | #ifndef IN_RING3
|
---|
| 378 | Assert(pVCpu->iom.s.PendingIOPortWrite.cbValue == 0);
|
---|
| 379 | #endif
|
---|
| 380 |
|
---|
[80641] | 381 | /* For lookups we need to share lock IOM. */
|
---|
[45311] | 382 | int rc2 = IOM_LOCK_SHARED(pVM);
|
---|
[90426] | 383 | if (RT_SUCCESS(rc2))
|
---|
| 384 | { /* likely */ }
|
---|
[19472] | 385 | #ifndef IN_RING3
|
---|
[90426] | 386 | else if (rc2 == VERR_SEM_BUSY)
|
---|
[60847] | 387 | return iomIOPortRing3WritePending(pVCpu, Port, u32Value, cbValue);
|
---|
[19472] | 388 | #endif
|
---|
[90426] | 389 | else
|
---|
| 390 | AssertMsgFailedReturn(("rc2=%Rrc\n", rc2), rc2);
|
---|
[19472] | 391 |
|
---|
[80641] | 392 | /*
|
---|
| 393 | * Get the entry for the current context.
|
---|
| 394 | */
|
---|
[80679] | 395 | uint16_t offPort;
|
---|
| 396 | CTX_SUFF(PIOMIOPORTENTRY) pRegEntry = iomIoPortGetEntry(pVM, Port, &offPort, &pVCpu->iom.s.idxIoPortLastWrite);
|
---|
[80641] | 397 | if (pRegEntry)
|
---|
| 398 | {
|
---|
[1] | 399 | #ifdef VBOX_WITH_STATISTICS
|
---|
[80679] | 400 | PIOMIOPORTSTATSENTRY pStats = iomIoPortGetStats(pVM, pRegEntry, offPort);
|
---|
[80641] | 401 | #endif
|
---|
| 402 |
|
---|
| 403 | /*
|
---|
| 404 | * Found an entry, get the data so we can leave the IOM lock.
|
---|
| 405 | */
|
---|
[81136] | 406 | uint16_t const fFlags = pRegEntry->fFlags;
|
---|
[80960] | 407 | PFNIOMIOPORTNEWOUT pfnOutCallback = pRegEntry->pfnOutCallback;
|
---|
| 408 | PPDMDEVINS pDevIns = pRegEntry->pDevIns;
|
---|
[80641] | 409 | #ifndef IN_RING3
|
---|
| 410 | if ( pfnOutCallback
|
---|
| 411 | && pDevIns
|
---|
| 412 | && pRegEntry->cPorts > 0)
|
---|
| 413 | { /* likely */ }
|
---|
| 414 | else
|
---|
| 415 | {
|
---|
| 416 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 417 | STAM_COUNTER_INC(&pStats->OutRZToR3);
|
---|
| 418 | return iomIOPortRing3WritePending(pVCpu, Port, u32Value, cbValue);
|
---|
| 419 | }
|
---|
| 420 | #endif
|
---|
| 421 | void *pvUser = pRegEntry->pvUser;
|
---|
| 422 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 423 | AssertPtr(pDevIns);
|
---|
| 424 | AssertPtr(pfnOutCallback);
|
---|
| 425 |
|
---|
| 426 | /*
|
---|
| 427 | * Call the device.
|
---|
| 428 | */
|
---|
[90346] | 429 | VBOXSTRICTRC rcStrict = PDMCritSectEnter(pVM, pDevIns->CTX_SUFF(pCritSectRo), VINF_IOM_R3_IOPORT_WRITE);
|
---|
[80641] | 430 | if (rcStrict == VINF_SUCCESS)
|
---|
| 431 | {
|
---|
| 432 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
[81136] | 433 | rcStrict = pfnOutCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? Port : offPort, u32Value, (unsigned)cbValue);
|
---|
[80641] | 434 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
| 435 |
|
---|
[90346] | 436 | PDMCritSectLeave(pVM, pDevIns->CTX_SUFF(pCritSectRo));
|
---|
[80641] | 437 |
|
---|
| 438 | #ifdef VBOX_WITH_STATISTICS
|
---|
[82378] | 439 | # ifndef IN_RING3
|
---|
| 440 | if (rcStrict != VINF_IOM_R3_IOPORT_WRITE)
|
---|
| 441 | # endif
|
---|
| 442 | {
|
---|
[80641] | 443 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Out));
|
---|
[82378] | 444 | STAM_COUNTER_INC(&iomIoPortGetStats(pVM, pRegEntry, 0)->Total);
|
---|
| 445 | }
|
---|
[80641] | 446 | #endif
|
---|
| 447 | Log3(("IOMIOPortWrite: Port=%RTiop u32=%08RX32 cb=%d rc=%Rrc\n", Port, u32Value, cbValue, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
| 448 | }
|
---|
| 449 | #ifndef IN_RING3
|
---|
| 450 | if (rcStrict == VINF_IOM_R3_IOPORT_WRITE)
|
---|
| 451 | {
|
---|
| 452 | STAM_COUNTER_INC(&pStats->OutRZToR3);
|
---|
| 453 | return iomIOPortRing3WritePending(pVCpu, Port, u32Value, cbValue);
|
---|
| 454 | }
|
---|
| 455 | #endif
|
---|
| 456 | return rcStrict;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[1] | 459 | /*
|
---|
| 460 | * Ok, no handler for that port.
|
---|
| 461 | */
|
---|
[82311] | 462 | IOM_UNLOCK_SHARED(pVM);
|
---|
[1] | 463 | Log3(("IOMIOPortWrite: Port=%RTiop u32=%08RX32 cb=%d nop\n", Port, u32Value, cbValue));
|
---|
| 464 | return VINF_SUCCESS;
|
---|
| 465 | }
|
---|
[2504] | 466 |
|
---|
| 467 |
|
---|
[1] | 468 | /**
|
---|
| 469 | * Writes the string buffer of an I/O port register.
|
---|
| 470 | *
|
---|
[7724] | 471 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
[3145] | 472 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
[56403] | 473 | * @retval VINF_SUCCESS Success or no string I/O callback in
|
---|
| 474 | * this context.
|
---|
[7724] | 475 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
[3145] | 476 | * status code must be passed on to EM.
|
---|
[56403] | 477 | * @retval VINF_IOM_R3_IOPORT_WRITE Defer the write to ring-3. (R0/RC only)
|
---|
[1] | 478 | *
|
---|
[58122] | 479 | * @param pVM The cross context VM structure.
|
---|
[58123] | 480 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
[56413] | 481 | * @param uPort The port to write to.
|
---|
| 482 | * @param pvSrc The guest page to read from.
|
---|
| 483 | * @param pcTransfers Pointer to the number of transfer units to write, on
|
---|
| 484 | * return remaining transfer units.
|
---|
[1] | 485 | * @param cb Size of the transfer unit (1, 2 or 4 bytes).
|
---|
[45305] | 486 | */
|
---|
[80641] | 487 | VMM_INT_DECL(VBOXSTRICTRC) IOMIOPortWriteString(PVMCC pVM, PVMCPU pVCpu, RTIOPORT uPort, void const *pvSrc,
|
---|
[56415] | 488 | uint32_t *pcTransfers, unsigned cb)
|
---|
[1] | 489 | {
|
---|
[82378] | 490 | STAM_COUNTER_INC(&pVM->iom.s.StatIoPortOutS);
|
---|
[60847] | 491 | Assert(pVCpu->iom.s.PendingIOPortWrite.cbValue == 0);
|
---|
[56413] | 492 | Assert(cb == 1 || cb == 2 || cb == 4);
|
---|
| 493 |
|
---|
[19472] | 494 | /* Take the IOM lock before performing any device I/O. */
|
---|
[45311] | 495 | int rc2 = IOM_LOCK_SHARED(pVM);
|
---|
[90426] | 496 | if (RT_SUCCESS(rc2))
|
---|
| 497 | { /* likely */ }
|
---|
[19472] | 498 | #ifndef IN_RING3
|
---|
[90426] | 499 | else if (rc2 == VERR_SEM_BUSY)
|
---|
[40280] | 500 | return VINF_IOM_R3_IOPORT_WRITE;
|
---|
[19472] | 501 | #endif
|
---|
[90426] | 502 | else
|
---|
| 503 | AssertMsgFailedReturn(("rc2=%Rrc\n", rc2), rc2);
|
---|
[19472] | 504 |
|
---|
[56415] | 505 | const uint32_t cRequestedTransfers = *pcTransfers;
|
---|
| 506 | Assert(cRequestedTransfers > 0);
|
---|
| 507 |
|
---|
[80641] | 508 | /*
|
---|
| 509 | * Get the entry for the current context.
|
---|
| 510 | */
|
---|
[80679] | 511 | uint16_t offPort;
|
---|
| 512 | CTX_SUFF(PIOMIOPORTENTRY) pRegEntry = iomIoPortGetEntry(pVM, uPort, &offPort, &pVCpu->iom.s.idxIoPortLastWriteStr);
|
---|
[80641] | 513 | if (pRegEntry)
|
---|
| 514 | {
|
---|
[1] | 515 | #ifdef VBOX_WITH_STATISTICS
|
---|
[80679] | 516 | PIOMIOPORTSTATSENTRY pStats = iomIoPortGetStats(pVM, pRegEntry, offPort);
|
---|
[80641] | 517 | #endif
|
---|
| 518 |
|
---|
| 519 | /*
|
---|
| 520 | * Found an entry, get the data so we can leave the IOM lock.
|
---|
| 521 | */
|
---|
[81136] | 522 | uint16_t const fFlags = pRegEntry->fFlags;
|
---|
[80960] | 523 | PFNIOMIOPORTNEWOUTSTRING pfnOutStrCallback = pRegEntry->pfnOutStrCallback;
|
---|
| 524 | PFNIOMIOPORTNEWOUT pfnOutCallback = pRegEntry->pfnOutCallback;
|
---|
| 525 | PPDMDEVINS pDevIns = pRegEntry->pDevIns;
|
---|
[80641] | 526 | #ifndef IN_RING3
|
---|
| 527 | if ( pfnOutCallback
|
---|
| 528 | && pDevIns
|
---|
| 529 | && pRegEntry->cPorts > 0)
|
---|
| 530 | { /* likely */ }
|
---|
| 531 | else
|
---|
| 532 | {
|
---|
| 533 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 534 | STAM_COUNTER_INC(&pStats->OutRZToR3);
|
---|
| 535 | return VINF_IOM_R3_IOPORT_WRITE;
|
---|
| 536 | }
|
---|
| 537 | #endif
|
---|
| 538 | void *pvUser = pRegEntry->pvUser;
|
---|
| 539 | IOM_UNLOCK_SHARED(pVM);
|
---|
| 540 | AssertPtr(pDevIns);
|
---|
| 541 | AssertPtr(pfnOutCallback);
|
---|
| 542 |
|
---|
| 543 | /*
|
---|
| 544 | * Call the device.
|
---|
| 545 | */
|
---|
[90346] | 546 | VBOXSTRICTRC rcStrict = PDMCritSectEnter(pVM, pDevIns->CTX_SUFF(pCritSectRo), VINF_IOM_R3_IOPORT_WRITE);
|
---|
[80641] | 547 | if (rcStrict == VINF_SUCCESS)
|
---|
| 548 | {
|
---|
| 549 | /*
|
---|
| 550 | * First using string I/O if possible.
|
---|
| 551 | */
|
---|
| 552 | if (pfnOutStrCallback)
|
---|
| 553 | {
|
---|
| 554 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
[81136] | 555 | rcStrict = pfnOutStrCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? uPort : offPort,
|
---|
| 556 | (uint8_t const *)pvSrc, pcTransfers, cb);
|
---|
[80641] | 557 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | /*
|
---|
| 561 | * Then doing the single I/O fallback.
|
---|
| 562 | */
|
---|
| 563 | if ( *pcTransfers > 0
|
---|
| 564 | && rcStrict == VINF_SUCCESS)
|
---|
| 565 | {
|
---|
| 566 | pvSrc = (uint8_t *)pvSrc + (cRequestedTransfers - *pcTransfers) * cb;
|
---|
| 567 | do
|
---|
| 568 | {
|
---|
| 569 | uint32_t u32Value;
|
---|
| 570 | switch (cb)
|
---|
| 571 | {
|
---|
| 572 | case 4: u32Value = *(uint32_t *)pvSrc; pvSrc = (uint8_t const *)pvSrc + 4; break;
|
---|
| 573 | case 2: u32Value = *(uint16_t *)pvSrc; pvSrc = (uint8_t const *)pvSrc + 2; break;
|
---|
| 574 | case 1: u32Value = *(uint8_t *)pvSrc; pvSrc = (uint8_t const *)pvSrc + 1; break;
|
---|
| 575 | default: AssertFailed(); u32Value = UINT32_MAX;
|
---|
| 576 | }
|
---|
| 577 | STAM_PROFILE_START(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
[81136] | 578 | rcStrict = pfnOutCallback(pDevIns, pvUser, fFlags & IOM_IOPORT_F_ABS ? uPort : offPort, u32Value, cb);
|
---|
[80641] | 579 | STAM_PROFILE_STOP(&pStats->CTX_SUFF_Z(ProfOut), a);
|
---|
| 580 | if (IOM_SUCCESS(rcStrict))
|
---|
| 581 | *pcTransfers -= 1;
|
---|
| 582 | } while ( *pcTransfers > 0
|
---|
| 583 | && rcStrict == VINF_SUCCESS);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[90346] | 586 | PDMCritSectLeave(pVM, pDevIns->CTX_SUFF(pCritSectRo));
|
---|
[80641] | 587 |
|
---|
| 588 | #ifdef VBOX_WITH_STATISTICS
|
---|
| 589 | # ifndef IN_RING3
|
---|
[82378] | 590 | if (rcStrict == VINF_IOM_R3_IOPORT_WRITE)
|
---|
[80641] | 591 | STAM_COUNTER_INC(&pStats->OutRZToR3);
|
---|
[82378] | 592 | else
|
---|
[80641] | 593 | # endif
|
---|
[82378] | 594 | {
|
---|
| 595 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Out));
|
---|
| 596 | STAM_COUNTER_INC(&iomIoPortGetStats(pVM, pRegEntry, 0)->Total);
|
---|
| 597 | }
|
---|
[80641] | 598 | #endif
|
---|
| 599 | Log3(("IOMIOPortWriteStr: uPort=%RTiop pvSrc=%p pcTransfer=%p:{%#x->%#x} cb=%d rcStrict=%Rrc\n",
|
---|
| 600 | uPort, pvSrc, pcTransfers, cRequestedTransfers, *pcTransfers, cb, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
| 601 | }
|
---|
| 602 | #ifndef IN_RING3
|
---|
| 603 | else
|
---|
| 604 | STAM_COUNTER_INC(&pStats->OutRZToR3);
|
---|
| 605 | #endif
|
---|
| 606 | return rcStrict;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[1] | 609 | /*
|
---|
| 610 | * Ok, no handler for this port.
|
---|
| 611 | */
|
---|
[82311] | 612 | IOM_UNLOCK_SHARED(pVM);
|
---|
[58354] | 613 | *pcTransfers = 0;
|
---|
| 614 | Log3(("IOMIOPortWriteStr: uPort=%RTiop (unused) pvSrc=%p pcTransfer=%p:{%#x->%#x} cb=%d rc=VINF_SUCCESS\n",
|
---|
[56415] | 615 | uPort, pvSrc, pcTransfers, cRequestedTransfers, *pcTransfers, cb));
|
---|
[1] | 616 | return VINF_SUCCESS;
|
---|
| 617 | }
|
---|
| 618 |
|
---|