Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / x86.h
1 // Basic x86 asm functions.
2 #ifndef __X86_H
3 #define __X86_H
4
5 // CPU flag bitdefs
6 #define F_CF (1<<0)
7 #define F_ZF (1<<6)
8 #define F_IF (1<<9)
9 #define F_ID (1<<21)
10
11 // CR0 flags
12 #define CR0_PG (1<<31) // Paging
13 #define CR0_CD (1<<30) // Cache disable
14 #define CR0_NW (1<<29) // Not Write-through
15 #define CR0_PE (1<<0)  // Protection enable
16
17 // PORT_A20 bitdefs
18 #define PORT_A20 0x0092
19 #define A20_ENABLE_BIT 0x02
20
21 #ifndef __ASSEMBLY__
22
23 #include "types.h" // u32
24
25 static inline void irq_disable(void)
26 {
27     asm volatile("cli": : :"memory");
28 }
29
30 static inline void irq_enable(void)
31 {
32     asm volatile("sti": : :"memory");
33 }
34
35 static inline u32 save_flags(void)
36 {
37     u32 flags;
38     asm volatile("pushfl ; popl %0" : "=rm" (flags));
39     return flags;
40 }
41
42 static inline void restore_flags(u32 flags)
43 {
44     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
45 }
46
47 static inline void cpu_relax(void)
48 {
49     asm volatile("rep ; nop": : :"memory");
50 }
51
52 static inline void nop(void)
53 {
54     asm volatile("nop");
55 }
56
57 static inline void hlt(void)
58 {
59     asm volatile("hlt": : :"memory");
60 }
61
62 static inline void wbinvd(void)
63 {
64     asm volatile("wbinvd": : :"memory");
65 }
66
67 #define CPUID_TSC (1 << 4)
68 #define CPUID_MSR (1 << 5)
69 #define CPUID_APIC (1 << 9)
70 #define CPUID_MTRR (1 << 12)
71 static inline void __cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
72 {
73     asm("cpuid"
74         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
75         : "0" (index));
76 }
77
78 static inline u32 getcr0(void) {
79     u32 cr0;
80     asm("movl %%cr0, %0" : "=r"(cr0));
81     return cr0;
82 }
83 static inline void setcr0(u32 cr0) {
84     asm("movl %0, %%cr0" : : "r"(cr0));
85 }
86
87 static inline u64 rdmsr(u32 index)
88 {
89     u64 ret;
90     asm ("rdmsr" : "=A"(ret) : "c"(index));
91     return ret;
92 }
93
94 static inline void wrmsr(u32 index, u64 val)
95 {
96     asm volatile ("wrmsr" : : "c"(index), "A"(val));
97 }
98
99 static inline u64 rdtscll(void)
100 {
101     u64 val;
102     asm volatile("rdtsc" : "=A" (val));
103     return val;
104 }
105
106 static inline u32 __ffs(u32 word)
107 {
108     asm("bsf %1,%0"
109         : "=r" (word)
110         : "rm" (word));
111     return word;
112 }
113 static inline u32 __fls(u32 word)
114 {
115     asm("bsr %1,%0"
116         : "=r" (word)
117         : "rm" (word));
118     return word;
119 }
120
121 static inline u32 getesp(void) {
122     u32 esp;
123     asm("movl %%esp, %0" : "=rm"(esp));
124     return esp;
125 }
126
127 static inline void outb(u8 value, u16 port) {
128     __asm__ __volatile__("outb %b0, %w1" : : "a"(value), "Nd"(port));
129 }
130 static inline void outw(u16 value, u16 port) {
131     __asm__ __volatile__("outw %w0, %w1" : : "a"(value), "Nd"(port));
132 }
133 static inline void outl(u32 value, u16 port) {
134     __asm__ __volatile__("outl %0, %w1" : : "a"(value), "Nd"(port));
135 }
136 static inline u8 inb(u16 port) {
137     u8 value;
138     __asm__ __volatile__("inb %w1, %b0" : "=a"(value) : "Nd"(port));
139     return value;
140 }
141 static inline u16 inw(u16 port) {
142     u16 value;
143     __asm__ __volatile__("inw %w1, %w0" : "=a"(value) : "Nd"(port));
144     return value;
145 }
146 static inline u32 inl(u16 port) {
147     u32 value;
148     __asm__ __volatile__("inl %w1, %0" : "=a"(value) : "Nd"(port));
149     return value;
150 }
151
152 static inline void insb(u16 port, u8 *data, u32 count) {
153     asm volatile("rep insb (%%dx), %%es:(%%edi)"
154                  : "+c"(count), "+D"(data) : "d"(port) : "memory");
155 }
156 static inline void insw(u16 port, u16 *data, u32 count) {
157     asm volatile("rep insw (%%dx), %%es:(%%edi)"
158                  : "+c"(count), "+D"(data) : "d"(port) : "memory");
159 }
160 static inline void insl(u16 port, u32 *data, u32 count) {
161     asm volatile("rep insl (%%dx), %%es:(%%edi)"
162                  : "+c"(count), "+D"(data) : "d"(port) : "memory");
163 }
164 // XXX - outs not limited to es segment
165 static inline void outsb(u16 port, u8 *data, u32 count) {
166     asm volatile("rep outsb %%es:(%%esi), (%%dx)"
167                  : "+c"(count), "+S"(data) : "d"(port) : "memory");
168 }
169 static inline void outsw(u16 port, u16 *data, u32 count) {
170     asm volatile("rep outsw %%es:(%%esi), (%%dx)"
171                  : "+c"(count), "+S"(data) : "d"(port) : "memory");
172 }
173 static inline void outsl(u16 port, u32 *data, u32 count) {
174     asm volatile("rep outsl %%es:(%%esi), (%%dx)"
175                  : "+c"(count), "+S"(data) : "d"(port) : "memory");
176 }
177
178 static inline void writel(void *addr, u32 val) {
179     barrier();
180     *(volatile u32 *)addr = val;
181 }
182 static inline void writew(void *addr, u16 val) {
183     barrier();
184     *(volatile u16 *)addr = val;
185 }
186 static inline void writeb(void *addr, u8 val) {
187     barrier();
188     *(volatile u8 *)addr = val;
189 }
190 static inline u32 readl(const void *addr) {
191     u32 val = *(volatile const u32 *)addr;
192     barrier();
193     return val;
194 }
195 static inline u16 readw(const void *addr) {
196     u16 val = *(volatile const u16 *)addr;
197     barrier();
198     return val;
199 }
200 static inline u8 readb(const void *addr) {
201     u8 val = *(volatile const u8 *)addr;
202     barrier();
203     return val;
204 }
205
206 // GDT bits
207 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
208 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
209 #define GDT_B        (0x1ULL << 54)  // Big flag
210 #define GDT_G        (0x1ULL << 55)  // Granularity flag
211 // GDT bits for segment base
212 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
213                       | (((u64)(v) & 0x00ffffff) << 16))
214 // GDT bits for segment limit (0-1Meg)
215 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
216                       | (((u64)(v) & 0x0000ffff) << 0))
217 // GDT bits for segment limit (0-4Gig in 4K chunks)
218 #define GDT_GRANLIMIT(v) (GDT_G | GDT_LIMIT((v) >> 12))
219
220 struct descloc_s {
221     u16 length;
222     u32 addr;
223 } PACKED;
224
225 static inline void sgdt(struct descloc_s *desc) {
226     asm("sgdtl %0" : "=m"(*desc));
227 }
228 static inline void lgdt(struct descloc_s *desc) {
229     asm("lgdtl %0" : : "m"(*desc) : "memory");
230 }
231
232 static inline u8 get_a20(void) {
233     return (inb(PORT_A20) & A20_ENABLE_BIT) != 0;
234 }
235
236 static inline u8 set_a20(u8 cond) {
237     u8 val = inb(PORT_A20);
238     outb((val & ~A20_ENABLE_BIT) | (cond ? A20_ENABLE_BIT : 0), PORT_A20);
239     return (val & A20_ENABLE_BIT) != 0;
240 }
241
242 // x86.c
243 void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
244
245 #endif // !__ASSEMBLY__
246
247 #endif // x86.h