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 ALIGN_UP(value, align) ( ((value) & (align-1))? ( ((value) + (align-1)) & ~(align-1) ): (value) )
34 #define ALIGN_DOWN(value, align) ( (value) & ~(align-1) )
35 #define NUM_PAGES_SPANNED(addr, len) ( (ALIGN_UP(addr+len, PAGE_SIZE) - ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
37 #define LOWORD(dw) ((unsigned short) (dw))
38 #define HIWORD(dw) ((unsigned short) (((unsigned int) (dw) >> 16) & 0xFFFF))
40 #define FIELD_OFFSET(t, f) ((unsigned int)(unsigned long)&(((t *)0)->f))
53 #define NULL (void *)0
56 typedef struct _DLIST_ENTRY {
57 struct _DLIST_ENTRY *Flink;
58 struct _DLIST_ENTRY *Blink;
64 //typedef unsigned char GUID[16];
68 unsigned char Data[16];
71 typedef void (*PFN_WORKITEM_CALLBACK)(void* context);
72 typedef void (*PFN_TIMER_CALLBACK)(void* context);
77 #define RDMSR(reg, v) { \
79 __asm__ __volatile__("rdmsr" \
80 : "=a" (l), "=d" (h) \
82 v = (((u64)h) << 32) | l; \
85 #define WRMSR(reg, v) { \
87 l = (u32)(((u64)(v)) & 0xFFFFFFFF); \
88 h = (u32)((((u64)(v)) >> 32) & 0xFFFFFFFF); \
89 __asm__ __volatile__("wrmsr" \
91 : "c" (reg), "a" (l), "d" (h)); \
96 #define RDMSR(reg, v) \
97 __asm__ __volatile__("rdmsr" \
101 #define WRMSR(reg, v) \
102 __asm__ __volatile__("wrmsr" \
104 : "c" (reg), "A" ((u64)v))
109 static inline void do_cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
111 __asm__ __volatile__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "c" (ecx));
117 extern void LogMsg(const char *fmt, ...);
119 extern void BitSet(unsigned int* addr, int value);
120 extern void BitClear(unsigned int* addr, int value);
121 extern int BitTest(unsigned int* addr, int value);
122 extern int BitTestAndClear(unsigned int* addr, int value);
123 extern int BitTestAndSet(unsigned int* addr, int value);
125 extern int InterlockedIncrement(int *val);
126 extern int InterlockedDecrement(int *val);
127 extern int InterlockedCompareExchange(int *val, int new, int curr);
129 extern void Sleep(unsigned long usecs);
131 extern void* VirtualAllocExec(unsigned int size);
132 extern void VirtualFree(void* VirtAddr);
134 extern void* PageAlloc(unsigned int count);
135 extern void PageFree(void* page, unsigned int count);
137 extern void* MemMapIO(unsigned long phys, unsigned long size);
138 extern void MemUnmapIO(void* virt);
140 extern void* MemAlloc(unsigned int size);
141 extern void* MemAllocZeroed(unsigned int size);
142 extern void* MemAllocAtomic(unsigned int size);
143 extern void MemFree(void* buf);
144 extern void MemoryFence(void);
146 extern HANDLE TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context);
147 extern void TimerClose(HANDLE hTimer);
148 extern int TimerStop(HANDLE hTimer);
149 extern void TimerStart(HANDLE hTimer, u32 expirationInUs);
150 extern size_t GetTickCount(void);
152 extern HANDLE WaitEventCreate(void);
153 extern void WaitEventClose(HANDLE hWait);
154 extern void WaitEventSet(HANDLE hWait);
155 extern int WaitEventWait(HANDLE hWait);
157 // If >0, hWait got signaled. If ==0, timeout. If < 0, error
158 extern int WaitEventWaitEx(HANDLE hWait, u32 TimeoutInMs);
160 extern HANDLE SpinlockCreate(void);
161 extern void SpinlockClose(HANDLE hSpin);
162 extern void SpinlockAcquire(HANDLE hSpin);
163 extern void SpinlockRelease(HANDLE hSpin);
166 #define GetVirtualAddress Physical2LogicalAddr
167 void* Physical2LogicalAddr(unsigned long PhysAddr);
169 #define GetPhysicalAddress Logical2PhysicalAddr
170 unsigned long Logical2PhysicalAddr(void * LogicalAddr);
172 unsigned long Virtual2Physical(void * VirtAddr);
174 void* PageMapVirtualAddress(unsigned long Pfn);
175 void PageUnmapVirtualAddress(void* VirtAddr);
178 extern HANDLE WorkQueueCreate(char* name);
179 extern void WorkQueueClose(HANDLE hWorkQueue);
180 extern int WorkQueueQueueWorkItem(HANDLE hWorkQueue, PFN_WORKITEM_CALLBACK workItem, void* context);
182 extern void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem, void* context);