win-pvdrivers

view common/include/xen_windows.h @ 497:312a41f82b8f

Updated to support the proposed patches to qemu from Steven Smith on ~20081216
author James Harper <james.harper@bendigoit.com.au>
date Fri Dec 19 22:08:17 2008 +1100 (2008-12-19)
parents a6e3f76419c3
children 1d39de3ab8d6
line source
1 #if !defined(_XEN_WINDOWS_H_)
2 #define _XEN_WINDOWS_H_
4 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
5 #pragma warning( disable : 4214 ) // nonstandard extension used : bit field types other than int
6 #pragma warning( disable : 4505 ) // 'XenDbgPrint' : unreferenced local function has been removed
8 #define __XEN_INTERFACE_VERSION__ 0x00030205
9 #if defined(_AMD64_)
10 #define __x86_64__
11 #elif defined(_IA64_)
12 #define __ia64__
13 #elif defined(__MINGW32__)
14 /* __i386__ already defined */
15 #elif defined(_X86_)
16 #define __i386__
17 #else
18 #error Unknown architecture
19 #endif
21 #ifdef __MINGW32__
22 typedef signed char int8_t;
23 typedef unsigned char uint8_t;
24 typedef signed short int16_t;
25 typedef unsigned short uint16_t;
26 typedef signed int int32_t;
27 typedef unsigned int uint32_t;
28 typedef unsigned long long uint64_t;
29 #else
30 typedef INT8 int8_t;
31 typedef UINT8 uint8_t;
32 typedef INT16 int16_t;
33 typedef UINT16 uint16_t;
34 typedef INT32 int32_t;
35 typedef UINT32 uint32_t;
36 typedef UINT64 uint64_t;
37 #endif
39 #include <xen.h>
41 #define _PAGE_PRESENT 0x001UL
42 #define _PAGE_RW 0x002UL
43 #define _PAGE_USER 0x004UL
44 #define _PAGE_PWT 0x008UL
45 #define _PAGE_PCD 0x010UL
46 #define _PAGE_ACCESSED 0x020UL
47 #define _PAGE_DIRTY 0x040UL
48 #define _PAGE_PAT 0x080UL
49 #define _PAGE_PSE 0x080UL
50 #define _PAGE_GLOBAL 0x100UL
52 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
54 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
56 typedef unsigned long xenbus_transaction_t;
58 #define XBT_NIL ((xenbus_transaction_t)0)
60 #define SPLITSTRING_POOL_TAG (ULONG) 'SSPT'
62 #define wmb() KeMemoryBarrier()
63 #define mb() KeMemoryBarrier()
64 #define FUNCTION_ENTER() XenDbgPrint(__DRIVER_NAME " --> %s\n", __FUNCTION__)
65 #define FUNCTION_EXIT() XenDbgPrint(__DRIVER_NAME " <-- %s\n", __FUNCTION__)
66 #define FUNCTION_EXIT_STATUS(_status) XenDbgPrint(__DRIVER_NAME " <-- %s, status = %08x\n", __FUNCTION__, _status)
67 #define FUNCTION_ERROR_EXIT() XenDbgPrint(__DRIVER_NAME " <-- %s (error path)\n", __FUNCTION__)
68 #define FUNCTION_CALLED() XenDbgPrint(__DRIVER_NAME " %s called (line %d)\n", __FUNCTION__, __LINE__)
69 #ifdef __MINGW32__
70 #define FUNCTION_MSG(_x) _FUNCTION_MSG _x
71 #define _FUNCTION_MSG(format, args...) XenDbgPrint(__DRIVER_NAME " %s called: " format, __FUNCTION__, ##args)
72 #else
73 #define FUNCTION_MSG(format, ...) XenDbgPrint(__DRIVER_NAME " " format, __VA_ARGS__);
74 #endif
76 static __inline char **
77 SplitString(char *String, char Split, int MaxParts, int *Count)
78 {
79 char **RetVal;
80 char *first;
81 char *last;
83 //KdPrint((__DRIVER_NAME " a\n"));
85 *Count = 0;
87 RetVal = (char **)ExAllocatePoolWithTag(NonPagedPool, (MaxParts + 1) * sizeof(char *), SPLITSTRING_POOL_TAG);
88 last = String;
89 do
90 {
91 if (*Count == MaxParts)
92 break;
93 //KdPrint((__DRIVER_NAME " b - count = %d\n", *Count));
94 first = last;
95 for (last = first; *last != '\0' && *last != Split; last++);
96 RetVal[*Count] = (char *)ExAllocatePoolWithTag(NonPagedPool, last - first + 1, SPLITSTRING_POOL_TAG);
97 //KdPrint((__DRIVER_NAME " c - count = %d\n", *Count));
98 strncpy(RetVal[*Count], first, last - first);
99 RetVal[*Count][last - first] = 0;
100 //KdPrint((__DRIVER_NAME " d - count = %d\n", *Count));
101 (*Count)++;
102 //KdPrint((__DRIVER_NAME " e - count = %d\n", *Count));
103 if (*last == Split)
104 last++;
105 } while (*last != 0);
106 //KdPrint((__DRIVER_NAME " f - count = %d\n", *Count));
107 RetVal[*Count] = NULL;
108 return RetVal;
109 }
111 static __inline VOID
112 FreeSplitString(char **Bits, int Count)
113 {
114 int i;
116 for (i = 0; i < Count; i++)
117 ExFreePoolWithTag(Bits[i], SPLITSTRING_POOL_TAG);
118 ExFreePoolWithTag(Bits, SPLITSTRING_POOL_TAG);
119 }
121 #define ALLOCATE_PAGES_POOL_TAG (ULONG) 'APPT'
123 static PMDL
124 AllocatePagesExtra(int Pages, int ExtraSize)
125 {
126 PMDL Mdl;
127 PVOID Buf;
129 Buf = ExAllocatePoolWithTag(NonPagedPool, Pages * PAGE_SIZE, ALLOCATE_PAGES_POOL_TAG);
130 if (Buf == NULL)
131 {
132 KdPrint((__DRIVER_NAME " AllocatePages Failed at ExAllocatePoolWithTag\n"));
133 return NULL;
134 }
135 // KdPrint((__DRIVER_NAME " --- AllocatePages IRQL = %d, Buf = %p\n", KeGetCurrentIrql(), Buf));
136 Mdl = (PMDL)ExAllocatePoolWithTag(NonPagedPool, MmSizeOfMdl(Buf, Pages * PAGE_SIZE) + ExtraSize, ALLOCATE_PAGES_POOL_TAG);
137 //Mdl = IoAllocateMdl(Buf, Pages * PAGE_SIZE, FALSE, FALSE, NULL);
138 if (Mdl == NULL)
139 {
140 // free the memory here
141 KdPrint((__DRIVER_NAME " AllocatePages Failed at IoAllocateMdl\n"));
142 return NULL;
143 }
145 MmInitializeMdl(Mdl, Buf, Pages * PAGE_SIZE);
146 MmBuildMdlForNonPagedPool(Mdl);
148 return Mdl;
149 }
151 static __inline PMDL
152 AllocatePages(int Pages)
153 {
154 return AllocatePagesExtra(Pages, 0);
155 }
157 static __inline PMDL
158 AllocatePage()
159 {
160 return AllocatePagesExtra(1, 0);
161 }
163 static __inline PMDL
164 AllocateUncachedPage()
165 {
166 PMDL mdl;
167 PVOID buf;
169 buf = MmAllocateNonCachedMemory(PAGE_SIZE);
170 mdl = IoAllocateMdl(buf, PAGE_SIZE, FALSE, FALSE, NULL);
171 MmBuildMdlForNonPagedPool(mdl);
173 return mdl;
174 }
176 static __inline VOID
177 FreeUncachedPage(PMDL mdl)
178 {
179 PVOID buf = MmGetMdlVirtualAddress(mdl);
181 IoFreeMdl(mdl);
182 MmFreeNonCachedMemory(buf, PAGE_SIZE);
183 }
185 static __inline VOID
186 FreePages(PMDL Mdl)
187 {
188 PVOID Buf = MmGetMdlVirtualAddress(Mdl);
189 // KdPrint((__DRIVER_NAME " --- FreePages IRQL = %d, Buf = %p\n", KeGetCurrentIrql(), Buf));
190 // IoFreeMdl(Mdl);
191 ExFreePoolWithTag(Mdl, ALLOCATE_PAGES_POOL_TAG);
192 ExFreePoolWithTag(Buf, ALLOCATE_PAGES_POOL_TAG);
193 }
195 #define XEN_IOPORT_BASE 0x10
197 /*
198 define these as pointers so that the READ_PORT* functions complain if
199 the wrong width is used with the wrong defined port
200 */
202 #define XEN_IOPORT_MAGIC ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 0))
203 #define XEN_IOPORT_LOG ((PUCHAR)UlongToPtr(XEN_IOPORT_BASE + 2))
204 #define XEN_IOPORT_VERSION ((PUCHAR)UlongToPtr(XEN_IOPORT_BASE + 2))
205 #define XEN_IOPORT_PRODUCT ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 2))
206 #define XEN_IOPORT_BUILD ((PULONG)UlongToPtr(XEN_IOPORT_BASE + 0))
207 #define XEN_IOPORT_DEVICE_MASK ((PUSHORT)UlongToPtr(XEN_IOPORT_BASE + 0))
209 #define QEMU_UNPLUG_ALL_IDE_DISKS 1
210 #define QEMU_UNPLUG_ALL_NICS 2
211 #define QEMU_UNPLUG_AUX_IDE_DISKS 4
213 static BOOLEAN debug_port_probed = FALSE;
214 static BOOLEAN debug_port_enabled;
216 static void XenDbgPrint(PCHAR format, ...)
217 {
218 CHAR buf[512];
219 va_list ap;
220 ULONG i;
221 //int cpu;
222 KIRQL old_irql = 0;
224 if (!debug_port_probed)
225 {
226 if (READ_PORT_USHORT(XEN_IOPORT_MAGIC) == 0x49d2
227 || READ_PORT_USHORT(XEN_IOPORT_MAGIC) == 0xd249)
228 {
229 debug_port_enabled = TRUE;
230 }
231 else
232 {
233 debug_port_enabled = FALSE;
234 }
235 debug_port_probed = TRUE;
236 }
237 va_start(ap, format);
238 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), format, ap);
239 va_end(ap);
240 DbgPrint(buf);
242 if (debug_port_enabled)
243 {
244 KeRaiseIrql(HIGH_LEVEL, &old_irql);
245 //cpu = KeGetCurrentProcessorNumber() & 0x07;
246 for (i = 0; i < strlen(buf); i++)
247 {
248 WRITE_PORT_UCHAR(XEN_IOPORT_LOG, buf[i]);
249 }
250 KeLowerIrql(old_irql);
251 }
252 }
254 #ifdef KdPrint
255 #undef KdPrint
256 #endif
257 #define KdPrint(_x_) XenDbgPrint _x_
259 #endif