[22053] | 1 | /* $Id: SUPDrv-darwin.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[22077] | 3 | * VirtualBox Support Driver - Darwin Specific Code.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[10662] | 41 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
[57252] | 42 | #include "../../../Runtime/r0drv/darwin/the-darwin-kernel.h"
|
---|
[4817] | 43 |
|
---|
[10263] | 44 | #include "../SUPDrvInternal.h"
|
---|
[378] | 45 | #include <VBox/version.h>
|
---|
[75052] | 46 | #include <iprt/assert.h>
|
---|
[29255] | 47 | #include <iprt/asm.h>
|
---|
[43379] | 48 | #include <iprt/asm-amd64-x86.h>
|
---|
[75052] | 49 | #include <iprt/ctype.h>
|
---|
| 50 | #include <iprt/dbg.h>
|
---|
[206] | 51 | #include <iprt/initterm.h>
|
---|
[75052] | 52 | #include <iprt/file.h>
|
---|
| 53 | #include <iprt/ldr.h>
|
---|
| 54 | #include <iprt/mem.h>
|
---|
| 55 | #include <iprt/power.h>
|
---|
| 56 | #include <iprt/process.h>
|
---|
[1] | 57 | #include <iprt/spinlock.h>
|
---|
| 58 | #include <iprt/semaphore.h>
|
---|
[55318] | 59 | #include <iprt/x86.h>
|
---|
[75052] | 60 | #include <iprt/crypto/applecodesign.h>
|
---|
| 61 | #include <iprt/crypto/store.h>
|
---|
| 62 | #include <iprt/crypto/pkcs7.h>
|
---|
| 63 | #include <iprt/crypto/x509.h>
|
---|
[14901] | 64 | #include <VBox/err.h>
|
---|
[10662] | 65 | #include <VBox/log.h>
|
---|
[1] | 66 |
|
---|
| 67 | #include <mach/kmod.h>
|
---|
| 68 | #include <miscfs/devfs/devfs.h>
|
---|
| 69 | #include <sys/conf.h>
|
---|
| 70 | #include <sys/errno.h>
|
---|
| 71 | #include <sys/ioccom.h>
|
---|
| 72 | #include <sys/malloc.h>
|
---|
| 73 | #include <sys/proc.h>
|
---|
[16352] | 74 | #include <sys/kauth.h>
|
---|
[378] | 75 | #include <IOKit/IOService.h>
|
---|
[23491] | 76 | #include <IOKit/IOUserClient.h>
|
---|
[15041] | 77 | #include <IOKit/pwr_mgt/RootDomain.h>
|
---|
[41067] | 78 | #include <IOKit/IODeviceTreeSupport.h>
|
---|
[85172] | 79 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
|
---|
| 80 | # include <IOKit/usb/IOUSBHIDDriver.h>
|
---|
| 81 | #endif
|
---|
[49965] | 82 | #include <IOKit/bluetooth/IOBluetoothHIDDriver.h>
|
---|
| 83 | #include <IOKit/bluetooth/IOBluetoothHIDDriverTypes.h>
|
---|
[1] | 84 |
|
---|
[15130] | 85 | #ifdef VBOX_WITH_HOST_VMX
|
---|
[43308] | 86 | # include <libkern/version.h>
|
---|
[20374] | 87 | RT_C_DECLS_BEGIN
|
---|
[15130] | 88 | # include <i386/vmx.h>
|
---|
[20374] | 89 | RT_C_DECLS_END
|
---|
[15130] | 90 | #endif
|
---|
[1] | 91 |
|
---|
[15130] | 92 |
|
---|
[57358] | 93 | /*********************************************************************************************************************************
|
---|
| 94 | * Defined Constants And Macros *
|
---|
| 95 | *********************************************************************************************************************************/
|
---|
[1] | 96 |
|
---|
[44173] | 97 | /** The system device node name. */
|
---|
| 98 | #define DEVICE_NAME_SYS "vboxdrv"
|
---|
| 99 | /** The user device node name. */
|
---|
| 100 | #define DEVICE_NAME_USR "vboxdrvu"
|
---|
[1] | 101 |
|
---|
| 102 |
|
---|
[75710] | 103 | /** @name For debugging/whatever, now permanent.
|
---|
| 104 | * @{ */
|
---|
| 105 | #define VBOX_PROC_SELFNAME_LEN 31
|
---|
| 106 | #define VBOX_RETRIEVE_CUR_PROC_NAME(a_Name) char a_Name[VBOX_PROC_SELFNAME_LEN + 1]; \
|
---|
| 107 | proc_selfname(a_Name, VBOX_PROC_SELFNAME_LEN)
|
---|
| 108 | /** @} */
|
---|
[1] | 109 |
|
---|
[75710] | 110 |
|
---|
[57358] | 111 | /*********************************************************************************************************************************
|
---|
| 112 | * Internal Functions *
|
---|
| 113 | *********************************************************************************************************************************/
|
---|
[20374] | 114 | RT_C_DECLS_BEGIN
|
---|
[4817] | 115 | static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData);
|
---|
| 116 | static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData);
|
---|
[75053] | 117 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75052] | 118 | static int supdrvDarwinInitCertStores(PSUPDRVDEVEXT pDevExt);
|
---|
| 119 | static void supdrvDarwinDestroyCertStores(PSUPDRVDEVEXT pDevExt);
|
---|
| 120 | #endif
|
---|
[1] | 121 |
|
---|
[4817] | 122 | static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
|
---|
| 123 | static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
|
---|
| 124 | static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
|
---|
[82818] | 125 | #ifndef VBOX_WITHOUT_EFLAGS_AC_SET_IN_VBOXDRV
|
---|
[55318] | 126 | static int VBoxDrvDarwinIOCtlSMAP(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
|
---|
[82818] | 127 | #endif
|
---|
[4817] | 128 | static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
|
---|
[1] | 129 |
|
---|
[4817] | 130 | static int VBoxDrvDarwinErr2DarwinErr(int rc);
|
---|
[15041] | 131 |
|
---|
| 132 | static IOReturn VBoxDrvDarwinSleepHandler(void *pvTarget, void *pvRefCon, UInt32 uMessageType, IOService *pProvider, void *pvMessageArgument, vm_size_t argSize);
|
---|
[20374] | 133 | RT_C_DECLS_END
|
---|
[1] | 134 |
|
---|
[56356] | 135 | static int vboxdrvDarwinResolveSymbols(void);
|
---|
[55318] | 136 | static bool vboxdrvDarwinCpuHasSMAP(void);
|
---|
[1] | 137 |
|
---|
[43379] | 138 |
|
---|
[57358] | 139 | /*********************************************************************************************************************************
|
---|
| 140 | * Structures and Typedefs *
|
---|
| 141 | *********************************************************************************************************************************/
|
---|
[378] | 142 | /**
|
---|
| 143 | * The service class.
|
---|
| 144 | * This is just a formality really.
|
---|
| 145 | */
|
---|
| 146 | class org_virtualbox_SupDrv : public IOService
|
---|
| 147 | {
|
---|
[12653] | 148 | OSDeclareDefaultStructors(org_virtualbox_SupDrv);
|
---|
[378] | 149 |
|
---|
| 150 | public:
|
---|
| 151 | virtual bool init(OSDictionary *pDictionary = 0);
|
---|
| 152 | virtual void free(void);
|
---|
| 153 | virtual bool start(IOService *pProvider);
|
---|
| 154 | virtual void stop(IOService *pProvider);
|
---|
| 155 | virtual IOService *probe(IOService *pProvider, SInt32 *pi32Score);
|
---|
[10391] | 156 | virtual bool terminate(IOOptionBits fOptions);
|
---|
[58275] | 157 |
|
---|
| 158 | RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT();
|
---|
[58277] | 159 |
|
---|
| 160 | private:
|
---|
| 161 | /** Guard against the parent class growing and us using outdated headers. */
|
---|
| 162 | uint8_t m_abSafetyPadding[256];
|
---|
[378] | 163 | };
|
---|
| 164 |
|
---|
[12653] | 165 | OSDefineMetaClassAndStructors(org_virtualbox_SupDrv, IOService);
|
---|
[378] | 166 |
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * An attempt at getting that clientDied() notification.
|
---|
| 170 | * I don't think it'll work as I cannot figure out where/what creates the correct
|
---|
| 171 | * port right.
|
---|
| 172 | */
|
---|
| 173 | class org_virtualbox_SupDrvClient : public IOUserClient
|
---|
| 174 | {
|
---|
[12653] | 175 | OSDeclareDefaultStructors(org_virtualbox_SupDrvClient);
|
---|
[378] | 176 |
|
---|
| 177 | private:
|
---|
[58277] | 178 | /** Guard against the parent class growing and us using outdated headers. */
|
---|
| 179 | uint8_t m_abSafetyPadding[256];
|
---|
| 180 |
|
---|
[378] | 181 | PSUPDRVSESSION m_pSession; /**< The session. */
|
---|
| 182 | task_t m_Task; /**< The client task. */
|
---|
| 183 | org_virtualbox_SupDrv *m_pProvider; /**< The service provider. */
|
---|
| 184 |
|
---|
| 185 | public:
|
---|
| 186 | virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
|
---|
| 187 | virtual bool start(IOService *pProvider);
|
---|
[10836] | 188 | static void sessionClose(RTPROCESS Process);
|
---|
[378] | 189 | virtual IOReturn clientClose(void);
|
---|
| 190 | virtual IOReturn clientDied(void);
|
---|
| 191 | virtual bool terminate(IOOptionBits fOptions = 0);
|
---|
| 192 | virtual bool finalize(IOOptionBits fOptions);
|
---|
[387] | 193 | virtual void stop(IOService *pProvider);
|
---|
[58275] | 194 |
|
---|
| 195 | RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT();
|
---|
[378] | 196 | };
|
---|
| 197 |
|
---|
[12653] | 198 | OSDefineMetaClassAndStructors(org_virtualbox_SupDrvClient, IOUserClient);
|
---|
[378] | 199 |
|
---|
| 200 |
|
---|
| 201 |
|
---|
[57358] | 202 | /*********************************************************************************************************************************
|
---|
| 203 | * Global Variables *
|
---|
| 204 | *********************************************************************************************************************************/
|
---|
[1] | 205 | /**
|
---|
| 206 | * Declare the module stuff.
|
---|
| 207 | */
|
---|
[20374] | 208 | RT_C_DECLS_BEGIN
|
---|
[378] | 209 | extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
|
---|
| 210 | extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
|
---|
[1] | 211 |
|
---|
[378] | 212 | KMOD_EXPLICIT_DECL(VBoxDrv, VBOX_VERSION_STRING, _start, _stop)
|
---|
[85124] | 213 | DECL_HIDDEN_DATA(kmod_start_func_t *) _realmain = VBoxDrvDarwinStart;
|
---|
| 214 | DECL_HIDDEN_DATA(kmod_stop_func_t *) _antimain = VBoxDrvDarwinStop;
|
---|
| 215 | DECL_HIDDEN_DATA(int) _kext_apple_cc = __APPLE_CC__;
|
---|
[20374] | 216 | RT_C_DECLS_END
|
---|
[378] | 217 |
|
---|
| 218 |
|
---|
[1] | 219 | /**
|
---|
| 220 | * Device extention & session data association structure.
|
---|
| 221 | */
|
---|
| 222 | static SUPDRVDEVEXT g_DevExt;
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | * The character device switch table for the driver.
|
---|
| 226 | */
|
---|
| 227 | static struct cdevsw g_DevCW =
|
---|
| 228 | {
|
---|
[378] | 229 | /** @todo g++ doesn't like this syntax - it worked with gcc before renaming to .cpp. */
|
---|
[4817] | 230 | /*.d_open = */VBoxDrvDarwinOpen,
|
---|
| 231 | /*.d_close = */VBoxDrvDarwinClose,
|
---|
[378] | 232 | /*.d_read = */eno_rdwrt,
|
---|
| 233 | /*.d_write = */eno_rdwrt,
|
---|
[4817] | 234 | /*.d_ioctl = */VBoxDrvDarwinIOCtl,
|
---|
[378] | 235 | /*.d_stop = */eno_stop,
|
---|
| 236 | /*.d_reset = */eno_reset,
|
---|
| 237 | /*.d_ttys = */NULL,
|
---|
| 238 | /*.d_select= */eno_select,
|
---|
| 239 | /*.d_mmap = */eno_mmap,
|
---|
| 240 | /*.d_strategy = */eno_strat,
|
---|
[63510] | 241 | /*.d_getc = */(void *)(uintptr_t)&enodev, //eno_getc,
|
---|
| 242 | /*.d_putc = */(void *)(uintptr_t)&enodev, //eno_putc,
|
---|
[378] | 243 | /*.d_type = */0
|
---|
[1] | 244 | };
|
---|
| 245 |
|
---|
| 246 | /** Major device number. */
|
---|
[75710] | 247 | static int g_iMajorDeviceNo = -1;
|
---|
[44173] | 248 | /** Registered devfs device handle for the system device. */
|
---|
[75710] | 249 | static void *g_hDevFsDeviceSys = NULL;
|
---|
[44173] | 250 | /** Registered devfs device handle for the user device. */
|
---|
[75710] | 251 | static void *g_hDevFsDeviceUsr = NULL;
|
---|
[1] | 252 |
|
---|
| 253 | /** Spinlock protecting g_apSessionHashTab. */
|
---|
[75710] | 254 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
[1] | 255 | /** Hash table */
|
---|
[75710] | 256 | static PSUPDRVSESSION g_apSessionHashTab[19];
|
---|
[1] | 257 | /** Calculates the index into g_apSessionHashTab.*/
|
---|
[75710] | 258 | #define SESSION_HASH(pid) ((pid) % RT_ELEMENTS(g_apSessionHashTab))
|
---|
[10391] | 259 | /** The number of open sessions. */
|
---|
[75710] | 260 | static int32_t volatile g_cSessions = 0;
|
---|
[15041] | 261 | /** The notifier handle for the sleep callback handler. */
|
---|
[75710] | 262 | static IONotifier *g_pSleepNotifier = NULL;
|
---|
[1] | 263 |
|
---|
[43379] | 264 | /** Pointer to vmx_suspend(). */
|
---|
| 265 | static PFNRT g_pfnVmxSuspend = NULL;
|
---|
| 266 | /** Pointer to vmx_resume(). */
|
---|
| 267 | static PFNRT g_pfnVmxResume = NULL;
|
---|
| 268 | /** Pointer to vmx_use_count. */
|
---|
| 269 | static int volatile *g_pVmxUseCount = NULL;
|
---|
[1] | 270 |
|
---|
[49767] | 271 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
| 272 | /** Pointer to rdmsr_carefully if found. Returns 0 on success. */
|
---|
| 273 | static int (*g_pfnRdMsrCarefully)(uint32_t uMsr, uint32_t *puLow, uint32_t *puHigh) = NULL;
|
---|
| 274 | /** Pointer to rdmsr64_carefully if found. Returns 0 on success. */
|
---|
| 275 | static int (*g_pfnRdMsr64Carefully)(uint32_t uMsr, uint64_t *uValue) = NULL;
|
---|
| 276 | /** Pointer to wrmsr[64]_carefully if found. Returns 0 on success. */
|
---|
| 277 | static int (*g_pfnWrMsr64Carefully)(uint32_t uMsr, uint64_t uValue) = NULL;
|
---|
| 278 | #endif
|
---|
[10389] | 279 |
|
---|
[82818] | 280 | /** SUPKERNELFEATURES_XXX */
|
---|
| 281 | static uint32_t g_fKernelFeatures = 0;
|
---|
[49767] | 282 |
|
---|
[1] | 283 | /**
|
---|
| 284 | * Start the kernel module.
|
---|
| 285 | */
|
---|
[4817] | 286 | static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData)
|
---|
[1] | 287 | {
|
---|
[63510] | 288 | RT_NOREF(pKModInfo, pvData);
|
---|
[10662] | 289 | #ifdef DEBUG
|
---|
| 290 | printf("VBoxDrvDarwinStart\n");
|
---|
| 291 | #endif
|
---|
[1] | 292 |
|
---|
| 293 | /*
|
---|
[206] | 294 | * Initialize IPRT.
|
---|
[1] | 295 | */
|
---|
[75710] | 296 | int rc = RTR0Init(0);
|
---|
[1] | 297 | if (RT_SUCCESS(rc))
|
---|
| 298 | {
|
---|
| 299 | /*
|
---|
[206] | 300 | * Initialize the device extension.
|
---|
[1] | 301 | */
|
---|
[25484] | 302 | rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
|
---|
[1] | 303 | if (RT_SUCCESS(rc))
|
---|
| 304 | {
|
---|
[75053] | 305 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75052] | 306 | supdrvDarwinInitCertStores(&g_DevExt);
|
---|
| 307 | #endif
|
---|
| 308 |
|
---|
[1] | 309 | /*
|
---|
[206] | 310 | * Initialize the session hash table.
|
---|
[1] | 311 | */
|
---|
[206] | 312 | memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab)); /* paranoia */
|
---|
[40806] | 313 | rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDrvDarwin");
|
---|
[206] | 314 | if (RT_SUCCESS(rc))
|
---|
[1] | 315 | {
|
---|
[55318] | 316 | if (vboxdrvDarwinCpuHasSMAP())
|
---|
[55319] | 317 | {
|
---|
[82818] | 318 | g_fKernelFeatures |= SUPKERNELFEATURES_SMAP;
|
---|
| 319 | #ifndef VBOX_WITHOUT_EFLAGS_AC_SET_IN_VBOXDRV
|
---|
[55319] | 320 | LogRel(("disabling SMAP for VBoxDrvDarwinIOCtl\n"));
|
---|
[55318] | 321 | g_DevCW.d_ioctl = VBoxDrvDarwinIOCtlSMAP;
|
---|
[82818] | 322 | #endif
|
---|
[55319] | 323 | }
|
---|
| 324 |
|
---|
[206] | 325 | /*
|
---|
[56356] | 326 | * Resolve some extra kernel symbols.
|
---|
[206] | 327 | */
|
---|
[56356] | 328 | rc = vboxdrvDarwinResolveSymbols();
|
---|
| 329 | if (RT_SUCCESS(rc))
|
---|
[1] | 330 | {
|
---|
[56356] | 331 |
|
---|
| 332 | /*
|
---|
| 333 | * Registering ourselves as a character device.
|
---|
| 334 | */
|
---|
| 335 | g_iMajorDeviceNo = cdevsw_add(-1, &g_DevCW);
|
---|
| 336 | if (g_iMajorDeviceNo >= 0)
|
---|
| 337 | {
|
---|
[11725] | 338 | #ifdef VBOX_WITH_HARDENING
|
---|
[56356] | 339 | g_hDevFsDeviceSys = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
|
---|
| 340 | UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME_SYS);
|
---|
[11725] | 341 | #else
|
---|
[56356] | 342 | g_hDevFsDeviceSys = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
|
---|
| 343 | UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_SYS);
|
---|
[11725] | 344 | #endif
|
---|
[56356] | 345 | if (g_hDevFsDeviceSys)
|
---|
[44173] | 346 | {
|
---|
[56356] | 347 | g_hDevFsDeviceUsr = devfs_make_node(makedev(g_iMajorDeviceNo, 1), DEVFS_CHAR,
|
---|
| 348 | UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_USR);
|
---|
| 349 | if (g_hDevFsDeviceUsr)
|
---|
| 350 | {
|
---|
| 351 | LogRel(("VBoxDrv: version " VBOX_VERSION_STRING " r%d; IOCtl version %#x; IDC version %#x; dev major=%d\n",
|
---|
| 352 | VBOX_SVN_REV, SUPDRV_IOC_VERSION, SUPDRV_IDC_VERSION, g_iMajorDeviceNo));
|
---|
[15041] | 353 |
|
---|
[56356] | 354 | /* Register a sleep/wakeup notification callback */
|
---|
| 355 | g_pSleepNotifier = registerPrioritySleepWakeInterest(&VBoxDrvDarwinSleepHandler, &g_DevExt, NULL);
|
---|
| 356 | if (g_pSleepNotifier == NULL)
|
---|
| 357 | LogRel(("VBoxDrv: register for sleep/wakeup events failed\n"));
|
---|
[15041] | 358 |
|
---|
[56356] | 359 | return KMOD_RETURN_SUCCESS;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | LogRel(("VBoxDrv: devfs_make_node(makedev(%d,1),,,,%s) failed\n", g_iMajorDeviceNo, DEVICE_NAME_USR));
|
---|
| 363 | devfs_remove(g_hDevFsDeviceSys);
|
---|
| 364 | g_hDevFsDeviceSys = NULL;
|
---|
[44173] | 365 | }
|
---|
[56356] | 366 | else
|
---|
| 367 | LogRel(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n", g_iMajorDeviceNo, DEVICE_NAME_SYS));
|
---|
[44173] | 368 |
|
---|
[56356] | 369 | cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
|
---|
| 370 | g_iMajorDeviceNo = -1;
|
---|
[206] | 371 | }
|
---|
[44173] | 372 | else
|
---|
[56356] | 373 | LogRel(("VBoxDrv: cdevsw_add failed (%d)\n", g_iMajorDeviceNo));
|
---|
[1] | 374 | }
|
---|
[75053] | 375 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75052] | 376 | supdrvDarwinDestroyCertStores(&g_DevExt);
|
---|
| 377 | #endif
|
---|
[206] | 378 | RTSpinlockDestroy(g_Spinlock);
|
---|
| 379 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
[1] | 380 | }
|
---|
| 381 | else
|
---|
[10662] | 382 | LogRel(("VBoxDrv: RTSpinlockCreate failed (rc=%d)\n", rc));
|
---|
[206] | 383 | supdrvDeleteDevExt(&g_DevExt);
|
---|
[1] | 384 | }
|
---|
| 385 | else
|
---|
[10662] | 386 | printf("VBoxDrv: failed to initialize device extension (rc=%d)\n", rc);
|
---|
[24183] | 387 | RTR0TermForced();
|
---|
[1] | 388 | }
|
---|
| 389 | else
|
---|
[10662] | 390 | printf("VBoxDrv: failed to initialize IPRT (rc=%d)\n", rc);
|
---|
[1] | 391 |
|
---|
| 392 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
| 393 | return KMOD_RETURN_FAILURE;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | /**
|
---|
[56357] | 398 | * Resolves kernel symbols we need and some we just would like to have.
|
---|
[43379] | 399 | */
|
---|
[56356] | 400 | static int vboxdrvDarwinResolveSymbols(void)
|
---|
[43379] | 401 | {
|
---|
| 402 | RTDBGKRNLINFO hKrnlInfo;
|
---|
| 403 | int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0);
|
---|
| 404 | if (RT_SUCCESS(rc))
|
---|
| 405 | {
|
---|
[56356] | 406 | /*
|
---|
[56375] | 407 | * The VMX stuff - required with raw-mode (in theory for 64-bit on
|
---|
| 408 | * 32-bit too, but we never did that on darwin).
|
---|
[56356] | 409 | */
|
---|
[43379] | 410 | int rc1 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_resume", (void **)&g_pfnVmxResume);
|
---|
| 411 | int rc2 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_suspend", (void **)&g_pfnVmxSuspend);
|
---|
| 412 | int rc3 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_use_count", (void **)&g_pVmxUseCount);
|
---|
| 413 | if (RT_SUCCESS(rc1) && RT_SUCCESS(rc2) && RT_SUCCESS(rc3))
|
---|
| 414 | {
|
---|
| 415 | LogRel(("VBoxDrv: vmx_resume=%p vmx_suspend=%p vmx_use_count=%p (%d) cr4=%#x\n",
|
---|
| 416 | g_pfnVmxResume, g_pfnVmxSuspend, g_pVmxUseCount, *g_pVmxUseCount, ASMGetCR4() ));
|
---|
| 417 | }
|
---|
| 418 | else
|
---|
| 419 | {
|
---|
| 420 | LogRel(("VBoxDrv: failed to resolve vmx stuff: vmx_resume=%Rrc vmx_suspend=%Rrc vmx_use_count=%Rrc", rc1, rc2, rc3));
|
---|
| 421 | g_pfnVmxResume = NULL;
|
---|
| 422 | g_pfnVmxSuspend = NULL;
|
---|
| 423 | g_pVmxUseCount = NULL;
|
---|
[56375] | 424 | #ifdef VBOX_WITH_RAW_MODE
|
---|
[56356] | 425 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
[56375] | 426 | #endif
|
---|
[43379] | 427 | }
|
---|
| 428 |
|
---|
[56356] | 429 | if (RT_SUCCESS(rc))
|
---|
| 430 | {
|
---|
[49767] | 431 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
[56356] | 432 | /*
|
---|
| 433 | * MSR prober stuff - optional!
|
---|
| 434 | */
|
---|
[63510] | 435 | rc2 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "rdmsr_carefully", (void **)&g_pfnRdMsrCarefully);
|
---|
[56356] | 436 | if (RT_FAILURE(rc2))
|
---|
| 437 | g_pfnRdMsrCarefully = NULL;
|
---|
| 438 | rc2 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "rdmsr64_carefully", (void **)&g_pfnRdMsr64Carefully);
|
---|
| 439 | if (RT_FAILURE(rc2))
|
---|
| 440 | g_pfnRdMsr64Carefully = NULL;
|
---|
[49767] | 441 | # ifdef RT_ARCH_AMD64 /* Missing 64 in name, so if implemented on 32-bit it could have different signature. */
|
---|
[56356] | 442 | rc2 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "wrmsr_carefully", (void **)&g_pfnWrMsr64Carefully);
|
---|
| 443 | if (RT_FAILURE(rc2))
|
---|
[49767] | 444 | # endif
|
---|
[56356] | 445 | g_pfnWrMsr64Carefully = NULL;
|
---|
[49917] | 446 |
|
---|
[56356] | 447 | LogRel(("VBoxDrv: g_pfnRdMsrCarefully=%p g_pfnRdMsr64Carefully=%p g_pfnWrMsr64Carefully=%p\n",
|
---|
| 448 | g_pfnRdMsrCarefully, g_pfnRdMsr64Carefully, g_pfnWrMsr64Carefully));
|
---|
[49917] | 449 |
|
---|
[49767] | 450 | #endif /* SUPDRV_WITH_MSR_PROBER */
|
---|
[56356] | 451 | }
|
---|
[49767] | 452 |
|
---|
[43379] | 453 | RTR0DbgKrnlInfoRelease(hKrnlInfo);
|
---|
| 454 | }
|
---|
| 455 | else
|
---|
| 456 | LogRel(("VBoxDrv: Failed to open kernel symbols, rc=%Rrc\n", rc));
|
---|
[56356] | 457 | return rc;
|
---|
[43379] | 458 | }
|
---|
| 459 |
|
---|
| 460 |
|
---|
[75053] | 461 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75052] | 462 |
|
---|
[43379] | 463 | /**
|
---|
[75052] | 464 | * Initalizes the certificate stores (code signing) in the device extension.
|
---|
| 465 | */
|
---|
| 466 | static int supdrvDarwinInitCertStores(PSUPDRVDEVEXT pDevExt)
|
---|
| 467 | {
|
---|
| 468 | pDevExt->hAdditionalStore = NIL_RTCRSTORE;
|
---|
| 469 |
|
---|
| 470 | pDevExt->hRootStore = NIL_RTCRSTORE;
|
---|
| 471 | int rc = RTCrStoreCreateInMem(&pDevExt->hRootStore, g_cSUPTrustedTAs + 1);
|
---|
| 472 | if (RT_SUCCESS(rc))
|
---|
| 473 | {
|
---|
| 474 | for (uint32_t i = 0; i < g_cSUPTrustedTAs; i++)
|
---|
| 475 | {
|
---|
| 476 | int rc2 = RTCrStoreCertAddEncoded(pDevExt->hRootStore, RTCRCERTCTX_F_ENC_TAF_DER,
|
---|
| 477 | g_aSUPTrustedTAs[i].pch, g_aSUPTrustedTAs[i].cb, NULL);
|
---|
| 478 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 479 | {
|
---|
| 480 | printf("VBoxDrv: Error loading g_aSUPTrustedTAs[%u]: %d\n", i, rc);
|
---|
| 481 | rc = rc2;
|
---|
| 482 | }
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /* We implicitly trust the build certificate. */
|
---|
| 486 | int rc2 = RTCrStoreCertAddEncoded(pDevExt->hRootStore, RTCRCERTCTX_F_ENC_X509_DER,
|
---|
| 487 | g_abSUPBuildCert, g_cbSUPBuildCert, NULL);
|
---|
| 488 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 489 | {
|
---|
| 490 | printf("VBoxDrv: Error loading g_cbSUPBuildCert: %d\n", rc);
|
---|
| 491 | rc = rc2;
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 | return rc;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 | /**
|
---|
| 499 | * Releases the certificate stores in the device extension.
|
---|
| 500 | */
|
---|
| 501 | static void supdrvDarwinDestroyCertStores(PSUPDRVDEVEXT pDevExt)
|
---|
| 502 | {
|
---|
| 503 | if (pDevExt->hRootStore != NIL_RTCRSTORE)
|
---|
| 504 | {
|
---|
| 505 | uint32_t cRefs = RTCrStoreRelease(pDevExt->hRootStore);
|
---|
| 506 | Assert(cRefs == 0); RT_NOREF(cRefs);
|
---|
| 507 | pDevExt->hRootStore = NIL_RTCRSTORE;
|
---|
| 508 | }
|
---|
| 509 | if (pDevExt->hAdditionalStore != NIL_RTCRSTORE)
|
---|
| 510 | {
|
---|
| 511 | uint32_t cRefs = RTCrStoreRelease(pDevExt->hAdditionalStore);
|
---|
| 512 | Assert(cRefs == 0); RT_NOREF(cRefs);
|
---|
| 513 | pDevExt->hAdditionalStore = NIL_RTCRSTORE;
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[75053] | 517 | #endif /* VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION */
|
---|
[75052] | 518 |
|
---|
| 519 | /**
|
---|
[1] | 520 | * Stop the kernel module.
|
---|
| 521 | */
|
---|
[4817] | 522 | static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData)
|
---|
[1] | 523 | {
|
---|
[63510] | 524 | RT_NOREF(pKModInfo, pvData);
|
---|
[1] | 525 | int rc;
|
---|
[10662] | 526 | LogFlow(("VBoxDrvDarwinStop\n"));
|
---|
[1] | 527 |
|
---|
| 528 | /** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
|
---|
| 529 | * unloading if we're busy. Investigate and implement this! */
|
---|
| 530 |
|
---|
| 531 | /*
|
---|
| 532 | * Undo the work done during start (in reverse order).
|
---|
| 533 | */
|
---|
[15041] | 534 | if (g_pSleepNotifier)
|
---|
| 535 | {
|
---|
| 536 | g_pSleepNotifier->remove();
|
---|
| 537 | g_pSleepNotifier = NULL;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[44173] | 540 | devfs_remove(g_hDevFsDeviceUsr);
|
---|
| 541 | g_hDevFsDeviceUsr = NULL;
|
---|
[1] | 542 |
|
---|
[44173] | 543 | devfs_remove(g_hDevFsDeviceSys);
|
---|
| 544 | g_hDevFsDeviceSys = NULL;
|
---|
| 545 |
|
---|
[1] | 546 | rc = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
|
---|
| 547 | Assert(rc == g_iMajorDeviceNo);
|
---|
| 548 | g_iMajorDeviceNo = -1;
|
---|
| 549 |
|
---|
[4800] | 550 | supdrvDeleteDevExt(&g_DevExt);
|
---|
[1] | 551 |
|
---|
| 552 | rc = RTSpinlockDestroy(g_Spinlock);
|
---|
| 553 | AssertRC(rc);
|
---|
| 554 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
| 555 |
|
---|
[75053] | 556 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75052] | 557 | supdrvDarwinDestroyCertStores(&g_DevExt);
|
---|
| 558 | #endif
|
---|
| 559 |
|
---|
[24183] | 560 | RTR0TermForced();
|
---|
[206] | 561 |
|
---|
[1] | 562 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
[10662] | 563 | #ifdef DEBUG
|
---|
| 564 | printf("VBoxDrvDarwinStop - done\n");
|
---|
| 565 | #endif
|
---|
[1] | 566 | return KMOD_RETURN_SUCCESS;
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | /**
|
---|
| 571 | * Device open. Called on open /dev/vboxdrv
|
---|
| 572 | *
|
---|
[44173] | 573 | * @param Dev The device number.
|
---|
| 574 | * @param fFlags ???.
|
---|
| 575 | * @param fDevType ???.
|
---|
| 576 | * @param pProcess The process issuing this request.
|
---|
[1] | 577 | */
|
---|
[4817] | 578 | static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
|
---|
[1] | 579 | {
|
---|
[63510] | 580 | RT_NOREF(fFlags, fDevType);
|
---|
[2142] | 581 | #ifdef DEBUG_DARWIN_GIP
|
---|
[2141] | 582 | char szName[128];
|
---|
| 583 | szName[0] = '\0';
|
---|
| 584 | proc_name(proc_pid(pProcess), szName, sizeof(szName));
|
---|
[10662] | 585 | Log(("VBoxDrvDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
|
---|
[2141] | 586 | #endif
|
---|
[1] | 587 |
|
---|
| 588 | /*
|
---|
[44173] | 589 | * Only two minor devices numbers are allowed.
|
---|
| 590 | */
|
---|
| 591 | if (minor(Dev) != 0 && minor(Dev) != 1)
|
---|
| 592 | return EACCES;
|
---|
| 593 |
|
---|
| 594 | /*
|
---|
[51100] | 595 | * The process issuing the request must be the current process.
|
---|
| 596 | */
|
---|
| 597 | RTPROCESS Process = RTProcSelf();
|
---|
[57220] | 598 | if ((int)Process != proc_pid(pProcess))
|
---|
[51100] | 599 | return EIO;
|
---|
| 600 |
|
---|
| 601 | /*
|
---|
[10836] | 602 | * Find the session created by org_virtualbox_SupDrvClient, fail
|
---|
| 603 | * if no such session, and mark it as opened. We set the uid & gid
|
---|
| 604 | * here too, since that is more straight forward at this point.
|
---|
[1] | 605 | */
|
---|
[44173] | 606 | const bool fUnrestricted = minor(Dev) == 0;
|
---|
[10877] | 607 | int rc = VINF_SUCCESS;
|
---|
| 608 | PSUPDRVSESSION pSession = NULL;
|
---|
[16352] | 609 | kauth_cred_t pCred = kauth_cred_proc_ref(pProcess);
|
---|
[10836] | 610 | if (pCred)
|
---|
[1] | 611 | {
|
---|
[37093] | 612 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
|
---|
[37116] | 613 | RTUID Uid = kauth_cred_getruid(pCred);
|
---|
| 614 | RTGID Gid = kauth_cred_getrgid(pCred);
|
---|
[37093] | 615 | #else
|
---|
[37112] | 616 | RTUID Uid = pCred->cr_ruid;
|
---|
[11725] | 617 | RTGID Gid = pCred->cr_rgid;
|
---|
[37093] | 618 | #endif
|
---|
[10836] | 619 | unsigned iHash = SESSION_HASH(Process);
|
---|
[40806] | 620 | RTSpinlockAcquire(g_Spinlock);
|
---|
[10836] | 621 |
|
---|
| 622 | pSession = g_apSessionHashTab[iHash];
|
---|
[44173] | 623 | while (pSession && pSession->Process != Process)
|
---|
| 624 | pSession = pSession->pNextHash;
|
---|
[10836] | 625 | if (pSession)
|
---|
| 626 | {
|
---|
| 627 | if (!pSession->fOpened)
|
---|
| 628 | {
|
---|
| 629 | pSession->fOpened = true;
|
---|
[44173] | 630 | pSession->fUnrestricted = fUnrestricted;
|
---|
[10836] | 631 | pSession->Uid = Uid;
|
---|
| 632 | pSession->Gid = Gid;
|
---|
| 633 | }
|
---|
| 634 | else
|
---|
| 635 | rc = VERR_ALREADY_LOADED;
|
---|
| 636 | }
|
---|
| 637 | else
|
---|
| 638 | rc = VERR_GENERAL_FAILURE;
|
---|
[1] | 639 |
|
---|
[52618] | 640 | RTSpinlockRelease(g_Spinlock);
|
---|
[16356] | 641 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
|
---|
[16352] | 642 | kauth_cred_unref(&pCred);
|
---|
[16356] | 643 | #else /* 10.4 */
|
---|
[33540] | 644 | /* The 10.4u SDK headers and 10.4.11 kernel source have inconsistent definitions
|
---|
[16356] | 645 | of kauth_cred_unref(), so use the other (now deprecated) API for releasing it. */
|
---|
| 646 | kauth_cred_rele(pCred);
|
---|
| 647 | #endif /* 10.4 */
|
---|
[1] | 648 | }
|
---|
[10836] | 649 | else
|
---|
[25466] | 650 | rc = VERR_INVALID_PARAMETER;
|
---|
[1] | 651 |
|
---|
[2142] | 652 | #ifdef DEBUG_DARWIN_GIP
|
---|
[4817] | 653 | OSDBGPRINT(("VBoxDrvDarwinOpen: pid=%d '%s' pSession=%p rc=%d\n", proc_pid(pProcess), szName, pSession, rc));
|
---|
[2142] | 654 | #else
|
---|
[10662] | 655 | Log(("VBoxDrvDarwinOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
|
---|
[2141] | 656 | #endif
|
---|
[4817] | 657 | return VBoxDrvDarwinErr2DarwinErr(rc);
|
---|
[1] | 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
| 661 | /**
|
---|
| 662 | * Close device.
|
---|
| 663 | */
|
---|
[4817] | 664 | static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
|
---|
[1] | 665 | {
|
---|
[63510] | 666 | RT_NOREF(Dev, fFlags, fDevType, pProcess);
|
---|
[10836] | 667 | Log(("VBoxDrvDarwinClose: pid=%d\n", (int)RTProcSelf()));
|
---|
| 668 | Assert(proc_pid(pProcess) == (int)RTProcSelf());
|
---|
[1] | 669 |
|
---|
| 670 | /*
|
---|
[10836] | 671 | * Hand the session closing to org_virtualbox_SupDrvClient.
|
---|
[1] | 672 | */
|
---|
[10836] | 673 | org_virtualbox_SupDrvClient::sessionClose(RTProcSelf());
|
---|
[1] | 674 | return 0;
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 |
|
---|
| 678 | /**
|
---|
| 679 | * Device I/O Control entry point.
|
---|
| 680 | *
|
---|
| 681 | * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
|
---|
| 682 | * @param Dev The device number (major+minor).
|
---|
| 683 | * @param iCmd The IOCtl command.
|
---|
| 684 | * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
|
---|
| 685 | * @param fFlags Flag saying we're a character device (like we didn't know already).
|
---|
| 686 | * @param pProcess The process issuing this request.
|
---|
| 687 | */
|
---|
[4817] | 688 | static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
|
---|
[1] | 689 | {
|
---|
[63510] | 690 | RT_NOREF(fFlags);
|
---|
[44173] | 691 | const bool fUnrestricted = minor(Dev) == 0;
|
---|
[1] | 692 | const RTPROCESS Process = proc_pid(pProcess);
|
---|
| 693 | const unsigned iHash = SESSION_HASH(Process);
|
---|
| 694 | PSUPDRVSESSION pSession;
|
---|
| 695 |
|
---|
[57229] | 696 | #ifdef VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV
|
---|
[1] | 697 | /*
|
---|
[57229] | 698 | * Refuse all I/O control calls if we've ever detected EFLAGS.AC being cleared.
|
---|
| 699 | *
|
---|
| 700 | * This isn't a problem, as there is absolutely nothing in the kernel context that
|
---|
| 701 | * depend on user context triggering cleanups. That would be pretty wild, right?
|
---|
| 702 | */
|
---|
| 703 | if (RT_UNLIKELY(g_DevExt.cBadContextCalls > 0))
|
---|
| 704 | {
|
---|
| 705 | SUPR0Printf("VBoxDrvDarwinIOCtl: EFLAGS.AC=0 detected %u times, refusing all I/O controls!\n", g_DevExt.cBadContextCalls);
|
---|
| 706 | return EDEVERR;
|
---|
| 707 | }
|
---|
| 708 | #endif
|
---|
| 709 |
|
---|
| 710 | /*
|
---|
[1] | 711 | * Find the session.
|
---|
| 712 | */
|
---|
[40806] | 713 | RTSpinlockAcquire(g_Spinlock);
|
---|
[51100] | 714 |
|
---|
[1] | 715 | pSession = g_apSessionHashTab[iHash];
|
---|
[51100] | 716 | while (pSession && (pSession->Process != Process || pSession->fUnrestricted != fUnrestricted || !pSession->fOpened))
|
---|
[44173] | 717 | pSession = pSession->pNextHash;
|
---|
[47537] | 718 |
|
---|
| 719 | if (RT_LIKELY(pSession))
|
---|
| 720 | supdrvSessionRetain(pSession);
|
---|
[51100] | 721 |
|
---|
[52618] | 722 | RTSpinlockRelease(g_Spinlock);
|
---|
[51100] | 723 | if (RT_UNLIKELY(!pSession))
|
---|
[1] | 724 | {
|
---|
[16352] | 725 | OSDBGPRINT(("VBoxDrvDarwinIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#lx\n",
|
---|
[2141] | 726 | (int)Process, iCmd));
|
---|
[1] | 727 | return EINVAL;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | /*
|
---|
| 731 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
| 732 | * the session and iCmd, and only returns a VBox status code.
|
---|
| 733 | */
|
---|
[47537] | 734 | int rc;
|
---|
[71198] | 735 | AssertCompile((SUP_IOCTL_FAST_DO_FIRST & 0xff) == (SUP_IOCTL_FLAG | 64));
|
---|
| 736 | if ( (uintptr_t)(iCmd - SUP_IOCTL_FAST_DO_FIRST) < (uintptr_t)32
|
---|
[44173] | 737 | && fUnrestricted)
|
---|
[71198] | 738 | rc = supdrvIOCtlFast(iCmd - SUP_IOCTL_FAST_DO_FIRST, *(uint32_t *)pData, &g_DevExt, pSession);
|
---|
[47537] | 739 | else
|
---|
| 740 | rc = VBoxDrvDarwinIOCtlSlow(pSession, iCmd, pData, pProcess);
|
---|
| 741 |
|
---|
| 742 | supdrvSessionRelease(pSession);
|
---|
| 743 | return rc;
|
---|
[1] | 744 | }
|
---|
| 745 |
|
---|
| 746 |
|
---|
[82818] | 747 | #ifndef VBOX_WITHOUT_EFLAGS_AC_SET_IN_VBOXDRV
|
---|
[1] | 748 | /**
|
---|
[55318] | 749 | * Alternative Device I/O Control entry point on hosts with SMAP support.
|
---|
| 750 | *
|
---|
| 751 | * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
|
---|
| 752 | * @param Dev The device number (major+minor).
|
---|
| 753 | * @param iCmd The IOCtl command.
|
---|
| 754 | * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
|
---|
| 755 | * @param fFlags Flag saying we're a character device (like we didn't know already).
|
---|
| 756 | * @param pProcess The process issuing this request.
|
---|
| 757 | */
|
---|
| 758 | static int VBoxDrvDarwinIOCtlSMAP(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
|
---|
| 759 | {
|
---|
| 760 | /*
|
---|
| 761 | * Allow VBox R0 code to touch R3 memory. Setting the AC bit disables the
|
---|
| 762 | * SMAP check.
|
---|
| 763 | */
|
---|
[57220] | 764 | RTCCUINTREG fSavedEfl = ASMAddFlags(X86_EFL_AC);
|
---|
| 765 |
|
---|
[55318] | 766 | int rc = VBoxDrvDarwinIOCtl(Dev, iCmd, pData, fFlags, pProcess);
|
---|
[57220] | 767 |
|
---|
[82818] | 768 | # if defined(VBOX_STRICT) || defined(VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV)
|
---|
[57254] | 769 | /*
|
---|
| 770 | * Before we restore AC and the rest of EFLAGS, check if the IOCtl handler code
|
---|
| 771 | * accidentially modified it or some other important flag.
|
---|
| 772 | */
|
---|
| 773 | if (RT_UNLIKELY( (ASMGetFlags() & (X86_EFL_AC | X86_EFL_IF | X86_EFL_DF | X86_EFL_IOPL))
|
---|
| 774 | != ((fSavedEfl & (X86_EFL_AC | X86_EFL_IF | X86_EFL_DF | X86_EFL_IOPL)) | X86_EFL_AC) ))
|
---|
[57244] | 775 | {
|
---|
[57254] | 776 | char szTmp[48];
|
---|
| 777 | RTStrPrintf(szTmp, sizeof(szTmp), "iCmd=%#x: %#x->%#x!", iCmd, (uint32_t)fSavedEfl, (uint32_t)ASMGetFlags());
|
---|
[57244] | 778 | supdrvBadContext(&g_DevExt, "SUPDrv-darwin.cpp", __LINE__, szTmp);
|
---|
| 779 | }
|
---|
[82818] | 780 | # endif
|
---|
[57254] | 781 |
|
---|
[57220] | 782 | ASMSetFlags(fSavedEfl);
|
---|
[55318] | 783 | return rc;
|
---|
| 784 | }
|
---|
[82818] | 785 | #endif /* VBOX_WITHOUT_EFLAGS_AC_SET_IN_VBOXDRV */
|
---|
[55318] | 786 |
|
---|
| 787 |
|
---|
| 788 | /**
|
---|
[4817] | 789 | * Worker for VBoxDrvDarwinIOCtl that takes the slow IOCtl functions.
|
---|
[1] | 790 | *
|
---|
| 791 | * @returns Darwin errno.
|
---|
| 792 | *
|
---|
| 793 | * @param pSession The session.
|
---|
| 794 | * @param iCmd The IOCtl command.
|
---|
| 795 | * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
|
---|
| 796 | * @param pProcess The calling process.
|
---|
| 797 | */
|
---|
[4817] | 798 | static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
|
---|
[1] | 799 | {
|
---|
[63510] | 800 | RT_NOREF(pProcess);
|
---|
[10662] | 801 | LogFlow(("VBoxDrvDarwinIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
|
---|
[1] | 802 |
|
---|
[4817] | 803 |
|
---|
[1] | 804 | /*
|
---|
[4817] | 805 | * Buffered or unbuffered?
|
---|
[1] | 806 | */
|
---|
[4818] | 807 | PSUPREQHDR pHdr;
|
---|
| 808 | user_addr_t pUser = 0;
|
---|
[4817] | 809 | void *pvPageBuf = NULL;
|
---|
[4818] | 810 | uint32_t cbReq = IOCPARM_LEN(iCmd);
|
---|
| 811 | if ((IOC_DIRMASK & iCmd) == IOC_INOUT)
|
---|
[1] | 812 | {
|
---|
[4817] | 813 | pHdr = (PSUPREQHDR)pData;
|
---|
[4818] | 814 | if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
|
---|
[4817] | 815 | {
|
---|
[4818] | 816 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: cbReq=%#x < %#x; iCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), iCmd));
|
---|
[4817] | 817 | return EINVAL;
|
---|
| 818 | }
|
---|
| 819 | if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
| 820 | {
|
---|
[9498] | 821 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", pHdr->fFlags, iCmd));
|
---|
[4817] | 822 | return EINVAL;
|
---|
| 823 | }
|
---|
[4818] | 824 | if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
|
---|
| 825 | || pHdr->cbIn < sizeof(*pHdr)
|
---|
| 826 | || pHdr->cbOut < sizeof(*pHdr)))
|
---|
[4817] | 827 | {
|
---|
[4818] | 828 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x) != %#x; iCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, iCmd));
|
---|
[4817] | 829 | return EINVAL;
|
---|
| 830 | }
|
---|
[1] | 831 | }
|
---|
[4818] | 832 | else if ((IOC_DIRMASK & iCmd) == IOC_VOID && !cbReq)
|
---|
[1] | 833 | {
|
---|
[9498] | 834 | /*
|
---|
[4817] | 835 | * Get the header and figure out how much we're gonna have to read.
|
---|
[9498] | 836 | */
|
---|
[57374] | 837 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[4817] | 838 | SUPREQHDR Hdr;
|
---|
[4818] | 839 | pUser = (user_addr_t)*(void **)pData;
|
---|
| 840 | int rc = copyin(pUser, &Hdr, sizeof(Hdr));
|
---|
[4817] | 841 | if (RT_UNLIKELY(rc))
|
---|
[1] | 842 | {
|
---|
[4818] | 843 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,Hdr,) -> %#x; iCmd=%#lx\n", (unsigned long long)pUser, rc, iCmd));
|
---|
[57374] | 844 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[4817] | 845 | return rc;
|
---|
| 846 | }
|
---|
| 847 | if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
| 848 | {
|
---|
[9498] | 849 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", Hdr.fFlags, iCmd));
|
---|
[63510] | 850 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[4817] | 851 | return EINVAL;
|
---|
| 852 | }
|
---|
[4818] | 853 | cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
|
---|
| 854 | if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
|
---|
| 855 | || Hdr.cbOut < sizeof(Hdr)
|
---|
| 856 | || cbReq > _1M*16))
|
---|
[4817] | 857 | {
|
---|
| 858 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x); iCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, iCmd));
|
---|
[63510] | 859 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[4817] | 860 | return EINVAL;
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | /*
|
---|
| 864 | * Allocate buffer and copy in the data.
|
---|
| 865 | */
|
---|
[4818] | 866 | pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
|
---|
[4817] | 867 | if (!pHdr)
|
---|
[4818] | 868 | pvPageBuf = pHdr = (PSUPREQHDR)IOMallocAligned(RT_ALIGN_Z(cbReq, PAGE_SIZE), 8);
|
---|
[4817] | 869 | if (RT_UNLIKELY(!pHdr))
|
---|
| 870 | {
|
---|
[4818] | 871 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: failed to allocate buffer of %d bytes; iCmd=%#lx\n", cbReq, iCmd));
|
---|
[57374] | 872 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 873 | return ENOMEM;
|
---|
| 874 | }
|
---|
[4818] | 875 | rc = copyin(pUser, pHdr, Hdr.cbIn);
|
---|
[4817] | 876 | if (RT_UNLIKELY(rc))
|
---|
[1] | 877 | {
|
---|
[9498] | 878 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,%p,%#x) -> %#x; iCmd=%#lx\n",
|
---|
[4818] | 879 | (unsigned long long)pUser, pHdr, Hdr.cbIn, rc, iCmd));
|
---|
[219] | 880 | if (pvPageBuf)
|
---|
[4818] | 881 | IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
|
---|
[219] | 882 | else
|
---|
[4817] | 883 | RTMemTmpFree(pHdr);
|
---|
[57374] | 884 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 885 | return rc;
|
---|
| 886 | }
|
---|
[52575] | 887 | if (Hdr.cbIn < cbReq)
|
---|
[52576] | 888 | RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbReq - Hdr.cbIn);
|
---|
[57374] | 889 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 890 | }
|
---|
[4818] | 891 | else
|
---|
| 892 | {
|
---|
[10662] | 893 | Log(("VBoxDrvDarwinIOCtlSlow: huh? cbReq=%#x iCmd=%#lx\n", cbReq, iCmd));
|
---|
[4818] | 894 | return EINVAL;
|
---|
| 895 | }
|
---|
[1] | 896 |
|
---|
| 897 | /*
|
---|
| 898 | * Process the IOCtl.
|
---|
| 899 | */
|
---|
[52575] | 900 | int rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr, cbReq);
|
---|
[4817] | 901 | if (RT_LIKELY(!rc))
|
---|
[1] | 902 | {
|
---|
[4817] | 903 | /*
|
---|
| 904 | * If not buffered, copy back the buffer before returning.
|
---|
| 905 | */
|
---|
[4818] | 906 | if (pUser)
|
---|
[1] | 907 | {
|
---|
[57374] | 908 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[4817] | 909 | uint32_t cbOut = pHdr->cbOut;
|
---|
[4818] | 910 | if (cbOut > cbReq)
|
---|
[1] | 911 | {
|
---|
[4818] | 912 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, iCmd));
|
---|
| 913 | cbOut = cbReq;
|
---|
[1] | 914 | }
|
---|
[4818] | 915 | rc = copyout(pHdr, pUser, cbOut);
|
---|
[4817] | 916 | if (RT_UNLIKELY(rc))
|
---|
[9498] | 917 | OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyout(%p,%llx,%#x) -> %d; uCmd=%#lx!\n",
|
---|
[4818] | 918 | pHdr, (unsigned long long)pUser, cbOut, rc, iCmd));
|
---|
[4817] | 919 |
|
---|
| 920 | /* cleanup */
|
---|
| 921 | if (pvPageBuf)
|
---|
[4818] | 922 | IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
|
---|
[4817] | 923 | else
|
---|
| 924 | RTMemTmpFree(pHdr);
|
---|
[57374] | 925 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 926 | }
|
---|
[4817] | 927 | }
|
---|
| 928 | else
|
---|
| 929 | {
|
---|
[9498] | 930 | /*
|
---|
[4817] | 931 | * The request failed, just clean up.
|
---|
| 932 | */
|
---|
[4818] | 933 | if (pUser)
|
---|
[1] | 934 | {
|
---|
[4817] | 935 | if (pvPageBuf)
|
---|
[57374] | 936 | {
|
---|
| 937 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[4818] | 938 | IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
|
---|
[57374] | 939 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
| 940 | }
|
---|
[4817] | 941 | else
|
---|
| 942 | RTMemTmpFree(pHdr);
|
---|
[1] | 943 | }
|
---|
[4817] | 944 |
|
---|
[10662] | 945 | Log(("VBoxDrvDarwinIOCtlSlow: pid=%d iCmd=%lx pData=%p failed, rc=%d\n", proc_pid(pProcess), iCmd, (void *)pData, rc));
|
---|
[4817] | 946 | rc = EINVAL;
|
---|
[1] | 947 | }
|
---|
| 948 |
|
---|
[10662] | 949 | Log2(("VBoxDrvDarwinIOCtlSlow: returns %d\n", rc));
|
---|
[1] | 950 | return rc;
|
---|
| 951 | }
|
---|
| 952 |
|
---|
| 953 |
|
---|
[10263] | 954 | /**
|
---|
| 955 | * The SUPDRV IDC entry point.
|
---|
| 956 | *
|
---|
| 957 | * @returns VBox status code, see supdrvIDC.
|
---|
[58340] | 958 | * @param uReq The request code.
|
---|
[10263] | 959 | * @param pReq The request.
|
---|
| 960 | */
|
---|
[45250] | 961 | DECLEXPORT(int) VBOXCALL SUPDrvDarwinIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
[10263] | 962 | {
|
---|
| 963 | PSUPDRVSESSION pSession;
|
---|
[1] | 964 |
|
---|
[10263] | 965 | /*
|
---|
| 966 | * Some quick validations.
|
---|
| 967 | */
|
---|
[90780] | 968 | if (RT_UNLIKELY(!RT_VALID_PTR(pReq)))
|
---|
[10263] | 969 | return VERR_INVALID_POINTER;
|
---|
| 970 |
|
---|
| 971 | pSession = pReq->pSession;
|
---|
| 972 | if (pSession)
|
---|
| 973 | {
|
---|
[90780] | 974 | if (RT_UNLIKELY(!RT_VALID_PTR(pSession)))
|
---|
[10263] | 975 | return VERR_INVALID_PARAMETER;
|
---|
| 976 | if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
|
---|
| 977 | return VERR_INVALID_PARAMETER;
|
---|
| 978 | }
|
---|
| 979 | else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
|
---|
| 980 | return VERR_INVALID_PARAMETER;
|
---|
| 981 |
|
---|
| 982 | /*
|
---|
| 983 | * Do the job.
|
---|
| 984 | */
|
---|
| 985 | return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 |
|
---|
[51770] | 989 | void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
|
---|
| 990 | {
|
---|
| 991 | NOREF(pDevExt);
|
---|
| 992 | NOREF(pSession);
|
---|
| 993 | }
|
---|
| 994 |
|
---|
| 995 |
|
---|
| 996 | void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
| 997 | {
|
---|
| 998 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 |
|
---|
| 1002 | void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
| 1003 | {
|
---|
| 1004 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 |
|
---|
[1] | 1008 | /**
|
---|
| 1009 | * Initializes any OS specific object creator fields.
|
---|
| 1010 | */
|
---|
| 1011 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
| 1012 | {
|
---|
| 1013 | NOREF(pObj);
|
---|
| 1014 | NOREF(pSession);
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 |
|
---|
| 1018 | /**
|
---|
| 1019 | * Checks if the session can access the object.
|
---|
| 1020 | *
|
---|
| 1021 | * @returns true if a decision has been made.
|
---|
| 1022 | * @returns false if the default access policy should be applied.
|
---|
| 1023 | *
|
---|
| 1024 | * @param pObj The object in question.
|
---|
| 1025 | * @param pSession The session wanting to access the object.
|
---|
| 1026 | * @param pszObjName The object name, can be NULL.
|
---|
| 1027 | * @param prc Where to store the result when returning true.
|
---|
| 1028 | */
|
---|
| 1029 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
| 1030 | {
|
---|
| 1031 | NOREF(pObj);
|
---|
| 1032 | NOREF(pSession);
|
---|
| 1033 | NOREF(pszObjName);
|
---|
| 1034 | NOREF(prc);
|
---|
| 1035 | return false;
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
[15130] | 1038 | /**
|
---|
| 1039 | * Callback for blah blah blah.
|
---|
| 1040 | */
|
---|
[75718] | 1041 | IOReturn VBoxDrvDarwinSleepHandler(void * /* pvTarget */, void *pvRefCon, UInt32 uMessageType,
|
---|
[75710] | 1042 | IOService *pProvider, void *pvMsgArg, vm_size_t cbMsgArg)
|
---|
[15041] | 1043 | {
|
---|
[75710] | 1044 | RT_NOREF(pProvider, pvMsgArg, cbMsgArg);
|
---|
| 1045 | LogFlow(("VBoxDrv: Got sleep/wake notice. Message type was %x\n", uMessageType));
|
---|
[15041] | 1046 |
|
---|
| 1047 | if (uMessageType == kIOMessageSystemWillSleep)
|
---|
| 1048 | RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
|
---|
| 1049 | else if (uMessageType == kIOMessageSystemHasPoweredOn)
|
---|
| 1050 | RTPowerSignalEvent(RTPOWEREVENT_RESUME);
|
---|
| 1051 |
|
---|
| 1052 | acknowledgeSleepWakeNotification(pvRefCon);
|
---|
| 1053 |
|
---|
| 1054 | return 0;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
[9498] | 1057 |
|
---|
[56558] | 1058 | #ifdef VBOX_WITH_HOST_VMX
|
---|
[14901] | 1059 | /**
|
---|
[56558] | 1060 | * For cleaning up the mess we left behind on Yosemite with 4.3.28 and earlier.
|
---|
| 1061 | *
|
---|
| 1062 | * We ASSUME VT-x is supported by the CPU.
|
---|
| 1063 | *
|
---|
| 1064 | * @param idCpu Unused.
|
---|
| 1065 | * @param pvUser1 Unused.
|
---|
| 1066 | * @param pvUser2 Unused.
|
---|
| 1067 | */
|
---|
| 1068 | static DECLCALLBACK(void) vboxdrvDarwinVmxEnableFix(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
| 1069 | {
|
---|
[63510] | 1070 | RT_NOREF(idCpu, pvUser1, pvUser2);
|
---|
[56558] | 1071 | RTCCUINTREG uCr4 = ASMGetCR4();
|
---|
| 1072 | if (!(uCr4 & X86_CR4_VMXE))
|
---|
| 1073 | {
|
---|
| 1074 | uCr4 |= X86_CR4_VMXE;
|
---|
| 1075 | ASMSetCR4(uCr4);
|
---|
| 1076 | }
|
---|
| 1077 | }
|
---|
| 1078 | #endif
|
---|
| 1079 |
|
---|
| 1080 |
|
---|
| 1081 | /**
|
---|
[43379] | 1082 | * @copydoc SUPR0EnableVTx
|
---|
[14901] | 1083 | */
|
---|
| 1084 | int VBOXCALL supdrvOSEnableVTx(bool fEnable)
|
---|
| 1085 | {
|
---|
[43308] | 1086 | #ifdef VBOX_WITH_HOST_VMX
|
---|
[14901] | 1087 | int rc;
|
---|
[43379] | 1088 | if ( version_major >= 10 /* 10 = 10.6.x = Snow Leopard */
|
---|
[56375] | 1089 | # ifdef VBOX_WITH_RAW_MODE
|
---|
[43379] | 1090 | && g_pfnVmxSuspend
|
---|
| 1091 | && g_pfnVmxResume
|
---|
[56375] | 1092 | && g_pVmxUseCount
|
---|
| 1093 | # endif
|
---|
| 1094 | )
|
---|
[14901] | 1095 | {
|
---|
[57371] | 1096 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[43308] | 1097 | if (fEnable)
|
---|
[15130] | 1098 | {
|
---|
[56558] | 1099 | /*
|
---|
| 1100 | * We screwed up on Yosemite and didn't notice that we weren't
|
---|
[56559] | 1101 | * calling host_vmxon. CR4.VMXE may therefore have been disabled
|
---|
[56558] | 1102 | * by us. So, first time around we make sure it's set so we won't
|
---|
| 1103 | * crash in the pre-4.3.28/5.0RC1 upgrade scenario.
|
---|
[56576] | 1104 | * See @bugref{7907}.
|
---|
[56558] | 1105 | */
|
---|
| 1106 | static bool volatile g_fDoneCleanup = false;
|
---|
| 1107 | if (!g_fDoneCleanup)
|
---|
| 1108 | {
|
---|
| 1109 | if (version_major == 14 /* 14 = 10.10 = yosemite */)
|
---|
| 1110 | {
|
---|
| 1111 | uint32_t fCaps;
|
---|
[63510] | 1112 | rc = supdrvQueryVTCapsInternal(&fCaps);
|
---|
[56558] | 1113 | if (RT_SUCCESS(rc))
|
---|
| 1114 | {
|
---|
| 1115 | if (fCaps & SUPVTCAPS_VT_X)
|
---|
| 1116 | rc = RTMpOnAll(vboxdrvDarwinVmxEnableFix, NULL, NULL);
|
---|
| 1117 | else
|
---|
| 1118 | rc = VERR_VMX_NO_VMX;
|
---|
| 1119 | }
|
---|
| 1120 | if (RT_FAILURE(rc))
|
---|
[57371] | 1121 | {
|
---|
| 1122 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[56558] | 1123 | return rc;
|
---|
[57371] | 1124 | }
|
---|
[56558] | 1125 | }
|
---|
| 1126 | g_fDoneCleanup = true;
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | /*
|
---|
| 1130 | * Call the kernel.
|
---|
| 1131 | */
|
---|
[56702] | 1132 | AssertLogRelMsg(!g_pVmxUseCount || *g_pVmxUseCount >= 0,
|
---|
| 1133 | ("vmx_use_count=%d (@ %p, expected it to be a positive number\n",
|
---|
| 1134 | *g_pVmxUseCount, g_pVmxUseCount));
|
---|
| 1135 |
|
---|
[43308] | 1136 | rc = host_vmxon(false /* exclusive */);
|
---|
| 1137 | if (rc == VMX_OK)
|
---|
| 1138 | rc = VINF_SUCCESS;
|
---|
| 1139 | else if (rc == VMX_UNSUPPORTED)
|
---|
| 1140 | rc = VERR_VMX_NO_VMX;
|
---|
| 1141 | else if (rc == VMX_INUSE)
|
---|
| 1142 | rc = VERR_VMX_IN_VMX_ROOT_MODE;
|
---|
| 1143 | else /* shouldn't happen, but just in case. */
|
---|
| 1144 | {
|
---|
| 1145 | LogRel(("host_vmxon returned %d\n", rc));
|
---|
| 1146 | rc = VERR_UNRESOLVED_ERROR;
|
---|
| 1147 | }
|
---|
[43379] | 1148 | LogRel(("VBoxDrv: host_vmxon -> vmx_use_count=%d rc=%Rrc\n", *g_pVmxUseCount, rc));
|
---|
[15130] | 1149 | }
|
---|
[43308] | 1150 | else
|
---|
| 1151 | {
|
---|
[56702] | 1152 | AssertLogRelMsgReturn(!g_pVmxUseCount || *g_pVmxUseCount >= 1,
|
---|
| 1153 | ("vmx_use_count=%d (@ %p, expected it to be a non-zero positive number\n",
|
---|
| 1154 | *g_pVmxUseCount, g_pVmxUseCount),
|
---|
| 1155 | VERR_WRONG_ORDER);
|
---|
[43308] | 1156 | host_vmxoff();
|
---|
| 1157 | rc = VINF_SUCCESS;
|
---|
[43379] | 1158 | LogRel(("VBoxDrv: host_vmxoff -> vmx_use_count=%d\n", *g_pVmxUseCount));
|
---|
[43308] | 1159 | }
|
---|
[57371] | 1160 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[15003] | 1161 | }
|
---|
| 1162 | else
|
---|
| 1163 | {
|
---|
[43308] | 1164 | /* In 10.5.x the host_vmxon is severely broken! Don't use it, it will
|
---|
| 1165 | frequnetly panic the host. */
|
---|
| 1166 | rc = VERR_NOT_SUPPORTED;
|
---|
[15003] | 1167 | }
|
---|
| 1168 | return rc;
|
---|
[15130] | 1169 | #else
|
---|
| 1170 | return VERR_NOT_SUPPORTED;
|
---|
| 1171 | #endif
|
---|
[15003] | 1172 | }
|
---|
| 1173 |
|
---|
| 1174 |
|
---|
[43379] | 1175 | /**
|
---|
| 1176 | * @copydoc SUPR0SuspendVTxOnCpu
|
---|
| 1177 | */
|
---|
| 1178 | bool VBOXCALL supdrvOSSuspendVTxOnCpu(void)
|
---|
| 1179 | {
|
---|
| 1180 | #ifdef VBOX_WITH_HOST_VMX
|
---|
| 1181 | /*
|
---|
| 1182 | * Consult the VMX usage counter, don't try suspend if not enabled.
|
---|
| 1183 | *
|
---|
| 1184 | * Note! The host_vmxon/off code is still race prone since, but this is
|
---|
| 1185 | * currently the best we can do without always enable VMX when
|
---|
| 1186 | * loading the driver.
|
---|
| 1187 | */
|
---|
| 1188 | if ( g_pVmxUseCount
|
---|
| 1189 | && *g_pVmxUseCount > 0)
|
---|
| 1190 | {
|
---|
[57371] | 1191 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[43379] | 1192 | g_pfnVmxSuspend();
|
---|
[57371] | 1193 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[43379] | 1194 | return true;
|
---|
| 1195 | }
|
---|
| 1196 | return false;
|
---|
| 1197 | #else
|
---|
| 1198 | return false;
|
---|
| 1199 | #endif
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
| 1202 |
|
---|
| 1203 | /**
|
---|
| 1204 | * @copydoc SUPR0ResumeVTxOnCpu
|
---|
| 1205 | */
|
---|
| 1206 | void VBOXCALL supdrvOSResumeVTxOnCpu(bool fSuspended)
|
---|
| 1207 | {
|
---|
| 1208 | #ifdef VBOX_WITH_HOST_VMX
|
---|
| 1209 | /*
|
---|
| 1210 | * Don't consult the counter here, the state knows better.
|
---|
| 1211 | * We're executing with interrupts disabled and anyone racing us with
|
---|
| 1212 | * disabling VT-x will be waiting in the rendezvous code.
|
---|
| 1213 | */
|
---|
| 1214 | if ( fSuspended
|
---|
| 1215 | && g_pfnVmxResume)
|
---|
[57371] | 1216 | {
|
---|
| 1217 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[43379] | 1218 | g_pfnVmxResume();
|
---|
[57371] | 1219 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
| 1220 | }
|
---|
[43379] | 1221 | else
|
---|
| 1222 | Assert(!fSuspended);
|
---|
| 1223 | #else
|
---|
| 1224 | Assert(!fSuspended);
|
---|
| 1225 | #endif
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 |
|
---|
[14901] | 1229 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
| 1230 | {
|
---|
[9498] | 1231 | NOREF(pDevExt);
|
---|
| 1232 | return false;
|
---|
| 1233 | }
|
---|
| 1234 |
|
---|
| 1235 |
|
---|
[54581] | 1236 | bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void)
|
---|
| 1237 | {
|
---|
| 1238 | /** @todo verify this. */
|
---|
| 1239 | return false;
|
---|
| 1240 | }
|
---|
| 1241 |
|
---|
| 1242 |
|
---|
[53396] | 1243 | bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
|
---|
| 1244 | {
|
---|
| 1245 | return false;
|
---|
| 1246 | }
|
---|
| 1247 |
|
---|
[58872] | 1248 |
|
---|
[75053] | 1249 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1250 |
|
---|
| 1251 | /**
|
---|
| 1252 | * @callback_method_impl{FNRTLDRIMPORT}
|
---|
| 1253 | */
|
---|
| 1254 | static DECLCALLBACK(int) supdrvDarwinLdrOpenImportCallback(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
|
---|
| 1255 | unsigned uSymbol, PRTLDRADDR pValue, void *pvUser)
|
---|
| 1256 | {
|
---|
| 1257 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
|
---|
| 1258 |
|
---|
| 1259 | /*
|
---|
[75186] | 1260 | * First consult the VMMR0 module if there is one fully loaded.
|
---|
| 1261 | * This is necessary as VMMR0 may overload assertion and logger symbols.
|
---|
[75049] | 1262 | */
|
---|
[75186] | 1263 | if (pDevExt->pvVMMR0)
|
---|
| 1264 | for (PSUPDRVLDRIMAGE pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
|
---|
| 1265 | if (pImage->pvImage == pDevExt->pvVMMR0)
|
---|
| 1266 | {
|
---|
| 1267 | if ( pImage->uState == SUP_IOCTL_LDR_LOAD
|
---|
| 1268 | && pImage->hLdrMod != NIL_RTLDRMOD)
|
---|
| 1269 | {
|
---|
| 1270 | int rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pvImage, (uintptr_t)pImage->pvImage,
|
---|
| 1271 | UINT32_MAX, pszSymbol, pValue);
|
---|
| 1272 | if (RT_SUCCESS(rc))
|
---|
| 1273 | return VINF_SUCCESS;
|
---|
| 1274 | }
|
---|
| 1275 | break;
|
---|
| 1276 | }
|
---|
| 1277 |
|
---|
| 1278 | /*
|
---|
| 1279 | * Then we consult the SUPDrv export table.
|
---|
| 1280 | */
|
---|
[75049] | 1281 | uintptr_t uValue = 0;
|
---|
| 1282 | int rc = supdrvLdrGetExportedSymbol(pszSymbol, &uValue);
|
---|
| 1283 | if (RT_SUCCESS(rc))
|
---|
| 1284 | {
|
---|
| 1285 | *pValue = uValue;
|
---|
| 1286 | return VINF_SUCCESS;
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
| 1289 | /*
|
---|
| 1290 | * Failed.
|
---|
| 1291 | */
|
---|
| 1292 | printf("VBoxDrv: Unable to resolve symbol '%s'.\n", pszSymbol);
|
---|
[75052] | 1293 | RT_NOREF(hLdrMod, pszModule, uSymbol);
|
---|
[75049] | 1294 | return VERR_SYMBOL_NOT_FOUND;
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 |
|
---|
| 1298 | /**
|
---|
| 1299 | * @callback_method_impl{FNRTCRPKCS7VERIFYCERTCALLBACK,
|
---|
| 1300 | * Verify that the signing certificate is sane.}
|
---|
| 1301 | */
|
---|
| 1302 | static DECLCALLBACK(int) supdrvDarwinLdrOpenVerifyCertificatCallback(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
|
---|
| 1303 | uint32_t fFlags, void *pvUser, PRTERRINFO pErrInfo)
|
---|
| 1304 | {
|
---|
| 1305 | RT_NOREF(pvUser); //PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
|
---|
[75053] | 1306 | # ifdef DEBUG_bird
|
---|
[85172] | 1307 | printf("supdrvDarwinLdrOpenVerifyCertificatCallback: pCert=%p hCertPaths=%p\n", (void *)pCert, (void *)hCertPaths);
|
---|
[75053] | 1308 | # endif
|
---|
[75049] | 1309 |
|
---|
| 1310 | # if 0
|
---|
| 1311 | /*
|
---|
| 1312 | * Test signing certificates normally doesn't have all the necessary
|
---|
| 1313 | * features required below. So, treat them as special cases.
|
---|
| 1314 | */
|
---|
| 1315 | if ( hCertPaths == NIL_RTCRX509CERTPATHS
|
---|
| 1316 | && RTCrX509Name_Compare(&pCert->TbsCertificate.Issuer, &pCert->TbsCertificate.Subject) == 0)
|
---|
| 1317 | {
|
---|
| 1318 | RTMsgInfo("Test signed.\n");
|
---|
| 1319 | return VINF_SUCCESS;
|
---|
| 1320 | }
|
---|
| 1321 | # endif
|
---|
| 1322 |
|
---|
| 1323 | /*
|
---|
| 1324 | * Standard code signing capabilites required.
|
---|
| 1325 | */
|
---|
| 1326 | int rc = RTCrPkcs7VerifyCertCallbackCodeSigning(pCert, hCertPaths, fFlags, NULL, pErrInfo);
|
---|
| 1327 | if ( RT_SUCCESS(rc)
|
---|
| 1328 | && (fFlags & RTCRPKCS7VCC_F_SIGNED_DATA))
|
---|
| 1329 | {
|
---|
| 1330 | uint32_t cDevIdApp = 0;
|
---|
| 1331 | uint32_t cDevIdKext = 0;
|
---|
[89018] | 1332 | uint32_t cDevIdMacDev = 0;
|
---|
[75049] | 1333 | for (uint32_t i = 0; i < pCert->TbsCertificate.T3.Extensions.cItems; i++)
|
---|
| 1334 | {
|
---|
| 1335 | PCRTCRX509EXTENSION pExt = pCert->TbsCertificate.T3.Extensions.papItems[i];
|
---|
| 1336 | if (RTAsn1ObjId_CompareWithString(&pExt->ExtnId, RTCR_APPLE_CS_DEVID_APPLICATION_OID) == 0)
|
---|
| 1337 | {
|
---|
| 1338 | cDevIdApp++;
|
---|
| 1339 | if (!pExt->Critical.fValue)
|
---|
| 1340 | rc = RTErrInfoSetF(pErrInfo, VERR_GENERAL_FAILURE,
|
---|
| 1341 | "Dev ID Application certificate extension is not flagged critical");
|
---|
| 1342 | }
|
---|
| 1343 | else if (RTAsn1ObjId_CompareWithString(&pExt->ExtnId, RTCR_APPLE_CS_DEVID_KEXT_OID) == 0)
|
---|
| 1344 | {
|
---|
| 1345 | cDevIdKext++;
|
---|
| 1346 | if (!pExt->Critical.fValue)
|
---|
| 1347 | rc = RTErrInfoSetF(pErrInfo, VERR_GENERAL_FAILURE,
|
---|
| 1348 | "Dev ID kext certificate extension is not flagged critical");
|
---|
| 1349 | }
|
---|
[89018] | 1350 | else if (RTAsn1ObjId_CompareWithString(&pExt->ExtnId, RTCR_APPLE_CS_DEVID_MAC_SW_DEV_OID) == 0)
|
---|
| 1351 | {
|
---|
| 1352 | cDevIdMacDev++;
|
---|
| 1353 | if (!pExt->Critical.fValue)
|
---|
| 1354 | rc = RTErrInfoSetF(pErrInfo, VERR_GENERAL_FAILURE,
|
---|
| 1355 | "Dev ID MAC SW dev certificate extension is not flagged critical");
|
---|
| 1356 | }
|
---|
[75049] | 1357 | }
|
---|
[89018] | 1358 | # ifdef VBOX_WITH_DARWIN_R0_TEST_SIGN
|
---|
| 1359 | /*
|
---|
| 1360 | * Mac application software development certs do not have the usually required extensions.
|
---|
| 1361 | */
|
---|
| 1362 | if (cDevIdMacDev)
|
---|
| 1363 | {
|
---|
| 1364 | cDevIdApp++;
|
---|
| 1365 | cDevIdKext++;
|
---|
| 1366 | }
|
---|
| 1367 | # endif
|
---|
[75049] | 1368 | if (cDevIdApp == 0)
|
---|
| 1369 | rc = RTErrInfoSetF(pErrInfo, VERR_GENERAL_FAILURE,
|
---|
| 1370 | "Certificate is missing the 'Dev ID Application' extension");
|
---|
[75052] | 1371 | if (cDevIdKext == 0)
|
---|
[75049] | 1372 | rc = RTErrInfoSetF(pErrInfo, VERR_GENERAL_FAILURE,
|
---|
| 1373 | "Certificate is missing the 'Dev ID kext' extension");
|
---|
| 1374 | }
|
---|
| 1375 |
|
---|
| 1376 | return rc;
|
---|
| 1377 | }
|
---|
| 1378 |
|
---|
| 1379 |
|
---|
| 1380 | /**
|
---|
| 1381 | * @callback_method_impl{FNRTLDRVALIDATESIGNEDDATA}
|
---|
| 1382 | */
|
---|
[86549] | 1383 | static DECLCALLBACK(int) supdrvDarwinLdrOpenVerifyCallback(RTLDRMOD hLdrMod, PCRTLDRSIGNATUREINFO pInfo,
|
---|
[75049] | 1384 | PRTERRINFO pErrInfo, void *pvUser)
|
---|
| 1385 | {
|
---|
| 1386 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
|
---|
[86549] | 1387 | RT_NOREF_PV(hLdrMod);
|
---|
[75052] | 1388 |
|
---|
[86549] | 1389 | switch (pInfo->enmType)
|
---|
[75049] | 1390 | {
|
---|
| 1391 | case RTLDRSIGNATURETYPE_PKCS7_SIGNED_DATA:
|
---|
[86549] | 1392 | if (pInfo->pvExternalData)
|
---|
[75049] | 1393 | {
|
---|
[86549] | 1394 | PCRTCRPKCS7CONTENTINFO pContentInfo = (PCRTCRPKCS7CONTENTINFO)pInfo->pvSignature;
|
---|
[75049] | 1395 | RTTIMESPEC ValidationTime;
|
---|
[75052] | 1396 | RTTimeNow(&ValidationTime);
|
---|
[75049] | 1397 |
|
---|
| 1398 | return RTCrPkcs7VerifySignedDataWithExternalData(pContentInfo,
|
---|
| 1399 | RTCRPKCS7VERIFY_SD_F_COUNTER_SIGNATURE_SIGNING_TIME_ONLY
|
---|
| 1400 | | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_SIGNING_TIME_IF_PRESENT
|
---|
| 1401 | | RTCRPKCS7VERIFY_SD_F_ALWAYS_USE_MS_TIMESTAMP_IF_PRESENT,
|
---|
| 1402 | pDevExt->hAdditionalStore, pDevExt->hRootStore, &ValidationTime,
|
---|
[75052] | 1403 | supdrvDarwinLdrOpenVerifyCertificatCallback, pDevExt,
|
---|
[86549] | 1404 | pInfo->pvExternalData, pInfo->cbExternalData, pErrInfo);
|
---|
[75049] | 1405 | }
|
---|
| 1406 | return RTErrInfoSetF(pErrInfo, VERR_NOT_SUPPORTED, "Expected external data with signature!");
|
---|
| 1407 |
|
---|
| 1408 | default:
|
---|
[86549] | 1409 | return RTErrInfoSetF(pErrInfo, VERR_NOT_SUPPORTED, "Unsupported signature type: %d", pInfo->enmType);
|
---|
[75049] | 1410 | }
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
[75053] | 1413 | #endif /* VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION */
|
---|
[75049] | 1414 |
|
---|
[58872] | 1415 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
[41067] | 1416 | {
|
---|
[75053] | 1417 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1418 | /*
|
---|
| 1419 | * Initialize our members.
|
---|
| 1420 | */
|
---|
| 1421 | pImage->hLdrMod = NIL_RTLDRMOD;
|
---|
| 1422 | pImage->hMemAlloc = NIL_RTR0MEMOBJ;
|
---|
| 1423 |
|
---|
| 1424 | /*
|
---|
| 1425 | * We have to double buffer the file to be avoid a potential race between
|
---|
| 1426 | * validation and actual image loading. This could be eliminated later by
|
---|
| 1427 | * baking the image validation into the RTLdrGetBits operation.
|
---|
| 1428 | *
|
---|
| 1429 | * Note! After calling RTLdrOpenInMemory, pvFile is owned by the loader and will be
|
---|
| 1430 | * freed via the RTFileReadAllFree callback when the loader module is closed.
|
---|
| 1431 | */
|
---|
[75052] | 1432 | void *pvFile = NULL;
|
---|
| 1433 | size_t cbFile = 0;
|
---|
[75049] | 1434 | int rc = RTFileReadAllEx(pszFilename, 0, _32M, RTFILE_RDALL_O_DENY_WRITE, &pvFile, &cbFile);
|
---|
| 1435 | if (RT_SUCCESS(rc))
|
---|
| 1436 | {
|
---|
| 1437 | PRTERRINFOSTATIC pErrInfo = (PRTERRINFOSTATIC)RTMemTmpAlloc(sizeof(RTERRINFOSTATIC));
|
---|
| 1438 | RTLDRMOD hLdrMod = NIL_RTLDRMOD;
|
---|
| 1439 | rc = RTLdrOpenInMemory(pszFilename, 0 /*fFlags*/, RTLDRARCH_HOST, cbFile,
|
---|
| 1440 | NULL /*pfnRead*/, RTFileReadAllFree, pvFile,
|
---|
| 1441 | &hLdrMod, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
|
---|
| 1442 | if (RT_SUCCESS(rc))
|
---|
| 1443 | {
|
---|
| 1444 | /*
|
---|
| 1445 | * Validate the image.
|
---|
| 1446 | */
|
---|
| 1447 | rc = RTLdrVerifySignature(hLdrMod, supdrvDarwinLdrOpenVerifyCallback, pDevExt,
|
---|
| 1448 | pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
|
---|
| 1449 | if (RT_SUCCESS(rc))
|
---|
| 1450 | {
|
---|
| 1451 | /*
|
---|
| 1452 | * Allocate memory for the object and load it into it.
|
---|
| 1453 | */
|
---|
| 1454 | size_t cbImage = RTLdrSize(hLdrMod);
|
---|
| 1455 | if (cbImage == pImage->cbImageBits)
|
---|
| 1456 | {
|
---|
| 1457 | RTR0MEMOBJ hMemAlloc;
|
---|
| 1458 | rc = RTR0MemObjAllocPage(&hMemAlloc, cbImage, true /*fExecutable*/);
|
---|
| 1459 | if (RT_SUCCESS(rc))
|
---|
| 1460 | {
|
---|
| 1461 | void *pvImageBits = RTR0MemObjAddress(hMemAlloc);
|
---|
| 1462 | rc = RTLdrGetBits(hLdrMod, pvImageBits, (uintptr_t)pvImageBits,
|
---|
| 1463 | supdrvDarwinLdrOpenImportCallback, pDevExt);
|
---|
| 1464 | if (RT_SUCCESS(rc))
|
---|
| 1465 | {
|
---|
| 1466 | /*
|
---|
| 1467 | * Commit.
|
---|
| 1468 | */
|
---|
| 1469 | pImage->hMemAlloc = hMemAlloc;
|
---|
| 1470 | pImage->hLdrMod = hLdrMod;
|
---|
| 1471 | pImage->pvImage = pvImageBits;
|
---|
| 1472 | RTMemTmpFree(pErrInfo);
|
---|
| 1473 | /** @todo Call RTLdrDone. */
|
---|
[82858] | 1474 | kprintf("VBoxDrv: Loaded %s at %p\n", pImage->szName, pvImageBits);
|
---|
[75049] | 1475 | return VINF_SUCCESS;
|
---|
| 1476 | }
|
---|
| 1477 |
|
---|
[75052] | 1478 | RTR0MemObjFree(hMemAlloc, true /*fFreeMappings*/);
|
---|
[75049] | 1479 | }
|
---|
| 1480 | else
|
---|
[75052] | 1481 | printf("VBoxDrv: Failed to allocate %u bytes for %s: %d\n", (unsigned)cbImage, pszFilename, rc);
|
---|
[75049] | 1482 | }
|
---|
| 1483 | else
|
---|
| 1484 | {
|
---|
| 1485 | printf("VBoxDrv: Image size mismatch for %s: %#x, ring-3 says %#x\n",
|
---|
[75052] | 1486 | pszFilename, (unsigned)cbImage, (unsigned)pImage->cbImageBits);
|
---|
[75049] | 1487 | rc = VERR_LDR_MISMATCH_NATIVE;
|
---|
| 1488 | }
|
---|
| 1489 | }
|
---|
| 1490 | else if (pErrInfo && RTErrInfoIsSet(&pErrInfo->Core))
|
---|
[84671] | 1491 | printf("VBoxDrv: RTLdrVerifySignature(%s) failed: %d - %s\n", pszFilename, rc, pErrInfo->Core.pszMsg);
|
---|
[75049] | 1492 | else
|
---|
[84671] | 1493 | printf("VBoxDrv: RTLdrVerifySignature(%s) failed: %d\n", pszFilename, rc);
|
---|
[75049] | 1494 | RTLdrClose(hLdrMod);
|
---|
| 1495 | }
|
---|
| 1496 | else if (pErrInfo && RTErrInfoIsSet(&pErrInfo->Core))
|
---|
| 1497 | printf("VBoxDrv: RTLdrOpenInMemory(%s) failed: %d - %s\n", pszFilename, rc, pErrInfo->Core.pszMsg);
|
---|
| 1498 | else
|
---|
| 1499 | printf("VBoxDrv: RTLdrOpenInMemory(%s) failed: %d\n", pszFilename, rc);
|
---|
| 1500 | RTMemTmpFree(pErrInfo);
|
---|
| 1501 | }
|
---|
| 1502 | return rc;
|
---|
[75053] | 1503 | #else /* !VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION */
|
---|
[58872] | 1504 | NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
|
---|
| 1505 | return VERR_NOT_SUPPORTED;
|
---|
[75053] | 1506 | #endif /* !VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION */
|
---|
[58872] | 1507 | }
|
---|
| 1508 |
|
---|
| 1509 |
|
---|
[75053] | 1510 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1511 | /**
|
---|
| 1512 | * @callback_method_impl{FNRTLDRENUMSYMS,
|
---|
| 1513 | * Worker for supdrvOSLdrValidatePointer.
|
---|
| 1514 | */
|
---|
| 1515 | static DECLCALLBACK(int) supdrvDarwinLdrValidatePointerCallback(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
|
---|
| 1516 | RTLDRADDR Value, void *pvUser)
|
---|
| 1517 | {
|
---|
| 1518 | RT_NOREF(hLdrMod, pszSymbol, uSymbol);
|
---|
[75052] | 1519 | if (Value == (uintptr_t)pvUser)
|
---|
[75049] | 1520 | return VINF_CALLBACK_RETURN;
|
---|
| 1521 | return VINF_SUCCESS;
|
---|
| 1522 | }
|
---|
| 1523 | #endif
|
---|
| 1524 |
|
---|
| 1525 |
|
---|
[75003] | 1526 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv,
|
---|
| 1527 | const uint8_t *pbImageBits, const char *pszSymbol)
|
---|
[58872] | 1528 | {
|
---|
[75053] | 1529 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1530 | AssertReturn(pImage->hLdrMod != NIL_RTLDRMOD, VERR_INVALID_STATE);
|
---|
| 1531 |
|
---|
| 1532 | /*
|
---|
| 1533 | * If we've got a symbol name, just to a lookup and compare addresses.
|
---|
| 1534 | */
|
---|
| 1535 | int rc;
|
---|
| 1536 | if (RT_C_IS_UPPER(*pszSymbol))
|
---|
| 1537 | {
|
---|
[75053] | 1538 | RTLDRADDR uValueFound;
|
---|
| 1539 | rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pvImage, (uintptr_t)pImage->pvImage, UINT32_MAX, pszSymbol, &uValueFound);
|
---|
[75049] | 1540 | if (RT_SUCCESS(rc))
|
---|
| 1541 | {
|
---|
[75053] | 1542 | if (uValueFound == (uintptr_t)pv)
|
---|
[75049] | 1543 | rc = VINF_SUCCESS;
|
---|
| 1544 | else
|
---|
| 1545 | {
|
---|
[75053] | 1546 | SUPR0Printf("SUPDrv: Different exports found for %s in %s: %RTptr, expected %p\n",
|
---|
| 1547 | pszSymbol, pImage->szName, (RTUINTPTR)uValueFound, pv);
|
---|
[75049] | 1548 | rc = VERR_LDR_BAD_FIXUP;
|
---|
| 1549 | }
|
---|
| 1550 | }
|
---|
| 1551 | else
|
---|
[75052] | 1552 | SUPR0Printf("SUPDrv: No export named %s (%p) in %s!\n", pszSymbol, pv, pImage->szName);
|
---|
[75049] | 1553 | }
|
---|
| 1554 | /*
|
---|
| 1555 | * Otherwise do a symbol enumeration and look for the entrypoint.
|
---|
| 1556 | */
|
---|
| 1557 | else
|
---|
| 1558 | {
|
---|
| 1559 | rc = RTLdrEnumSymbols(pImage->hLdrMod, 0 /*fFlags*/, pImage->pvImage, (uintptr_t)pImage->pvImage,
|
---|
| 1560 | supdrvDarwinLdrValidatePointerCallback, pv);
|
---|
| 1561 | if (rc == VINF_CALLBACK_RETURN)
|
---|
| 1562 | rc = VINF_SUCCESS;
|
---|
| 1563 | else if (RT_SUCCESS(rc))
|
---|
| 1564 | {
|
---|
| 1565 | SUPR0Printf("SUPDrv: No export with address %p (%s) in %s!\n", pv, pszSymbol, pImage->szName);
|
---|
| 1566 | rc = VERR_NOT_FOUND;
|
---|
| 1567 | }
|
---|
| 1568 | else
|
---|
| 1569 | SUPR0Printf("SUPDrv: RTLdrEnumSymbols failed on %s: %Rrc\n", pImage->szName, rc);
|
---|
| 1570 | }
|
---|
| 1571 | RT_NOREF(pDevExt, pbImageBits);
|
---|
| 1572 | return rc;
|
---|
| 1573 | #else
|
---|
[75003] | 1574 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits); NOREF(pszSymbol);
|
---|
[58872] | 1575 | return VERR_NOT_SUPPORTED;
|
---|
[75049] | 1576 | #endif
|
---|
[58872] | 1577 | }
|
---|
| 1578 |
|
---|
| 1579 |
|
---|
[75282] | 1580 | int VBOXCALL supdrvOSLdrQuerySymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage,
|
---|
| 1581 | const char *pszSymbol, size_t cchSymbol, void **ppvSymbol)
|
---|
| 1582 | {
|
---|
| 1583 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
| 1584 | /*
|
---|
| 1585 | * Just hand the problem to RTLdrGetSymbolEx.
|
---|
| 1586 | */
|
---|
| 1587 | RTLDRADDR uValueFound;
|
---|
| 1588 | int rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pvImage, (uintptr_t)pImage->pvImage, UINT32_MAX, pszSymbol, &uValueFound);
|
---|
| 1589 | if (RT_SUCCESS(rc))
|
---|
| 1590 | {
|
---|
| 1591 | *ppvSymbol = (void *)(uintptr_t)uValueFound;
|
---|
| 1592 | return VINF_SUCCESS;
|
---|
| 1593 | }
|
---|
| 1594 | RT_NOREF(pDevExt, cchSymbol);
|
---|
| 1595 | return rc;
|
---|
| 1596 |
|
---|
| 1597 | #else
|
---|
| 1598 | RT_NOREF(pDevExt, pImage, pszSymbol, cchSymbol, ppvSymbol);
|
---|
| 1599 | return VERR_WRONG_ORDER;
|
---|
| 1600 | #endif
|
---|
| 1601 | }
|
---|
| 1602 |
|
---|
| 1603 |
|
---|
[58872] | 1604 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
| 1605 | {
|
---|
[75053] | 1606 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1607 | /* State paranoia. */
|
---|
| 1608 | AssertReturn(pImage->hLdrMod != NIL_RTLDRMOD, VERR_INVALID_STATE);
|
---|
| 1609 | AssertReturn(pImage->hMemAlloc != NIL_RTR0MEMOBJ, VERR_INVALID_STATE);
|
---|
| 1610 | AssertReturn(pImage->pvImage, VERR_INVALID_STATE);
|
---|
| 1611 |
|
---|
| 1612 | /*
|
---|
| 1613 | * We should get an identical match with ring-3 here, so the code here is
|
---|
| 1614 | * trivial in comparision to SUPDrv-win.cpp.
|
---|
| 1615 | */
|
---|
| 1616 | if (!memcmp(pImage->pvImage, pbImageBits, pImage->cbImageBits))
|
---|
| 1617 | return VINF_SUCCESS;
|
---|
[75052] | 1618 |
|
---|
[75180] | 1619 | /*
|
---|
| 1620 | * Try show what when wrong (code is copied from supdrvNtCompare).
|
---|
| 1621 | */
|
---|
| 1622 | uint32_t cbLeft = pImage->cbImageBits;
|
---|
| 1623 | const uint8_t *pbNativeBits = (const uint8_t *)pImage->pvImage;
|
---|
| 1624 | for (size_t off = 0; cbLeft > 0; off++, cbLeft--)
|
---|
| 1625 | if (pbNativeBits[off] != pbImageBits[off])
|
---|
| 1626 | {
|
---|
| 1627 | /* Note! We need to copy image bits into a temporary stack buffer here as we'd
|
---|
| 1628 | otherwise risk overwriting them while formatting the error message. */
|
---|
| 1629 | uint8_t abBytes[64];
|
---|
| 1630 | memcpy(abBytes, &pbImageBits[off], RT_MIN(64, cbLeft));
|
---|
| 1631 | supdrvLdrLoadError(VERR_LDR_MISMATCH_NATIVE, pReq,
|
---|
| 1632 | "Mismatch at %#x (%p) of %s loaded at %p:\n"
|
---|
| 1633 | "ring-0: %.*Rhxs\n"
|
---|
| 1634 | "ring-3: %.*Rhxs",
|
---|
| 1635 | off, &pbNativeBits[off], pImage->szName, pImage->pvImage,
|
---|
| 1636 | RT_MIN(64, cbLeft), &pbNativeBits[off],
|
---|
| 1637 | RT_MIN(64, cbLeft), &abBytes[0]);
|
---|
[75189] | 1638 | printf("SUPDrv: %s\n", pReq->u.Out.szError);
|
---|
[75180] | 1639 | break;
|
---|
| 1640 | }
|
---|
| 1641 |
|
---|
| 1642 | RT_NOREF(pDevExt);
|
---|
[75049] | 1643 | return VERR_LDR_MISMATCH_NATIVE;
|
---|
| 1644 |
|
---|
| 1645 | #else
|
---|
[58872] | 1646 | NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
|
---|
| 1647 | return VERR_NOT_SUPPORTED;
|
---|
[75049] | 1648 | #endif
|
---|
[58872] | 1649 | }
|
---|
| 1650 |
|
---|
| 1651 |
|
---|
| 1652 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
| 1653 | {
|
---|
[75053] | 1654 | #ifdef VBOX_WITH_DARWIN_R0_DARWIN_IMAGE_VERIFICATION
|
---|
[75049] | 1655 | if (pImage->hLdrMod != NIL_RTLDRMOD)
|
---|
| 1656 | {
|
---|
| 1657 | int rc = RTLdrClose(pImage->hLdrMod);
|
---|
| 1658 | AssertRC(rc);
|
---|
| 1659 | pImage->hLdrMod = NIL_RTLDRMOD;
|
---|
| 1660 | }
|
---|
| 1661 | if (pImage->hMemAlloc != NIL_RTR0MEMOBJ)
|
---|
| 1662 | {
|
---|
| 1663 | RTR0MemObjFree(pImage->hMemAlloc, true /*fFreeMappings*/);
|
---|
| 1664 | pImage->hMemAlloc = NIL_RTR0MEMOBJ;
|
---|
| 1665 | }
|
---|
| 1666 | NOREF(pDevExt);
|
---|
| 1667 | #else
|
---|
[58872] | 1668 | NOREF(pDevExt); NOREF(pImage);
|
---|
[75049] | 1669 | #endif
|
---|
[58872] | 1670 | }
|
---|
| 1671 |
|
---|
| 1672 |
|
---|
| 1673 | void VBOXCALL supdrvOSLdrNotifyLoaded(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
| 1674 | {
|
---|
| 1675 | NOREF(pDevExt); NOREF(pImage);
|
---|
| 1676 | }
|
---|
| 1677 |
|
---|
| 1678 |
|
---|
| 1679 | void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
| 1680 | {
|
---|
[41067] | 1681 | #if 1
|
---|
[58872] | 1682 | NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
|
---|
[41067] | 1683 | #else
|
---|
[41091] | 1684 | /*
|
---|
| 1685 | * Try store the image load address in NVRAM so we can retrived it on panic.
|
---|
| 1686 | * Note! This only works if you're root! - Acutally, it doesn't work at all at the moment. FIXME!
|
---|
| 1687 | */
|
---|
[41067] | 1688 | IORegistryEntry *pEntry = IORegistryEntry::fromPath("/options", gIODTPlane);
|
---|
| 1689 | if (pEntry)
|
---|
| 1690 | {
|
---|
| 1691 | char szVar[80];
|
---|
[41091] | 1692 | RTStrPrintf(szVar, sizeof(szVar), "vboximage"/*-%s*/, pImage->szName);
|
---|
[41067] | 1693 | char szValue[48];
|
---|
| 1694 | RTStrPrintf(szValue, sizeof(szValue), "%#llx,%#llx", (uint64_t)(uintptr_t)pImage->pvImage,
|
---|
| 1695 | (uint64_t)(uintptr_t)pImage->pvImage + pImage->cbImageBits - 1);
|
---|
| 1696 | bool fRc = pEntry->setProperty(szVar, szValue); NOREF(fRc);
|
---|
| 1697 | pEntry->release();
|
---|
[41091] | 1698 | SUPR0Printf("fRc=%d '%s'='%s'\n", fRc, szVar, szValue);
|
---|
[41067] | 1699 | }
|
---|
| 1700 | /*else
|
---|
| 1701 | SUPR0Printf("failed to find /options in gIODTPlane\n");*/
|
---|
| 1702 | #endif
|
---|
| 1703 | }
|
---|
| 1704 |
|
---|
| 1705 |
|
---|
[58872] | 1706 | void VBOXCALL supdrvOSLdrNotifyUnloaded(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
[25258] | 1707 | {
|
---|
| 1708 | NOREF(pDevExt); NOREF(pImage);
|
---|
| 1709 | }
|
---|
| 1710 |
|
---|
| 1711 |
|
---|
[87700] | 1712 | void VBOXCALL supdrvOSLdrRetainWrapperModule(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
| 1713 | {
|
---|
| 1714 | RT_NOREF(pDevExt, pImage);
|
---|
| 1715 | AssertFailed();
|
---|
| 1716 | }
|
---|
| 1717 |
|
---|
| 1718 |
|
---|
| 1719 | void VBOXCALL supdrvOSLdrReleaseWrapperModule(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
| 1720 | {
|
---|
| 1721 | RT_NOREF(pDevExt, pImage);
|
---|
| 1722 | AssertFailed();
|
---|
| 1723 | }
|
---|
| 1724 |
|
---|
| 1725 |
|
---|
[49634] | 1726 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
| 1727 |
|
---|
[49767] | 1728 | typedef struct SUPDRVDARWINMSRARGS
|
---|
| 1729 | {
|
---|
| 1730 | RTUINT64U uValue;
|
---|
| 1731 | uint32_t uMsr;
|
---|
| 1732 | int rc;
|
---|
| 1733 | } SUPDRVDARWINMSRARGS, *PSUPDRVDARWINMSRARGS;
|
---|
| 1734 |
|
---|
| 1735 | /**
|
---|
| 1736 | * On CPU worker for supdrvOSMsrProberRead.
|
---|
| 1737 | *
|
---|
| 1738 | * @param idCpu Ignored.
|
---|
| 1739 | * @param pvUser1 Pointer to a SUPDRVDARWINMSRARGS.
|
---|
| 1740 | * @param pvUser2 Ignored.
|
---|
| 1741 | */
|
---|
| 1742 | static DECLCALLBACK(void) supdrvDarwinMsrProberReadOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
| 1743 | {
|
---|
| 1744 | PSUPDRVDARWINMSRARGS pArgs = (PSUPDRVDARWINMSRARGS)pvUser1;
|
---|
| 1745 | if (g_pfnRdMsr64Carefully)
|
---|
| 1746 | pArgs->rc = g_pfnRdMsr64Carefully(pArgs->uMsr, &pArgs->uValue.u);
|
---|
| 1747 | else if (g_pfnRdMsrCarefully)
|
---|
| 1748 | pArgs->rc = g_pfnRdMsrCarefully(pArgs->uMsr, &pArgs->uValue.s.Lo, &pArgs->uValue.s.Hi);
|
---|
| 1749 | else
|
---|
| 1750 | pArgs->rc = 2;
|
---|
| 1751 | NOREF(idCpu); NOREF(pvUser2);
|
---|
| 1752 | }
|
---|
| 1753 |
|
---|
| 1754 |
|
---|
[49634] | 1755 | int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
|
---|
| 1756 | {
|
---|
[49767] | 1757 | if (!g_pfnRdMsr64Carefully && !g_pfnRdMsrCarefully)
|
---|
| 1758 | return VERR_NOT_SUPPORTED;
|
---|
| 1759 |
|
---|
| 1760 | SUPDRVDARWINMSRARGS Args;
|
---|
| 1761 | Args.uMsr = uMsr;
|
---|
| 1762 | Args.uValue.u = 0;
|
---|
| 1763 | Args.rc = -1;
|
---|
| 1764 |
|
---|
| 1765 | if (idCpu == NIL_RTCPUID)
|
---|
[57371] | 1766 | {
|
---|
| 1767 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[49767] | 1768 | supdrvDarwinMsrProberReadOnCpu(idCpu, &Args, NULL);
|
---|
[57371] | 1769 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
| 1770 | }
|
---|
[49767] | 1771 | else
|
---|
| 1772 | {
|
---|
| 1773 | int rc = RTMpOnSpecific(idCpu, supdrvDarwinMsrProberReadOnCpu, &Args, NULL);
|
---|
| 1774 | if (RT_FAILURE(rc))
|
---|
| 1775 | return rc;
|
---|
| 1776 | }
|
---|
| 1777 |
|
---|
| 1778 | if (Args.rc)
|
---|
| 1779 | return VERR_ACCESS_DENIED;
|
---|
| 1780 | *puValue = Args.uValue.u;
|
---|
| 1781 | return VINF_SUCCESS;
|
---|
[49634] | 1782 | }
|
---|
| 1783 |
|
---|
| 1784 |
|
---|
[49767] | 1785 | /**
|
---|
| 1786 | * On CPU worker for supdrvOSMsrProberWrite.
|
---|
| 1787 | *
|
---|
| 1788 | * @param idCpu Ignored.
|
---|
| 1789 | * @param pvUser1 Pointer to a SUPDRVDARWINMSRARGS.
|
---|
| 1790 | * @param pvUser2 Ignored.
|
---|
| 1791 | */
|
---|
| 1792 | static DECLCALLBACK(void) supdrvDarwinMsrProberWriteOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
| 1793 | {
|
---|
| 1794 | PSUPDRVDARWINMSRARGS pArgs = (PSUPDRVDARWINMSRARGS)pvUser1;
|
---|
| 1795 | if (g_pfnWrMsr64Carefully)
|
---|
| 1796 | pArgs->rc = g_pfnWrMsr64Carefully(pArgs->uMsr, pArgs->uValue.u);
|
---|
| 1797 | else
|
---|
| 1798 | pArgs->rc = 2;
|
---|
| 1799 | NOREF(idCpu); NOREF(pvUser2);
|
---|
| 1800 | }
|
---|
| 1801 |
|
---|
| 1802 |
|
---|
[49634] | 1803 | int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
|
---|
| 1804 | {
|
---|
[49767] | 1805 | if (!g_pfnWrMsr64Carefully)
|
---|
| 1806 | return VERR_NOT_SUPPORTED;
|
---|
| 1807 |
|
---|
| 1808 | SUPDRVDARWINMSRARGS Args;
|
---|
| 1809 | Args.uMsr = uMsr;
|
---|
| 1810 | Args.uValue.u = uValue;
|
---|
| 1811 | Args.rc = -1;
|
---|
| 1812 |
|
---|
| 1813 | if (idCpu == NIL_RTCPUID)
|
---|
[57371] | 1814 | {
|
---|
| 1815 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[49767] | 1816 | supdrvDarwinMsrProberWriteOnCpu(idCpu, &Args, NULL);
|
---|
[57371] | 1817 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
| 1818 | }
|
---|
[49767] | 1819 | else
|
---|
| 1820 | {
|
---|
| 1821 | int rc = RTMpOnSpecific(idCpu, supdrvDarwinMsrProberWriteOnCpu, &Args, NULL);
|
---|
| 1822 | if (RT_FAILURE(rc))
|
---|
| 1823 | return rc;
|
---|
| 1824 | }
|
---|
| 1825 |
|
---|
| 1826 | if (Args.rc)
|
---|
| 1827 | return VERR_ACCESS_DENIED;
|
---|
| 1828 | return VINF_SUCCESS;
|
---|
[49634] | 1829 | }
|
---|
| 1830 |
|
---|
| 1831 |
|
---|
[49767] | 1832 | /**
|
---|
| 1833 | * Worker for supdrvOSMsrProberModify.
|
---|
| 1834 | */
|
---|
| 1835 | static DECLCALLBACK(void) supdrvDarwinMsrProberModifyOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
| 1836 | {
|
---|
[63510] | 1837 | RT_NOREF(idCpu, pvUser2);
|
---|
[49767] | 1838 | PSUPMSRPROBER pReq = (PSUPMSRPROBER)pvUser1;
|
---|
| 1839 | register uint32_t uMsr = pReq->u.In.uMsr;
|
---|
| 1840 | bool const fFaster = pReq->u.In.enmOp == SUPMSRPROBEROP_MODIFY_FASTER;
|
---|
| 1841 | uint64_t uBefore;
|
---|
| 1842 | uint64_t uWritten;
|
---|
| 1843 | uint64_t uAfter;
|
---|
| 1844 | int rcBefore, rcWrite, rcAfter, rcRestore;
|
---|
| 1845 | RTCCUINTREG fOldFlags;
|
---|
| 1846 |
|
---|
| 1847 | /* Initialize result variables. */
|
---|
| 1848 | uBefore = uWritten = uAfter = 0;
|
---|
| 1849 | rcWrite = rcAfter = rcRestore = -1;
|
---|
| 1850 |
|
---|
| 1851 | /*
|
---|
| 1852 | * Do the job.
|
---|
| 1853 | */
|
---|
| 1854 | fOldFlags = ASMIntDisableFlags();
|
---|
| 1855 | ASMCompilerBarrier(); /* paranoia */
|
---|
| 1856 | if (!fFaster)
|
---|
| 1857 | ASMWriteBackAndInvalidateCaches();
|
---|
| 1858 |
|
---|
| 1859 | rcBefore = g_pfnRdMsr64Carefully(uMsr, &uBefore);
|
---|
| 1860 | if (rcBefore >= 0)
|
---|
| 1861 | {
|
---|
| 1862 | register uint64_t uRestore = uBefore;
|
---|
| 1863 | uWritten = uRestore;
|
---|
| 1864 | uWritten &= pReq->u.In.uArgs.Modify.fAndMask;
|
---|
| 1865 | uWritten |= pReq->u.In.uArgs.Modify.fOrMask;
|
---|
| 1866 |
|
---|
| 1867 | rcWrite = g_pfnWrMsr64Carefully(uMsr, uWritten);
|
---|
| 1868 | rcAfter = g_pfnRdMsr64Carefully(uMsr, &uAfter);
|
---|
| 1869 | rcRestore = g_pfnWrMsr64Carefully(uMsr, uRestore);
|
---|
| 1870 |
|
---|
| 1871 | if (!fFaster)
|
---|
| 1872 | {
|
---|
| 1873 | ASMWriteBackAndInvalidateCaches();
|
---|
| 1874 | ASMReloadCR3();
|
---|
| 1875 | ASMNopPause();
|
---|
| 1876 | }
|
---|
| 1877 | }
|
---|
| 1878 |
|
---|
| 1879 | ASMCompilerBarrier(); /* paranoia */
|
---|
| 1880 | ASMSetFlags(fOldFlags);
|
---|
| 1881 |
|
---|
| 1882 | /*
|
---|
| 1883 | * Write out the results.
|
---|
| 1884 | */
|
---|
| 1885 | pReq->u.Out.uResults.Modify.uBefore = uBefore;
|
---|
| 1886 | pReq->u.Out.uResults.Modify.uWritten = uWritten;
|
---|
| 1887 | pReq->u.Out.uResults.Modify.uAfter = uAfter;
|
---|
| 1888 | pReq->u.Out.uResults.Modify.fBeforeGp = rcBefore != 0;
|
---|
| 1889 | pReq->u.Out.uResults.Modify.fModifyGp = rcWrite != 0;
|
---|
| 1890 | pReq->u.Out.uResults.Modify.fAfterGp = rcAfter != 0;
|
---|
| 1891 | pReq->u.Out.uResults.Modify.fRestoreGp = rcRestore != 0;
|
---|
| 1892 | RT_ZERO(pReq->u.Out.uResults.Modify.afReserved);
|
---|
| 1893 | }
|
---|
| 1894 |
|
---|
| 1895 |
|
---|
[49634] | 1896 | int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
|
---|
| 1897 | {
|
---|
[49767] | 1898 | if (!g_pfnWrMsr64Carefully || !g_pfnRdMsr64Carefully)
|
---|
| 1899 | return VERR_NOT_SUPPORTED;
|
---|
| 1900 | if (idCpu == NIL_RTCPUID)
|
---|
| 1901 | {
|
---|
[57371] | 1902 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[49767] | 1903 | supdrvDarwinMsrProberModifyOnCpu(idCpu, pReq, NULL);
|
---|
[57371] | 1904 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[49767] | 1905 | return VINF_SUCCESS;
|
---|
| 1906 | }
|
---|
| 1907 | return RTMpOnSpecific(idCpu, supdrvDarwinMsrProberModifyOnCpu, pReq, NULL);
|
---|
[49634] | 1908 | }
|
---|
| 1909 |
|
---|
| 1910 | #endif /* SUPDRV_WITH_MSR_PROBER */
|
---|
| 1911 |
|
---|
[49965] | 1912 | /**
|
---|
| 1913 | * Resume Bluetooth keyboard.
|
---|
| 1914 | * If there is no Bluetooth keyboard device connected to the system we just ignore this.
|
---|
| 1915 | */
|
---|
| 1916 | static void supdrvDarwinResumeBluetoothKbd(void)
|
---|
| 1917 | {
|
---|
| 1918 | OSDictionary *pDictionary = IOService::serviceMatching("AppleBluetoothHIDKeyboard");
|
---|
| 1919 | if (pDictionary)
|
---|
| 1920 | {
|
---|
| 1921 | OSIterator *pIter;
|
---|
| 1922 | IOBluetoothHIDDriver *pDriver;
|
---|
[49634] | 1923 |
|
---|
[49965] | 1924 | pIter = IOService::getMatchingServices(pDictionary);
|
---|
| 1925 | if (pIter)
|
---|
| 1926 | {
|
---|
| 1927 | while ((pDriver = (IOBluetoothHIDDriver *)pIter->getNextObject()))
|
---|
| 1928 | if (pDriver->isKeyboard())
|
---|
| 1929 | (void)pDriver->hidControl(IOBTHID_CONTROL_EXIT_SUSPEND);
|
---|
| 1930 |
|
---|
| 1931 | pIter->release();
|
---|
| 1932 | }
|
---|
| 1933 | pDictionary->release();
|
---|
| 1934 | }
|
---|
| 1935 | }
|
---|
| 1936 |
|
---|
[49787] | 1937 | /**
|
---|
| 1938 | * Resume built-in keyboard on MacBook Air and Pro hosts.
|
---|
[49965] | 1939 | * If there is no built-in keyboard device attached to the system we just ignore this.
|
---|
[49787] | 1940 | */
|
---|
[49965] | 1941 | static void supdrvDarwinResumeBuiltinKbd(void)
|
---|
[49787] | 1942 | {
|
---|
[85172] | 1943 | /** @todo macbook pro 16 w/ 10.15.5 as the "Apple Internal Keyboard /
|
---|
| 1944 | * Trackpad" hooked up to "HID Relay" / "AppleUserUSBHostHIDDevice"
|
---|
| 1945 | * and "AppleUserUSBHostHIDDevice" among other things, but not
|
---|
| 1946 | * "AppleUSBTCKeyboard". This change is probably older than 10.15,
|
---|
| 1947 | * given that IOUSBHIDDriver not is present in the 10.11 SDK. */
|
---|
| 1948 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
|
---|
[49787] | 1949 | /*
|
---|
| 1950 | * AppleUSBTCKeyboard KEXT is responsible for built-in keyboard management.
|
---|
[85172] | 1951 | * We resume keyboard by accessing to its IOService.
|
---|
| 1952 | */
|
---|
[49787] | 1953 | OSDictionary *pDictionary = IOService::serviceMatching("AppleUSBTCKeyboard");
|
---|
| 1954 | if (pDictionary)
|
---|
| 1955 | {
|
---|
| 1956 | OSIterator *pIter;
|
---|
| 1957 | IOUSBHIDDriver *pDriver;
|
---|
[49634] | 1958 |
|
---|
[49787] | 1959 | pIter = IOService::getMatchingServices(pDictionary);
|
---|
| 1960 | if (pIter)
|
---|
| 1961 | {
|
---|
| 1962 | while ((pDriver = (IOUSBHIDDriver *)pIter->getNextObject()))
|
---|
[49955] | 1963 | if (pDriver->IsPortSuspended())
|
---|
| 1964 | pDriver->SuspendPort(false, 0);
|
---|
[49787] | 1965 |
|
---|
| 1966 | pIter->release();
|
---|
| 1967 | }
|
---|
| 1968 | pDictionary->release();
|
---|
| 1969 | }
|
---|
[85172] | 1970 | #endif
|
---|
[49965] | 1971 | }
|
---|
[49787] | 1972 |
|
---|
[49965] | 1973 |
|
---|
| 1974 | /**
|
---|
| 1975 | * Resume suspended keyboard devices (if any).
|
---|
| 1976 | */
|
---|
| 1977 | int VBOXCALL supdrvDarwinResumeSuspendedKbds(void)
|
---|
| 1978 | {
|
---|
[57266] | 1979 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[49965] | 1980 | supdrvDarwinResumeBuiltinKbd();
|
---|
| 1981 | supdrvDarwinResumeBluetoothKbd();
|
---|
[57266] | 1982 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[49787] | 1983 | return 0;
|
---|
| 1984 | }
|
---|
| 1985 |
|
---|
| 1986 |
|
---|
[1] | 1987 | /**
|
---|
[25465] | 1988 | * Converts an IPRT error code to a darwin error code.
|
---|
[1] | 1989 | *
|
---|
| 1990 | * @returns corresponding darwin error code.
|
---|
[25465] | 1991 | * @param rc IPRT status code.
|
---|
[1] | 1992 | */
|
---|
[4817] | 1993 | static int VBoxDrvDarwinErr2DarwinErr(int rc)
|
---|
[1] | 1994 | {
|
---|
| 1995 | switch (rc)
|
---|
| 1996 | {
|
---|
[25465] | 1997 | case VINF_SUCCESS: return 0;
|
---|
| 1998 | case VERR_GENERAL_FAILURE: return EACCES;
|
---|
| 1999 | case VERR_INVALID_PARAMETER: return EINVAL;
|
---|
| 2000 | case VERR_INVALID_MAGIC: return EILSEQ;
|
---|
| 2001 | case VERR_INVALID_HANDLE: return ENXIO;
|
---|
| 2002 | case VERR_INVALID_POINTER: return EFAULT;
|
---|
| 2003 | case VERR_LOCK_FAILED: return ENOLCK;
|
---|
| 2004 | case VERR_ALREADY_LOADED: return EEXIST;
|
---|
| 2005 | case VERR_PERMISSION_DENIED: return EPERM;
|
---|
| 2006 | case VERR_VERSION_MISMATCH: return ENOSYS;
|
---|
[1] | 2007 | }
|
---|
| 2008 |
|
---|
| 2009 | return EPERM;
|
---|
| 2010 | }
|
---|
| 2011 |
|
---|
[57108] | 2012 |
|
---|
[55318] | 2013 | /**
|
---|
| 2014 | * Check if the CPU has SMAP support.
|
---|
| 2015 | */
|
---|
| 2016 | static bool vboxdrvDarwinCpuHasSMAP(void)
|
---|
| 2017 | {
|
---|
| 2018 | uint32_t uMaxId, uEAX, uEBX, uECX, uEDX;
|
---|
| 2019 | ASMCpuId(0, &uMaxId, &uEBX, &uECX, &uEDX);
|
---|
[93515] | 2020 | if ( RTX86IsValidStdRange(uMaxId)
|
---|
[55318] | 2021 | && uMaxId >= 0x00000007)
|
---|
| 2022 | {
|
---|
| 2023 | ASMCpuId_Idx_ECX(0x00000007, 0, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
| 2024 | if (uEBX & X86_CPUID_STEXT_FEATURE_EBX_SMAP)
|
---|
| 2025 | return true;
|
---|
| 2026 | }
|
---|
[57220] | 2027 | #ifdef VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV
|
---|
| 2028 | return true;
|
---|
| 2029 | #else
|
---|
[55318] | 2030 | return false;
|
---|
[57220] | 2031 | #endif
|
---|
[55318] | 2032 | }
|
---|
[1] | 2033 |
|
---|
[57108] | 2034 |
|
---|
[91789] | 2035 | RTDECL(int) SUPR0PrintfV(const char *pszFormat, va_list va)
|
---|
[1] | 2036 | {
|
---|
[57229] | 2037 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[1] | 2038 |
|
---|
[91789] | 2039 | char szMsg[512];
|
---|
[37970] | 2040 | RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
| 2041 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
[1] | 2042 |
|
---|
| 2043 | printf("%s", szMsg);
|
---|
[82858] | 2044 | kprintf("%s", szMsg);
|
---|
[57229] | 2045 |
|
---|
| 2046 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 2047 | return 0;
|
---|
| 2048 | }
|
---|
| 2049 |
|
---|
| 2050 |
|
---|
[52192] | 2051 | SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
|
---|
| 2052 | {
|
---|
[82818] | 2053 | return g_fKernelFeatures;
|
---|
[52192] | 2054 | }
|
---|
| 2055 |
|
---|
| 2056 |
|
---|
[95087] | 2057 | SUPR0DECL(bool) SUPR0FpuBegin(bool fCtxHook)
|
---|
| 2058 | {
|
---|
| 2059 | RT_NOREF(fCtxHook);
|
---|
| 2060 | return false;
|
---|
| 2061 | }
|
---|
| 2062 |
|
---|
| 2063 |
|
---|
| 2064 | SUPR0DECL(void) SUPR0FpuEnd(bool fCtxHook)
|
---|
| 2065 | {
|
---|
| 2066 | RT_NOREF(fCtxHook);
|
---|
| 2067 | }
|
---|
| 2068 |
|
---|
[75718] | 2069 | /*
|
---|
| 2070 | *
|
---|
[378] | 2071 | * org_virtualbox_SupDrv
|
---|
| 2072 | *
|
---|
[75710] | 2073 | * - IOService diff resync -
|
---|
| 2074 | * - IOService diff resync -
|
---|
| 2075 | * - IOService diff resync -
|
---|
[75718] | 2076 | *
|
---|
[378] | 2077 | */
|
---|
| 2078 |
|
---|
| 2079 |
|
---|
| 2080 | /**
|
---|
| 2081 | * Initialize the object.
|
---|
| 2082 | */
|
---|
| 2083 | bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
|
---|
| 2084 | {
|
---|
[75710] | 2085 | LogFlow(("IOService::init([%p], %p)\n", this, pDictionary));
|
---|
[378] | 2086 | if (IOService::init(pDictionary))
|
---|
| 2087 | {
|
---|
| 2088 | /* init members. */
|
---|
| 2089 | return true;
|
---|
| 2090 | }
|
---|
| 2091 | return false;
|
---|
| 2092 | }
|
---|
| 2093 |
|
---|
| 2094 |
|
---|
| 2095 | /**
|
---|
| 2096 | * Free the object.
|
---|
| 2097 | */
|
---|
| 2098 | void org_virtualbox_SupDrv::free(void)
|
---|
| 2099 | {
|
---|
[10662] | 2100 | LogFlow(("IOService::free([%p])\n", this));
|
---|
[378] | 2101 | IOService::free();
|
---|
| 2102 | }
|
---|
| 2103 |
|
---|
| 2104 |
|
---|
| 2105 | /**
|
---|
| 2106 | * Check if it's ok to start this service.
|
---|
| 2107 | * It's always ok by us, so it's up to IOService to decide really.
|
---|
| 2108 | */
|
---|
| 2109 | IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
|
---|
| 2110 | {
|
---|
[75710] | 2111 | LogFlow(("IOService::probe([%p])\n", this));
|
---|
[378] | 2112 | return IOService::probe(pProvider, pi32Score);
|
---|
| 2113 | }
|
---|
| 2114 |
|
---|
| 2115 |
|
---|
| 2116 | /**
|
---|
| 2117 | * Start this service.
|
---|
| 2118 | */
|
---|
| 2119 | bool org_virtualbox_SupDrv::start(IOService *pProvider)
|
---|
| 2120 | {
|
---|
[10662] | 2121 | LogFlow(("org_virtualbox_SupDrv::start([%p])\n", this));
|
---|
[378] | 2122 |
|
---|
| 2123 | if (IOService::start(pProvider))
|
---|
| 2124 | {
|
---|
| 2125 | /* register the service. */
|
---|
| 2126 | registerService();
|
---|
| 2127 | return true;
|
---|
| 2128 | }
|
---|
| 2129 | return false;
|
---|
| 2130 | }
|
---|
| 2131 |
|
---|
| 2132 |
|
---|
| 2133 | /**
|
---|
| 2134 | * Stop this service.
|
---|
| 2135 | */
|
---|
| 2136 | void org_virtualbox_SupDrv::stop(IOService *pProvider)
|
---|
| 2137 | {
|
---|
[10662] | 2138 | LogFlow(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
|
---|
[378] | 2139 | IOService::stop(pProvider);
|
---|
| 2140 | }
|
---|
| 2141 |
|
---|
| 2142 |
|
---|
[10391] | 2143 | /**
|
---|
| 2144 | * Termination request.
|
---|
| 2145 | *
|
---|
| 2146 | * @return true if we're ok with shutting down now, false if we're not.
|
---|
| 2147 | * @param fOptions Flags.
|
---|
| 2148 | */
|
---|
| 2149 | bool org_virtualbox_SupDrv::terminate(IOOptionBits fOptions)
|
---|
| 2150 | {
|
---|
| 2151 | bool fRc;
|
---|
[10662] | 2152 | LogFlow(("org_virtualbox_SupDrv::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
|
---|
[10392] | 2153 | KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions));
|
---|
[10391] | 2154 | if ( KMOD_INFO_NAME.reference_count != 0
|
---|
| 2155 | || ASMAtomicUoReadS32(&g_cSessions))
|
---|
| 2156 | fRc = false;
|
---|
| 2157 | else
|
---|
| 2158 | fRc = IOService::terminate(fOptions);
|
---|
[10662] | 2159 | LogFlow(("org_virtualbox_SupDrv::terminate: returns %d\n", fRc));
|
---|
[10391] | 2160 | return fRc;
|
---|
| 2161 | }
|
---|
| 2162 |
|
---|
| 2163 |
|
---|
[378] | 2164 | /*
|
---|
| 2165 | *
|
---|
| 2166 | * org_virtualbox_SupDrvClient
|
---|
| 2167 | *
|
---|
| 2168 | */
|
---|
| 2169 |
|
---|
| 2170 |
|
---|
| 2171 | /**
|
---|
| 2172 | * Initializer called when the client opens the service.
|
---|
| 2173 | */
|
---|
| 2174 | bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
|
---|
| 2175 | {
|
---|
[10836] | 2176 | LogFlow(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x) (cur pid=%d proc=%p)\n",
|
---|
| 2177 | this, OwningTask, pvSecurityId, u32Type, RTProcSelf(), RTR0ProcHandleSelf()));
|
---|
| 2178 | AssertMsg((RTR0PROCESS)OwningTask == RTR0ProcHandleSelf(), ("%p %p\n", OwningTask, RTR0ProcHandleSelf()));
|
---|
[378] | 2179 |
|
---|
| 2180 | if (!OwningTask)
|
---|
| 2181 | return false;
|
---|
[51488] | 2182 |
|
---|
| 2183 | if (u32Type != SUP_DARWIN_IOSERVICE_COOKIE)
|
---|
| 2184 | {
|
---|
[75710] | 2185 | VBOX_RETRIEVE_CUR_PROC_NAME(szProcName);
|
---|
| 2186 | LogRelMax(10,("org_virtualbox_SupDrvClient::initWithTask: Bad cookie %#x (%s)\n", u32Type, szProcName));
|
---|
[51488] | 2187 | return false;
|
---|
| 2188 | }
|
---|
| 2189 |
|
---|
[378] | 2190 | if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
|
---|
| 2191 | {
|
---|
[64432] | 2192 | /*
|
---|
| 2193 | * In theory we have to call task_reference() to make sure that the task is
|
---|
| 2194 | * valid during the lifetime of this object. The pointer is only used to check
|
---|
| 2195 | * for the context this object is called in though and never dereferenced
|
---|
| 2196 | * or passed to anything which might, so we just skip this step.
|
---|
| 2197 | */
|
---|
[378] | 2198 | m_Task = OwningTask;
|
---|
| 2199 | m_pSession = NULL;
|
---|
| 2200 | m_pProvider = NULL;
|
---|
| 2201 | return true;
|
---|
| 2202 | }
|
---|
| 2203 | return false;
|
---|
| 2204 | }
|
---|
| 2205 |
|
---|
| 2206 |
|
---|
| 2207 | /**
|
---|
| 2208 | * Start the client service.
|
---|
| 2209 | */
|
---|
| 2210 | bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
|
---|
| 2211 | {
|
---|
[10836] | 2212 | LogFlow(("org_virtualbox_SupDrvClient::start([%p], %p) (cur pid=%d proc=%p)\n",
|
---|
| 2213 | this, pProvider, RTProcSelf(), RTR0ProcHandleSelf() ));
|
---|
| 2214 | AssertMsgReturn((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(),
|
---|
| 2215 | ("%p %p\n", m_Task, RTR0ProcHandleSelf()),
|
---|
| 2216 | false);
|
---|
| 2217 |
|
---|
[378] | 2218 | if (IOUserClient::start(pProvider))
|
---|
| 2219 | {
|
---|
| 2220 | m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
|
---|
| 2221 | if (m_pProvider)
|
---|
| 2222 | {
|
---|
[10836] | 2223 | Assert(!m_pSession);
|
---|
| 2224 |
|
---|
| 2225 | /*
|
---|
| 2226 | * Create a new session.
|
---|
| 2227 | */
|
---|
[44173] | 2228 | int rc = supdrvCreateSession(&g_DevExt, true /* fUser */, false /*fUnrestricted*/, &m_pSession);
|
---|
[10836] | 2229 | if (RT_SUCCESS(rc))
|
---|
| 2230 | {
|
---|
| 2231 | m_pSession->fOpened = false;
|
---|
[44173] | 2232 | /* The Uid, Gid and fUnrestricted fields are set on open. */
|
---|
[10836] | 2233 |
|
---|
| 2234 | /*
|
---|
| 2235 | * Insert it into the hash table, checking that there isn't
|
---|
[44173] | 2236 | * already one for this process first. (One session per proc!)
|
---|
[10836] | 2237 | */
|
---|
| 2238 | unsigned iHash = SESSION_HASH(m_pSession->Process);
|
---|
[40806] | 2239 | RTSpinlockAcquire(g_Spinlock);
|
---|
[10836] | 2240 |
|
---|
| 2241 | PSUPDRVSESSION pCur = g_apSessionHashTab[iHash];
|
---|
[51100] | 2242 | while (pCur && pCur->Process != m_pSession->Process)
|
---|
| 2243 | pCur = pCur->pNextHash;
|
---|
[10836] | 2244 | if (!pCur)
|
---|
| 2245 | {
|
---|
| 2246 | m_pSession->pNextHash = g_apSessionHashTab[iHash];
|
---|
| 2247 | g_apSessionHashTab[iHash] = m_pSession;
|
---|
| 2248 | m_pSession->pvSupDrvClient = this;
|
---|
| 2249 | ASMAtomicIncS32(&g_cSessions);
|
---|
| 2250 | rc = VINF_SUCCESS;
|
---|
| 2251 | }
|
---|
| 2252 | else
|
---|
| 2253 | rc = VERR_ALREADY_LOADED;
|
---|
| 2254 |
|
---|
[52618] | 2255 | RTSpinlockRelease(g_Spinlock);
|
---|
[10836] | 2256 | if (RT_SUCCESS(rc))
|
---|
| 2257 | {
|
---|
| 2258 | Log(("org_virtualbox_SupDrvClient::start: created session %p for pid %d\n", m_pSession, (int)RTProcSelf()));
|
---|
| 2259 | return true;
|
---|
| 2260 | }
|
---|
| 2261 |
|
---|
| 2262 | LogFlow(("org_virtualbox_SupDrvClient::start: already got a session for this process (%p)\n", pCur));
|
---|
[47537] | 2263 | supdrvSessionRelease(m_pSession);
|
---|
[10836] | 2264 | }
|
---|
| 2265 |
|
---|
| 2266 | m_pSession = NULL;
|
---|
| 2267 | LogFlow(("org_virtualbox_SupDrvClient::start: rc=%Rrc from supdrvCreateSession\n", rc));
|
---|
[378] | 2268 | }
|
---|
[10836] | 2269 | else
|
---|
| 2270 | LogFlow(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
|
---|
[378] | 2271 | }
|
---|
| 2272 | return false;
|
---|
| 2273 | }
|
---|
| 2274 |
|
---|
| 2275 |
|
---|
| 2276 | /**
|
---|
[10836] | 2277 | * Common worker for clientClose and VBoxDrvDarwinClose.
|
---|
| 2278 | */
|
---|
| 2279 | /* static */ void org_virtualbox_SupDrvClient::sessionClose(RTPROCESS Process)
|
---|
| 2280 | {
|
---|
| 2281 | /*
|
---|
[44173] | 2282 | * Find the session and remove it from the hash table.
|
---|
| 2283 | *
|
---|
| 2284 | * Note! Only one session per process. (Both start() and
|
---|
| 2285 | * VBoxDrvDarwinOpen makes sure this is so.)
|
---|
[10836] | 2286 | */
|
---|
| 2287 | const unsigned iHash = SESSION_HASH(Process);
|
---|
[40806] | 2288 | RTSpinlockAcquire(g_Spinlock);
|
---|
[10836] | 2289 | PSUPDRVSESSION pSession = g_apSessionHashTab[iHash];
|
---|
| 2290 | if (pSession)
|
---|
| 2291 | {
|
---|
| 2292 | if (pSession->Process == Process)
|
---|
| 2293 | {
|
---|
| 2294 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
| 2295 | pSession->pNextHash = NULL;
|
---|
| 2296 | ASMAtomicDecS32(&g_cSessions);
|
---|
| 2297 | }
|
---|
| 2298 | else
|
---|
| 2299 | {
|
---|
| 2300 | PSUPDRVSESSION pPrev = pSession;
|
---|
| 2301 | pSession = pSession->pNextHash;
|
---|
| 2302 | while (pSession)
|
---|
| 2303 | {
|
---|
| 2304 | if (pSession->Process == Process)
|
---|
| 2305 | {
|
---|
| 2306 | pPrev->pNextHash = pSession->pNextHash;
|
---|
| 2307 | pSession->pNextHash = NULL;
|
---|
| 2308 | ASMAtomicDecS32(&g_cSessions);
|
---|
| 2309 | break;
|
---|
| 2310 | }
|
---|
| 2311 |
|
---|
| 2312 | /* next */
|
---|
| 2313 | pPrev = pSession;
|
---|
| 2314 | pSession = pSession->pNextHash;
|
---|
| 2315 | }
|
---|
| 2316 | }
|
---|
| 2317 | }
|
---|
[52618] | 2318 | RTSpinlockRelease(g_Spinlock);
|
---|
[10836] | 2319 | if (!pSession)
|
---|
| 2320 | {
|
---|
| 2321 | Log(("SupDrvClient::sessionClose: pSession == NULL, pid=%d; freed already?\n", (int)Process));
|
---|
| 2322 | return;
|
---|
| 2323 | }
|
---|
| 2324 |
|
---|
| 2325 | /*
|
---|
| 2326 | * Remove it from the client object.
|
---|
| 2327 | */
|
---|
| 2328 | org_virtualbox_SupDrvClient *pThis = (org_virtualbox_SupDrvClient *)pSession->pvSupDrvClient;
|
---|
| 2329 | pSession->pvSupDrvClient = NULL;
|
---|
| 2330 | if (pThis)
|
---|
| 2331 | {
|
---|
| 2332 | Assert(pThis->m_pSession == pSession);
|
---|
| 2333 | pThis->m_pSession = NULL;
|
---|
| 2334 | }
|
---|
| 2335 |
|
---|
| 2336 | /*
|
---|
| 2337 | * Close the session.
|
---|
| 2338 | */
|
---|
[47537] | 2339 | supdrvSessionRelease(pSession);
|
---|
[10836] | 2340 | }
|
---|
| 2341 |
|
---|
| 2342 |
|
---|
| 2343 | /**
|
---|
[378] | 2344 | * Client exits normally.
|
---|
| 2345 | */
|
---|
| 2346 | IOReturn org_virtualbox_SupDrvClient::clientClose(void)
|
---|
| 2347 | {
|
---|
[10836] | 2348 | LogFlow(("org_virtualbox_SupDrvClient::clientClose([%p]) (cur pid=%d proc=%p)\n", this, RTProcSelf(), RTR0ProcHandleSelf()));
|
---|
| 2349 | AssertMsg((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(), ("%p %p\n", m_Task, RTR0ProcHandleSelf()));
|
---|
[378] | 2350 |
|
---|
[10836] | 2351 | /*
|
---|
| 2352 | * Clean up the session if it's still around.
|
---|
| 2353 | *
|
---|
| 2354 | * We cannot rely 100% on close, and in the case of a dead client
|
---|
| 2355 | * we'll end up hanging inside vm_map_remove() if we postpone it.
|
---|
| 2356 | */
|
---|
| 2357 | if (m_pSession)
|
---|
| 2358 | {
|
---|
| 2359 | sessionClose(RTProcSelf());
|
---|
| 2360 | Assert(!m_pSession);
|
---|
| 2361 | }
|
---|
| 2362 |
|
---|
[378] | 2363 | m_pProvider = NULL;
|
---|
| 2364 | terminate();
|
---|
| 2365 |
|
---|
| 2366 | return kIOReturnSuccess;
|
---|
| 2367 | }
|
---|
| 2368 |
|
---|
| 2369 |
|
---|
| 2370 | /**
|
---|
[10836] | 2371 | * The client exits abnormally / forgets to do cleanups. (logging)
|
---|
[378] | 2372 | */
|
---|
| 2373 | IOReturn org_virtualbox_SupDrvClient::clientDied(void)
|
---|
| 2374 | {
|
---|
[75710] | 2375 | LogFlow(("IOService::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n", this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
|
---|
[378] | 2376 |
|
---|
[10836] | 2377 | /* IOUserClient::clientDied() calls clientClose, so we'll just do the work there. */
|
---|
[378] | 2378 | return IOUserClient::clientDied();
|
---|
| 2379 | }
|
---|
| 2380 |
|
---|
| 2381 |
|
---|
| 2382 | /**
|
---|
[10836] | 2383 | * Terminate the service (initiate the destruction). (logging)
|
---|
[378] | 2384 | */
|
---|
| 2385 | bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
|
---|
| 2386 | {
|
---|
[75710] | 2387 | LogFlow(("IOService::terminate([%p], %#x)\n", this, fOptions));
|
---|
[378] | 2388 | return IOUserClient::terminate(fOptions);
|
---|
| 2389 | }
|
---|
| 2390 |
|
---|
| 2391 |
|
---|
| 2392 | /**
|
---|
[10836] | 2393 | * The final stage of the client service destruction. (logging)
|
---|
[378] | 2394 | */
|
---|
| 2395 | bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
|
---|
| 2396 | {
|
---|
[75710] | 2397 | LogFlow(("IOService::finalize([%p], %#x)\n", this, fOptions));
|
---|
[378] | 2398 | return IOUserClient::finalize(fOptions);
|
---|
| 2399 | }
|
---|
| 2400 |
|
---|
[387] | 2401 |
|
---|
| 2402 | /**
|
---|
[10836] | 2403 | * Stop the client service. (logging)
|
---|
[387] | 2404 | */
|
---|
| 2405 | void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
|
---|
| 2406 | {
|
---|
[75710] | 2407 | LogFlow(("IOService::stop([%p])\n", this));
|
---|
[387] | 2408 | IOUserClient::stop(pProvider);
|
---|
| 2409 | }
|
---|
| 2410 |
|
---|