1 | /* $Id: SUPDrv-darwin.cpp 54581 2015-03-02 14:56:02Z 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 supdrvOSAreCpusOfflinedOnSuspend(void)
|
---|
957 | {
|
---|
958 | /** @todo verify this. */
|
---|
959 | return false;
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
|
---|
964 | {
|
---|
965 | return false;
|
---|
966 | }
|
---|
967 |
|
---|
968 | void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
969 | {
|
---|
970 | #if 1
|
---|
971 | NOREF(pDevExt); NOREF(pImage);
|
---|
972 | #else
|
---|
973 | /*
|
---|
974 | * Try store the image load address in NVRAM so we can retrived it on panic.
|
---|
975 | * Note! This only works if you're root! - Acutally, it doesn't work at all at the moment. FIXME!
|
---|
976 | */
|
---|
977 | IORegistryEntry *pEntry = IORegistryEntry::fromPath("/options", gIODTPlane);
|
---|
978 | if (pEntry)
|
---|
979 | {
|
---|
980 | char szVar[80];
|
---|
981 | RTStrPrintf(szVar, sizeof(szVar), "vboximage"/*-%s*/, pImage->szName);
|
---|
982 | char szValue[48];
|
---|
983 | RTStrPrintf(szValue, sizeof(szValue), "%#llx,%#llx", (uint64_t)(uintptr_t)pImage->pvImage,
|
---|
984 | (uint64_t)(uintptr_t)pImage->pvImage + pImage->cbImageBits - 1);
|
---|
985 | bool fRc = pEntry->setProperty(szVar, szValue); NOREF(fRc);
|
---|
986 | pEntry->release();
|
---|
987 | SUPR0Printf("fRc=%d '%s'='%s'\n", fRc, szVar, szValue);
|
---|
988 | }
|
---|
989 | /*else
|
---|
990 | SUPR0Printf("failed to find /options in gIODTPlane\n");*/
|
---|
991 | #endif
|
---|
992 | }
|
---|
993 |
|
---|
994 |
|
---|
995 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
996 | {
|
---|
997 | NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
|
---|
998 | return VERR_NOT_SUPPORTED;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
1003 | {
|
---|
1004 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
1005 | return VERR_NOT_SUPPORTED;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 |
|
---|
1009 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
1010 | {
|
---|
1011 | NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
|
---|
1012 | return VERR_NOT_SUPPORTED;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
1017 | {
|
---|
1018 | NOREF(pDevExt); NOREF(pImage);
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 |
|
---|
1022 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
1023 |
|
---|
1024 | typedef struct SUPDRVDARWINMSRARGS
|
---|
1025 | {
|
---|
1026 | RTUINT64U uValue;
|
---|
1027 | uint32_t uMsr;
|
---|
1028 | int rc;
|
---|
1029 | } SUPDRVDARWINMSRARGS, *PSUPDRVDARWINMSRARGS;
|
---|
1030 |
|
---|
1031 | /**
|
---|
1032 | * On CPU worker for supdrvOSMsrProberRead.
|
---|
1033 | *
|
---|
1034 | * @param idCpu Ignored.
|
---|
1035 | * @param pvUser1 Pointer to a SUPDRVDARWINMSRARGS.
|
---|
1036 | * @param pvUser2 Ignored.
|
---|
1037 | */
|
---|
1038 | static DECLCALLBACK(void) supdrvDarwinMsrProberReadOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
1039 | {
|
---|
1040 | PSUPDRVDARWINMSRARGS pArgs = (PSUPDRVDARWINMSRARGS)pvUser1;
|
---|
1041 | if (g_pfnRdMsr64Carefully)
|
---|
1042 | pArgs->rc = g_pfnRdMsr64Carefully(pArgs->uMsr, &pArgs->uValue.u);
|
---|
1043 | else if (g_pfnRdMsrCarefully)
|
---|
1044 | pArgs->rc = g_pfnRdMsrCarefully(pArgs->uMsr, &pArgs->uValue.s.Lo, &pArgs->uValue.s.Hi);
|
---|
1045 | else
|
---|
1046 | pArgs->rc = 2;
|
---|
1047 | NOREF(idCpu); NOREF(pvUser2);
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
|
---|
1052 | {
|
---|
1053 | if (!g_pfnRdMsr64Carefully && !g_pfnRdMsrCarefully)
|
---|
1054 | return VERR_NOT_SUPPORTED;
|
---|
1055 |
|
---|
1056 | SUPDRVDARWINMSRARGS Args;
|
---|
1057 | Args.uMsr = uMsr;
|
---|
1058 | Args.uValue.u = 0;
|
---|
1059 | Args.rc = -1;
|
---|
1060 |
|
---|
1061 | if (idCpu == NIL_RTCPUID)
|
---|
1062 | supdrvDarwinMsrProberReadOnCpu(idCpu, &Args, NULL);
|
---|
1063 | else
|
---|
1064 | {
|
---|
1065 | int rc = RTMpOnSpecific(idCpu, supdrvDarwinMsrProberReadOnCpu, &Args, NULL);
|
---|
1066 | if (RT_FAILURE(rc))
|
---|
1067 | return rc;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | if (Args.rc)
|
---|
1071 | return VERR_ACCESS_DENIED;
|
---|
1072 | *puValue = Args.uValue.u;
|
---|
1073 | return VINF_SUCCESS;
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * On CPU worker for supdrvOSMsrProberWrite.
|
---|
1079 | *
|
---|
1080 | * @param idCpu Ignored.
|
---|
1081 | * @param pvUser1 Pointer to a SUPDRVDARWINMSRARGS.
|
---|
1082 | * @param pvUser2 Ignored.
|
---|
1083 | */
|
---|
1084 | static DECLCALLBACK(void) supdrvDarwinMsrProberWriteOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
1085 | {
|
---|
1086 | PSUPDRVDARWINMSRARGS pArgs = (PSUPDRVDARWINMSRARGS)pvUser1;
|
---|
1087 | if (g_pfnWrMsr64Carefully)
|
---|
1088 | pArgs->rc = g_pfnWrMsr64Carefully(pArgs->uMsr, pArgs->uValue.u);
|
---|
1089 | else
|
---|
1090 | pArgs->rc = 2;
|
---|
1091 | NOREF(idCpu); NOREF(pvUser2);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 |
|
---|
1095 | int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
|
---|
1096 | {
|
---|
1097 | if (!g_pfnWrMsr64Carefully)
|
---|
1098 | return VERR_NOT_SUPPORTED;
|
---|
1099 |
|
---|
1100 | SUPDRVDARWINMSRARGS Args;
|
---|
1101 | Args.uMsr = uMsr;
|
---|
1102 | Args.uValue.u = uValue;
|
---|
1103 | Args.rc = -1;
|
---|
1104 |
|
---|
1105 | if (idCpu == NIL_RTCPUID)
|
---|
1106 | supdrvDarwinMsrProberWriteOnCpu(idCpu, &Args, NULL);
|
---|
1107 | else
|
---|
1108 | {
|
---|
1109 | int rc = RTMpOnSpecific(idCpu, supdrvDarwinMsrProberWriteOnCpu, &Args, NULL);
|
---|
1110 | if (RT_FAILURE(rc))
|
---|
1111 | return rc;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | if (Args.rc)
|
---|
1115 | return VERR_ACCESS_DENIED;
|
---|
1116 | return VINF_SUCCESS;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | /**
|
---|
1121 | * Worker for supdrvOSMsrProberModify.
|
---|
1122 | */
|
---|
1123 | static DECLCALLBACK(void) supdrvDarwinMsrProberModifyOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
1124 | {
|
---|
1125 | PSUPMSRPROBER pReq = (PSUPMSRPROBER)pvUser1;
|
---|
1126 | register uint32_t uMsr = pReq->u.In.uMsr;
|
---|
1127 | bool const fFaster = pReq->u.In.enmOp == SUPMSRPROBEROP_MODIFY_FASTER;
|
---|
1128 | uint64_t uBefore;
|
---|
1129 | uint64_t uWritten;
|
---|
1130 | uint64_t uAfter;
|
---|
1131 | int rcBefore, rcWrite, rcAfter, rcRestore;
|
---|
1132 | RTCCUINTREG fOldFlags;
|
---|
1133 |
|
---|
1134 | /* Initialize result variables. */
|
---|
1135 | uBefore = uWritten = uAfter = 0;
|
---|
1136 | rcWrite = rcAfter = rcRestore = -1;
|
---|
1137 |
|
---|
1138 | /*
|
---|
1139 | * Do the job.
|
---|
1140 | */
|
---|
1141 | fOldFlags = ASMIntDisableFlags();
|
---|
1142 | ASMCompilerBarrier(); /* paranoia */
|
---|
1143 | if (!fFaster)
|
---|
1144 | ASMWriteBackAndInvalidateCaches();
|
---|
1145 |
|
---|
1146 | rcBefore = g_pfnRdMsr64Carefully(uMsr, &uBefore);
|
---|
1147 | if (rcBefore >= 0)
|
---|
1148 | {
|
---|
1149 | register uint64_t uRestore = uBefore;
|
---|
1150 | uWritten = uRestore;
|
---|
1151 | uWritten &= pReq->u.In.uArgs.Modify.fAndMask;
|
---|
1152 | uWritten |= pReq->u.In.uArgs.Modify.fOrMask;
|
---|
1153 |
|
---|
1154 | rcWrite = g_pfnWrMsr64Carefully(uMsr, uWritten);
|
---|
1155 | rcAfter = g_pfnRdMsr64Carefully(uMsr, &uAfter);
|
---|
1156 | rcRestore = g_pfnWrMsr64Carefully(uMsr, uRestore);
|
---|
1157 |
|
---|
1158 | if (!fFaster)
|
---|
1159 | {
|
---|
1160 | ASMWriteBackAndInvalidateCaches();
|
---|
1161 | ASMReloadCR3();
|
---|
1162 | ASMNopPause();
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | ASMCompilerBarrier(); /* paranoia */
|
---|
1167 | ASMSetFlags(fOldFlags);
|
---|
1168 |
|
---|
1169 | /*
|
---|
1170 | * Write out the results.
|
---|
1171 | */
|
---|
1172 | pReq->u.Out.uResults.Modify.uBefore = uBefore;
|
---|
1173 | pReq->u.Out.uResults.Modify.uWritten = uWritten;
|
---|
1174 | pReq->u.Out.uResults.Modify.uAfter = uAfter;
|
---|
1175 | pReq->u.Out.uResults.Modify.fBeforeGp = rcBefore != 0;
|
---|
1176 | pReq->u.Out.uResults.Modify.fModifyGp = rcWrite != 0;
|
---|
1177 | pReq->u.Out.uResults.Modify.fAfterGp = rcAfter != 0;
|
---|
1178 | pReq->u.Out.uResults.Modify.fRestoreGp = rcRestore != 0;
|
---|
1179 | RT_ZERO(pReq->u.Out.uResults.Modify.afReserved);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
|
---|
1184 | {
|
---|
1185 | if (!g_pfnWrMsr64Carefully || !g_pfnRdMsr64Carefully)
|
---|
1186 | return VERR_NOT_SUPPORTED;
|
---|
1187 | if (idCpu == NIL_RTCPUID)
|
---|
1188 | {
|
---|
1189 | supdrvDarwinMsrProberModifyOnCpu(idCpu, pReq, NULL);
|
---|
1190 | return VINF_SUCCESS;
|
---|
1191 | }
|
---|
1192 | return RTMpOnSpecific(idCpu, supdrvDarwinMsrProberModifyOnCpu, pReq, NULL);
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | #endif /* SUPDRV_WITH_MSR_PROBER */
|
---|
1196 |
|
---|
1197 | /**
|
---|
1198 | * Resume Bluetooth keyboard.
|
---|
1199 | * If there is no Bluetooth keyboard device connected to the system we just ignore this.
|
---|
1200 | */
|
---|
1201 | static void supdrvDarwinResumeBluetoothKbd(void)
|
---|
1202 | {
|
---|
1203 | OSDictionary *pDictionary = IOService::serviceMatching("AppleBluetoothHIDKeyboard");
|
---|
1204 | if (pDictionary)
|
---|
1205 | {
|
---|
1206 | OSIterator *pIter;
|
---|
1207 | IOBluetoothHIDDriver *pDriver;
|
---|
1208 |
|
---|
1209 | pIter = IOService::getMatchingServices(pDictionary);
|
---|
1210 | if (pIter)
|
---|
1211 | {
|
---|
1212 | while ((pDriver = (IOBluetoothHIDDriver *)pIter->getNextObject()))
|
---|
1213 | if (pDriver->isKeyboard())
|
---|
1214 | (void)pDriver->hidControl(IOBTHID_CONTROL_EXIT_SUSPEND);
|
---|
1215 |
|
---|
1216 | pIter->release();
|
---|
1217 | }
|
---|
1218 | pDictionary->release();
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /**
|
---|
1223 | * Resume built-in keyboard on MacBook Air and Pro hosts.
|
---|
1224 | * If there is no built-in keyboard device attached to the system we just ignore this.
|
---|
1225 | */
|
---|
1226 | static void supdrvDarwinResumeBuiltinKbd(void)
|
---|
1227 | {
|
---|
1228 | /*
|
---|
1229 | * AppleUSBTCKeyboard KEXT is responsible for built-in keyboard management.
|
---|
1230 | * We resume keyboard by accessing to its IOService. */
|
---|
1231 | OSDictionary *pDictionary = IOService::serviceMatching("AppleUSBTCKeyboard");
|
---|
1232 | if (pDictionary)
|
---|
1233 | {
|
---|
1234 | OSIterator *pIter;
|
---|
1235 | IOUSBHIDDriver *pDriver;
|
---|
1236 |
|
---|
1237 | pIter = IOService::getMatchingServices(pDictionary);
|
---|
1238 | if (pIter)
|
---|
1239 | {
|
---|
1240 | while ((pDriver = (IOUSBHIDDriver *)pIter->getNextObject()))
|
---|
1241 | if (pDriver->IsPortSuspended())
|
---|
1242 | pDriver->SuspendPort(false, 0);
|
---|
1243 |
|
---|
1244 | pIter->release();
|
---|
1245 | }
|
---|
1246 | pDictionary->release();
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 |
|
---|
1251 | /**
|
---|
1252 | * Resume suspended keyboard devices (if any).
|
---|
1253 | */
|
---|
1254 | int VBOXCALL supdrvDarwinResumeSuspendedKbds(void)
|
---|
1255 | {
|
---|
1256 | supdrvDarwinResumeBuiltinKbd();
|
---|
1257 | supdrvDarwinResumeBluetoothKbd();
|
---|
1258 |
|
---|
1259 | return 0;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 |
|
---|
1263 | /**
|
---|
1264 | * Converts an IPRT error code to a darwin error code.
|
---|
1265 | *
|
---|
1266 | * @returns corresponding darwin error code.
|
---|
1267 | * @param rc IPRT status code.
|
---|
1268 | */
|
---|
1269 | static int VBoxDrvDarwinErr2DarwinErr(int rc)
|
---|
1270 | {
|
---|
1271 | switch (rc)
|
---|
1272 | {
|
---|
1273 | case VINF_SUCCESS: return 0;
|
---|
1274 | case VERR_GENERAL_FAILURE: return EACCES;
|
---|
1275 | case VERR_INVALID_PARAMETER: return EINVAL;
|
---|
1276 | case VERR_INVALID_MAGIC: return EILSEQ;
|
---|
1277 | case VERR_INVALID_HANDLE: return ENXIO;
|
---|
1278 | case VERR_INVALID_POINTER: return EFAULT;
|
---|
1279 | case VERR_LOCK_FAILED: return ENOLCK;
|
---|
1280 | case VERR_ALREADY_LOADED: return EEXIST;
|
---|
1281 | case VERR_PERMISSION_DENIED: return EPERM;
|
---|
1282 | case VERR_VERSION_MISMATCH: return ENOSYS;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | return EPERM;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 |
|
---|
1289 | RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
1290 | {
|
---|
1291 | va_list va;
|
---|
1292 | char szMsg[512];
|
---|
1293 |
|
---|
1294 | va_start(va, pszFormat);
|
---|
1295 | RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
1296 | va_end(va);
|
---|
1297 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
1298 |
|
---|
1299 | printf("%s", szMsg);
|
---|
1300 | return 0;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | * Returns configuration flags of the host kernel.
|
---|
1306 | */
|
---|
1307 | SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
|
---|
1308 | {
|
---|
1309 | return 0;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 |
|
---|
1313 | /*
|
---|
1314 | *
|
---|
1315 | * org_virtualbox_SupDrv
|
---|
1316 | *
|
---|
1317 | */
|
---|
1318 |
|
---|
1319 |
|
---|
1320 | /**
|
---|
1321 | * Initialize the object.
|
---|
1322 | */
|
---|
1323 | bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
|
---|
1324 | {
|
---|
1325 | LogFlow(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
|
---|
1326 | if (IOService::init(pDictionary))
|
---|
1327 | {
|
---|
1328 | /* init members. */
|
---|
1329 | return true;
|
---|
1330 | }
|
---|
1331 | return false;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Free the object.
|
---|
1337 | */
|
---|
1338 | void org_virtualbox_SupDrv::free(void)
|
---|
1339 | {
|
---|
1340 | LogFlow(("IOService::free([%p])\n", this));
|
---|
1341 | IOService::free();
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 |
|
---|
1345 | /**
|
---|
1346 | * Check if it's ok to start this service.
|
---|
1347 | * It's always ok by us, so it's up to IOService to decide really.
|
---|
1348 | */
|
---|
1349 | IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
|
---|
1350 | {
|
---|
1351 | LogFlow(("org_virtualbox_SupDrv::probe([%p])\n", this));
|
---|
1352 | return IOService::probe(pProvider, pi32Score);
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 |
|
---|
1356 | /**
|
---|
1357 | * Start this service.
|
---|
1358 | */
|
---|
1359 | bool org_virtualbox_SupDrv::start(IOService *pProvider)
|
---|
1360 | {
|
---|
1361 | LogFlow(("org_virtualbox_SupDrv::start([%p])\n", this));
|
---|
1362 |
|
---|
1363 | if (IOService::start(pProvider))
|
---|
1364 | {
|
---|
1365 | /* register the service. */
|
---|
1366 | registerService();
|
---|
1367 | return true;
|
---|
1368 | }
|
---|
1369 | return false;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 |
|
---|
1373 | /**
|
---|
1374 | * Stop this service.
|
---|
1375 | */
|
---|
1376 | void org_virtualbox_SupDrv::stop(IOService *pProvider)
|
---|
1377 | {
|
---|
1378 | LogFlow(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
|
---|
1379 | IOService::stop(pProvider);
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 |
|
---|
1383 | /**
|
---|
1384 | * Termination request.
|
---|
1385 | *
|
---|
1386 | * @return true if we're ok with shutting down now, false if we're not.
|
---|
1387 | * @param fOptions Flags.
|
---|
1388 | */
|
---|
1389 | bool org_virtualbox_SupDrv::terminate(IOOptionBits fOptions)
|
---|
1390 | {
|
---|
1391 | bool fRc;
|
---|
1392 | LogFlow(("org_virtualbox_SupDrv::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
|
---|
1393 | KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions));
|
---|
1394 | if ( KMOD_INFO_NAME.reference_count != 0
|
---|
1395 | || ASMAtomicUoReadS32(&g_cSessions))
|
---|
1396 | fRc = false;
|
---|
1397 | else
|
---|
1398 | fRc = IOService::terminate(fOptions);
|
---|
1399 | LogFlow(("org_virtualbox_SupDrv::terminate: returns %d\n", fRc));
|
---|
1400 | return fRc;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 |
|
---|
1404 | /*
|
---|
1405 | *
|
---|
1406 | * org_virtualbox_SupDrvClient
|
---|
1407 | *
|
---|
1408 | */
|
---|
1409 |
|
---|
1410 |
|
---|
1411 | /**
|
---|
1412 | * Initializer called when the client opens the service.
|
---|
1413 | */
|
---|
1414 | bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
|
---|
1415 | {
|
---|
1416 | LogFlow(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x) (cur pid=%d proc=%p)\n",
|
---|
1417 | this, OwningTask, pvSecurityId, u32Type, RTProcSelf(), RTR0ProcHandleSelf()));
|
---|
1418 | AssertMsg((RTR0PROCESS)OwningTask == RTR0ProcHandleSelf(), ("%p %p\n", OwningTask, RTR0ProcHandleSelf()));
|
---|
1419 |
|
---|
1420 | if (!OwningTask)
|
---|
1421 | return false;
|
---|
1422 |
|
---|
1423 | VBOX_RETRIEVE_CUR_PROC_NAME(pszProcName);
|
---|
1424 |
|
---|
1425 | if (u32Type != SUP_DARWIN_IOSERVICE_COOKIE)
|
---|
1426 | {
|
---|
1427 | LogRel(("org_virtualbox_SupDrvClient::initWithTask: Bad cookie %#x (%s)\n", u32Type, pszProcName));
|
---|
1428 | return false;
|
---|
1429 | }
|
---|
1430 | else
|
---|
1431 | LogRel(("org_virtualbox_SupDrvClient::initWithTask: Expected cookie %#x (%s)\n", u32Type, pszProcName));
|
---|
1432 |
|
---|
1433 | if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
|
---|
1434 | {
|
---|
1435 | m_Task = OwningTask;
|
---|
1436 | m_pSession = NULL;
|
---|
1437 | m_pProvider = NULL;
|
---|
1438 | return true;
|
---|
1439 | }
|
---|
1440 | return false;
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 |
|
---|
1444 | /**
|
---|
1445 | * Start the client service.
|
---|
1446 | */
|
---|
1447 | bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
|
---|
1448 | {
|
---|
1449 | LogFlow(("org_virtualbox_SupDrvClient::start([%p], %p) (cur pid=%d proc=%p)\n",
|
---|
1450 | this, pProvider, RTProcSelf(), RTR0ProcHandleSelf() ));
|
---|
1451 | AssertMsgReturn((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(),
|
---|
1452 | ("%p %p\n", m_Task, RTR0ProcHandleSelf()),
|
---|
1453 | false);
|
---|
1454 |
|
---|
1455 | if (IOUserClient::start(pProvider))
|
---|
1456 | {
|
---|
1457 | m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
|
---|
1458 | if (m_pProvider)
|
---|
1459 | {
|
---|
1460 | Assert(!m_pSession);
|
---|
1461 |
|
---|
1462 | /*
|
---|
1463 | * Create a new session.
|
---|
1464 | */
|
---|
1465 | int rc = supdrvCreateSession(&g_DevExt, true /* fUser */, false /*fUnrestricted*/, &m_pSession);
|
---|
1466 | if (RT_SUCCESS(rc))
|
---|
1467 | {
|
---|
1468 | m_pSession->fOpened = false;
|
---|
1469 | /* The Uid, Gid and fUnrestricted fields are set on open. */
|
---|
1470 |
|
---|
1471 | /*
|
---|
1472 | * Insert it into the hash table, checking that there isn't
|
---|
1473 | * already one for this process first. (One session per proc!)
|
---|
1474 | */
|
---|
1475 | unsigned iHash = SESSION_HASH(m_pSession->Process);
|
---|
1476 | RTSpinlockAcquire(g_Spinlock);
|
---|
1477 |
|
---|
1478 | PSUPDRVSESSION pCur = g_apSessionHashTab[iHash];
|
---|
1479 | while (pCur && pCur->Process != m_pSession->Process)
|
---|
1480 | pCur = pCur->pNextHash;
|
---|
1481 | if (!pCur)
|
---|
1482 | {
|
---|
1483 | m_pSession->pNextHash = g_apSessionHashTab[iHash];
|
---|
1484 | g_apSessionHashTab[iHash] = m_pSession;
|
---|
1485 | m_pSession->pvSupDrvClient = this;
|
---|
1486 | ASMAtomicIncS32(&g_cSessions);
|
---|
1487 | rc = VINF_SUCCESS;
|
---|
1488 | }
|
---|
1489 | else
|
---|
1490 | rc = VERR_ALREADY_LOADED;
|
---|
1491 |
|
---|
1492 | RTSpinlockRelease(g_Spinlock);
|
---|
1493 | if (RT_SUCCESS(rc))
|
---|
1494 | {
|
---|
1495 | Log(("org_virtualbox_SupDrvClient::start: created session %p for pid %d\n", m_pSession, (int)RTProcSelf()));
|
---|
1496 | return true;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | LogFlow(("org_virtualbox_SupDrvClient::start: already got a session for this process (%p)\n", pCur));
|
---|
1500 | supdrvSessionRelease(m_pSession);
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | m_pSession = NULL;
|
---|
1504 | LogFlow(("org_virtualbox_SupDrvClient::start: rc=%Rrc from supdrvCreateSession\n", rc));
|
---|
1505 | }
|
---|
1506 | else
|
---|
1507 | LogFlow(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
|
---|
1508 | }
|
---|
1509 | return false;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 |
|
---|
1513 | /**
|
---|
1514 | * Common worker for clientClose and VBoxDrvDarwinClose.
|
---|
1515 | */
|
---|
1516 | /* static */ void org_virtualbox_SupDrvClient::sessionClose(RTPROCESS Process)
|
---|
1517 | {
|
---|
1518 | /*
|
---|
1519 | * Find the session and remove it from the hash table.
|
---|
1520 | *
|
---|
1521 | * Note! Only one session per process. (Both start() and
|
---|
1522 | * VBoxDrvDarwinOpen makes sure this is so.)
|
---|
1523 | */
|
---|
1524 | const unsigned iHash = SESSION_HASH(Process);
|
---|
1525 | RTSpinlockAcquire(g_Spinlock);
|
---|
1526 | PSUPDRVSESSION pSession = g_apSessionHashTab[iHash];
|
---|
1527 | if (pSession)
|
---|
1528 | {
|
---|
1529 | if (pSession->Process == Process)
|
---|
1530 | {
|
---|
1531 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
1532 | pSession->pNextHash = NULL;
|
---|
1533 | ASMAtomicDecS32(&g_cSessions);
|
---|
1534 | }
|
---|
1535 | else
|
---|
1536 | {
|
---|
1537 | PSUPDRVSESSION pPrev = pSession;
|
---|
1538 | pSession = pSession->pNextHash;
|
---|
1539 | while (pSession)
|
---|
1540 | {
|
---|
1541 | if (pSession->Process == Process)
|
---|
1542 | {
|
---|
1543 | pPrev->pNextHash = pSession->pNextHash;
|
---|
1544 | pSession->pNextHash = NULL;
|
---|
1545 | ASMAtomicDecS32(&g_cSessions);
|
---|
1546 | break;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /* next */
|
---|
1550 | pPrev = pSession;
|
---|
1551 | pSession = pSession->pNextHash;
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 | }
|
---|
1555 | RTSpinlockRelease(g_Spinlock);
|
---|
1556 | if (!pSession)
|
---|
1557 | {
|
---|
1558 | Log(("SupDrvClient::sessionClose: pSession == NULL, pid=%d; freed already?\n", (int)Process));
|
---|
1559 | return;
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | /*
|
---|
1563 | * Remove it from the client object.
|
---|
1564 | */
|
---|
1565 | org_virtualbox_SupDrvClient *pThis = (org_virtualbox_SupDrvClient *)pSession->pvSupDrvClient;
|
---|
1566 | pSession->pvSupDrvClient = NULL;
|
---|
1567 | if (pThis)
|
---|
1568 | {
|
---|
1569 | Assert(pThis->m_pSession == pSession);
|
---|
1570 | pThis->m_pSession = NULL;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | /*
|
---|
1574 | * Close the session.
|
---|
1575 | */
|
---|
1576 | supdrvSessionRelease(pSession);
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 |
|
---|
1580 | /**
|
---|
1581 | * Client exits normally.
|
---|
1582 | */
|
---|
1583 | IOReturn org_virtualbox_SupDrvClient::clientClose(void)
|
---|
1584 | {
|
---|
1585 | LogFlow(("org_virtualbox_SupDrvClient::clientClose([%p]) (cur pid=%d proc=%p)\n", this, RTProcSelf(), RTR0ProcHandleSelf()));
|
---|
1586 | AssertMsg((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(), ("%p %p\n", m_Task, RTR0ProcHandleSelf()));
|
---|
1587 |
|
---|
1588 | /*
|
---|
1589 | * Clean up the session if it's still around.
|
---|
1590 | *
|
---|
1591 | * We cannot rely 100% on close, and in the case of a dead client
|
---|
1592 | * we'll end up hanging inside vm_map_remove() if we postpone it.
|
---|
1593 | */
|
---|
1594 | if (m_pSession)
|
---|
1595 | {
|
---|
1596 | sessionClose(RTProcSelf());
|
---|
1597 | Assert(!m_pSession);
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | m_pProvider = NULL;
|
---|
1601 | terminate();
|
---|
1602 |
|
---|
1603 | return kIOReturnSuccess;
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 |
|
---|
1607 | /**
|
---|
1608 | * The client exits abnormally / forgets to do cleanups. (logging)
|
---|
1609 | */
|
---|
1610 | IOReturn org_virtualbox_SupDrvClient::clientDied(void)
|
---|
1611 | {
|
---|
1612 | LogFlow(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
|
---|
1613 | this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
|
---|
1614 |
|
---|
1615 | /* IOUserClient::clientDied() calls clientClose, so we'll just do the work there. */
|
---|
1616 | return IOUserClient::clientDied();
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 |
|
---|
1620 | /**
|
---|
1621 | * Terminate the service (initiate the destruction). (logging)
|
---|
1622 | */
|
---|
1623 | bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
|
---|
1624 | {
|
---|
1625 | LogFlow(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
|
---|
1626 | return IOUserClient::terminate(fOptions);
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 |
|
---|
1630 | /**
|
---|
1631 | * The final stage of the client service destruction. (logging)
|
---|
1632 | */
|
---|
1633 | bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
|
---|
1634 | {
|
---|
1635 | LogFlow(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
|
---|
1636 | return IOUserClient::finalize(fOptions);
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 |
|
---|
1640 | /**
|
---|
1641 | * Stop the client service. (logging)
|
---|
1642 | */
|
---|
1643 | void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
|
---|
1644 | {
|
---|
1645 | LogFlow(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
|
---|
1646 | IOUserClient::stop(pProvider);
|
---|
1647 | }
|
---|
1648 |
|
---|