ia64/xen-unstable

view xen/arch/x86/hvm/svm/vmcb.c @ 15369:1feb91894e11

[HVM] HAP tidying.
Tighten up locking discipline, dead/useless code and unnecessary VMEXITS.
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Fri Jun 15 16:51:08 2007 +0100 (2007-06-15)
parents d4a0706d6747
children 005dd6b1cf8e
line source
1 /*
2 * vmcb.c: VMCB management
3 * Copyright (c) 2005-2007, Advanced Micro Devices, Inc.
4 * Copyright (c) 2004, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 *
19 */
21 #include <xen/config.h>
22 #include <xen/init.h>
23 #include <xen/mm.h>
24 #include <xen/lib.h>
25 #include <xen/errno.h>
26 #include <asm/cpufeature.h>
27 #include <asm/processor.h>
28 #include <asm/msr.h>
29 #include <asm/paging.h>
30 #include <asm/hvm/hvm.h>
31 #include <asm/hvm/io.h>
32 #include <asm/hvm/support.h>
33 #include <asm/hvm/svm/svm.h>
34 #include <asm/hvm/svm/intr.h>
35 #include <asm/hvm/svm/asid.h>
36 #include <xen/event.h>
37 #include <xen/kernel.h>
38 #include <xen/domain_page.h>
39 #include <xen/keyhandler.h>
41 extern int svm_dbg_on;
43 #define GUEST_SEGMENT_LIMIT 0xffffffff
45 #define IOPM_SIZE (12 * 1024)
46 #define MSRPM_SIZE (8 * 1024)
48 struct vmcb_struct *alloc_vmcb(void)
49 {
50 struct vmcb_struct *vmcb;
52 vmcb = alloc_xenheap_page();
53 if ( vmcb == NULL )
54 {
55 printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n");
56 return NULL;
57 }
59 memset(vmcb, 0, PAGE_SIZE);
60 return vmcb;
61 }
63 void free_vmcb(struct vmcb_struct *vmcb)
64 {
65 free_xenheap_page(vmcb);
66 }
68 struct host_save_area *alloc_host_save_area(void)
69 {
70 struct host_save_area *hsa;
72 hsa = alloc_xenheap_page();
73 if ( hsa == NULL )
74 {
75 printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n");
76 return NULL;
77 }
79 memset(hsa, 0, PAGE_SIZE);
80 return hsa;
81 }
83 static void disable_intercept_for_msr(char *msr_bitmap, u32 msr)
84 {
85 /*
86 * See AMD64 Programmers Manual, Vol 2, Section 15.10 (MSR-Bitmap Address).
87 */
88 if ( msr <= 0x1fff )
89 {
90 __clear_bit(msr*2, msr_bitmap + 0x000);
91 __clear_bit(msr*2+1, msr_bitmap + 0x000);
92 }
93 else if ( (msr >= 0xc0000000) && (msr <= 0xc0001fff) )
94 {
95 msr &= 0x1fff;
96 __clear_bit(msr*2, msr_bitmap + 0x800);
97 __clear_bit(msr*2+1, msr_bitmap + 0x800);
98 }
99 else if ( (msr >= 0xc001000) && (msr <= 0xc0011fff) )
100 {
101 msr &= 0x1fff;
102 __clear_bit(msr*2, msr_bitmap + 0x1000);
103 __clear_bit(msr*2+1, msr_bitmap + 0x1000);
104 }
105 }
107 static int construct_vmcb(struct vcpu *v)
108 {
109 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
110 struct vmcb_struct *vmcb = arch_svm->vmcb;
111 svm_segment_attributes_t attrib;
113 /* TLB control, and ASID assigment. */
114 svm_asid_init_vcpu (v);
116 vmcb->general1_intercepts =
117 GENERAL1_INTERCEPT_INTR | GENERAL1_INTERCEPT_NMI |
118 GENERAL1_INTERCEPT_SMI | GENERAL1_INTERCEPT_INIT |
119 GENERAL1_INTERCEPT_CPUID | GENERAL1_INTERCEPT_INVD |
120 GENERAL1_INTERCEPT_HLT | GENERAL1_INTERCEPT_INVLPG |
121 GENERAL1_INTERCEPT_INVLPGA | GENERAL1_INTERCEPT_IOIO_PROT |
122 GENERAL1_INTERCEPT_MSR_PROT | GENERAL1_INTERCEPT_SHUTDOWN_EVT;
123 vmcb->general2_intercepts =
124 GENERAL2_INTERCEPT_VMRUN | GENERAL2_INTERCEPT_VMMCALL |
125 GENERAL2_INTERCEPT_VMLOAD | GENERAL2_INTERCEPT_VMSAVE |
126 GENERAL2_INTERCEPT_STGI | GENERAL2_INTERCEPT_CLGI |
127 GENERAL2_INTERCEPT_SKINIT | GENERAL2_INTERCEPT_RDTSCP;
129 /* Intercept all debug-register writes. */
130 vmcb->dr_intercepts = DR_INTERCEPT_ALL_WRITES;
132 /* Intercept all control-register accesses, except to CR2. */
133 vmcb->cr_intercepts = ~(CR_INTERCEPT_CR2_READ | CR_INTERCEPT_CR2_WRITE);
135 /* I/O and MSR permission bitmaps. */
136 arch_svm->msrpm = alloc_xenheap_pages(get_order_from_bytes(MSRPM_SIZE));
137 if ( arch_svm->msrpm == NULL )
138 return -ENOMEM;
139 memset(arch_svm->msrpm, 0xff, MSRPM_SIZE);
141 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_FS_BASE);
142 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_GS_BASE);
143 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_SHADOW_GS_BASE);
144 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_CSTAR);
145 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_LSTAR);
146 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_STAR);
147 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_SYSCALL_MASK);
148 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_IA32_SYSENTER_CS);
149 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_IA32_SYSENTER_ESP);
150 disable_intercept_for_msr((char *)arch_svm->msrpm, MSR_IA32_SYSENTER_EIP);
152 vmcb->msrpm_base_pa = (u64)virt_to_maddr(arch_svm->msrpm);
153 vmcb->iopm_base_pa = (u64)virt_to_maddr(hvm_io_bitmap);
155 /* Virtualise EFLAGS.IF and LAPIC TPR (CR8). */
156 vmcb->vintr.fields.intr_masking = 1;
158 /* Initialise event injection to no-op. */
159 vmcb->eventinj.bytes = 0;
161 /* TSC. */
162 vmcb->tsc_offset = 0;
164 /* Guest EFER: *must* contain SVME or VMRUN will fail. */
165 vmcb->efer = EFER_SVME;
167 /* Guest segment limits. */
168 vmcb->cs.limit = GUEST_SEGMENT_LIMIT;
169 vmcb->es.limit = GUEST_SEGMENT_LIMIT;
170 vmcb->ss.limit = GUEST_SEGMENT_LIMIT;
171 vmcb->ds.limit = GUEST_SEGMENT_LIMIT;
172 vmcb->fs.limit = GUEST_SEGMENT_LIMIT;
173 vmcb->gs.limit = GUEST_SEGMENT_LIMIT;
175 /* Guest segment bases. */
176 vmcb->cs.base = 0;
177 vmcb->es.base = 0;
178 vmcb->ss.base = 0;
179 vmcb->ds.base = 0;
180 vmcb->fs.base = 0;
181 vmcb->gs.base = 0;
183 /* Guest segment AR bytes. */
184 attrib.bytes = 0;
185 attrib.fields.type = 0x3; /* type = 3 */
186 attrib.fields.s = 1; /* code or data, i.e. not system */
187 attrib.fields.dpl = 0; /* DPL = 0 */
188 attrib.fields.p = 1; /* segment present */
189 attrib.fields.db = 1; /* 32-bit */
190 attrib.fields.g = 1; /* 4K pages in limit */
191 vmcb->es.attr = attrib;
192 vmcb->ss.attr = attrib;
193 vmcb->ds.attr = attrib;
194 vmcb->fs.attr = attrib;
195 vmcb->gs.attr = attrib;
196 attrib.fields.type = 0xb; /* type=0xb -> executable/readable, accessed */
197 vmcb->cs.attr = attrib;
199 /* Guest IDT. */
200 vmcb->idtr.base = 0;
201 vmcb->idtr.limit = 0;
203 /* Guest GDT. */
204 vmcb->gdtr.base = 0;
205 vmcb->gdtr.limit = 0;
207 /* Guest LDT. */
208 vmcb->ldtr.sel = 0;
209 vmcb->ldtr.base = 0;
210 vmcb->ldtr.limit = 0;
211 vmcb->ldtr.attr.bytes = 0;
213 /* Guest TSS. */
214 attrib.fields.type = 0xb; /* 32-bit TSS (busy) */
215 vmcb->tr.attr = attrib;
216 vmcb->tr.base = 0;
217 vmcb->tr.limit = 0xff;
219 /* Guest CR0. */
220 vmcb->cr0 = read_cr0();
221 arch_svm->cpu_shadow_cr0 = vmcb->cr0 & ~(X86_CR0_PG | X86_CR0_TS);
222 vmcb->cr0 |= X86_CR0_WP;
224 /* Guest CR4. */
225 arch_svm->cpu_shadow_cr4 =
226 read_cr4() & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE);
227 vmcb->cr4 = arch_svm->cpu_shadow_cr4 | SVM_CR4_HOST_MASK;
229 paging_update_paging_modes(v);
230 vmcb->cr3 = v->arch.hvm_vcpu.hw_cr3;
232 if ( paging_mode_hap(v->domain) )
233 {
234 vmcb->cr0 = arch_svm->cpu_shadow_cr0;
235 vmcb->np_enable = 1; /* enable nested paging */
236 vmcb->g_pat = 0x0007040600070406ULL; /* guest PAT */
237 vmcb->h_cr3 = pagetable_get_paddr(v->domain->arch.phys_table);
238 vmcb->cr4 = arch_svm->cpu_shadow_cr4 = 0;
240 /* No point in intercepting CR0/3/4 reads, because the hardware
241 * will return the guest versions anyway. */
242 vmcb->cr_intercepts &= ~(CR_INTERCEPT_CR0_READ
243 |CR_INTERCEPT_CR3_READ
244 |CR_INTERCEPT_CR4_READ);
246 /* No point in intercepting INVLPG if we don't have shadow pagetables
247 * that need to be fixed up. */
248 vmcb->general1_intercepts &= ~GENERAL1_INTERCEPT_INVLPG;
249 }
250 else
251 {
252 vmcb->exception_intercepts = 1U << TRAP_page_fault;
253 }
255 return 0;
256 }
258 int svm_create_vmcb(struct vcpu *v)
259 {
260 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
261 int rc;
263 if ( (arch_svm->vmcb == NULL) &&
264 (arch_svm->vmcb = alloc_vmcb()) == NULL )
265 {
266 printk("Failed to create a new VMCB\n");
267 return -ENOMEM;
268 }
270 if ( (rc = construct_vmcb(v)) != 0 )
271 {
272 free_vmcb(arch_svm->vmcb);
273 arch_svm->vmcb = NULL;
274 return rc;
275 }
277 arch_svm->vmcb_pa = virt_to_maddr(arch_svm->vmcb);
279 return 0;
280 }
282 void svm_destroy_vmcb(struct vcpu *v)
283 {
284 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
286 if ( arch_svm->vmcb != NULL )
287 free_vmcb(arch_svm->vmcb);
289 if ( arch_svm->msrpm != NULL )
290 {
291 free_xenheap_pages(
292 arch_svm->msrpm, get_order_from_bytes(MSRPM_SIZE));
293 arch_svm->msrpm = NULL;
294 }
296 arch_svm->vmcb = NULL;
297 }
299 static void svm_dump_sel(char *name, svm_segment_register_t *s)
300 {
301 printk("%s: sel=0x%04x, attr=0x%04x, limit=0x%08x, base=0x%016llx\n",
302 name, s->sel, s->attr.bytes, s->limit,
303 (unsigned long long)s->base);
304 }
306 void svm_dump_vmcb(const char *from, struct vmcb_struct *vmcb)
307 {
308 printk("Dumping guest's current state at %s...\n", from);
309 printk("Size of VMCB = %d, address = %p\n",
310 (int) sizeof(struct vmcb_struct), vmcb);
312 printk("cr_intercepts = 0x%08x dr_intercepts = 0x%08x "
313 "exception_intercepts = 0x%08x\n",
314 vmcb->cr_intercepts, vmcb->dr_intercepts,
315 vmcb->exception_intercepts);
316 printk("general1_intercepts = 0x%08x general2_intercepts = 0x%08x\n",
317 vmcb->general1_intercepts, vmcb->general2_intercepts);
318 printk("iopm_base_pa = %016llx msrpm_base_pa = 0x%016llx tsc_offset = "
319 "0x%016llx\n",
320 (unsigned long long) vmcb->iopm_base_pa,
321 (unsigned long long) vmcb->msrpm_base_pa,
322 (unsigned long long) vmcb->tsc_offset);
323 printk("tlb_control = 0x%08x vintr = 0x%016llx interrupt_shadow = "
324 "0x%016llx\n", vmcb->tlb_control,
325 (unsigned long long) vmcb->vintr.bytes,
326 (unsigned long long) vmcb->interrupt_shadow);
327 printk("exitcode = 0x%016llx exitintinfo = 0x%016llx\n",
328 (unsigned long long) vmcb->exitcode,
329 (unsigned long long) vmcb->exitintinfo.bytes);
330 printk("exitinfo1 = 0x%016llx exitinfo2 = 0x%016llx \n",
331 (unsigned long long) vmcb->exitinfo1,
332 (unsigned long long) vmcb->exitinfo2);
333 printk("np_enable = 0x%016llx guest_asid = 0x%03x\n",
334 (unsigned long long) vmcb->np_enable, vmcb->guest_asid);
335 printk("cpl = %d efer = 0x%016llx star = 0x%016llx lstar = 0x%016llx\n",
336 vmcb->cpl, (unsigned long long) vmcb->efer,
337 (unsigned long long) vmcb->star, (unsigned long long) vmcb->lstar);
338 printk("CR0 = 0x%016llx CR2 = 0x%016llx\n",
339 (unsigned long long) vmcb->cr0, (unsigned long long) vmcb->cr2);
340 printk("CR3 = 0x%016llx CR4 = 0x%016llx\n",
341 (unsigned long long) vmcb->cr3, (unsigned long long) vmcb->cr4);
342 printk("RSP = 0x%016llx RIP = 0x%016llx\n",
343 (unsigned long long) vmcb->rsp, (unsigned long long) vmcb->rip);
344 printk("RAX = 0x%016llx RFLAGS=0x%016llx\n",
345 (unsigned long long) vmcb->rax, (unsigned long long) vmcb->rflags);
346 printk("DR6 = 0x%016llx, DR7 = 0x%016llx\n",
347 (unsigned long long) vmcb->dr6, (unsigned long long) vmcb->dr7);
348 printk("CSTAR = 0x%016llx SFMask = 0x%016llx\n",
349 (unsigned long long) vmcb->cstar,
350 (unsigned long long) vmcb->sfmask);
351 printk("KernGSBase = 0x%016llx PAT = 0x%016llx \n",
352 (unsigned long long) vmcb->kerngsbase,
353 (unsigned long long) vmcb->g_pat);
354 printk("H_CR3 = 0x%016llx\n", (unsigned long long)vmcb->h_cr3);
356 /* print out all the selectors */
357 svm_dump_sel("CS", &vmcb->cs);
358 svm_dump_sel("DS", &vmcb->ds);
359 svm_dump_sel("SS", &vmcb->ss);
360 svm_dump_sel("ES", &vmcb->es);
361 svm_dump_sel("FS", &vmcb->fs);
362 svm_dump_sel("GS", &vmcb->gs);
363 svm_dump_sel("GDTR", &vmcb->gdtr);
364 svm_dump_sel("LDTR", &vmcb->ldtr);
365 svm_dump_sel("IDTR", &vmcb->idtr);
366 svm_dump_sel("TR", &vmcb->tr);
367 }
369 static void vmcb_dump(unsigned char ch)
370 {
371 struct domain *d;
372 struct vcpu *v;
374 printk("*********** VMCB Areas **************\n");
376 rcu_read_lock(&domlist_read_lock);
378 for_each_domain ( d )
379 {
380 if ( !is_hvm_domain(d) )
381 continue;
382 printk("\n>>> Domain %d <<<\n", d->domain_id);
383 for_each_vcpu ( d, v )
384 {
385 printk("\tVCPU %d\n", v->vcpu_id);
386 svm_dump_vmcb("key_handler", v->arch.hvm_svm.vmcb);
387 }
388 }
390 rcu_read_unlock(&domlist_read_lock);
392 printk("**************************************\n");
393 }
395 void setup_vmcb_dump(void)
396 {
397 register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs");
398 }
400 /*
401 * Local variables:
402 * mode: C
403 * c-set-style: "BSD"
404 * c-basic-offset: 4
405 * tab-width: 4
406 * indent-tabs-mode: nil
407 * End:
408 */