1 | /* $Id: VBoxGuestR0LibIdc-win.cpp 72627 2018-06-20 13:53:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLib - Ring-0 Support Library for VBoxGuest, IDC, Windows specific.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #include <iprt/nt/nt.h>
|
---|
36 | #include "VBoxGuestR0LibInternal.h"
|
---|
37 | #include <VBox/VBoxGuest.h>
|
---|
38 | #include <VBox/err.h>
|
---|
39 | #include <VBox/log.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Internal I/O Control call worker.
|
---|
44 | *
|
---|
45 | * @returns VBox status code.
|
---|
46 | * @param pDeviceObject The device object to call.
|
---|
47 | * @param pFileObject The file object for the connection.
|
---|
48 | * @param uReq The request.
|
---|
49 | * @param pReq The request packet.
|
---|
50 | */
|
---|
51 | static int vbglR0IdcNtCallInternal(PDEVICE_OBJECT pDeviceObject, PFILE_OBJECT pFileObject, uint32_t uReq, PVBGLREQHDR pReq)
|
---|
52 | {
|
---|
53 | int rc;
|
---|
54 | NTSTATUS rcNt;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Build the request.
|
---|
58 | *
|
---|
59 | * We want to avoid double buffering of the request, therefore we don't
|
---|
60 | * specify any request pointers or sizes when asking the kernel to build
|
---|
61 | * the IRP for us, but instead do that part our selves.
|
---|
62 | */
|
---|
63 | KEVENT Event;
|
---|
64 | KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
---|
65 |
|
---|
66 | IO_STATUS_BLOCK IoStatusBlock = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
67 | #if 0
|
---|
68 | PIRP pIrp = IoBuildDeviceIoControlRequest(uReq, /* IoControlCode */
|
---|
69 | pDeviceObject,
|
---|
70 | pReq, /* InputBuffer */
|
---|
71 | pReq->cbIn, /* InputBufferLength */
|
---|
72 | pReq, /* OutputBuffer */
|
---|
73 | pReq->cbOut, /* OutputBufferLength */
|
---|
74 | TRUE, /* InternalDeviceIoControl (=> IRP_MJ_INTERNAL_DEVICE_CONTROL) */
|
---|
75 | &Event, /* Event */
|
---|
76 | &IoStatusBlock); /* IoStatusBlock */
|
---|
77 | #else
|
---|
78 | PIRP pIrp = IoBuildDeviceIoControlRequest(uReq, /* IoControlCode */
|
---|
79 | pDeviceObject,
|
---|
80 | NULL, /* InputBuffer */
|
---|
81 | 0, /* InputBufferLength */
|
---|
82 | NULL, /* OutputBuffer */
|
---|
83 | 0, /* OutputBufferLength */
|
---|
84 | TRUE, /* InternalDeviceIoControl (=> IRP_MJ_INTERNAL_DEVICE_CONTROL) */
|
---|
85 | &Event, /* Event */
|
---|
86 | &IoStatusBlock); /* IoStatusBlock */
|
---|
87 | #endif
|
---|
88 | if (pIrp)
|
---|
89 | {
|
---|
90 | #if 0
|
---|
91 | IoGetNextIrpStackLocation(pIrp)->FileObject = pFileObject;
|
---|
92 | #else
|
---|
93 | pIrp->Flags |= IRP_SYNCHRONOUS_API;
|
---|
94 | pIrp->UserBuffer = pReq;
|
---|
95 | pIrp->AssociatedIrp.SystemBuffer = pReq;
|
---|
96 | PIO_STACK_LOCATION pStack = IoGetNextIrpStackLocation(pIrp);
|
---|
97 | pStack->FileObject = pFileObject;
|
---|
98 | pStack->Parameters.DeviceIoControl.OutputBufferLength = pReq->cbOut;
|
---|
99 | pStack->Parameters.DeviceIoControl.InputBufferLength = pReq->cbIn;
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Call the driver, wait for an async request to complete (should never happen).
|
---|
104 | */
|
---|
105 | rcNt = IoCallDriver(pDeviceObject, pIrp);
|
---|
106 | if (rcNt == STATUS_PENDING)
|
---|
107 | rcNt = KeWaitForSingleObject(&Event, /* Object */
|
---|
108 | Executive, /* WaitReason */
|
---|
109 | KernelMode, /* WaitMode */
|
---|
110 | FALSE, /* Alertable */
|
---|
111 | NULL); /* TimeOut */
|
---|
112 | if (NT_SUCCESS(rcNt))
|
---|
113 | rcNt = IoStatusBlock.Status;
|
---|
114 | if (NT_SUCCESS(rcNt))
|
---|
115 | rc = pReq->rc;
|
---|
116 | else
|
---|
117 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | rc = VERR_NO_MEMORY;
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | int VBOXCALL vbglR0IdcNativeOpen(PVBGLIDCHANDLE pHandle, PVBGLIOCIDCCONNECT pReq)
|
---|
126 | {
|
---|
127 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
128 | PFILE_OBJECT pFileObject = NULL;
|
---|
129 | UNICODE_STRING wszDeviceName;
|
---|
130 | NTSTATUS rcNt;
|
---|
131 | int rc;
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Get the device object pointer.
|
---|
135 | */
|
---|
136 | RtlInitUnicodeString(&wszDeviceName, VBOXGUEST_DEVICE_NAME_NT);
|
---|
137 | rcNt = IoGetDeviceObjectPointer(&wszDeviceName, FILE_ALL_ACCESS, &pFileObject, &pDeviceObject);
|
---|
138 | if (NT_SUCCESS(rcNt))
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Make the connection call.
|
---|
142 | */
|
---|
143 | rc = vbglR0IdcNtCallInternal(pDeviceObject, pFileObject, VBGL_IOCTL_IDC_CONNECT, &pReq->Hdr);
|
---|
144 | if (RT_SUCCESS(rc) && RT_SUCCESS(pReq->Hdr.rc))
|
---|
145 | {
|
---|
146 | pHandle->s.pDeviceObject = pDeviceObject;
|
---|
147 | pHandle->s.pFileObject = pFileObject;
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* only the file object. */
|
---|
152 | ObDereferenceObject(pFileObject);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
156 |
|
---|
157 | pHandle->s.pDeviceObject = NULL;
|
---|
158 | pHandle->s.pFileObject = NULL;
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | int VBOXCALL vbglR0IdcNativeClose(PVBGLIDCHANDLE pHandle, PVBGLIOCIDCDISCONNECT pReq)
|
---|
164 | {
|
---|
165 | PFILE_OBJECT pFileObject = pHandle->s.pFileObject;
|
---|
166 | int rc = vbglR0IdcNtCallInternal(pHandle->s.pDeviceObject, pFileObject, VBGL_IOCTL_IDC_DISCONNECT, &pReq->Hdr);
|
---|
167 | if (RT_SUCCESS(rc) && RT_SUCCESS(pReq->Hdr.rc))
|
---|
168 | {
|
---|
169 | pHandle->s.pDeviceObject = NULL;
|
---|
170 | pHandle->s.pFileObject = NULL;
|
---|
171 | ObDereferenceObject(pFileObject);
|
---|
172 | }
|
---|
173 |
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Makes an IDC call, returning only the I/O control status code.
|
---|
180 | *
|
---|
181 | * @returns VBox status code (the I/O control failure status).
|
---|
182 | * @param pHandle The IDC handle.
|
---|
183 | * @param uReq The request number.
|
---|
184 | * @param pReqHdr The request header.
|
---|
185 | * @param cbReq The request size.
|
---|
186 | */
|
---|
187 | DECLR0VBGL(int) VbglR0IdcCallRaw(PVBGLIDCHANDLE pHandle, uintptr_t uReq, PVBGLREQHDR pReqHdr, uint32_t cbReq)
|
---|
188 | {
|
---|
189 | NOREF(cbReq);
|
---|
190 | return vbglR0IdcNtCallInternal(pHandle->s.pDeviceObject, pHandle->s.pFileObject, (uint32_t)uReq, pReqHdr);
|
---|
191 | }
|
---|
192 |
|
---|