VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPLib.cpp@ 8758

Last change on this file since 8758 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.4 KB
Line 
1/* $Id: SUPLib.cpp 8155 2008-04-18 15:16:47Z 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/*******************************************************************************
51* Header Files *
52*******************************************************************************/
53#define LOG_GROUP LOG_GROUP_SUP
54#include <VBox/sup.h>
55#include <VBox/err.h>
56#include <VBox/param.h>
57#include <VBox/vmm.h>
58#include <VBox/log.h>
59#include <VBox/x86.h>
60
61#include <iprt/assert.h>
62#include <iprt/alloc.h>
63#include <iprt/alloca.h>
64#include <iprt/ldr.h>
65#include <iprt/asm.h>
66#include <iprt/system.h>
67#include <iprt/thread.h>
68#include <iprt/process.h>
69#include <iprt/string.h>
70#include <iprt/env.h>
71#include <iprt/rand.h>
72
73#include "SUPLibInternal.h"
74#include "SUPDRVIOC.h"
75
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*******************************************************************************/
88typedef DECLCALLBACK(int) FNCALLVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
89typedef FNCALLVMMR0 *PFNCALLVMMR0;
90
91
92/*******************************************************************************
93* Global Variables *
94*******************************************************************************/
95/** Pointer to the Global Information Page.
96 *
97 * This pointer is valid as long as SUPLib has a open session. Anyone using
98 * the page must treat this pointer as higly volatile and not trust it beyond
99 * one transaction.
100 *
101 * @todo This will probably deserve it's own session or some other good solution...
102 */
103DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
104/** Address of the ring-0 mapping of the GIP. */
105static PSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
106/** The physical address of the GIP. */
107static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
108
109/** The negotiated cookie. */
110uint32_t g_u32Cookie = 0;
111/** The negotiated session cookie. */
112uint32_t g_u32SessionCookie;
113/** Session handle. */
114PSUPDRVSESSION g_pSession;
115/** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
116static PSUPQUERYFUNCS g_pFunctions;
117
118#ifdef VBOX_WITH_IDT_PATCHING
119/** The negotiated interrupt number. */
120static uint8_t g_u8Interrupt = 3;
121/** Pointer to the generated code fore calling VMMR0. */
122static PFNCALLVMMR0 g_pfnCallVMMR0;
123#endif
124/** VMMR0 Load Address. */
125static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
126/** Init counter. */
127static unsigned g_cInits = 0;
128/** PAGE_ALLOC support indicator. */
129static bool g_fSupportsPageAllocLocked = true;
130/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
131static uint32_t g_u32FakeMode = ~0;
132
133
134/*******************************************************************************
135* Internal Functions *
136*******************************************************************************/
137static int supInitFake(PSUPDRVSESSION *ppSession);
138static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase);
139#ifdef VBOX_WITH_IDT_PATCHING
140static int supInstallIDTE(void);
141#endif
142static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
143
144
145SUPR3DECL(int) SUPInstall(void)
146{
147 return suplibOsInstall();
148}
149
150
151SUPR3DECL(int) SUPUninstall(void)
152{
153 return suplibOsUninstall();
154}
155
156
157SUPR3DECL(int) SUPInit(PSUPDRVSESSION *ppSession /* NULL */, size_t cbReserve /* 0 */)
158{
159 /*
160 * Perform some sanity checks.
161 * (Got some trouble with compile time member alignment assertions.)
162 */
163 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
164 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
165 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
166 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
167 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
168 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64CpuHz) & 0x7));
169
170 /*
171 * Check if already initialized.
172 */
173 if (ppSession)
174 *ppSession = g_pSession;
175 if (g_cInits++ > 0)
176 return VINF_SUCCESS;
177
178 /*
179 * Check for fake mode.
180 *
181 * Fake mode is used when we're doing smoke testing and debugging.
182 * It's also useful on platforms where we haven't root access or which
183 * we haven't ported the support driver to.
184 */
185 if (g_u32FakeMode == ~0U)
186 {
187 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
188 if (psz && !strcmp(psz, "fake"))
189 ASMAtomicCmpXchgU32(&g_u32FakeMode, 1, ~0U);
190 else
191 ASMAtomicCmpXchgU32(&g_u32FakeMode, 0, ~0U);
192 }
193 if (RT_UNLIKELY(g_u32FakeMode))
194 return supInitFake(ppSession);
195
196 /**
197 * Open the support driver.
198 */
199 int rc = suplibOsInit(cbReserve);
200 if (RT_SUCCESS(rc))
201 {
202 /*
203 * Negotiate the cookie.
204 */
205 SUPCOOKIE CookieReq;
206 memset(&CookieReq, 0xff, sizeof(CookieReq));
207 CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
208 CookieReq.Hdr.u32SessionCookie = RTRandU32();
209 CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
210 CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
211 CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
212 CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
213 strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
214 CookieReq.u.In.u32ReqVersion = SUPDRVIOC_VERSION;
215 const uint32_t MinVersion = (SUPDRVIOC_VERSION & 0xffff0000) == 0x00070000
216 ? 0x00070001 /* need new exports */
217 : SUPDRVIOC_VERSION & 0xffff0000;
218 CookieReq.u.In.u32MinVersion = MinVersion;
219 rc = suplibOsIOCtl(SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
220 if ( RT_SUCCESS(rc)
221 && RT_SUCCESS(CookieReq.Hdr.rc))
222 {
223 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRVIOC_VERSION & 0xffff0000)
224 && CookieReq.u.Out.u32SessionVersion >= MinVersion)
225 {
226 /*
227 * Query the functions.
228 */
229 PSUPQUERYFUNCS pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
230 if (pFuncsReq)
231 {
232 pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
233 pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
234 pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
235 pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
236 pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
237 pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
238 rc = suplibOsIOCtl(SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq, SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
239 if (RT_SUCCESS(rc))
240 rc = pFuncsReq->Hdr.rc;
241 if (RT_SUCCESS(rc))
242 {
243 g_u32Cookie = CookieReq.u.Out.u32Cookie;
244 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
245 g_pSession = CookieReq.u.Out.pSession;
246 g_pFunctions = pFuncsReq;
247 if (ppSession)
248 *ppSession = CookieReq.u.Out.pSession;
249
250 /*
251 * Map the GIP into userspace.
252 * This is an optional feature, so we will ignore any failures here.
253 */
254 if (!g_pSUPGlobalInfoPage)
255 {
256 SUPGIPMAP GipMapReq;
257 GipMapReq.Hdr.u32Cookie = g_u32Cookie;
258 GipMapReq.Hdr.u32SessionCookie = g_u32SessionCookie;
259 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
260 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
261 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
262 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
263 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
264 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
265 GipMapReq.u.Out.pGipR3 = NULL;
266 rc = suplibOsIOCtl(SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
267 if (RT_SUCCESS(rc))
268 rc = GipMapReq.Hdr.rc;
269 if (RT_SUCCESS(rc))
270 {
271 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
272 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
273 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
274 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
275 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
276 }
277 }
278 return VINF_SUCCESS;
279 }
280
281 /* bailout */
282 RTMemFree(pFuncsReq);
283 }
284 else
285 rc = VERR_NO_MEMORY;
286 }
287 else
288 {
289 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
290 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRVIOC_VERSION, MinVersion));
291 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
292 }
293 }
294 else
295 {
296 if (RT_SUCCESS(rc))
297 {
298 rc = CookieReq.Hdr.rc;
299 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
300 CookieReq.u.Out.u32DriverVersion, SUPDRVIOC_VERSION, rc));
301 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
302 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
303 }
304 else
305 {
306 /* for pre 0x00060000 drivers */
307 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRVIOC_VERSION));
308 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
309 }
310 }
311
312 suplibOsTerm();
313 }
314 AssertMsgFailed(("SUPInit() failed rc=%Vrc\n", rc));
315 g_cInits--;
316
317 return rc;
318}
319
320/**
321 * Fake mode init.
322 */
323static int supInitFake(PSUPDRVSESSION *ppSession)
324{
325 Log(("SUP: Fake mode!\n"));
326 static const SUPFUNC s_aFakeFunctions[] =
327 {
328 /* name function */
329 { "SUPR0ObjRegister", 0xefef0000 },
330 { "SUPR0ObjAddRef", 0xefef0001 },
331 { "SUPR0ObjRelease", 0xefef0002 },
332 { "SUPR0ObjVerifyAccess", 0xefef0003 },
333 { "SUPR0LockMem", 0xefef0004 },
334 { "SUPR0UnlockMem", 0xefef0005 },
335 { "SUPR0ContAlloc", 0xefef0006 },
336 { "SUPR0ContFree", 0xefef0007 },
337 { "SUPR0MemAlloc", 0xefef0008 },
338 { "SUPR0MemGetPhys", 0xefef0009 },
339 { "SUPR0MemFree", 0xefef000a },
340 { "SUPR0Printf", 0xefef000b },
341 { "SUPR0ExecuteCallback", 0xefef000c },
342 { "RTMemAlloc", 0xefef000d },
343 { "RTMemAllocZ", 0xefef000e },
344 { "RTMemFree", 0xefef000f },
345 { "RTR0MemObjAddress", 0xefef0010 },
346 { "RTR0MemObjAddressR3", 0xefef0011 },
347 { "RTR0MemObjAllocPage", 0xefef0012 },
348 { "RTR0MemObjAllocPhysNC", 0xefef0013 },
349 { "RTR0MemObjAllocLow", 0xefef0014 },
350 { "RTR0MemObjFree", 0xefef0015 },
351 { "RTR0MemObjGetPagePhysAddr", 0xefef0016 },
352 { "RTR0MemObjMapUser", 0xefef0017 },
353 { "RTProcSelf", 0xefef0038 },
354 { "RTR0ProcHandleSelf", 0xefef0039 },
355 { "RTSemEventCreate", 0xefef0018 },
356 { "RTSemEventSignal", 0xefef0019 },
357 { "RTSemEventWait", 0xefef001a },
358 { "RTSemEventWaitNoResume", 0xefef001b },
359 { "RTSemEventDestroy", 0xefef001c },
360 { "RTSemEventMultiCreate", 0xefef001d },
361 { "RTSemEventMultiSignal", 0xefef001e },
362 { "RTSemEventMultiReset", 0xefef001f },
363 { "RTSemEventMultiWait", 0xefef0020 },
364 { "RTSemEventMultiWaitNoResume", 0xefef0021 },
365 { "RTSemEventMultiDestroy", 0xefef0022 },
366 { "RTSemFastMutexCreate", 0xefef0023 },
367 { "RTSemFastMutexDestroy", 0xefef0024 },
368 { "RTSemFastMutexRequest", 0xefef0025 },
369 { "RTSemFastMutexRelease", 0xefef0026 },
370 { "RTSpinlockCreate", 0xefef0027 },
371 { "RTSpinlockDestroy", 0xefef0028 },
372 { "RTSpinlockAcquire", 0xefef0029 },
373 { "RTSpinlockRelease", 0xefef002a },
374 { "RTSpinlockAcquireNoInts", 0xefef002b },
375 { "RTSpinlockReleaseNoInts", 0xefef002c },
376 { "RTThreadNativeSelf", 0xefef002d },
377 { "RTThreadSleep", 0xefef002e },
378 { "RTThreadYield", 0xefef002f },
379 { "RTLogDefaultInstance", 0xefef0030 },
380 { "RTLogRelDefaultInstance", 0xefef0031 },
381 { "RTLogSetDefaultInstanceThread", 0xefef0032 },
382 { "RTLogLogger", 0xefef0033 },
383 { "RTLogLoggerEx", 0xefef0034 },
384 { "RTLogLoggerExV", 0xefef0035 },
385 { "AssertMsg1", 0xefef0036 },
386 { "AssertMsg2", 0xefef0037 },
387 };
388
389 /* fake r0 functions. */
390 g_pFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
391 if (g_pFunctions)
392 {
393 g_pFunctions->u.Out.cFunctions = RT_ELEMENTS(s_aFakeFunctions);
394 memcpy(&g_pFunctions->u.Out.aFunctions[0], &s_aFakeFunctions[0], sizeof(s_aFakeFunctions));
395 g_pSession = (PSUPDRVSESSION)(void *)g_pFunctions;
396 if (ppSession)
397 *ppSession = g_pSession;
398#ifdef VBOX_WITH_IDT_PATCHING
399 Assert(g_u8Interrupt == 3);
400#endif
401
402 /* fake the GIP. */
403 g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
404 if (g_pSUPGlobalInfoPage)
405 {
406 g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
407 g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
408 /* the page is supposed to be invalid, so don't set the magic. */
409 return VINF_SUCCESS;
410 }
411
412 RTMemFree(g_pFunctions);
413 g_pFunctions = NULL;
414 }
415 return VERR_NO_MEMORY;
416}
417
418
419SUPR3DECL(int) SUPTerm(bool fForced)
420{
421 /*
422 * Verify state.
423 */
424 AssertMsg(g_cInits > 0, ("SUPTerm() is called before SUPInit()!\n"));
425 if (g_cInits == 0)
426 return VERR_WRONG_ORDER;
427 if (g_cInits == 1 || fForced)
428 {
429 /*
430 * NULL the GIP pointer.
431 */
432 if (g_pSUPGlobalInfoPage)
433 {
434 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, NULL);
435 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, NULL);
436 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
437 /* just a little safe guard against threads using the page. */
438 RTThreadSleep(50);
439 }
440
441 /*
442 * Close the support driver.
443 */
444 int rc = suplibOsTerm();
445 if (rc)
446 return rc;
447
448 g_u32Cookie = 0;
449 g_u32SessionCookie = 0;
450#ifdef VBOX_WITH_IDT_PATCHING
451 g_u8Interrupt = 3;
452#endif
453 g_cInits = 0;
454 }
455 else
456 g_cInits--;
457
458 return 0;
459}
460
461
462SUPR3DECL(SUPPAGINGMODE) SUPGetPagingMode(void)
463{
464 /* fake */
465 if (RT_UNLIKELY(g_u32FakeMode))
466#ifdef RT_ARCH_AMD64
467 return SUPPAGINGMODE_AMD64_GLOBAL_NX;
468#else
469 return SUPPAGINGMODE_32_BIT_GLOBAL;
470#endif
471
472 /*
473 * Issue IOCtl to the SUPDRV kernel module.
474 */
475 SUPGETPAGINGMODE Req;
476 Req.Hdr.u32Cookie = g_u32Cookie;
477 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
478 Req.Hdr.cbIn = SUP_IOCTL_GET_PAGING_MODE_SIZE_IN;
479 Req.Hdr.cbOut = SUP_IOCTL_GET_PAGING_MODE_SIZE_OUT;
480 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
481 Req.Hdr.rc = VERR_INTERNAL_ERROR;
482 int rc = suplibOsIOCtl(SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
483 if ( RT_FAILURE(rc)
484 || RT_FAILURE(Req.Hdr.rc))
485 {
486 LogRel(("SUPGetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
487 Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
488 }
489
490 return Req.u.Out.enmMode;
491}
492
493
494/**
495 * For later.
496 */
497static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
498{
499 AssertMsgFailed(("%d\n", uOperation));
500 return VERR_NOT_SUPPORTED;
501}
502
503
504SUPR3DECL(int) SUPCallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation)
505{
506 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
507 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_RAW_RUN);
508 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HWACC_RUN))
509 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_HWACC_RUN);
510 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
511 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_NOP);
512
513 AssertMsgFailed(("%#x\n", uOperation));
514 return VERR_INTERNAL_ERROR;
515}
516
517
518SUPR3DECL(int) SUPCallVMMR0Ex(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
519{
520#if 0 /* temp hack. */
521 /*
522 * The following operations don't belong here.
523 */
524 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
525 && uOperation != SUP_VMMR0_DO_HWACC_RUN
526 && uOperation != SUP_VMMR0_DO_NOP,
527 ("%#x\n", uOperation),
528 VERR_INTERNAL_ERROR);
529#else
530 if ( ( uOperation == SUP_VMMR0_DO_RAW_RUN
531 || uOperation == SUP_VMMR0_DO_HWACC_RUN
532 || uOperation == SUP_VMMR0_DO_NOP)
533 && !pReqHdr
534 && !u64Arg)
535 return (int) SUPCallVMMR0Fast(pVMR0, uOperation);
536#endif
537
538 /* fake */
539 if (RT_UNLIKELY(g_u32FakeMode))
540 return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
541
542 int rc;
543 if (!pReqHdr)
544 {
545 /* no data. */
546 SUPCALLVMMR0 Req;
547 Req.Hdr.u32Cookie = g_u32Cookie;
548 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
549 Req.Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(0);
550 Req.Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0);
551 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
552 Req.Hdr.rc = VERR_INTERNAL_ERROR;
553 Req.u.In.pVMR0 = pVMR0;
554 Req.u.In.uOperation = uOperation;
555 Req.u.In.u64Arg = u64Arg;
556 rc = suplibOsIOCtl(SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
557 if (RT_SUCCESS(rc))
558 rc = Req.Hdr.rc;
559 }
560 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
561 {
562 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
563 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
564 const size_t cbReq = pReqHdr->cbReq;
565
566 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
567 pReq->Hdr.u32Cookie = g_u32Cookie;
568 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
569 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
570 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
571 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
572 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
573 pReq->u.In.pVMR0 = pVMR0;
574 pReq->u.In.uOperation = uOperation;
575 pReq->u.In.u64Arg = u64Arg;
576 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
577 rc = suplibOsIOCtl(SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
578 if (RT_SUCCESS(rc))
579 rc = pReq->Hdr.rc;
580 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
581 }
582 else /** @todo may have to remove the size limits one this request... */
583 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
584 return rc;
585}
586
587
588SUPR3DECL(int) SUPCallVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg)
589{
590#if defined(VBOX_WITH_IDT_PATCHING)
591 return g_pfnCallVMMR0(pVMR0, uOperation, pvArg);
592
593#else
594 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
595 {
596 Assert(!pvArg);
597 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_RAW_RUN);
598 }
599 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HWACC_RUN))
600 {
601 Assert(!pvArg);
602 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_HWACC_RUN);
603 }
604 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
605 {
606 Assert(!pvArg);
607 return suplibOsIOCtlFast(SUP_IOCTL_FAST_DO_NOP);
608 }
609 return SUPCallVMMR0Ex(pVMR0, uOperation, (uintptr_t)pvArg, NULL);
610#endif
611}
612
613
614SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0)
615{
616 if (RT_UNLIKELY(g_u32FakeMode))
617 return VINF_SUCCESS;
618
619 SUPSETVMFORFAST Req;
620 Req.Hdr.u32Cookie = g_u32Cookie;
621 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
622 Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
623 Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
624 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
625 Req.Hdr.rc = VERR_INTERNAL_ERROR;
626 Req.u.In.pVMR0 = pVMR0;
627 int rc = suplibOsIOCtl(SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
628 if (RT_SUCCESS(rc))
629 rc = Req.Hdr.rc;
630 return rc;
631}
632
633
634SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages)
635{
636 /*
637 * Validate.
638 */
639 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
640 *ppvPages = NULL;
641 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
642
643#ifdef RT_OS_WINDOWS
644 /*
645 * Temporary hack for windows until we've sorted out the
646 * locked memory that doesn't need to be accessible from kernel space.
647 */
648 return SUPPageAllocLockedEx(cPages, ppvPages, NULL);
649#else
650 /*
651 * Call OS specific worker.
652 */
653 return suplibOsPageAlloc(cPages, ppvPages);
654#endif
655}
656
657
658SUPR3DECL(int) SUPPageFree(void *pvPages, size_t cPages)
659{
660 /*
661 * Validate.
662 */
663 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
664 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
665
666#ifdef RT_OS_WINDOWS
667 /*
668 * Temporary hack for windows, see above.
669 */
670 return SUPPageFreeLocked(pvPages, cPages);
671#else
672 /*
673 * Call OS specific worker.
674 */
675 return suplibOsPageFree(pvPages, cPages);
676#endif
677}
678
679
680SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
681{
682 /*
683 * Validate.
684 */
685 AssertPtr(pvStart);
686 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
687 AssertPtr(paPages);
688
689 /* fake */
690 if (RT_UNLIKELY(g_u32FakeMode))
691 {
692 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
693 unsigned iPage = cPages;
694 while (iPage-- > 0)
695 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
696 return VINF_SUCCESS;
697 }
698
699 /*
700 * Issue IOCtl to the SUPDRV kernel module.
701 */
702 int rc;
703 PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
704 if (RT_LIKELY(pReq))
705 {
706 pReq->Hdr.u32Cookie = g_u32Cookie;
707 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
708 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
709 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
710 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
711 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
712 pReq->u.In.pvR3 = pvStart;
713 pReq->u.In.cPages = cPages; AssertRelease(pReq->u.In.cPages == cPages);
714 rc = suplibOsIOCtl(SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
715 if (RT_SUCCESS(rc))
716 rc = pReq->Hdr.rc;
717 if (RT_SUCCESS(rc))
718 {
719 for (uint32_t iPage = 0; iPage < cPages; iPage++)
720 {
721 paPages[iPage].uReserved = 0;
722 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
723 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
724 }
725 }
726 RTMemTmpFree(pReq);
727 }
728 else
729 rc = VERR_NO_TMP_MEMORY;
730
731 return rc;
732}
733
734
735SUPR3DECL(int) SUPPageUnlock(void *pvStart)
736{
737 /*
738 * Validate.
739 */
740 AssertPtr(pvStart);
741 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
742
743 /* fake */
744 if (RT_UNLIKELY(g_u32FakeMode))
745 return VINF_SUCCESS;
746
747 /*
748 * Issue IOCtl to the SUPDRV kernel module.
749 */
750 SUPPAGEUNLOCK Req;
751 Req.Hdr.u32Cookie = g_u32Cookie;
752 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
753 Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
754 Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
755 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
756 Req.Hdr.rc = VERR_INTERNAL_ERROR;
757 Req.u.In.pvR3 = pvStart;
758 int rc = suplibOsIOCtl(SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
759 if (RT_SUCCESS(rc))
760 rc = Req.Hdr.rc;
761 return rc;
762}
763
764
765SUPR3DECL(int) SUPPageAllocLocked(size_t cPages, void **ppvPages)
766{
767 return SUPPageAllocLockedEx(cPages, ppvPages, NULL);
768}
769
770
771/**
772 * Fallback for SUPPageAllocLockedEx on systems where RTR0MemObjPhysAllocNC isn't supported.
773 */
774static int supPageAllocLockedFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
775{
776 int rc = suplibOsPageAlloc(cPages, ppvPages);
777 if (RT_SUCCESS(rc))
778 {
779 if (!paPages)
780 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
781 rc = SUPPageLock(*ppvPages, cPages, paPages);
782 if (RT_FAILURE(rc))
783 suplibOsPageFree(*ppvPages, cPages);
784 }
785 return rc;
786}
787
788
789SUPR3DECL(int) SUPPageAllocLockedEx(size_t cPages, void **ppvPages, PSUPPAGE paPages)
790{
791 /*
792 * Validate.
793 */
794 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
795 *ppvPages = NULL;
796 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
797
798 /* fake */
799 if (RT_UNLIKELY(g_u32FakeMode))
800 {
801 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
802 if (!*ppvPages)
803 return VERR_NO_MEMORY;
804 if (paPages)
805 for (size_t iPage = 0; iPage < cPages; iPage++)
806 {
807 paPages[iPage].uReserved = 0;
808 paPages[iPage].Phys = (iPage + 1234) << PAGE_SHIFT;
809 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
810 }
811 return VINF_SUCCESS;
812 }
813
814 /* use fallback? */
815 if (!g_fSupportsPageAllocLocked)
816 return supPageAllocLockedFallback(cPages, ppvPages, paPages);
817
818 /*
819 * Issue IOCtl to the SUPDRV kernel module.
820 */
821 int rc;
822 PSUPPAGEALLOC pReq = (PSUPPAGEALLOC)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_SIZE(cPages));
823 if (pReq)
824 {
825 pReq->Hdr.u32Cookie = g_u32Cookie;
826 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
827 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_SIZE_IN;
828 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_SIZE_OUT(cPages);
829 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
830 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
831 pReq->u.In.cPages = cPages; AssertRelease(pReq->u.In.cPages == cPages);
832 rc = suplibOsIOCtl(SUP_IOCTL_PAGE_ALLOC, pReq, SUP_IOCTL_PAGE_ALLOC_SIZE(cPages));
833 if (RT_SUCCESS(rc))
834 {
835 rc = pReq->Hdr.rc;
836 if (RT_SUCCESS(rc))
837 {
838 *ppvPages = pReq->u.Out.pvR3;
839 if (paPages)
840 for (size_t iPage = 0; iPage < cPages; iPage++)
841 {
842 paPages[iPage].uReserved = 0;
843 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
844 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
845 }
846 }
847 else if (rc == VERR_NOT_SUPPORTED)
848 {
849 g_fSupportsPageAllocLocked = false;
850 rc = supPageAllocLockedFallback(cPages, ppvPages, paPages);
851 }
852 }
853
854 RTMemTmpFree(pReq);
855 }
856 else
857 rc = VERR_NO_TMP_MEMORY;
858 return rc;
859}
860
861
862SUPR3DECL(int) SUPPageFreeLocked(void *pvPages, size_t cPages)
863{
864 /*
865 * Validate.
866 */
867 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
868 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
869
870 /* fake */
871 if (RT_UNLIKELY(g_u32FakeMode))
872 {
873 RTMemPageFree(pvPages);
874 return VINF_SUCCESS;
875 }
876
877 /*
878 * Issue IOCtl to the SUPDRV kernel module.
879 */
880 int rc;
881 if (g_fSupportsPageAllocLocked)
882 {
883 SUPPAGEFREE Req;
884 Req.Hdr.u32Cookie = g_u32Cookie;
885 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
886 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
887 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
888 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
889 Req.Hdr.rc = VERR_INTERNAL_ERROR;
890 Req.u.In.pvR3 = pvPages;
891 rc = suplibOsIOCtl(SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
892 if (RT_SUCCESS(rc))
893 rc = Req.Hdr.rc;
894 }
895 else
896 {
897 /* fallback */
898 rc = SUPPageUnlock(pvPages);
899 if (RT_SUCCESS(rc))
900 rc = suplibOsPageFree(pvPages, cPages);
901 }
902 return rc;
903}
904
905
906SUPR3DECL(void *) SUPContAlloc(size_t cPages, PRTHCPHYS pHCPhys)
907{
908 return SUPContAlloc2(cPages, NIL_RTR0PTR, pHCPhys);
909}
910
911
912SUPR3DECL(void *) SUPContAlloc2(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
913{
914 /*
915 * Validate.
916 */
917 AssertPtrReturn(pHCPhys, NULL);
918 *pHCPhys = NIL_RTHCPHYS;
919 AssertPtrNullReturn(pR0Ptr, NULL);
920 if (pR0Ptr)
921 *pR0Ptr = NIL_RTR0PTR;
922 AssertPtrNullReturn(pHCPhys, NULL);
923 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
924
925 /* fake */
926 if (RT_UNLIKELY(g_u32FakeMode))
927 {
928 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
929 if (pR0Ptr)
930 *pR0Ptr = (RTR0PTR)pv;
931 if (pHCPhys)
932 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
933 return pv;
934 }
935
936 /*
937 * Issue IOCtl to the SUPDRV kernel module.
938 */
939 SUPCONTALLOC Req;
940 Req.Hdr.u32Cookie = g_u32Cookie;
941 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
942 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
943 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
944 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
945 Req.Hdr.rc = VERR_INTERNAL_ERROR;
946 Req.u.In.cPages = cPages;
947 int rc = suplibOsIOCtl(SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
948 if ( RT_SUCCESS(rc)
949 && RT_SUCCESS(Req.Hdr.rc))
950 {
951 *pHCPhys = Req.u.Out.HCPhys;
952 if (pR0Ptr)
953 *pR0Ptr = Req.u.Out.pvR0;
954 return Req.u.Out.pvR3;
955 }
956
957 return NULL;
958}
959
960
961SUPR3DECL(int) SUPContFree(void *pv, size_t cPages)
962{
963 /*
964 * Validate.
965 */
966 if (!pv)
967 return VINF_SUCCESS;
968 AssertPtrReturn(pv, VERR_INVALID_POINTER);
969 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
970
971 /* fake */
972 if (RT_UNLIKELY(g_u32FakeMode))
973 {
974 RTMemPageFree(pv);
975 return VINF_SUCCESS;
976 }
977
978 /*
979 * Issue IOCtl to the SUPDRV kernel module.
980 */
981 SUPCONTFREE Req;
982 Req.Hdr.u32Cookie = g_u32Cookie;
983 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
984 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
985 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
986 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
987 Req.Hdr.rc = VERR_INTERNAL_ERROR;
988 Req.u.In.pvR3 = pv;
989 int rc = suplibOsIOCtl(SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
990 if (RT_SUCCESS(rc))
991 rc = Req.Hdr.rc;
992 return rc;
993}
994
995
996SUPR3DECL(int) SUPLowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
997{
998 /*
999 * Validate.
1000 */
1001 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1002 *ppvPages = NULL;
1003 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1004 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_INVALID_PARAMETER);
1005
1006 /* fake */
1007 if (RT_UNLIKELY(g_u32FakeMode))
1008 {
1009 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1010 if (!*ppvPages)
1011 return VERR_NO_LOW_MEMORY;
1012
1013 /* fake physical addresses. */
1014 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1015 unsigned iPage = cPages;
1016 while (iPage-- > 0)
1017 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1018 return VINF_SUCCESS;
1019 }
1020
1021 /*
1022 * Issue IOCtl to the SUPDRV kernel module.
1023 */
1024 int rc;
1025 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1026 if (pReq)
1027 {
1028 pReq->Hdr.u32Cookie = g_u32Cookie;
1029 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1030 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1031 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1032 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1033 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1034 pReq->u.In.cPages = cPages; AssertRelease(pReq->u.In.cPages == cPages);
1035 rc = suplibOsIOCtl(SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1036 if (RT_SUCCESS(rc))
1037 rc = pReq->Hdr.rc;
1038 if (RT_SUCCESS(rc))
1039 {
1040 *ppvPages = pReq->u.Out.pvR3;
1041 if (ppvPagesR0)
1042 *ppvPagesR0 = pReq->u.Out.pvR0;
1043 if (paPages)
1044 for (size_t iPage = 0; iPage < cPages; iPage++)
1045 {
1046 paPages[iPage].uReserved = 0;
1047 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1048 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1049 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1050 }
1051 }
1052 RTMemTmpFree(pReq);
1053 }
1054 else
1055 rc = VERR_NO_TMP_MEMORY;
1056
1057 return rc;
1058}
1059
1060
1061SUPR3DECL(int) SUPLowFree(void *pv, size_t cPages)
1062{
1063 /*
1064 * Validate.
1065 */
1066 if (!pv)
1067 return VINF_SUCCESS;
1068 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1069 AssertReturn(cPages > 0, VERR_INVALID_PARAMETER);
1070
1071 /* fake */
1072 if (RT_UNLIKELY(g_u32FakeMode))
1073 {
1074 RTMemPageFree(pv);
1075 return VINF_SUCCESS;
1076 }
1077
1078 /*
1079 * Issue IOCtl to the SUPDRV kernel module.
1080 */
1081 SUPCONTFREE Req;
1082 Req.Hdr.u32Cookie = g_u32Cookie;
1083 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1084 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1085 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1086 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1087 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1088 Req.u.In.pvR3 = pv;
1089 int rc = suplibOsIOCtl(SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1090 if (RT_SUCCESS(rc))
1091 rc = Req.Hdr.rc;
1092 return rc;
1093}
1094
1095
1096SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1097{
1098 /*
1099 * Load the module.
1100 * If it's VMMR0.r0 we need to install the IDTE.
1101 */
1102 int rc = supLoadModule(pszFilename, pszModule, ppvImageBase);
1103#ifdef VBOX_WITH_IDT_PATCHING
1104 if ( RT_SUCCESS(rc)
1105 && !strcmp(pszModule, "VMMR0.r0"))
1106 {
1107 rc = supInstallIDTE();
1108 if (RT_FAILURE(rc))
1109 SUPFreeModule(*ppvImageBase);
1110 }
1111#endif /* VBOX_WITH_IDT_PATCHING */
1112
1113 return rc;
1114}
1115
1116
1117#ifdef VBOX_WITH_IDT_PATCHING
1118/**
1119 * Generates the code for calling the interrupt gate.
1120 *
1121 * @returns VBox status code.
1122 * g_pfnCallVMMR0 is changed on success.
1123 * @param u8Interrupt The interrupt number.
1124 */
1125static int suplibGenerateCallVMMR0(uint8_t u8Interrupt)
1126{
1127 /*
1128 * Allocate memory.
1129 */
1130 uint8_t *pb = (uint8_t *)RTMemExecAlloc(256);
1131 AssertReturn(pb, VERR_NO_MEMORY);
1132 memset(pb, 0xcc, 256);
1133 Assert(!g_pfnCallVMMR0);
1134 g_pfnCallVMMR0 = *(PFNCALLVMMR0*)&pb;
1135
1136 /*
1137 * Generate the code.
1138 */
1139#ifdef RT_ARCH_AMD64
1140 /*
1141 * reg params:
1142 * <GCC> <MSC> <argument>
1143 * rdi rcx pVMR0
1144 * esi edx uOperation
1145 * rdx r8 pvArg
1146 *
1147 * eax eax [g_u32Gookie]
1148 */
1149 *pb++ = 0xb8; /* mov eax, <g_u32Cookie> */
1150 *(uint32_t *)pb = g_u32Cookie;
1151 pb += sizeof(uint32_t);
1152
1153 *pb++ = 0xcd; /* int <u8Interrupt> */
1154 *pb++ = u8Interrupt;
1155
1156 *pb++ = 0xc3; /* ret */
1157
1158#else
1159 /*
1160 * x86 stack:
1161 * 0 saved esi
1162 * 0 4 ret
1163 * 4 8 pVM
1164 * 8 c uOperation
1165 * c 10 pvArg
1166 */
1167 *pb++ = 0x56; /* push esi */
1168
1169 *pb++ = 0x8b; /* mov eax, [pVM] */
1170 *pb++ = 0x44;
1171 *pb++ = 0x24;
1172 *pb++ = 0x08; /* esp+08h */
1173
1174 *pb++ = 0x8b; /* mov edx, [uOperation] */
1175 *pb++ = 0x54;
1176 *pb++ = 0x24;
1177 *pb++ = 0x0c; /* esp+0ch */
1178
1179 *pb++ = 0x8b; /* mov ecx, [pvArg] */
1180 *pb++ = 0x4c;
1181 *pb++ = 0x24;
1182 *pb++ = 0x10; /* esp+10h */
1183
1184 *pb++ = 0xbe; /* mov esi, <g_u32Cookie> */
1185 *(uint32_t *)pb = g_u32Cookie;
1186 pb += sizeof(uint32_t);
1187
1188 *pb++ = 0xcd; /* int <u8Interrupt> */
1189 *pb++ = u8Interrupt;
1190
1191 *pb++ = 0x5e; /* pop esi */
1192
1193 *pb++ = 0xc3; /* ret */
1194#endif
1195
1196 return VINF_SUCCESS;
1197}
1198
1199
1200/**
1201 * Installs the IDTE patch.
1202 *
1203 * @return VBox status code.
1204 */
1205static int supInstallIDTE(void)
1206{
1207 /* already installed? */
1208 if (g_u8Interrupt != 3 || g_u32FakeMode)
1209 return VINF_SUCCESS;
1210
1211 int rc = VINF_SUCCESS;
1212 const unsigned cCpus = RTSystemProcessorGetCount();
1213 if (cCpus <= 1)
1214 {
1215 /* UNI */
1216 SUPIDTINSTALL Req;
1217 Req.Hdr.u32Cookie = g_u32Cookie;
1218 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1219 Req.Hdr.cbIn = SUP_IOCTL_IDT_INSTALL_SIZE_IN;
1220 Req.Hdr.cbOut = SUP_IOCTL_IDT_INSTALL_SIZE_OUT;
1221 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1222 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1223 rc = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &Req, SUP_IOCTL_IDT_INSTALL_SIZE);
1224 if (RT_SUCCESS(rc))
1225 rc = Req.Hdr.rc;
1226 if (RT_SUCCESS(rc))
1227 {
1228 g_u8Interrupt = Req.u.Out.u8Idt;
1229 rc = suplibGenerateCallVMMR0(Req.u.Out.u8Idt);
1230 }
1231 }
1232 else
1233 {
1234 /* SMP */
1235 uint64_t u64AffMaskSaved = RTThreadGetAffinity();
1236 uint64_t u64AffMaskPatched = RTSystemProcessorGetActiveMask() & u64AffMaskSaved;
1237 unsigned cCpusPatched = 0;
1238
1239 for (int i = 0; i < 64; i++)
1240 {
1241 /* Skip absent and inactive processors. */
1242 uint64_t u64Mask = 1ULL << i;
1243 if (!(u64Mask & u64AffMaskPatched))
1244 continue;
1245
1246 /* Change CPU */
1247 int rc2 = RTThreadSetAffinity(u64Mask);
1248 if (RT_FAILURE(rc2))
1249 {
1250 u64AffMaskPatched &= ~u64Mask;
1251 LogRel(("SUPLoadVMM: Failed to set affinity to cpu no. %d, rc=%Vrc.\n", i, rc2));
1252 continue;
1253 }
1254
1255 /* Patch the CPU. */
1256 SUPIDTINSTALL Req;
1257 Req.Hdr.u32Cookie = g_u32Cookie;
1258 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1259 Req.Hdr.cbIn = SUP_IOCTL_IDT_INSTALL_SIZE_IN;
1260 Req.Hdr.cbOut = SUP_IOCTL_IDT_INSTALL_SIZE_OUT;
1261 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1262 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1263 rc2 = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &Req, SUP_IOCTL_IDT_INSTALL_SIZE);
1264 if (RT_SUCCESS(rc2))
1265 rc2 = Req.Hdr.rc;
1266 if (RT_SUCCESS(rc2))
1267 {
1268 if (!cCpusPatched)
1269 {
1270 g_u8Interrupt = Req.u.Out.u8Idt;
1271 rc2 = suplibGenerateCallVMMR0(Req.u.Out.u8Idt);
1272 if (RT_FAILURE(rc2))
1273 {
1274 LogRel(("suplibGenerateCallVMMR0 failed with rc=%Vrc.\n", i, rc2));
1275 rc = rc2;
1276 }
1277 }
1278 else
1279 Assert(g_u8Interrupt == Req.u.Out.u8Idt);
1280 cCpusPatched++;
1281 }
1282 else
1283 {
1284
1285 LogRel(("SUPLoadVMM: Failed to patch cpu no. %d, rc=%Vrc.\n", i, rc2));
1286 if (RT_SUCCESS(rc))
1287 rc = rc2;
1288 }
1289 }
1290
1291 /* Fail if no CPUs was patched! */
1292 if (RT_SUCCESS(rc) && cCpusPatched <= 0)
1293 rc = VERR_GENERAL_FAILURE;
1294 /* Ignore failures if a CPU was patched. */
1295 else if (RT_FAILURE(rc) && cCpusPatched > 0)
1296 rc = VINF_SUCCESS;
1297
1298 /* Set/restore the thread affinity. */
1299 if (RT_SUCCESS(rc))
1300 {
1301 rc = RTThreadSetAffinity(u64AffMaskPatched);
1302 AssertRC(rc);
1303 }
1304 else
1305 {
1306 int rc2 = RTThreadSetAffinity(u64AffMaskSaved);
1307 AssertRC(rc2);
1308 }
1309 }
1310 return rc;
1311}
1312#endif /* VBOX_WITH_IDT_PATCHING */
1313
1314
1315/**
1316 * Resolve an external symbol during RTLdrGetBits().
1317 *
1318 * @returns VBox status code.
1319 * @param hLdrMod The loader module handle.
1320 * @param pszModule Module name.
1321 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
1322 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
1323 * @param pValue Where to store the symbol value (address).
1324 * @param pvUser User argument.
1325 */
1326static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
1327 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
1328{
1329 AssertPtr(pValue);
1330 AssertPtr(pvUser);
1331
1332 /*
1333 * Only SUPR0 and VMMR0.r0
1334 */
1335 if ( pszModule
1336 && *pszModule
1337 && strcmp(pszModule, "SUPR0.dll")
1338 && strcmp(pszModule, "VMMR0.r0"))
1339 {
1340 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
1341 return VERR_SYMBOL_NOT_FOUND;
1342 }
1343
1344 /*
1345 * No ordinals.
1346 */
1347 if (pszSymbol < (const char*)0x10000)
1348 {
1349 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
1350 return VERR_SYMBOL_NOT_FOUND;
1351 }
1352
1353 /*
1354 * Lookup symbol.
1355 */
1356 /* skip the 64-bit ELF import prefix first. */
1357 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
1358 pszSymbol += sizeof("SUPR0$") - 1;
1359
1360 /*
1361 * Check the VMMR0.r0 module if loaded.
1362 */
1363 /** @todo call the SUPLoadModule caller.... */
1364 /** @todo proper reference counting and such. */
1365 if (g_pvVMMR0 != NIL_RTR0PTR)
1366 {
1367 void *pvValue;
1368 if (!SUPGetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
1369 {
1370 *pValue = (uintptr_t)pvValue;
1371 return VINF_SUCCESS;
1372 }
1373 }
1374
1375 /* iterate the function table. */
1376 int c = g_pFunctions->u.Out.cFunctions;
1377 PSUPFUNC pFunc = &g_pFunctions->u.Out.aFunctions[0];
1378 while (c-- > 0)
1379 {
1380 if (!strcmp(pFunc->szName, pszSymbol))
1381 {
1382 *pValue = (uintptr_t)pFunc->pfn;
1383 return VINF_SUCCESS;
1384 }
1385 pFunc++;
1386 }
1387
1388 /*
1389 * The GIP.
1390 */
1391 /** @todo R0 mapping? */
1392 if ( pszSymbol
1393 && g_pSUPGlobalInfoPage
1394 && g_pSUPGlobalInfoPageR0
1395 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
1396 {
1397 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
1398 return VINF_SUCCESS;
1399 }
1400
1401 /*
1402 * Despair.
1403 */
1404 c = g_pFunctions->u.Out.cFunctions;
1405 pFunc = &g_pFunctions->u.Out.aFunctions[0];
1406 while (c-- > 0)
1407 {
1408 AssertMsg2("%d: %s\n", g_pFunctions->u.Out.cFunctions - c, pFunc->szName);
1409 pFunc++;
1410 }
1411
1412 AssertMsg2("%s is importing %s which we couldn't find\n", pvUser, pszSymbol);
1413 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
1414 if (g_u32FakeMode)
1415 {
1416 *pValue = 0xdeadbeef;
1417 return VINF_SUCCESS;
1418 }
1419 return VERR_SYMBOL_NOT_FOUND;
1420}
1421
1422
1423/** Argument package for supLoadModuleCalcSizeCB. */
1424typedef struct SUPLDRCALCSIZEARGS
1425{
1426 size_t cbStrings;
1427 uint32_t cSymbols;
1428 size_t cbImage;
1429} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
1430
1431/**
1432 * Callback used to calculate the image size.
1433 * @return VINF_SUCCESS
1434 */
1435static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1436{
1437 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
1438 if ( pszSymbol != NULL
1439 && *pszSymbol
1440 && Value <= pArgs->cbImage)
1441 {
1442 pArgs->cSymbols++;
1443 pArgs->cbStrings += strlen(pszSymbol) + 1;
1444 }
1445 return VINF_SUCCESS;
1446}
1447
1448
1449/** Argument package for supLoadModuleCreateTabsCB. */
1450typedef struct SUPLDRCREATETABSARGS
1451{
1452 size_t cbImage;
1453 PSUPLDRSYM pSym;
1454 char *pszBase;
1455 char *psz;
1456} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1457
1458/**
1459 * Callback used to calculate the image size.
1460 * @return VINF_SUCCESS
1461 */
1462static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1463{
1464 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1465 if ( pszSymbol != NULL
1466 && *pszSymbol
1467 && Value <= pArgs->cbImage)
1468 {
1469 pArgs->pSym->offSymbol = (uint32_t)Value;
1470 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1471 pArgs->pSym++;
1472
1473 size_t cbCopy = strlen(pszSymbol) + 1;
1474 memcpy(pArgs->psz, pszSymbol, cbCopy);
1475 pArgs->psz += cbCopy;
1476 }
1477 return VINF_SUCCESS;
1478}
1479
1480
1481/**
1482 * Worker for SUPLoadModule().
1483 *
1484 * @returns VBox status code.
1485 * @param pszFilename Name of the VMMR0 image file
1486 */
1487static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1488{
1489 /*
1490 * Validate input.
1491 */
1492 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1493 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1494 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1495 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
1496
1497 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1498 *ppvImageBase = NULL;
1499
1500 /*
1501 * Open image file and figure its size.
1502 */
1503 RTLDRMOD hLdrMod;
1504 int rc = RTLdrOpen(pszFilename, &hLdrMod);
1505 if (!RT_SUCCESS(rc))
1506 return rc;
1507
1508 SUPLDRCALCSIZEARGS CalcArgs;
1509 CalcArgs.cbStrings = 0;
1510 CalcArgs.cSymbols = 0;
1511 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1512 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1513 if (RT_SUCCESS(rc))
1514 {
1515 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1516 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1517 const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1518
1519 /*
1520 * Open the R0 image.
1521 */
1522 SUPLDROPEN OpenReq;
1523 OpenReq.Hdr.u32Cookie = g_u32Cookie;
1524 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
1525 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
1526 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
1527 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1528 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
1529 OpenReq.u.In.cbImage = cbImage;
1530 strcpy(OpenReq.u.In.szName, pszModule);
1531 if (!g_u32FakeMode)
1532 {
1533 rc = suplibOsIOCtl(SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
1534 if (RT_SUCCESS(rc))
1535 rc = OpenReq.Hdr.rc;
1536 }
1537 else
1538 {
1539 OpenReq.u.Out.fNeedsLoading = true;
1540 OpenReq.u.Out.pvImageBase = 0xef423420;
1541 }
1542 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
1543 if ( RT_SUCCESS(rc)
1544 && OpenReq.u.Out.fNeedsLoading)
1545 {
1546 /*
1547 * We need to load it.
1548 * Allocate memory for the image bits.
1549 */
1550 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1551 if (pLoadReq)
1552 {
1553 /*
1554 * Get the image bits.
1555 */
1556 rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
1557 supLoadModuleResolveImport, (void *)pszModule);
1558
1559 if (RT_SUCCESS(rc))
1560 {
1561 /*
1562 * Get the entry points.
1563 */
1564 RTUINTPTR VMMR0EntryInt = 0;
1565 RTUINTPTR VMMR0EntryFast = 0;
1566 RTUINTPTR VMMR0EntryEx = 0;
1567 RTUINTPTR ModuleInit = 0;
1568 RTUINTPTR ModuleTerm = 0;
1569 if (fIsVMMR0)
1570 {
1571 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
1572 if (RT_SUCCESS(rc))
1573 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
1574 if (RT_SUCCESS(rc))
1575 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
1576 }
1577 if (RT_SUCCESS(rc))
1578 {
1579 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
1580 if (RT_FAILURE(rc2))
1581 ModuleInit = 0;
1582
1583 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
1584 if (RT_FAILURE(rc2))
1585 ModuleTerm = 0;
1586 }
1587 if (RT_SUCCESS(rc))
1588 {
1589 /*
1590 * Create the symbol and string tables.
1591 */
1592 SUPLDRCREATETABSARGS CreateArgs;
1593 CreateArgs.cbImage = CalcArgs.cbImage;
1594 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab];
1595 CreateArgs.pszBase = (char *)&pLoadReq->u.In.achImage[offStrTab];
1596 CreateArgs.psz = CreateArgs.pszBase;
1597 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1598 if (RT_SUCCESS(rc))
1599 {
1600 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1601 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab]) <= CalcArgs.cSymbols);
1602
1603 /*
1604 * Upload the image.
1605 */
1606 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
1607 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1608 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImage);
1609 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
1610 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
1611 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
1612
1613 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
1614 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
1615 if (fIsVMMR0)
1616 {
1617 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
1618 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
1619 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryInt = (RTR0PTR)VMMR0EntryInt;
1620 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
1621 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
1622 }
1623 else
1624 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
1625 pLoadReq->u.In.offStrTab = offStrTab;
1626 pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
1627 AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
1628 pLoadReq->u.In.offSymbols = offSymTab;
1629 pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
1630 pLoadReq->u.In.cbImage = cbImage;
1631 pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
1632 if (!g_u32FakeMode)
1633 {
1634 rc = suplibOsIOCtl(SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1635 if (RT_SUCCESS(rc))
1636 rc = pLoadReq->Hdr.rc;
1637 }
1638 else
1639 rc = VINF_SUCCESS;
1640 if ( RT_SUCCESS(rc)
1641 || rc == VERR_ALREADY_LOADED /* A competing process. */
1642 )
1643 {
1644 LogRel(("SUP: Loaded %s (%s) at %#p - ModuleInit at %RTptr and ModuleTerm at %RTptr\n", pszModule, pszFilename,
1645 OpenReq.u.Out.pvImageBase, ModuleInit, ModuleTerm));
1646 if (fIsVMMR0)
1647 {
1648 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1649 LogRel(("SUP: VMMR0EntryEx located at %RTptr, VMMR0EntryFast at %RTptr and VMMR0EntryInt at %RTptr\n",
1650 VMMR0EntryEx, VMMR0EntryFast, VMMR0EntryInt));
1651 }
1652#ifdef RT_OS_WINDOWS
1653 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1654#endif
1655
1656 RTMemTmpFree(pLoadReq);
1657 RTLdrClose(hLdrMod);
1658 return VINF_SUCCESS;
1659 }
1660 }
1661 }
1662 }
1663 RTMemTmpFree(pLoadReq);
1664 }
1665 else
1666 {
1667 AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImage)));
1668 rc = VERR_NO_TMP_MEMORY;
1669 }
1670 }
1671 else if (RT_SUCCESS(rc))
1672 {
1673 if (fIsVMMR0)
1674 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1675 LogRel(("SUP: Opened %s (%s) at %#p.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase));
1676#ifdef RT_OS_WINDOWS
1677 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1678#endif
1679 }
1680 }
1681 RTLdrClose(hLdrMod);
1682 return rc;
1683}
1684
1685
1686SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
1687{
1688 /* fake */
1689 if (RT_UNLIKELY(g_u32FakeMode))
1690 {
1691#ifdef VBOX_WITH_IDT_PATCHING
1692 g_u8Interrupt = 3;
1693 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1694 g_pfnCallVMMR0 = NULL;
1695#endif
1696 g_pvVMMR0 = NIL_RTR0PTR;
1697 return VINF_SUCCESS;
1698 }
1699
1700#ifdef VBOX_WITH_IDT_PATCHING
1701 /*
1702 * There is one special module. When this is freed we'll
1703 * free the IDT entry that goes with it.
1704 *
1705 * Note that we don't keep count of VMMR0.r0 loads here, so the
1706 * first unload will free it.
1707 */
1708 if ( (RTR0PTR)pvImageBase == g_pvVMMR0
1709 && g_u8Interrupt != 3)
1710 {
1711 SUPIDTREMOVE Req;
1712 Req.Hdr.u32Cookie = g_u32Cookie;
1713 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1714 Req.Hdr.cbIn = SUP_IOCTL_IDT_REMOVE_SIZE_IN;
1715 Req.Hdr.cbOut = SUP_IOCTL_IDT_REMOVE_SIZE_OUT;
1716 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1717 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1718 int rc = suplibOsIOCtl(SUP_IOCTL_IDT_REMOVE, &Req, SUP_IOCTL_IDT_REMOVE_SIZE);
1719 if (RT_SUCCESS(rc))
1720 rc = Req.Hdr.rc;
1721 AssertRC(rc);
1722 g_u8Interrupt = 3;
1723 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1724 g_pfnCallVMMR0 = NULL;
1725 }
1726#endif /* VBOX_WITH_IDT_PATCHING */
1727
1728 /*
1729 * Free the requested module.
1730 */
1731 SUPLDRFREE Req;
1732 Req.Hdr.u32Cookie = g_u32Cookie;
1733 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1734 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
1735 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
1736 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1737 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1738 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1739 int rc = suplibOsIOCtl(SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
1740 if (RT_SUCCESS(rc))
1741 rc = Req.Hdr.rc;
1742 if ( RT_SUCCESS(rc)
1743 && (RTR0PTR)pvImageBase == g_pvVMMR0)
1744 g_pvVMMR0 = NIL_RTR0PTR;
1745 return rc;
1746}
1747
1748
1749SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1750{
1751 *ppvValue = NULL;
1752
1753 /* fake */
1754 if (RT_UNLIKELY(g_u32FakeMode))
1755 {
1756 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
1757 return VINF_SUCCESS;
1758 }
1759
1760 /*
1761 * Do ioctl.
1762 */
1763 SUPLDRGETSYMBOL Req;
1764 Req.Hdr.u32Cookie = g_u32Cookie;
1765 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1766 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
1767 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
1768 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1769 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1770 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1771 size_t cchSymbol = strlen(pszSymbol);
1772 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
1773 return VERR_SYMBOL_NOT_FOUND;
1774 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
1775 int rc = suplibOsIOCtl(SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
1776 if (RT_SUCCESS(rc))
1777 rc = Req.Hdr.rc;
1778 if (RT_SUCCESS(rc))
1779 *ppvValue = (void *)Req.u.Out.pvSymbol;
1780 return rc;
1781}
1782
1783
1784SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
1785{
1786 void *pvImageBase;
1787 return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1788}
1789
1790
1791SUPR3DECL(int) SUPUnloadVMM(void)
1792{
1793 return SUPFreeModule((void*)g_pvVMMR0);
1794}
1795
1796
1797SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
1798{
1799 if (g_pSUPGlobalInfoPage)
1800 {
1801 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1802 return VINF_SUCCESS;
1803 }
1804 *pHCPhys = NIL_RTHCPHYS;
1805 return VERR_WRONG_ORDER;
1806}
1807
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