VirtualBox

source: vbox/trunk/src/VBox/VMM/tools/VBoxVMMPreload.cpp@ 57984

Last change on this file since 57984 was 56287, checked in by vboxsync, 9 years ago

VMM: Updated (C) year.

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