VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/linux/SUPDrv-linux.c@ 27334

Last change on this file since 27334 was 27334, checked in by vboxsync, 15 years ago

Linux hosts: properly suspend/resume during host hibernation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.8 KB
Line 
1/* $Rev: 27334 $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - Linux specifics.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 * Some lines of code to disable the local APIC on x86_64 machines taken
30 * from a Mandriva patch by Gwenole Beauchesne <gbeauchesne@mandriva.com>.
31 */
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#define LOG_GROUP LOG_GROUP_SUP_DRV
37#include "../SUPDrvInternal.h"
38#include "the-linux-kernel.h"
39#include "version-generated.h"
40
41#include <iprt/assert.h>
42#include <iprt/spinlock.h>
43#include <iprt/semaphore.h>
44#include <iprt/initterm.h>
45#include <iprt/process.h>
46#include <VBox/err.h>
47#include <iprt/mem.h>
48#include <VBox/log.h>
49#include <iprt/mp.h>
50
51/** @todo figure out the exact version number */
52#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
53# include <iprt/power.h>
54# define VBOX_WITH_SUSPEND_NOTIFICATION
55#endif
56
57#include <linux/sched.h>
58#ifdef CONFIG_DEVFS_FS
59# include <linux/devfs_fs_kernel.h>
60#endif
61#ifdef CONFIG_VBOXDRV_AS_MISC
62# include <linux/miscdevice.h>
63#endif
64#ifdef CONFIG_X86_LOCAL_APIC
65# include <asm/apic.h>
66# include <asm/nmi.h>
67#endif
68#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
69# include <linux/platform_device.h>
70#endif
71
72#include <iprt/mem.h>
73
74
75/*******************************************************************************
76* Defined Constants And Macros *
77*******************************************************************************/
78/* check kernel version */
79# ifndef SUPDRV_AGNOSTIC
80# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
81# error Unsupported kernel version!
82# endif
83# endif
84
85/* devfs defines */
86#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
87# ifdef VBOX_WITH_HARDENING
88# define VBOX_DEV_FMASK (S_IWUSR | S_IRUSR)
89# else
90# define VBOX_DEV_FMASK (S_IRUGO | S_IWUGO)
91# endif
92#endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
93
94#ifdef CONFIG_X86_HIGH_ENTRY
95# error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
96#endif
97
98#ifdef CONFIG_X86_LOCAL_APIC
99
100/* If an NMI occurs while we are inside the world switcher the machine will
101 * crash. The Linux NMI watchdog generates periodic NMIs increasing a counter
102 * which is compared with another counter increased in the timer interrupt
103 * handler. We disable the NMI watchdog.
104 *
105 * - Linux >= 2.6.21: The watchdog is disabled by default on i386 and x86_64.
106 * - Linux < 2.6.21: The watchdog is normally enabled by default on x86_64
107 * and disabled on i386.
108 */
109# if defined(RT_ARCH_AMD64)
110# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21) && !defined(VBOX_REDHAT_KABI)
111# define DO_DISABLE_NMI 1
112# endif
113# endif
114
115# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
116extern int nmi_active;
117# define nmi_atomic_read(P) *(P)
118# define nmi_atomic_set(P, V) *(P) = (V)
119# define nmi_atomic_dec(P) nmi_atomic_set(P, 0)
120# else
121# define nmi_atomic_read(P) atomic_read(P)
122# define nmi_atomic_set(P, V) atomic_set(P, V)
123# define nmi_atomic_dec(P) atomic_dec(P)
124# endif
125
126# ifndef X86_FEATURE_ARCH_PERFMON
127# define X86_FEATURE_ARCH_PERFMON (3*32+9) /* Intel Architectural PerfMon */
128# endif
129# ifndef MSR_ARCH_PERFMON_EVENTSEL0
130# define MSR_ARCH_PERFMON_EVENTSEL0 0x186
131# endif
132# ifndef ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT
133# define ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT (1 << 0)
134# endif
135
136#endif /* CONFIG_X86_LOCAL_APIC */
137
138
139/*******************************************************************************
140* Internal Functions *
141*******************************************************************************/
142static int VBoxDrvLinuxInit(void);
143static void VBoxDrvLinuxUnload(void);
144static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp);
145static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp);
146#ifdef HAVE_UNLOCKED_IOCTL
147static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
148#else
149static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
150#endif
151static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
152static int VBoxDrvLinuxErr2LinuxErr(int);
153#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
154static int VBoxDrvProbe(struct platform_device *pDev);
155# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
156static int VBoxDrvSuspend(struct device *pDev);
157static int VBoxDrvResume(struct device *pDev);
158# else
159static int VBoxDrvSuspend(struct platform_device *pDev, pm_message_t State);
160static int VBoxDrvResume(struct platform_device *pDev);
161# endif
162static void VBoxDevRelease(struct device *pDev);
163#endif
164
165
166/*******************************************************************************
167* Global Variables *
168*******************************************************************************/
169/**
170 * Device extention & session data association structure.
171 */
172static SUPDRVDEVEXT g_DevExt;
173
174#ifndef CONFIG_VBOXDRV_AS_MISC
175/** Module major number */
176#define DEVICE_MAJOR 234
177/** Saved major device number */
178static int g_iModuleMajor;
179#endif /* !CONFIG_VBOXDRV_AS_MISC */
180
181/** Module parameter.
182 * Not prefixed because the name is used by macros and the end of this file. */
183static int force_async_tsc = 0;
184
185/** The module name. */
186#define DEVICE_NAME "vboxdrv"
187
188#ifdef RT_ARCH_AMD64
189/**
190 * Memory for the executable memory heap (in IPRT).
191 */
192extern uint8_t g_abExecMemory[1572864]; /* 1.5 MB */
193__asm__(".section execmemory, \"awx\", @progbits\n\t"
194 ".align 32\n\t"
195 ".globl g_abExecMemory\n"
196 "g_abExecMemory:\n\t"
197 ".zero 1572864\n\t"
198 ".type g_abExecMemory, @object\n\t"
199 ".size g_abExecMemory, 1572864\n\t"
200 ".text\n\t");
201#endif
202
203/** The file_operations structure. */
204static struct file_operations gFileOpsVBoxDrv =
205{
206 owner: THIS_MODULE,
207 open: VBoxDrvLinuxCreate,
208 release: VBoxDrvLinuxClose,
209#ifdef HAVE_UNLOCKED_IOCTL
210 unlocked_ioctl: VBoxDrvLinuxIOCtl,
211#else
212 ioctl: VBoxDrvLinuxIOCtl,
213#endif
214};
215
216#ifdef CONFIG_VBOXDRV_AS_MISC
217/** The miscdevice structure. */
218static struct miscdevice gMiscDevice =
219{
220 minor: MISC_DYNAMIC_MINOR,
221 name: DEVICE_NAME,
222 fops: &gFileOpsVBoxDrv,
223# if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
224 devfs_name: DEVICE_NAME,
225# endif
226};
227#endif
228
229
230#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
231# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
232static struct dev_pm_ops gPlatformPMOps =
233{
234 .suspend = VBoxDrvSuspend, /* before entering deep sleep */
235 .resume = VBoxDrvResume, /* after wakeup from deep sleep */
236 .freeze = VBoxDrvSuspend, /* before creating hibernation image */
237 .restore = VBoxDrvResume, /* after wakeing up from hibernation */
238};
239# endif
240
241static struct platform_driver gPlatformDriver =
242{
243 .probe = VBoxDrvProbe,
244# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
245 .suspend = VBoxDrvSuspend,
246 .resume = VBoxDrvResume,
247# endif
248 /** @todo .shutdown? */
249 .driver =
250 {
251 .name = "vboxdrv",
252# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
253 .pm = &gPlatformPMOps,
254# endif
255 }
256};
257
258static struct platform_device gPlatformDevice =
259{
260 .name = "vboxdrv",
261 .dev =
262 {
263 .release = VBoxDevRelease
264 }
265};
266#endif /* VBOX_WITH_SUSPEND_NOTIFICATION */
267
268
269
270
271
272#ifdef CONFIG_X86_LOCAL_APIC
273# ifdef DO_DISABLE_NMI
274/** Stop AMD NMI watchdog (x86_64 only). */
275static int vboxdrvStopK7Watchdog(void)
276{
277 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
278 return 1;
279}
280
281/** Stop Intel P4 NMI watchdog (x86_64 only). */
282static int vboxdrvStopP4Watchdog(void)
283{
284 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
285 wrmsr(MSR_P4_IQ_CCCR1, 0, 0);
286 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
287 return 1;
288}
289
290/** The new method of detecting the event counter */
291static int vboxdrvStopIntelArchWatchdog(void)
292{
293 unsigned ebx;
294
295 ebx = cpuid_ebx(10);
296 if (!(ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
297 wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, 0, 0);
298 return 1;
299}
300
301/** Stop NMI watchdog. */
302static void vboxdrvStopApicNmiWatchdog(void *unused)
303{
304 int stopped = 0;
305
306 /* only support LOCAL and IO APICs for now */
307 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
308 (nmi_watchdog != NMI_IO_APIC))
309 return;
310
311 if (nmi_watchdog == NMI_LOCAL_APIC)
312 {
313 switch (boot_cpu_data.x86_vendor)
314 {
315 case X86_VENDOR_AMD:
316 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
317 return;
318 stopped = vboxdrvStopK7Watchdog();
319 break;
320 case X86_VENDOR_INTEL:
321 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
322 {
323 stopped = vboxdrvStopIntelArchWatchdog();
324 break;
325 }
326 stopped = vboxdrvStopP4Watchdog();
327 break;
328 default:
329 return;
330 }
331 }
332
333 if (stopped)
334 nmi_atomic_dec(&nmi_active);
335}
336
337/** Disable LAPIC NMI watchdog. */
338static void DisableLapicNmiWatchdog(void)
339{
340 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
341
342 if (nmi_atomic_read(&nmi_active) <= 0)
343 return;
344
345 on_each_cpu(vboxdrvStopApicNmiWatchdog, NULL, 1, 1);
346
347 BUG_ON(nmi_atomic_read(&nmi_active) != 0);
348
349 /* tell do_nmi() and others that we're not active any more */
350 nmi_watchdog = NMI_NONE;
351}
352
353/** Shutdown NMI. */
354static void vboxdrvNmiCpuShutdown(void * dummy)
355{
356 unsigned int vERR, vPC;
357
358 vPC = apic_read(APIC_LVTPC);
359
360 if ((GET_APIC_DELIVERY_MODE(vPC) == APIC_MODE_NMI) && !(vPC & APIC_LVT_MASKED))
361 {
362 vERR = apic_read(APIC_LVTERR);
363 apic_write(APIC_LVTERR, vERR | APIC_LVT_MASKED);
364 apic_write(APIC_LVTPC, vPC | APIC_LVT_MASKED);
365 apic_write(APIC_LVTERR, vERR);
366 }
367}
368
369static void vboxdrvNmiShutdown(void)
370{
371 on_each_cpu(vboxdrvNmiCpuShutdown, NULL, 0, 1);
372}
373# endif /* DO_DISABLE_NMI */
374#endif /* CONFIG_X86_LOCAL_APIC */
375
376
377DECLINLINE(RTUID) vboxdrvLinuxUid(void)
378{
379#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
380 return current->cred->uid;
381#else
382 return current->uid;
383#endif
384}
385
386DECLINLINE(RTGID) vboxdrvLinuxGid(void)
387{
388#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
389 return current->cred->gid;
390#else
391 return current->gid;
392#endif
393}
394
395DECLINLINE(RTUID) vboxdrvLinuxEuid(void)
396{
397#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
398 return current->cred->euid;
399#else
400 return current->euid;
401#endif
402}
403
404/**
405 * Initialize module.
406 *
407 * @returns appropriate status code.
408 */
409static int __init VBoxDrvLinuxInit(void)
410{
411 int rc;
412
413#ifdef CONFIG_X86_LOCAL_APIC
414 /*
415 * If an NMI occurs while we are inside the world switcher the macine will crash.
416 * The Linux NMI watchdog generates periodic NMIs increasing a counter which is
417 * compared with another counter increased in the timer interrupt handler. Therefore
418 * we don't allow to setup an NMI watchdog.
419 */
420# if !defined(VBOX_REDHAT_KABI)
421 /*
422 * First test: NMI actiated? Works only works with Linux 2.6 -- 2.4 does not export
423 * the nmi_watchdog variable.
424 */
425# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || defined CONFIG_X86_64
426# ifdef DO_DISABLE_NMI
427 if (nmi_atomic_read(&nmi_active) > 0)
428 {
429 printk(KERN_DEBUG DEVICE_NAME ": Trying to deactivate the NMI watchdog...\n");
430
431 switch (nmi_watchdog)
432 {
433 case NMI_LOCAL_APIC:
434 DisableLapicNmiWatchdog();
435 break;
436 case NMI_NONE:
437 nmi_atomic_dec(&nmi_active);
438 break;
439 }
440
441 if (nmi_atomic_read(&nmi_active) == 0)
442 {
443 vboxdrvNmiShutdown();
444 printk(KERN_DEBUG DEVICE_NAME ": Successfully done.\n");
445 }
446 else
447 printk(KERN_DEBUG DEVICE_NAME ": Failed!\n");
448 }
449# endif /* DO_DISABLE_NMI */
450
451 /*
452 * Permanent IO_APIC mode active? No way to handle this!
453 */
454 if (nmi_watchdog == NMI_IO_APIC)
455 {
456 printk(KERN_ERR DEVICE_NAME
457 ": NMI watchdog in IO_APIC mode active -- refused to load the kernel module!\n"
458 DEVICE_NAME
459 ": Please disable the NMI watchdog by specifying 'nmi_watchdog=0' at kernel\n"
460 DEVICE_NAME
461 ": command line.\n");
462 return -EINVAL;
463 }
464
465 /*
466 * See arch/i386/kernel/nmi.c on >= 2.6.19: -1 means it can never enabled again
467 */
468 nmi_atomic_set(&nmi_active, -1);
469 printk(KERN_DEBUG DEVICE_NAME ": Trying to deactivate the NMI watchdog permanently...\n");
470
471 /*
472 * Now fall through and see if it actually was enabled before. If so, fail
473 * as we cannot deactivate it cleanly from here.
474 */
475# else /* < 2.6.19 */
476 /*
477 * Older 2.6 kernels: nmi_watchdog is not initalized by default
478 */
479 if (nmi_watchdog != NMI_NONE)
480 goto nmi_activated;
481# endif
482# endif /* >= 2.6.0 && !defined(VBOX_REDHAT_KABI) */
483
484 /*
485 * Second test: Interrupt generated by performance counter not masked and can
486 * generate an NMI. Works also with Linux 2.4.
487 */
488 {
489 unsigned int v, ver, maxlvt;
490
491 v = apic_read(APIC_LVR);
492 ver = GET_APIC_VERSION(v);
493 /* 82489DXs do not report # of LVT entries. */
494 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
495 if (maxlvt >= 4)
496 {
497 /* Read status of performance counter IRQ vector */
498 v = apic_read(APIC_LVTPC);
499
500 /* performance counter generates NMI and is not masked? */
501 if ((GET_APIC_DELIVERY_MODE(v) == APIC_MODE_NMI) && !(v & APIC_LVT_MASKED))
502 {
503# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) \
504 && (defined(CONFIG_PERF_COUNTERS) || defined(CONFIG_PERF_EVENTS))
505 /* 2.6.31+: The performance counter framework will initialize the LVTPC
506 * vector as NMI. We can't disable the framework but the kernel loader
507 * script will do 'echo 2 > /proc/sys/kernel/perf_counter_paranoid'
508 * which hopefilly prevents any usage of hardware performance counters
509 * and therefore triggering of NMIs.
510 * 2.6.32+: CONFIG_PERF_COUNTERS => CONFIG_PERF_EVENTS */
511 printk(KERN_ERR DEVICE_NAME
512 ": Warning: 2.6.31+ kernel detected. Most likely the hardware performance\n"
513 DEVICE_NAME
514 ": counter framework which can generate NMIs is active. You have to prevent\n"
515 DEVICE_NAME
516 ": the usage of hardware performance counters by\n"
517 DEVICE_NAME
518 ": echo 2 > /proc/sys/kernel/perf_counter_paranoid\n");
519 /* We can't do more here :-( */
520 goto no_error;
521# endif
522
523# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || defined CONFIG_X86_64
524 printk(KERN_ERR DEVICE_NAME
525 ": NMI watchdog either active or at least initialized. Please disable the NMI\n"
526 DEVICE_NAME
527 ": watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
528 return -EINVAL;
529# else /* < 2.6.19 */
530# if !defined(VBOX_REDHAT_KABI)
531nmi_activated:
532# endif
533 printk(KERN_ERR DEVICE_NAME
534 ": NMI watchdog active -- refused to load the kernel module! Please disable\n"
535 DEVICE_NAME
536 ": the NMI watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
537 return -EINVAL;
538# endif /* >= 2.6.19 */
539 }
540 }
541 }
542# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
543 printk(KERN_DEBUG DEVICE_NAME ": Successfully done.\n");
544# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) \
545 && (defined(CONFIG_PERF_COUNTERS) || defined(CONFIG_PERF_EVENTS))
546no_error:
547# endif
548# endif /* >= 2.6.19 */
549#endif /* CONFIG_X86_LOCAL_APIC */
550
551 /*
552 * Check for synchronous/asynchronous TSC mode.
553 */
554 printk(KERN_DEBUG DEVICE_NAME ": Found %u processor cores.\n", (unsigned)RTMpGetOnlineCount());
555#ifdef CONFIG_VBOXDRV_AS_MISC
556 rc = misc_register(&gMiscDevice);
557 if (rc)
558 {
559 printk(KERN_ERR DEVICE_NAME ": Can't register misc device! rc=%d\n", rc);
560 return rc;
561 }
562#else /* !CONFIG_VBOXDRV_AS_MISC */
563 /*
564 * Register character device.
565 */
566 g_iModuleMajor = DEVICE_MAJOR;
567 rc = register_chrdev((dev_t)g_iModuleMajor, DEVICE_NAME, &gFileOpsVBoxDrv);
568 if (rc < 0)
569 {
570 Log(("register_chrdev() failed with rc=%#x!\n", rc));
571 return rc;
572 }
573
574 /*
575 * Save returned module major number
576 */
577 if (DEVICE_MAJOR != 0)
578 g_iModuleMajor = DEVICE_MAJOR;
579 else
580 g_iModuleMajor = rc;
581 rc = 0;
582
583# ifdef CONFIG_DEVFS_FS
584 /*
585 * Register a device entry
586 */
587 if (devfs_mk_cdev(MKDEV(DEVICE_MAJOR, 0), S_IFCHR | VBOX_DEV_FMASK, DEVICE_NAME) != 0)
588 {
589 Log(("devfs_register failed!\n"));
590 rc = -EINVAL;
591 }
592# endif
593#endif /* !CONFIG_VBOXDRV_AS_MISC */
594 if (!rc)
595 {
596 /*
597 * Initialize the runtime.
598 * On AMD64 we'll have to donate the high rwx memory block to the exec allocator.
599 */
600 rc = RTR0Init(0);
601 if (RT_SUCCESS(rc))
602 {
603#ifdef RT_ARCH_AMD64
604 rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
605 printk("VBoxDrv: dbg - g_abExecMemory=%p\n", (void *)&g_abExecMemory[0]);
606#endif
607 Log(("VBoxDrv::ModuleInit\n"));
608
609 /*
610 * Initialize the device extension.
611 */
612 if (RT_SUCCESS(rc))
613 rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
614 if (RT_SUCCESS(rc))
615 {
616#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
617 rc = platform_driver_register(&gPlatformDriver);
618 if (rc == 0)
619 {
620 rc = platform_device_register(&gPlatformDevice);
621 if (rc == 0)
622#endif
623 {
624 printk(KERN_INFO DEVICE_NAME ": TSC mode is %s, kernel timer mode is "
625#ifdef VBOX_HRTIMER
626 "'high-res'"
627#else
628 "'normal'"
629#endif
630 ".\n",
631 g_DevExt.pGip->u32Mode == SUPGIPMODE_SYNC_TSC ? "'synchronous'" : "'asynchronous'");
632 LogFlow(("VBoxDrv::ModuleInit returning %#x\n", rc));
633 printk(KERN_DEBUG DEVICE_NAME ": Successfully loaded version "
634 VBOX_VERSION_STRING " (interface " RT_XSTR(SUPDRV_IOC_VERSION) ").\n");
635 return rc;
636 }
637#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
638 else
639 platform_driver_unregister(&gPlatformDriver);
640 }
641#endif
642 }
643
644 rc = -EINVAL;
645 RTR0TermForced();
646 }
647 else
648 rc = -EINVAL;
649
650 /*
651 * Failed, cleanup and return the error code.
652 */
653#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
654 devfs_remove(DEVICE_NAME);
655#endif
656 }
657#ifdef CONFIG_VBOXDRV_AS_MISC
658 misc_deregister(&gMiscDevice);
659 Log(("VBoxDrv::ModuleInit returning %#x (minor:%d)\n", rc, gMiscDevice.minor));
660#else
661 unregister_chrdev(g_iModuleMajor, DEVICE_NAME);
662 Log(("VBoxDrv::ModuleInit returning %#x (major:%d)\n", rc, g_iModuleMajor));
663#endif
664 return rc;
665}
666
667
668/**
669 * Unload the module.
670 */
671static void __exit VBoxDrvLinuxUnload(void)
672{
673 int rc;
674 Log(("VBoxDrvLinuxUnload\n"));
675 NOREF(rc);
676
677#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
678 platform_device_unregister(&gPlatformDevice);
679 platform_driver_unregister(&gPlatformDriver);
680#endif
681
682 /*
683 * I Don't think it's possible to unload a driver which processes have
684 * opened, at least we'll blindly assume that here.
685 */
686#ifdef CONFIG_VBOXDRV_AS_MISC
687 rc = misc_deregister(&gMiscDevice);
688 if (rc < 0)
689 {
690 Log(("misc_deregister failed with rc=%#x\n", rc));
691 }
692#else /* !CONFIG_VBOXDRV_AS_MISC */
693# ifdef CONFIG_DEVFS_FS
694 /*
695 * Unregister a device entry
696 */
697 devfs_remove(DEVICE_NAME);
698# endif /* devfs */
699 unregister_chrdev(g_iModuleMajor, DEVICE_NAME);
700#endif /* !CONFIG_VBOXDRV_AS_MISC */
701
702 /*
703 * Destroy GIP, delete the device extension and terminate IPRT.
704 */
705 supdrvDeleteDevExt(&g_DevExt);
706 RTR0TermForced();
707}
708
709
710/**
711 * Device open. Called on open /dev/vboxdrv
712 *
713 * @param pInode Pointer to inode info structure.
714 * @param pFilp Associated file pointer.
715 */
716static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp)
717{
718 int rc;
719 PSUPDRVSESSION pSession;
720 Log(("VBoxDrvLinuxCreate: pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
721
722#ifdef VBOX_WITH_HARDENING
723 /*
724 * Only root is allowed to access the device, enforce it!
725 */
726 if (vboxdrvLinuxEuid() != 0 /* root */ )
727 {
728 Log(("VBoxDrvLinuxCreate: euid=%d, expected 0 (root)\n", vboxdrvLinuxEuid()));
729 return -EPERM;
730 }
731#endif /* VBOX_WITH_HARDENING */
732
733 /*
734 * Call common code for the rest.
735 */
736 rc = supdrvCreateSession(&g_DevExt, true /* fUser */, &pSession);
737 if (!rc)
738 {
739 pSession->Uid = vboxdrvLinuxUid();
740 pSession->Gid = vboxdrvLinuxGid();
741 }
742
743 pFilp->private_data = pSession;
744
745 Log(("VBoxDrvLinuxCreate: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
746 &g_DevExt, pSession, rc, VBoxDrvLinuxErr2LinuxErr(rc),
747 RTProcSelf(), current->pid, current->comm));
748 return VBoxDrvLinuxErr2LinuxErr(rc);
749}
750
751
752/**
753 * Close device.
754 *
755 * @param pInode Pointer to inode info structure.
756 * @param pFilp Associated file pointer.
757 */
758static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp)
759{
760 Log(("VBoxDrvLinuxClose: pFilp=%p pSession=%p pid=%d/%d %s\n",
761 pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
762 supdrvCloseSession(&g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
763 pFilp->private_data = NULL;
764 return 0;
765}
766
767
768#ifdef VBOX_WITH_SUSPEND_NOTIFICATION
769/**
770 * Dummy device release function. We have to provide this function,
771 * otherwise the kernel will complain.
772 *
773 * @param pDev Pointer to the platform device.
774 */
775static void VBoxDevRelease(struct device *pDev)
776{
777}
778
779/**
780 * Dummy probe function.
781 *
782 * @param pDev Pointer to the platform device.
783 */
784static int VBoxDrvProbe(struct platform_device *pDev)
785{
786 return 0;
787}
788
789/**
790 * Suspend callback.
791 * @param pDev Pointer to the platform device.
792 * @param State message type, see Documentation/power/devices.txt.
793 */
794# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
795static int VBoxDrvSuspend(struct device *pDev)
796# else
797static int VBoxDrvSuspend(struct platform_device *pDev, pm_message_t State)
798# endif
799{
800 RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
801 return 0;
802}
803
804/**
805 * Resume callback.
806 *
807 * @param pDev Pointer to the platform device.
808 */
809# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
810static int VBoxDrvResume(struct device *pDev)
811# else
812static int VBoxDrvResume(struct platform_device *pDev)
813# endif
814{
815 RTPowerSignalEvent(RTPOWEREVENT_RESUME);
816 return 0;
817}
818#endif /* VBOX_WITH_SUSPEND_NOTIFICATION */
819
820
821/**
822 * Device I/O Control entry point.
823 *
824 * @param pFilp Associated file pointer.
825 * @param uCmd The function specified to ioctl().
826 * @param ulArg The argument specified to ioctl().
827 */
828#ifdef HAVE_UNLOCKED_IOCTL
829static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
830#else
831static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
832#endif
833{
834 /*
835 * Deal with the two high-speed IOCtl that takes it's arguments from
836 * the session and iCmd, and only returns a VBox status code.
837 */
838#ifdef HAVE_UNLOCKED_IOCTL
839 if (RT_LIKELY( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
840 || uCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
841 || uCmd == SUP_IOCTL_FAST_DO_NOP))
842 return supdrvIOCtlFast(uCmd, ulArg, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
843 return VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg);
844
845#else /* !HAVE_UNLOCKED_IOCTL */
846
847 int rc;
848 unlock_kernel();
849 if (RT_LIKELY( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
850 || uCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
851 || uCmd == SUP_IOCTL_FAST_DO_NOP))
852 rc = supdrvIOCtlFast(uCmd, ulArg, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
853 else
854 rc = VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg);
855 lock_kernel();
856 return rc;
857#endif /* !HAVE_UNLOCKED_IOCTL */
858}
859
860
861/**
862 * Device I/O Control entry point.
863 *
864 * @param pFilp Associated file pointer.
865 * @param uCmd The function specified to ioctl().
866 * @param ulArg The argument specified to ioctl().
867 */
868static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
869{
870 int rc;
871 SUPREQHDR Hdr;
872 PSUPREQHDR pHdr;
873 uint32_t cbBuf;
874
875 Log6(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
876
877 /*
878 * Read the header.
879 */
880 if (RT_UNLIKELY(copy_from_user(&Hdr, (void *)ulArg, sizeof(Hdr))))
881 {
882 Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
883 return -EFAULT;
884 }
885 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
886 {
887 Log(("VBoxDrvLinuxIOCtl: bad header magic %#x; uCmd=%#x\n", Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, uCmd));
888 return -EINVAL;
889 }
890
891 /*
892 * Buffer the request.
893 */
894 cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
895 if (RT_UNLIKELY(cbBuf > _1M*16))
896 {
897 Log(("VBoxDrvLinuxIOCtl: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
898 return -E2BIG;
899 }
900 if (RT_UNLIKELY(cbBuf != _IOC_SIZE(uCmd) && _IOC_SIZE(uCmd)))
901 {
902 Log(("VBoxDrvLinuxIOCtl: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
903 return -EINVAL;
904 }
905 pHdr = RTMemAlloc(cbBuf);
906 if (RT_UNLIKELY(!pHdr))
907 {
908 OSDBGPRINT(("VBoxDrvLinuxIOCtl: failed to allocate buffer of %d bytes for uCmd=%#x.\n", cbBuf, uCmd));
909 return -ENOMEM;
910 }
911 if (RT_UNLIKELY(copy_from_user(pHdr, (void *)ulArg, Hdr.cbIn)))
912 {
913 Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx, %#x) failed; uCmd=%#x.\n", ulArg, Hdr.cbIn, uCmd));
914 RTMemFree(pHdr);
915 return -EFAULT;
916 }
917
918 /*
919 * Process the IOCtl.
920 */
921 rc = supdrvIOCtl(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data, pHdr);
922
923 /*
924 * Copy ioctl data and output buffer back to user space.
925 */
926 if (RT_LIKELY(!rc))
927 {
928 uint32_t cbOut = pHdr->cbOut;
929 if (RT_UNLIKELY(cbOut > cbBuf))
930 {
931 OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
932 cbOut = cbBuf;
933 }
934 if (RT_UNLIKELY(copy_to_user((void *)ulArg, pHdr, cbOut)))
935 {
936 /* this is really bad! */
937 OSDBGPRINT(("VBoxDrvLinuxIOCtl: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
938 rc = -EFAULT;
939 }
940 }
941 else
942 {
943 Log(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
944 rc = -EINVAL;
945 }
946 RTMemFree(pHdr);
947
948 Log6(("VBoxDrvLinuxIOCtl: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
949 return rc;
950}
951
952
953/**
954 * The SUPDRV IDC entry point.
955 *
956 * @returns VBox status code, see supdrvIDC.
957 * @param iReq The request code.
958 * @param pReq The request.
959 */
960int VBOXCALL SUPDrvLinuxIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
961{
962 PSUPDRVSESSION pSession;
963
964 /*
965 * Some quick validations.
966 */
967 if (RT_UNLIKELY(!VALID_PTR(pReq)))
968 return VERR_INVALID_POINTER;
969
970 pSession = pReq->pSession;
971 if (pSession)
972 {
973 if (RT_UNLIKELY(!VALID_PTR(pSession)))
974 return VERR_INVALID_PARAMETER;
975 if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
976 return VERR_INVALID_PARAMETER;
977 }
978 else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
979 return VERR_INVALID_PARAMETER;
980
981 /*
982 * Do the job.
983 */
984 return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
985}
986
987EXPORT_SYMBOL(SUPDrvLinuxIDC);
988
989
990/**
991 * Initializes any OS specific object creator fields.
992 */
993void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
994{
995 NOREF(pObj);
996 NOREF(pSession);
997}
998
999
1000/**
1001 * Checks if the session can access the object.
1002 *
1003 * @returns true if a decision has been made.
1004 * @returns false if the default access policy should be applied.
1005 *
1006 * @param pObj The object in question.
1007 * @param pSession The session wanting to access the object.
1008 * @param pszObjName The object name, can be NULL.
1009 * @param prc Where to store the result when returning true.
1010 */
1011bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
1012{
1013 NOREF(pObj);
1014 NOREF(pSession);
1015 NOREF(pszObjName);
1016 NOREF(prc);
1017 return false;
1018}
1019
1020
1021bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
1022{
1023 return force_async_tsc != 0;
1024}
1025
1026
1027int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
1028{
1029 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
1030 return VERR_NOT_SUPPORTED;
1031}
1032
1033
1034int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
1035{
1036 NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
1037 return VERR_NOT_SUPPORTED;
1038}
1039
1040
1041int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits)
1042{
1043 NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits);
1044 return VERR_NOT_SUPPORTED;
1045}
1046
1047
1048void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
1049{
1050 NOREF(pDevExt); NOREF(pImage);
1051}
1052
1053
1054/**
1055 * Converts a supdrv error code to an linux error code.
1056 *
1057 * @returns corresponding linux error code.
1058 * @param rc IPRT status code.
1059 */
1060static int VBoxDrvLinuxErr2LinuxErr(int rc)
1061{
1062 switch (rc)
1063 {
1064 case VINF_SUCCESS: return 0;
1065 case VERR_GENERAL_FAILURE: return -EACCES;
1066 case VERR_INVALID_PARAMETER: return -EINVAL;
1067 case VERR_INVALID_MAGIC: return -EILSEQ;
1068 case VERR_INVALID_HANDLE: return -ENXIO;
1069 case VERR_INVALID_POINTER: return -EFAULT;
1070 case VERR_LOCK_FAILED: return -ENOLCK;
1071 case VERR_ALREADY_LOADED: return -EEXIST;
1072 case VERR_PERMISSION_DENIED: return -EPERM;
1073 case VERR_VERSION_MISMATCH: return -ENOSYS;
1074 case VERR_IDT_FAILED: return -1000;
1075 }
1076
1077 return -EPERM;
1078}
1079
1080
1081RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
1082{
1083#if 1
1084 va_list args;
1085 char szMsg[512];
1086
1087 va_start(args, pszFormat);
1088 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
1089 szMsg[sizeof(szMsg) - 1] = '\0';
1090 printk("%s", szMsg);
1091 va_end(args);
1092#else
1093 /* forward to printf - needs some more GCC hacking to fix ebp... */
1094 __asm__ __volatile__ ("mov %0, %esp\n\t"
1095 "jmp %1\n\t",
1096 :: "r" ((uintptr_t)&pszFormat - 4),
1097 "m" (printk));
1098#endif
1099 return 0;
1100}
1101
1102module_init(VBoxDrvLinuxInit);
1103module_exit(VBoxDrvLinuxUnload);
1104
1105MODULE_AUTHOR("Sun Microsystems, Inc.");
1106MODULE_DESCRIPTION("VirtualBox Support Driver");
1107MODULE_LICENSE("GPL");
1108#ifdef MODULE_VERSION
1109MODULE_VERSION(VBOX_VERSION_STRING " (" RT_XSTR(SUPDRV_IOC_VERSION) ")");
1110#endif
1111
1112module_param(force_async_tsc, int, 0444);
1113MODULE_PARM_DESC(force_async_tsc, "force the asynchronous TSC mode");
1114
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette