1 | /* $Rev: 57315 $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - The VirtualBox Support Driver - Linux specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
31 | #include "../SUPDrvInternal.h"
|
---|
32 | #include "the-linux-kernel.h"
|
---|
33 | #include "version-generated.h"
|
---|
34 | #include "product-generated.h"
|
---|
35 |
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 | #include <iprt/semaphore.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/process.h>
|
---|
41 | #include <VBox/err.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <VBox/log.h>
|
---|
44 | #include <iprt/mp.h>
|
---|
45 |
|
---|
46 | /** @todo figure out the exact version number */
|
---|
47 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
|
---|
48 | # include <iprt/power.h>
|
---|
49 | # define VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #include <linux/sched.h>
|
---|
53 | #ifdef CONFIG_DEVFS_FS
|
---|
54 | # include <linux/devfs_fs_kernel.h>
|
---|
55 | #endif
|
---|
56 | #ifdef CONFIG_VBOXDRV_AS_MISC
|
---|
57 | # include <linux/miscdevice.h>
|
---|
58 | #endif
|
---|
59 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
60 | # include <linux/platform_device.h>
|
---|
61 | #endif
|
---|
62 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) && defined(SUPDRV_WITH_MSR_PROBER)
|
---|
63 | # define SUPDRV_LINUX_HAS_SAFE_MSR_API
|
---|
64 | # include <asm/msr.h>
|
---|
65 | #endif
|
---|
66 | #include <iprt/asm-amd64-x86.h>
|
---|
67 |
|
---|
68 |
|
---|
69 | /*******************************************************************************
|
---|
70 | * Defined Constants And Macros *
|
---|
71 | *******************************************************************************/
|
---|
72 | /* check kernel version */
|
---|
73 | # ifndef SUPDRV_AGNOSTIC
|
---|
74 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
75 | # error Unsupported kernel version!
|
---|
76 | # endif
|
---|
77 | # endif
|
---|
78 |
|
---|
79 | /* devfs defines */
|
---|
80 | #if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
|
---|
81 | # ifdef VBOX_WITH_HARDENING
|
---|
82 | # define VBOX_DEV_FMASK (S_IWUSR | S_IRUSR)
|
---|
83 | # else
|
---|
84 | # define VBOX_DEV_FMASK (S_IRUGO | S_IWUGO)
|
---|
85 | # endif
|
---|
86 | #endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
|
---|
87 |
|
---|
88 | #ifdef CONFIG_X86_HIGH_ENTRY
|
---|
89 | # error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
|
---|
90 | #endif
|
---|
91 |
|
---|
92 | /* We cannot include x86.h, so we copy the defines we need here: */
|
---|
93 | #define X86_EFL_IF RT_BIT(9)
|
---|
94 | #define X86_EFL_AC RT_BIT(18)
|
---|
95 | #define X86_EFL_DF RT_BIT(10)
|
---|
96 | #define X86_EFL_IOPL (RT_BIT(12) | RT_BIT(13))
|
---|
97 |
|
---|
98 | /* To include the version number of VirtualBox into kernel backtraces: */
|
---|
99 | #define VBoxDrvLinuxVersion RT_CONCAT3(RT_CONCAT(VBOX_VERSION_MAJOR, _), \
|
---|
100 | RT_CONCAT(VBOX_VERSION_MINOR, _), \
|
---|
101 | VBOX_VERSION_BUILD)
|
---|
102 | #define VBoxDrvLinuxIOCtl RT_CONCAT(VBoxDrvLinuxIOCtl_,VBoxDrvLinuxVersion)
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | /*******************************************************************************
|
---|
107 | * Internal Functions *
|
---|
108 | *******************************************************************************/
|
---|
109 | static int VBoxDrvLinuxInit(void);
|
---|
110 | static void VBoxDrvLinuxUnload(void);
|
---|
111 | static int VBoxDrvLinuxCreateSys(struct inode *pInode, struct file *pFilp);
|
---|
112 | static int VBoxDrvLinuxCreateUsr(struct inode *pInode, struct file *pFilp);
|
---|
113 | static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp);
|
---|
114 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
115 | static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
|
---|
116 | #else
|
---|
117 | static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
|
---|
118 | #endif
|
---|
119 | static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PSUPDRVSESSION pSession);
|
---|
120 | static int VBoxDrvLinuxErr2LinuxErr(int);
|
---|
121 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
122 | static int VBoxDrvProbe(struct platform_device *pDev);
|
---|
123 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
|
---|
124 | static int VBoxDrvSuspend(struct device *pDev);
|
---|
125 | static int VBoxDrvResume(struct device *pDev);
|
---|
126 | # else
|
---|
127 | static int VBoxDrvSuspend(struct platform_device *pDev, pm_message_t State);
|
---|
128 | static int VBoxDrvResume(struct platform_device *pDev);
|
---|
129 | # endif
|
---|
130 | static void VBoxDevRelease(struct device *pDev);
|
---|
131 | #endif
|
---|
132 |
|
---|
133 |
|
---|
134 | /*******************************************************************************
|
---|
135 | * Global Variables *
|
---|
136 | *******************************************************************************/
|
---|
137 | /**
|
---|
138 | * Device extention & session data association structure.
|
---|
139 | */
|
---|
140 | static SUPDRVDEVEXT g_DevExt;
|
---|
141 |
|
---|
142 | #ifndef CONFIG_VBOXDRV_AS_MISC
|
---|
143 | /** Module major number for vboxdrv. */
|
---|
144 | #define DEVICE_MAJOR_SYS 234
|
---|
145 | /** Saved major device number for vboxdrv. */
|
---|
146 | static int g_iModuleMajorSys;
|
---|
147 | /** Module major number for vboxdrvu. */
|
---|
148 | #define DEVICE_MAJOR_USR 235
|
---|
149 | /** Saved major device number for vboxdrvu. */
|
---|
150 | static int g_iModuleMajorUsr;
|
---|
151 | #endif /* !CONFIG_VBOXDRV_AS_MISC */
|
---|
152 |
|
---|
153 | /** Module parameter.
|
---|
154 | * Not prefixed because the name is used by macros and the end of this file. */
|
---|
155 | static int force_async_tsc = 0;
|
---|
156 |
|
---|
157 | /** The system device name. */
|
---|
158 | #define DEVICE_NAME_SYS "vboxdrv"
|
---|
159 | /** The user device name. */
|
---|
160 | #define DEVICE_NAME_USR "vboxdrvu"
|
---|
161 |
|
---|
162 | #if (defined(RT_ARCH_AMD64) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)) || defined(VBOX_WITH_TEXT_MODMEM_HACK)
|
---|
163 | /**
|
---|
164 | * Memory for the executable memory heap (in IPRT).
|
---|
165 | */
|
---|
166 | # ifdef DEBUG
|
---|
167 | # define EXEC_MEMORY_SIZE 6291456 /* 6 MB */
|
---|
168 | # else
|
---|
169 | # define EXEC_MEMORY_SIZE 1572864 /* 1.5 MB */
|
---|
170 | # endif
|
---|
171 | extern uint8_t g_abExecMemory[EXEC_MEMORY_SIZE];
|
---|
172 | # ifndef VBOX_WITH_TEXT_MODMEM_HACK
|
---|
173 | __asm__(".section execmemory, \"awx\", @progbits\n\t"
|
---|
174 | ".align 32\n\t"
|
---|
175 | ".globl g_abExecMemory\n"
|
---|
176 | "g_abExecMemory:\n\t"
|
---|
177 | ".zero " RT_XSTR(EXEC_MEMORY_SIZE) "\n\t"
|
---|
178 | ".type g_abExecMemory, @object\n\t"
|
---|
179 | ".size g_abExecMemory, " RT_XSTR(EXEC_MEMORY_SIZE) "\n\t"
|
---|
180 | ".text\n\t");
|
---|
181 | # else
|
---|
182 | __asm__(".text\n\t"
|
---|
183 | ".align 4096\n\t"
|
---|
184 | ".globl g_abExecMemory\n"
|
---|
185 | "g_abExecMemory:\n\t"
|
---|
186 | ".zero " RT_XSTR(EXEC_MEMORY_SIZE) "\n\t"
|
---|
187 | ".type g_abExecMemory, @object\n\t"
|
---|
188 | ".size g_abExecMemory, " RT_XSTR(EXEC_MEMORY_SIZE) "\n\t"
|
---|
189 | ".text\n\t");
|
---|
190 | # endif
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | /** The file_operations structure. */
|
---|
194 | static struct file_operations gFileOpsVBoxDrvSys =
|
---|
195 | {
|
---|
196 | owner: THIS_MODULE,
|
---|
197 | open: VBoxDrvLinuxCreateSys,
|
---|
198 | release: VBoxDrvLinuxClose,
|
---|
199 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
200 | unlocked_ioctl: VBoxDrvLinuxIOCtl,
|
---|
201 | #else
|
---|
202 | ioctl: VBoxDrvLinuxIOCtl,
|
---|
203 | #endif
|
---|
204 | };
|
---|
205 |
|
---|
206 | /** The file_operations structure. */
|
---|
207 | static struct file_operations gFileOpsVBoxDrvUsr =
|
---|
208 | {
|
---|
209 | owner: THIS_MODULE,
|
---|
210 | open: VBoxDrvLinuxCreateUsr,
|
---|
211 | release: VBoxDrvLinuxClose,
|
---|
212 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
213 | unlocked_ioctl: VBoxDrvLinuxIOCtl,
|
---|
214 | #else
|
---|
215 | ioctl: VBoxDrvLinuxIOCtl,
|
---|
216 | #endif
|
---|
217 | };
|
---|
218 |
|
---|
219 | #ifdef CONFIG_VBOXDRV_AS_MISC
|
---|
220 | /** The miscdevice structure for vboxdrv. */
|
---|
221 | static struct miscdevice gMiscDeviceSys =
|
---|
222 | {
|
---|
223 | minor: MISC_DYNAMIC_MINOR,
|
---|
224 | name: DEVICE_NAME_SYS,
|
---|
225 | fops: &gFileOpsVBoxDrvSys,
|
---|
226 | # if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
|
---|
227 | devfs_name: DEVICE_NAME_SYS,
|
---|
228 | # endif
|
---|
229 | };
|
---|
230 | /** The miscdevice structure for vboxdrvu. */
|
---|
231 | static struct miscdevice gMiscDeviceUsr =
|
---|
232 | {
|
---|
233 | minor: MISC_DYNAMIC_MINOR,
|
---|
234 | name: DEVICE_NAME_USR,
|
---|
235 | fops: &gFileOpsVBoxDrvUsr,
|
---|
236 | # if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
|
---|
237 | devfs_name: DEVICE_NAME_USR,
|
---|
238 | # endif
|
---|
239 | };
|
---|
240 | #endif
|
---|
241 |
|
---|
242 |
|
---|
243 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
244 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
|
---|
245 | static struct dev_pm_ops gPlatformPMOps =
|
---|
246 | {
|
---|
247 | .suspend = VBoxDrvSuspend, /* before entering deep sleep */
|
---|
248 | .resume = VBoxDrvResume, /* after wakeup from deep sleep */
|
---|
249 | .freeze = VBoxDrvSuspend, /* before creating hibernation image */
|
---|
250 | .restore = VBoxDrvResume, /* after waking up from hibernation */
|
---|
251 | };
|
---|
252 | # endif
|
---|
253 |
|
---|
254 | static struct platform_driver gPlatformDriver =
|
---|
255 | {
|
---|
256 | .probe = VBoxDrvProbe,
|
---|
257 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
|
---|
258 | .suspend = VBoxDrvSuspend,
|
---|
259 | .resume = VBoxDrvResume,
|
---|
260 | # endif
|
---|
261 | /** @todo .shutdown? */
|
---|
262 | .driver =
|
---|
263 | {
|
---|
264 | .name = "vboxdrv",
|
---|
265 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
|
---|
266 | .pm = &gPlatformPMOps,
|
---|
267 | # endif
|
---|
268 | }
|
---|
269 | };
|
---|
270 |
|
---|
271 | static struct platform_device gPlatformDevice =
|
---|
272 | {
|
---|
273 | .name = "vboxdrv",
|
---|
274 | .dev =
|
---|
275 | {
|
---|
276 | .release = VBoxDevRelease
|
---|
277 | }
|
---|
278 | };
|
---|
279 | #endif /* VBOX_WITH_SUSPEND_NOTIFICATION */
|
---|
280 |
|
---|
281 |
|
---|
282 | DECLINLINE(RTUID) vboxdrvLinuxUid(void)
|
---|
283 | {
|
---|
284 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
285 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
286 | return from_kuid(current_user_ns(), current->cred->uid);
|
---|
287 | # else
|
---|
288 | return current->cred->uid;
|
---|
289 | # endif
|
---|
290 | #else
|
---|
291 | return current->uid;
|
---|
292 | #endif
|
---|
293 | }
|
---|
294 |
|
---|
295 | DECLINLINE(RTGID) vboxdrvLinuxGid(void)
|
---|
296 | {
|
---|
297 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
298 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
299 | return from_kgid(current_user_ns(), current->cred->gid);
|
---|
300 | # else
|
---|
301 | return current->cred->gid;
|
---|
302 | # endif
|
---|
303 | #else
|
---|
304 | return current->gid;
|
---|
305 | #endif
|
---|
306 | }
|
---|
307 |
|
---|
308 | DECLINLINE(RTUID) vboxdrvLinuxEuid(void)
|
---|
309 | {
|
---|
310 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
|
---|
311 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
312 | return from_kuid(current_user_ns(), current->cred->euid);
|
---|
313 | # else
|
---|
314 | return current->cred->euid;
|
---|
315 | # endif
|
---|
316 | #else
|
---|
317 | return current->euid;
|
---|
318 | #endif
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Initialize module.
|
---|
323 | *
|
---|
324 | * @returns appropriate status code.
|
---|
325 | */
|
---|
326 | static int __init VBoxDrvLinuxInit(void)
|
---|
327 | {
|
---|
328 | int rc;
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Check for synchronous/asynchronous TSC mode.
|
---|
332 | */
|
---|
333 | printk(KERN_DEBUG "vboxdrv: Found %u processor cores\n", (unsigned)RTMpGetOnlineCount());
|
---|
334 | #ifdef CONFIG_VBOXDRV_AS_MISC
|
---|
335 | rc = misc_register(&gMiscDeviceSys);
|
---|
336 | if (rc)
|
---|
337 | {
|
---|
338 | printk(KERN_ERR "vboxdrv: Can't register system misc device! rc=%d\n", rc);
|
---|
339 | return rc;
|
---|
340 | }
|
---|
341 | rc = misc_register(&gMiscDeviceUsr);
|
---|
342 | if (rc)
|
---|
343 | {
|
---|
344 | printk(KERN_ERR "vboxdrv: Can't register user misc device! rc=%d\n", rc);
|
---|
345 | misc_deregister(&gMiscDeviceSys);
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 | #else /* !CONFIG_VBOXDRV_AS_MISC */
|
---|
349 | /*
|
---|
350 | * Register character devices and save the returned major numbers.
|
---|
351 | */
|
---|
352 | /* /dev/vboxdrv */
|
---|
353 | g_iModuleMajorSys = DEVICE_MAJOR_SYS;
|
---|
354 | rc = register_chrdev((dev_t)g_iModuleMajorSys, DEVICE_NAME_SYS, &gFileOpsVBoxDrvSys);
|
---|
355 | if (rc < 0)
|
---|
356 | {
|
---|
357 | Log(("register_chrdev() failed with rc=%#x for vboxdrv!\n", rc));
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 | if (DEVICE_MAJOR_SYS != 0)
|
---|
361 | g_iModuleMajorSys = DEVICE_MAJOR_SYS;
|
---|
362 | else
|
---|
363 | g_iModuleMajorSys = rc;
|
---|
364 |
|
---|
365 | /* /dev/vboxdrvu */
|
---|
366 | /** @todo Use a minor number of this bugger (not sure if this code is used
|
---|
367 | * though, so not bothering right now.) */
|
---|
368 | g_iModuleMajorUsr = DEVICE_MAJOR_USR;
|
---|
369 | rc = register_chrdev((dev_t)g_iModuleMajorUsr, DEVICE_NAME_USR, &gFileOpsVBoxDrvUsr);
|
---|
370 | if (rc < 0)
|
---|
371 | {
|
---|
372 | Log(("register_chrdev() failed with rc=%#x for vboxdrv!\n", rc));
|
---|
373 | return rc;
|
---|
374 | }
|
---|
375 | if (DEVICE_MAJOR_USR != 0)
|
---|
376 | g_iModuleMajorUsr = DEVICE_MAJOR_USR;
|
---|
377 | else
|
---|
378 | g_iModuleMajorUsr = rc;
|
---|
379 | rc = 0;
|
---|
380 |
|
---|
381 | # ifdef CONFIG_DEVFS_FS
|
---|
382 | /*
|
---|
383 | * Register a device entry
|
---|
384 | */
|
---|
385 | if ( devfs_mk_cdev(MKDEV(DEVICE_MAJOR_SYS, 0), S_IFCHR | VBOX_DEV_FMASK, DEVICE_NAME_SYS) != 0
|
---|
386 | || devfs_mk_cdev(MKDEV(DEVICE_MAJOR_USR, 0), S_IFCHR | VBOX_DEV_FMASK, DEVICE_NAME_USR) != 0)
|
---|
387 | {
|
---|
388 | Log(("devfs_register failed!\n"));
|
---|
389 | rc = -EINVAL;
|
---|
390 | }
|
---|
391 | # endif
|
---|
392 | #endif /* !CONFIG_VBOXDRV_AS_MISC */
|
---|
393 | if (!rc)
|
---|
394 | {
|
---|
395 | /*
|
---|
396 | * Initialize the runtime.
|
---|
397 | * On AMD64 we'll have to donate the high rwx memory block to the exec allocator.
|
---|
398 | */
|
---|
399 | rc = RTR0Init(0);
|
---|
400 | if (RT_SUCCESS(rc))
|
---|
401 | {
|
---|
402 | #if (defined(RT_ARCH_AMD64) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)) || defined(VBOX_WITH_TEXT_MODMEM_HACK)
|
---|
403 | # ifdef VBOX_WITH_TEXT_MODMEM_HACK
|
---|
404 | set_memory_x(&g_abExecMemory[0], sizeof(g_abExecMemory) / PAGE_SIZE);
|
---|
405 | set_memory_rw(&g_abExecMemory[0], sizeof(g_abExecMemory) / PAGE_SIZE);
|
---|
406 | # endif
|
---|
407 | rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
|
---|
408 | printk(KERN_DEBUG "VBoxDrv: dbg - g_abExecMemory=%p\n", (void *)&g_abExecMemory[0]);
|
---|
409 | #endif
|
---|
410 | Log(("VBoxDrv::ModuleInit\n"));
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * Initialize the device extension.
|
---|
414 | */
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
|
---|
417 | if (RT_SUCCESS(rc))
|
---|
418 | {
|
---|
419 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
420 | rc = platform_driver_register(&gPlatformDriver);
|
---|
421 | if (rc == 0)
|
---|
422 | {
|
---|
423 | rc = platform_device_register(&gPlatformDevice);
|
---|
424 | if (rc == 0)
|
---|
425 | #endif
|
---|
426 | {
|
---|
427 | printk(KERN_INFO "vboxdrv: TSC mode is %s, tentative frequency %llu Hz\n",
|
---|
428 | SUPGetGIPModeName(g_DevExt.pGip), g_DevExt.pGip->u64CpuHz);
|
---|
429 | LogFlow(("VBoxDrv::ModuleInit returning %#x\n", rc));
|
---|
430 | printk(KERN_DEBUG "vboxdrv: Successfully loaded version "
|
---|
431 | VBOX_VERSION_STRING " (interface " RT_XSTR(SUPDRV_IOC_VERSION) ")\n");
|
---|
432 | return rc;
|
---|
433 | }
|
---|
434 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
435 | else
|
---|
436 | platform_driver_unregister(&gPlatformDriver);
|
---|
437 | }
|
---|
438 | #endif
|
---|
439 | }
|
---|
440 |
|
---|
441 | rc = -EINVAL;
|
---|
442 | RTR0TermForced();
|
---|
443 | }
|
---|
444 | else
|
---|
445 | rc = -EINVAL;
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Failed, cleanup and return the error code.
|
---|
449 | */
|
---|
450 | #if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
|
---|
451 | devfs_remove(DEVICE_NAME_SYS);
|
---|
452 | devfs_remove(DEVICE_NAME_USR);
|
---|
453 | #endif
|
---|
454 | }
|
---|
455 | #ifdef CONFIG_VBOXDRV_AS_MISC
|
---|
456 | misc_deregister(&gMiscDeviceSys);
|
---|
457 | misc_deregister(&gMiscDeviceUsr);
|
---|
458 | Log(("VBoxDrv::ModuleInit returning %#x (minor:%d & %d)\n", rc, gMiscDeviceSys.minor, gMiscDeviceUsr.minor));
|
---|
459 | #else
|
---|
460 | unregister_chrdev(g_iModuleMajorUsr, DEVICE_NAME_USR);
|
---|
461 | unregister_chrdev(g_iModuleMajorSys, DEVICE_NAME_SYS);
|
---|
462 | Log(("VBoxDrv::ModuleInit returning %#x (major:%d & %d)\n", rc, g_iModuleMajorSys, g_iModuleMajorUsr));
|
---|
463 | #endif
|
---|
464 | return rc;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Unload the module.
|
---|
470 | */
|
---|
471 | static void __exit VBoxDrvLinuxUnload(void)
|
---|
472 | {
|
---|
473 | int rc;
|
---|
474 | Log(("VBoxDrvLinuxUnload\n"));
|
---|
475 | NOREF(rc);
|
---|
476 |
|
---|
477 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
478 | platform_device_unregister(&gPlatformDevice);
|
---|
479 | platform_driver_unregister(&gPlatformDriver);
|
---|
480 | #endif
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * I Don't think it's possible to unload a driver which processes have
|
---|
484 | * opened, at least we'll blindly assume that here.
|
---|
485 | */
|
---|
486 | #ifdef CONFIG_VBOXDRV_AS_MISC
|
---|
487 | rc = misc_deregister(&gMiscDeviceUsr);
|
---|
488 | if (rc < 0)
|
---|
489 | {
|
---|
490 | Log(("misc_deregister failed with rc=%#x on vboxdrvu\n", rc));
|
---|
491 | }
|
---|
492 | rc = misc_deregister(&gMiscDeviceSys);
|
---|
493 | if (rc < 0)
|
---|
494 | {
|
---|
495 | Log(("misc_deregister failed with rc=%#x on vboxdrv\n", rc));
|
---|
496 | }
|
---|
497 | #else /* !CONFIG_VBOXDRV_AS_MISC */
|
---|
498 | # ifdef CONFIG_DEVFS_FS
|
---|
499 | /*
|
---|
500 | * Unregister a device entry
|
---|
501 | */
|
---|
502 | devfs_remove(DEVICE_NAME_USR);
|
---|
503 | devfs_remove(DEVICE_NAME_SYS);
|
---|
504 | # endif /* devfs */
|
---|
505 | unregister_chrdev(g_iModuleMajorUsr, DEVICE_NAME_USR);
|
---|
506 | unregister_chrdev(g_iModuleMajorSys, DEVICE_NAME_SYS);
|
---|
507 | #endif /* !CONFIG_VBOXDRV_AS_MISC */
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * Destroy GIP, delete the device extension and terminate IPRT.
|
---|
511 | */
|
---|
512 | supdrvDeleteDevExt(&g_DevExt);
|
---|
513 | RTR0TermForced();
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Common open code.
|
---|
519 | *
|
---|
520 | * @param pInode Pointer to inode info structure.
|
---|
521 | * @param pFilp Associated file pointer.
|
---|
522 | * @param fUnrestricted Indicates which device node which was opened.
|
---|
523 | */
|
---|
524 | static int vboxdrvLinuxCreateCommon(struct inode *pInode, struct file *pFilp, bool fUnrestricted)
|
---|
525 | {
|
---|
526 | int rc;
|
---|
527 | PSUPDRVSESSION pSession;
|
---|
528 | Log(("VBoxDrvLinuxCreate: pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
|
---|
529 |
|
---|
530 | #ifdef VBOX_WITH_HARDENING
|
---|
531 | /*
|
---|
532 | * Only root is allowed to access the unrestricted device, enforce it!
|
---|
533 | */
|
---|
534 | if ( fUnrestricted
|
---|
535 | && vboxdrvLinuxEuid() != 0 /* root */ )
|
---|
536 | {
|
---|
537 | Log(("VBoxDrvLinuxCreate: euid=%d, expected 0 (root)\n", vboxdrvLinuxEuid()));
|
---|
538 | return -EPERM;
|
---|
539 | }
|
---|
540 | #endif /* VBOX_WITH_HARDENING */
|
---|
541 |
|
---|
542 | /*
|
---|
543 | * Call common code for the rest.
|
---|
544 | */
|
---|
545 | rc = supdrvCreateSession(&g_DevExt, true /* fUser */, fUnrestricted, &pSession);
|
---|
546 | if (!rc)
|
---|
547 | {
|
---|
548 | pSession->Uid = vboxdrvLinuxUid();
|
---|
549 | pSession->Gid = vboxdrvLinuxGid();
|
---|
550 | }
|
---|
551 |
|
---|
552 | pFilp->private_data = pSession;
|
---|
553 |
|
---|
554 | Log(("VBoxDrvLinuxCreate: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
|
---|
555 | &g_DevExt, pSession, rc, VBoxDrvLinuxErr2LinuxErr(rc),
|
---|
556 | RTProcSelf(), current->pid, current->comm));
|
---|
557 | return VBoxDrvLinuxErr2LinuxErr(rc);
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | /** /dev/vboxdrv. */
|
---|
562 | static int VBoxDrvLinuxCreateSys(struct inode *pInode, struct file *pFilp)
|
---|
563 | {
|
---|
564 | return vboxdrvLinuxCreateCommon(pInode, pFilp, true);
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | /** /dev/vboxdrvu. */
|
---|
569 | static int VBoxDrvLinuxCreateUsr(struct inode *pInode, struct file *pFilp)
|
---|
570 | {
|
---|
571 | return vboxdrvLinuxCreateCommon(pInode, pFilp, false);
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Close device.
|
---|
577 | *
|
---|
578 | * @param pInode Pointer to inode info structure.
|
---|
579 | * @param pFilp Associated file pointer.
|
---|
580 | */
|
---|
581 | static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp)
|
---|
582 | {
|
---|
583 | Log(("VBoxDrvLinuxClose: pFilp=%p pSession=%p pid=%d/%d %s\n",
|
---|
584 | pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
|
---|
585 | supdrvSessionRelease((PSUPDRVSESSION)pFilp->private_data);
|
---|
586 | pFilp->private_data = NULL;
|
---|
587 | return 0;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | #ifdef VBOX_WITH_SUSPEND_NOTIFICATION
|
---|
592 | /**
|
---|
593 | * Dummy device release function. We have to provide this function,
|
---|
594 | * otherwise the kernel will complain.
|
---|
595 | *
|
---|
596 | * @param pDev Pointer to the platform device.
|
---|
597 | */
|
---|
598 | static void VBoxDevRelease(struct device *pDev)
|
---|
599 | {
|
---|
600 | }
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Dummy probe function.
|
---|
604 | *
|
---|
605 | * @param pDev Pointer to the platform device.
|
---|
606 | */
|
---|
607 | static int VBoxDrvProbe(struct platform_device *pDev)
|
---|
608 | {
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Suspend callback.
|
---|
614 | * @param pDev Pointer to the platform device.
|
---|
615 | * @param State message type, see Documentation/power/devices.txt.
|
---|
616 | */
|
---|
617 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
|
---|
618 | static int VBoxDrvSuspend(struct device *pDev)
|
---|
619 | # else
|
---|
620 | static int VBoxDrvSuspend(struct platform_device *pDev, pm_message_t State)
|
---|
621 | # endif
|
---|
622 | {
|
---|
623 | RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
|
---|
624 | return 0;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * Resume callback.
|
---|
629 | *
|
---|
630 | * @param pDev Pointer to the platform device.
|
---|
631 | */
|
---|
632 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
|
---|
633 | static int VBoxDrvResume(struct device *pDev)
|
---|
634 | # else
|
---|
635 | static int VBoxDrvResume(struct platform_device *pDev)
|
---|
636 | # endif
|
---|
637 | {
|
---|
638 | RTPowerSignalEvent(RTPOWEREVENT_RESUME);
|
---|
639 | return 0;
|
---|
640 | }
|
---|
641 | #endif /* VBOX_WITH_SUSPEND_NOTIFICATION */
|
---|
642 |
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Device I/O Control entry point.
|
---|
646 | *
|
---|
647 | * @param pFilp Associated file pointer.
|
---|
648 | * @param uCmd The function specified to ioctl().
|
---|
649 | * @param ulArg The argument specified to ioctl().
|
---|
650 | */
|
---|
651 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
652 | static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
|
---|
653 | #else
|
---|
654 | static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
|
---|
655 | #endif
|
---|
656 | {
|
---|
657 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pFilp->private_data;
|
---|
658 | int rc;
|
---|
659 | #if defined(VBOX_STRICT) || defined(VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV)
|
---|
660 | RTCCUINTREG fSavedEfl;
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Refuse all I/O control calls if we've ever detected EFLAGS.AC being cleared.
|
---|
664 | *
|
---|
665 | * This isn't a problem, as there is absolutely nothing in the kernel context that
|
---|
666 | * depend on user context triggering cleanups. That would be pretty wild, right?
|
---|
667 | */
|
---|
668 | if (RT_UNLIKELY(g_DevExt.cBadContextCalls > 0))
|
---|
669 | {
|
---|
670 | SUPR0Printf("VBoxDrvLinuxIOCtl: EFLAGS.AC=0 detected %u times, refusing all I/O controls!\n", g_DevExt.cBadContextCalls);
|
---|
671 | return ESPIPE;
|
---|
672 | }
|
---|
673 |
|
---|
674 | fSavedEfl = ASMAddFlags(X86_EFL_AC);
|
---|
675 | # else
|
---|
676 | stac();
|
---|
677 | # endif
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
681 | * the session and iCmd, and only returns a VBox status code.
|
---|
682 | */
|
---|
683 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
684 | if (RT_LIKELY( ( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
685 | || uCmd == SUP_IOCTL_FAST_DO_HM_RUN
|
---|
686 | || uCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
687 | && pSession->fUnrestricted == true))
|
---|
688 | rc = supdrvIOCtlFast(uCmd, ulArg, &g_DevExt, pSession);
|
---|
689 | else
|
---|
690 | rc = VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg, pSession);
|
---|
691 | #else /* !HAVE_UNLOCKED_IOCTL */
|
---|
692 | unlock_kernel();
|
---|
693 | if (RT_LIKELY( ( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
694 | || uCmd == SUP_IOCTL_FAST_DO_HM_RUN
|
---|
695 | || uCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
696 | && pSession->fUnrestricted == true))
|
---|
697 | rc = supdrvIOCtlFast(uCmd, ulArg, &g_DevExt, pSession);
|
---|
698 | else
|
---|
699 | rc = VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg, pSession);
|
---|
700 | lock_kernel();
|
---|
701 | #endif /* !HAVE_UNLOCKED_IOCTL */
|
---|
702 |
|
---|
703 | #if defined(VBOX_STRICT) || defined(VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV)
|
---|
704 | /*
|
---|
705 | * Before we restore AC and the rest of EFLAGS, check if the IOCtl handler code
|
---|
706 | * accidentially modified it or some other important flag.
|
---|
707 | */
|
---|
708 | if (RT_UNLIKELY( (ASMGetFlags() & (X86_EFL_AC | X86_EFL_IF | X86_EFL_DF | X86_EFL_IOPL))
|
---|
709 | != ((fSavedEfl & (X86_EFL_AC | X86_EFL_IF | X86_EFL_DF | X86_EFL_IOPL)) | X86_EFL_AC) ))
|
---|
710 | {
|
---|
711 | char szTmp[48];
|
---|
712 | RTStrPrintf(szTmp, sizeof(szTmp), "uCmd=%#x: %#x->%#x!", _IOC_NR(uCmd), (uint32_t)fSavedEfl, (uint32_t)ASMGetFlags());
|
---|
713 | supdrvBadContext(&g_DevExt, "SUPDrv-linux.c", __LINE__, szTmp);
|
---|
714 | }
|
---|
715 | ASMSetFlags(fSavedEfl);
|
---|
716 | #else
|
---|
717 | clac();
|
---|
718 | #endif
|
---|
719 | return rc;
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | /**
|
---|
724 | * Device I/O Control entry point.
|
---|
725 | *
|
---|
726 | * @param pFilp Associated file pointer.
|
---|
727 | * @param uCmd The function specified to ioctl().
|
---|
728 | * @param ulArg The argument specified to ioctl().
|
---|
729 | * @param pSession The session instance.
|
---|
730 | */
|
---|
731 | static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PSUPDRVSESSION pSession)
|
---|
732 | {
|
---|
733 | int rc;
|
---|
734 | SUPREQHDR Hdr;
|
---|
735 | PSUPREQHDR pHdr;
|
---|
736 | uint32_t cbBuf;
|
---|
737 |
|
---|
738 | Log6(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
|
---|
739 |
|
---|
740 | /*
|
---|
741 | * Read the header.
|
---|
742 | */
|
---|
743 | if (RT_FAILURE(RTR0MemUserCopyFrom(&Hdr, ulArg, sizeof(Hdr))))
|
---|
744 | {
|
---|
745 | Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x\n", ulArg, uCmd));
|
---|
746 | return -EFAULT;
|
---|
747 | }
|
---|
748 | if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
749 | {
|
---|
750 | Log(("VBoxDrvLinuxIOCtl: bad header magic %#x; uCmd=%#x\n", Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, uCmd));
|
---|
751 | return -EINVAL;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * Buffer the request.
|
---|
756 | */
|
---|
757 | cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
|
---|
758 | if (RT_UNLIKELY(cbBuf > _1M*16))
|
---|
759 | {
|
---|
760 | Log(("VBoxDrvLinuxIOCtl: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
|
---|
761 | return -E2BIG;
|
---|
762 | }
|
---|
763 | if (RT_UNLIKELY(_IOC_SIZE(uCmd) ? cbBuf != _IOC_SIZE(uCmd) : Hdr.cbIn < sizeof(Hdr)))
|
---|
764 | {
|
---|
765 | Log(("VBoxDrvLinuxIOCtl: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
|
---|
766 | return -EINVAL;
|
---|
767 | }
|
---|
768 | pHdr = RTMemAlloc(cbBuf);
|
---|
769 | if (RT_UNLIKELY(!pHdr))
|
---|
770 | {
|
---|
771 | OSDBGPRINT(("VBoxDrvLinuxIOCtl: failed to allocate buffer of %d bytes for uCmd=%#x\n", cbBuf, uCmd));
|
---|
772 | return -ENOMEM;
|
---|
773 | }
|
---|
774 | if (RT_FAILURE(RTR0MemUserCopyFrom(pHdr, ulArg, Hdr.cbIn)))
|
---|
775 | {
|
---|
776 | Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx, %#x) failed; uCmd=%#x\n", ulArg, Hdr.cbIn, uCmd));
|
---|
777 | RTMemFree(pHdr);
|
---|
778 | return -EFAULT;
|
---|
779 | }
|
---|
780 | if (Hdr.cbIn < cbBuf)
|
---|
781 | RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbBuf - Hdr.cbIn);
|
---|
782 |
|
---|
783 | /*
|
---|
784 | * Process the IOCtl.
|
---|
785 | */
|
---|
786 | rc = supdrvIOCtl(uCmd, &g_DevExt, pSession, pHdr, cbBuf);
|
---|
787 |
|
---|
788 | /*
|
---|
789 | * Copy ioctl data and output buffer back to user space.
|
---|
790 | */
|
---|
791 | if (RT_LIKELY(!rc))
|
---|
792 | {
|
---|
793 | uint32_t cbOut = pHdr->cbOut;
|
---|
794 | if (RT_UNLIKELY(cbOut > cbBuf))
|
---|
795 | {
|
---|
796 | OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
|
---|
797 | cbOut = cbBuf;
|
---|
798 | }
|
---|
799 | if (RT_FAILURE(RTR0MemUserCopyTo(ulArg, pHdr, cbOut)))
|
---|
800 | {
|
---|
801 | /* this is really bad! */
|
---|
802 | OSDBGPRINT(("VBoxDrvLinuxIOCtl: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
|
---|
803 | rc = -EFAULT;
|
---|
804 | }
|
---|
805 | }
|
---|
806 | else
|
---|
807 | {
|
---|
808 | Log(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
|
---|
809 | rc = -EINVAL;
|
---|
810 | }
|
---|
811 | RTMemFree(pHdr);
|
---|
812 |
|
---|
813 | Log6(("VBoxDrvLinuxIOCtl: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
|
---|
814 | return rc;
|
---|
815 | }
|
---|
816 |
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * The SUPDRV IDC entry point.
|
---|
820 | *
|
---|
821 | * @returns VBox status code, see supdrvIDC.
|
---|
822 | * @param iReq The request code.
|
---|
823 | * @param pReq The request.
|
---|
824 | */
|
---|
825 | int VBOXCALL SUPDrvLinuxIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
826 | {
|
---|
827 | PSUPDRVSESSION pSession;
|
---|
828 |
|
---|
829 | /*
|
---|
830 | * Some quick validations.
|
---|
831 | */
|
---|
832 | if (RT_UNLIKELY(!VALID_PTR(pReq)))
|
---|
833 | return VERR_INVALID_POINTER;
|
---|
834 |
|
---|
835 | pSession = pReq->pSession;
|
---|
836 | if (pSession)
|
---|
837 | {
|
---|
838 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
839 | return VERR_INVALID_PARAMETER;
|
---|
840 | if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
|
---|
841 | return VERR_INVALID_PARAMETER;
|
---|
842 | }
|
---|
843 | else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
|
---|
844 | return VERR_INVALID_PARAMETER;
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Do the job.
|
---|
848 | */
|
---|
849 | return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
|
---|
850 | }
|
---|
851 |
|
---|
852 | EXPORT_SYMBOL(SUPDrvLinuxIDC);
|
---|
853 |
|
---|
854 |
|
---|
855 | RTCCUINTREG VBOXCALL supdrvOSChangeCR4(RTCCUINTREG fOrMask, RTCCUINTREG fAndMask)
|
---|
856 | {
|
---|
857 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 20, 0)
|
---|
858 | RTCCUINTREG uOld = this_cpu_read(cpu_tlbstate.cr4);
|
---|
859 | RTCCUINTREG uNew = (uOld & fAndMask) | fOrMask;
|
---|
860 | if (uNew != uOld)
|
---|
861 | {
|
---|
862 | this_cpu_write(cpu_tlbstate.cr4, uNew);
|
---|
863 | __write_cr4(uNew);
|
---|
864 | }
|
---|
865 | #else
|
---|
866 | RTCCUINTREG uOld = ASMGetCR4();
|
---|
867 | RTCCUINTREG uNew = (uOld & fAndMask) | fOrMask;
|
---|
868 | if (uNew != uOld)
|
---|
869 | ASMSetCR4(uNew);
|
---|
870 | #endif
|
---|
871 | return uOld;
|
---|
872 | }
|
---|
873 |
|
---|
874 |
|
---|
875 | void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
|
---|
876 | {
|
---|
877 | NOREF(pDevExt);
|
---|
878 | NOREF(pSession);
|
---|
879 | }
|
---|
880 |
|
---|
881 |
|
---|
882 | void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
883 | {
|
---|
884 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
885 | }
|
---|
886 |
|
---|
887 |
|
---|
888 | void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
889 | {
|
---|
890 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
891 | }
|
---|
892 |
|
---|
893 |
|
---|
894 | /**
|
---|
895 | * Initializes any OS specific object creator fields.
|
---|
896 | */
|
---|
897 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
898 | {
|
---|
899 | NOREF(pObj);
|
---|
900 | NOREF(pSession);
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Checks if the session can access the object.
|
---|
906 | *
|
---|
907 | * @returns true if a decision has been made.
|
---|
908 | * @returns false if the default access policy should be applied.
|
---|
909 | *
|
---|
910 | * @param pObj The object in question.
|
---|
911 | * @param pSession The session wanting to access the object.
|
---|
912 | * @param pszObjName The object name, can be NULL.
|
---|
913 | * @param prc Where to store the result when returning true.
|
---|
914 | */
|
---|
915 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
916 | {
|
---|
917 | NOREF(pObj);
|
---|
918 | NOREF(pSession);
|
---|
919 | NOREF(pszObjName);
|
---|
920 | NOREF(prc);
|
---|
921 | return false;
|
---|
922 | }
|
---|
923 |
|
---|
924 |
|
---|
925 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
926 | {
|
---|
927 | return force_async_tsc != 0;
|
---|
928 | }
|
---|
929 |
|
---|
930 |
|
---|
931 | bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void)
|
---|
932 | {
|
---|
933 | return true;
|
---|
934 | }
|
---|
935 |
|
---|
936 |
|
---|
937 | bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
|
---|
938 | {
|
---|
939 | return false;
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
944 | {
|
---|
945 | NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
|
---|
946 | return VERR_NOT_SUPPORTED;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
951 | {
|
---|
952 | NOREF(pDevExt); NOREF(pImage);
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
957 | {
|
---|
958 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
959 | return VERR_NOT_SUPPORTED;
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
964 | {
|
---|
965 | NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
|
---|
966 | return VERR_NOT_SUPPORTED;
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
971 | {
|
---|
972 | NOREF(pDevExt); NOREF(pImage);
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
977 |
|
---|
978 | int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
|
---|
979 | {
|
---|
980 | # ifdef SUPDRV_LINUX_HAS_SAFE_MSR_API
|
---|
981 | uint32_t u32Low, u32High;
|
---|
982 | int rc;
|
---|
983 |
|
---|
984 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
985 | if (idCpu == NIL_RTCPUID)
|
---|
986 | rc = rdmsr_safe(uMsr, &u32Low, &u32High);
|
---|
987 | else if (RTMpIsCpuOnline(idCpu))
|
---|
988 | rc = rdmsr_safe_on_cpu(idCpu, uMsr, &u32Low, &u32High);
|
---|
989 | else
|
---|
990 | return VERR_CPU_OFFLINE;
|
---|
991 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
992 | if (rc == 0)
|
---|
993 | {
|
---|
994 | *puValue = RT_MAKE_U64(u32Low, u32High);
|
---|
995 | return VINF_SUCCESS;
|
---|
996 | }
|
---|
997 | return VERR_ACCESS_DENIED;
|
---|
998 | # else
|
---|
999 | return VERR_NOT_SUPPORTED;
|
---|
1000 | # endif
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
|
---|
1005 | {
|
---|
1006 | # ifdef SUPDRV_LINUX_HAS_SAFE_MSR_API
|
---|
1007 | int rc;
|
---|
1008 |
|
---|
1009 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1010 | if (idCpu == NIL_RTCPUID)
|
---|
1011 | rc = wrmsr_safe(uMsr, RT_LODWORD(uValue), RT_HIDWORD(uValue));
|
---|
1012 | else if (RTMpIsCpuOnline(idCpu))
|
---|
1013 | rc = wrmsr_safe_on_cpu(idCpu, uMsr, RT_LODWORD(uValue), RT_HIDWORD(uValue));
|
---|
1014 | else
|
---|
1015 | return VERR_CPU_OFFLINE;
|
---|
1016 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1017 |
|
---|
1018 | if (rc == 0)
|
---|
1019 | return VINF_SUCCESS;
|
---|
1020 | return VERR_ACCESS_DENIED;
|
---|
1021 | # else
|
---|
1022 | return VERR_NOT_SUPPORTED;
|
---|
1023 | # endif
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | # ifdef SUPDRV_LINUX_HAS_SAFE_MSR_API
|
---|
1027 | /**
|
---|
1028 | * Worker for supdrvOSMsrProberModify.
|
---|
1029 | */
|
---|
1030 | static DECLCALLBACK(void) supdrvLnxMsrProberModifyOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
1031 | {
|
---|
1032 | PSUPMSRPROBER pReq = (PSUPMSRPROBER)pvUser1;
|
---|
1033 | register uint32_t uMsr = pReq->u.In.uMsr;
|
---|
1034 | bool const fFaster = pReq->u.In.enmOp == SUPMSRPROBEROP_MODIFY_FASTER;
|
---|
1035 | uint64_t uBefore;
|
---|
1036 | uint64_t uWritten;
|
---|
1037 | uint64_t uAfter;
|
---|
1038 | int rcBefore, rcWrite, rcAfter, rcRestore;
|
---|
1039 | RTCCUINTREG fOldFlags;
|
---|
1040 |
|
---|
1041 | /* Initialize result variables. */
|
---|
1042 | uBefore = uWritten = uAfter = 0;
|
---|
1043 | rcWrite = rcAfter = rcRestore = -EIO;
|
---|
1044 |
|
---|
1045 | /*
|
---|
1046 | * Do the job.
|
---|
1047 | */
|
---|
1048 | fOldFlags = ASMIntDisableFlags();
|
---|
1049 | ASMCompilerBarrier(); /* paranoia */
|
---|
1050 | if (!fFaster)
|
---|
1051 | ASMWriteBackAndInvalidateCaches();
|
---|
1052 |
|
---|
1053 | rcBefore = rdmsrl_safe(uMsr, &uBefore);
|
---|
1054 | if (rcBefore >= 0)
|
---|
1055 | {
|
---|
1056 | register uint64_t uRestore = uBefore;
|
---|
1057 | uWritten = uRestore;
|
---|
1058 | uWritten &= pReq->u.In.uArgs.Modify.fAndMask;
|
---|
1059 | uWritten |= pReq->u.In.uArgs.Modify.fOrMask;
|
---|
1060 |
|
---|
1061 | rcWrite = wrmsr_safe(uMsr, RT_LODWORD(uWritten), RT_HIDWORD(uWritten));
|
---|
1062 | rcAfter = rdmsrl_safe(uMsr, &uAfter);
|
---|
1063 | rcRestore = wrmsr_safe(uMsr, RT_LODWORD(uRestore), RT_HIDWORD(uRestore));
|
---|
1064 |
|
---|
1065 | if (!fFaster)
|
---|
1066 | {
|
---|
1067 | ASMWriteBackAndInvalidateCaches();
|
---|
1068 | ASMReloadCR3();
|
---|
1069 | ASMNopPause();
|
---|
1070 | }
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | ASMCompilerBarrier(); /* paranoia */
|
---|
1074 | ASMSetFlags(fOldFlags);
|
---|
1075 |
|
---|
1076 | /*
|
---|
1077 | * Write out the results.
|
---|
1078 | */
|
---|
1079 | pReq->u.Out.uResults.Modify.uBefore = uBefore;
|
---|
1080 | pReq->u.Out.uResults.Modify.uWritten = uWritten;
|
---|
1081 | pReq->u.Out.uResults.Modify.uAfter = uAfter;
|
---|
1082 | pReq->u.Out.uResults.Modify.fBeforeGp = rcBefore != 0;
|
---|
1083 | pReq->u.Out.uResults.Modify.fModifyGp = rcWrite != 0;
|
---|
1084 | pReq->u.Out.uResults.Modify.fAfterGp = rcAfter != 0;
|
---|
1085 | pReq->u.Out.uResults.Modify.fRestoreGp = rcRestore != 0;
|
---|
1086 | RT_ZERO(pReq->u.Out.uResults.Modify.afReserved);
|
---|
1087 | }
|
---|
1088 | # endif
|
---|
1089 |
|
---|
1090 |
|
---|
1091 | int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
|
---|
1092 | {
|
---|
1093 | # ifdef SUPDRV_LINUX_HAS_SAFE_MSR_API
|
---|
1094 | if (idCpu == NIL_RTCPUID)
|
---|
1095 | {
|
---|
1096 | supdrvLnxMsrProberModifyOnCpu(idCpu, pReq, NULL);
|
---|
1097 | return VINF_SUCCESS;
|
---|
1098 | }
|
---|
1099 | return RTMpOnSpecific(idCpu, supdrvLnxMsrProberModifyOnCpu, pReq, NULL);
|
---|
1100 | # else
|
---|
1101 | return VERR_NOT_SUPPORTED;
|
---|
1102 | # endif
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | #endif /* SUPDRV_WITH_MSR_PROBER */
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Converts a supdrv error code to an linux error code.
|
---|
1110 | *
|
---|
1111 | * @returns corresponding linux error code.
|
---|
1112 | * @param rc IPRT status code.
|
---|
1113 | */
|
---|
1114 | static int VBoxDrvLinuxErr2LinuxErr(int rc)
|
---|
1115 | {
|
---|
1116 | switch (rc)
|
---|
1117 | {
|
---|
1118 | case VINF_SUCCESS: return 0;
|
---|
1119 | case VERR_GENERAL_FAILURE: return -EACCES;
|
---|
1120 | case VERR_INVALID_PARAMETER: return -EINVAL;
|
---|
1121 | case VERR_INVALID_MAGIC: return -EILSEQ;
|
---|
1122 | case VERR_INVALID_HANDLE: return -ENXIO;
|
---|
1123 | case VERR_INVALID_POINTER: return -EFAULT;
|
---|
1124 | case VERR_LOCK_FAILED: return -ENOLCK;
|
---|
1125 | case VERR_ALREADY_LOADED: return -EEXIST;
|
---|
1126 | case VERR_PERMISSION_DENIED: return -EPERM;
|
---|
1127 | case VERR_VERSION_MISMATCH: return -ENOSYS;
|
---|
1128 | case VERR_IDT_FAILED: return -1000;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | return -EPERM;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 |
|
---|
1135 | RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
1136 | {
|
---|
1137 | va_list va;
|
---|
1138 | char szMsg[512];
|
---|
1139 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1140 |
|
---|
1141 | va_start(va, pszFormat);
|
---|
1142 | RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
1143 | va_end(va);
|
---|
1144 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
1145 |
|
---|
1146 | printk("%s", szMsg);
|
---|
1147 |
|
---|
1148 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1149 | return 0;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 |
|
---|
1153 | SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
|
---|
1154 | {
|
---|
1155 | uint32_t fFlags = 0;
|
---|
1156 | #ifdef CONFIG_PAX_KERNEXEC
|
---|
1157 | fFlags |= SUPKERNELFEATURES_GDT_READ_ONLY;
|
---|
1158 | #endif
|
---|
1159 | #if defined(VBOX_STRICT) || defined(VBOX_WITH_EFLAGS_AC_SET_IN_VBOXDRV)
|
---|
1160 | fFlags |= SUPKERNELFEATURES_SMAP;
|
---|
1161 | #elif defined(CONFIG_X86_SMAP)
|
---|
1162 | if (ASMGetCR4() & X86_CR4_SMAP)
|
---|
1163 | fFlags |= SUPKERNELFEATURES_SMAP;
|
---|
1164 | #endif
|
---|
1165 | return fFlags;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 |
|
---|
1169 | module_init(VBoxDrvLinuxInit);
|
---|
1170 | module_exit(VBoxDrvLinuxUnload);
|
---|
1171 |
|
---|
1172 | MODULE_AUTHOR(VBOX_VENDOR);
|
---|
1173 | MODULE_DESCRIPTION(VBOX_PRODUCT " Support Driver");
|
---|
1174 | MODULE_LICENSE("GPL");
|
---|
1175 | #ifdef MODULE_VERSION
|
---|
1176 | MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(SUPDRV_IOC_VERSION) ")");
|
---|
1177 | #endif
|
---|
1178 |
|
---|
1179 | module_param(force_async_tsc, int, 0444);
|
---|
1180 | MODULE_PARM_DESC(force_async_tsc, "force the asynchronous TSC mode");
|
---|
1181 |
|
---|