win-pvdrivers

view xenpci/xenpci.h @ 1001:c21dd04d3ae6

Less noise for xennet6
author James Harper <james.harper@bendigoit.com.au>
date Fri Dec 14 21:26:22 2012 +1100 (2012-12-14)
parents ef1f25e2c281
children e9011a1ba5bb a5d1d333e0e2
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 <ntddk.h>
28 /* including ntifs.h (only needed for this function) causes PREFast to trip up on lots of bogus errors */
29 PDEVICE_OBJECT IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject);
30 //#include <ntifs.h>
32 //#include <wdm.h>
33 #include <wdf.h>
34 #include <initguid.h>
35 #include <wdmguid.h>
36 #include <errno.h>
37 #define NTSTRSAFE_LIB
38 #include <ntstrsafe.h>
40 #include <liblfds.h>
42 #define __DRIVER_NAME "XenPCI"
44 #include <xen_windows.h>
45 #include <memory.h>
46 #include <grant_table.h>
47 #include <event_channel.h>
48 #include <hvm/params.h>
49 #include <hvm/hvm_op.h>
50 #include <sched.h>
51 #include <io/xenbus.h>
52 #include <io/xs_wire.h>
54 #include <xen_public.h>
56 //{C828ABE9-14CA-4445-BAA6-82C2376C6518}
57 DEFINE_GUID( GUID_XENPCI_DEVCLASS, 0xC828ABE9, 0x14CA, 0x4445, 0xBA, 0xA6, 0x82, 0xC2, 0x37, 0x6C, 0x65, 0x18);
59 #define XENPCI_POOL_TAG (ULONG) 'XenP'
61 #define NR_RESERVED_ENTRIES 8
62 #define NR_GRANT_FRAMES 32
63 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
65 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
67 #define EVT_ACTION_TYPE_EMPTY 0
68 #define EVT_ACTION_TYPE_NORMAL 1
69 #define EVT_ACTION_TYPE_DPC 2
70 #define EVT_ACTION_TYPE_IRQ 3
71 #define EVT_ACTION_TYPE_SUSPEND 4
72 #define EVT_ACTION_TYPE_NEW 5 /* setup of event is in progress */
74 #define EVT_ACTION_FLAGS_DEFAULT 0 /* no special flags */
75 #define EVT_ACTION_FLAGS_NO_SUSPEND 1 /* should not be fired on EVT_ACTION_TYPE_SUSPEND event */
77 #define XEN_PV_PRODUCT_NUMBER 0x0002
78 #define XEN_PV_PRODUCT_BUILD 0x00000001
80 #define BALLOON_UNITS_KB (1 * 1024) /* 1MB */
81 #define BALLOON_UNIT_PAGES ((BALLOON_UNITS_KB << 10) >> PAGE_SHIFT)
83 extern ULONG qemu_protocol_version;
84 extern USHORT xen_version_major;
85 extern USHORT xen_version_minor;
87 typedef struct _ev_action_t {
88 PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine;
89 PVOID ServiceContext;
90 CHAR description[128];
91 ULONG type; /* EVT_ACTION_TYPE_* */
92 ULONG flags; /* EVT_ACTION_FLAGS_* */
93 KDPC Dpc;
94 ULONG port;
95 ULONG vector;
96 ULONG count;
97 PVOID xpdd;
98 ULONG generation; /* increases each time the event is renewed */
99 } ev_action_t;
101 typedef struct _XENBUS_WATCH_RING
102 {
103 char Path[128];
104 char Token[10];
105 } XENBUS_WATCH_RING;
107 typedef struct xsd_sockmsg xsd_sockmsg_t;
109 typedef struct _XENBUS_WATCH_ENTRY {
110 char Path[128];
111 PXENBUS_WATCH_CALLBACK ServiceRoutine;
112 PVOID ServiceContext;
113 int Count;
114 int Active;
115 } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
117 /* number of events is 1024 on 32 bits and 4096 on 64 bits */
118 #define NR_EVENTS (sizeof(xen_ulong_t) * 8 * sizeof(xen_ulong_t) * 8)
119 #define WATCH_RING_SIZE 128
120 #define NR_XB_REQS 32
121 #define MAX_WATCH_ENTRIES 128
123 #define CHILD_STATE_EMPTY 0
124 #define CHILD_STATE_DELETED 1
125 #define CHILD_STATE_ADDED 2
127 #define SUSPEND_STATE_NONE 0 /* no suspend in progress */
128 #define SUSPEND_STATE_SCHEDULED 1 /* suspend scheduled */
129 #define SUSPEND_STATE_HIGH_IRQL 2 /* all processors are at high IRQL and spinning */
130 #define SUSPEND_STATE_RESUMING 3 /* we are the other side of the suspend and things are starting to get back to normal */
132 /* we take some grant refs out and put them aside so that we dont get corrupted by hibernate */
133 #define HIBER_GREF_COUNT 128
135 typedef struct {
136 ULONG generation;
137 ULONG tag;
138 } grant_tag_t;
140 typedef struct {
141 WDFDEVICE wdf_device;
143 BOOLEAN tpr_patched;
145 WDFINTERRUPT interrupt;
146 ULONG irq_number;
147 ULONG irq_vector;
148 KIRQL irq_level;
149 KINTERRUPT_MODE irq_mode;
150 KAFFINITY irq_affinity;
152 PHYSICAL_ADDRESS shared_info_area_unmapped;
153 shared_info_t *shared_info_area;
154 xen_ulong_t evtchn_pending_pvt[MAX_VIRT_CPUS][sizeof(xen_ulong_t) * 8];
155 xen_ulong_t evtchn_pending_suspend[sizeof(xen_ulong_t) * 8];
156 evtchn_port_t pdo_event_channel;
157 KEVENT pdo_suspend_event;
158 BOOLEAN interrupts_masked;
160 PHYSICAL_ADDRESS platform_mmio_addr;
161 ULONG platform_mmio_orig_len;
162 ULONG platform_mmio_len;
163 ULONG platform_mmio_alloc;
164 USHORT platform_mmio_flags;
166 ULONG platform_ioport_addr;
167 ULONG platform_ioport_len;
169 char *hypercall_stubs;
171 evtchn_port_t xen_store_evtchn;
173 /* grant related */
174 struct stack_state *gnttbl_ss;
175 struct stack_state *gnttbl_ss_copy;
176 grant_ref_t hiber_grefs[HIBER_GREF_COUNT];
177 PMDL gnttbl_mdl;
178 grant_entry_t *gnttbl_table;
179 grant_entry_t *gnttbl_table_copy;
180 #if DBG
181 ULONG gnttbl_generation; /* incremented once per save or hibernate */
182 grant_tag_t *gnttbl_tag;
183 grant_tag_t *gnttbl_tag_copy;
184 #endif
185 ULONG grant_frames;
187 ev_action_t ev_actions[NR_EVENTS];
188 // unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
190 BOOLEAN xb_state;
192 struct xenstore_domain_interface *xen_store_interface;
194 PKTHREAD balloon_thread;
195 KEVENT balloon_event;
196 BOOLEAN balloon_shutdown;
197 ULONG initial_memory_kb;
198 ULONG current_memory_kb;
199 ULONG target_memory_kb;
201 /* xenbus related */
202 XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
203 KSPIN_LOCK xb_ring_spinlock;
204 FAST_MUTEX xb_watch_mutex;
205 FAST_MUTEX xb_request_mutex;
206 KEVENT xb_request_complete_event;
207 struct xsd_sockmsg *xb_reply;
208 struct xsd_sockmsg *xb_msg;
209 ULONG xb_msg_offset;
211 WDFCHILDLIST child_list;
213 KSPIN_LOCK suspend_lock;
214 evtchn_port_t suspend_evtchn;
215 int suspend_state;
217 UNICODE_STRING legacy_interface_name;
218 UNICODE_STRING interface_name;
219 BOOLEAN interface_open;
221 BOOLEAN removable;
223 BOOLEAN hibernated;
225 WDFQUEUE io_queue;
227 //WDFCOLLECTION veto_devices;
228 LIST_ENTRY veto_list;
230 #if 0
231 KSPIN_LOCK mmio_freelist_lock;
232 PPFN_NUMBER mmio_freelist_base;
233 ULONG mmio_freelist_free;
234 #endif
236 } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
238 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_DATA, GetXpdd)
240 typedef struct {
241 UCHAR front_target;
242 UCHAR back_expected;
243 UCHAR wait; /* units = 100ms */
244 } XENPCI_STATE_MAP_ELEMENT, *PXENPCI_STATE_MAP_ELEMENT;
246 typedef struct {
247 WDFDEVICE wdf_device;
248 WDFDEVICE wdf_device_bus_fdo;
249 BOOLEAN reported_missing;
250 char path[128];
251 char device[128];
252 ULONG index;
253 ULONG irq_number;
254 ULONG irq_vector;
255 KIRQL irq_level;
256 char backend_path[128];
257 domid_t backend_id;
258 KEVENT backend_state_event;
259 ULONG backend_state;
260 FAST_MUTEX backend_state_mutex;
261 ULONG frontend_state;
262 PMDL config_page_mdl;
263 PHYSICAL_ADDRESS config_page_phys;
264 ULONG config_page_length;
265 PUCHAR requested_resources_start;
266 PUCHAR requested_resources_ptr;
267 PUCHAR assigned_resources_start;
268 PUCHAR assigned_resources_ptr;
269 XENPCI_DEVICE_STATE device_state;
270 BOOLEAN restart_on_resume;
271 BOOLEAN backend_initiated_remove;
272 BOOLEAN do_not_enumerate;
274 XENPCI_STATE_MAP_ELEMENT xb_pre_connect_map[5];
275 XENPCI_STATE_MAP_ELEMENT xb_post_connect_map[5];
276 XENPCI_STATE_MAP_ELEMENT xb_shutdown_map[5];
278 BOOLEAN hiber_usage_kludge;
279 } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
281 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_PDO_DEVICE_DATA, GetXppdd)
283 typedef struct {
284 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER header;
285 CHAR path[128];
286 CHAR device[128];
287 ULONG index;
288 } XENPCI_PDO_IDENTIFICATION_DESCRIPTION, *PXENPCI_PDO_IDENTIFICATION_DESCRIPTION;
290 #define XEN_INTERFACE_VERSION 1
292 //#define DEVICE_INTERFACE_TYPE_LEGACY 0
293 #define DEVICE_INTERFACE_TYPE_XENBUS 1
294 #define DEVICE_INTERFACE_TYPE_EVTCHN 2
295 #define DEVICE_INTERFACE_TYPE_GNTDEV 3
297 typedef struct {
298 ULONG len;
299 WDFQUEUE io_queue;
300 union {
301 struct xsd_sockmsg msg;
302 UCHAR buffer[PAGE_SIZE];
303 } u;
304 LIST_ENTRY read_list_head;
305 LIST_ENTRY watch_list_head;
306 } XENBUS_INTERFACE_DATA, *PXENBUS_INTERFACE_DATA;
308 typedef struct {
309 ULONG dummy; /* fill this in with whatever is required */
310 } EVTCHN_INTERFACE_DATA, *PEVTCHN_INTERFACE_DATA;
312 typedef struct {
313 ULONG dummy; /* fill this in with whatever is required */
314 } GNTDEV_INTERFACE_DATA, *PGNTDEV_INTERFACE_DATA;
316 typedef struct {
317 ULONG type;
318 KSPIN_LOCK lock;
319 WDFQUEUE io_queue;
320 EVT_WDF_FILE_CLEANUP *EvtFileCleanup;
321 EVT_WDF_FILE_CLOSE *EvtFileClose;
322 union {
323 XENBUS_INTERFACE_DATA xenbus;
324 EVTCHN_INTERFACE_DATA evtchn;
325 GNTDEV_INTERFACE_DATA gntdev;
326 };
327 } XENPCI_DEVICE_INTERFACE_DATA, *PXENPCI_DEVICE_INTERFACE_DATA;
329 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_INTERFACE_DATA, GetXpdid)
331 NTSTATUS
332 XenBus_DeviceFileInit(WDFDEVICE device, PWDF_IO_QUEUE_CONFIG queue_config, WDFFILEOBJECT file_object);
334 EVT_WDF_DEVICE_FILE_CREATE XenPci_EvtDeviceFileCreate;
335 EVT_WDF_FILE_CLOSE XenPci_EvtFileClose;
336 EVT_WDF_FILE_CLEANUP XenPci_EvtFileCleanup;
337 EVT_WDF_IO_QUEUE_IO_DEFAULT XenPci_EvtIoDefault;
339 #define HYPERVISOR_memory_op(xpdd, cmd, arg) _HYPERVISOR_memory_op(xpdd->hypercall_stubs, cmd, arg)
340 #define HYPERVISOR_xen_version(xpdd, cmd, arg) _HYPERVISOR_xen_version(xpdd->hypercall_stubs, cmd, arg)
341 #define HYPERVISOR_grant_table_op(xpdd, cmd, uop, count) _HYPERVISOR_grant_table_op(xpdd->hypercall_stubs, cmd, uop, count)
342 #define HYPERVISOR_hvm_op(xpdd, op, arg) _HYPERVISOR_hvm_op(xpdd->hypercall_stubs, op, arg)
343 #define HYPERVISOR_event_channel_op(xpdd, cmd, op) _HYPERVISOR_event_channel_op(xpdd->hypercall_stubs, cmd, op)
344 #define HYPERVISOR_sched_op(xpdd, cmd, arg) _HYPERVISOR_sched_op(xpdd->hypercall_stubs, cmd, arg)
345 #define HYPERVISOR_shutdown(xpdd, reason) _HYPERVISOR_shutdown(xpdd->hypercall_stubs, reason)
347 #define hvm_get_parameter(xvdd, hvm_param) _hvm_get_parameter(xvdd->hypercall_stubs, hvm_param);
348 #define hvm_set_parameter(xvdd, hvm_param, value) _hvm_set_parameter(xvdd->hypercall_stubs, hvm_param, value);
349 #define hvm_shutdown(xvdd, reason) _hvm_shutdown(xvdd->hypercall_stubs, reason);
350 #define HYPERVISOR_yield(xvdd) _HYPERVISOR_yield(xvdd->hypercall_stubs);
352 #include "hypercall.h"
354 #define XBT_NIL ((xenbus_transaction_t)0)
356 PVOID
357 hvm_get_hypercall_stubs();
358 VOID
359 hvm_free_hypercall_stubs(PVOID hypercall_stubs);
361 EVT_WDF_DEVICE_PREPARE_HARDWARE XenPci_EvtDevicePrepareHardware;
362 EVT_WDF_DEVICE_RELEASE_HARDWARE XenPci_EvtDeviceReleaseHardware;
363 EVT_WDF_DEVICE_D0_ENTRY XenPci_EvtDeviceD0Entry;
364 EVT_WDF_DEVICE_D0_ENTRY_POST_INTERRUPTS_ENABLED XenPci_EvtDeviceD0EntryPostInterruptsEnabled;
365 EVT_WDF_DEVICE_D0_EXIT XenPci_EvtDeviceD0Exit;
366 EVT_WDF_DEVICE_D0_EXIT_PRE_INTERRUPTS_DISABLED XenPci_EvtDeviceD0ExitPreInterruptsDisabled;
367 EVT_WDF_DEVICE_QUERY_REMOVE XenPci_EvtDeviceQueryRemove;
368 EVT_WDF_CHILD_LIST_CREATE_DEVICE XenPci_EvtChildListCreateDevice;
369 EVT_WDF_CHILD_LIST_SCAN_FOR_CHILDREN XenPci_EvtChildListScanForChildren;
371 VOID
372 XenPci_HideQemuDevices();
373 extern WDFCOLLECTION qemu_hide_devices;
374 extern USHORT qemu_hide_flags_value;
376 NTSTATUS
377 XenPci_Pdo_Suspend(WDFDEVICE device);
378 NTSTATUS
379 XenPci_Pdo_Resume(WDFDEVICE device);
381 VOID
382 XenPci_DumpPdoConfig(PDEVICE_OBJECT device_object);
384 typedef VOID
385 (*PXENPCI_HIGHSYNC_FUNCTION)(PVOID context);
387 VOID
388 XenPci_HighSync(PXENPCI_HIGHSYNC_FUNCTION function0, PXENPCI_HIGHSYNC_FUNCTION functionN, PVOID context);
390 VOID
391 XenPci_PatchKernel(PXENPCI_DEVICE_DATA xpdd, PVOID base, ULONG length);
393 NTSTATUS
394 XenPci_HookDbgPrint();
395 NTSTATUS
396 XenPci_UnHookDbgPrint();
398 struct xsd_sockmsg *
399 XenBus_Raw(PXENPCI_DEVICE_DATA xpdd, struct xsd_sockmsg *msg);
400 char *
401 XenBus_Read(PVOID Context, xenbus_transaction_t xbt, char *path, char **value);
402 char *
403 XenBus_Write(PVOID Context, xenbus_transaction_t xbt, char *path, char *value);
404 char *
405 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, char *path, char *fmt, ...);
406 char *
407 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
408 char *
409 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
410 char *
411 XenBus_List(PVOID Context, xenbus_transaction_t xbt, char *prefix, char ***contents);
412 char *
413 XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
414 char *
415 XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
416 //VOID
417 //XenBus_ThreadProc(PVOID StartContext);
418 NTSTATUS
419 XenBus_Init(PXENPCI_DEVICE_DATA xpdd);
420 NTSTATUS
421 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd);
422 NTSTATUS
423 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd);
424 NTSTATUS
425 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd);
427 PHYSICAL_ADDRESS
428 XenPci_AllocMMIO(PXENPCI_DEVICE_DATA xpdd, ULONG len);
430 EVT_WDF_INTERRUPT_ISR EvtChn_EvtInterruptIsr;
431 EVT_WDF_INTERRUPT_ENABLE EvtChn_EvtInterruptEnable;
432 EVT_WDF_INTERRUPT_DISABLE EvtChn_EvtInterruptDisable;
434 NTSTATUS
435 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd);
436 NTSTATUS
437 EvtChn_Suspend(PXENPCI_DEVICE_DATA xpdd);
438 NTSTATUS
439 EvtChn_Resume(PXENPCI_DEVICE_DATA xpdd);
441 NTSTATUS
442 EvtChn_Mask(PVOID Context, evtchn_port_t Port);
443 NTSTATUS
444 EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
445 NTSTATUS
446 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext, ULONG flags);
447 NTSTATUS
448 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext, ULONG flags);
449 NTSTATUS
450 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector, PCHAR description, ULONG flags);
451 evtchn_port_t
452 EvtChn_AllocIpi(PVOID context, ULONG vcpu);
453 NTSTATUS
454 EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
455 NTSTATUS
456 EvtChn_Notify(PVOID Context, evtchn_port_t Port);
457 VOID
458 EvtChn_Close(PVOID Context, evtchn_port_t Port);
459 evtchn_port_t
460 EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
461 BOOLEAN
462 EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt);
464 VOID
465 GntTbl_Init(PXENPCI_DEVICE_DATA xpdd);
466 VOID
467 GntTbl_Suspend(PXENPCI_DEVICE_DATA xpdd);
468 VOID
469 GntTbl_Resume(PXENPCI_DEVICE_DATA xpdd);
470 grant_ref_t
471 GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t, int readonly, grant_ref_t ref, ULONG tag);
472 BOOLEAN
473 GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref, ULONG tag);
474 VOID
475 GntTbl_PutRef(PVOID Context, grant_ref_t ref, ULONG tag);
476 grant_ref_t
477 GntTbl_GetRef(PVOID Context, ULONG tag);
479 #endif