ia64/xen-unstable

view xen/arch/x86/hvm/svm/vmcb.c @ 14635:5c52e5ca8459

hvm: Clean up handling of exception intercepts.
Only intercept #DB/#BP if a debugger is attached.
Signed-off-by: Keir Fraser <keir@xensource.com>
author Keir Fraser <keir@xensource.com>
date Wed Mar 28 18:47:17 2007 +0100 (2007-03-28)
parents c489a25c9f9a
children e9986e48ebe1
line source
1 /*
2 * vmcb.c: VMCB management
3 * Copyright (c) 2005, AMD Corporation.
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 <xen/event.h>
36 #include <xen/kernel.h>
37 #include <xen/domain_page.h>
38 #include <xen/keyhandler.h>
40 extern int svm_dbg_on;
42 #define GUEST_SEGMENT_LIMIT 0xffffffff
44 #define IOPM_SIZE (12 * 1024)
45 #define MSRPM_SIZE (8 * 1024)
47 struct vmcb_struct *alloc_vmcb(void)
48 {
49 struct vmcb_struct *vmcb;
51 vmcb = alloc_xenheap_page();
52 if ( vmcb == NULL )
53 {
54 printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n");
55 return NULL;
56 }
58 memset(vmcb, 0, PAGE_SIZE);
59 return vmcb;
60 }
62 void free_vmcb(struct vmcb_struct *vmcb)
63 {
64 free_xenheap_page(vmcb);
65 }
67 struct host_save_area *alloc_host_save_area(void)
68 {
69 struct host_save_area *hsa;
71 hsa = alloc_xenheap_page();
72 if ( hsa == NULL )
73 {
74 printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n");
75 return NULL;
76 }
78 memset(hsa, 0, PAGE_SIZE);
79 return hsa;
80 }
82 static int construct_vmcb(struct vcpu *v)
83 {
84 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
85 struct vmcb_struct *vmcb = arch_svm->vmcb;
86 svm_segment_attributes_t attrib;
88 /* Always flush the TLB on VMRUN. All guests share a single ASID (1). */
89 vmcb->tlb_control = 1;
90 vmcb->guest_asid = 1;
92 /* SVM intercepts. */
93 vmcb->general1_intercepts =
94 GENERAL1_INTERCEPT_INTR | GENERAL1_INTERCEPT_NMI |
95 GENERAL1_INTERCEPT_SMI | GENERAL1_INTERCEPT_INIT |
96 GENERAL1_INTERCEPT_CPUID | GENERAL1_INTERCEPT_INVD |
97 GENERAL1_INTERCEPT_HLT | GENERAL1_INTERCEPT_INVLPG |
98 GENERAL1_INTERCEPT_INVLPGA | GENERAL1_INTERCEPT_IOIO_PROT |
99 GENERAL1_INTERCEPT_MSR_PROT | GENERAL1_INTERCEPT_SHUTDOWN_EVT;
100 vmcb->general2_intercepts =
101 GENERAL2_INTERCEPT_VMRUN | GENERAL2_INTERCEPT_VMMCALL |
102 GENERAL2_INTERCEPT_VMLOAD | GENERAL2_INTERCEPT_VMSAVE |
103 GENERAL2_INTERCEPT_STGI | GENERAL2_INTERCEPT_CLGI |
104 GENERAL2_INTERCEPT_SKINIT | GENERAL2_INTERCEPT_RDTSCP;
106 /* Intercept all debug-register writes. */
107 vmcb->dr_intercepts = DR_INTERCEPT_ALL_WRITES;
109 /* Intercept all control-register accesses, except to CR2. */
110 vmcb->cr_intercepts = ~(CR_INTERCEPT_CR2_READ | CR_INTERCEPT_CR2_WRITE);
112 /* I/O and MSR permission bitmaps. */
113 arch_svm->msrpm = alloc_xenheap_pages(get_order_from_bytes(MSRPM_SIZE));
114 if ( arch_svm->msrpm == NULL )
115 return -ENOMEM;
116 memset(arch_svm->msrpm, 0xff, MSRPM_SIZE);
117 vmcb->msrpm_base_pa = (u64)virt_to_maddr(arch_svm->msrpm);
118 vmcb->iopm_base_pa = (u64)virt_to_maddr(hvm_io_bitmap);
120 /* Virtualise EFLAGS.IF and LAPIC TPR (CR8). */
121 vmcb->vintr.fields.intr_masking = 1;
123 /* Initialise event injection to no-op. */
124 vmcb->eventinj.bytes = 0;
126 /* TSC. */
127 vmcb->tsc_offset = 0;
129 /* Guest EFER: *must* contain SVME or VMRUN will fail. */
130 vmcb->efer = EFER_SVME;
132 /* Guest segment limits. */
133 vmcb->cs.limit = GUEST_SEGMENT_LIMIT;
134 vmcb->es.limit = GUEST_SEGMENT_LIMIT;
135 vmcb->ss.limit = GUEST_SEGMENT_LIMIT;
136 vmcb->ds.limit = GUEST_SEGMENT_LIMIT;
137 vmcb->fs.limit = GUEST_SEGMENT_LIMIT;
138 vmcb->gs.limit = GUEST_SEGMENT_LIMIT;
140 /* Guest segment bases. */
141 vmcb->cs.base = 0;
142 vmcb->es.base = 0;
143 vmcb->ss.base = 0;
144 vmcb->ds.base = 0;
145 vmcb->fs.base = 0;
146 vmcb->gs.base = 0;
148 /* Guest segment AR bytes. */
149 attrib.bytes = 0;
150 attrib.fields.type = 0x3; /* type = 3 */
151 attrib.fields.s = 1; /* code or data, i.e. not system */
152 attrib.fields.dpl = 0; /* DPL = 0 */
153 attrib.fields.p = 1; /* segment present */
154 attrib.fields.db = 1; /* 32-bit */
155 attrib.fields.g = 1; /* 4K pages in limit */
156 vmcb->es.attr = attrib;
157 vmcb->ss.attr = attrib;
158 vmcb->ds.attr = attrib;
159 vmcb->fs.attr = attrib;
160 vmcb->gs.attr = attrib;
161 attrib.fields.type = 0xb; /* type=0xb -> executable/readable, accessed */
162 vmcb->cs.attr = attrib;
164 /* Guest IDT. */
165 vmcb->idtr.base = 0;
166 vmcb->idtr.limit = 0;
168 /* Guest GDT. */
169 vmcb->gdtr.base = 0;
170 vmcb->gdtr.limit = 0;
172 /* Guest LDT. */
173 vmcb->ldtr.sel = 0;
174 vmcb->ldtr.base = 0;
175 vmcb->ldtr.limit = 0;
176 vmcb->ldtr.attr.bytes = 0;
178 /* Guest TSS. */
179 attrib.fields.type = 0xb; /* 32-bit TSS (busy) */
180 vmcb->tr.attr = attrib;
181 vmcb->tr.base = 0;
182 vmcb->tr.limit = 0xff;
184 /* Guest CR0. */
185 vmcb->cr0 = read_cr0();
186 arch_svm->cpu_shadow_cr0 = vmcb->cr0 & ~(X86_CR0_PG | X86_CR0_TS);
187 vmcb->cr0 |= X86_CR0_WP;
189 /* Guest CR4. */
190 arch_svm->cpu_shadow_cr4 =
191 read_cr4() & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE);
192 vmcb->cr4 = arch_svm->cpu_shadow_cr4 | SVM_CR4_HOST_MASK;
194 paging_update_paging_modes(v);
195 vmcb->cr3 = v->arch.hvm_vcpu.hw_cr3;
197 if ( paging_mode_hap(v->domain) )
198 {
199 vmcb->cr0 = arch_svm->cpu_shadow_cr0;
200 vmcb->np_enable = 1; /* enable nested paging */
201 vmcb->g_pat = 0x0007040600070406ULL; /* guest PAT */
202 vmcb->h_cr3 = pagetable_get_paddr(v->domain->arch.phys_table);
203 vmcb->cr4 = arch_svm->cpu_shadow_cr4 = 0;
204 }
205 else
206 {
207 vmcb->exception_intercepts = 1U << TRAP_page_fault;
208 }
210 return 0;
211 }
213 int svm_create_vmcb(struct vcpu *v)
214 {
215 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
216 int rc;
218 if ( (arch_svm->vmcb == NULL) &&
219 (arch_svm->vmcb = alloc_vmcb()) == NULL )
220 {
221 printk("Failed to create a new VMCB\n");
222 return -ENOMEM;
223 }
225 if ( (rc = construct_vmcb(v)) != 0 )
226 {
227 free_vmcb(arch_svm->vmcb);
228 arch_svm->vmcb = NULL;
229 return rc;
230 }
232 arch_svm->vmcb_pa = virt_to_maddr(arch_svm->vmcb);
234 return 0;
235 }
237 void svm_destroy_vmcb(struct vcpu *v)
238 {
239 struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
241 if ( arch_svm->vmcb != NULL )
242 free_vmcb(arch_svm->vmcb);
244 if ( arch_svm->msrpm != NULL )
245 {
246 free_xenheap_pages(
247 arch_svm->msrpm, get_order_from_bytes(MSRPM_SIZE));
248 arch_svm->msrpm = NULL;
249 }
251 arch_svm->vmcb = NULL;
252 }
254 static void svm_dump_sel(char *name, svm_segment_register_t *s)
255 {
256 printk("%s: sel=0x%04x, attr=0x%04x, limit=0x%08x, base=0x%016llx\n",
257 name, s->sel, s->attr.bytes, s->limit,
258 (unsigned long long)s->base);
259 }
261 void svm_dump_vmcb(const char *from, struct vmcb_struct *vmcb)
262 {
263 printk("Dumping guest's current state at %s...\n", from);
264 printk("Size of VMCB = %d, address = %p\n",
265 (int) sizeof(struct vmcb_struct), vmcb);
267 printk("cr_intercepts = 0x%08x dr_intercepts = 0x%08x "
268 "exception_intercepts = 0x%08x\n",
269 vmcb->cr_intercepts, vmcb->dr_intercepts,
270 vmcb->exception_intercepts);
271 printk("general1_intercepts = 0x%08x general2_intercepts = 0x%08x\n",
272 vmcb->general1_intercepts, vmcb->general2_intercepts);
273 printk("iopm_base_pa = %016llx msrpm_base_pa = 0x%016llx tsc_offset = "
274 "0x%016llx\n",
275 (unsigned long long) vmcb->iopm_base_pa,
276 (unsigned long long) vmcb->msrpm_base_pa,
277 (unsigned long long) vmcb->tsc_offset);
278 printk("tlb_control = 0x%08x vintr = 0x%016llx interrupt_shadow = "
279 "0x%016llx\n", vmcb->tlb_control,
280 (unsigned long long) vmcb->vintr.bytes,
281 (unsigned long long) vmcb->interrupt_shadow);
282 printk("exitcode = 0x%016llx exitintinfo = 0x%016llx\n",
283 (unsigned long long) vmcb->exitcode,
284 (unsigned long long) vmcb->exitintinfo.bytes);
285 printk("exitinfo1 = 0x%016llx exitinfo2 = 0x%016llx \n",
286 (unsigned long long) vmcb->exitinfo1,
287 (unsigned long long) vmcb->exitinfo2);
288 printk("np_enable = 0x%016llx guest_asid = 0x%03x\n",
289 (unsigned long long) vmcb->np_enable, vmcb->guest_asid);
290 printk("cpl = %d efer = 0x%016llx star = 0x%016llx lstar = 0x%016llx\n",
291 vmcb->cpl, (unsigned long long) vmcb->efer,
292 (unsigned long long) vmcb->star, (unsigned long long) vmcb->lstar);
293 printk("CR0 = 0x%016llx CR2 = 0x%016llx\n",
294 (unsigned long long) vmcb->cr0, (unsigned long long) vmcb->cr2);
295 printk("CR3 = 0x%016llx CR4 = 0x%016llx\n",
296 (unsigned long long) vmcb->cr3, (unsigned long long) vmcb->cr4);
297 printk("RSP = 0x%016llx RIP = 0x%016llx\n",
298 (unsigned long long) vmcb->rsp, (unsigned long long) vmcb->rip);
299 printk("RAX = 0x%016llx RFLAGS=0x%016llx\n",
300 (unsigned long long) vmcb->rax, (unsigned long long) vmcb->rflags);
301 printk("DR6 = 0x%016llx, DR7 = 0x%016llx\n",
302 (unsigned long long) vmcb->dr6, (unsigned long long) vmcb->dr7);
303 printk("CSTAR = 0x%016llx SFMask = 0x%016llx\n",
304 (unsigned long long) vmcb->cstar,
305 (unsigned long long) vmcb->sfmask);
306 printk("KernGSBase = 0x%016llx PAT = 0x%016llx \n",
307 (unsigned long long) vmcb->kerngsbase,
308 (unsigned long long) vmcb->g_pat);
309 printk("H_CR3 = 0x%016llx\n", (unsigned long long)vmcb->h_cr3);
311 /* print out all the selectors */
312 svm_dump_sel("CS", &vmcb->cs);
313 svm_dump_sel("DS", &vmcb->ds);
314 svm_dump_sel("SS", &vmcb->ss);
315 svm_dump_sel("ES", &vmcb->es);
316 svm_dump_sel("FS", &vmcb->fs);
317 svm_dump_sel("GS", &vmcb->gs);
318 svm_dump_sel("GDTR", &vmcb->gdtr);
319 svm_dump_sel("LDTR", &vmcb->ldtr);
320 svm_dump_sel("IDTR", &vmcb->idtr);
321 svm_dump_sel("TR", &vmcb->tr);
322 }
324 static void vmcb_dump(unsigned char ch)
325 {
326 struct domain *d;
327 struct vcpu *v;
329 printk("*********** VMCB Areas **************\n");
331 rcu_read_lock(&domlist_read_lock);
333 for_each_domain ( d )
334 {
335 if ( !is_hvm_domain(d) )
336 continue;
337 printk("\n>>> Domain %d <<<\n", d->domain_id);
338 for_each_vcpu ( d, v )
339 {
340 printk("\tVCPU %d\n", v->vcpu_id);
341 svm_dump_vmcb("key_handler", v->arch.hvm_svm.vmcb);
342 }
343 }
345 rcu_read_unlock(&domlist_read_lock);
347 printk("**************************************\n");
348 }
350 void setup_vmcb_dump(void)
351 {
352 register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs");
353 }
355 /*
356 * Local variables:
357 * mode: C
358 * c-set-style: "BSD"
359 * c-basic-offset: 4
360 * tab-width: 4
361 * indent-tabs-mode: nil
362 * End:
363 */