win-pvdrivers

view common/include/xen_windows.h @ 1054:471c94d04d8a

Refactoring to support xencache (tmem)
author James Harper <james.harper@bendigoit.com.au>
date Sun Jun 02 16:37:21 2013 +1000 (2013-06-02)
parents e0944ce4ea98
children ebf4d299224b
line source
1 #if !defined(_XEN_WINDOWS_H_)
2 #define _XEN_WINDOWS_H_
4 #include "gplpv_version.h"
6 #include <ntverp.h>
7 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
8 #pragma warning( disable : 4214 ) // nonstandard extension used : bit field types other than int
9 #pragma warning( disable : 4505 ) // 'XenDbgPrint' : unreferenced local function has been removed
11 #define __XEN_INTERFACE_VERSION__ 0x00030205
12 #if defined(_AMD64_)
13 #define __x86_64__
14 #elif defined(_IA64_)
15 #define __ia64__
16 #elif defined(_X86_)
17 #define __i386__
18 #else
19 #error Unknown architecture
20 #endif
22 typedef INT8 int8_t;
23 typedef UINT8 uint8_t;
24 typedef INT16 int16_t;
25 typedef UINT16 uint16_t;
26 typedef INT32 int32_t;
27 typedef UINT32 uint32_t;
28 typedef UINT64 uint64_t;
30 #include <xen.h>
31 #include <grant_table.h>
32 #include <event_channel.h>
33 #include <xen_guids.h>
35 #define _PAGE_PRESENT 0x001UL
36 #define _PAGE_RW 0x002UL
37 #define _PAGE_USER 0x004UL
38 #define _PAGE_PWT 0x008UL
39 #define _PAGE_PCD 0x010UL
40 #define _PAGE_ACCESSED 0x020UL
41 #define _PAGE_DIRTY 0x040UL
42 #define _PAGE_PAT 0x080UL
43 #define _PAGE_PSE 0x080UL
44 #define _PAGE_GLOBAL 0x100UL
46 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
48 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
50 typedef unsigned long xenbus_transaction_t;
52 #define XBT_NIL ((xenbus_transaction_t)0)
54 #define SPLITSTRING_POOL_TAG (ULONG) 'SSPT'
56 #define wmb() KeMemoryBarrier()
57 #define mb() KeMemoryBarrier()
59 static __inline char **
60 SplitString(char *String, char Split, int MaxParts, int *Count) {
61 char **RetVal;
62 char *first;
63 char *last;
65 *Count = 0;
67 RetVal = (char **)ExAllocatePoolWithTag(NonPagedPool, (MaxParts + 1) * sizeof(char *), SPLITSTRING_POOL_TAG);
68 last = String;
69 do {
70 if (*Count == MaxParts)
71 break;
72 first = last;
73 for (last = first; *last != '\0' && *last != Split; last++);
74 RetVal[*Count] = (char *)ExAllocatePoolWithTag(NonPagedPool, last - first + 1, SPLITSTRING_POOL_TAG);
75 RtlStringCbCopyNA(RetVal[*Count], last - first + 1, first, last - first);
76 RetVal[*Count][last - first] = 0;
77 (*Count)++;
78 if (*last == Split)
79 last++;
80 } while (*last != 0);
81 RetVal[*Count] = NULL;
82 return RetVal;
83 }
85 static __inline VOID
86 FreeSplitString(char **Bits, int Count) {
87 int i;
89 for (i = 0; i < Count; i++)
90 ExFreePoolWithTag(Bits[i], SPLITSTRING_POOL_TAG);
91 ExFreePoolWithTag(Bits, SPLITSTRING_POOL_TAG);
92 }
94 #define ALLOCATE_PAGES_POOL_TAG (ULONG) 'APPT'
96 static PMDL
97 AllocatePagesExtra(int Pages, int ExtraSize)
98 {
99 PMDL Mdl;
100 PVOID Buf;
102 Buf = ExAllocatePoolWithTag(NonPagedPool, Pages * PAGE_SIZE, ALLOCATE_PAGES_POOL_TAG);
103 if (Buf == NULL) {
104 KdPrint((__DRIVER_NAME " AllocatePages Failed at ExAllocatePoolWithTag\n"));
105 return NULL;
106 }
107 Mdl = (PMDL)ExAllocatePoolWithTag(NonPagedPool, MmSizeOfMdl(Buf, Pages * PAGE_SIZE) + ExtraSize, ALLOCATE_PAGES_POOL_TAG);
108 if (Mdl == NULL) {
109 ExFreePoolWithTag(Buf, ALLOCATE_PAGES_POOL_TAG);
110 KdPrint((__DRIVER_NAME " AllocatePages Failed at IoAllocateMdl\n"));
111 return NULL;
112 }
114 MmInitializeMdl(Mdl, Buf, Pages * PAGE_SIZE);
115 MmBuildMdlForNonPagedPool(Mdl);
117 return Mdl;
118 }
120 static __inline PMDL
121 AllocatePages(int Pages) {
122 return AllocatePagesExtra(Pages, 0);
123 }
125 static __inline PMDL
126 AllocatePage() {
127 return AllocatePagesExtra(1, 0);
128 }
130 static __inline PMDL
131 AllocateUncachedPage() {
132 PMDL mdl;
133 PVOID buf;
135 buf = MmAllocateNonCachedMemory(PAGE_SIZE);
136 mdl = IoAllocateMdl(buf, PAGE_SIZE, FALSE, FALSE, NULL);
137 MmBuildMdlForNonPagedPool(mdl);
139 return mdl;
140 }
142 static __inline VOID
143 FreeUncachedPage(PMDL mdl) {
144 PVOID buf = MmGetMdlVirtualAddress(mdl);
146 IoFreeMdl(mdl);
147 MmFreeNonCachedMemory(buf, PAGE_SIZE);
148 }
150 static __inline VOID
151 FreePages(PMDL Mdl) {
152 PVOID Buf = MmGetMdlVirtualAddress(Mdl);
153 ExFreePoolWithTag(Mdl, ALLOCATE_PAGES_POOL_TAG);
154 ExFreePoolWithTag(Buf, ALLOCATE_PAGES_POOL_TAG);
155 }
157 #define XEN_IOPORT_BASE 0x10
159 /*
160 define these as pointers so that the READ_PORT* functions complain if
161 the wrong width is used with the wrong defined port
162 */
164 #define XEN_IOPORT_MAGIC ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 0))
165 #define XEN_IOPORT_LOG ((PUCHAR)UlongToPtr(XEN_IOPORT_BASE + 2))
166 #define XEN_IOPORT_VERSION ((PUCHAR)UlongToPtr(XEN_IOPORT_BASE + 2))
167 #define XEN_IOPORT_PRODUCT ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 2))
168 #define XEN_IOPORT_BUILD ((PULONG)UlongToPtr(XEN_IOPORT_BASE + 0))
169 #define XEN_IOPORT_DEVICE_MASK ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 0))
171 #define QEMU_UNPLUG_ALL_IDE_DISKS 1
172 #define QEMU_UNPLUG_ALL_NICS 2
173 #define QEMU_UNPLUG_AUX_IDE_DISKS 4
175 #if DBG
176 #define FUNCTION_ENTER() XnDebugPrint(__DRIVER_NAME " --> %s\n", __FUNCTION__)
177 #define FUNCTION_EXIT() XnDebugPrint(__DRIVER_NAME " <-- %s\n", __FUNCTION__)
178 #define FUNCTION_EXIT_STATUS(_status) XnDebugPrint(__DRIVER_NAME " <-- %s, status = %08x\n", __FUNCTION__, _status)
179 #define FUNCTION_MSG(...) XnDebugPrint(__DRIVER_NAME " " __VA_ARGS__)
180 #else
181 #define FUNCTION_ENTER()
182 #define FUNCTION_EXIT()
183 #define FUNCTION_EXIT_STATUS(_status)
184 #define FUNCTION_MSG(...)
185 #endif
187 #define INVALID_GRANT_REF 0xFFFFFFFF
189 #define XN_BASE_FRONTEND 1 /* path is relative to frontend device */
190 #define XN_BASE_BACKEND 2 /* path is relative to backend device */
191 #define XN_BASE_GLOBAL 3 /* path is relative to root of xenstore */
193 #define XN_DEVICE_CALLBACK_BACKEND_STATE 1 /* backend state change callback */
194 #define XN_DEVICE_CALLBACK_SUSPEND 2
195 #define XN_DEVICE_CALLBACK_RESUME 3
197 typedef PVOID XN_HANDLE;
199 typedef VOID
200 (*PXN_WATCH_CALLBACK)(PVOID context, char *path);
202 typedef VOID
203 (*PXN_EVENT_CALLBACK)(PVOID context);
205 typedef VOID
206 (*PXN_DEVICE_CALLBACK)(PVOID context, ULONG callback_type, PVOID value);
208 ULONG
209 XnGetVersion();
211 XN_HANDLE
212 XnOpenDevice(PDEVICE_OBJECT pdo, PXN_DEVICE_CALLBACK callback, PVOID context);
214 VOID
215 XnCloseDevice(XN_HANDLE handle);
217 #define XN_VALUE_TYPE_QEMU_HIDE_FLAGS 1
218 #define XN_VALUE_TYPE_QEMU_FILTER 2 /* true if qemu devices hidden by device filter, not by qemu */
220 VOID
221 XnGetValue(XN_HANDLE handle, ULONG value_type, PVOID value);
223 NTSTATUS
224 XnReadInt32(XN_HANDLE handle, ULONG base, PCHAR path, ULONG *value);
226 NTSTATUS
227 XnWriteInt32(XN_HANDLE handle, ULONG base, PCHAR path, ULONG value);
229 NTSTATUS
230 XnReadInt64(XN_HANDLE handle, ULONG base, PCHAR path, ULONGLONG *value);
232 NTSTATUS
233 XnWriteInt64(XN_HANDLE handle, ULONG base, PCHAR path, ULONGLONG value);
235 NTSTATUS
236 XnReadString(XN_HANDLE handle, ULONG base, PCHAR path, PCHAR *value);
238 NTSTATUS
239 XnWriteString(XN_HANDLE handle, ULONG base, PCHAR path, PCHAR value);
241 NTSTATUS
242 XnFreeString(XN_HANDLE handle, PCHAR string);
244 NTSTATUS
245 XnNotify(XN_HANDLE handle, evtchn_port_t port);
247 grant_ref_t
248 XnGrantAccess(XN_HANDLE handle, uint32_t frame, int readonly, grant_ref_t ref, ULONG tag);
250 BOOLEAN
251 XnEndAccess(XN_HANDLE handle, grant_ref_t ref, BOOLEAN keepref, ULONG tag);
253 grant_ref_t
254 XnAllocateGrant(XN_HANDLE handle, ULONG tag);
256 VOID
257 XnFreeGrant(XN_HANDLE handle, grant_ref_t ref, ULONG tag);
259 NTSTATUS
260 XnBindEvent(XN_HANDLE handle, evtchn_port_t *port, PXN_EVENT_CALLBACK callback, PVOID context);
262 NTSTATUS
263 XnUnbindEvent(XN_HANDLE handle, evtchn_port_t port);
265 ULONG
266 XnTmemOp(struct tmem_op *tmem_op);
268 #ifndef XENPCI_POOL_TAG
269 #define XENPCI_POOL_TAG (ULONG) 'XenP'
270 #endif
272 static __inline VOID
273 XnFreeMem(XN_HANDLE handle, PVOID Ptr) {
274 UNREFERENCED_PARAMETER(handle);
275 ExFreePoolWithTag(Ptr, XENPCI_POOL_TAG);
276 }
279 //VOID
280 //XnDumpModeHookDebugPrint();
283 NTSTATUS
284 XnDebugPrint(PCHAR format, ...);
286 VOID
287 XnPrintDump();
289 #if DBG
290 #define XN_ASSERT(expr) \
291 if (!(expr)) { \
292 XnDebugPrint("ASSERT(%s) %s:%d\n", #expr, __FILE__, __LINE__); \
293 ASSERT(expr); \
294 }
295 #else
296 #define XN_ASSERT(expr)
297 #endif
299 #endif