1 | /** @file
|
---|
2 | *
|
---|
3 | * VBoxGuestLib - A support library for VirtualBox guest additions:
|
---|
4 | * Physical memory heap
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 | #define LOG_GROUP LOG_GROUP_HGCM
|
---|
23 | #include <VBox/log.h>
|
---|
24 |
|
---|
25 | #include <VBox/VBoxGuestLib.h>
|
---|
26 | #include "SysHlp.h"
|
---|
27 |
|
---|
28 | #include <iprt/assert.h>
|
---|
29 |
|
---|
30 | #ifndef VBGL_VBOXGUEST
|
---|
31 |
|
---|
32 | #if defined (__LINUX__) && !defined (__KERNEL__)
|
---|
33 | # include <unistd.h>
|
---|
34 | # include <errno.h>
|
---|
35 | # include <sys/fcntl.h>
|
---|
36 | # include <sys/ioctl.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #ifndef __WIN__
|
---|
40 | # ifdef __cplusplus
|
---|
41 | extern "C" {
|
---|
42 | # endif
|
---|
43 | extern DECLVBGL(void *) vboxadd_cmc_open (void);
|
---|
44 | extern DECLVBGL(void) vboxadd_cmc_close (void *);
|
---|
45 | extern DECLVBGL(int) vboxadd_cmc_call (void *opaque, uint32_t func, void *data);
|
---|
46 | # ifdef __cplusplus
|
---|
47 | }
|
---|
48 | # endif
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | int vbglDriverOpen (VBGLDRIVER *pDriver)
|
---|
52 | {
|
---|
53 | #ifdef __WIN__
|
---|
54 | UNICODE_STRING uszDeviceName;
|
---|
55 | RtlInitUnicodeString (&uszDeviceName, L"\\Device\\VBoxGuest");
|
---|
56 |
|
---|
57 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
58 | PFILE_OBJECT pFileObject = NULL;
|
---|
59 |
|
---|
60 | NTSTATUS rc = IoGetDeviceObjectPointer (&uszDeviceName, FILE_ALL_ACCESS,
|
---|
61 | &pFileObject, &pDeviceObject);
|
---|
62 |
|
---|
63 | if (NT_SUCCESS (rc))
|
---|
64 | {
|
---|
65 | Log(("vbglDriverOpen VBoxGuest successful pDeviceObject=%x\n", pDeviceObject));
|
---|
66 | pDriver->pDeviceObject = pDeviceObject;
|
---|
67 | pDriver->pFileObject = pFileObject;
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 | /** @todo return RTErrConvertFromNtStatus(rc)! */
|
---|
71 | Log(("vbglDriverOpen VBoxGuest failed with ntstatus=%x\n", rc));
|
---|
72 | return rc;
|
---|
73 | #else
|
---|
74 | void *opaque;
|
---|
75 |
|
---|
76 | opaque = (void *) vboxadd_cmc_open ();
|
---|
77 | if (!opaque)
|
---|
78 | {
|
---|
79 | return VERR_NOT_IMPLEMENTED;
|
---|
80 | }
|
---|
81 | pDriver->opaque = opaque;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | #endif
|
---|
84 | }
|
---|
85 |
|
---|
86 | int vbglDriverIOCtl (VBGLDRIVER *pDriver, uint32_t u32Function, void *pvData, uint32_t cbData)
|
---|
87 | {
|
---|
88 | #ifdef __WIN__
|
---|
89 | IO_STATUS_BLOCK ioStatusBlock;
|
---|
90 |
|
---|
91 | KEVENT *pEvent;
|
---|
92 | #ifdef KEVENT_STACK_ALLOC
|
---|
93 | KEVENT eventAlloc;
|
---|
94 | pEvent = &eventAlloc;
|
---|
95 | #else
|
---|
96 | pEvent = (KEVENT *)ExAllocatePool (NonPagedPool, sizeof (KEVENT));
|
---|
97 | if (!pEvent) return VERR_NO_MEMORY;
|
---|
98 | #endif /* KEVENT_STACK_ALLOC */
|
---|
99 | KeInitializeEvent (pEvent, NotificationEvent, FALSE);
|
---|
100 |
|
---|
101 | PIRP irp = IoBuildDeviceIoControlRequest (u32Function,
|
---|
102 | pDriver->pDeviceObject,
|
---|
103 | pvData,
|
---|
104 | cbData,
|
---|
105 | pvData,
|
---|
106 | cbData,
|
---|
107 | FALSE,
|
---|
108 | pEvent,
|
---|
109 | &ioStatusBlock);
|
---|
110 | if (irp == NULL)
|
---|
111 | {
|
---|
112 | Log(("vbglDriverIOCtl: IoBuildDeviceIoControlRequest failed\n"));
|
---|
113 | #ifndef KEVENT_STACK_ALLOC
|
---|
114 | ExFreePool (pEvent);
|
---|
115 | #endif /* KEVENT_STACK_ALLOC */
|
---|
116 | return VERR_NO_MEMORY;
|
---|
117 | }
|
---|
118 |
|
---|
119 | NTSTATUS rc = IoCallDriver (pDriver->pDeviceObject, irp);
|
---|
120 |
|
---|
121 | if (rc == STATUS_PENDING)
|
---|
122 | {
|
---|
123 | Log(("vbglDriverIOCtl: STATUS_PENDING\n"));
|
---|
124 | rc = KeWaitForSingleObject(pEvent,
|
---|
125 | Executive,
|
---|
126 | KernelMode,
|
---|
127 | FALSE,
|
---|
128 | NULL);
|
---|
129 |
|
---|
130 | rc = ioStatusBlock.Status;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (!NT_SUCCESS(rc))
|
---|
134 | Log(("vbglDriverIOCtl: IoCallDriver failed with ntstatus=%x\n", rc));
|
---|
135 |
|
---|
136 | #ifndef KEVENT_STACK_ALLOC
|
---|
137 | ExFreePool (pEvent);
|
---|
138 | #endif /* KEVENT_STACK_ALLOC */
|
---|
139 |
|
---|
140 | return NT_SUCCESS(rc)? VINF_SUCCESS: VERR_VBGL_IOCTL_FAILED;
|
---|
141 | #else
|
---|
142 | return vboxadd_cmc_call (pDriver->opaque, u32Function, pvData);
|
---|
143 | #endif
|
---|
144 | }
|
---|
145 |
|
---|
146 | void vbglDriverClose (VBGLDRIVER *pDriver)
|
---|
147 | {
|
---|
148 | #ifdef __WIN__
|
---|
149 | Log(("vbglDriverClose pDeviceObject=%x\n", pDriver->pDeviceObject));
|
---|
150 | ObDereferenceObject (pDriver->pFileObject);
|
---|
151 | #else
|
---|
152 | vboxadd_cmc_close (pDriver->opaque);
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 |
|
---|
156 | #endif /* !VBGL_VBOXGUEST */
|
---|