VirtualBox

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

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

not needed here anymore (stuff now done in Runtime/r0drv)

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