1 | /* $Id: VBoxGuestR0LibIdc-win.cpp 68568 2017-08-31 12:10:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLib - Ring-0 Support Library for VBoxGuest, IDC, Windows specific.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/nt/nt.h>
|
---|
32 | #include "VBoxGuestR0LibInternal.h"
|
---|
33 | #include <VBox/VBoxGuest.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Internal I/O Control call worker.
|
---|
40 | *
|
---|
41 | * @returns VBox status code.
|
---|
42 | * @param pDeviceObject The device object to call.
|
---|
43 | * @param pFileObject The file object for the connection.
|
---|
44 | * @param uReq The request.
|
---|
45 | * @param pReq The request packet.
|
---|
46 | */
|
---|
47 | static int vbglR0IdcNtCallInternal(PDEVICE_OBJECT pDeviceObject, PFILE_OBJECT pFileObject, uint32_t uReq, PVBGLREQHDR pReq)
|
---|
48 | {
|
---|
49 | int rc;
|
---|
50 | NTSTATUS rcNt;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Build the request.
|
---|
54 | *
|
---|
55 | * We want to avoid double buffering of the request, therefore we don't
|
---|
56 | * specify any request pointers or sizes when asking the kernel to build
|
---|
57 | * the IRP for us, but instead do that part our selves.
|
---|
58 | */
|
---|
59 | KEVENT Event;
|
---|
60 | KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
---|
61 |
|
---|
62 | IO_STATUS_BLOCK IoStatusBlock = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
63 | #if 0
|
---|
64 | PIRP pIrp = IoBuildDeviceIoControlRequest(uReq, /* IoControlCode */
|
---|
65 | pDeviceObject,
|
---|
66 | pReq, /* InputBuffer */
|
---|
67 | pReq->cbIn, /* InputBufferLength */
|
---|
68 | pReq, /* OutputBuffer */
|
---|
69 | pReq->cbOut, /* OutputBufferLength */
|
---|
70 | TRUE, /* InternalDeviceIoControl (=> IRP_MJ_INTERNAL_DEVICE_CONTROL) */
|
---|
71 | &Event, /* Event */
|
---|
72 | &IoStatusBlock); /* IoStatusBlock */
|
---|
73 | #else
|
---|
74 | PIRP pIrp = IoBuildDeviceIoControlRequest(uReq, /* IoControlCode */
|
---|
75 | pDeviceObject,
|
---|
76 | NULL, /* InputBuffer */
|
---|
77 | 0, /* InputBufferLength */
|
---|
78 | NULL, /* OutputBuffer */
|
---|
79 | 0, /* OutputBufferLength */
|
---|
80 | TRUE, /* InternalDeviceIoControl (=> IRP_MJ_INTERNAL_DEVICE_CONTROL) */
|
---|
81 | &Event, /* Event */
|
---|
82 | &IoStatusBlock); /* IoStatusBlock */
|
---|
83 | #endif
|
---|
84 | if (pIrp)
|
---|
85 | {
|
---|
86 | #if 0
|
---|
87 | IoGetNextIrpStackLocation(pIrp)->FileObject = pFileObject;
|
---|
88 | #else
|
---|
89 | pIrp->Flags |= IRP_SYNCHRONOUS_API;
|
---|
90 | pIrp->UserBuffer = pReq;
|
---|
91 | pIrp->AssociatedIrp.SystemBuffer = pReq;
|
---|
92 | PIO_STACK_LOCATION pStack = IoGetNextIrpStackLocation(pIrp);
|
---|
93 | pStack->FileObject = pFileObject;
|
---|
94 | pStack->Parameters.DeviceIoControl.OutputBufferLength = pReq->cbOut;
|
---|
95 | pStack->Parameters.DeviceIoControl.InputBufferLength = pReq->cbIn;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Call the driver, wait for an async request to complete (should never happen).
|
---|
100 | */
|
---|
101 | rcNt = IoCallDriver(pDeviceObject, pIrp);
|
---|
102 | if (rcNt == STATUS_PENDING)
|
---|
103 | rcNt = KeWaitForSingleObject(&Event, /* Object */
|
---|
104 | Executive, /* WaitReason */
|
---|
105 | KernelMode, /* WaitMode */
|
---|
106 | FALSE, /* Alertable */
|
---|
107 | NULL); /* TimeOut */
|
---|
108 | if (NT_SUCCESS(rcNt))
|
---|
109 | rcNt = IoStatusBlock.Status;
|
---|
110 | if (NT_SUCCESS(rcNt))
|
---|
111 | rc = pReq->rc;
|
---|
112 | else
|
---|
113 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
114 | }
|
---|
115 | else
|
---|
116 | rc = VERR_NO_MEMORY;
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | int VBOXCALL vbglR0IdcNativeOpen(PVBGLIDCHANDLE pHandle, PVBGLIOCIDCCONNECT pReq)
|
---|
122 | {
|
---|
123 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
124 | PFILE_OBJECT pFileObject = NULL;
|
---|
125 | UNICODE_STRING wszDeviceName;
|
---|
126 | NTSTATUS rcNt;
|
---|
127 | int rc;
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Get the device object pointer.
|
---|
131 | */
|
---|
132 | RtlInitUnicodeString(&wszDeviceName, VBOXGUEST_DEVICE_NAME_NT);
|
---|
133 | rcNt = IoGetDeviceObjectPointer(&wszDeviceName, FILE_ALL_ACCESS, &pFileObject, &pDeviceObject);
|
---|
134 | if (NT_SUCCESS(rcNt))
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Make the connection call.
|
---|
138 | */
|
---|
139 | rc = vbglR0IdcNtCallInternal(pDeviceObject, pFileObject, VBGL_IOCTL_IDC_CONNECT, &pReq->Hdr);
|
---|
140 | if (RT_SUCCESS(rc) && RT_SUCCESS(pReq->Hdr.rc))
|
---|
141 | {
|
---|
142 | pHandle->s.pDeviceObject = pDeviceObject;
|
---|
143 | pHandle->s.pFileObject = pFileObject;
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /* only the file object. */
|
---|
148 | ObDereferenceObject(pFileObject);
|
---|
149 | }
|
---|
150 | else
|
---|
151 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
152 |
|
---|
153 | pHandle->s.pDeviceObject = NULL;
|
---|
154 | pHandle->s.pFileObject = NULL;
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | int VBOXCALL vbglR0IdcNativeClose(PVBGLIDCHANDLE pHandle, PVBGLIOCIDCDISCONNECT pReq)
|
---|
160 | {
|
---|
161 | PFILE_OBJECT pFileObject = pHandle->s.pFileObject;
|
---|
162 | int rc = vbglR0IdcNtCallInternal(pHandle->s.pDeviceObject, pFileObject, VBGL_IOCTL_IDC_DISCONNECT, &pReq->Hdr);
|
---|
163 | if (RT_SUCCESS(rc) && RT_SUCCESS(pReq->Hdr.rc))
|
---|
164 | {
|
---|
165 | pHandle->s.pDeviceObject = NULL;
|
---|
166 | pHandle->s.pFileObject = NULL;
|
---|
167 | ObDereferenceObject(pFileObject);
|
---|
168 | }
|
---|
169 |
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Makes an IDC call, returning only the I/O control status code.
|
---|
176 | *
|
---|
177 | * @returns VBox status code (the I/O control failure status).
|
---|
178 | * @param pHandle The IDC handle.
|
---|
179 | * @param uReq The request number.
|
---|
180 | * @param pReqHdr The request header.
|
---|
181 | * @param cbReq The request size.
|
---|
182 | */
|
---|
183 | DECLR0VBGL(int) VbglR0IdcCallRaw(PVBGLIDCHANDLE pHandle, uintptr_t uReq, PVBGLREQHDR pReqHdr, uint32_t cbReq)
|
---|
184 | {
|
---|
185 | NOREF(cbReq);
|
---|
186 | return vbglR0IdcNtCallInternal(pHandle->s.pDeviceObject, pHandle->s.pFileObject, (uint32_t)uReq, pReqHdr);
|
---|
187 | }
|
---|
188 |
|
---|