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