win-pvdrivers

view xenpci/xenpci.h @ 982:e9011a1ba5bb

started rewriting xenpci<->child interface
author James Harper <james.harper@bendigoit.com.au>
date Mon Aug 13 17:10:03 2012 +1000 (2012-08-13)
parents ef1f25e2c281
children
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];
189 BOOLEAN xb_state;
191 struct xenstore_domain_interface *xen_store_interface;
193 PKTHREAD balloon_thread;
194 KEVENT balloon_event;
195 BOOLEAN balloon_shutdown;
196 ULONG initial_memory_kb;
197 ULONG current_memory_kb;
198 ULONG target_memory_kb;
200 /* xenbus related */
201 XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
202 KSPIN_LOCK xb_ring_spinlock;
203 FAST_MUTEX xb_watch_mutex;
204 FAST_MUTEX xb_request_mutex;
205 KEVENT xb_request_complete_event;
206 struct xsd_sockmsg *xb_reply;
207 struct xsd_sockmsg *xb_msg;
208 ULONG xb_msg_offset;
210 WDFCHILDLIST child_list;
212 KSPIN_LOCK suspend_lock;
213 evtchn_port_t suspend_evtchn;
214 int suspend_state;
216 UNICODE_STRING legacy_interface_name;
217 UNICODE_STRING interface_name;
218 BOOLEAN interface_open;
220 BOOLEAN removable;
222 BOOLEAN hibernated;
224 WDFQUEUE io_queue;
226 LIST_ENTRY veto_list;
228 } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
230 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_DATA, GetXpdd)
232 #if 0
233 typedef struct {
234 UCHAR front_target;
235 UCHAR back_expected;
236 UCHAR wait; /* units = 100ms */
237 } XENPCI_STATE_MAP_ELEMENT, *PXENPCI_STATE_MAP_ELEMENT;
238 #endif
240 typedef struct {
241 WDFDEVICE wdf_device;
242 WDFDEVICE wdf_device_bus_fdo;
243 BOOLEAN reported_missing;
244 char path[128];
245 char device[128];
246 ULONG index;
247 ULONG irq_number;
248 ULONG irq_vector;
249 KIRQL irq_level;
250 char backend_path[128];
251 domid_t backend_id;
252 ULONG backend_state;
253 BOOLEAN backend_initiated_remove;
254 #if 0
255 KEVENT backend_state_event;
256 FAST_MUTEX backend_state_mutex;
257 ULONG frontend_state;
258 PMDL config_page_mdl;
259 PHYSICAL_ADDRESS config_page_phys;
260 ULONG config_page_length;
261 PUCHAR requested_resources_start;
262 PUCHAR requested_resources_ptr;
263 PUCHAR assigned_resources_start;
264 PUCHAR assigned_resources_ptr;
265 XENPCI_DEVICE_STATE device_state;
266 BOOLEAN restart_on_resume;
268 XENPCI_STATE_MAP_ELEMENT xb_pre_connect_map[5];
269 XENPCI_STATE_MAP_ELEMENT xb_post_connect_map[5];
270 XENPCI_STATE_MAP_ELEMENT xb_shutdown_map[5];
272 #endif
273 BOOLEAN hiber_usage_kludge;
274 BOOLEAN do_not_enumerate;
275 } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
277 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_PDO_DEVICE_DATA, GetXppdd)
279 typedef struct {
280 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER header;
281 CHAR path[128];
282 CHAR device[128];
283 ULONG index;
284 } XENPCI_PDO_IDENTIFICATION_DESCRIPTION, *PXENPCI_PDO_IDENTIFICATION_DESCRIPTION;
286 #define XEN_INTERFACE_VERSION 1
288 //#define DEVICE_INTERFACE_TYPE_LEGACY 0
289 #define DEVICE_INTERFACE_TYPE_XENBUS 1
290 #define DEVICE_INTERFACE_TYPE_EVTCHN 2
291 #define DEVICE_INTERFACE_TYPE_GNTDEV 3
293 typedef struct {
294 ULONG len;
295 WDFQUEUE io_queue;
296 union {
297 struct xsd_sockmsg msg;
298 UCHAR buffer[PAGE_SIZE];
299 } u;
300 LIST_ENTRY read_list_head;
301 LIST_ENTRY watch_list_head;
302 } XENBUS_INTERFACE_DATA, *PXENBUS_INTERFACE_DATA;
304 typedef struct {
305 ULONG dummy; /* fill this in with whatever is required */
306 } EVTCHN_INTERFACE_DATA, *PEVTCHN_INTERFACE_DATA;
308 typedef struct {
309 ULONG dummy; /* fill this in with whatever is required */
310 } GNTDEV_INTERFACE_DATA, *PGNTDEV_INTERFACE_DATA;
312 typedef struct {
313 ULONG type;
314 KSPIN_LOCK lock;
315 WDFQUEUE io_queue;
316 EVT_WDF_FILE_CLEANUP *EvtFileCleanup;
317 EVT_WDF_FILE_CLOSE *EvtFileClose;
318 union {
319 XENBUS_INTERFACE_DATA xenbus;
320 EVTCHN_INTERFACE_DATA evtchn;
321 GNTDEV_INTERFACE_DATA gntdev;
322 };
323 } XENPCI_DEVICE_INTERFACE_DATA, *PXENPCI_DEVICE_INTERFACE_DATA;
325 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_INTERFACE_DATA, GetXpdid)
327 NTSTATUS
328 XenBus_DeviceFileInit(WDFDEVICE device, PWDF_IO_QUEUE_CONFIG queue_config, WDFFILEOBJECT file_object);
330 EVT_WDF_DEVICE_FILE_CREATE XenPci_EvtDeviceFileCreate;
331 EVT_WDF_FILE_CLOSE XenPci_EvtFileClose;
332 EVT_WDF_FILE_CLEANUP XenPci_EvtFileCleanup;
333 EVT_WDF_IO_QUEUE_IO_DEFAULT XenPci_EvtIoDefault;
335 #define HYPERVISOR_memory_op(xpdd, cmd, arg) _HYPERVISOR_memory_op(xpdd->hypercall_stubs, cmd, arg)
336 #define HYPERVISOR_xen_version(xpdd, cmd, arg) _HYPERVISOR_xen_version(xpdd->hypercall_stubs, cmd, arg)
337 #define HYPERVISOR_grant_table_op(xpdd, cmd, uop, count) _HYPERVISOR_grant_table_op(xpdd->hypercall_stubs, cmd, uop, count)
338 #define HYPERVISOR_hvm_op(xpdd, op, arg) _HYPERVISOR_hvm_op(xpdd->hypercall_stubs, op, arg)
339 #define HYPERVISOR_event_channel_op(xpdd, cmd, op) _HYPERVISOR_event_channel_op(xpdd->hypercall_stubs, cmd, op)
340 #define HYPERVISOR_sched_op(xpdd, cmd, arg) _HYPERVISOR_sched_op(xpdd->hypercall_stubs, cmd, arg)
341 #define HYPERVISOR_shutdown(xpdd, reason) _HYPERVISOR_shutdown(xpdd->hypercall_stubs, reason)
343 #define hvm_get_parameter(xvdd, hvm_param) _hvm_get_parameter(xvdd->hypercall_stubs, hvm_param);
344 #define hvm_set_parameter(xvdd, hvm_param, value) _hvm_set_parameter(xvdd->hypercall_stubs, hvm_param, value);
345 #define hvm_shutdown(xvdd, reason) _hvm_shutdown(xvdd->hypercall_stubs, reason);
346 #define HYPERVISOR_yield(xvdd) _HYPERVISOR_yield(xvdd->hypercall_stubs);
348 #include "hypercall.h"
350 #define XBT_NIL ((xenbus_transaction_t)0)
352 PVOID
353 hvm_get_hypercall_stubs();
354 VOID
355 hvm_free_hypercall_stubs(PVOID hypercall_stubs);
357 EVT_WDF_DEVICE_PREPARE_HARDWARE XenPci_EvtDevicePrepareHardware;
358 EVT_WDF_DEVICE_RELEASE_HARDWARE XenPci_EvtDeviceReleaseHardware;
359 EVT_WDF_DEVICE_D0_ENTRY XenPci_EvtDeviceD0Entry;
360 EVT_WDF_DEVICE_D0_ENTRY_POST_INTERRUPTS_ENABLED XenPci_EvtDeviceD0EntryPostInterruptsEnabled;
361 EVT_WDF_DEVICE_D0_EXIT XenPci_EvtDeviceD0Exit;
362 EVT_WDF_DEVICE_D0_EXIT_PRE_INTERRUPTS_DISABLED XenPci_EvtDeviceD0ExitPreInterruptsDisabled;
363 EVT_WDF_DEVICE_QUERY_REMOVE XenPci_EvtDeviceQueryRemove;
364 EVT_WDF_CHILD_LIST_CREATE_DEVICE XenPci_EvtChildListCreateDevice;
365 EVT_WDF_CHILD_LIST_SCAN_FOR_CHILDREN XenPci_EvtChildListScanForChildren;
367 VOID
368 XenPci_HideQemuDevices();
369 extern WDFCOLLECTION qemu_hide_devices;
370 extern USHORT qemu_hide_flags_value;
372 NTSTATUS
373 XenPci_Pdo_Suspend(WDFDEVICE device);
374 NTSTATUS
375 XenPci_Pdo_Resume(WDFDEVICE device);
377 VOID
378 XenPci_DumpPdoConfig(PDEVICE_OBJECT device_object);
380 typedef VOID
381 (*PXENPCI_HIGHSYNC_FUNCTION)(PVOID context);
383 VOID
384 XenPci_HighSync(PXENPCI_HIGHSYNC_FUNCTION function0, PXENPCI_HIGHSYNC_FUNCTION functionN, PVOID context);
386 VOID
387 XenPci_PatchKernel(PXENPCI_DEVICE_DATA xpdd, PVOID base, ULONG length);
389 NTSTATUS
390 XenPci_HookDbgPrint();
391 NTSTATUS
392 XenPci_UnHookDbgPrint();
394 struct xsd_sockmsg *
395 XenBus_Raw(PXENPCI_DEVICE_DATA xpdd, struct xsd_sockmsg *msg);
396 char *
397 XenBus_Read(PVOID Context, xenbus_transaction_t xbt, char *path, char **value);
398 char *
399 XenBus_Write(PVOID Context, xenbus_transaction_t xbt, char *path, char *value);
400 char *
401 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, char *path, char *fmt, ...);
402 char *
403 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
404 char *
405 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
406 char *
407 XenBus_List(PVOID Context, xenbus_transaction_t xbt, char *prefix, char ***contents);
408 char *
409 XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
410 char *
411 XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
412 //VOID
413 //XenBus_ThreadProc(PVOID StartContext);
414 NTSTATUS
415 XenBus_Init(PXENPCI_DEVICE_DATA xpdd);
416 NTSTATUS
417 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd);
418 NTSTATUS
419 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd);
420 NTSTATUS
421 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd);
423 PHYSICAL_ADDRESS
424 XenPci_AllocMMIO(PXENPCI_DEVICE_DATA xpdd, ULONG len);
426 EVT_WDF_INTERRUPT_ISR EvtChn_EvtInterruptIsr;
427 EVT_WDF_INTERRUPT_ENABLE EvtChn_EvtInterruptEnable;
428 EVT_WDF_INTERRUPT_DISABLE EvtChn_EvtInterruptDisable;
430 NTSTATUS
431 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd);
432 NTSTATUS
433 EvtChn_Suspend(PXENPCI_DEVICE_DATA xpdd);
434 NTSTATUS
435 EvtChn_Resume(PXENPCI_DEVICE_DATA xpdd);
437 NTSTATUS
438 EvtChn_Mask(PVOID Context, evtchn_port_t Port);
439 NTSTATUS
440 EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
441 NTSTATUS
442 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext, ULONG flags);
443 NTSTATUS
444 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext, ULONG flags);
445 NTSTATUS
446 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector, PCHAR description, ULONG flags);
447 evtchn_port_t
448 EvtChn_AllocIpi(PVOID context, ULONG vcpu);
449 NTSTATUS
450 EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
451 NTSTATUS
452 EvtChn_Notify(PVOID Context, evtchn_port_t Port);
453 VOID
454 EvtChn_Close(PVOID Context, evtchn_port_t Port);
455 evtchn_port_t
456 EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
457 BOOLEAN
458 EvtChn_AckEvent(PVOID context, evtchn_port_t port, BOOLEAN *last_interrupt);
460 VOID
461 GntTbl_Init(PXENPCI_DEVICE_DATA xpdd);
462 VOID
463 GntTbl_Suspend(PXENPCI_DEVICE_DATA xpdd);
464 VOID
465 GntTbl_Resume(PXENPCI_DEVICE_DATA xpdd);
466 grant_ref_t
467 GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t, int readonly, grant_ref_t ref, ULONG tag);
468 BOOLEAN
469 GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref, ULONG tag);
470 VOID
471 GntTbl_PutRef(PVOID Context, grant_ref_t ref, ULONG tag);
472 grant_ref_t
473 GntTbl_GetRef(PVOID Context, ULONG tag);
475 #endif