com32: Unbreak <sys/cpu.h>
[profile/ivi/syslinux.git] / com32 / include / sys / cpu.h
1 #ifndef _CPU_H
2 #define _CPU_H
3
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <klibc/compiler.h>
7
8 static inline uint64_t rdtsc(void)
9 {
10     uint64_t v;
11     asm volatile("rdtsc" : "=A" (v));
12     return v;
13 }
14
15 static inline uint32_t rdtscl(void)
16 {
17     uint32_t v;
18     asm volatile("rdtsc" : "=a" (v) : : "edx");
19     return v;
20 }
21
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)
25 {
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));
31 }
32
33 static inline void cpuid(uint32_t op, uint32_t * eax, uint32_t * ebx,
34                          uint32_t * ecx, uint32_t * edx)
35 {
36     cpuid_count(op, 0, eax, ebx, ecx, edx);
37 }
38
39 static inline __constfunc uint32_t cpuid_eax(uint32_t level)
40 {
41     uint32_t v;
42
43     asm volatile("pushl %%ebx ; "
44                  "cpuid ; "
45                  "popl %%ebx"
46                  : "=a" (v)
47                  : "a"(level)
48                  : "ecx", "edx");
49     return v;
50 }
51
52 static inline __constfunc uint32_t cpuid_ebx(uint32_t level)
53 {
54     uint32_t v;
55
56     asm volatile("movl %%ebx,%0 ; "
57                  "cpuid ; "
58                  "xchgl %0,%%ebx"
59                  : "=SD" (v), "+a" (level)
60                  : : "ecx", "edx");
61     return v;
62 }
63
64 static inline __constfunc uint32_t cpuid_ecx(uint32_t level)
65 {
66     uint32_t v;
67
68     asm volatile("pushl %%ebx ; "
69                  "cpuid ; "
70                  "popl %%ebx"
71                  : "=c" (v), "+a" (level)
72                  : : "edx");
73     return v;
74 }
75
76 static inline __constfunc uint32_t cpuid_edx(uint32_t level)
77 {
78     uint32_t v;
79
80     asm volatile("pushl %%ebx ; "
81                  "cpuid ; "
82                  "popl %%ebx"
83                  : "=d" (v), "+a" (level)
84                  : : "ecx");
85     return v;
86 }
87
88 /* Standard macro to see if a specific flag is changeable */
89 static inline __constfunc bool cpu_has_eflag(uint32_t flag)
90 {
91         uint32_t f0, f1;
92
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));
105
106         return !!((f0^f1) & flag);
107 }
108
109 static inline uint64_t rdmsr(uint32_t msr)
110 {
111     uint64_t v;
112
113     asm volatile("rdmsr" : "=A" (v) : "c"(msr));
114     return v;
115 }
116
117 static inline void wrmsr(uint64_t v, uint32_t msr)
118 {
119     asm volatile("wrmsr" : : "A" (v), "c" (msr));
120 }
121
122 static inline void cpu_relax(void)
123 {
124     asm volatile("rep ; nop");
125 }
126
127 static inline void hlt(void)
128 {
129     asm volatile("hlt");
130 }
131
132 static inline void cli(void)
133 {
134     asm volatile("cli");
135 }
136
137 static inline void sti(void)
138 {
139     asm volatile("sti");
140 }
141
142 #endif