Debian Codegraph @

windows/netuio/netuio_queue.c

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Microsoft Corporation.
3 */
4
5 #include "netuio_drv.h"
6
7 #ifdef ALLOC_PRAGMA
8 #pragma alloc_text (PAGE, netuio_queue_initialize)
9 #endif
10
11 static
12 BOOLEAN
13 netuio_get_usermode_mapping_flag(WDFREQUEST Request)
14 {
15 WDFFILEOBJECT file_object;
16
17 file_object = WdfRequestGetFileObject(Request);
18 if (file_object == NULL)
19 {
20 goto end;
21 }
22
23 PNETUIO_FILE_CONTEXT_DATA file_ctx;
24
25 file_ctx = netuio_get_file_object_context_data(file_object);
26 if (file_ctx->bMapped)
27 {
28 return TRUE;
29 }
30
31 end:
32 return FALSE;
33 }
34
35 static
36 VOID
37 netuio_set_usermode_mapping_flag(WDFREQUEST Request)
38 {
39 WDFFILEOBJECT file_object;
40
41 file_object = WdfRequestGetFileObject(Request);
42 if (file_object == NULL)
43 {
44 return;
45 }
46
47 PNETUIO_FILE_CONTEXT_DATA file_ctx;
48
49 file_ctx = netuio_get_file_object_context_data(file_object);
50
51 file_ctx->bMapped = TRUE;
52 }
53
54 static void
55 netuio_handle_get_hw_data_request(_In_ PNETUIO_CONTEXT_DATA ctx,
56 _In_ PVOID outputBuf, _In_ size_t outputBufSize)
57 {
58 ASSERT(outputBufSize == sizeof(struct device_info));
59
60 struct device_info *dpdk_pvt_info = (struct device_info *)outputBuf;
61 RtlZeroMemory(dpdk_pvt_info, outputBufSize);
62
63 for (ULONG idx = 0; idx < PCI_MAX_BAR; idx++) {
64 dpdk_pvt_info->hw[idx].phys_addr.QuadPart = ctx->bar[idx].base_addr.QuadPart;
65 dpdk_pvt_info->hw[idx].user_mapped_virt_addr = ctx->dpdk_hw[idx].mem.user_mapped_virt_addr;
66 dpdk_pvt_info->hw[idx].size = ctx->bar[idx].size;
67 }
68 }
69
70 /*
71 Routine Description:
72 Maps address ranges into the usermode process's address space. The following
73 ranges are mapped:
74
75 * Any PCI BARs that our device was assigned
76 * The scratch buffer of contiguous pages
77
78 Return Value:
79 NTSTATUS
80 */
81 static NTSTATUS
82 netuio_map_address_into_user_process(_In_ PNETUIO_CONTEXT_DATA ctx, WDFREQUEST Request)
83 {
84 NTSTATUS status = STATUS_SUCCESS;
85
86 if (netuio_get_usermode_mapping_flag(Request))
87 {
88 goto end;
89 }
90
91 // Map any device BAR(s) to the user's process context
92 for (INT idx = 0; idx < PCI_MAX_BAR; idx++) {
93 if (ctx->dpdk_hw[idx].mdl == NULL) {
94 continue;
95 }
96
97 MmBuildMdlForNonPagedPool(ctx->dpdk_hw[idx].mdl);
98 __try {
99 ctx->dpdk_hw[idx].mem.user_mapped_virt_addr =
100 MmMapLockedPagesSpecifyCache(ctx->dpdk_hw[idx].mdl, UserMode,
101 MmCached, NULL, FALSE, NormalPagePriority);
102
103 if (ctx->dpdk_hw[idx].mem.user_mapped_virt_addr == NULL) {
104 status = STATUS_INSUFFICIENT_RESOURCES;
105 goto end;
106 }
107 }
108 __except (EXCEPTION_EXECUTE_HANDLER) {
109 status = GetExceptionCode();
110 goto end;
111 }
112 }
113
114 end:
115 if (status != STATUS_SUCCESS) {
116 netuio_unmap_address_from_user_process(ctx);
117 }
118
119 return status;
120 }
121
122 /*
123 Routine Description:
124 Unmaps all address ranges from the usermode process address space.
125 MUST be called in the context of the same process which created
126 the mapping.
127
128 Return Value:
129 None
130 */
131 _Use_decl_annotations_
132 VOID
133 netuio_unmap_address_from_user_process(PNETUIO_CONTEXT_DATA ctx)
134 {
135 for (INT idx = 0; idx < PCI_MAX_BAR; idx++) {
136 if (ctx->dpdk_hw[idx].mem.user_mapped_virt_addr != NULL) {
137 MmUnmapLockedPages(ctx->dpdk_hw[idx].mem.user_mapped_virt_addr,
138 ctx->dpdk_hw[idx].mdl);
139
140 ctx->dpdk_hw[idx].mem.user_mapped_virt_addr = NULL;
141 }
142 }
143 }
144
145 /*
146 Routine Description:
147 The I/O dispatch callbacks for the frameworks device object are configured here.
148 A single default I/O Queue is configured for parallel request processing, and a
149 driver context memory allocation is created to hold our structure QUEUE_CONTEXT.
150
151 Return Value:
152 None
153 */
154 _Use_decl_annotations_
155 NTSTATUS
156 netuio_queue_initialize(WDFDEVICE Device)
157 {
158 WDFQUEUE queue;
159 NTSTATUS status;
160 WDF_IO_QUEUE_CONFIG queueConfig;
161
162 PAGED_CODE();
163
164 // Configure a default queue so that requests that are not
165 // configure-fowarded using WdfDeviceConfigureRequestDispatching to goto
166 // other queues get dispatched here.
167 WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&queueConfig, WdfIoQueueDispatchParallel);
168
169 queueConfig.EvtIoDeviceControl = netuio_evt_IO_device_control;
170
171 status = WdfIoQueueCreate(Device,
172 &queueConfig,
173 WDF_NO_OBJECT_ATTRIBUTES,
174 &queue);
175
176 if( !NT_SUCCESS(status) ) {
177 return status;
178 }
179
180 return status;
181 }
182
183 /*
184 Routine Description:
185 This routine is invoked to preprocess an I/O request before being placed into a queue.
186 It is guaranteed that it executes in the context of the process that generated the request.
187
188 Return Value:
189 None
190 */
191 _Use_decl_annotations_
192 VOID
193 netuio_evt_IO_in_caller_context(WDFDEVICE Device,
194 WDFREQUEST Request)
195 {
196 WDF_REQUEST_PARAMETERS params = { 0 };
197 NTSTATUS status = STATUS_SUCCESS;
198 PVOID output_buf = NULL;
199 size_t output_buf_size;
200 size_t bytes_returned = 0;
201 PNETUIO_CONTEXT_DATA ctx = NULL;
202
203 ctx = netuio_get_context_data(Device);
204
205 WDF_REQUEST_PARAMETERS_INIT(&params);
206 WdfRequestGetParameters(Request, &params);
207
208 if (params.Type != WdfRequestTypeDeviceControl)
209 {
210 status = STATUS_INVALID_DEVICE_REQUEST;
211 goto end;
212 }
213
214 // We only need to be in the context of the process that initiated the request
215 //when we need to map memory to userspace. Otherwise, send the request back to framework.
216 if (params.Parameters.DeviceIoControl.IoControlCode != IOCTL_NETUIO_MAP_HW_INTO_USERSPACE)
217 {
218 status = WdfDeviceEnqueueRequest(Device, Request);
219 if (!NT_SUCCESS(status))
220 {
221 goto end;
222 }
223 return;
224 }
225
226 // Return relevant data to the caller
227 status = WdfRequestRetrieveOutputBuffer(Request, sizeof(struct device_info), &output_buf, &output_buf_size);
228 if (!NT_SUCCESS(status)) {
229 goto end;
230 }
231
232 status = netuio_map_address_into_user_process(ctx, Request);
233 if (!NT_SUCCESS(status)) {
234 goto end;
235 }
236
237 netuio_set_usermode_mapping_flag(Request);
238
239 netuio_handle_get_hw_data_request(ctx, output_buf, output_buf_size);
240 bytes_returned = output_buf_size;
241
242 end:
243 WdfRequestCompleteWithInformation(Request, status, bytes_returned);
244
245 return;
246 }
247
248 /*
249 Routine Description:
250 This event is invoked when the framework receives IRP_MJ_DEVICE_CONTROL request.
251
252 Return Value:
253 None
254 */
255 _Use_decl_annotations_
256 VOID
257 netuio_evt_IO_device_control(WDFQUEUE Queue, WDFREQUEST Request,
258 size_t OutputBufferLength, size_t InputBufferLength,
259 ULONG IoControlCode)
260 {
261 UNREFERENCED_PARAMETER(OutputBufferLength);
262 UNREFERENCED_PARAMETER(InputBufferLength);
263
264 NTSTATUS status = STATUS_SUCCESS;
265 PVOID input_buf = NULL, output_buf = NULL;
266 size_t input_buf_size, output_buf_size;
267 size_t bytes_returned = 0;
268
269 WDFDEVICE device = WdfIoQueueGetDevice(Queue);
270
271 PNETUIO_CONTEXT_DATA ctx;
272 ctx = netuio_get_context_data(device);
273
274 if (IoControlCode != IOCTL_NETUIO_PCI_CONFIG_IO)
275 {
276 status = STATUS_INVALID_DEVICE_REQUEST;
277 goto end;
278 }
279
280 // First retrieve the input buffer and see if it matches our device
281 status = WdfRequestRetrieveInputBuffer(Request, sizeof(struct dpdk_pci_config_io), &input_buf, &input_buf_size);
282 if (!NT_SUCCESS(status)) {
283 goto end;
284 }
285
286 struct dpdk_pci_config_io *dpdk_pci_io_input = (struct dpdk_pci_config_io *)input_buf;
287
288 if (dpdk_pci_io_input->access_size != 1 &&
289 dpdk_pci_io_input->access_size != 2 &&
290 dpdk_pci_io_input->access_size != 4 &&
291 dpdk_pci_io_input->access_size != 8) {
292 status = STATUS_INVALID_PARAMETER;
293 goto end;
294 }
295
296 // Retrieve output buffer
297 status = WdfRequestRetrieveOutputBuffer(Request, sizeof(UINT64), &output_buf, &output_buf_size);
298 if (!NT_SUCCESS(status)) {
299 goto end;
300 }
301 ASSERT(output_buf_size == OutputBufferLength);
302
303 if (dpdk_pci_io_input->op == PCI_IO_READ) {
304 *(UINT64 *)output_buf = 0;
305 bytes_returned = ctx->bus_interface.GetBusData(
306 ctx->bus_interface.Context,
307 PCI_WHICHSPACE_CONFIG,
308 output_buf,
309 dpdk_pci_io_input->offset,
310 dpdk_pci_io_input->access_size);
311 }
312 else if (dpdk_pci_io_input->op == PCI_IO_WRITE) {
313 // returns bytes written
314 bytes_returned = ctx->bus_interface.SetBusData(
315 ctx->bus_interface.Context,
316 PCI_WHICHSPACE_CONFIG,
317 (PVOID)&dpdk_pci_io_input->data,
318 dpdk_pci_io_input->offset,
319 dpdk_pci_io_input->access_size);
320 }
321 else {
322 status = STATUS_INVALID_PARAMETER;
323 goto end;
324 }
325
326 if (bytes_returned != dpdk_pci_io_input->access_size) {
327 status = STATUS_INVALID_PARAMETER;
328 }
329
330 end:
331 WdfRequestCompleteWithInformation(Request, status, bytes_returned);
332
333 return;
334 }