Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / com32 / include / sys / cpu.h
blob53a6250eca2e5bb5f9141aa2527e99c782c734ed
1 #ifndef _CPU_H
2 #define _CPU_H
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <klibc/compiler.h>
8 static inline uint64_t rdtsc(void)
10 uint64_t v;
11 asm volatile("rdtsc" : "=A" (v));
12 return v;
15 static inline uint32_t rdtscl(void)
17 uint32_t v;
18 asm volatile("rdtsc" : "=a" (v) : : "edx");
19 return v;
22 static inline void cpuid_count(uint32_t op, uint32_t cnt,
23 uint32_t * eax, uint32_t * ebx,
24 uint32_t * ecx, uint32_t * edx)
26 asm volatile("movl %%ebx,%1 ; "
27 "cpuid ; "
28 "xchgl %1,%%ebx"
29 : "=a" (*eax), "=SD" (*ebx), "=c" (*ecx), "=d" (*edx)
30 : "a"(op), "c"(cnt));
33 static inline void cpuid(uint32_t op, uint32_t * eax, uint32_t * ebx,
34 uint32_t * ecx, uint32_t * edx)
36 cpuid_count(op, 0, eax, ebx, ecx, edx);
39 static inline __constfunc uint32_t cpuid_eax(uint32_t level)
41 uint32_t v;
43 asm volatile("pushl %%ebx ; "
44 "cpuid ; "
45 "popl %%ebx"
46 : "=a" (v)
47 : "a"(level)
48 : "ecx", "edx");
49 return v;
52 static inline __constfunc uint32_t cpuid_ebx(uint32_t level)
54 uint32_t v;
56 asm volatile("movl %%ebx,%0 ; "
57 "cpuid ; "
58 "xchgl %0,%%ebx"
59 : "=SD" (v), "+a" (level)
60 : : "ecx", "edx");
61 return v;
64 static inline __constfunc uint32_t cpuid_ecx(uint32_t level)
66 uint32_t v;
68 asm volatile("pushl %%ebx ; "
69 "cpuid ; "
70 "popl %%ebx"
71 : "=c" (v), "+a" (level)
72 : : "edx");
73 return v;
76 static inline __constfunc uint32_t cpuid_edx(uint32_t level)
78 uint32_t v;
80 asm volatile("pushl %%ebx ; "
81 "cpuid ; "
82 "popl %%ebx"
83 : "=d" (v), "+a" (level)
84 : : "ecx");
85 return v;
88 /* Standard macro to see if a specific flag is changeable */
89 static inline __constfunc bool cpu_has_eflag(uint32_t flag)
91 uint32_t f0, f1;
93 asm("pushfl ; "
94 "pushfl ; "
95 "popl %0 ; "
96 "movl %0,%1 ; "
97 "xorl %2,%1 ; "
98 "pushl %1 ; "
99 "popfl ; "
100 "pushfl ; "
101 "popl %1 ; "
102 "popfl"
103 : "=&r" (f0), "=&r" (f1)
104 : "ri" (flag));
106 return !!((f0^f1) & flag);
109 static inline uint64_t rdmsr(uint32_t msr)
111 uint64_t v;
113 asm volatile("rdmsr" : "=A" (v) : "c"(msr));
114 return v;
117 static inline void wrmsr(uint64_t v, uint32_t msr)
119 asm volatile("wrmsr" : : "A" (v), "c" (msr));
122 static inline void cpu_relax(void)
124 asm volatile("rep ; nop");
127 static inline void hlt(void)
129 asm volatile("hlt");
132 static inline void cli(void)
134 asm volatile("cli");
137 static inline void sti(void)
139 asm volatile("sti");
142 #endif