VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GCM.cpp@ 104507

Last change on this file since 104507 was 104507, checked in by vboxsync, 9 months ago

VMM,Main: Some GCM adjustments. bugref:9735 bugref:10683

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/** @file
2 * GCM - Guest Compatibility Manager.
3 */
4
5/*
6 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * SPDX-License-Identifier: GPL-3.0-only
25 */
26
27/** @page pg_gcm GCM - The Guest Compatibility Manager
28 *
29 * The Guest Compatibility Manager provides run-time compatibility fixes
30 * for certain known guest bugs.
31 *
32 * @see grp_gcm
33 *
34 *
35 * @section sec_gcm_fixer Fixers
36 *
37 * A GCM fixer implements a collection of run-time helpers/patches suitable for
38 * a specific guest type. Several fixers can be active at the same time; for
39 * example OS/2 or Windows 9x need their own fixers, but can also runs DOS
40 * applications which need DOS-specific fixers.
41 *
42 * The concept of fixers exists to reduce the number of false positives to a
43 * minimum. Heuristics are used to decide whether a particular fix should be
44 * applied or not; restricting the number of applicable fixes minimizes the
45 * chance that a fix could be misapplied.
46 *
47 * The fixers are invisible to a guest. A common problem is division by zero
48 * caused by a software timing loop which cannot deal with fast CPUs (where
49 * "fast" very much depends on the era when the software was written). A fixer
50 * intercepts division by zero, recognizes known register contents and code
51 * sequence, modifies one or more registers to avoid a divide error, and
52 * restarts the instruction.
53 *
54 * It is not expected that the set of active fixers would be changed during
55 * the lifetime of the VM.
56 */
57
58
59/*********************************************************************************************************************************
60* Header Files *
61*********************************************************************************************************************************/
62#define LOG_GROUP LOG_GROUP_GIM
63#include <VBox/vmm/gcm.h>
64#include <VBox/vmm/hm.h>
65#include <VBox/vmm/ssm.h>
66#include <VBox/vmm/pdmdev.h>
67#include "GCMInternal.h"
68#include <VBox/vmm/vm.h>
69
70#include <VBox/log.h>
71
72#include <iprt/err.h>
73#include <iprt/semaphore.h>
74#include <iprt/string.h>
75
76
77/*********************************************************************************************************************************
78* Internal Functions *
79*********************************************************************************************************************************/
80static FNSSMINTSAVEEXEC gcmR3Save;
81static FNSSMINTLOADEXEC gcmR3Load;
82
83
84/**
85 * Initializes the GCM.
86 *
87 * @returns VBox status code.
88 * @param pVM The cross context VM structure.
89 */
90VMMR3_INT_DECL(int) GCMR3Init(PVM pVM)
91{
92 LogFlow(("GCMR3Init\n"));
93
94 /*
95 * Assert alignment and sizes.
96 */
97 AssertCompile(sizeof(pVM->gcm.s) <= sizeof(pVM->gcm.padding));
98
99 /*
100 * Register the saved state data unit.
101 */
102 int rc = SSMR3RegisterInternal(pVM, "GCM", 0 /* uInstance */, GCM_SAVED_STATE_VERSION, sizeof(GCM),
103 NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
104 NULL /* pfnSavePrep */, gcmR3Save, NULL /* pfnSaveDone */,
105 NULL /* pfnLoadPrep */, gcmR3Load, NULL /* pfnLoadDone */);
106 if (RT_FAILURE(rc))
107 return rc;
108
109 /*
110 * Read & validate configuration.
111 */
112 static struct { const char *pszName; uint32_t cchName; uint32_t fFlag; } const s_aFixerIds[] =
113 {
114 { RT_STR_TUPLE("DivByZeroDOS"), GCMFIXER_DBZ_DOS },
115 { RT_STR_TUPLE("DivByZeroOS2"), GCMFIXER_DBZ_OS2 },
116 { RT_STR_TUPLE("DivByZeroWin9x"), GCMFIXER_DBZ_WIN9X },
117 };
118
119 /* Assemble valid value names for CFMGR3ValidateConfig. */
120 char szValidValues[1024];
121 size_t offValidValues = 0;
122 for (unsigned i = 0; i < RT_ELEMENTS(s_aFixerIds); i++)
123 {
124 AssertReturn(offValidValues + s_aFixerIds[i].cchName + 2 <= sizeof(szValidValues), VERR_INTERNAL_ERROR_2);
125 if (offValidValues)
126 szValidValues[offValidValues++] = '|';
127 memcpy(&szValidValues[offValidValues], s_aFixerIds[i].pszName, s_aFixerIds[i].cchName);
128 offValidValues += s_aFixerIds[i].cchName;
129 }
130 szValidValues[offValidValues] = '\0';
131
132 /* Validate the configuration. */
133 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GCM/");
134 rc = CFGMR3ValidateConfig(pCfgNode,
135 "/GCM/", /* pszNode (for error msgs) */
136 szValidValues,
137 "", /* pszValidNodes */
138 "GCM", /* pszWho */
139 0); /* uInstance */
140 if (RT_FAILURE(rc))
141 return rc;
142
143 /* Read the configuration. */
144 pVM->gcm.s.fFixerSet = 0;
145 for (unsigned i = 0; i < RT_ELEMENTS(s_aFixerIds); i++)
146 {
147 bool fEnabled = false;
148 rc = CFGMR3QueryBoolDef(pCfgNode, s_aFixerIds[i].pszName, &fEnabled, false);
149 if (RT_FAILURE(rc))
150 return VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "Error reading /GCM/%s as boolean: %Rrc", s_aFixerIds[i].pszName, rc);
151 if (fEnabled)
152 pVM->gcm.s.fFixerSet = s_aFixerIds[i].fFlag;
153 }
154
155#if 0 /* development override */
156 pVM->gcm.s.enmFixerIds = GCMFIXER_DBZ_OS2 | GCMFIXER_DBZ_DOS | GCMFIXER_DBZ_WIN9X;
157#endif
158
159 /*
160 * Log what's enabled.
161 */
162 offValidValues = 0;
163 for (unsigned i = 0; i < RT_ELEMENTS(s_aFixerIds); i++)
164 if (pVM->gcm.s.fFixerSet & s_aFixerIds[i].fFlag)
165 {
166 AssertReturn(offValidValues + s_aFixerIds[i].cchName + 4 <= sizeof(szValidValues), VERR_INTERNAL_ERROR_2);
167 if (!offValidValues)
168 {
169 szValidValues[offValidValues++] = ' ';
170 szValidValues[offValidValues++] = '(';
171 }
172 else
173 {
174 szValidValues[offValidValues++] = ',';
175 szValidValues[offValidValues++] = ' ';
176 }
177 memcpy(&szValidValues[offValidValues], s_aFixerIds[i].pszName, s_aFixerIds[i].cchName);
178 offValidValues += s_aFixerIds[i].cchName;
179 }
180 if (offValidValues)
181 szValidValues[offValidValues++] = ')';
182 szValidValues[offValidValues] = '\0';
183 LogRel(("GCM: Initialized - Fixer bits: %#x%s\n", pVM->gcm.s.fFixerSet, szValidValues));
184
185 return VINF_SUCCESS;
186}
187
188
189/**
190 * @callback_method_impl{FNSSMINTSAVEEXEC}
191 */
192static DECLCALLBACK(int) gcmR3Save(PVM pVM, PSSMHANDLE pSSM)
193{
194 AssertReturn(pVM, VERR_INVALID_PARAMETER);
195 AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
196
197 /*
198 * At present there is only configuration to save.
199 */
200 return SSMR3PutU32(pSSM, pVM->gcm.s.fFixerSet);
201}
202
203
204/**
205 * @callback_method_impl{FNSSMINTLOADEXEC}
206 */
207static DECLCALLBACK(int) gcmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
208{
209 if (uPass != SSM_PASS_FINAL)
210 return VINF_SUCCESS;
211 if (uVersion != GCM_SAVED_STATE_VERSION)
212 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
213
214 /*
215 * Load configuration and check it aginst the current (live migration,
216 * general paranoia).
217 */
218 uint32_t fFixerSet = 0;
219 int rc = SSMR3GetU32(pSSM, &fFixerSet);
220 AssertRCReturn(rc, rc);
221
222 if (fFixerSet != pVM->gcm.s.fFixerSet)
223 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GCM fixer set %#X differs from the configured one (%#X)."),
224 fFixerSet, pVM->gcm.s.fFixerSet);
225
226 return VINF_SUCCESS;
227}
228
229
230/**
231 * Terminates the GCM.
232 *
233 * Termination means cleaning up and freeing all resources,
234 * the VM itself is, at this point, powered off or suspended.
235 *
236 * @returns VBox status code.
237 * @param pVM The cross context VM structure.
238 */
239VMMR3_INT_DECL(int) GCMR3Term(PVM pVM)
240{
241 RT_NOREF(pVM);
242 return VINF_SUCCESS;
243}
244
245
246/**
247 * The VM is being reset.
248 *
249 * Do whatever fixer-specific resetting that needs to be done.
250 *
251 * @param pVM The cross context VM structure.
252 */
253VMMR3_INT_DECL(void) GCMR3Reset(PVM pVM)
254{
255 RT_NOREF(pVM);
256}
257
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette