]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/x86/include/asm/processor.h
Add support for xsetbv guest exists
[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  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #ifndef _JAILHOUSE_ASM_PROCESSOR_H
14 #define _JAILHOUSE_ASM_PROCESSOR_H
15
16 #include <asm/types.h>
17
18 #define X86_FEATURE_VMX                                 (1 << 5)
19
20 #define X86_CR0_PE                                      0x00000001
21 #define X86_CR0_ET                                      0x00000010
22 #define X86_CR0_NW                                      0x20000000
23 #define X86_CR0_CD                                      0x40000000
24 #define X86_CR0_PG                                      0x80000000
25
26 #define X86_CR4_PGE                                     0x00000080
27 #define X86_CR4_VMXE                                    0x00002000
28
29 #define X86_XCR0_FP                                     0x00000001
30
31 #define MSR_IA32_APICBASE                               0x0000001b
32 #define MSR_IA32_FEATURE_CONTROL                        0x0000003a
33 #define MSR_IA32_SYSENTER_CS                            0x00000174
34 #define MSR_IA32_SYSENTER_ESP                           0x00000175
35 #define MSR_IA32_SYSENTER_EIP                           0x00000176
36 #define MSR_IA32_VMX_BASIC                              0x00000480
37 #define MSR_IA32_VMX_PINBASED_CTLS                      0x00000481
38 #define MSR_IA32_VMX_PROCBASED_CTLS                     0x00000482
39 #define MSR_IA32_VMX_EXIT_CTLS                          0x00000483
40 #define MSR_IA32_VMX_ENTRY_CTLS                         0x00000484
41 #define MSR_IA32_VMX_CR0_FIXED0                         0x00000486
42 #define MSR_IA32_VMX_CR0_FIXED1                         0x00000487
43 #define MSR_IA32_VMX_CR4_FIXED0                         0x00000488
44 #define MSR_IA32_VMX_CR4_FIXED1                         0x00000489
45 #define MSR_IA32_VMX_PROCBASED_CTLS2                    0x0000048b
46 #define MSR_IA32_VMX_EPT_VPID_CAP                       0x0000048c
47 #define MSR_IA32_VMX_TRUE_PINBASED_CTLS                 0x0000048d
48 #define MSR_IA32_VMX_TRUE_PROCBASED_CTLS                0x0000048e
49 #define MSR_IA32_VMX_TRUE_EXIT_CTLS                     0x0000048f
50 #define MSR_IA32_VMX_TRUE_ENTRY_CTLS                    0x00000490
51 #define MSR_X2APIC_BASE                                 0x00000800
52 #define MSR_X2APIC_ICR                                  0x00000830
53 #define MSR_X2APIC_SELF_IPI                             0x0000083f
54 #define MSR_X2APIC_END                                  MSR_X2APIC_SELF_IPI
55 #define MSR_EFER                                        0xc0000080
56 #define MSR_FS_BASE                                     0xc0000100
57 #define MSR_GS_BASE                                     0xc0000101
58
59 #define FEATURE_CONTROL_LOCKED                          (1 << 0)
60 #define FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX       (1 << 2)
61
62 #define EFER_LME                                        0x00000100
63 #define EFER_LMA                                        0x00000400
64
65 #define GDT_DESC_NULL                                   0
66 #define GDT_DESC_CODE                                   1
67 #define GDT_DESC_TSS                                    2
68 #define GDT_DESC_TSS_HI                                 3
69 #define NUM_GDT_DESC                                    4
70
71 #define X86_INST_LEN_CPUID                              2
72 #define X86_INST_LEN_RDMSR                              2
73 #define X86_INST_LEN_WRMSR                              2
74 #define X86_INST_LEN_VMCALL                             3
75 #define X86_INST_LEN_MOV_TO_CR                          3
76 #define X86_INST_LEN_XSETBV                             3
77
78 #define X86_OP_REGR_PREFIX                              0x44
79 #define X86_OP_MOV_TO_MEM                               0x89
80 #define X86_OP_MOV_FROM_MEM                             0x8b
81
82 #define NMI_VECTOR                                      2
83
84 #define DESC_PRESENT                                    (1UL << (15 + 32))
85 #define DESC_CODE_DATA                                  (1UL << (12 + 32))
86 #define DESC_PAGE_GRAN                                  (1UL << (23 + 32))
87
88 #ifndef __ASSEMBLY__
89
90 struct registers {
91         unsigned long r15;
92         unsigned long r14;
93         unsigned long r13;
94         unsigned long r12;
95         unsigned long r11;
96         unsigned long r10;
97         unsigned long r9;
98         unsigned long r8;
99         unsigned long rdi;
100         unsigned long rsi;
101         unsigned long rbp;
102         unsigned long unused;
103         unsigned long rbx;
104         unsigned long rdx;
105         unsigned long rcx;
106         unsigned long rax;
107 };
108
109 struct desc_table_reg {
110         u16 limit;
111         u64 base;
112 } __attribute__((packed));
113
114 struct segment {
115         u64 base;
116         u32 limit;
117         u32 access_rights;
118         u16 selector;
119 };
120
121 static unsigned long __force_order;
122
123 static inline void cpu_relax(void)
124 {
125         asm volatile("rep; nop");
126 }
127
128 static inline void memory_barrier(void)
129 {
130         asm volatile("mfence" : : : "memory");
131 }
132
133 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
134                            unsigned int *ecx, unsigned int *edx)
135 {
136         /* ecx is often an input as well as an output. */
137         asm volatile("cpuid"
138             : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
139             : "0" (*eax), "2" (*ecx)
140             : "memory");
141 }
142
143 static inline void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
144                          unsigned int *ecx, unsigned int *edx)
145 {
146         *eax =op;
147         *ecx = 0;
148         __cpuid(eax, ebx, ecx, edx);
149 }
150
151 static inline unsigned int cpuid_eax(unsigned int op)
152 {
153         unsigned int eax, ebx, ecx, edx;
154
155         cpuid(op, &eax, &ebx, &ecx, &edx);
156         return eax;
157 }
158
159 static inline unsigned int cpuid_ecx(unsigned int op)
160 {
161         unsigned int eax, ebx, ecx, edx;
162
163         cpuid(op, &eax, &ebx, &ecx, &edx);
164         return ecx;
165 }
166
167 static inline unsigned long read_cr0(void)
168 {
169         unsigned long cr0;
170
171         asm volatile("mov %%cr0,%0" : "=r" (cr0), "=m" (__force_order));
172         return cr0;
173 }
174
175 static inline void write_cr0(unsigned long val)
176 {
177         asm volatile("mov %0,%%cr0" : : "r" (val), "m" (__force_order));
178 }
179
180 static inline unsigned long read_cr3(void)
181 {
182         unsigned long cr3;
183
184         asm volatile("mov %%cr3,%0" : "=r" (cr3), "=m" (__force_order));
185         return cr3;
186 }
187
188 static inline void write_cr3(unsigned long val)
189 {
190         asm volatile("mov %0,%%cr3" : : "r" (val), "m" (__force_order));
191 }
192
193 static inline unsigned long read_cr4(void)
194 {
195         unsigned long cr4;
196
197         asm volatile("mov %%cr4,%0" : "=r" (cr4), "=m" (__force_order));
198         return cr4;
199 }
200
201 static inline void write_cr4(unsigned long val)
202 {
203         asm volatile("mov %0,%%cr4" : : "r" (val), "m" (__force_order));
204 }
205
206 static inline unsigned long read_msr(unsigned int msr)
207 {
208         u32 low, high;
209
210         asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
211         return low | ((unsigned long)high << 32);
212 }
213
214 static inline void write_msr(unsigned int msr, unsigned long val)
215 {
216         asm volatile("wrmsr"
217                 : /* no output */
218                 : "c" (msr), "a" (val), "d" (val >> 32)
219                 : "memory");
220 }
221
222 static inline void read_gdtr(struct desc_table_reg *val)
223 {
224         asm volatile("sgdtq %0" : "=m" (*val));
225 }
226
227 static inline void write_gdtr(struct desc_table_reg *val)
228 {
229         asm volatile("lgdtq %0" : "=m" (*val));
230 }
231
232 static inline void read_idtr(struct desc_table_reg *val)
233 {
234         asm volatile("sidtq %0" : "=m" (*val));
235 }
236
237 static inline void write_idtr(struct desc_table_reg *val)
238 {
239         asm volatile("lidtq %0" : "=m" (*val));
240 }
241
242 #endif /* !__ASSEMBLY__ */
243
244 #endif /* !_JAILHOUSE_ASM_PROCESSOR_H */