1 | /* $Id: SUPLib.cpp 19255 2009-04-29 10:14:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - Common code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @page pg_sup SUP - The Support Library
|
---|
32 | *
|
---|
33 | * The support library is responsible for providing facilities to load
|
---|
34 | * VMM Host Ring-0 code, to call Host VMM Ring-0 code from Ring-3 Host
|
---|
35 | * code, to pin down physical memory, and more.
|
---|
36 | *
|
---|
37 | * The VMM Host Ring-0 code can be combined in the support driver if
|
---|
38 | * permitted by kernel module license policies. If it is not combined
|
---|
39 | * it will be externalized in a .r0 module that will be loaded using
|
---|
40 | * the IPRT loader.
|
---|
41 | *
|
---|
42 | * The Ring-0 calling is done thru a generic SUP interface which will
|
---|
43 | * tranfer an argument set and call a predefined entry point in the Host
|
---|
44 | * VMM Ring-0 code.
|
---|
45 | *
|
---|
46 | * See @ref grp_sup "SUP - Support APIs" for API details.
|
---|
47 | */
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Header Files *
|
---|
51 | *******************************************************************************/
|
---|
52 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
53 | #include <VBox/sup.h>
|
---|
54 | #include <VBox/err.h>
|
---|
55 | #include <VBox/param.h>
|
---|
56 | #include <VBox/vmm.h>
|
---|
57 | #include <VBox/log.h>
|
---|
58 | #include <VBox/x86.h>
|
---|
59 |
|
---|
60 | #include <iprt/assert.h>
|
---|
61 | #include <iprt/alloc.h>
|
---|
62 | #include <iprt/alloca.h>
|
---|
63 | #include <iprt/ldr.h>
|
---|
64 | #include <iprt/asm.h>
|
---|
65 | #include <iprt/mp.h>
|
---|
66 | #include <iprt/cpuset.h>
|
---|
67 | #include <iprt/thread.h>
|
---|
68 | #include <iprt/process.h>
|
---|
69 | #include <iprt/path.h>
|
---|
70 | #include <iprt/string.h>
|
---|
71 | #include <iprt/env.h>
|
---|
72 | #include <iprt/rand.h>
|
---|
73 |
|
---|
74 | #include "SUPLibInternal.h"
|
---|
75 | #include "SUPDrvIOC.h"
|
---|
76 |
|
---|
77 |
|
---|
78 | /*******************************************************************************
|
---|
79 | * Defined Constants And Macros *
|
---|
80 | *******************************************************************************/
|
---|
81 | /** R0 VMM module name. */
|
---|
82 | #define VMMR0_NAME "VMMR0"
|
---|
83 |
|
---|
84 |
|
---|
85 | /*******************************************************************************
|
---|
86 | * Structures and Typedefs *
|
---|
87 | *******************************************************************************/
|
---|
88 | typedef DECLCALLBACK(int) FNCALLVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
|
---|
89 | typedef FNCALLVMMR0 *PFNCALLVMMR0;
|
---|
90 |
|
---|
91 |
|
---|
92 | /*******************************************************************************
|
---|
93 | * Global Variables *
|
---|
94 | *******************************************************************************/
|
---|
95 | /** Init counter. */
|
---|
96 | static uint32_t g_cInits = 0;
|
---|
97 | /** Whether we've been preinitied. */
|
---|
98 | static bool g_fPreInited = false;
|
---|
99 | /** The SUPLib instance data.
|
---|
100 | * Well, at least parts of it, specificly the parts that are being handed over
|
---|
101 | * via the pre-init mechanism from the hardened executable stub. */
|
---|
102 | static SUPLIBDATA g_supLibData =
|
---|
103 | {
|
---|
104 | NIL_RTFILE
|
---|
105 | #if defined(RT_OS_DARWIN)
|
---|
106 | , NULL
|
---|
107 | #elif defined(RT_OS_LINUX)
|
---|
108 | , false
|
---|
109 | #endif
|
---|
110 | };
|
---|
111 |
|
---|
112 | /** Pointer to the Global Information Page.
|
---|
113 | *
|
---|
114 | * This pointer is valid as long as SUPLib has a open session. Anyone using
|
---|
115 | * the page must treat this pointer as higly volatile and not trust it beyond
|
---|
116 | * one transaction.
|
---|
117 | *
|
---|
118 | * @todo This will probably deserve it's own session or some other good solution...
|
---|
119 | */
|
---|
120 | DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
|
---|
121 | /** Address of the ring-0 mapping of the GIP. */
|
---|
122 | static PSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
|
---|
123 | /** The physical address of the GIP. */
|
---|
124 | static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
|
---|
125 |
|
---|
126 | /** The negotiated cookie. */
|
---|
127 | uint32_t g_u32Cookie = 0;
|
---|
128 | /** The negotiated session cookie. */
|
---|
129 | uint32_t g_u32SessionCookie;
|
---|
130 | /** Session handle. */
|
---|
131 | PSUPDRVSESSION g_pSession;
|
---|
132 | /** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
|
---|
133 | static PSUPQUERYFUNCS g_pFunctions;
|
---|
134 |
|
---|
135 | /** VMMR0 Load Address. */
|
---|
136 | static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
|
---|
137 | /** PAGE_ALLOC_EX sans kernel mapping support indicator. */
|
---|
138 | static bool g_fSupportsPageAllocNoKernel = true;
|
---|
139 | /** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
|
---|
140 | static uint32_t g_u32FakeMode = ~0;
|
---|
141 |
|
---|
142 |
|
---|
143 | /*******************************************************************************
|
---|
144 | * Internal Functions *
|
---|
145 | *******************************************************************************/
|
---|
146 | static int supInitFake(PSUPDRVSESSION *ppSession);
|
---|
147 | static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase);
|
---|
148 | static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
|
---|
149 |
|
---|
150 |
|
---|
151 | SUPR3DECL(int) SUPInstall(void)
|
---|
152 | {
|
---|
153 | return suplibOsInstall();
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | SUPR3DECL(int) SUPUninstall(void)
|
---|
158 | {
|
---|
159 | return suplibOsUninstall();
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | DECLEXPORT(int) supR3PreInit(PSUPPREINITDATA pPreInitData, uint32_t fFlags)
|
---|
164 | {
|
---|
165 | /*
|
---|
166 | * The caller is kind of trustworthy, just perform some basic checks.
|
---|
167 | *
|
---|
168 | * Note! Do not do any fancy stuff here because IPRT has NOT been
|
---|
169 | * initialized at this point.
|
---|
170 | */
|
---|
171 | if (!VALID_PTR(pPreInitData))
|
---|
172 | return VERR_INVALID_POINTER;
|
---|
173 | if (g_fPreInited || g_cInits > 0)
|
---|
174 | return VERR_WRONG_ORDER;
|
---|
175 |
|
---|
176 | if ( pPreInitData->u32Magic != SUPPREINITDATA_MAGIC
|
---|
177 | || pPreInitData->u32EndMagic != SUPPREINITDATA_MAGIC)
|
---|
178 | return VERR_INVALID_MAGIC;
|
---|
179 | if ( !(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
|
---|
180 | && pPreInitData->Data.hDevice == NIL_RTFILE)
|
---|
181 | return VERR_INVALID_HANDLE;
|
---|
182 | if ( (fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
|
---|
183 | && pPreInitData->Data.hDevice != NIL_RTFILE)
|
---|
184 | return VERR_INVALID_PARAMETER;
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Hand out the data.
|
---|
188 | */
|
---|
189 | int rc = supR3HardenedRecvPreInitData(pPreInitData);
|
---|
190 | if (RT_FAILURE(rc))
|
---|
191 | return rc;
|
---|
192 |
|
---|
193 | /** @todo This may need some small restructuring later, it doesn't quite work with a root service flag... */
|
---|
194 | if (!(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV))
|
---|
195 | {
|
---|
196 | g_supLibData = pPreInitData->Data;
|
---|
197 | g_fPreInited = true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
|
---|
205 | {
|
---|
206 | /*
|
---|
207 | * Perform some sanity checks.
|
---|
208 | * (Got some trouble with compile time member alignment assertions.)
|
---|
209 | */
|
---|
210 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
|
---|
211 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
|
---|
212 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
|
---|
213 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
|
---|
214 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
|
---|
215 | Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64CpuHz) & 0x7));
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Check if already initialized.
|
---|
219 | */
|
---|
220 | if (ppSession)
|
---|
221 | *ppSession = g_pSession;
|
---|
222 | if (g_cInits++ > 0)
|
---|
223 | return VINF_SUCCESS;
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Check for fake mode.
|
---|
227 | *
|
---|
228 | * Fake mode is used when we're doing smoke testing and debugging.
|
---|
229 | * It's also useful on platforms where we haven't root access or which
|
---|
230 | * we haven't ported the support driver to.
|
---|
231 | */
|
---|
232 | if (g_u32FakeMode == ~0U)
|
---|
233 | {
|
---|
234 | const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
|
---|
235 | if (psz && !strcmp(psz, "fake"))
|
---|
236 | ASMAtomicCmpXchgU32(&g_u32FakeMode, 1, ~0U);
|
---|
237 | else
|
---|
238 | ASMAtomicCmpXchgU32(&g_u32FakeMode, 0, ~0U);
|
---|
239 | }
|
---|
240 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
241 | return supInitFake(ppSession);
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Open the support driver.
|
---|
245 | */
|
---|
246 | int rc = suplibOsInit(&g_supLibData, g_fPreInited);
|
---|
247 | if (RT_SUCCESS(rc))
|
---|
248 | {
|
---|
249 | /*
|
---|
250 | * Negotiate the cookie.
|
---|
251 | */
|
---|
252 | SUPCOOKIE CookieReq;
|
---|
253 | memset(&CookieReq, 0xff, sizeof(CookieReq));
|
---|
254 | CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
|
---|
255 | CookieReq.Hdr.u32SessionCookie = RTRandU32();
|
---|
256 | CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
|
---|
257 | CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
|
---|
258 | CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
259 | CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
260 | strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
|
---|
261 | CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
|
---|
262 | const uint32_t MinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x000a0000
|
---|
263 | ? 0x000a0009
|
---|
264 | : SUPDRV_IOC_VERSION & 0xffff0000;
|
---|
265 | CookieReq.u.In.u32MinVersion = MinVersion;
|
---|
266 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
|
---|
267 | if ( RT_SUCCESS(rc)
|
---|
268 | && RT_SUCCESS(CookieReq.Hdr.rc))
|
---|
269 | {
|
---|
270 | if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
|
---|
271 | && CookieReq.u.Out.u32SessionVersion >= MinVersion)
|
---|
272 | {
|
---|
273 | /*
|
---|
274 | * Query the functions.
|
---|
275 | */
|
---|
276 | PSUPQUERYFUNCS pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
|
---|
277 | if (pFuncsReq)
|
---|
278 | {
|
---|
279 | pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
|
---|
280 | pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
|
---|
281 | pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
|
---|
282 | pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
|
---|
283 | pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
284 | pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
285 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq, SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
|
---|
286 | if (RT_SUCCESS(rc))
|
---|
287 | rc = pFuncsReq->Hdr.rc;
|
---|
288 | if (RT_SUCCESS(rc))
|
---|
289 | {
|
---|
290 | /*
|
---|
291 | * Map the GIP into userspace.
|
---|
292 | */
|
---|
293 | Assert(!g_pSUPGlobalInfoPage);
|
---|
294 | SUPGIPMAP GipMapReq;
|
---|
295 | GipMapReq.Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
|
---|
296 | GipMapReq.Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
|
---|
297 | GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
|
---|
298 | GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
|
---|
299 | GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
300 | GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
301 | GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
|
---|
302 | GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
|
---|
303 | GipMapReq.u.Out.pGipR3 = NULL;
|
---|
304 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
|
---|
305 | if (RT_SUCCESS(rc))
|
---|
306 | rc = GipMapReq.Hdr.rc;
|
---|
307 | if (RT_SUCCESS(rc))
|
---|
308 | {
|
---|
309 | AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
|
---|
310 | AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Set the globals and return success.
|
---|
314 | */
|
---|
315 | ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
|
---|
316 | ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
|
---|
317 | ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
|
---|
318 |
|
---|
319 | g_u32Cookie = CookieReq.u.Out.u32Cookie;
|
---|
320 | g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
|
---|
321 | g_pSession = CookieReq.u.Out.pSession;
|
---|
322 | g_pFunctions = pFuncsReq;
|
---|
323 | if (ppSession)
|
---|
324 | *ppSession = CookieReq.u.Out.pSession;
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | /* bailout */
|
---|
330 | RTMemFree(pFuncsReq);
|
---|
331 | }
|
---|
332 | else
|
---|
333 | rc = VERR_NO_MEMORY;
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
|
---|
338 | CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, MinVersion));
|
---|
339 | rc = VERR_VM_DRIVER_VERSION_MISMATCH;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | if (RT_SUCCESS(rc))
|
---|
345 | {
|
---|
346 | rc = CookieReq.Hdr.rc;
|
---|
347 | LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
|
---|
348 | CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
|
---|
349 | if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
|
---|
350 | rc = VERR_VM_DRIVER_VERSION_MISMATCH;
|
---|
351 | }
|
---|
352 | else
|
---|
353 | {
|
---|
354 | /* for pre 0x00060000 drivers */
|
---|
355 | LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
|
---|
356 | rc = VERR_VM_DRIVER_VERSION_MISMATCH;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | suplibOsTerm(&g_supLibData);
|
---|
361 | }
|
---|
362 | g_cInits--;
|
---|
363 |
|
---|
364 | return rc;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Fake mode init.
|
---|
369 | */
|
---|
370 | static int supInitFake(PSUPDRVSESSION *ppSession)
|
---|
371 | {
|
---|
372 | Log(("SUP: Fake mode!\n"));
|
---|
373 | static const SUPFUNC s_aFakeFunctions[] =
|
---|
374 | {
|
---|
375 | /* name function */
|
---|
376 | { "SUPR0AbsIs64bit", 0 },
|
---|
377 | { "SUPR0Abs64bitKernelCS", 0 },
|
---|
378 | { "SUPR0Abs64bitKernelSS", 0 },
|
---|
379 | { "SUPR0Abs64bitKernelDS", 0 },
|
---|
380 | { "SUPR0AbsKernelCS", 8 },
|
---|
381 | { "SUPR0AbsKernelSS", 16 },
|
---|
382 | { "SUPR0AbsKernelDS", 16 },
|
---|
383 | { "SUPR0AbsKernelES", 16 },
|
---|
384 | { "SUPR0AbsKernelFS", 24 },
|
---|
385 | { "SUPR0AbsKernelGS", 32 },
|
---|
386 | { "SUPR0ComponentRegisterFactory", 0xefeefffd },
|
---|
387 | { "SUPR0ComponentDeregisterFactory", 0xefeefffe },
|
---|
388 | { "SUPR0ComponentQueryFactory", 0xefeeffff },
|
---|
389 | { "SUPR0ObjRegister", 0xefef0000 },
|
---|
390 | { "SUPR0ObjAddRef", 0xefef0001 },
|
---|
391 | { "SUPR0ObjAddRefEx", 0xefef0001 },
|
---|
392 | { "SUPR0ObjRelease", 0xefef0002 },
|
---|
393 | { "SUPR0ObjVerifyAccess", 0xefef0003 },
|
---|
394 | { "SUPR0LockMem", 0xefef0004 },
|
---|
395 | { "SUPR0UnlockMem", 0xefef0005 },
|
---|
396 | { "SUPR0ContAlloc", 0xefef0006 },
|
---|
397 | { "SUPR0ContFree", 0xefef0007 },
|
---|
398 | { "SUPR0MemAlloc", 0xefef0008 },
|
---|
399 | { "SUPR0MemGetPhys", 0xefef0009 },
|
---|
400 | { "SUPR0MemFree", 0xefef000a },
|
---|
401 | { "SUPR0Printf", 0xefef000b },
|
---|
402 | { "SUPR0GetPagingMode", 0xefef000c },
|
---|
403 | { "SUPR0EnableVTx", 0xefef000c },
|
---|
404 | { "RTMemAlloc", 0xefef000d },
|
---|
405 | { "RTMemAllocZ", 0xefef000e },
|
---|
406 | { "RTMemFree", 0xefef000f },
|
---|
407 | { "RTR0MemObjAddress", 0xefef0010 },
|
---|
408 | { "RTR0MemObjAddressR3", 0xefef0011 },
|
---|
409 | { "RTR0MemObjAllocPage", 0xefef0012 },
|
---|
410 | { "RTR0MemObjAllocPhysNC", 0xefef0013 },
|
---|
411 | { "RTR0MemObjAllocLow", 0xefef0014 },
|
---|
412 | { "RTR0MemObjEnterPhys", 0xefef0014 },
|
---|
413 | { "RTR0MemObjFree", 0xefef0015 },
|
---|
414 | { "RTR0MemObjGetPagePhysAddr", 0xefef0016 },
|
---|
415 | { "RTR0MemObjMapUser", 0xefef0017 },
|
---|
416 | { "RTR0MemObjMapKernel", 0xefef0017 },
|
---|
417 | { "RTR0MemObjMapKernelEx", 0xefef0017 },
|
---|
418 | { "RTProcSelf", 0xefef0038 },
|
---|
419 | { "RTR0ProcHandleSelf", 0xefef0039 },
|
---|
420 | { "RTSemEventCreate", 0xefef0018 },
|
---|
421 | { "RTSemEventSignal", 0xefef0019 },
|
---|
422 | { "RTSemEventWait", 0xefef001a },
|
---|
423 | { "RTSemEventWaitNoResume", 0xefef001b },
|
---|
424 | { "RTSemEventDestroy", 0xefef001c },
|
---|
425 | { "RTSemEventMultiCreate", 0xefef001d },
|
---|
426 | { "RTSemEventMultiSignal", 0xefef001e },
|
---|
427 | { "RTSemEventMultiReset", 0xefef001f },
|
---|
428 | { "RTSemEventMultiWait", 0xefef0020 },
|
---|
429 | { "RTSemEventMultiWaitNoResume", 0xefef0021 },
|
---|
430 | { "RTSemEventMultiDestroy", 0xefef0022 },
|
---|
431 | { "RTSemFastMutexCreate", 0xefef0023 },
|
---|
432 | { "RTSemFastMutexDestroy", 0xefef0024 },
|
---|
433 | { "RTSemFastMutexRequest", 0xefef0025 },
|
---|
434 | { "RTSemFastMutexRelease", 0xefef0026 },
|
---|
435 | { "RTSpinlockCreate", 0xefef0027 },
|
---|
436 | { "RTSpinlockDestroy", 0xefef0028 },
|
---|
437 | { "RTSpinlockAcquire", 0xefef0029 },
|
---|
438 | { "RTSpinlockRelease", 0xefef002a },
|
---|
439 | { "RTSpinlockAcquireNoInts", 0xefef002b },
|
---|
440 | { "RTSpinlockReleaseNoInts", 0xefef002c },
|
---|
441 | { "RTTimeNanoTS", 0xefef002d },
|
---|
442 | { "RTTimeMillieTS", 0xefef002e },
|
---|
443 | { "RTTimeSystemNanoTS", 0xefef002f },
|
---|
444 | { "RTTimeSystemMillieTS", 0xefef0030 },
|
---|
445 | { "RTThreadNativeSelf", 0xefef0031 },
|
---|
446 | { "RTThreadSleep", 0xefef0032 },
|
---|
447 | { "RTThreadYield", 0xefef0033 },
|
---|
448 | { "RTLogDefaultInstance", 0xefef0034 },
|
---|
449 | { "RTLogRelDefaultInstance", 0xefef0035 },
|
---|
450 | { "RTLogSetDefaultInstanceThread", 0xefef0036 },
|
---|
451 | { "RTLogLogger", 0xefef0037 },
|
---|
452 | { "RTLogLoggerEx", 0xefef0038 },
|
---|
453 | { "RTLogLoggerExV", 0xefef0039 },
|
---|
454 | { "AssertMsg1", 0xefef003a },
|
---|
455 | { "AssertMsg2", 0xefef003b },
|
---|
456 | { "RTAssertMsg1", 0xefef003c },
|
---|
457 | { "RTAssertMsg2", 0xefef003d },
|
---|
458 | { "RTAssertMsg2V", 0xefef003e },
|
---|
459 | };
|
---|
460 |
|
---|
461 | /* fake r0 functions. */
|
---|
462 | g_pFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
|
---|
463 | if (g_pFunctions)
|
---|
464 | {
|
---|
465 | g_pFunctions->u.Out.cFunctions = RT_ELEMENTS(s_aFakeFunctions);
|
---|
466 | memcpy(&g_pFunctions->u.Out.aFunctions[0], &s_aFakeFunctions[0], sizeof(s_aFakeFunctions));
|
---|
467 | g_pSession = (PSUPDRVSESSION)(void *)g_pFunctions;
|
---|
468 | if (ppSession)
|
---|
469 | *ppSession = g_pSession;
|
---|
470 |
|
---|
471 | /* fake the GIP. */
|
---|
472 | g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
|
---|
473 | if (g_pSUPGlobalInfoPage)
|
---|
474 | {
|
---|
475 | g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
|
---|
476 | g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
|
---|
477 | /* the page is supposed to be invalid, so don't set the magic. */
|
---|
478 | return VINF_SUCCESS;
|
---|
479 | }
|
---|
480 |
|
---|
481 | RTMemFree(g_pFunctions);
|
---|
482 | g_pFunctions = NULL;
|
---|
483 | }
|
---|
484 | return VERR_NO_MEMORY;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | SUPR3DECL(int) SUPTerm(bool fForced)
|
---|
489 | {
|
---|
490 | /*
|
---|
491 | * Verify state.
|
---|
492 | */
|
---|
493 | AssertMsg(g_cInits > 0, ("SUPTerm() is called before SUPR3Init()!\n"));
|
---|
494 | if (g_cInits == 0)
|
---|
495 | return VERR_WRONG_ORDER;
|
---|
496 | if (g_cInits == 1 || fForced)
|
---|
497 | {
|
---|
498 | /*
|
---|
499 | * NULL the GIP pointer.
|
---|
500 | */
|
---|
501 | if (g_pSUPGlobalInfoPage)
|
---|
502 | {
|
---|
503 | ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, NULL);
|
---|
504 | ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, NULL);
|
---|
505 | ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
|
---|
506 | /* just a little safe guard against threads using the page. */
|
---|
507 | RTThreadSleep(50);
|
---|
508 | }
|
---|
509 |
|
---|
510 | /*
|
---|
511 | * Close the support driver.
|
---|
512 | */
|
---|
513 | int rc = suplibOsTerm(&g_supLibData);
|
---|
514 | if (rc)
|
---|
515 | return rc;
|
---|
516 |
|
---|
517 | g_u32Cookie = 0;
|
---|
518 | g_u32SessionCookie = 0;
|
---|
519 | g_cInits = 0;
|
---|
520 | }
|
---|
521 | else
|
---|
522 | g_cInits--;
|
---|
523 |
|
---|
524 | return 0;
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | SUPR3DECL(SUPPAGINGMODE) SUPGetPagingMode(void)
|
---|
529 | {
|
---|
530 | /* fake */
|
---|
531 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
532 | #ifdef RT_ARCH_AMD64
|
---|
533 | return SUPPAGINGMODE_AMD64_GLOBAL_NX;
|
---|
534 | #else
|
---|
535 | return SUPPAGINGMODE_32_BIT_GLOBAL;
|
---|
536 | #endif
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
540 | */
|
---|
541 | SUPGETPAGINGMODE Req;
|
---|
542 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
543 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
544 | Req.Hdr.cbIn = SUP_IOCTL_GET_PAGING_MODE_SIZE_IN;
|
---|
545 | Req.Hdr.cbOut = SUP_IOCTL_GET_PAGING_MODE_SIZE_OUT;
|
---|
546 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
547 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
548 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
|
---|
549 | if ( RT_FAILURE(rc)
|
---|
550 | || RT_FAILURE(Req.Hdr.rc))
|
---|
551 | {
|
---|
552 | LogRel(("SUPGetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
|
---|
553 | Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
|
---|
554 | }
|
---|
555 |
|
---|
556 | return Req.u.Out.enmMode;
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * For later.
|
---|
562 | */
|
---|
563 | static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
|
---|
564 | {
|
---|
565 | AssertMsgFailed(("%d\n", uOperation));
|
---|
566 | return VERR_NOT_SUPPORTED;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | SUPR3DECL(int) SUPCallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, unsigned idCpu)
|
---|
571 | {
|
---|
572 | if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
|
---|
573 | return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_RAW_RUN, idCpu);
|
---|
574 | if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HWACC_RUN))
|
---|
575 | return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_HWACC_RUN, idCpu);
|
---|
576 | if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
|
---|
577 | return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_NOP, idCpu);
|
---|
578 |
|
---|
579 | AssertMsgFailed(("%#x\n", uOperation));
|
---|
580 | return VERR_INTERNAL_ERROR;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | SUPR3DECL(int) SUPCallVMMR0Ex(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
|
---|
585 | {
|
---|
586 | /*
|
---|
587 | * The following operations don't belong here.
|
---|
588 | */
|
---|
589 | AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
|
---|
590 | && uOperation != SUP_VMMR0_DO_HWACC_RUN
|
---|
591 | && uOperation != SUP_VMMR0_DO_NOP,
|
---|
592 | ("%#x\n", uOperation),
|
---|
593 | VERR_INTERNAL_ERROR);
|
---|
594 |
|
---|
595 | /* fake */
|
---|
596 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
597 | return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
|
---|
598 |
|
---|
599 | int rc;
|
---|
600 | if (!pReqHdr)
|
---|
601 | {
|
---|
602 | /* no data. */
|
---|
603 | SUPCALLVMMR0 Req;
|
---|
604 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
605 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
606 | Req.Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(0);
|
---|
607 | Req.Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0);
|
---|
608 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
609 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
610 | Req.u.In.pVMR0 = pVMR0;
|
---|
611 | Req.u.In.uOperation = uOperation;
|
---|
612 | Req.u.In.u64Arg = u64Arg;
|
---|
613 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
|
---|
614 | if (RT_SUCCESS(rc))
|
---|
615 | rc = Req.Hdr.rc;
|
---|
616 | }
|
---|
617 | else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
|
---|
618 | {
|
---|
619 | AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
|
---|
620 | AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
|
---|
621 | const size_t cbReq = pReqHdr->cbReq;
|
---|
622 |
|
---|
623 | PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
|
---|
624 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
625 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
626 | pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
|
---|
627 | pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
|
---|
628 | pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
629 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
630 | pReq->u.In.pVMR0 = pVMR0;
|
---|
631 | pReq->u.In.uOperation = uOperation;
|
---|
632 | pReq->u.In.u64Arg = u64Arg;
|
---|
633 | memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
|
---|
634 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
|
---|
635 | if (RT_SUCCESS(rc))
|
---|
636 | rc = pReq->Hdr.rc;
|
---|
637 | memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
|
---|
638 | }
|
---|
639 | else /** @todo may have to remove the size limits one this request... */
|
---|
640 | AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
|
---|
641 | return rc;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | SUPR3DECL(int) SUPCallVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg)
|
---|
646 | {
|
---|
647 | /*
|
---|
648 | * The following operations don't belong here.
|
---|
649 | */
|
---|
650 | AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
|
---|
651 | && uOperation != SUP_VMMR0_DO_HWACC_RUN
|
---|
652 | && uOperation != SUP_VMMR0_DO_NOP,
|
---|
653 | ("%#x\n", uOperation),
|
---|
654 | VERR_INTERNAL_ERROR);
|
---|
655 | return SUPCallVMMR0Ex(pVMR0, uOperation, (uintptr_t)pvArg, NULL);
|
---|
656 | }
|
---|
657 |
|
---|
658 |
|
---|
659 | SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0)
|
---|
660 | {
|
---|
661 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
662 | return VINF_SUCCESS;
|
---|
663 |
|
---|
664 | SUPSETVMFORFAST Req;
|
---|
665 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
666 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
667 | Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
|
---|
668 | Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
|
---|
669 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
670 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
671 | Req.u.In.pVMR0 = pVMR0;
|
---|
672 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
|
---|
673 | if (RT_SUCCESS(rc))
|
---|
674 | rc = Req.Hdr.rc;
|
---|
675 | return rc;
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
|
---|
680 | {
|
---|
681 | AssertReturn(cchService < RT_SIZEOFMEMB(SUPCALLSERVICE, u.In.szName), VERR_INVALID_PARAMETER);
|
---|
682 | Assert(strlen(pszService) == cchService);
|
---|
683 |
|
---|
684 | /* fake */
|
---|
685 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
686 | return VERR_NOT_SUPPORTED;
|
---|
687 |
|
---|
688 | int rc;
|
---|
689 | if (!pReqHdr)
|
---|
690 | {
|
---|
691 | /* no data. */
|
---|
692 | SUPCALLSERVICE Req;
|
---|
693 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
694 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
695 | Req.Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(0);
|
---|
696 | Req.Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0);
|
---|
697 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
698 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
699 | memcpy(Req.u.In.szName, pszService, cchService);
|
---|
700 | Req.u.In.szName[cchService] = '\0';
|
---|
701 | Req.u.In.uOperation = uOperation;
|
---|
702 | Req.u.In.u64Arg = u64Arg;
|
---|
703 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(0), &Req, SUP_IOCTL_CALL_SERVICE_SIZE(0));
|
---|
704 | if (RT_SUCCESS(rc))
|
---|
705 | rc = Req.Hdr.rc;
|
---|
706 | }
|
---|
707 | else if (SUP_IOCTL_CALL_SERVICE_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
|
---|
708 | {
|
---|
709 | AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
|
---|
710 | AssertReturn(pReqHdr->u32Magic == SUPR0SERVICEREQHDR_MAGIC, VERR_INVALID_MAGIC);
|
---|
711 | const size_t cbReq = pReqHdr->cbReq;
|
---|
712 |
|
---|
713 | PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)alloca(SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
|
---|
714 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
715 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
716 | pReq->Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(cbReq);
|
---|
717 | pReq->Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(cbReq);
|
---|
718 | pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
719 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
720 | memcpy(pReq->u.In.szName, pszService, cchService);
|
---|
721 | pReq->u.In.szName[cchService] = '\0';
|
---|
722 | pReq->u.In.uOperation = uOperation;
|
---|
723 | pReq->u.In.u64Arg = u64Arg;
|
---|
724 | memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
|
---|
725 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(cbReq), pReq, SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
|
---|
726 | if (RT_SUCCESS(rc))
|
---|
727 | rc = pReq->Hdr.rc;
|
---|
728 | memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
|
---|
729 | }
|
---|
730 | else /** @todo may have to remove the size limits one this request... */
|
---|
731 | AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
|
---|
732 | return rc;
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | /**
|
---|
737 | * Worker for the SUPR3Logger* APIs.
|
---|
738 | *
|
---|
739 | * @returns VBox status code.
|
---|
740 | * @param enmWhich Which logger.
|
---|
741 | * @param fWhat What to do with the logger.
|
---|
742 | * @param pszFlags The flags settings.
|
---|
743 | * @param pszGroups The groups settings.
|
---|
744 | * @param pszDest The destionation specificier.
|
---|
745 | */
|
---|
746 | static int supR3LoggerSettings(SUPLOGGER enmWhich, uint32_t fWhat, const char *pszFlags, const char *pszGroups, const char *pszDest)
|
---|
747 | {
|
---|
748 | uint32_t const cchFlags = pszFlags ? (uint32_t)strlen(pszFlags) : 0;
|
---|
749 | uint32_t const cchGroups = pszGroups ? (uint32_t)strlen(pszGroups) : 0;
|
---|
750 | uint32_t const cchDest = pszDest ? (uint32_t)strlen(pszDest) : 0;
|
---|
751 | uint32_t const cbStrTab = cchFlags + !!cchFlags
|
---|
752 | + cchGroups + !!cchGroups
|
---|
753 | + cchDest + !!cchDest
|
---|
754 | + (!cchFlags && !cchGroups && !cchDest);
|
---|
755 |
|
---|
756 | PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)alloca(SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
|
---|
757 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
758 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
759 | pReq->Hdr.cbIn = SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(cbStrTab);
|
---|
760 | pReq->Hdr.cbOut = SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT;
|
---|
761 | pReq->Hdr.fFlags= SUPREQHDR_FLAGS_DEFAULT;
|
---|
762 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
763 | switch (enmWhich)
|
---|
764 | {
|
---|
765 | case SUPLOGGER_DEBUG: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_DEBUG; break;
|
---|
766 | case SUPLOGGER_RELEASE: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_RELEASE; break;
|
---|
767 | default:
|
---|
768 | return VERR_INVALID_PARAMETER;
|
---|
769 | }
|
---|
770 | pReq->u.In.fWhat = fWhat;
|
---|
771 |
|
---|
772 | uint32_t off = 0;
|
---|
773 | if (cchFlags)
|
---|
774 | {
|
---|
775 | pReq->u.In.offFlags = off;
|
---|
776 | memcpy(&pReq->u.In.szStrings[off], pszFlags, cchFlags + 1);
|
---|
777 | off += cchFlags + 1;
|
---|
778 | }
|
---|
779 | else
|
---|
780 | pReq->u.In.offFlags = cbStrTab - 1;
|
---|
781 |
|
---|
782 | if (cchGroups)
|
---|
783 | {
|
---|
784 | pReq->u.In.offGroups = off;
|
---|
785 | memcpy(&pReq->u.In.szStrings[off], pszGroups, cchGroups + 1);
|
---|
786 | off += cchGroups + 1;
|
---|
787 | }
|
---|
788 | else
|
---|
789 | pReq->u.In.offGroups = cbStrTab - 1;
|
---|
790 |
|
---|
791 | if (cchDest)
|
---|
792 | {
|
---|
793 | pReq->u.In.offDestination = off;
|
---|
794 | memcpy(&pReq->u.In.szStrings[off], pszDest, cchDest + 1);
|
---|
795 | off += cchDest + 1;
|
---|
796 | }
|
---|
797 | else
|
---|
798 | pReq->u.In.offDestination = cbStrTab - 1;
|
---|
799 |
|
---|
800 | if (!off)
|
---|
801 | {
|
---|
802 | pReq->u.In.szStrings[0] = '\0';
|
---|
803 | off++;
|
---|
804 | }
|
---|
805 | Assert(off == cbStrTab);
|
---|
806 | Assert(pReq->u.In.szStrings[cbStrTab - 1] == '\0');
|
---|
807 |
|
---|
808 |
|
---|
809 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOGGER_SETTINGS(cbStrTab), pReq, SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
|
---|
810 | if (RT_SUCCESS(rc))
|
---|
811 | rc = pReq->Hdr.rc;
|
---|
812 | return rc;
|
---|
813 | }
|
---|
814 |
|
---|
815 |
|
---|
816 | SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
|
---|
817 | {
|
---|
818 | return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_SETTINGS, pszFlags, pszGroups, pszDest);
|
---|
819 | }
|
---|
820 |
|
---|
821 |
|
---|
822 | SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
|
---|
823 | {
|
---|
824 | return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_CREATE, pszFlags, pszGroups, pszDest);
|
---|
825 | }
|
---|
826 |
|
---|
827 |
|
---|
828 | SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich)
|
---|
829 | {
|
---|
830 | return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_DESTROY, NULL, NULL, NULL);
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages)
|
---|
835 | {
|
---|
836 | /*
|
---|
837 | * Validate.
|
---|
838 | */
|
---|
839 | AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
|
---|
840 | *ppvPages = NULL;
|
---|
841 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
842 |
|
---|
843 | #ifdef RT_OS_WINDOWS
|
---|
844 | /*
|
---|
845 | * Temporary hack for windows until we've sorted out the
|
---|
846 | * locked memory that doesn't need to be accessible from kernel space.
|
---|
847 | */
|
---|
848 | return SUPPageAllocLockedEx(cPages, ppvPages, NULL);
|
---|
849 | #else
|
---|
850 | /*
|
---|
851 | * Call OS specific worker.
|
---|
852 | */
|
---|
853 | return suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
|
---|
854 | #endif
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | SUPR3DECL(int) SUPPageFree(void *pvPages, size_t cPages)
|
---|
859 | {
|
---|
860 | /*
|
---|
861 | * Validate.
|
---|
862 | */
|
---|
863 | AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
|
---|
864 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
865 |
|
---|
866 | #ifdef RT_OS_WINDOWS
|
---|
867 | /*
|
---|
868 | * Temporary hack for windows, see above.
|
---|
869 | */
|
---|
870 | return SUPPageFreeLocked(pvPages, cPages);
|
---|
871 | #else
|
---|
872 | /*
|
---|
873 | * Call OS specific worker.
|
---|
874 | */
|
---|
875 | return suplibOsPageFree(&g_supLibData, pvPages, cPages);
|
---|
876 | #endif
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
|
---|
881 | {
|
---|
882 | /*
|
---|
883 | * Validate.
|
---|
884 | */
|
---|
885 | AssertPtr(pvStart);
|
---|
886 | AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
|
---|
887 | AssertPtr(paPages);
|
---|
888 |
|
---|
889 | /* fake */
|
---|
890 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
891 | {
|
---|
892 | RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
|
---|
893 | size_t iPage = cPages;
|
---|
894 | while (iPage-- > 0)
|
---|
895 | paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
|
---|
896 | return VINF_SUCCESS;
|
---|
897 | }
|
---|
898 |
|
---|
899 | /*
|
---|
900 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
901 | */
|
---|
902 | int rc;
|
---|
903 | PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
|
---|
904 | if (RT_LIKELY(pReq))
|
---|
905 | {
|
---|
906 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
907 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
908 | pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
|
---|
909 | pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
|
---|
910 | pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
|
---|
911 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
912 | pReq->u.In.pvR3 = pvStart;
|
---|
913 | pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
|
---|
914 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
|
---|
915 | if (RT_SUCCESS(rc))
|
---|
916 | rc = pReq->Hdr.rc;
|
---|
917 | if (RT_SUCCESS(rc))
|
---|
918 | {
|
---|
919 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
920 | {
|
---|
921 | paPages[iPage].uReserved = 0;
|
---|
922 | paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
|
---|
923 | Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
|
---|
924 | }
|
---|
925 | }
|
---|
926 | RTMemTmpFree(pReq);
|
---|
927 | }
|
---|
928 | else
|
---|
929 | rc = VERR_NO_TMP_MEMORY;
|
---|
930 |
|
---|
931 | return rc;
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | SUPR3DECL(int) SUPPageUnlock(void *pvStart)
|
---|
936 | {
|
---|
937 | /*
|
---|
938 | * Validate.
|
---|
939 | */
|
---|
940 | AssertPtr(pvStart);
|
---|
941 | AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
|
---|
942 |
|
---|
943 | /* fake */
|
---|
944 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
945 | return VINF_SUCCESS;
|
---|
946 |
|
---|
947 | /*
|
---|
948 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
949 | */
|
---|
950 | SUPPAGEUNLOCK Req;
|
---|
951 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
952 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
953 | Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
|
---|
954 | Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
|
---|
955 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
956 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
957 | Req.u.In.pvR3 = pvStart;
|
---|
958 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
|
---|
959 | if (RT_SUCCESS(rc))
|
---|
960 | rc = Req.Hdr.rc;
|
---|
961 | return rc;
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | SUPR3DECL(int) SUPPageAllocLockedEx(size_t cPages, void **ppvPages, PSUPPAGE paPages)
|
---|
966 | {
|
---|
967 | return SUPR3PageAllocEx(cPages, 0 /*fFlags*/, ppvPages, NULL /*pR0Ptr*/, paPages);
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 | SUPR3DECL(int) SUPPageFreeLocked(void *pvPages, size_t cPages)
|
---|
972 | {
|
---|
973 | /*
|
---|
974 | * Validate.
|
---|
975 | */
|
---|
976 | AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
|
---|
977 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
978 |
|
---|
979 | /*
|
---|
980 | * Check if we're employing the fallback or not to avoid the
|
---|
981 | * fuzzy handling of this in SUPR3PageFreeEx.
|
---|
982 | */
|
---|
983 | int rc;
|
---|
984 | if (g_fSupportsPageAllocNoKernel)
|
---|
985 | rc = SUPR3PageFreeEx(pvPages, cPages);
|
---|
986 | else
|
---|
987 | {
|
---|
988 | /* fallback */
|
---|
989 | rc = SUPPageUnlock(pvPages);
|
---|
990 | if (RT_SUCCESS(rc))
|
---|
991 | rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
|
---|
992 | }
|
---|
993 | return rc;
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 | /**
|
---|
998 | * Fallback for SUPPageAllocLockedEx on systems where RTR0MemObjPhysAllocNC isn't supported.
|
---|
999 | */
|
---|
1000 | static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
|
---|
1001 | {
|
---|
1002 | int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
|
---|
1003 | if (RT_SUCCESS(rc))
|
---|
1004 | {
|
---|
1005 | if (!paPages)
|
---|
1006 | paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
|
---|
1007 | rc = SUPPageLock(*ppvPages, cPages, paPages);
|
---|
1008 | if (RT_FAILURE(rc))
|
---|
1009 | suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
|
---|
1010 | }
|
---|
1011 | return rc;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
|
---|
1016 | {
|
---|
1017 | /*
|
---|
1018 | * Validate.
|
---|
1019 | */
|
---|
1020 | AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
|
---|
1021 | *ppvPages = NULL;
|
---|
1022 | AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
|
---|
1023 | if (pR0Ptr)
|
---|
1024 | *pR0Ptr = NIL_RTR0PTR;
|
---|
1025 | AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
|
---|
1026 | AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
1027 |
|
---|
1028 | /* fake */
|
---|
1029 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1030 | {
|
---|
1031 | void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
|
---|
1032 | if (!pv)
|
---|
1033 | return VERR_NO_MEMORY;
|
---|
1034 | *ppvPages = pv;
|
---|
1035 | if (pR0Ptr)
|
---|
1036 | *pR0Ptr = (RTR0PTR)pv;
|
---|
1037 | if (paPages)
|
---|
1038 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
1039 | {
|
---|
1040 | paPages[iPage].uReserved = 0;
|
---|
1041 | paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
|
---|
1042 | Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
|
---|
1043 | }
|
---|
1044 | return VINF_SUCCESS;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | /*
|
---|
1048 | * Use fallback for non-R0 mapping?
|
---|
1049 | */
|
---|
1050 | if ( !pR0Ptr
|
---|
1051 | && !g_fSupportsPageAllocNoKernel)
|
---|
1052 | return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1056 | */
|
---|
1057 | int rc;
|
---|
1058 | PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
|
---|
1059 | if (pReq)
|
---|
1060 | {
|
---|
1061 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
1062 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1063 | pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
|
---|
1064 | pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
|
---|
1065 | pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
|
---|
1066 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1067 | pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
|
---|
1068 | pReq->u.In.fKernelMapping = pR0Ptr != NULL;
|
---|
1069 | pReq->u.In.fUserMapping = true;
|
---|
1070 | pReq->u.In.fReserved0 = false;
|
---|
1071 | pReq->u.In.fReserved1 = false;
|
---|
1072 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
|
---|
1073 | if (RT_SUCCESS(rc))
|
---|
1074 | {
|
---|
1075 | rc = pReq->Hdr.rc;
|
---|
1076 | if (RT_SUCCESS(rc))
|
---|
1077 | {
|
---|
1078 | *ppvPages = pReq->u.Out.pvR3;
|
---|
1079 | if (pR0Ptr)
|
---|
1080 | *pR0Ptr = pReq->u.Out.pvR0;
|
---|
1081 | if (paPages)
|
---|
1082 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
1083 | {
|
---|
1084 | paPages[iPage].uReserved = 0;
|
---|
1085 | paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
|
---|
1086 | Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
|
---|
1087 | }
|
---|
1088 | }
|
---|
1089 | else if ( rc == VERR_NOT_SUPPORTED
|
---|
1090 | && !pR0Ptr)
|
---|
1091 | {
|
---|
1092 | g_fSupportsPageAllocNoKernel = false;
|
---|
1093 | rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | RTMemTmpFree(pReq);
|
---|
1098 | }
|
---|
1099 | else
|
---|
1100 | rc = VERR_NO_TMP_MEMORY;
|
---|
1101 | return rc;
|
---|
1102 |
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 |
|
---|
1106 | SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
|
---|
1107 | {
|
---|
1108 | /*
|
---|
1109 | * Validate.
|
---|
1110 | */
|
---|
1111 | AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
|
---|
1112 | AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
|
---|
1113 | Assert(!(off & PAGE_OFFSET_MASK));
|
---|
1114 | Assert(!(cb & PAGE_OFFSET_MASK) && cb);
|
---|
1115 | Assert(!fFlags);
|
---|
1116 | *pR0Ptr = NIL_RTR0PTR;
|
---|
1117 |
|
---|
1118 | /* fake */
|
---|
1119 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1120 | return VERR_NOT_SUPPORTED;
|
---|
1121 |
|
---|
1122 | /*
|
---|
1123 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1124 | */
|
---|
1125 | SUPPAGEMAPKERNEL Req;
|
---|
1126 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1127 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1128 | Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
|
---|
1129 | Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
|
---|
1130 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1131 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1132 | Req.u.In.pvR3 = pvR3;
|
---|
1133 | Req.u.In.offSub = off;
|
---|
1134 | Req.u.In.cbSub = cb;
|
---|
1135 | Req.u.In.fFlags = fFlags;
|
---|
1136 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
|
---|
1137 | if (RT_SUCCESS(rc))
|
---|
1138 | rc = Req.Hdr.rc;
|
---|
1139 | if (RT_SUCCESS(rc))
|
---|
1140 | *pR0Ptr = Req.u.Out.pvR0;
|
---|
1141 | return rc;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 |
|
---|
1145 | SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
|
---|
1146 | {
|
---|
1147 | /*
|
---|
1148 | * Validate.
|
---|
1149 | */
|
---|
1150 | AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
|
---|
1151 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
1152 |
|
---|
1153 | /* fake */
|
---|
1154 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1155 | {
|
---|
1156 | RTMemPageFree(pvPages);
|
---|
1157 | return VINF_SUCCESS;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | /*
|
---|
1161 | * Try normal free first, then if it fails check if we're using the fallback .
|
---|
1162 | * for the allocations without kernel mappings and attempt unlocking it.
|
---|
1163 | */
|
---|
1164 | NOREF(cPages);
|
---|
1165 | SUPPAGEFREE Req;
|
---|
1166 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1167 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1168 | Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
|
---|
1169 | Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
|
---|
1170 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1171 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1172 | Req.u.In.pvR3 = pvPages;
|
---|
1173 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
|
---|
1174 | if (RT_SUCCESS(rc))
|
---|
1175 | {
|
---|
1176 | rc = Req.Hdr.rc;
|
---|
1177 | if ( rc == VERR_INVALID_PARAMETER
|
---|
1178 | && !g_fSupportsPageAllocNoKernel)
|
---|
1179 | {
|
---|
1180 | int rc2 = SUPPageUnlock(pvPages);
|
---|
1181 | if (RT_SUCCESS(rc2))
|
---|
1182 | rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | return rc;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 |
|
---|
1189 | SUPR3DECL(void *) SUPContAlloc(size_t cPages, PRTHCPHYS pHCPhys)
|
---|
1190 | {
|
---|
1191 | return SUPContAlloc2(cPages, NIL_RTR0PTR, pHCPhys);
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 |
|
---|
1195 | SUPR3DECL(void *) SUPContAlloc2(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
|
---|
1196 | {
|
---|
1197 | /*
|
---|
1198 | * Validate.
|
---|
1199 | */
|
---|
1200 | AssertPtrReturn(pHCPhys, NULL);
|
---|
1201 | *pHCPhys = NIL_RTHCPHYS;
|
---|
1202 | AssertPtrNullReturn(pR0Ptr, NULL);
|
---|
1203 | if (pR0Ptr)
|
---|
1204 | *pR0Ptr = NIL_RTR0PTR;
|
---|
1205 | AssertPtrNullReturn(pHCPhys, NULL);
|
---|
1206 | AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
|
---|
1207 |
|
---|
1208 | /* fake */
|
---|
1209 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1210 | {
|
---|
1211 | void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
|
---|
1212 | if (pR0Ptr)
|
---|
1213 | *pR0Ptr = (RTR0PTR)pv;
|
---|
1214 | if (pHCPhys)
|
---|
1215 | *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
|
---|
1216 | return pv;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1221 | */
|
---|
1222 | SUPCONTALLOC Req;
|
---|
1223 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1224 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1225 | Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
|
---|
1226 | Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
|
---|
1227 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1228 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1229 | Req.u.In.cPages = (uint32_t)cPages;
|
---|
1230 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
|
---|
1231 | if ( RT_SUCCESS(rc)
|
---|
1232 | && RT_SUCCESS(Req.Hdr.rc))
|
---|
1233 | {
|
---|
1234 | *pHCPhys = Req.u.Out.HCPhys;
|
---|
1235 | if (pR0Ptr)
|
---|
1236 | *pR0Ptr = Req.u.Out.pvR0;
|
---|
1237 | return Req.u.Out.pvR3;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | return NULL;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 |
|
---|
1244 | SUPR3DECL(int) SUPContFree(void *pv, size_t cPages)
|
---|
1245 | {
|
---|
1246 | /*
|
---|
1247 | * Validate.
|
---|
1248 | */
|
---|
1249 | if (!pv)
|
---|
1250 | return VINF_SUCCESS;
|
---|
1251 | AssertPtrReturn(pv, VERR_INVALID_POINTER);
|
---|
1252 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
1253 |
|
---|
1254 | /* fake */
|
---|
1255 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1256 | {
|
---|
1257 | RTMemPageFree(pv);
|
---|
1258 | return VINF_SUCCESS;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | /*
|
---|
1262 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1263 | */
|
---|
1264 | SUPCONTFREE Req;
|
---|
1265 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1266 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1267 | Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
|
---|
1268 | Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
|
---|
1269 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1270 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1271 | Req.u.In.pvR3 = pv;
|
---|
1272 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
|
---|
1273 | if (RT_SUCCESS(rc))
|
---|
1274 | rc = Req.Hdr.rc;
|
---|
1275 | return rc;
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 |
|
---|
1279 | SUPR3DECL(int) SUPLowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
|
---|
1280 | {
|
---|
1281 | /*
|
---|
1282 | * Validate.
|
---|
1283 | */
|
---|
1284 | AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
|
---|
1285 | *ppvPages = NULL;
|
---|
1286 | AssertPtrReturn(paPages, VERR_INVALID_POINTER);
|
---|
1287 | AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
1288 |
|
---|
1289 | /* fake */
|
---|
1290 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1291 | {
|
---|
1292 | *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
|
---|
1293 | if (!*ppvPages)
|
---|
1294 | return VERR_NO_LOW_MEMORY;
|
---|
1295 |
|
---|
1296 | /* fake physical addresses. */
|
---|
1297 | RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
|
---|
1298 | size_t iPage = cPages;
|
---|
1299 | while (iPage-- > 0)
|
---|
1300 | paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
|
---|
1301 | return VINF_SUCCESS;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /*
|
---|
1305 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1306 | */
|
---|
1307 | int rc;
|
---|
1308 | PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
|
---|
1309 | if (pReq)
|
---|
1310 | {
|
---|
1311 | pReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
1312 | pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1313 | pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
|
---|
1314 | pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
|
---|
1315 | pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
|
---|
1316 | pReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1317 | pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
|
---|
1318 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
|
---|
1319 | if (RT_SUCCESS(rc))
|
---|
1320 | rc = pReq->Hdr.rc;
|
---|
1321 | if (RT_SUCCESS(rc))
|
---|
1322 | {
|
---|
1323 | *ppvPages = pReq->u.Out.pvR3;
|
---|
1324 | if (ppvPagesR0)
|
---|
1325 | *ppvPagesR0 = pReq->u.Out.pvR0;
|
---|
1326 | if (paPages)
|
---|
1327 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
1328 | {
|
---|
1329 | paPages[iPage].uReserved = 0;
|
---|
1330 | paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
|
---|
1331 | Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
|
---|
1332 | Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
|
---|
1333 | }
|
---|
1334 | }
|
---|
1335 | RTMemTmpFree(pReq);
|
---|
1336 | }
|
---|
1337 | else
|
---|
1338 | rc = VERR_NO_TMP_MEMORY;
|
---|
1339 |
|
---|
1340 | return rc;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 |
|
---|
1344 | SUPR3DECL(int) SUPLowFree(void *pv, size_t cPages)
|
---|
1345 | {
|
---|
1346 | /*
|
---|
1347 | * Validate.
|
---|
1348 | */
|
---|
1349 | if (!pv)
|
---|
1350 | return VINF_SUCCESS;
|
---|
1351 | AssertPtrReturn(pv, VERR_INVALID_POINTER);
|
---|
1352 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
1353 |
|
---|
1354 | /* fake */
|
---|
1355 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1356 | {
|
---|
1357 | RTMemPageFree(pv);
|
---|
1358 | return VINF_SUCCESS;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | /*
|
---|
1362 | * Issue IOCtl to the SUPDRV kernel module.
|
---|
1363 | */
|
---|
1364 | SUPCONTFREE Req;
|
---|
1365 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1366 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1367 | Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
|
---|
1368 | Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
|
---|
1369 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1370 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1371 | Req.u.In.pvR3 = pv;
|
---|
1372 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
|
---|
1373 | if (RT_SUCCESS(rc))
|
---|
1374 | rc = Req.Hdr.rc;
|
---|
1375 | return rc;
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 |
|
---|
1379 | SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
|
---|
1380 | {
|
---|
1381 | /*
|
---|
1382 | * Quick input validation.
|
---|
1383 | */
|
---|
1384 | AssertPtr(pszFilename);
|
---|
1385 | AssertPtr(pszMsg);
|
---|
1386 | AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
|
---|
1387 | file is the same we verified after opening it. */
|
---|
1388 |
|
---|
1389 | /*
|
---|
1390 | * Only do the actual check in hardened builds.
|
---|
1391 | */
|
---|
1392 | #ifdef VBOX_WITH_HARDENING
|
---|
1393 | int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
|
---|
1394 | if (RT_FAILURE(rc))
|
---|
1395 | LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
|
---|
1396 | return rc;
|
---|
1397 | #else
|
---|
1398 | return VINF_SUCCESS;
|
---|
1399 | #endif
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 |
|
---|
1403 | SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
|
---|
1404 | {
|
---|
1405 | int rc = VINF_SUCCESS;
|
---|
1406 | #ifdef VBOX_WITH_HARDENING
|
---|
1407 | /*
|
---|
1408 | * Check that the module can be trusted.
|
---|
1409 | */
|
---|
1410 | rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
|
---|
1411 | #endif
|
---|
1412 | if (RT_SUCCESS(rc))
|
---|
1413 | rc = supLoadModule(pszFilename, pszModule, NULL, ppvImageBase);
|
---|
1414 | else
|
---|
1415 | LogRel(("SUPLoadModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
|
---|
1416 | return rc;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
|
---|
1421 | const char *pszSrvReqHandler, void **ppvImageBase)
|
---|
1422 | {
|
---|
1423 | int rc = VINF_SUCCESS;
|
---|
1424 | AssertPtrReturn(pszSrvReqHandler, VERR_INVALID_PARAMETER);
|
---|
1425 |
|
---|
1426 | #ifdef VBOX_WITH_HARDENING
|
---|
1427 | /*
|
---|
1428 | * Check that the module can be trusted.
|
---|
1429 | */
|
---|
1430 | rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
|
---|
1431 | #endif
|
---|
1432 | if (RT_SUCCESS(rc))
|
---|
1433 | rc = supLoadModule(pszFilename, pszModule, pszSrvReqHandler, ppvImageBase);
|
---|
1434 | else
|
---|
1435 | LogRel(("SUPR3LoadServiceModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
|
---|
1436 | return rc;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 |
|
---|
1440 | /**
|
---|
1441 | * Resolve an external symbol during RTLdrGetBits().
|
---|
1442 | *
|
---|
1443 | * @returns VBox status code.
|
---|
1444 | * @param hLdrMod The loader module handle.
|
---|
1445 | * @param pszModule Module name.
|
---|
1446 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
1447 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
1448 | * @param pValue Where to store the symbol value (address).
|
---|
1449 | * @param pvUser User argument.
|
---|
1450 | */
|
---|
1451 | static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
|
---|
1452 | const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
|
---|
1453 | {
|
---|
1454 | AssertPtr(pValue);
|
---|
1455 | AssertPtr(pvUser);
|
---|
1456 |
|
---|
1457 | /*
|
---|
1458 | * Only SUPR0 and VMMR0.r0
|
---|
1459 | */
|
---|
1460 | if ( pszModule
|
---|
1461 | && *pszModule
|
---|
1462 | && strcmp(pszModule, "SUPR0.dll")
|
---|
1463 | && strcmp(pszModule, "VMMR0.r0"))
|
---|
1464 | {
|
---|
1465 | AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
|
---|
1466 | return VERR_SYMBOL_NOT_FOUND;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | /*
|
---|
1470 | * No ordinals.
|
---|
1471 | */
|
---|
1472 | if (pszSymbol < (const char*)0x10000)
|
---|
1473 | {
|
---|
1474 | AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
|
---|
1475 | return VERR_SYMBOL_NOT_FOUND;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | /*
|
---|
1479 | * Lookup symbol.
|
---|
1480 | */
|
---|
1481 | /* skip the 64-bit ELF import prefix first. */
|
---|
1482 | if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
|
---|
1483 | pszSymbol += sizeof("SUPR0$") - 1;
|
---|
1484 |
|
---|
1485 | /*
|
---|
1486 | * Check the VMMR0.r0 module if loaded.
|
---|
1487 | */
|
---|
1488 | /** @todo call the SUPLoadModule caller.... */
|
---|
1489 | /** @todo proper reference counting and such. */
|
---|
1490 | if (g_pvVMMR0 != NIL_RTR0PTR)
|
---|
1491 | {
|
---|
1492 | void *pvValue;
|
---|
1493 | if (!SUPGetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
|
---|
1494 | {
|
---|
1495 | *pValue = (uintptr_t)pvValue;
|
---|
1496 | return VINF_SUCCESS;
|
---|
1497 | }
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | /* iterate the function table. */
|
---|
1501 | int c = g_pFunctions->u.Out.cFunctions;
|
---|
1502 | PSUPFUNC pFunc = &g_pFunctions->u.Out.aFunctions[0];
|
---|
1503 | while (c-- > 0)
|
---|
1504 | {
|
---|
1505 | if (!strcmp(pFunc->szName, pszSymbol))
|
---|
1506 | {
|
---|
1507 | *pValue = (uintptr_t)pFunc->pfn;
|
---|
1508 | return VINF_SUCCESS;
|
---|
1509 | }
|
---|
1510 | pFunc++;
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | /*
|
---|
1514 | * The GIP.
|
---|
1515 | */
|
---|
1516 | /** @todo R0 mapping? */
|
---|
1517 | if ( pszSymbol
|
---|
1518 | && g_pSUPGlobalInfoPage
|
---|
1519 | && g_pSUPGlobalInfoPageR0
|
---|
1520 | && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
|
---|
1521 | {
|
---|
1522 | *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
|
---|
1523 | return VINF_SUCCESS;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /*
|
---|
1527 | * Despair.
|
---|
1528 | */
|
---|
1529 | c = g_pFunctions->u.Out.cFunctions;
|
---|
1530 | pFunc = &g_pFunctions->u.Out.aFunctions[0];
|
---|
1531 | while (c-- > 0)
|
---|
1532 | {
|
---|
1533 | AssertMsg2("%d: %s\n", g_pFunctions->u.Out.cFunctions - c, pFunc->szName);
|
---|
1534 | pFunc++;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | AssertMsg2("%s is importing %s which we couldn't find\n", pvUser, pszSymbol);
|
---|
1538 | AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
|
---|
1539 | if (g_u32FakeMode)
|
---|
1540 | {
|
---|
1541 | *pValue = 0xdeadbeef;
|
---|
1542 | return VINF_SUCCESS;
|
---|
1543 | }
|
---|
1544 | return VERR_SYMBOL_NOT_FOUND;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 |
|
---|
1548 | /** Argument package for supLoadModuleCalcSizeCB. */
|
---|
1549 | typedef struct SUPLDRCALCSIZEARGS
|
---|
1550 | {
|
---|
1551 | size_t cbStrings;
|
---|
1552 | uint32_t cSymbols;
|
---|
1553 | size_t cbImage;
|
---|
1554 | } SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
|
---|
1555 |
|
---|
1556 | /**
|
---|
1557 | * Callback used to calculate the image size.
|
---|
1558 | * @return VINF_SUCCESS
|
---|
1559 | */
|
---|
1560 | static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
|
---|
1561 | {
|
---|
1562 | PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
|
---|
1563 | if ( pszSymbol != NULL
|
---|
1564 | && *pszSymbol
|
---|
1565 | && Value <= pArgs->cbImage)
|
---|
1566 | {
|
---|
1567 | pArgs->cSymbols++;
|
---|
1568 | pArgs->cbStrings += strlen(pszSymbol) + 1;
|
---|
1569 | }
|
---|
1570 | return VINF_SUCCESS;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 |
|
---|
1574 | /** Argument package for supLoadModuleCreateTabsCB. */
|
---|
1575 | typedef struct SUPLDRCREATETABSARGS
|
---|
1576 | {
|
---|
1577 | size_t cbImage;
|
---|
1578 | PSUPLDRSYM pSym;
|
---|
1579 | char *pszBase;
|
---|
1580 | char *psz;
|
---|
1581 | } SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
|
---|
1582 |
|
---|
1583 | /**
|
---|
1584 | * Callback used to calculate the image size.
|
---|
1585 | * @return VINF_SUCCESS
|
---|
1586 | */
|
---|
1587 | static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
|
---|
1588 | {
|
---|
1589 | PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
|
---|
1590 | if ( pszSymbol != NULL
|
---|
1591 | && *pszSymbol
|
---|
1592 | && Value <= pArgs->cbImage)
|
---|
1593 | {
|
---|
1594 | pArgs->pSym->offSymbol = (uint32_t)Value;
|
---|
1595 | pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
|
---|
1596 | pArgs->pSym++;
|
---|
1597 |
|
---|
1598 | size_t cbCopy = strlen(pszSymbol) + 1;
|
---|
1599 | memcpy(pArgs->psz, pszSymbol, cbCopy);
|
---|
1600 | pArgs->psz += cbCopy;
|
---|
1601 | }
|
---|
1602 | return VINF_SUCCESS;
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 |
|
---|
1606 | /**
|
---|
1607 | * Worker for SUPLoadModule().
|
---|
1608 | *
|
---|
1609 | * @returns VBox status code.
|
---|
1610 | * @param pszFilename Name of the VMMR0 image file
|
---|
1611 | */
|
---|
1612 | static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase)
|
---|
1613 | {
|
---|
1614 | /*
|
---|
1615 | * Validate input.
|
---|
1616 | */
|
---|
1617 | AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
|
---|
1618 | AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
|
---|
1619 | AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
|
---|
1620 | AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
|
---|
1621 |
|
---|
1622 | const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
|
---|
1623 | AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
|
---|
1624 | *ppvImageBase = NULL;
|
---|
1625 |
|
---|
1626 | /*
|
---|
1627 | * Open image file and figure its size.
|
---|
1628 | */
|
---|
1629 | RTLDRMOD hLdrMod;
|
---|
1630 | int rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_HOST, &hLdrMod);
|
---|
1631 | if (!RT_SUCCESS(rc))
|
---|
1632 | return rc;
|
---|
1633 |
|
---|
1634 | SUPLDRCALCSIZEARGS CalcArgs;
|
---|
1635 | CalcArgs.cbStrings = 0;
|
---|
1636 | CalcArgs.cSymbols = 0;
|
---|
1637 | CalcArgs.cbImage = RTLdrSize(hLdrMod);
|
---|
1638 | rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
|
---|
1639 | if (RT_SUCCESS(rc))
|
---|
1640 | {
|
---|
1641 | const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
|
---|
1642 | const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
|
---|
1643 | const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
|
---|
1644 |
|
---|
1645 | /*
|
---|
1646 | * Open the R0 image.
|
---|
1647 | */
|
---|
1648 | SUPLDROPEN OpenReq;
|
---|
1649 | OpenReq.Hdr.u32Cookie = g_u32Cookie;
|
---|
1650 | OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1651 | OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
|
---|
1652 | OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
|
---|
1653 | OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1654 | OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1655 | OpenReq.u.In.cbImage = cbImage;
|
---|
1656 | strcpy(OpenReq.u.In.szName, pszModule);
|
---|
1657 | if (!g_u32FakeMode)
|
---|
1658 | {
|
---|
1659 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
|
---|
1660 | if (RT_SUCCESS(rc))
|
---|
1661 | rc = OpenReq.Hdr.rc;
|
---|
1662 | }
|
---|
1663 | else
|
---|
1664 | {
|
---|
1665 | OpenReq.u.Out.fNeedsLoading = true;
|
---|
1666 | OpenReq.u.Out.pvImageBase = 0xef423420;
|
---|
1667 | }
|
---|
1668 | *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
|
---|
1669 | if ( RT_SUCCESS(rc)
|
---|
1670 | && OpenReq.u.Out.fNeedsLoading)
|
---|
1671 | {
|
---|
1672 | /*
|
---|
1673 | * We need to load it.
|
---|
1674 | * Allocate memory for the image bits.
|
---|
1675 | */
|
---|
1676 | PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
|
---|
1677 | if (pLoadReq)
|
---|
1678 | {
|
---|
1679 | /*
|
---|
1680 | * Get the image bits.
|
---|
1681 | */
|
---|
1682 | rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
|
---|
1683 | supLoadModuleResolveImport, (void *)pszModule);
|
---|
1684 |
|
---|
1685 | if (RT_SUCCESS(rc))
|
---|
1686 | {
|
---|
1687 | /*
|
---|
1688 | * Get the entry points.
|
---|
1689 | */
|
---|
1690 | RTUINTPTR VMMR0EntryInt = 0;
|
---|
1691 | RTUINTPTR VMMR0EntryFast = 0;
|
---|
1692 | RTUINTPTR VMMR0EntryEx = 0;
|
---|
1693 | RTUINTPTR SrvReqHandler = 0;
|
---|
1694 | RTUINTPTR ModuleInit = 0;
|
---|
1695 | RTUINTPTR ModuleTerm = 0;
|
---|
1696 | if (fIsVMMR0)
|
---|
1697 | {
|
---|
1698 | rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
|
---|
1699 | if (RT_SUCCESS(rc))
|
---|
1700 | rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
|
---|
1701 | if (RT_SUCCESS(rc))
|
---|
1702 | rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
|
---|
1703 | }
|
---|
1704 | else if (pszSrvReqHandler)
|
---|
1705 | rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, pszSrvReqHandler, &SrvReqHandler);
|
---|
1706 | if (RT_SUCCESS(rc))
|
---|
1707 | {
|
---|
1708 | int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
|
---|
1709 | if (RT_FAILURE(rc2))
|
---|
1710 | ModuleInit = 0;
|
---|
1711 |
|
---|
1712 | rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
|
---|
1713 | if (RT_FAILURE(rc2))
|
---|
1714 | ModuleTerm = 0;
|
---|
1715 | }
|
---|
1716 | if (RT_SUCCESS(rc))
|
---|
1717 | {
|
---|
1718 | /*
|
---|
1719 | * Create the symbol and string tables.
|
---|
1720 | */
|
---|
1721 | SUPLDRCREATETABSARGS CreateArgs;
|
---|
1722 | CreateArgs.cbImage = CalcArgs.cbImage;
|
---|
1723 | CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab];
|
---|
1724 | CreateArgs.pszBase = (char *)&pLoadReq->u.In.achImage[offStrTab];
|
---|
1725 | CreateArgs.psz = CreateArgs.pszBase;
|
---|
1726 | rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
|
---|
1727 | if (RT_SUCCESS(rc))
|
---|
1728 | {
|
---|
1729 | AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
|
---|
1730 | AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab]) <= CalcArgs.cSymbols);
|
---|
1731 |
|
---|
1732 | /*
|
---|
1733 | * Upload the image.
|
---|
1734 | */
|
---|
1735 | pLoadReq->Hdr.u32Cookie = g_u32Cookie;
|
---|
1736 | pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1737 | pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImage);
|
---|
1738 | pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
|
---|
1739 | pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
|
---|
1740 | pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1741 |
|
---|
1742 | pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
|
---|
1743 | pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
|
---|
1744 | if (fIsVMMR0)
|
---|
1745 | {
|
---|
1746 | pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
|
---|
1747 | pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
|
---|
1748 | pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryInt = (RTR0PTR)VMMR0EntryInt;
|
---|
1749 | pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
|
---|
1750 | pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
|
---|
1751 | }
|
---|
1752 | else if (pszSrvReqHandler)
|
---|
1753 | {
|
---|
1754 | pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
|
---|
1755 | pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
|
---|
1756 | pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
|
---|
1757 | pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
|
---|
1758 | pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
|
---|
1759 | }
|
---|
1760 | else
|
---|
1761 | pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
|
---|
1762 | pLoadReq->u.In.offStrTab = offStrTab;
|
---|
1763 | pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
|
---|
1764 | AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
|
---|
1765 | pLoadReq->u.In.offSymbols = offSymTab;
|
---|
1766 | pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
|
---|
1767 | pLoadReq->u.In.cbImage = cbImage;
|
---|
1768 | pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
|
---|
1769 | if (!g_u32FakeMode)
|
---|
1770 | {
|
---|
1771 | rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
|
---|
1772 | if (RT_SUCCESS(rc))
|
---|
1773 | rc = pLoadReq->Hdr.rc;
|
---|
1774 | }
|
---|
1775 | else
|
---|
1776 | rc = VINF_SUCCESS;
|
---|
1777 | if ( RT_SUCCESS(rc)
|
---|
1778 | || rc == VERR_ALREADY_LOADED /* A competing process. */
|
---|
1779 | )
|
---|
1780 | {
|
---|
1781 | LogRel(("SUP: Loaded %s (%s) at %#p - ModuleInit at %RTptr and ModuleTerm at %RTptr\n", pszModule, pszFilename,
|
---|
1782 | OpenReq.u.Out.pvImageBase, ModuleInit, ModuleTerm));
|
---|
1783 | if (fIsVMMR0)
|
---|
1784 | {
|
---|
1785 | g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
|
---|
1786 | LogRel(("SUP: VMMR0EntryEx located at %RTptr, VMMR0EntryFast at %RTptr and VMMR0EntryInt at %RTptr\n",
|
---|
1787 | VMMR0EntryEx, VMMR0EntryFast, VMMR0EntryInt));
|
---|
1788 | }
|
---|
1789 | #ifdef RT_OS_WINDOWS
|
---|
1790 | LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
|
---|
1791 | #endif
|
---|
1792 |
|
---|
1793 | RTMemTmpFree(pLoadReq);
|
---|
1794 | RTLdrClose(hLdrMod);
|
---|
1795 | return VINF_SUCCESS;
|
---|
1796 | }
|
---|
1797 | }
|
---|
1798 | }
|
---|
1799 | }
|
---|
1800 | RTMemTmpFree(pLoadReq);
|
---|
1801 | }
|
---|
1802 | else
|
---|
1803 | {
|
---|
1804 | AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImage)));
|
---|
1805 | rc = VERR_NO_TMP_MEMORY;
|
---|
1806 | }
|
---|
1807 | }
|
---|
1808 | else if (RT_SUCCESS(rc))
|
---|
1809 | {
|
---|
1810 | if (fIsVMMR0)
|
---|
1811 | g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
|
---|
1812 | LogRel(("SUP: Opened %s (%s) at %#p.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase));
|
---|
1813 | #ifdef RT_OS_WINDOWS
|
---|
1814 | LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
|
---|
1815 | #endif
|
---|
1816 | }
|
---|
1817 | }
|
---|
1818 | RTLdrClose(hLdrMod);
|
---|
1819 | return rc;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 |
|
---|
1823 | SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
|
---|
1824 | {
|
---|
1825 | /* fake */
|
---|
1826 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1827 | {
|
---|
1828 | g_pvVMMR0 = NIL_RTR0PTR;
|
---|
1829 | return VINF_SUCCESS;
|
---|
1830 | }
|
---|
1831 |
|
---|
1832 | /*
|
---|
1833 | * Free the requested module.
|
---|
1834 | */
|
---|
1835 | SUPLDRFREE Req;
|
---|
1836 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1837 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1838 | Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
|
---|
1839 | Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
|
---|
1840 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1841 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1842 | Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
|
---|
1843 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
|
---|
1844 | if (RT_SUCCESS(rc))
|
---|
1845 | rc = Req.Hdr.rc;
|
---|
1846 | if ( RT_SUCCESS(rc)
|
---|
1847 | && (RTR0PTR)pvImageBase == g_pvVMMR0)
|
---|
1848 | g_pvVMMR0 = NIL_RTR0PTR;
|
---|
1849 | return rc;
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 |
|
---|
1853 | SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
|
---|
1854 | {
|
---|
1855 | *ppvValue = NULL;
|
---|
1856 |
|
---|
1857 | /* fake */
|
---|
1858 | if (RT_UNLIKELY(g_u32FakeMode))
|
---|
1859 | {
|
---|
1860 | *ppvValue = (void *)(uintptr_t)0xdeadf00d;
|
---|
1861 | return VINF_SUCCESS;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | /*
|
---|
1865 | * Do ioctl.
|
---|
1866 | */
|
---|
1867 | SUPLDRGETSYMBOL Req;
|
---|
1868 | Req.Hdr.u32Cookie = g_u32Cookie;
|
---|
1869 | Req.Hdr.u32SessionCookie = g_u32SessionCookie;
|
---|
1870 | Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
|
---|
1871 | Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
|
---|
1872 | Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
|
---|
1873 | Req.Hdr.rc = VERR_INTERNAL_ERROR;
|
---|
1874 | Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
|
---|
1875 | size_t cchSymbol = strlen(pszSymbol);
|
---|
1876 | if (cchSymbol >= sizeof(Req.u.In.szSymbol))
|
---|
1877 | return VERR_SYMBOL_NOT_FOUND;
|
---|
1878 | memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
|
---|
1879 | int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
|
---|
1880 | if (RT_SUCCESS(rc))
|
---|
1881 | rc = Req.Hdr.rc;
|
---|
1882 | if (RT_SUCCESS(rc))
|
---|
1883 | *ppvValue = (void *)Req.u.Out.pvSymbol;
|
---|
1884 | return rc;
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 |
|
---|
1888 | SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
|
---|
1889 | {
|
---|
1890 | void *pvImageBase;
|
---|
1891 | return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 |
|
---|
1895 | SUPR3DECL(int) SUPUnloadVMM(void)
|
---|
1896 | {
|
---|
1897 | return SUPFreeModule((void*)g_pvVMMR0);
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 |
|
---|
1901 | SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
|
---|
1902 | {
|
---|
1903 | if (g_pSUPGlobalInfoPage)
|
---|
1904 | {
|
---|
1905 | *pHCPhys = g_HCPhysSUPGlobalInfoPage;
|
---|
1906 | return VINF_SUCCESS;
|
---|
1907 | }
|
---|
1908 | *pHCPhys = NIL_RTHCPHYS;
|
---|
1909 | return VERR_WRONG_ORDER;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 |
|
---|
1913 | /**
|
---|
1914 | * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
|
---|
1915 | *
|
---|
1916 | * @returns iprt status code.
|
---|
1917 | * @param pszFilename The full file name.
|
---|
1918 | * @param phLdrMod Where to store the handle to the loaded module.
|
---|
1919 | */
|
---|
1920 | static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod)
|
---|
1921 | {
|
---|
1922 | #ifdef VBOX_WITH_HARDENING
|
---|
1923 | /*
|
---|
1924 | * Verify the image file.
|
---|
1925 | */
|
---|
1926 | int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
|
---|
1927 | if (RT_FAILURE(rc))
|
---|
1928 | {
|
---|
1929 | LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
|
---|
1930 | return rc;
|
---|
1931 | }
|
---|
1932 | #endif
|
---|
1933 |
|
---|
1934 | /*
|
---|
1935 | * Try load it.
|
---|
1936 | */
|
---|
1937 | return RTLdrLoad(pszFilename, phLdrMod);
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 |
|
---|
1941 | SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
|
---|
1942 | {
|
---|
1943 | /*
|
---|
1944 | * Validate input.
|
---|
1945 | */
|
---|
1946 | AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
|
---|
1947 | AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
|
---|
1948 | *phLdrMod = NIL_RTLDRMOD;
|
---|
1949 | AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
|
---|
1950 |
|
---|
1951 | /*
|
---|
1952 | * Add the default extension if it's missing.
|
---|
1953 | */
|
---|
1954 | if (!RTPathHaveExt(pszFilename))
|
---|
1955 | {
|
---|
1956 | const char *pszSuff = RTLdrGetSuff();
|
---|
1957 | size_t cchSuff = strlen(pszSuff);
|
---|
1958 | size_t cchFilename = strlen(pszFilename);
|
---|
1959 | char *psz = (char *)alloca(cchFilename + cchSuff + 1);
|
---|
1960 | AssertReturn(psz, VERR_NO_TMP_MEMORY);
|
---|
1961 | memcpy(psz, pszFilename, cchFilename);
|
---|
1962 | memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
|
---|
1963 | pszFilename = psz;
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | /*
|
---|
1967 | * Pass it on to the common library loader.
|
---|
1968 | */
|
---|
1969 | return supR3HardenedLdrLoadIt(pszFilename, phLdrMod);
|
---|
1970 | }
|
---|
1971 |
|
---|
1972 |
|
---|
1973 | SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
|
---|
1974 | {
|
---|
1975 | LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
|
---|
1976 |
|
---|
1977 | /*
|
---|
1978 | * Validate input.
|
---|
1979 | */
|
---|
1980 | AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
|
---|
1981 | *phLdrMod = NIL_RTLDRMOD;
|
---|
1982 | AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
|
---|
1983 | AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
|
---|
1984 |
|
---|
1985 | /*
|
---|
1986 | * Check the filename.
|
---|
1987 | */
|
---|
1988 | size_t cchFilename = strlen(pszFilename);
|
---|
1989 | AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
|
---|
1990 |
|
---|
1991 | const char *pszExt = "";
|
---|
1992 | size_t cchExt = 0;
|
---|
1993 | if (!RTPathHaveExt(pszFilename))
|
---|
1994 | {
|
---|
1995 | pszExt = RTLdrGetSuff();
|
---|
1996 | cchExt = strlen(pszExt);
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | /*
|
---|
2000 | * Construct the private arch path and check if the file exists.
|
---|
2001 | */
|
---|
2002 | char szPath[RTPATH_MAX];
|
---|
2003 | int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
|
---|
2004 | AssertRCReturn(rc, rc);
|
---|
2005 |
|
---|
2006 | char *psz = strchr(szPath, '\0');
|
---|
2007 | *psz++ = RTPATH_SLASH;
|
---|
2008 | memcpy(psz, pszFilename, cchFilename);
|
---|
2009 | psz += cchFilename;
|
---|
2010 | memcpy(psz, pszExt, cchExt + 1);
|
---|
2011 |
|
---|
2012 | if (!RTPathExists(szPath))
|
---|
2013 | {
|
---|
2014 | LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
|
---|
2015 | return VERR_FILE_NOT_FOUND;
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | /*
|
---|
2019 | * Pass it on to SUPR3HardenedLdrLoad.
|
---|
2020 | */
|
---|
2021 | rc = SUPR3HardenedLdrLoad(szPath, phLdrMod);
|
---|
2022 |
|
---|
2023 | LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
|
---|
2024 | return rc;
|
---|
2025 | }
|
---|
2026 |
|
---|