debuggers.hg

view xen/arch/x86/vmx_vmcs.c @ 4671:18a8f5216548

bitkeeper revision 1.1366 (4268c126o36cKcnzrSkVxkbrPsoz1g)

Clean up shadow destruction and fix domain destroy when shadow mode
is disabled.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Apr 22 09:17:26 2005 +0000 (2005-04-22)
parents b1cb9f7f34f9
children 38a02ee9a9c8 65b28c74cec2
line source
1 /*
2 * vmx_vmcs.c: VMCS management
3 * Copyright (c) 2004, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 *
18 */
20 #include <xen/config.h>
21 #include <xen/init.h>
22 #include <xen/mm.h>
23 #include <xen/lib.h>
24 #include <xen/errno.h>
26 #include <asm/cpufeature.h>
27 #include <asm/processor.h>
28 #include <asm/msr.h>
29 #include <asm/vmx.h>
30 #include <xen/event.h>
31 #include <xen/kernel.h>
32 #include <public/io/ioreq.h>
33 #include <asm/domain_page.h>
35 #ifdef CONFIG_VMX
37 struct vmcs_struct *alloc_vmcs(void)
38 {
39 struct vmcs_struct *vmcs;
40 unsigned int cpu_sig = cpuid_eax(0x00000001);
42 vmcs = (struct vmcs_struct *) alloc_xenheap_pages(get_order(vmcs_size));
43 memset((char *) vmcs, 0, vmcs_size); /* don't remove this */
45 vmcs->vmcs_revision_id = (cpu_sig > 0xf41)? 3 : 1;
46 return vmcs;
47 }
49 void free_vmcs(struct vmcs_struct *vmcs)
50 {
51 int order;
53 order = (vmcs_size >> PAGE_SHIFT) - 1;
54 free_xenheap_pages((unsigned long) vmcs, order);
55 }
57 static inline int construct_vmcs_controls(void)
58 {
59 int error = 0;
61 error |= __vmwrite(PIN_BASED_VM_EXEC_CONTROL,
62 MONITOR_PIN_BASED_EXEC_CONTROLS);
64 error |= __vmwrite(CPU_BASED_VM_EXEC_CONTROL,
65 MONITOR_CPU_BASED_EXEC_CONTROLS);
67 error |= __vmwrite(VM_EXIT_CONTROLS, MONITOR_VM_EXIT_CONTROLS);
68 error |= __vmwrite(VM_ENTRY_CONTROLS, MONITOR_VM_ENTRY_CONTROLS);
70 return error;
71 }
73 #define GUEST_SEGMENT_LIMIT 0xffffffff
74 #define HOST_SEGMENT_LIMIT 0xffffffff
76 struct host_execution_env {
77 /* selectors */
78 unsigned short ldtr_selector;
79 unsigned short tr_selector;
80 unsigned short ds_selector;
81 unsigned short cs_selector;
82 /* limits */
83 unsigned short gdtr_limit;
84 unsigned short ldtr_limit;
85 unsigned short idtr_limit;
86 unsigned short tr_limit;
87 /* base */
88 unsigned long gdtr_base;
89 unsigned long ldtr_base;
90 unsigned long idtr_base;
91 unsigned long tr_base;
92 unsigned long ds_base;
93 unsigned long cs_base;
94 /* control registers */
95 unsigned long cr3;
96 unsigned long cr0;
97 unsigned long cr4;
98 unsigned long dr7;
99 };
101 #define round_pgdown(_p) ((_p)&PAGE_MASK) /* coped from domain.c */
103 int vmx_setup_platform(struct exec_domain *d, execution_context_t *context)
104 {
105 int i;
106 unsigned int n;
107 unsigned long *p, mpfn, offset, addr;
108 struct e820entry *e820p;
109 unsigned long gpfn = 0;
111 context->ebx = 0; /* Linux expects ebx to be 0 for boot proc */
113 n = context->ecx;
114 if (n > 32) {
115 VMX_DBG_LOG(DBG_LEVEL_1, "Too many e820 entries: %d", n);
116 return -1;
117 }
119 addr = context->edi;
120 offset = (addr & ~PAGE_MASK);
121 addr = round_pgdown(addr);
122 mpfn = phys_to_machine_mapping(addr >> PAGE_SHIFT);
123 p = map_domain_mem(mpfn << PAGE_SHIFT);
125 e820p = (struct e820entry *) ((unsigned long) p + offset);
127 #ifndef NDEBUG
128 print_e820_memory_map(e820p, n);
129 #endif
131 for (i = 0; i < n; i++) {
132 if (e820p[i].type == E820_SHARED_PAGE) {
133 gpfn = (e820p[i].addr >> PAGE_SHIFT);
134 break;
135 }
136 }
138 if (gpfn == 0) {
139 printk("No shared Page ?\n");
140 unmap_domain_mem(p);
141 return -1;
142 }
143 unmap_domain_mem(p);
145 mpfn = phys_to_machine_mapping(gpfn);
146 p = map_domain_mem(mpfn << PAGE_SHIFT);
147 ASSERT(p != NULL);
149 /* Initialise shared page */
150 memset(p, 0, PAGE_SIZE);
152 d->arch.arch_vmx.vmx_platform.shared_page_va = (unsigned long) p;
154 return 0;
155 }
157 void vmx_do_launch(struct exec_domain *ed)
158 {
159 /* Update CR3, GDT, LDT, TR */
160 unsigned int tr, cpu, error = 0;
161 struct host_execution_env host_env;
162 struct Xgt_desc_struct desc;
163 unsigned long pfn = 0;
164 struct pfn_info *page;
165 execution_context_t *ec = get_execution_context();
167 cpu = smp_processor_id();
169 page = (struct pfn_info *) alloc_domheap_page(NULL);
170 pfn = (unsigned long) (page - frame_table);
172 vmx_setup_platform(ed, ec);
174 __asm__ __volatile__ ("sgdt (%0) \n" :: "a"(&desc) : "memory");
175 host_env.gdtr_limit = desc.size;
176 host_env.gdtr_base = desc.address;
178 error |= __vmwrite(HOST_GDTR_BASE, host_env.gdtr_base);
180 error |= __vmwrite(GUEST_LDTR_SELECTOR, 0);
181 error |= __vmwrite(GUEST_LDTR_BASE, 0);
182 error |= __vmwrite(GUEST_LDTR_LIMIT, 0);
184 __asm__ __volatile__ ("str (%0) \n" :: "a"(&tr) : "memory");
185 host_env.tr_selector = tr;
186 host_env.tr_limit = sizeof(struct tss_struct);
187 host_env.tr_base = (unsigned long) &init_tss[cpu];
189 error |= __vmwrite(HOST_TR_SELECTOR, host_env.tr_selector);
190 error |= __vmwrite(HOST_TR_BASE, host_env.tr_base);
191 error |= __vmwrite(GUEST_TR_BASE, 0);
192 error |= __vmwrite(GUEST_TR_LIMIT, 0xff);
194 __vmwrite(GUEST_CR3, pagetable_val(ed->arch.guest_table));
195 __vmwrite(HOST_CR3, pagetable_val(ed->arch.monitor_table));
196 __vmwrite(HOST_ESP, (unsigned long)get_stack_bottom());
198 ed->arch.schedule_tail = arch_vmx_do_resume;
199 }
201 /*
202 * Initially set the same environement as host.
203 */
204 static inline int
205 construct_init_vmcs_guest(execution_context_t *context,
206 full_execution_context_t *full_context,
207 struct host_execution_env *host_env)
208 {
209 int error = 0;
210 union vmcs_arbytes arbytes;
211 unsigned long dr7;
212 unsigned long eflags, shadow_cr;
214 /* MSR */
215 error |= __vmwrite(VM_EXIT_MSR_LOAD_ADDR, 0);
216 error |= __vmwrite(VM_EXIT_MSR_STORE_ADDR, 0);
218 error |= __vmwrite(VM_EXIT_MSR_STORE_COUNT, 0);
219 error |= __vmwrite(VM_EXIT_MSR_LOAD_COUNT, 0);
220 error |= __vmwrite(VM_ENTRY_MSR_LOAD_COUNT, 0);
221 /* interrupt */
222 error |= __vmwrite(VM_ENTRY_INTR_INFO_FIELD, 0);
223 /* mask */
224 error |= __vmwrite(CR0_GUEST_HOST_MASK, 0xffffffff);
225 error |= __vmwrite(CR4_GUEST_HOST_MASK, 0xffffffff);
227 error |= __vmwrite(PAGE_FAULT_ERROR_CODE_MASK, 0);
228 error |= __vmwrite(PAGE_FAULT_ERROR_CODE_MATCH, 0);
230 /* TSC */
231 error |= __vmwrite(TSC_OFFSET, 0);
232 error |= __vmwrite(CR3_TARGET_COUNT, 0);
234 /* Guest Selectors */
235 error |= __vmwrite(GUEST_CS_SELECTOR, context->cs);
236 error |= __vmwrite(GUEST_ES_SELECTOR, context->es);
237 error |= __vmwrite(GUEST_SS_SELECTOR, context->ss);
238 error |= __vmwrite(GUEST_DS_SELECTOR, context->ds);
239 error |= __vmwrite(GUEST_FS_SELECTOR, context->fs);
240 error |= __vmwrite(GUEST_GS_SELECTOR, context->gs);
242 /* Guest segment Limits */
243 error |= __vmwrite(GUEST_CS_LIMIT, GUEST_SEGMENT_LIMIT);
244 error |= __vmwrite(GUEST_ES_LIMIT, GUEST_SEGMENT_LIMIT);
245 error |= __vmwrite(GUEST_SS_LIMIT, GUEST_SEGMENT_LIMIT);
246 error |= __vmwrite(GUEST_DS_LIMIT, GUEST_SEGMENT_LIMIT);
247 error |= __vmwrite(GUEST_FS_LIMIT, GUEST_SEGMENT_LIMIT);
248 error |= __vmwrite(GUEST_GS_LIMIT, GUEST_SEGMENT_LIMIT);
250 error |= __vmwrite(GUEST_IDTR_LIMIT, host_env->idtr_limit);
252 /* AR bytes */
253 arbytes.bytes = 0;
254 arbytes.fields.seg_type = 0x3; /* type = 3 */
255 arbytes.fields.s = 1; /* code or data, i.e. not system */
256 arbytes.fields.dpl = 0; /* DPL = 3 */
257 arbytes.fields.p = 1; /* segment present */
258 arbytes.fields.default_ops_size = 1; /* 32-bit */
259 arbytes.fields.g = 1;
260 arbytes.fields.null_bit = 0; /* not null */
262 error |= __vmwrite(GUEST_ES_AR_BYTES, arbytes.bytes);
263 error |= __vmwrite(GUEST_SS_AR_BYTES, arbytes.bytes);
264 error |= __vmwrite(GUEST_DS_AR_BYTES, arbytes.bytes);
265 error |= __vmwrite(GUEST_FS_AR_BYTES, arbytes.bytes);
266 error |= __vmwrite(GUEST_GS_AR_BYTES, arbytes.bytes);
268 arbytes.fields.seg_type = 0xb; /* type = 0xb */
269 error |= __vmwrite(GUEST_CS_AR_BYTES, arbytes.bytes);
271 error |= __vmwrite(GUEST_GDTR_BASE, context->edx);
272 context->edx = 0;
273 error |= __vmwrite(GUEST_GDTR_LIMIT, context->eax);
274 context->eax = 0;
276 arbytes.fields.s = 0; /* not code or data segement */
277 arbytes.fields.seg_type = 0x2; /* LTD */
278 arbytes.fields.default_ops_size = 0; /* 16-bit */
279 arbytes.fields.g = 0;
280 error |= __vmwrite(GUEST_LDTR_AR_BYTES, arbytes.bytes);
282 arbytes.fields.seg_type = 0xb; /* 32-bit TSS (busy) */
283 error |= __vmwrite(GUEST_TR_AR_BYTES, arbytes.bytes);
285 error |= __vmwrite(GUEST_CR0, host_env->cr0); /* same CR0 */
287 /* Initally PG, PE are not set*/
288 shadow_cr = host_env->cr0;
289 shadow_cr &= ~(X86_CR0_PE | X86_CR0_PG);
290 error |= __vmwrite(CR0_READ_SHADOW, shadow_cr);
291 /* CR3 is set in vmx_final_setup_guest */
292 error |= __vmwrite(GUEST_CR4, host_env->cr4);
293 shadow_cr = host_env->cr4;
294 shadow_cr &= ~(X86_CR4_PGE | X86_CR4_VMXE);
295 error |= __vmwrite(CR4_READ_SHADOW, shadow_cr);
297 error |= __vmwrite(GUEST_ES_BASE, host_env->ds_base);
298 error |= __vmwrite(GUEST_CS_BASE, host_env->cs_base);
299 error |= __vmwrite(GUEST_SS_BASE, host_env->ds_base);
300 error |= __vmwrite(GUEST_DS_BASE, host_env->ds_base);
301 error |= __vmwrite(GUEST_FS_BASE, host_env->ds_base);
302 error |= __vmwrite(GUEST_GS_BASE, host_env->ds_base);
303 error |= __vmwrite(GUEST_IDTR_BASE, host_env->idtr_base);
305 error |= __vmwrite(GUEST_ESP, context->esp);
306 error |= __vmwrite(GUEST_EIP, context->eip);
308 eflags = context->eflags & ~VMCS_EFLAGS_RESERVED_0; /* clear 0s */
309 eflags |= VMCS_EFLAGS_RESERVED_1; /* set 1s */
311 error |= __vmwrite(GUEST_EFLAGS, eflags);
313 error |= __vmwrite(GUEST_INTERRUPTIBILITY_INFO, 0);
314 #ifdef __i386__
315 __asm__ __volatile__ ("mov %%dr7, %0\n" : "=r" (dr7));
316 #else
317 __asm__ __volatile__ ("movq %%dr7, %0\n" : "=r" (dr7));
318 #endif
319 error |= __vmwrite(GUEST_DR7, dr7);
320 error |= __vmwrite(GUEST_VMCS0, 0xffffffff);
321 error |= __vmwrite(GUEST_VMCS1, 0xffffffff);
323 return error;
324 }
326 static inline int construct_vmcs_host(struct host_execution_env *host_env)
327 {
328 int error = 0;
329 unsigned long crn;
330 struct Xgt_desc_struct desc;
332 /* Host Selectors */
333 host_env->ds_selector = __HYPERVISOR_DS;
334 error |= __vmwrite(HOST_ES_SELECTOR, host_env->ds_selector);
335 error |= __vmwrite(HOST_SS_SELECTOR, host_env->ds_selector);
336 error |= __vmwrite(HOST_DS_SELECTOR, host_env->ds_selector);
337 error |= __vmwrite(HOST_FS_SELECTOR, host_env->ds_selector);
338 error |= __vmwrite(HOST_GS_SELECTOR, host_env->ds_selector);
340 host_env->cs_selector = __HYPERVISOR_CS;
341 error |= __vmwrite(HOST_CS_SELECTOR, host_env->cs_selector);
343 host_env->ds_base = 0;
344 host_env->cs_base = 0;
345 error |= __vmwrite(HOST_FS_BASE, host_env->ds_base);
346 error |= __vmwrite(HOST_GS_BASE, host_env->ds_base);
348 /* Debug */
349 __asm__ __volatile__ ("sidt (%0) \n" :: "a"(&desc) : "memory");
350 host_env->idtr_limit = desc.size;
351 host_env->idtr_base = desc.address;
352 error |= __vmwrite(HOST_IDTR_BASE, host_env->idtr_base);
354 #ifdef __i386__
355 __asm__ __volatile__ ("movl %%cr0,%0" : "=r" (crn) : );
356 #else
357 __asm__ __volatile__ ("movq %%cr0,%0" : "=r" (crn) : );
358 #endif
360 host_env->cr0 = crn;
361 error |= __vmwrite(HOST_CR0, crn); /* same CR0 */
363 /* CR3 is set in vmx_final_setup_hostos */
364 #ifdef __i386__
365 __asm__ __volatile__ ("movl %%cr4,%0" : "=r" (crn) : );
366 #else
367 __asm__ __volatile__ ("movq %%cr4,%0" : "=r" (crn) : );
368 #endif
369 host_env->cr4 = crn;
370 error |= __vmwrite(HOST_CR4, crn);
371 error |= __vmwrite(HOST_EIP, (unsigned long) vmx_asm_vmexit_handler);
373 return error;
374 }
376 /*
377 * Need to extend to support full virtualization.
378 * The variable use_host_env indicates if the new VMCS needs to use
379 * the same setups as the host has (xenolinux).
380 */
382 int construct_vmcs(struct arch_vmx_struct *arch_vmx,
383 execution_context_t *context,
384 full_execution_context_t *full_context,
385 int use_host_env)
386 {
387 int error;
388 u64 vmcs_phys_ptr;
390 struct host_execution_env host_env;
392 if (use_host_env != VMCS_USE_HOST_ENV)
393 return -EINVAL;
395 memset(&host_env, 0, sizeof(struct host_execution_env));
397 vmcs_phys_ptr = (u64) virt_to_phys(arch_vmx->vmcs);
399 if ((error = __vmpclear (vmcs_phys_ptr))) {
400 printk("construct_vmcs: VMCLEAR failed\n");
401 return -EINVAL;
402 }
403 if ((error = load_vmcs(arch_vmx, vmcs_phys_ptr))) {
404 printk("construct_vmcs: load_vmcs failed: VMCS = %lx\n",
405 (unsigned long) vmcs_phys_ptr);
406 return -EINVAL;
407 }
408 if ((error = construct_vmcs_controls())) {
409 printk("construct_vmcs: construct_vmcs_controls failed\n");
410 return -EINVAL;
411 }
412 /* host selectors */
413 if ((error = construct_vmcs_host(&host_env))) {
414 printk("construct_vmcs: construct_vmcs_host failed\n");
415 return -EINVAL;
416 }
417 /* guest selectors */
418 if ((error = construct_init_vmcs_guest(context, full_context, &host_env))) {
419 printk("construct_vmcs: construct_vmcs_guest failed\n");
420 return -EINVAL;
421 }
423 if ((error |= __vmwrite(EXCEPTION_BITMAP,
424 MONITOR_DEFAULT_EXCEPTION_BITMAP))) {
425 printk("construct_vmcs: setting Exception bitmap failed\n");
426 return -EINVAL;
427 }
429 return 0;
430 }
432 int load_vmcs(struct arch_vmx_struct *arch_vmx, u64 phys_ptr)
433 {
434 int error;
436 if ((error = __vmptrld(phys_ptr))) {
437 clear_bit(ARCH_VMX_VMCS_LOADED, &arch_vmx->flags);
438 return error;
439 }
440 set_bit(ARCH_VMX_VMCS_LOADED, &arch_vmx->flags);
441 return 0;
442 }
444 int store_vmcs(struct arch_vmx_struct *arch_vmx, u64 phys_ptr)
445 {
446 /* take the current VMCS */
447 __vmptrst(phys_ptr);
448 clear_bit(ARCH_VMX_VMCS_LOADED, &arch_vmx->flags);
449 return 0;
450 }
452 void vm_launch_fail(unsigned long eflags)
453 {
454 BUG();
455 }
457 void vm_resume_fail(unsigned long eflags)
458 {
459 BUG();
460 }
462 #endif /* CONFIG_VMX */
464 /*
465 * Local variables:
466 * mode: C
467 * c-set-style: "BSD"
468 * c-basic-offset: 4
469 * tab-width: 4
470 * indent-tabs-mode: nil
471 * End:
472 */