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