win-pvdrivers

view xenpci/xenpci.h @ 747:f43c89df09b2

Big changes to the way the device hiding works. Should fix the way Vista and above need a reboot into safe mode after install.
author James Harper <james.harper@bendigoit.com.au>
date Sun Jan 10 10:07:32 2010 +1100 (2010-01-10)
parents fadce0b7a84c
children 6300617040e0
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #if !defined(_XENPCI_H_)
21 #define _XENPCI_H_
23 #define __attribute__(arg) /* empty */
24 #define EISCONN 127
26 #include <ntifs.h>
27 //#include <ntddk.h>
29 #define DDKAPI
30 //#include <wdm.h>
31 #include <wdf.h>
32 #include <initguid.h>
33 #include <wdmguid.h>
34 #include <errno.h>
35 #define NTSTRSAFE_LIB
36 #include <ntstrsafe.h>
38 #define __DRIVER_NAME "XenPCI"
40 #include <xen_windows.h>
41 #include <memory.h>
42 #include <grant_table.h>
43 #include <event_channel.h>
44 #include <hvm/params.h>
45 #include <hvm/hvm_op.h>
46 #include <sched.h>
47 #include <io/xenbus.h>
48 #include <io/xs_wire.h>
50 #include <xen_public.h>
52 //{C828ABE9-14CA-4445-BAA6-82C2376C6518}
53 DEFINE_GUID( GUID_XENPCI_DEVCLASS, 0xC828ABE9, 0x14CA, 0x4445, 0xBA, 0xA6, 0x82, 0xC2, 0x37, 0x6C, 0x65, 0x18);
55 #define XENPCI_POOL_TAG (ULONG) 'XenP'
57 #define NR_RESERVED_ENTRIES 8
58 #define NR_GRANT_FRAMES 32
59 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
61 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
63 #define EVT_ACTION_TYPE_EMPTY 0
64 #define EVT_ACTION_TYPE_NORMAL 1
65 #define EVT_ACTION_TYPE_DPC 2
66 #define EVT_ACTION_TYPE_IRQ 3
67 #define EVT_ACTION_TYPE_SUSPEND 4
69 #define XEN_PV_PRODUCT_NUMBER 0x0002
70 #define XEN_PV_PRODUCT_BUILD 0x00000001
72 extern ULONG qemu_protocol_version;
74 typedef struct _ev_action_t {
75 PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine;
76 PVOID ServiceContext;
77 CHAR description[128];
78 ULONG type; /* EVT_ACTION_TYPE_* */
79 KDPC Dpc;
80 ULONG vector;
81 ULONG count;
82 PVOID xpdd;
83 } ev_action_t;
85 typedef struct _XENBUS_WATCH_RING
86 {
87 char Path[128];
88 char Token[10];
89 } XENBUS_WATCH_RING;
91 typedef struct xsd_sockmsg xsd_sockmsg_t;
93 typedef struct _XENBUS_WATCH_ENTRY {
94 char Path[128];
95 PXENBUS_WATCH_CALLBACK ServiceRoutine;
96 PVOID ServiceContext;
97 int Count;
98 int Active;
99 } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
101 #define NR_EVENTS 1024
102 #define WATCH_RING_SIZE 128
103 #define NR_XB_REQS 32
104 #define MAX_WATCH_ENTRIES 128
106 #define CHILD_STATE_EMPTY 0
107 #define CHILD_STATE_DELETED 1
108 #define CHILD_STATE_ADDED 2
110 #define SUSPEND_STATE_NONE 0 /* no suspend in progress */
111 #define SUSPEND_STATE_SCHEDULED 1 /* suspend scheduled */
112 #define SUSPEND_STATE_HIGH_IRQL 2 /* all processors are at high IRQL and spinning */
113 #define SUSPEND_STATE_RESUMING 3 /* we are the other side of the suspend and things are starting to get back to normal */
115 typedef struct {
116 WDFDEVICE wdf_device;
118 BOOLEAN tpr_patched;
120 WDFINTERRUPT interrupt;
121 ULONG irq_number;
122 ULONG irq_vector;
123 KIRQL irq_level;
124 KINTERRUPT_MODE irq_mode;
125 KAFFINITY irq_affinity;
127 PHYSICAL_ADDRESS shared_info_area_unmapped;
128 shared_info_t *shared_info_area;
129 xen_ulong_t evtchn_pending_pvt[MAX_VIRT_CPUS][sizeof(xen_ulong_t) * 8];
130 xen_ulong_t evtchn_pending_suspend[sizeof(xen_ulong_t) * 8];
131 evtchn_port_t pdo_event_channel;
132 KEVENT pdo_suspend_event;
133 BOOLEAN interrupts_masked;
135 PHYSICAL_ADDRESS platform_mmio_addr;
136 ULONG platform_mmio_orig_len;
137 ULONG platform_mmio_len;
138 ULONG platform_mmio_alloc;
139 USHORT platform_mmio_flags;
141 ULONG platform_ioport_addr;
142 ULONG platform_ioport_len;
144 char *hypercall_stubs;
146 evtchn_port_t xen_store_evtchn;
148 /* grant related */
149 grant_entry_t *gnttab_table;
150 grant_entry_t *gnttab_table_copy;
151 PHYSICAL_ADDRESS gnttab_table_physical;
152 grant_ref_t *gnttab_list;
153 int gnttab_list_free;
154 KSPIN_LOCK grant_lock;
155 ULONG grant_frames;
157 ev_action_t ev_actions[NR_EVENTS];
158 // unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
160 BOOLEAN xb_state;
162 struct xenstore_domain_interface *xen_store_interface;
164 #define BALLOON_UNITS (1024 * 1024) /* 1MB */
165 PKTHREAD balloon_thread;
166 KEVENT balloon_event;
167 BOOLEAN balloon_shutdown;
168 ULONG initial_memory;
169 ULONG current_memory;
170 ULONG target_memory;
172 /* xenbus related */
173 XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
174 KSPIN_LOCK xb_ring_spinlock;
175 FAST_MUTEX xb_watch_mutex;
176 FAST_MUTEX xb_request_mutex;
177 KEVENT xb_request_complete_event;
178 struct xsd_sockmsg *xb_reply;
179 struct xsd_sockmsg *xb_msg;
180 ULONG xb_msg_offset;
182 WDFCHILDLIST child_list;
184 KSPIN_LOCK suspend_lock;
185 evtchn_port_t suspend_evtchn;
186 int suspend_state;
188 UNICODE_STRING legacy_interface_name;
189 UNICODE_STRING interface_name;
190 BOOLEAN interface_open;
192 BOOLEAN removable;
194 BOOLEAN hibernated;
196 WDFQUEUE io_queue;
198 //WDFCOLLECTION veto_devices;
199 LIST_ENTRY veto_list;
201 #if 0
202 KSPIN_LOCK mmio_freelist_lock;
203 PPFN_NUMBER mmio_freelist_base;
204 ULONG mmio_freelist_free;
205 #endif
207 } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
209 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_DATA, GetXpdd)
211 typedef struct {
212 WDFDEVICE wdf_device;
213 WDFDEVICE wdf_device_bus_fdo;
214 BOOLEAN reported_missing;
215 char path[128];
216 char device[128];
217 ULONG index;
218 ULONG irq_number;
219 ULONG irq_vector;
220 KIRQL irq_level;
221 char backend_path[128];
222 //PVOID xenbus_request;
223 KEVENT backend_state_event;
224 ULONG backend_state;
225 FAST_MUTEX backend_state_mutex;
226 ULONG frontend_state;
227 PMDL config_page_mdl;
228 PHYSICAL_ADDRESS config_page_phys;
229 ULONG config_page_length;
230 PUCHAR requested_resources_start;
231 PUCHAR requested_resources_ptr;
232 PUCHAR assigned_resources_start;
233 PUCHAR assigned_resources_ptr;
234 XENPCI_DEVICE_STATE device_state;
235 BOOLEAN restart_on_resume;
237 BOOLEAN hiber_usage_kludge;
238 } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
240 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_PDO_DEVICE_DATA, GetXppdd)
242 typedef struct {
243 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER header;
244 CHAR path[128];
245 CHAR device[128];
246 ULONG index;
247 } XENPCI_PDO_IDENTIFICATION_DESCRIPTION, *PXENPCI_PDO_IDENTIFICATION_DESCRIPTION;
249 #define XEN_INTERFACE_VERSION 1
251 //#define DEVICE_INTERFACE_TYPE_LEGACY 0
252 #define DEVICE_INTERFACE_TYPE_XENBUS 1
253 #define DEVICE_INTERFACE_TYPE_EVTCHN 2
254 #define DEVICE_INTERFACE_TYPE_GNTDEV 3
256 typedef struct {
257 ULONG len;
258 WDFQUEUE io_queue;
259 union {
260 struct xsd_sockmsg msg;
261 UCHAR buffer[PAGE_SIZE];
262 } u;
263 LIST_ENTRY read_list_head;
264 LIST_ENTRY watch_list_head;
265 } XENBUS_INTERFACE_DATA, *PXENBUS_INTERFACE_DATA;
267 typedef struct {
268 ULONG dummy; /* fill this in with whatever is required */
269 } EVTCHN_INTERFACE_DATA, *PEVTCHN_INTERFACE_DATA;
271 typedef struct {
272 ULONG dummy; /* fill this in with whatever is required */
273 } GNTDEV_INTERFACE_DATA, *PGNTDEV_INTERFACE_DATA;
275 typedef struct {
276 ULONG type;
277 KSPIN_LOCK lock;
278 WDFQUEUE io_queue;
279 EVT_WDF_FILE_CLEANUP *EvtFileCleanup;
280 EVT_WDF_FILE_CLOSE *EvtFileClose;
281 union {
282 XENBUS_INTERFACE_DATA xenbus;
283 EVTCHN_INTERFACE_DATA evtchn;
284 GNTDEV_INTERFACE_DATA gntdev;
285 };
286 } XENPCI_DEVICE_INTERFACE_DATA, *PXENPCI_DEVICE_INTERFACE_DATA;
288 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_INTERFACE_DATA, GetXpdid)
290 NTSTATUS
291 XenBus_DeviceFileInit(WDFDEVICE device, PWDF_IO_QUEUE_CONFIG queue_config, WDFFILEOBJECT file_object);
293 EVT_WDF_DEVICE_FILE_CREATE XenPci_EvtDeviceFileCreate;
294 EVT_WDF_FILE_CLOSE XenPci_EvtFileClose;
295 EVT_WDF_FILE_CLEANUP XenPci_EvtFileCleanup;
296 EVT_WDF_IO_QUEUE_IO_DEFAULT XenPci_EvtIoDefault;
298 #include "hypercall.h"
300 #define XBT_NIL ((xenbus_transaction_t)0)
302 NTSTATUS
303 hvm_get_stubs(PXENPCI_DEVICE_DATA xpdd);
304 NTSTATUS
305 hvm_free_stubs(PXENPCI_DEVICE_DATA xpdd);
307 EVT_WDF_DEVICE_PREPARE_HARDWARE XenPci_EvtDevicePrepareHardware;
308 EVT_WDF_DEVICE_RELEASE_HARDWARE XenPci_EvtDeviceReleaseHardware;
309 EVT_WDF_DEVICE_D0_ENTRY XenPci_EvtDeviceD0Entry;
310 EVT_WDF_DEVICE_D0_ENTRY_POST_INTERRUPTS_ENABLED XenPci_EvtDeviceD0EntryPostInterruptsEnabled;
311 EVT_WDF_DEVICE_D0_EXIT XenPci_EvtDeviceD0Exit;
312 EVT_WDF_DEVICE_D0_EXIT_PRE_INTERRUPTS_DISABLED XenPci_EvtDeviceD0ExitPreInterruptsDisabled;
313 EVT_WDF_DEVICE_QUERY_REMOVE XenPci_EvtDeviceQueryRemove;
314 EVT_WDF_CHILD_LIST_CREATE_DEVICE XenPci_EvtChildListCreateDevice;
315 EVT_WDF_CHILD_LIST_SCAN_FOR_CHILDREN XenPci_EvtChildListScanForChildren;
317 VOID
318 XenPci_HideQemuDevices();
319 extern WDFCOLLECTION qemu_hide_devices;
320 extern USHORT qemu_hide_flags_value;
322 #if 0
323 NTSTATUS
324 XenPci_Power_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
325 NTSTATUS
326 XenPci_Dummy_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
327 NTSTATUS
328 XenPci_Pnp_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
329 NTSTATUS
330 XenPci_Irp_Create_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
331 NTSTATUS
332 XenPci_Irp_Close_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
333 NTSTATUS
334 XenPci_Irp_Read_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
335 NTSTATUS
336 XenPci_Irp_Write_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
337 NTSTATUS
338 XenPci_Irp_Cleanup_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
339 NTSTATUS
340 XenPci_SystemControl_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
342 NTSTATUS
343 XenPci_Irp_Create_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
344 NTSTATUS
345 XenPci_Irp_Close_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
346 NTSTATUS
347 XenPci_Irp_Read_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
348 NTSTATUS
349 XenPci_Irp_Write_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
350 NTSTATUS
351 XenPci_Irp_Cleanup_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
353 NTSTATUS
354 XenPci_Power_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
355 //NTSTATUS
356 //XenPci_Dummy_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
357 NTSTATUS
358 XenPci_Pnp_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
359 NTSTATUS
360 XenPci_Irp_Create_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
361 NTSTATUS
362 XenPci_Irp_Close_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
363 NTSTATUS
364 XenPci_Irp_Read_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
365 NTSTATUS
366 XenPci_Irp_Write_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
367 NTSTATUS
368 XenPci_Irp_Cleanup_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
369 NTSTATUS
370 XenPci_SystemControl_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
371 #endif
373 NTSTATUS
374 XenPci_Pdo_Suspend(WDFDEVICE device);
375 NTSTATUS
376 XenPci_Pdo_Resume(WDFDEVICE device);
378 VOID
379 XenPci_DumpPdoConfig(PDEVICE_OBJECT device_object);
381 typedef VOID
382 (*PXENPCI_HIGHSYNC_FUNCTION)(PVOID context);
384 VOID
385 XenPci_HighSync(PXENPCI_HIGHSYNC_FUNCTION function0, PXENPCI_HIGHSYNC_FUNCTION functionN, PVOID context);
387 VOID
388 XenPci_PatchKernel(PXENPCI_DEVICE_DATA xpdd, PVOID base, ULONG length);
390 NTSTATUS
391 XenPci_HookDbgPrint();
393 struct xsd_sockmsg *
394 XenBus_Raw(PXENPCI_DEVICE_DATA xpdd, struct xsd_sockmsg *msg);
395 char *
396 XenBus_Read(PVOID Context, xenbus_transaction_t xbt, char *path, char **value);
397 char *
398 XenBus_Write(PVOID Context, xenbus_transaction_t xbt, char *path, char *value);
399 char *
400 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, char *path, char *fmt, ...);
401 char *
402 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
403 char *
404 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
405 char *
406 XenBus_List(PVOID Context, xenbus_transaction_t xbt, char *prefix, char ***contents);
407 char *
408 XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
409 char *
410 XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
411 //VOID
412 //XenBus_ThreadProc(PVOID StartContext);
413 NTSTATUS
414 XenBus_Init(PXENPCI_DEVICE_DATA xpdd);
415 NTSTATUS
416 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd);
417 NTSTATUS
418 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd);
419 NTSTATUS
420 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd);
422 PHYSICAL_ADDRESS
423 XenPci_AllocMMIO(PXENPCI_DEVICE_DATA xpdd, ULONG len);
425 EVT_WDF_INTERRUPT_ISR EvtChn_EvtInterruptIsr;
426 EVT_WDF_INTERRUPT_ENABLE EvtChn_EvtInterruptEnable;
427 EVT_WDF_INTERRUPT_DISABLE EvtChn_EvtInterruptDisable;
429 NTSTATUS
430 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd);
431 NTSTATUS
432 EvtChn_Suspend(PXENPCI_DEVICE_DATA xpdd);
433 NTSTATUS
434 EvtChn_Resume(PXENPCI_DEVICE_DATA xpdd);
436 NTSTATUS
437 EvtChn_Mask(PVOID Context, evtchn_port_t Port);
438 NTSTATUS
439 EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
440 NTSTATUS
441 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
442 NTSTATUS
443 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
444 NTSTATUS
445 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector, PCHAR description);
446 evtchn_port_t
447 EvtChn_AllocIpi(PVOID context, ULONG vcpu);
448 NTSTATUS
449 EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
450 NTSTATUS
451 EvtChn_Notify(PVOID Context, evtchn_port_t Port);
452 VOID
453 EvtChn_Close(PVOID Context, evtchn_port_t Port);
454 evtchn_port_t
455 EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
456 BOOLEAN
457 EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt);
459 VOID
460 GntTbl_Init(PXENPCI_DEVICE_DATA xpdd);
461 VOID
462 GntTbl_Suspend(PXENPCI_DEVICE_DATA xpdd);
463 VOID
464 GntTbl_Resume(PXENPCI_DEVICE_DATA xpdd);
465 grant_ref_t
466 GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t, int readonly, grant_ref_t ref);
467 BOOLEAN
468 GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref);
469 VOID
470 GntTbl_PutRef(PVOID Context, grant_ref_t ref);
471 grant_ref_t
472 GntTbl_GetRef(PVOID Context);
474 TRANSLATE_BUS_ADDRESS XenPci_BIS_TranslateBusAddress;
475 GET_DMA_ADAPTER XenPci_BIS_GetDmaAdapter;
476 GET_SET_DEVICE_DATA XenPci_BIS_SetBusData;
477 GET_SET_DEVICE_DATA XenPci_BIS_GetBusData;
479 #endif