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