]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/x86/include/asm/processor.h
x86: Factor out set_rdmsr_value and get_wrmsr_value
[jailhouse.git] / hypervisor / arch / x86 / include / asm / processor.h
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013
5  * Copyright (c) Valentine Sinitsyn, 2014
6  *
7  * Authors:
8  *  Jan Kiszka <jan.kiszka@siemens.com>
9  *  Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  */
14
15 #ifndef _JAILHOUSE_ASM_PROCESSOR_H
16 #define _JAILHOUSE_ASM_PROCESSOR_H
17
18 #include <jailhouse/types.h>
19
20 /* leaf 0x01, ECX */
21 #define X86_FEATURE_VMX                                 (1 << 5)
22 #define X86_FEATURE_XSAVE                               (1 << 26)
23
24 /* leaf 0x80000001, ECX */
25 #define X86_FEATURE_SVM                                 (1 << 2)
26
27 /* leaf 0x80000001, EDX */
28 #define X86_FEATURE_GBPAGES                             (1 << 26)
29 #define X86_FEATURE_RDTSCP                              (1 << 27)
30
31 /* leaf 0x8000000a, EDX */
32 #define X86_FEATURE_NP                                  (1 << 0)
33 #define X86_FEATURE_FLUSH_BY_ASID                       (1 << 6)
34 #define X86_FEATURE_DECODE_ASSISTS                      (1 << 7)
35 #define X86_FEATURE_AVIC                                (1 << 13)
36
37 #define X86_RFLAGS_VM                                   (1 << 17)
38
39 #define X86_CR0_PE                                      (1UL << 0)
40 #define X86_CR0_MP                                      (1UL << 1)
41 #define X86_CR0_TS                                      (1UL << 3)
42 #define X86_CR0_ET                                      (1UL << 4)
43 #define X86_CR0_NE                                      (1UL << 5)
44 #define X86_CR0_WP                                      (1UL << 16)
45 #define X86_CR0_NW                                      (1UL << 29)
46 #define X86_CR0_CD                                      (1UL << 30)
47 #define X86_CR0_PG                                      (1UL << 31)
48 #define X86_CR0_RESERVED                                \
49         (BIT_MASK(28, 19) |  (1UL << 17) | BIT_MASK(15, 6))
50
51 #define X86_CR4_PAE                                     (1UL << 5)
52 #define X86_CR4_VMXE                                    (1UL << 13)
53 #define X86_CR4_OSXSAVE                                 (1UL << 18)
54 #define X86_CR4_RESERVED                                \
55         (BIT_MASK(31, 22) | (1UL << 19) | (1UL << 15) | BIT_MASK(12, 11))
56
57 #define X86_XCR0_FP                                     0x00000001
58
59 #define MSR_IA32_APICBASE                               0x0000001b
60 #define MSR_IA32_FEATURE_CONTROL                        0x0000003a
61 #define MSR_IA32_PAT                                    0x00000277
62 #define MSR_IA32_SYSENTER_CS                            0x00000174
63 #define MSR_IA32_SYSENTER_ESP                           0x00000175
64 #define MSR_IA32_SYSENTER_EIP                           0x00000176
65 #define MSR_IA32_PERF_GLOBAL_CTRL                       0x0000038f
66 #define MSR_IA32_VMX_BASIC                              0x00000480
67 #define MSR_IA32_VMX_PINBASED_CTLS                      0x00000481
68 #define MSR_IA32_VMX_PROCBASED_CTLS                     0x00000482
69 #define MSR_IA32_VMX_EXIT_CTLS                          0x00000483
70 #define MSR_IA32_VMX_ENTRY_CTLS                         0x00000484
71 #define MSR_IA32_VMX_MISC                               0x00000485
72 #define MSR_IA32_VMX_CR0_FIXED0                         0x00000486
73 #define MSR_IA32_VMX_CR0_FIXED1                         0x00000487
74 #define MSR_IA32_VMX_CR4_FIXED0                         0x00000488
75 #define MSR_IA32_VMX_CR4_FIXED1                         0x00000489
76 #define MSR_IA32_VMX_PROCBASED_CTLS2                    0x0000048b
77 #define MSR_IA32_VMX_EPT_VPID_CAP                       0x0000048c
78 #define MSR_IA32_VMX_TRUE_PROCBASED_CTLS                0x0000048e
79 #define MSR_X2APIC_BASE                                 0x00000800
80 #define MSR_X2APIC_ICR                                  0x00000830
81 #define MSR_X2APIC_END                                  0x0000083f
82 #define MSR_EFER                                        0xc0000080
83 #define MSR_STAR                                        0xc0000081
84 #define MSR_LSTAR                                       0xc0000082
85 #define MSR_CSTAR                                       0xc0000083
86 #define MSR_SFMASK                                      0xc0000084
87 #define MSR_FS_BASE                                     0xc0000100
88 #define MSR_GS_BASE                                     0xc0000101
89 #define MSR_KERNGS_BASE                                 0xc0000102
90
91 #define FEATURE_CONTROL_LOCKED                          (1 << 0)
92 #define FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX       (1 << 2)
93
94 #define PAT_RESET_VALUE                                 0x0007040600070406UL
95
96 #define EFER_LME                                        0x00000100
97 #define EFER_LMA                                        0x00000400
98 #define EFER_NXE                                        0x00000800
99
100 #define GDT_DESC_NULL                                   0
101 #define GDT_DESC_CODE                                   1
102 #define GDT_DESC_TSS                                    2
103 #define GDT_DESC_TSS_HI                                 3
104 #define NUM_GDT_DESC                                    4
105
106 #define X86_INST_LEN_CPUID                              2
107 #define X86_INST_LEN_RDMSR                              2
108 #define X86_INST_LEN_WRMSR                              2
109 /* This covers both VMCALL and VMMCALL */
110 #define X86_INST_LEN_HYPERCALL                          3
111 #define X86_INST_LEN_MOV_TO_CR                          3
112 #define X86_INST_LEN_XSETBV                             3
113
114 #define X86_REX_CODE                                    4
115
116 #define X86_OP_MOV_TO_MEM                               0x89
117 #define X86_OP_MOV_FROM_MEM                             0x8b
118
119 #define NMI_VECTOR                                      2
120 #define PF_VECTOR                                       14
121
122 #define DESC_TSS_BUSY                                   (1UL << (9 + 32))
123 #define DESC_PRESENT                                    (1UL << (15 + 32))
124 #define DESC_CODE_DATA                                  (1UL << (12 + 32))
125 #define DESC_PAGE_GRAN                                  (1UL << (23 + 32))
126
127 #ifndef __ASSEMBLY__
128
129 /**
130  * @ingroup X86
131  * @defgroup Processor Processor
132  *
133  * Low-level support for x86 processor configuration and status retrieval.
134  *
135  * @{
136  */
137
138 struct registers {
139         unsigned long r15;
140         unsigned long r14;
141         unsigned long r13;
142         unsigned long r12;
143         unsigned long r11;
144         unsigned long r10;
145         unsigned long r9;
146         unsigned long r8;
147         unsigned long rdi;
148         unsigned long rsi;
149         unsigned long rbp;
150         unsigned long unused;
151         unsigned long rbx;
152         unsigned long rdx;
153         unsigned long rcx;
154         unsigned long rax;
155 };
156
157 struct desc_table_reg {
158         u16 limit;
159         u64 base;
160 } __attribute__((packed));
161
162 struct segment {
163         u64 base;
164         u32 limit;
165         u32 access_rights;
166         u16 selector;
167 };
168
169 static unsigned long __force_order;
170
171 static inline void cpu_relax(void)
172 {
173         asm volatile("rep; nop" : : : "memory");
174 }
175
176 static inline void memory_barrier(void)
177 {
178         asm volatile("mfence" : : : "memory");
179 }
180
181 static inline void memory_load_barrier(void)
182 {
183         asm volatile("lfence" : : : "memory");
184 }
185
186 static inline void cpuid(unsigned int *eax, unsigned int *ebx,
187                          unsigned int *ecx, unsigned int *edx)
188 {
189         /* ecx is often an input as well as an output. */
190         asm volatile("cpuid"
191             : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
192             : "0" (*eax), "2" (*ecx)
193             : "memory");
194 }
195
196 #define CPUID_REG(reg)                                          \
197 static inline unsigned int cpuid_##reg(unsigned int op)         \
198 {                                                               \
199         unsigned int eax, ebx, ecx, edx;                        \
200                                                                 \
201         eax = op;                                               \
202         ecx = 0;                                                \
203         cpuid(&eax, &ebx, &ecx, &edx);                          \
204         return reg;                                             \
205 }
206
207 CPUID_REG(eax)
208 CPUID_REG(ebx)
209 CPUID_REG(ecx)
210 CPUID_REG(edx)
211
212 static inline unsigned long read_cr0(void)
213 {
214         unsigned long cr0;
215
216         asm volatile("mov %%cr0,%0" : "=r" (cr0), "=m" (__force_order));
217         return cr0;
218 }
219
220 static inline void write_cr0(unsigned long val)
221 {
222         asm volatile("mov %0,%%cr0" : : "r" (val), "m" (__force_order));
223 }
224
225 static inline unsigned long read_cr2(void)
226 {
227         unsigned long cr2;
228
229         asm volatile("mov %%cr2,%0" : "=r" (cr2), "=m" (__force_order));
230         return cr2;
231 }
232
233 static inline unsigned long read_cr3(void)
234 {
235         unsigned long cr3;
236
237         asm volatile("mov %%cr3,%0" : "=r" (cr3), "=m" (__force_order));
238         return cr3;
239 }
240
241 static inline void write_cr3(unsigned long val)
242 {
243         asm volatile("mov %0,%%cr3" : : "r" (val), "m" (__force_order));
244 }
245
246 static inline unsigned long read_cr4(void)
247 {
248         unsigned long cr4;
249
250         asm volatile("mov %%cr4,%0" : "=r" (cr4), "=m" (__force_order));
251         return cr4;
252 }
253
254 static inline void write_cr4(unsigned long val)
255 {
256         asm volatile("mov %0,%%cr4" : : "r" (val), "m" (__force_order));
257 }
258
259 static inline unsigned long read_msr(unsigned int msr)
260 {
261         u32 low, high;
262
263         asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
264         return low | ((unsigned long)high << 32);
265 }
266
267 static inline void write_msr(unsigned int msr, unsigned long val)
268 {
269         asm volatile("wrmsr"
270                 : /* no output */
271                 : "c" (msr), "a" (val), "d" (val >> 32)
272                 : "memory");
273 }
274
275 static inline void set_rdmsr_value(struct registers *regs, unsigned long val)
276 {
277         regs->rax = (u32)val;
278         regs->rdx = val >> 32;
279 }
280
281 static inline unsigned long get_wrmsr_value(struct registers *regs)
282 {
283         return (u32)regs->rax | (regs->rdx << 32);
284 }
285
286 static inline void read_gdtr(struct desc_table_reg *val)
287 {
288         asm volatile("sgdtq %0" : "=m" (*val));
289 }
290
291 static inline void write_gdtr(struct desc_table_reg *val)
292 {
293         asm volatile("lgdtq %0" : : "m" (*val));
294 }
295
296 static inline void read_idtr(struct desc_table_reg *val)
297 {
298         asm volatile("sidtq %0" : "=m" (*val));
299 }
300
301 static inline void write_idtr(struct desc_table_reg *val)
302 {
303         asm volatile("lidtq %0" : : "m" (*val));
304 }
305
306 /**
307  * Enable or disable interrupts delivery to the local CPU when in host mode.
308  *
309  * In some cases (AMD) changing IF isn't enough, so these are implemented on
310  * per-vendor basis.
311  * @{
312  */
313 void enable_irq(void);
314
315 void disable_irq(void);
316 /** @} */
317
318 /** @} */
319 #endif /* !__ASSEMBLY__ */
320
321 #endif /* !_JAILHOUSE_ASM_PROCESSOR_H */