1 | /* $Id: VBoxVMMPreload.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxVMMPreload - Preload VBox the ring-0 modules.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
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
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <iprt/buildconfig.h>
|
---|
33 | #include <iprt/getopt.h>
|
---|
34 | #include <iprt/initterm.h>
|
---|
35 | #include <iprt/message.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 |
|
---|
41 | #include <VBox/sup.h>
|
---|
42 | #include <VBox/version.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /**
|
---|
49 | * Known modules and their associated data (there are only known modules!).
|
---|
50 | */
|
---|
51 | static struct
|
---|
52 | {
|
---|
53 | const char *pszName;
|
---|
54 | bool fPreload;
|
---|
55 | void *pvImageBase;
|
---|
56 | } g_aModules[] =
|
---|
57 | {
|
---|
58 | { "VMMR0.r0", true, NULL },
|
---|
59 | { "VBoxDDR0.r0", true, NULL },
|
---|
60 | };
|
---|
61 |
|
---|
62 | static uint32_t g_cVerbose = 1;
|
---|
63 | static bool g_fLockDown = false;
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Parses the options.
|
---|
68 | *
|
---|
69 | * @returns RTEXITCODE_SUCCESS on success.
|
---|
70 | * @param argc Argument count .
|
---|
71 | * @param argv Argument vector.
|
---|
72 | * @param pfExit Set to @c true if we should exit.
|
---|
73 | */
|
---|
74 | static RTEXITCODE ParseOptions(int argc, char **argv, bool *pfExit)
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Parse arguments.
|
---|
78 | */
|
---|
79 | static const RTGETOPTDEF s_aOptions[] =
|
---|
80 | {
|
---|
81 | { "--only", 'o', RTGETOPT_REQ_STRING },
|
---|
82 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
83 | { "--lock" , 'l', RTGETOPT_REQ_NOTHING },
|
---|
84 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
85 | };
|
---|
86 |
|
---|
87 | bool fAll = true;
|
---|
88 |
|
---|
89 | int ch;
|
---|
90 | RTGETOPTUNION ValueUnion;
|
---|
91 | RTGETOPTSTATE GetState;
|
---|
92 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
|
---|
93 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
94 | {
|
---|
95 | switch(ch)
|
---|
96 | {
|
---|
97 | case 'o':
|
---|
98 | {
|
---|
99 | uint32_t i;
|
---|
100 |
|
---|
101 | if (fAll)
|
---|
102 | {
|
---|
103 | fAll = false;
|
---|
104 | for (i = 0; i < RT_ELEMENTS(g_aModules); i++)
|
---|
105 | g_aModules[i].fPreload = false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | i = RT_ELEMENTS(g_aModules);
|
---|
109 | while (i-- > 0)
|
---|
110 | if (!strcmp(ValueUnion.psz, g_aModules[i].pszName))
|
---|
111 | {
|
---|
112 | g_aModules[i].fPreload = true;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | if (i > RT_ELEMENTS(g_aModules))
|
---|
116 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "No known module '%s'", ValueUnion.psz);
|
---|
117 | break;
|
---|
118 | }
|
---|
119 |
|
---|
120 | case 'v':
|
---|
121 | g_cVerbose++;
|
---|
122 | break;
|
---|
123 |
|
---|
124 | case 'q':
|
---|
125 | g_cVerbose = 0;
|
---|
126 | break;
|
---|
127 |
|
---|
128 | case 'l':
|
---|
129 | g_fLockDown = true;
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case 'h':
|
---|
133 | RTPrintf(VBOX_PRODUCT " VMM ring-0 Module Preloader Version " VBOX_VERSION_STRING
|
---|
134 | "Copyright (C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
135 | "\n"
|
---|
136 | "Usage: VBoxVMMPreload [-hlqvV] [-o|--only <mod>]\n"
|
---|
137 | "\n");
|
---|
138 | *pfExit = true;
|
---|
139 | return RTEXITCODE_SUCCESS;
|
---|
140 |
|
---|
141 | case 'V':
|
---|
142 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
143 | *pfExit = true;
|
---|
144 | return RTEXITCODE_SUCCESS;
|
---|
145 |
|
---|
146 | default:
|
---|
147 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | return RTEXITCODE_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Loads the modules.
|
---|
156 | *
|
---|
157 | * @returns RTEXITCODE_SUCCESS on success.
|
---|
158 | */
|
---|
159 | static RTEXITCODE LoadModules(void)
|
---|
160 | {
|
---|
161 | RTERRINFOSTATIC ErrInfo;
|
---|
162 |
|
---|
163 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aModules); i++)
|
---|
164 | {
|
---|
165 | if (g_aModules[i].fPreload)
|
---|
166 | {
|
---|
167 | char szPath[RTPATH_MAX];
|
---|
168 | int rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
169 | if (RT_SUCCESS(rc))
|
---|
170 | rc = RTPathAppend(szPath, sizeof(szPath), g_aModules[i].pszName);
|
---|
171 | if (RT_FAILURE(rc))
|
---|
172 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAppPrivateArch or RTPathAppend returned %Rrc", rc);
|
---|
173 |
|
---|
174 | RTErrInfoInitStatic(&ErrInfo);
|
---|
175 | rc = SUPR3LoadModule(szPath, g_aModules[i].pszName, &g_aModules[i].pvImageBase, &ErrInfo.Core);
|
---|
176 | if (RT_FAILURE(rc))
|
---|
177 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "SUPR3LoadModule failed for %s (%s): %s (rc=%Rrc)",
|
---|
178 | g_aModules[i].pszName, szPath, ErrInfo.Core.pszMsg, rc);
|
---|
179 | if (g_cVerbose >= 1)
|
---|
180 | RTMsgInfo("Loaded '%s' ('%s') at %p\n", szPath, g_aModules[i].pszName, g_aModules[i].pvImageBase);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (g_fLockDown)
|
---|
185 | {
|
---|
186 | RTErrInfoInitStatic(&ErrInfo);
|
---|
187 | int rc = SUPR3LockDownLoader(&ErrInfo.Core);
|
---|
188 | if (RT_FAILURE(rc))
|
---|
189 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "SUPR3LockDownLoader failed: %s (rc=%Rrc)",
|
---|
190 | ErrInfo.Core.pszMsg, rc);
|
---|
191 | if (g_cVerbose >= 1)
|
---|
192 | RTMsgInfo("Locked down module loader interface!\n");
|
---|
193 | }
|
---|
194 |
|
---|
195 | RTStrmFlush(g_pStdOut);
|
---|
196 | return RTEXITCODE_SUCCESS;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Entry point.
|
---|
202 | */
|
---|
203 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
204 | {
|
---|
205 | RT_NOREF1(envp);
|
---|
206 | bool fExit = false;
|
---|
207 | RTEXITCODE rcExit = ParseOptions(argc, argv, &fExit);
|
---|
208 | if (rcExit == RTEXITCODE_SUCCESS && !fExit)
|
---|
209 | {
|
---|
210 | rcExit = LoadModules();
|
---|
211 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
212 | {
|
---|
213 | for (;;)
|
---|
214 | RTThreadSleep(RT_INDEFINITE_WAIT);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | return rcExit;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | #ifndef VBOX_WITH_HARDENING
|
---|
222 | /**
|
---|
223 | * Main entry point.
|
---|
224 | */
|
---|
225 | int main(int argc, char **argv, char **envp)
|
---|
226 | {
|
---|
227 | int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | return TrustedMain(argc, argv, envp);
|
---|
230 | return RTMsgInitFailure(rc);
|
---|
231 | }
|
---|
232 | #endif /* !VBOX_WITH_HARDENING */
|
---|
233 |
|
---|