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