1 // Basic x86 asm functions.
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
18 #define PORT_A20 0x0092
19 #define A20_ENABLE_BIT 0x02
23 #include "types.h" // u32
25 static inline void irq_disable(void)
27 asm volatile("cli": : :"memory");
30 static inline void irq_enable(void)
32 asm volatile("sti": : :"memory");
35 static inline u32 save_flags(void)
38 asm volatile("pushfl ; popl %0" : "=rm" (flags));
42 static inline void restore_flags(u32 flags)
44 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
47 static inline void cpu_relax(void)
49 asm volatile("rep ; nop": : :"memory");
52 static inline void nop(void)
57 static inline void hlt(void)
59 asm volatile("hlt": : :"memory");
62 static inline void wbinvd(void)
64 asm volatile("wbinvd": : :"memory");
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)
74 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
78 static inline u32 getcr0(void) {
80 asm("movl %%cr0, %0" : "=r"(cr0));
83 static inline void setcr0(u32 cr0) {
84 asm("movl %0, %%cr0" : : "r"(cr0));
87 static inline u64 rdmsr(u32 index)
90 asm ("rdmsr" : "=A"(ret) : "c"(index));
94 static inline void wrmsr(u32 index, u64 val)
96 asm volatile ("wrmsr" : : "c"(index), "A"(val));
99 static inline u64 rdtscll(void)
102 asm volatile("rdtsc" : "=A" (val));
106 static inline u32 __ffs(u32 word)
113 static inline u32 __fls(u32 word)
121 static inline u32 getesp(void) {
123 asm("movl %%esp, %0" : "=rm"(esp));
127 static inline void outb(u8 value, u16 port) {
128 __asm__ __volatile__("outb %b0, %w1" : : "a"(value), "Nd"(port));
130 static inline void outw(u16 value, u16 port) {
131 __asm__ __volatile__("outw %w0, %w1" : : "a"(value), "Nd"(port));
133 static inline void outl(u32 value, u16 port) {
134 __asm__ __volatile__("outl %0, %w1" : : "a"(value), "Nd"(port));
136 static inline u8 inb(u16 port) {
138 __asm__ __volatile__("inb %w1, %b0" : "=a"(value) : "Nd"(port));
141 static inline u16 inw(u16 port) {
143 __asm__ __volatile__("inw %w1, %w0" : "=a"(value) : "Nd"(port));
146 static inline u32 inl(u16 port) {
148 __asm__ __volatile__("inl %w1, %0" : "=a"(value) : "Nd"(port));
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");
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");
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");
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");
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");
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");
178 static inline void writel(void *addr, u32 val) {
180 *(volatile u32 *)addr = val;
182 static inline void writew(void *addr, u16 val) {
184 *(volatile u16 *)addr = val;
186 static inline void writeb(void *addr, u8 val) {
188 *(volatile u8 *)addr = val;
190 static inline u32 readl(const void *addr) {
191 u32 val = *(volatile const u32 *)addr;
195 static inline u16 readw(const void *addr) {
196 u16 val = *(volatile const u16 *)addr;
200 static inline u8 readb(const void *addr) {
201 u8 val = *(volatile const u8 *)addr;
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))
225 static inline void sgdt(struct descloc_s *desc) {
226 asm("sgdtl %0" : "=m"(*desc));
228 static inline void lgdt(struct descloc_s *desc) {
229 asm("lgdtl %0" : : "m"(*desc) : "memory");
232 static inline u8 get_a20(void) {
233 return (inb(PORT_A20) & A20_ENABLE_BIT) != 0;
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;
243 void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
245 #endif // !__ASSEMBLY__