blob: 43a9e6d80d413271329d3ede7dc8867ae16ac84d [file] [log] [blame]
Kevin O'Connorb9c6a962013-09-14 13:01:30 -04001// 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
Kevin O'Connor341f8d92014-10-11 13:16:12 -040017// PORT_A20 bitdefs
18#define PORT_A20 0x0092
19#define A20_ENABLE_BIT 0x02
20
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040021#ifndef __ASSEMBLY__
22
23#include "types.h" // u32
24
25static inline void irq_disable(void)
26{
27 asm volatile("cli": : :"memory");
28}
29
30static inline void irq_enable(void)
31{
32 asm volatile("sti": : :"memory");
33}
34
35static inline u32 save_flags(void)
36{
37 u32 flags;
38 asm volatile("pushfl ; popl %0" : "=rm" (flags));
39 return flags;
40}
41
42static inline void restore_flags(u32 flags)
43{
44 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
45}
46
47static inline void cpu_relax(void)
48{
49 asm volatile("rep ; nop": : :"memory");
50}
51
52static inline void nop(void)
53{
54 asm volatile("nop");
55}
56
57static inline void hlt(void)
58{
59 asm volatile("hlt": : :"memory");
60}
61
62static 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)
Igor Mammedovcb75c912016-10-13 14:38:28 +020071#define CPUID_X2APIC (1 << 21)
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040072static inline void __cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
73{
74 asm("cpuid"
75 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
76 : "0" (index));
77}
78
Kevin O'Connor62de31b2015-09-22 12:35:00 -040079static inline u32 cr0_read(void) {
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040080 u32 cr0;
81 asm("movl %%cr0, %0" : "=r"(cr0));
82 return cr0;
83}
Kevin O'Connor62de31b2015-09-22 12:35:00 -040084static inline void cr0_write(u32 cr0) {
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040085 asm("movl %0, %%cr0" : : "r"(cr0));
86}
Kevin O'Connor62de31b2015-09-22 12:35:00 -040087static inline void cr0_mask(u32 off, u32 on) {
88 cr0_write((cr0_read() & ~off) | on);
89}
90static inline u16 cr0_vm86_read(void) {
Kevin O'Connor251e2632015-03-17 11:37:25 -040091 u16 cr0;
92 asm("smsww %0" : "=r"(cr0));
93 return cr0;
94}
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040095
96static inline u64 rdmsr(u32 index)
97{
98 u64 ret;
99 asm ("rdmsr" : "=A"(ret) : "c"(index));
100 return ret;
101}
102
103static inline void wrmsr(u32 index, u64 val)
104{
105 asm volatile ("wrmsr" : : "c"(index), "A"(val));
106}
107
108static inline u64 rdtscll(void)
109{
110 u64 val;
111 asm volatile("rdtsc" : "=A" (val));
112 return val;
113}
114
115static inline u32 __ffs(u32 word)
116{
117 asm("bsf %1,%0"
118 : "=r" (word)
119 : "rm" (word));
120 return word;
121}
122static inline u32 __fls(u32 word)
123{
124 asm("bsr %1,%0"
125 : "=r" (word)
126 : "rm" (word));
127 return word;
128}
129
130static inline u32 getesp(void) {
131 u32 esp;
132 asm("movl %%esp, %0" : "=rm"(esp));
133 return esp;
134}
135
Stefan Berger5aa2a752015-03-23 14:22:17 -0400136static inline u32 rol(u32 val, u16 rol) {
137 u32 res;
138 asm volatile("roll %%cl, %%eax"
139 : "=a" (res) : "a" (val), "c" (rol));
140 return res;
141}
142
Stefan Berger4e57a542021-06-14 13:35:48 -0400143static inline u32 ror(u32 val, u16 ror) {
144 u32 res;
145 asm volatile("rorl %%cl, %%eax"
146 : "=a" (res) : "a" (val), "c" (ror));
147 return res;
148}
149
Kevin O'Connor4ade5232013-09-18 21:41:48 -0400150static inline void outb(u8 value, u16 port) {
151 __asm__ __volatile__("outb %b0, %w1" : : "a"(value), "Nd"(port));
152}
153static inline void outw(u16 value, u16 port) {
154 __asm__ __volatile__("outw %w0, %w1" : : "a"(value), "Nd"(port));
155}
156static inline void outl(u32 value, u16 port) {
157 __asm__ __volatile__("outl %0, %w1" : : "a"(value), "Nd"(port));
158}
159static inline u8 inb(u16 port) {
160 u8 value;
161 __asm__ __volatile__("inb %w1, %b0" : "=a"(value) : "Nd"(port));
162 return value;
163}
164static inline u16 inw(u16 port) {
165 u16 value;
166 __asm__ __volatile__("inw %w1, %w0" : "=a"(value) : "Nd"(port));
167 return value;
168}
169static inline u32 inl(u16 port) {
170 u32 value;
171 __asm__ __volatile__("inl %w1, %0" : "=a"(value) : "Nd"(port));
172 return value;
173}
174
175static inline void insb(u16 port, u8 *data, u32 count) {
176 asm volatile("rep insb (%%dx), %%es:(%%edi)"
177 : "+c"(count), "+D"(data) : "d"(port) : "memory");
178}
179static inline void insw(u16 port, u16 *data, u32 count) {
180 asm volatile("rep insw (%%dx), %%es:(%%edi)"
181 : "+c"(count), "+D"(data) : "d"(port) : "memory");
182}
183static inline void insl(u16 port, u32 *data, u32 count) {
184 asm volatile("rep insl (%%dx), %%es:(%%edi)"
185 : "+c"(count), "+D"(data) : "d"(port) : "memory");
186}
187// XXX - outs not limited to es segment
188static inline void outsb(u16 port, u8 *data, u32 count) {
189 asm volatile("rep outsb %%es:(%%esi), (%%dx)"
190 : "+c"(count), "+S"(data) : "d"(port) : "memory");
191}
192static inline void outsw(u16 port, u16 *data, u32 count) {
193 asm volatile("rep outsw %%es:(%%esi), (%%dx)"
194 : "+c"(count), "+S"(data) : "d"(port) : "memory");
195}
196static inline void outsl(u16 port, u32 *data, u32 count) {
197 asm volatile("rep outsl %%es:(%%esi), (%%dx)"
198 : "+c"(count), "+S"(data) : "d"(port) : "memory");
199}
200
Kevin O'Connoreee06e02015-09-29 10:14:58 -0400201/* Compiler barrier is enough as an x86 CPU does not reorder reads or writes */
202static inline void smp_rmb(void) {
203 barrier();
204}
205static inline void smp_wmb(void) {
206 barrier();
207}
208
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400209static inline void writel(void *addr, u32 val) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800210 barrier();
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400211 *(volatile u32 *)addr = val;
212}
213static inline void writew(void *addr, u16 val) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800214 barrier();
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400215 *(volatile u16 *)addr = val;
216}
217static inline void writeb(void *addr, u8 val) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800218 barrier();
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400219 *(volatile u8 *)addr = val;
220}
Marc-André Lureau8694c3b2018-02-26 09:12:11 -0500221static inline u64 readq(const void *addr) {
222 u64 val = *(volatile const u64 *)addr;
223 barrier();
224 return val;
225}
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400226static inline u32 readl(const void *addr) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800227 u32 val = *(volatile const u32 *)addr;
228 barrier();
229 return val;
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400230}
231static inline u16 readw(const void *addr) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800232 u16 val = *(volatile const u16 *)addr;
233 barrier();
234 return val;
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400235}
236static inline u8 readb(const void *addr) {
Ameya Palandebc82fa42015-02-17 14:00:49 -0800237 u8 val = *(volatile const u8 *)addr;
238 barrier();
239 return val;
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400240}
241
242// GDT bits
243#define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
244#define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
245#define GDT_B (0x1ULL << 54) // Big flag
246#define GDT_G (0x1ULL << 55) // Granularity flag
247// GDT bits for segment base
248#define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
249 | (((u64)(v) & 0x00ffffff) << 16))
250// GDT bits for segment limit (0-1Meg)
251#define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
252 | (((u64)(v) & 0x0000ffff) << 0))
253// GDT bits for segment limit (0-4Gig in 4K chunks)
254#define GDT_GRANLIMIT(v) (GDT_G | GDT_LIMIT((v) >> 12))
255
256struct descloc_s {
257 u16 length;
258 u32 addr;
259} PACKED;
260
Kevin O'Connor0f0612e2013-12-04 11:48:05 -0500261static inline void sgdt(struct descloc_s *desc) {
262 asm("sgdtl %0" : "=m"(*desc));
263}
264static inline void lgdt(struct descloc_s *desc) {
265 asm("lgdtl %0" : : "m"(*desc) : "memory");
266}
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400267
Kevin O'Connor341f8d92014-10-11 13:16:12 -0400268static inline u8 get_a20(void) {
269 return (inb(PORT_A20) & A20_ENABLE_BIT) != 0;
270}
271
272static inline u8 set_a20(u8 cond) {
Kevin O'Connor8ebb33b2017-05-16 11:47:27 -0400273 u8 val = inb(PORT_A20), a20_enabled = (val & A20_ENABLE_BIT) != 0;
274 if (a20_enabled != !!cond)
275 outb(val ^ A20_ENABLE_BIT, PORT_A20);
276 return a20_enabled;
Kevin O'Connor341f8d92014-10-11 13:16:12 -0400277}
278
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400279// x86.c
280void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
281
Kevin O'Connorb9c6a962013-09-14 13:01:30 -0400282#endif // !__ASSEMBLY__
283
284#endif // x86.h