3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
33 #define PAGE_SIZE 0x1000
41 #define memcpy __builtin_memcpy
45 #define memset __builtin_memset
49 #define memcmp __builtin_memcmp
53 #define strcpy __builtin_strcpy
58 //#define sprintf __builtin_sprintf
61 #define STRUCT_PACKED __attribute__((__packed__))
62 #define STRUCT_ALIGNED(x) __attribute__((__aligned__(x)))
64 #define UNUSED_VAR(v) v __attribute__((__unused__))
66 #define ALIGN_UP(value, align) ( ((value) & (align-1))? ( ((value) + (align-1)) & ~(align-1) ): (value) )
67 #define ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
68 #define NUM_PAGES_SPANNED(addr, len) ( (ALIGN_UP(addr+len, PAGE_SIZE) - ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
70 #define MIN(a, b) ((a) < (b)? (a): (b))
71 #define MAX(a, b) ((a) > (b)? (a): (b))
73 #define LOWORD(dw) ((unsigned short) (dw))
74 #define HIWORD(dw) ((unsigned short) (((unsigned int) (dw) >> 16) & 0xFFFF))
76 #define FIELD_OFFSET(t, f) ((unsigned int)(unsigned long)&(((t *)0)->f))
89 #define NULL (void *)0
92 typedef struct _DLIST_ENTRY
{
93 struct _DLIST_ENTRY
*Flink
;
94 struct _DLIST_ENTRY
*Blink
;
100 typedef unsigned int UINT32
;
102 typedef unsigned long UINT64
;
104 typedef unsigned long long UINT64
;
107 typedef unsigned long long ULONGLONG
;
108 typedef unsigned int ULONG
;
109 typedef unsigned short USHORT
;
110 typedef unsigned char UCHAR
;
119 typedef long long INT64
;
124 typedef long long LONGLONG
;
129 typedef unsigned long SIZE_T
;
130 //typedef unsigned char GUID[16];
131 typedef unsigned char BOOL
;
132 typedef unsigned char BOOLEAN
;
133 typedef void* HANDLE
;
134 typedef UINT32 DWORD
;
136 typedef unsigned char BYTE
;
138 typedef unsigned long ULONG_PTR
;
141 unsigned char Data
[16];
144 typedef void (*PFN_WORKITEM_CALLBACK
)(void* context
);
145 typedef void (*PFN_TIMER_CALLBACK
)(void* context
);
150 #define RDMSR(reg, v) { \
152 __asm__ __volatile__("rdmsr" \
153 : "=a" (l), "=d" (h) \
155 v = (((UINT64)h) << 32) | l; \
158 #define WRMSR(reg, v) { \
160 l = (UINT32)(((UINT64)(v)) & 0xFFFFFFFF); \
161 h = (UINT32)((((UINT64)(v)) >> 32) & 0xFFFFFFFF); \
162 __asm__ __volatile__("wrmsr" \
164 : "c" (reg), "a" (l), "d" (h)); \
169 #define RDMSR(reg, v) \
170 __asm__ __volatile__("rdmsr" \
174 #define WRMSR(reg, v) \
175 __asm__ __volatile__("wrmsr" \
177 : "c" (reg), "A" ((UINT64)v))
182 static inline void do_cpuid(unsigned int op
, unsigned int *eax
, unsigned int *ebx
, unsigned int *ecx
, unsigned int *edx
)
184 __asm__
__volatile__("cpuid" : "=a" (*eax
), "=b" (*ebx
), "=c" (*ecx
), "=d" (*edx
) : "0" (op
), "c" (ecx
));
190 extern void LogMsg(const char *fmt
, ...);
192 extern void BitSet(unsigned int* addr
, int value
);
193 extern void BitClear(unsigned int* addr
, int value
);
194 extern int BitTest(unsigned int* addr
, int value
);
195 extern int BitTestAndClear(unsigned int* addr
, int value
);
196 extern int BitTestAndSet(unsigned int* addr
, int value
);
198 extern int InterlockedIncrement(int *val
);
199 extern int InterlockedDecrement(int *val
);
200 extern int InterlockedCompareExchange(int *val
, int new, int curr
);
202 extern void Sleep(unsigned long usecs
);
204 extern void* VirtualAllocExec(unsigned int size
);
205 extern void VirtualFree(void* VirtAddr
);
207 extern void* PageAlloc(unsigned int count
);
208 extern void PageFree(void* page
, unsigned int count
);
210 extern void* MemMapIO(unsigned long phys
, unsigned long size
);
211 extern void MemUnmapIO(void* virt
);
213 extern void* MemAlloc(unsigned int size
);
214 extern void* MemAllocZeroed(unsigned int size
);
215 extern void* MemAllocAtomic(unsigned int size
);
216 extern void MemFree(void* buf
);
217 extern void MemoryFence(void);
219 extern HANDLE
TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB
, void* context
);
220 extern void TimerClose(HANDLE hTimer
);
221 extern int TimerStop(HANDLE hTimer
);
222 extern void TimerStart(HANDLE hTimer
, UINT32 expirationInUs
);
223 extern SIZE_T
GetTickCount(void);
225 extern HANDLE
WaitEventCreate(void);
226 extern void WaitEventClose(HANDLE hWait
);
227 extern void WaitEventSet(HANDLE hWait
);
228 extern int WaitEventWait(HANDLE hWait
);
230 // If >0, hWait got signaled. If ==0, timeout. If < 0, error
231 extern int WaitEventWaitEx(HANDLE hWait
, UINT32 TimeoutInMs
);
233 extern HANDLE
SpinlockCreate(void);
234 extern void SpinlockClose(HANDLE hSpin
);
235 extern void SpinlockAcquire(HANDLE hSpin
);
236 extern void SpinlockRelease(HANDLE hSpin
);
239 #define GetVirtualAddress Physical2LogicalAddr
240 void* Physical2LogicalAddr(ULONG_PTR PhysAddr
);
242 #define GetPhysicalAddress Logical2PhysicalAddr
243 ULONG_PTR
Logical2PhysicalAddr(void * LogicalAddr
);
245 ULONG_PTR
Virtual2Physical(void * VirtAddr
);
247 void* PageMapVirtualAddress(unsigned long Pfn
);
248 void PageUnmapVirtualAddress(void* VirtAddr
);
251 extern HANDLE
WorkQueueCreate(char* name
);
252 extern void WorkQueueClose(HANDLE hWorkQueue
);
253 extern int WorkQueueQueueWorkItem(HANDLE hWorkQueue
, PFN_WORKITEM_CALLBACK workItem
, void* context
);
255 extern void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem
, void* context
);