VirtualBox

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

Last change on this file since 7014 was 7014, checked in by vboxsync, 17 years ago

Making the logging more useful.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.6 KB
Line 
1/** @file
2 * The VirtualBox Support Driver - Linux hosts.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 * Some lines of code to disable the local APIC on x86_64 machines taken
25 * from a Mandriva patch by Gwenole Beauchesne <[email protected]>.
26 */
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "SUPDRV.h"
32#include "the-linux-kernel.h"
33#include "version-generated.h"
34
35#include <iprt/assert.h>
36#include <iprt/spinlock.h>
37#include <iprt/semaphore.h>
38#include <iprt/initterm.h>
39#include <iprt/process.h>
40#include <iprt/err.h>
41#include <iprt/mem.h>
42#include <iprt/log.h>
43
44#include <linux/sched.h>
45#ifdef CONFIG_DEVFS_FS
46# include <linux/devfs_fs_kernel.h>
47#endif
48#ifdef CONFIG_VBOXDRV_AS_MISC
49# include <linux/miscdevice.h>
50#endif
51#ifdef CONFIG_X86_LOCAL_APIC
52# include <asm/apic.h>
53# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
54# include <asm/nmi.h>
55# endif
56#endif
57
58#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
59# include <asm/pgtable.h>
60# define global_flush_tlb __flush_tlb_global
61#endif
62
63#include <iprt/mem.h>
64
65
66/* devfs defines */
67#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
68# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
69
70# define VBOX_REGISTER_DEVFS() \
71({ \
72 void *rc = NULL; \
73 if (devfs_mk_cdev(MKDEV(DEVICE_MAJOR, 0), \
74 S_IFCHR | S_IRUGO | S_IWUGO, \
75 DEVICE_NAME) == 0) \
76 rc = (void *)' '; /* return not NULL */ \
77 rc; \
78 })
79
80# define VBOX_UNREGISTER_DEVFS(handle) \
81 devfs_remove(DEVICE_NAME);
82
83# else /* < 2.6.0 */
84
85# define VBOX_REGISTER_DEVFS() \
86 devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, \
87 DEVICE_MAJOR, 0, \
88 S_IFCHR | S_IRUGO | S_IWUGO, \
89 &gFileOpsVBoxDrv, NULL)
90
91# define VBOX_UNREGISTER_DEVFS(handle) \
92 if (handle != NULL) \
93 devfs_unregister(handle)
94
95# endif /* < 2.6.0 */
96#endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
97
98#ifndef CONFIG_VBOXDRV_AS_MISC
99# if defined(CONFIG_DEVFS_FS) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 0)
100# define VBOX_REGISTER_DEVICE(a,b,c) devfs_register_chrdev(a,b,c)
101# define VBOX_UNREGISTER_DEVICE(a,b) devfs_unregister_chrdev(a,b)
102# else
103# define VBOX_REGISTER_DEVICE(a,b,c) register_chrdev(a,b,c)
104# define VBOX_UNREGISTER_DEVICE(a,b) unregister_chrdev(a,b)
105# endif
106#endif /* !CONFIG_VBOXDRV_AS_MISC */
107
108
109#ifdef CONFIG_X86_HIGH_ENTRY
110# error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
111#endif
112
113/*
114 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
115 */
116#if defined(RT_ARCH_AMD64)
117# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
118#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
119# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
120#else
121# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
122#endif
123
124/*
125 * The redhat hack section.
126 * - The current hacks are for 2.4.21-15.EL only.
127 */
128#ifndef NO_REDHAT_HACKS
129/* accounting. */
130# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
131# ifdef VM_ACCOUNT
132# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c, 0) /* should it be 1 or 0? */
133# endif
134# endif
135
136/* backported remap_page_range. */
137# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
138# include <asm/tlb.h>
139# ifdef tlb_vma /* probably not good enough... */
140# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
141# endif
142# endif
143
144# ifndef RT_ARCH_AMD64
145/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
146 * the page attributes from PAGE_KERNEL to something else, because there appears
147 * to be a bug in one of the many patches that redhat applied.
148 * It should be safe to do this on less buggy linux kernels too. ;-)
149 */
150# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
151 do { \
152 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
153 change_page_attr(pPages, cPages, prot); \
154 change_page_attr(pPages, cPages, prot); \
155 } while (0)
156# endif
157#endif /* !NO_REDHAT_HACKS */
158
159
160#ifndef MY_DO_MUNMAP
161# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c)
162#endif
163
164#ifndef MY_CHANGE_PAGE_ATTR
165# ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
166# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
167 do { \
168 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
169 change_page_attr(pPages, cPages, prot); \
170 } while (0)
171# else
172# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
173# endif
174#endif
175
176
177/** @def ONE_MSEC_IN_JIFFIES
178 * The number of jiffies that make up 1 millisecond. Must be at least 1! */
179#if HZ <= 1000
180# define ONE_MSEC_IN_JIFFIES 1
181#elif !(HZ % 1000)
182# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
183#else
184# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
185# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
186#endif
187
188/** @def TICK_NSEC
189 * The time between ticks in nsec */
190#ifndef TICK_NSEC
191# define TICK_NSEC (1000000UL / HZ)
192#endif
193
194#ifdef CONFIG_X86_LOCAL_APIC
195
196/* If an NMI occurs while we are inside the world switcher the machine will
197 * crash. The Linux NMI watchdog generates periodic NMIs increasing a counter
198 * which is compared with another counter increased in the timer interrupt
199 * handler. We disable the NMI watchdog.
200 *
201 * - Linux >= 2.6.21: The watchdog is disabled by default on i386 and x86_64.
202 * - Linux < 2.6.21: The watchdog is normally enabled by default on x86_64
203 * and disabled on i386.
204 */
205# if defined(RT_ARCH_AMD64)
206# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
207# define DO_DISABLE_NMI 1
208# endif
209# endif
210
211# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
212extern int nmi_active;
213# define nmi_atomic_read(P) *(P)
214# define nmi_atomic_set(P, V) *(P) = (V)
215# define nmi_atomic_dec(P) nmi_atomic_set(P, 0)
216# else
217# define nmi_atomic_read(P) atomic_read(P)
218# define nmi_atomic_set(P, V) atomic_set(P, V)
219# define nmi_atomic_dec(P) atomic_dec(P)
220# endif
221
222# ifndef X86_FEATURE_ARCH_PERFMON
223# define X86_FEATURE_ARCH_PERFMON (3*32+9) /* Intel Architectural PerfMon */
224# endif
225# ifndef MSR_ARCH_PERFMON_EVENTSEL0
226# define MSR_ARCH_PERFMON_EVENTSEL0 0x186
227# endif
228# ifndef ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT
229# define ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT (1 << 0)
230# endif
231
232#endif /* CONFIG_X86_LOCAL_APIC */
233
234#define xstr(s) str(s)
235#define str(s) #s
236
237/*******************************************************************************
238* Defined Constants And Macros *
239*******************************************************************************/
240/**
241 * Device extention & session data association structure.
242 */
243static SUPDRVDEVEXT g_DevExt;
244
245/** Timer structure for the GIP update. */
246static VBOXKTIMER g_GipTimer;
247/** Pointer to the page structure for the GIP. */
248struct page *g_pGipPage;
249
250/** Registered devfs device handle. */
251#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
252# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
253static void *g_hDevFsVBoxDrv = NULL;
254# else
255static devfs_handle_t g_hDevFsVBoxDrv = NULL;
256# endif
257#endif
258
259#ifndef CONFIG_VBOXDRV_AS_MISC
260/** Module major number */
261#define DEVICE_MAJOR 234
262/** Saved major device number */
263static int g_iModuleMajor;
264#endif /* !CONFIG_VBOXDRV_AS_MISC */
265
266/** Module parameter.
267 * Not prefixed because the name is used by macros and the end of this file. */
268static int force_async_tsc = 0;
269
270/** The module name. */
271#define DEVICE_NAME "vboxdrv"
272
273#ifdef RT_ARCH_AMD64
274/**
275 * Memory for the executable memory heap (in IPRT).
276 */
277extern uint8_t g_abExecMemory[1572864]; /* 1.5 MB */
278__asm__(".section execmemory, \"awx\", @progbits\n\t"
279 ".align 32\n\t"
280 ".globl g_abExecMemory\n"
281 "g_abExecMemory:\n\t"
282 ".zero 1572864\n\t"
283 ".type g_abExecMemory, @object\n\t"
284 ".size g_abExecMemory, 1572864\n\t"
285 ".text\n\t");
286#endif
287
288
289/*******************************************************************************
290* Internal Functions *
291*******************************************************************************/
292#ifdef VBOX_HRTIMER
293typedef enum hrtimer_restart (*PFNVBOXKTIMER)(struct hrtimer *);
294#else
295typedef void (*PFNVBOXKTIMER)(unsigned long);
296#endif
297
298static int VBoxDrvLinuxInit(void);
299static void VBoxDrvLinuxUnload(void);
300static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp);
301static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp);
302#ifdef HAVE_UNLOCKED_IOCTL
303static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
304#else
305static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
306#endif
307static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
308static int VBoxDrvLinuxInitGip(PSUPDRVDEVEXT pDevExt);
309static int VBoxDrvLinuxTermGip(PSUPDRVDEVEXT pDevExt);
310#ifdef VBOX_HRTIMER
311static enum hrtimer_restart VBoxDrvLinuxGipTimer(struct hrtimer *pTimer);
312#else
313static void VBoxDrvLinuxGipTimer(unsigned long ulUser);
314#endif
315#ifdef CONFIG_SMP
316# ifdef VBOX_HRTIMER
317static enum hrtimer_restart VBoxDrvLinuxGipTimerPerCpu(struct hrtimer *pTimer);
318# else
319static void VBoxDrvLinuxGipTimerPerCpu(unsigned long ulUser);
320# endif
321static void VBoxDrvLinuxGipResumePerCpu(void *pvUser);
322#endif
323static int VBoxDrvLinuxErr2LinuxErr(int);
324
325
326/** The file_operations structure. */
327static struct file_operations gFileOpsVBoxDrv =
328{
329 owner: THIS_MODULE,
330 open: VBoxDrvLinuxCreate,
331 release: VBoxDrvLinuxClose,
332#ifdef HAVE_UNLOCKED_IOCTL
333 unlocked_ioctl: VBoxDrvLinuxIOCtl,
334#else
335 ioctl: VBoxDrvLinuxIOCtl,
336#endif
337};
338
339#ifdef CONFIG_VBOXDRV_AS_MISC
340/** The miscdevice structure. */
341static struct miscdevice gMiscDevice =
342{
343 minor: MISC_DYNAMIC_MINOR,
344 name: DEVICE_NAME,
345 fops: &gFileOpsVBoxDrv,
346# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && \
347 LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
348 devfs_name: DEVICE_NAME,
349# endif
350};
351#endif
352
353static inline void vbox_ktimer_init(PVBOXKTIMER pTimer, PFNVBOXKTIMER pfnFunction, unsigned long ulData)
354{
355#ifdef VBOX_HRTIMER
356 hrtimer_init(pTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
357 pTimer->function = pfnFunction;
358#else
359 init_timer(pTimer);
360 pTimer->data = ulData;
361 pTimer->function = pfnFunction;
362 pTimer->expires = jiffies;
363#endif
364}
365
366static inline void vbox_ktimer_start(PVBOXKTIMER pTimer)
367{
368#ifdef VBOX_HRTIMER
369 hrtimer_start(pTimer, ktime_add_ns(ktime_get(), 1000000), HRTIMER_MODE_ABS);
370#else
371 mod_timer(pTimer, jiffies);
372#endif
373}
374
375static inline void vbox_ktimer_stop(PVBOXKTIMER pTimer)
376{
377#ifdef VBOX_HRTIMER
378 hrtimer_cancel(pTimer);
379#else
380 if (timer_pending(pTimer))
381 del_timer_sync(pTimer);
382#endif
383}
384
385#ifdef CONFIG_X86_LOCAL_APIC
386# ifdef DO_DISABLE_NMI
387
388/** Stop AMD NMI watchdog (x86_64 only). */
389static int stop_k7_watchdog(void)
390{
391 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
392 return 1;
393}
394
395/** Stop Intel P4 NMI watchdog (x86_64 only). */
396static int stop_p4_watchdog(void)
397{
398 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
399 wrmsr(MSR_P4_IQ_CCCR1, 0, 0);
400 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
401 return 1;
402}
403
404/** The new method of detecting the event counter */
405static int stop_intel_arch_watchdog(void)
406{
407 unsigned ebx;
408
409 ebx = cpuid_ebx(10);
410 if (!(ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
411 wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, 0, 0);
412 return 1;
413}
414
415/** Stop NMI watchdog. */
416static void vbox_stop_apic_nmi_watchdog(void *unused)
417{
418 int stopped = 0;
419
420 /* only support LOCAL and IO APICs for now */
421 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
422 (nmi_watchdog != NMI_IO_APIC))
423 return;
424
425 if (nmi_watchdog == NMI_LOCAL_APIC)
426 {
427 switch (boot_cpu_data.x86_vendor)
428 {
429 case X86_VENDOR_AMD:
430 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
431 return;
432 stopped = stop_k7_watchdog();
433 break;
434 case X86_VENDOR_INTEL:
435 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
436 {
437 stopped = stop_intel_arch_watchdog();
438 break;
439 }
440 stopped = stop_p4_watchdog();
441 break;
442 default:
443 return;
444 }
445 }
446
447 if (stopped)
448 nmi_atomic_dec(&nmi_active);
449}
450
451/** Disable LAPIC NMI watchdog. */
452static void disable_lapic_nmi_watchdog(void)
453{
454 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
455
456 if (nmi_atomic_read(&nmi_active) <= 0)
457 return;
458
459 on_each_cpu(vbox_stop_apic_nmi_watchdog, NULL, 1, 1);
460
461 BUG_ON(nmi_atomic_read(&nmi_active) != 0);
462
463 /* tell do_nmi() and others that we're not active any more */
464 nmi_watchdog = NMI_NONE;
465}
466
467/** Shutdown NMI. */
468static void nmi_cpu_shutdown(void * dummy)
469{
470 unsigned int vERR, vPC;
471
472 vPC = apic_read(APIC_LVTPC);
473
474 if ((GET_APIC_DELIVERY_MODE(vPC) == APIC_MODE_NMI) && !(vPC & APIC_LVT_MASKED))
475 {
476 vERR = apic_read(APIC_LVTERR);
477 apic_write(APIC_LVTERR, vERR | APIC_LVT_MASKED);
478 apic_write(APIC_LVTPC, vPC | APIC_LVT_MASKED);
479 apic_write(APIC_LVTERR, vERR);
480 }
481}
482
483static void nmi_shutdown(void)
484{
485 on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
486}
487# endif /* DO_DISABLE_NMI */
488#endif /* CONFIG_X86_LOCAL_APIC */
489
490/**
491 * Initialize module.
492 *
493 * @returns appropriate status code.
494 */
495static int __init VBoxDrvLinuxInit(void)
496{
497 int rc;
498
499 dprintf(("VBoxDrv::ModuleInit\n"));
500
501#ifdef CONFIG_X86_LOCAL_APIC
502 /*
503 * If an NMI occurs while we are inside the world switcher the macine will crash.
504 * The Linux NMI watchdog generates periodic NMIs increasing a counter which is
505 * compared with another counter increased in the timer interrupt handler. Therefore
506 * we don't allow to setup an NMI watchdog.
507 */
508# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
509 /*
510 * First test: NMI actiated? Works only works with Linux 2.6 -- 2.4 does not export
511 * the nmi_watchdog variable.
512 */
513# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || \
514 (defined CONFIG_X86_64 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
515# ifdef DO_DISABLE_NMI
516 if (nmi_atomic_read(&nmi_active) > 0)
517 {
518 printk(KERN_DEBUG DEVICE_NAME ": Trying to deactivate the NMI watchdog...\n");
519
520 switch (nmi_watchdog)
521 {
522 case NMI_LOCAL_APIC:
523 disable_lapic_nmi_watchdog();
524 break;
525 case NMI_NONE:
526 nmi_atomic_dec(&nmi_active);
527 break;
528 }
529
530 if (nmi_atomic_read(&nmi_active) == 0)
531 {
532 nmi_shutdown();
533 printk(KERN_DEBUG DEVICE_NAME ": Successfully done.\n");
534 }
535 else
536 printk(KERN_DEBUG DEVICE_NAME ": Failed!\n");
537 }
538# endif /* DO_DISABLE_NMI */
539
540 /*
541 * Permanent IO_APIC mode active? No way to handle this!
542 */
543 if (nmi_watchdog == NMI_IO_APIC)
544 {
545 printk(KERN_ERR DEVICE_NAME
546 ": NMI watchdog in IO_APIC mode active -- refused to load the kernel module!\n"
547 DEVICE_NAME
548 ": Please disable the NMI watchdog by specifying 'nmi_watchdog=0' at kernel\n"
549 DEVICE_NAME
550 ": command line.\n");
551 return -EINVAL;
552 }
553
554 /*
555 * See arch/i386/kernel/nmi.c on >= 2.6.19: -1 means it can never enabled again
556 */
557 nmi_atomic_set(&nmi_active, -1);
558 printk(KERN_DEBUG DEVICE_NAME ": Trying to deactivate the NMI watchdog permanently...\n");
559
560 /*
561 * Now fall through and see if it actually was enabled before. If so, fail
562 * as we cannot deactivate it cleanly from here.
563 */
564# else /* < 2.6.19 */
565 /*
566 * Older 2.6 kernels: nmi_watchdog is not initalized by default
567 */
568 if (nmi_watchdog != NMI_NONE)
569 goto nmi_activated;
570# endif
571# endif /* >= 2.6.0 */
572
573 /*
574 * Second test: Interrupt generated by performance counter not masked and can
575 * generate an NMI. Works also with Linux 2.4.
576 */
577 {
578 unsigned int v, ver, maxlvt;
579
580 v = apic_read(APIC_LVR);
581 ver = GET_APIC_VERSION(v);
582 /* 82489DXs do not report # of LVT entries. */
583 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
584 if (maxlvt >= 4)
585 {
586 /* Read status of performance counter IRQ vector */
587 v = apic_read(APIC_LVTPC);
588
589 /* performance counter generates NMI and is not masked? */
590 if ((GET_APIC_DELIVERY_MODE(v) == APIC_MODE_NMI) && !(v & APIC_LVT_MASKED))
591 {
592# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || \
593 (defined CONFIG_X86_64 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
594 printk(KERN_ERR DEVICE_NAME
595 ": NMI watchdog either active or at least initialized. Please disable the NMI\n"
596 DEVICE_NAME
597 ": watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
598 return -EINVAL;
599# else /* < 2.6.19 */
600# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
601nmi_activated:
602# endif
603 printk(KERN_ERR DEVICE_NAME
604 ": NMI watchdog active -- refused to load the kernel module! Please disable\n"
605 DEVICE_NAME
606 ": the NMI watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
607 return -EINVAL;
608# endif /* >= 2.6.19 */
609 }
610 }
611 }
612# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
613 printk(KERN_DEBUG DEVICE_NAME ": Successfully done.\n");
614# endif /* >= 2.6.19 */
615#endif /* CONFIG_X86_LOCAL_APIC */
616
617#ifdef CONFIG_VBOXDRV_AS_MISC
618 rc = misc_register(&gMiscDevice);
619 if (rc)
620 {
621 printk(KERN_ERR DEVICE_NAME ": Can't register misc device! rc=%d\n", rc);
622 return rc;
623 }
624#else /* !CONFIG_VBOXDRV_AS_MISC */
625 /*
626 * Register character device.
627 */
628 g_iModuleMajor = DEVICE_MAJOR;
629 rc = VBOX_REGISTER_DEVICE((dev_t)g_iModuleMajor, DEVICE_NAME, &gFileOpsVBoxDrv);
630 if (rc < 0)
631 {
632 dprintf(("VBOX_REGISTER_DEVICE failed with rc=%#x!\n", rc));
633 return rc;
634 }
635
636 /*
637 * Save returned module major number
638 */
639 if (DEVICE_MAJOR != 0)
640 g_iModuleMajor = DEVICE_MAJOR;
641 else
642 g_iModuleMajor = rc;
643 rc = 0;
644
645#ifdef CONFIG_DEVFS_FS
646 /*
647 * Register a device entry
648 */
649 g_hDevFsVBoxDrv = VBOX_REGISTER_DEVFS();
650 if (g_hDevFsVBoxDrv == NULL)
651 {
652 dprintf(("devfs_register failed!\n"));
653 rc = -EINVAL;
654 }
655#endif
656#endif /* !CONFIG_VBOXDRV_AS_MISC */
657 if (!rc)
658 {
659 /*
660 * Initialize the runtime.
661 * On AMD64 we'll have to donate the high rwx memory block to the exec allocator.
662 */
663 rc = RTR0Init(0);
664 if (RT_SUCCESS(rc))
665 {
666#ifdef RT_ARCH_AMD64
667 rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
668#endif
669 /*
670 * Initialize the device extension.
671 */
672 if (RT_SUCCESS(rc))
673 rc = supdrvInitDevExt(&g_DevExt);
674 if (!rc)
675 {
676 /*
677 * Create the GIP page.
678 */
679 rc = VBoxDrvLinuxInitGip(&g_DevExt);
680 if (!rc)
681 {
682 printk(KERN_INFO DEVICE_NAME ": TSC mode is %s, kernel timer mode is "
683#ifdef VBOX_HRTIMER
684 "'high-res'"
685#else
686 "'normal'"
687#endif
688 ".\n",
689 g_DevExt.pGip->u32Mode == SUPGIPMODE_SYNC_TSC ? "'synchronous'" : "'asynchronous'");
690 LogFlow(("VBoxDrv::ModuleInit returning %#x\n", rc));
691 printk(KERN_DEBUG DEVICE_NAME ": Successfully loaded version "
692 VBOX_VERSION_STRING " (interface " xstr(SUPDRVIOC_VERSION) ").\n");
693 return rc;
694 }
695
696 supdrvDeleteDevExt(&g_DevExt);
697 }
698 else
699 rc = -EINVAL;
700 RTR0Term();
701 }
702 else
703 rc = -EINVAL;
704
705 /*
706 * Failed, cleanup and return the error code.
707 */
708#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
709 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
710#endif
711 }
712#ifdef CONFIG_VBOXDRV_AS_MISC
713 misc_deregister(&gMiscDevice);
714 dprintf(("VBoxDrv::ModuleInit returning %#x (minor:%d)\n", rc, gMiscDevice.minor));
715#else
716 VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
717 dprintf(("VBoxDrv::ModuleInit returning %#x (major:%d)\n", rc, g_iModuleMajor));
718#endif
719 return rc;
720}
721
722
723/**
724 * Unload the module.
725 */
726static void __exit VBoxDrvLinuxUnload(void)
727{
728 int rc;
729 dprintf(("VBoxDrvLinuxUnload\n"));
730
731 /*
732 * I Don't think it's possible to unload a driver which processes have
733 * opened, at least we'll blindly assume that here.
734 */
735#ifdef CONFIG_VBOXDRV_AS_MISC
736 rc = misc_deregister(&gMiscDevice);
737 if (rc < 0)
738 {
739 dprintf(("misc_deregister failed with rc=%#x\n", rc));
740 }
741#else /* !CONFIG_VBOXDRV_AS_MISC */
742# ifdef CONFIG_DEVFS_FS
743 /*
744 * Unregister a device entry
745 */
746 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
747# endif /* devfs */
748 rc = VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
749 if (rc < 0)
750 {
751 dprintf(("VBOX_UNREGISTER_DEVICE failed with rc=%#x (major:%d)\n", rc, g_iModuleMajor));
752 }
753#endif /* !CONFIG_VBOXDRV_AS_MISC */
754
755 /*
756 * Destroy GIP, delete the device extension and terminate IPRT.
757 */
758 VBoxDrvLinuxTermGip(&g_DevExt);
759 supdrvDeleteDevExt(&g_DevExt);
760 RTR0Term();
761}
762
763
764/**
765 * Device open. Called on open /dev/vboxdrv
766 *
767 * @param pInode Pointer to inode info structure.
768 * @param pFilp Associated file pointer.
769 */
770static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp)
771{
772 int rc;
773 PSUPDRVSESSION pSession;
774 Log(("VBoxDrvLinuxCreate: pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
775
776 /*
777 * Call common code for the rest.
778 */
779 rc = supdrvCreateSession(&g_DevExt, (PSUPDRVSESSION *)&pSession);
780 if (!rc)
781 {
782 pSession->Uid = current->euid;
783 pSession->Gid = current->egid;
784 pSession->Process = RTProcSelf();
785 pSession->R0Process = RTR0ProcHandleSelf();
786 }
787
788 pFilp->private_data = pSession;
789
790 Log(("VBoxDrvLinuxCreate: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
791 &g_DevExt, pSession, rc, VBoxDrvLinuxErr2LinuxErr(rc),
792 RTProcSelf(), current->pid, current->comm));
793 return VBoxDrvLinuxErr2LinuxErr(rc);
794}
795
796
797/**
798 * Close device.
799 *
800 * @param pInode Pointer to inode info structure.
801 * @param pFilp Associated file pointer.
802 */
803static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp)
804{
805 Log(("VBoxDrvLinuxClose: pFilp=%p pSession=%p pid=%d/%d %s\n",
806 pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
807 supdrvCloseSession(&g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
808 pFilp->private_data = NULL;
809 return 0;
810}
811
812
813/**
814 * Device I/O Control entry point.
815 *
816 * @param pFilp Associated file pointer.
817 * @param uCmd The function specified to ioctl().
818 * @param ulArg The argument specified to ioctl().
819 */
820#ifdef HAVE_UNLOCKED_IOCTL
821static long VBoxDrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
822#else
823static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
824#endif
825{
826 /*
827 * Deal with the two high-speed IOCtl that takes it's arguments from
828 * the session and iCmd, and only returns a VBox status code.
829 */
830#ifdef HAVE_UNLOCKED_IOCTL
831 if (RT_LIKELY( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
832 || uCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
833 || uCmd == SUP_IOCTL_FAST_DO_NOP))
834 return supdrvIOCtlFast(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
835 return VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg);
836
837#else /* !HAVE_UNLOCKED_IOCTL */
838
839 int rc;
840 unlock_kernel();
841 if (RT_LIKELY( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
842 || uCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
843 || uCmd == SUP_IOCTL_FAST_DO_NOP))
844 rc = supdrvIOCtlFast(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
845 else
846 rc = VBoxDrvLinuxIOCtlSlow(pFilp, uCmd, ulArg);
847 lock_kernel();
848 return rc;
849#endif /* !HAVE_UNLOCKED_IOCTL */
850}
851
852
853/**
854 * Device I/O Control entry point.
855 *
856 * @param pFilp Associated file pointer.
857 * @param uCmd The function specified to ioctl().
858 * @param ulArg The argument specified to ioctl().
859 */
860static int VBoxDrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
861{
862 int rc;
863 SUPREQHDR Hdr;
864 PSUPREQHDR pHdr;
865 uint32_t cbBuf;
866
867 Log6(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
868
869 /*
870 * Read the header.
871 */
872 if (RT_UNLIKELY(copy_from_user(&Hdr, (void *)ulArg, sizeof(Hdr))))
873 {
874 Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
875 return -EFAULT;
876 }
877 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
878 {
879 Log(("VBoxDrvLinuxIOCtl: bad header magic %#x; uCmd=%#x\n", Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, uCmd));
880 return -EINVAL;
881 }
882
883 /*
884 * Buffer the request.
885 */
886 cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
887 if (RT_UNLIKELY(cbBuf > _1M*16))
888 {
889 Log(("VBoxDrvLinuxIOCtl: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
890 return -E2BIG;
891 }
892 if (RT_UNLIKELY(cbBuf != _IOC_SIZE(uCmd) && _IOC_SIZE(uCmd)))
893 {
894 Log(("VBoxDrvLinuxIOCtl: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
895 return -EINVAL;
896 }
897 pHdr = RTMemAlloc(cbBuf);
898 if (RT_UNLIKELY(!pHdr))
899 {
900 OSDBGPRINT(("VBoxDrvLinuxIOCtl: failed to allocate buffer of %d bytes for uCmd=%#x.\n", cbBuf, uCmd));
901 return -ENOMEM;
902 }
903 if (RT_UNLIKELY(copy_from_user(pHdr, (void *)ulArg, Hdr.cbIn)))
904 {
905 Log(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx, %#x) failed; uCmd=%#x.\n", ulArg, Hdr.cbIn, uCmd));
906 RTMemFree(pHdr);
907 return -EFAULT;
908 }
909
910 /*
911 * Process the IOCtl.
912 */
913 rc = supdrvIOCtl(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data, pHdr);
914
915 /*
916 * Copy ioctl data and output buffer back to user space.
917 */
918 if (RT_LIKELY(!rc))
919 {
920 uint32_t cbOut = pHdr->cbOut;
921 if (RT_UNLIKELY(cbOut > cbBuf))
922 {
923 OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
924 cbOut = cbBuf;
925 }
926 if (RT_UNLIKELY(copy_to_user((void *)ulArg, pHdr, cbOut)))
927 {
928 /* this is really bad! */
929 OSDBGPRINT(("VBoxDrvLinuxIOCtl: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
930 rc = -EFAULT;
931 }
932 }
933 else
934 {
935 Log(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
936 rc = -EINVAL;
937 }
938 RTMemFree(pHdr);
939
940 Log6(("VBoxDrvLinuxIOCtl: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
941 return rc;
942}
943
944
945/**
946 * Initializes any OS specific object creator fields.
947 */
948void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
949{
950 NOREF(pObj);
951 NOREF(pSession);
952}
953
954
955/**
956 * Checks if the session can access the object.
957 *
958 * @returns true if a decision has been made.
959 * @returns false if the default access policy should be applied.
960 *
961 * @param pObj The object in question.
962 * @param pSession The session wanting to access the object.
963 * @param pszObjName The object name, can be NULL.
964 * @param prc Where to store the result when returning true.
965 */
966bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
967{
968 NOREF(pObj);
969 NOREF(pSession);
970 NOREF(pszObjName);
971 NOREF(prc);
972 return false;
973}
974
975
976/**
977 * Initializes the GIP.
978 *
979 * @returns negative errno.
980 * @param pDevExt Instance data. GIP stuff may be updated.
981 */
982static int VBoxDrvLinuxInitGip(PSUPDRVDEVEXT pDevExt)
983{
984 struct page *pPage;
985 dma_addr_t HCPhys;
986 PSUPGLOBALINFOPAGE pGip;
987#ifdef CONFIG_SMP
988 unsigned i;
989#endif
990 LogFlow(("VBoxDrvLinuxInitGip:\n"));
991
992 /*
993 * Allocate the page.
994 */
995 pPage = alloc_pages(GFP_USER, 0);
996 if (!pPage)
997 {
998 Log(("VBoxDrvLinuxInitGip: failed to allocate the GIP page\n"));
999 return -ENOMEM;
1000 }
1001
1002 /*
1003 * Lock the page.
1004 */
1005 SetPageReserved(pPage);
1006 g_pGipPage = pPage;
1007
1008 /*
1009 * Call common initialization routine.
1010 */
1011 HCPhys = page_to_phys(pPage);
1012 pGip = (PSUPGLOBALINFOPAGE)page_address(pPage);
1013 pDevExt->ulLastJiffies = jiffies;
1014 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * TICK_NSEC;
1015 Log(("VBoxDrvInitGIP: TICK_NSEC=%ld HZ=%d jiffies=%ld now=%lld\n",
1016 TICK_NSEC, HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
1017 supdrvGipInit(pDevExt, pGip, HCPhys, pDevExt->u64LastMonotime,
1018 HZ <= 1000 ? HZ : 1000);
1019
1020 /*
1021 * Initialize the timer.
1022 */
1023 vbox_ktimer_init(&g_GipTimer, VBoxDrvLinuxGipTimer, (unsigned long)pDevExt);
1024#ifdef CONFIG_SMP
1025 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1026 {
1027 pDevExt->aCPUs[i].u64LastMonotime = pDevExt->u64LastMonotime;
1028 pDevExt->aCPUs[i].ulLastJiffies = pDevExt->ulLastJiffies;
1029 pDevExt->aCPUs[i].iSmpProcessorId = -512;
1030 vbox_ktimer_init(&pDevExt->aCPUs[i].Timer, VBoxDrvLinuxGipTimerPerCpu, i);
1031 }
1032#endif
1033
1034 return 0;
1035}
1036
1037
1038/**
1039 * Terminates the GIP.
1040 *
1041 * @returns negative errno.
1042 * @param pDevExt Instance data. GIP stuff may be updated.
1043 */
1044static int VBoxDrvLinuxTermGip(PSUPDRVDEVEXT pDevExt)
1045{
1046 struct page *pPage;
1047 PSUPGLOBALINFOPAGE pGip;
1048#ifdef CONFIG_SMP
1049 unsigned i;
1050#endif
1051 LogFlow(("VBoxDrvLinuxTermGip:\n"));
1052
1053 /*
1054 * Delete the timer if it's pending.
1055 */
1056 vbox_ktimer_stop(&g_GipTimer);
1057#ifdef CONFIG_SMP
1058 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1059 vbox_ktimer_stop(&pDevExt->aCPUs[i].Timer);
1060#endif
1061
1062 /*
1063 * Uninitialize the content.
1064 */
1065 pGip = pDevExt->pGip;
1066 pDevExt->pGip = NULL;
1067 if (pGip)
1068 supdrvGipTerm(pGip);
1069
1070 /*
1071 * Free the page.
1072 */
1073 pPage = g_pGipPage;
1074 g_pGipPage = NULL;
1075 if (pPage)
1076 {
1077 ClearPageReserved(pPage);
1078 __free_pages(pPage, 0);
1079 }
1080
1081 return 0;
1082}
1083
1084/**
1085 * Timer callback function.
1086 *
1087 * In ASYNC TSC mode this is called on the primary CPU, and we're
1088 * assuming that the CPU remains online.
1089 *
1090 * @param ulUser The device extension pointer.
1091 */
1092#ifdef VBOX_HRTIMER
1093static enum hrtimer_restart VBoxDrvLinuxGipTimer(struct hrtimer *pTimer)
1094#else
1095static void VBoxDrvLinuxGipTimer(unsigned long ulUser)
1096#endif
1097{
1098 PSUPDRVDEVEXT pDevExt;
1099 PSUPGLOBALINFOPAGE pGip;
1100 unsigned long ulNow;
1101 unsigned long ulDiff;
1102 uint64_t u64Monotime;
1103 unsigned long SavedFlags;
1104#ifdef VBOX_HRTIMER
1105 ktime_t KtNow;
1106#endif
1107
1108 local_irq_save(SavedFlags);
1109
1110 ulNow = jiffies;
1111#ifdef VBOX_HRTIMER
1112 KtNow = ktime_get();
1113 pDevExt = &g_DevExt;
1114#else
1115 pDevExt = (PSUPDRVDEVEXT)ulUser;
1116#endif
1117 pGip = pDevExt->pGip;
1118
1119#ifdef CONFIG_SMP
1120 if (pGip && pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
1121 {
1122 uint8_t iCPU = ASMGetApicId();
1123 ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1124 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1125 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1126 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1127 }
1128 else
1129#endif /* CONFIG_SMP */
1130 {
1131 ulDiff = ulNow - pDevExt->ulLastJiffies;
1132 pDevExt->ulLastJiffies = ulNow;
1133 u64Monotime = pDevExt->u64LastMonotime + ulDiff * TICK_NSEC;
1134 pDevExt->u64LastMonotime = u64Monotime;
1135 }
1136 if (RT_LIKELY(pGip))
1137 supdrvGipUpdate(pDevExt->pGip, u64Monotime);
1138 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1139 {
1140#ifdef VBOX_HRTIMER
1141 hrtimer_forward(&g_GipTimer, KtNow, ktime_set(0, 1000000));
1142#else
1143 mod_timer(&g_GipTimer, ulNow + ONE_MSEC_IN_JIFFIES);
1144#endif
1145 }
1146
1147 local_irq_restore(SavedFlags);
1148
1149#ifdef VBOX_HRTIMER
1150 return pDevExt->fGIPSuspended ? HRTIMER_NORESTART : HRTIMER_RESTART;
1151#endif
1152}
1153
1154
1155#ifdef CONFIG_SMP
1156/**
1157 * Timer callback function for the other CPUs.
1158 *
1159 * @param iTimerCPU The APIC ID of this timer.
1160 */
1161#ifdef VBOX_HRTIMER
1162static enum hrtimer_restart VBoxDrvLinuxGipTimerPerCpu(struct hrtimer *pTimer)
1163#else
1164static void VBoxDrvLinuxGipTimerPerCpu(unsigned long iTimerCPU)
1165#endif
1166{
1167 PSUPDRVDEVEXT pDevExt;
1168 PSUPGLOBALINFOPAGE pGip;
1169 uint8_t iCPU;
1170 uint64_t u64Monotime;
1171 unsigned long SavedFlags;
1172 unsigned long ulNow;
1173# ifdef VBOX_HRTIMER
1174 unsigned long iTimerCPU;
1175 ktime_t KtNow;
1176# endif
1177
1178 local_irq_save(SavedFlags);
1179
1180 ulNow = jiffies;
1181 pDevExt = &g_DevExt;
1182 pGip = pDevExt->pGip;
1183 iCPU = ASMGetApicId();
1184# ifdef VBOX_HRTIMER
1185 iTimerCPU = iCPU; /* XXX hrtimer does not support a 'data' field */
1186 KtNow = ktime_get();
1187# endif
1188
1189 if (RT_LIKELY(iCPU < RT_ELEMENTS(pGip->aCPUs)))
1190 {
1191 if (RT_LIKELY(iTimerCPU == iCPU))
1192 {
1193 unsigned long ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1194 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1195 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1196 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1197 if (RT_LIKELY(pGip))
1198 supdrvGipUpdatePerCpu(pGip, u64Monotime, iCPU);
1199 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1200 {
1201# ifdef VBOX_HRTIMER
1202 hrtimer_forward(&pDevExt->aCPUs[iCPU].Timer, KtNow, ktime_set(0, 1000000));
1203# else
1204 mod_timer(&pDevExt->aCPUs[iCPU].Timer, ulNow + ONE_MSEC_IN_JIFFIES);
1205# endif
1206 }
1207 }
1208 else
1209 printk("vboxdrv: error: GIP CPU update timer executing on the wrong CPU: apicid=%d != timer-apicid=%ld (cpuid=%d !=? timer-cpuid=%d)\n",
1210 iCPU, iTimerCPU, smp_processor_id(), pDevExt->aCPUs[iTimerCPU].iSmpProcessorId);
1211 }
1212 else
1213 printk("vboxdrv: error: APIC ID is bogus (GIP CPU update): apicid=%d max=%lu cpuid=%d\n",
1214 iCPU, (unsigned long)RT_ELEMENTS(pGip->aCPUs), smp_processor_id());
1215
1216 local_irq_restore(SavedFlags);
1217
1218# ifdef VBOX_HRTIMER
1219 return pDevExt->fGIPSuspended ? HRTIMER_NORESTART : HRTIMER_RESTART;
1220# endif
1221}
1222#endif /* CONFIG_SMP */
1223
1224
1225/**
1226 * Maps the GIP into user space.
1227 *
1228 * @returns negative errno.
1229 * @param pDevExt Instance data.
1230 */
1231int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip)
1232{
1233 int rc = 0;
1234 unsigned long ulAddr;
1235 unsigned long HCPhys = pDevExt->HCPhysGip;
1236 pgprot_t pgFlags;
1237 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_USER;
1238 LogFlow(("supdrvOSGipMap: ppGip=%p\n", ppGip));
1239
1240 /*
1241 * Allocate user space mapping and put the physical pages into it.
1242 */
1243 down_write(&current->mm->mmap_sem);
1244 ulAddr = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, 0);
1245 if (!(ulAddr & ~PAGE_MASK))
1246 {
1247#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1248 int rc2 = remap_page_range(ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1249#else
1250 int rc2 = 0;
1251 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
1252 if (vma)
1253#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1254 rc2 = remap_page_range(vma, ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1255#else
1256 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, PAGE_SIZE, pgFlags);
1257#endif
1258 else
1259 {
1260 rc = SUPDRV_ERR_NO_MEMORY;
1261 Log(("supdrvOSGipMap: no vma found for ulAddr=%#lx!\n", ulAddr));
1262 }
1263#endif
1264 if (rc2)
1265 {
1266 rc = SUPDRV_ERR_NO_MEMORY;
1267 Log(("supdrvOSGipMap: remap_page_range failed rc2=%d\n", rc2));
1268 }
1269 }
1270 else
1271 {
1272 Log(("supdrvOSGipMap: do_mmap failed ulAddr=%#lx\n", ulAddr));
1273 rc = SUPDRV_ERR_NO_MEMORY;
1274 }
1275 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
1276
1277 /*
1278 * Success?
1279 */
1280 if (!rc)
1281 {
1282 *ppGip = (PSUPGLOBALINFOPAGE)ulAddr;
1283 LogFlow(("supdrvOSGipMap: ppGip=%p\n", *ppGip));
1284 return 0;
1285 }
1286
1287 /*
1288 * Failure, cleanup and be gone.
1289 */
1290 if (ulAddr & ~PAGE_MASK)
1291 {
1292 down_write(&current->mm->mmap_sem);
1293 MY_DO_MUNMAP(current->mm, ulAddr, PAGE_SIZE);
1294 up_write(&current->mm->mmap_sem);
1295 }
1296
1297 LogFlow(("supdrvOSGipMap: returns %d\n", rc));
1298 return rc;
1299}
1300
1301
1302/**
1303 * Maps the GIP into user space.
1304 *
1305 * @returns negative errno.
1306 * @param pDevExt Instance data.
1307 */
1308int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip)
1309{
1310 LogFlow(("supdrvOSGipUnmap: pGip=%p\n", pGip));
1311 if (current->mm)
1312 {
1313 down_write(&current->mm->mmap_sem);
1314 MY_DO_MUNMAP(current->mm, (unsigned long)pGip, PAGE_SIZE);
1315 up_write(&current->mm->mmap_sem);
1316 }
1317 LogFlow(("supdrvOSGipUnmap: returns 0\n"));
1318 return 0;
1319}
1320
1321
1322/**
1323 * Resumes the GIP updating.
1324 *
1325 * @param pDevExt Instance data.
1326 */
1327void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
1328{
1329 LogFlow(("supdrvOSGipResume:\n"));
1330 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, false);
1331#ifdef CONFIG_SMP
1332 if (pDevExt->pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
1333 {
1334#endif
1335 vbox_ktimer_start(&g_GipTimer);
1336#ifdef CONFIG_SMP
1337 }
1338 else
1339 {
1340 vbox_ktimer_start(&g_GipTimer);
1341 smp_call_function(VBoxDrvLinuxGipResumePerCpu, pDevExt, 0 /* retry */, 1 /* wait */);
1342 }
1343#endif
1344}
1345
1346
1347#ifdef CONFIG_SMP
1348/**
1349 * Callback for resuming GIP updating on the other CPUs.
1350 *
1351 * This is only used when the GIP is in async tsc mode.
1352 *
1353 * @param pvUser Pointer to the device instance.
1354 */
1355static void VBoxDrvLinuxGipResumePerCpu(void *pvUser)
1356{
1357 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
1358 uint8_t iCPU = ASMGetApicId();
1359
1360 if (RT_UNLIKELY(iCPU >= RT_ELEMENTS(pDevExt->pGip->aCPUs)))
1361 {
1362 printk("vboxdrv: error: apicid=%d max=%lu cpuid=%d\n",
1363 iCPU, (unsigned long)RT_ELEMENTS(pDevExt->pGip->aCPUs), smp_processor_id());
1364 return;
1365 }
1366
1367 pDevExt->aCPUs[iCPU].iSmpProcessorId = smp_processor_id();
1368 vbox_ktimer_start(&pDevExt->aCPUs[iCPU].Timer);
1369}
1370#endif /* CONFIG_SMP */
1371
1372
1373/**
1374 * Suspends the GIP updating.
1375 *
1376 * @param pDevExt Instance data.
1377 */
1378void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
1379{
1380#ifdef CONFIG_SMP
1381 unsigned i;
1382#endif
1383 LogFlow(("supdrvOSGipSuspend:\n"));
1384 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, true);
1385
1386 vbox_ktimer_stop(&g_GipTimer);
1387#ifdef CONFIG_SMP
1388 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1389 vbox_ktimer_stop(&pDevExt->aCPUs[i].Timer);
1390#endif
1391}
1392
1393
1394/**
1395 * Get the current CPU count.
1396 * @returns Number of cpus.
1397 */
1398unsigned VBOXCALL supdrvOSGetCPUCount(void)
1399{
1400#ifdef CONFIG_SMP
1401# if defined(num_present_cpus)
1402 return num_present_cpus();
1403# elif defined(num_online_cpus)
1404 return num_online_cpus();
1405# else
1406 return smp_num_cpus;
1407# endif
1408#else
1409 return 1;
1410#endif
1411}
1412
1413/**
1414 * Force async tsc mode.
1415 * @todo add a module argument for this.
1416 */
1417bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
1418{
1419 return force_async_tsc != 0;
1420}
1421
1422
1423/**
1424 * Converts a supdrv error code to an linux error code.
1425 *
1426 * @returns corresponding linux error code.
1427 * @param rc supdrv error code (SUPDRV_ERR_* defines).
1428 */
1429static int VBoxDrvLinuxErr2LinuxErr(int rc)
1430{
1431 switch (rc)
1432 {
1433 case 0: return 0;
1434 case SUPDRV_ERR_GENERAL_FAILURE: return -EACCES;
1435 case SUPDRV_ERR_INVALID_PARAM: return -EINVAL;
1436 case SUPDRV_ERR_INVALID_MAGIC: return -EILSEQ;
1437 case SUPDRV_ERR_INVALID_HANDLE: return -ENXIO;
1438 case SUPDRV_ERR_INVALID_POINTER: return -EFAULT;
1439 case SUPDRV_ERR_LOCK_FAILED: return -ENOLCK;
1440 case SUPDRV_ERR_ALREADY_LOADED: return -EEXIST;
1441 case SUPDRV_ERR_PERMISSION_DENIED: return -EPERM;
1442 case SUPDRV_ERR_VERSION_MISMATCH: return -ENOSYS;
1443 case SUPDRV_ERR_IDT_FAILED: return -1000;
1444 }
1445
1446 return -EPERM;
1447}
1448
1449
1450RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
1451{
1452#if 1
1453 va_list args;
1454 char szMsg[512];
1455
1456 va_start(args, pszFormat);
1457 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
1458 szMsg[sizeof(szMsg) - 1] = '\0';
1459 printk("%s", szMsg);
1460 va_end(args);
1461#else
1462 /* forward to printf - needs some more GCC hacking to fix ebp... */
1463 __asm__ __volatile__ ("mov %0, %esp\n\t"
1464 "jmp %1\n\t",
1465 :: "r" ((uintptr_t)&pszFormat - 4),
1466 "m" (printk));
1467#endif
1468 return 0;
1469}
1470
1471
1472/** Runtime assert implementation for Linux Ring-0. */
1473RTDECL(bool) RTAssertDoBreakpoint(void)
1474{
1475 return true;
1476}
1477
1478
1479/** Runtime assert implementation for Linux Ring-0. */
1480RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1481{
1482 printk("!!Assertion Failed!!\n"
1483 "Expression: %s\n"
1484 "Location : %s(%d) %s\n",
1485 pszExpr, pszFile, uLine, pszFunction);
1486}
1487
1488
1489/** Runtime assert implementation for Linux Ring-0. */
1490RTDECL(void) AssertMsg2(const char *pszFormat, ...)
1491{ /* forwarder. */
1492 va_list ap;
1493 char msg[256];
1494
1495 va_start(ap, pszFormat);
1496 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
1497 msg[sizeof(msg) - 1] = '\0';
1498 printk("%s", msg);
1499 va_end(ap);
1500}
1501
1502
1503/* GCC C++ hack. */
1504unsigned __gxx_personality_v0 = 0xcccccccc;
1505
1506
1507module_init(VBoxDrvLinuxInit);
1508module_exit(VBoxDrvLinuxUnload);
1509
1510MODULE_AUTHOR("innotek GmbH");
1511MODULE_DESCRIPTION("VirtualBox Support Driver");
1512MODULE_LICENSE("GPL");
1513#ifdef MODULE_VERSION
1514MODULE_VERSION(VBOX_VERSION_STRING " (" xstr(SUPDRVIOC_VERSION) ")");
1515#endif
1516
1517#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1518module_param(force_async_tsc, int, 0444);
1519#else
1520MODULE_PARM(force_async_tsc, "i");
1521#endif
1522MODULE_PARM_DESC(force_async_tsc, "force the asynchronous TSC mode");
1523
Note: See TracBrowser for help on using the repository browser.

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