1 /* i386 cpu.h */
2
rdtsc(void)3 static inline uint64_t rdtsc(void)
4 {
5 uint64_t v;
6 asm volatile("rdtsc" : "=A" (v));
7 return v;
8 }
9
rdtscl(void)10 static inline uint32_t rdtscl(void)
11 {
12 uint32_t v;
13 asm volatile("rdtsc" : "=a" (v) : : "edx");
14 return v;
15 }
16
cpuid_count(uint32_t op,uint32_t cnt,uint32_t * eax,uint32_t * ebx,uint32_t * ecx,uint32_t * edx)17 static inline void cpuid_count(uint32_t op, uint32_t cnt,
18 uint32_t * eax, uint32_t * ebx,
19 uint32_t * ecx, uint32_t * edx)
20 {
21 asm volatile("movl %%ebx,%1 ; "
22 "cpuid ; "
23 "xchgl %1,%%ebx"
24 : "=a" (*eax), "=SD" (*ebx), "=c" (*ecx), "=d" (*edx)
25 : "a"(op), "c"(cnt));
26 }
27
cpuid(uint32_t op,uint32_t * eax,uint32_t * ebx,uint32_t * ecx,uint32_t * edx)28 static inline void cpuid(uint32_t op, uint32_t * eax, uint32_t * ebx,
29 uint32_t * ecx, uint32_t * edx)
30 {
31 cpuid_count(op, 0, eax, ebx, ecx, edx);
32 }
33
cpuid_eax(uint32_t level)34 static inline __constfunc uint32_t cpuid_eax(uint32_t level)
35 {
36 uint32_t v;
37
38 asm volatile("pushl %%ebx ; "
39 "cpuid ; "
40 "popl %%ebx"
41 : "=a" (v)
42 : "a"(level)
43 : "ecx", "edx");
44 return v;
45 }
46
cpuid_ebx(uint32_t level)47 static inline __constfunc uint32_t cpuid_ebx(uint32_t level)
48 {
49 uint32_t v;
50
51 asm volatile("movl %%ebx,%0 ; "
52 "cpuid ; "
53 "xchgl %0,%%ebx"
54 : "=SD" (v), "+a" (level)
55 : : "ecx", "edx");
56 return v;
57 }
58
cpuid_ecx(uint32_t level)59 static inline __constfunc uint32_t cpuid_ecx(uint32_t level)
60 {
61 uint32_t v;
62
63 asm volatile("pushl %%ebx ; "
64 "cpuid ; "
65 "popl %%ebx"
66 : "=c" (v), "+a" (level)
67 : : "edx");
68 return v;
69 }
70
cpuid_edx(uint32_t level)71 static inline __constfunc uint32_t cpuid_edx(uint32_t level)
72 {
73 uint32_t v;
74
75 asm volatile("pushl %%ebx ; "
76 "cpuid ; "
77 "popl %%ebx"
78 : "=d" (v), "+a" (level)
79 : : "ecx");
80 return v;
81 }
82
rdmsr(uint32_t msr)83 static inline uint64_t rdmsr(uint32_t msr)
84 {
85 uint64_t v;
86
87 asm volatile("rdmsr" : "=A" (v) : "c"(msr));
88 return v;
89 }
90
wrmsr(uint64_t v,uint32_t msr)91 static inline void wrmsr(uint64_t v, uint32_t msr)
92 {
93 asm volatile("wrmsr" : : "A" (v), "c" (msr));
94 }
95
cpu_relax(void)96 static inline void cpu_relax(void)
97 {
98 asm volatile("rep ; nop" : : : "memory");
99 }
100
hlt(void)101 static inline void hlt(void)
102 {
103 asm volatile("hlt" : : : "memory");
104 }
105
cli(void)106 static inline void cli(void)
107 {
108 asm volatile("cli" : : : "memory");
109 }
110
sti(void)111 static inline void sti(void)
112 {
113 asm volatile("sti" : : : "memory");
114 }
115