ia64/xen-unstable

view xen/arch/x86/hvm/vmx/vmx.c @ 10903:822c39808e62

[HVM] Initialise full regs structure for PIO requests.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Wed Aug 02 09:52:03 2006 +0100 (2006-08-02)
parents 022f29d4d2b8
children a6cb8ba24a91
line source
1 /*
2 * vmx.c: handling VMX architecture-related VM exits
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/lib.h>
23 #include <xen/trace.h>
24 #include <xen/sched.h>
25 #include <xen/irq.h>
26 #include <xen/softirq.h>
27 #include <xen/domain_page.h>
28 #include <asm/current.h>
29 #include <asm/io.h>
30 #include <asm/shadow.h>
31 #include <asm/regs.h>
32 #include <asm/cpufeature.h>
33 #include <asm/processor.h>
34 #include <asm/types.h>
35 #include <asm/msr.h>
36 #include <asm/spinlock.h>
37 #include <asm/hvm/hvm.h>
38 #include <asm/hvm/support.h>
39 #include <asm/hvm/vmx/vmx.h>
40 #include <asm/hvm/vmx/vmcs.h>
41 #include <asm/hvm/vmx/cpu.h>
42 #include <asm/shadow.h>
43 #if CONFIG_PAGING_LEVELS >= 3
44 #include <asm/shadow_64.h>
45 #endif
46 #include <public/sched.h>
47 #include <public/hvm/ioreq.h>
48 #include <asm/hvm/vpic.h>
49 #include <asm/hvm/vlapic.h>
51 static unsigned long trace_values[NR_CPUS][5];
52 #define TRACE_VMEXIT(index,value) trace_values[smp_processor_id()][index]=value
54 static void vmx_ctxt_switch_from(struct vcpu *v);
55 static void vmx_ctxt_switch_to(struct vcpu *v);
57 static int vmx_initialize_guest_resources(struct vcpu *v)
58 {
59 struct domain *d = v->domain;
60 struct vcpu *vc;
61 void *io_bitmap_a, *io_bitmap_b;
62 int rc;
64 v->arch.schedule_tail = arch_vmx_do_launch;
65 v->arch.ctxt_switch_from = vmx_ctxt_switch_from;
66 v->arch.ctxt_switch_to = vmx_ctxt_switch_to;
68 if ( v->vcpu_id != 0 )
69 return 1;
71 for_each_vcpu ( d, vc )
72 {
73 /* Initialize monitor page table */
74 vc->arch.monitor_table = pagetable_null();
76 memset(&vc->arch.hvm_vmx, 0, sizeof(struct arch_vmx_struct));
78 if ( (rc = vmx_create_vmcs(vc)) != 0 )
79 {
80 DPRINTK("Failed to create VMCS for vcpu %d: err=%d.\n",
81 vc->vcpu_id, rc);
82 return 0;
83 }
85 spin_lock_init(&vc->arch.hvm_vmx.vmcs_lock);
87 if ( (io_bitmap_a = alloc_xenheap_pages(IO_BITMAP_ORDER)) == NULL )
88 {
89 DPRINTK("Failed to allocate io bitmap b for vcpu %d.\n",
90 vc->vcpu_id);
91 return 0;
92 }
94 if ( (io_bitmap_b = alloc_xenheap_pages(IO_BITMAP_ORDER)) == NULL )
95 {
96 DPRINTK("Failed to allocate io bitmap b for vcpu %d.\n",
97 vc->vcpu_id);
98 return 0;
99 }
101 memset(io_bitmap_a, 0xff, 0x1000);
102 memset(io_bitmap_b, 0xff, 0x1000);
104 /* don't bother debug port access */
105 clear_bit(PC_DEBUG_PORT, io_bitmap_a);
107 vc->arch.hvm_vmx.io_bitmap_a = io_bitmap_a;
108 vc->arch.hvm_vmx.io_bitmap_b = io_bitmap_b;
109 }
111 /*
112 * Required to do this once per domain XXX todo: add a seperate function
113 * to do these.
114 */
115 memset(&d->shared_info->evtchn_mask[0], 0xff,
116 sizeof(d->shared_info->evtchn_mask));
118 /* Put the domain in shadow mode even though we're going to be using
119 * the shared 1:1 page table initially. It shouldn't hurt */
120 shadow_mode_enable(
121 d, SHM_enable|SHM_refcounts|SHM_translate|SHM_external|SHM_wr_pt_pte);
123 return 1;
124 }
126 static void vmx_relinquish_guest_resources(struct domain *d)
127 {
128 struct vcpu *v;
130 for_each_vcpu ( d, v )
131 {
132 vmx_destroy_vmcs(v);
133 if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
134 continue;
135 free_monitor_pagetable(v);
136 kill_timer(&v->arch.hvm_vmx.hlt_timer);
137 if ( hvm_apic_support(v->domain) && (VLAPIC(v) != NULL) )
138 {
139 kill_timer(&VLAPIC(v)->vlapic_timer);
140 xfree(VLAPIC(v));
141 }
142 }
144 kill_timer(&d->arch.hvm_domain.pl_time.periodic_tm.timer);
146 if ( d->arch.hvm_domain.shared_page_va )
147 unmap_domain_page_global(
148 (void *)d->arch.hvm_domain.shared_page_va);
150 shadow_direct_map_clean(d);
151 }
153 #ifdef __x86_64__
155 static struct vmx_msr_state percpu_msr[NR_CPUS];
157 static u32 msr_data_index[VMX_MSR_COUNT] =
158 {
159 MSR_LSTAR, MSR_STAR, MSR_CSTAR,
160 MSR_SYSCALL_MASK, MSR_EFER,
161 };
163 static void vmx_save_segments(struct vcpu *v)
164 {
165 rdmsrl(MSR_SHADOW_GS_BASE, v->arch.hvm_vmx.msr_content.shadow_gs);
166 }
168 /*
169 * To avoid MSR save/restore at every VM exit/entry time, we restore
170 * the x86_64 specific MSRs at domain switch time. Since those MSRs are
171 * are not modified once set for generic domains, we don't save them,
172 * but simply reset them to the values set at percpu_traps_init().
173 */
174 static void vmx_load_msrs(void)
175 {
176 struct vmx_msr_state *host_state = &percpu_msr[smp_processor_id()];
177 int i;
179 while ( host_state->flags )
180 {
181 i = find_first_set_bit(host_state->flags);
182 wrmsrl(msr_data_index[i], host_state->msr_items[i]);
183 clear_bit(i, &host_state->flags);
184 }
185 }
187 static void vmx_save_init_msrs(void)
188 {
189 struct vmx_msr_state *host_state = &percpu_msr[smp_processor_id()];
190 int i;
192 for ( i = 0; i < VMX_MSR_COUNT; i++ )
193 rdmsrl(msr_data_index[i], host_state->msr_items[i]);
194 }
196 #define CASE_READ_MSR(address) \
197 case MSR_ ## address: \
198 msr_content = msr->msr_items[VMX_INDEX_MSR_ ## address]; \
199 break
201 #define CASE_WRITE_MSR(address) \
202 case MSR_ ## address: \
203 { \
204 msr->msr_items[VMX_INDEX_MSR_ ## address] = msr_content; \
205 if (!test_bit(VMX_INDEX_MSR_ ## address, &msr->flags)) { \
206 set_bit(VMX_INDEX_MSR_ ## address, &msr->flags); \
207 } \
208 wrmsrl(MSR_ ## address, msr_content); \
209 set_bit(VMX_INDEX_MSR_ ## address, &host_state->flags); \
210 } \
211 break
213 #define IS_CANO_ADDRESS(add) 1
214 static inline int long_mode_do_msr_read(struct cpu_user_regs *regs)
215 {
216 u64 msr_content = 0;
217 struct vcpu *v = current;
218 struct vmx_msr_state *msr = &v->arch.hvm_vmx.msr_content;
220 switch ( regs->ecx ) {
221 case MSR_EFER:
222 HVM_DBG_LOG(DBG_LEVEL_2, "EFER msr_content 0x%"PRIx64, msr_content);
223 msr_content = msr->msr_items[VMX_INDEX_MSR_EFER];
225 /* the following code may be not needed */
226 if ( test_bit(VMX_CPU_STATE_LME_ENABLED, &v->arch.hvm_vmx.cpu_state) )
227 msr_content |= EFER_LME;
228 else
229 msr_content &= ~EFER_LME;
231 if ( VMX_LONG_GUEST(v) )
232 msr_content |= EFER_LMA;
233 else
234 msr_content &= ~EFER_LMA;
235 break;
237 case MSR_FS_BASE:
238 if ( !(VMX_LONG_GUEST(v)) )
239 /* XXX should it be GP fault */
240 domain_crash_synchronous();
242 __vmread(GUEST_FS_BASE, &msr_content);
243 break;
245 case MSR_GS_BASE:
246 if ( !(VMX_LONG_GUEST(v)) )
247 domain_crash_synchronous();
249 __vmread(GUEST_GS_BASE, &msr_content);
250 break;
252 case MSR_SHADOW_GS_BASE:
253 msr_content = msr->shadow_gs;
254 break;
256 CASE_READ_MSR(STAR);
257 CASE_READ_MSR(LSTAR);
258 CASE_READ_MSR(CSTAR);
259 CASE_READ_MSR(SYSCALL_MASK);
261 default:
262 return 0;
263 }
265 HVM_DBG_LOG(DBG_LEVEL_2, "msr_content: 0x%"PRIx64, msr_content);
267 regs->eax = msr_content & 0xffffffff;
268 regs->edx = msr_content >> 32;
270 return 1;
271 }
273 static inline int long_mode_do_msr_write(struct cpu_user_regs *regs)
274 {
275 u64 msr_content = regs->eax | ((u64)regs->edx << 32);
276 struct vcpu *v = current;
277 struct vmx_msr_state *msr = &v->arch.hvm_vmx.msr_content;
278 struct vmx_msr_state *host_state = &percpu_msr[smp_processor_id()];
280 HVM_DBG_LOG(DBG_LEVEL_1, "msr 0x%lx msr_content 0x%"PRIx64"\n",
281 (unsigned long)regs->ecx, msr_content);
283 switch ( regs->ecx ) {
284 case MSR_EFER:
285 /* offending reserved bit will cause #GP */
286 if ( msr_content & ~(EFER_LME | EFER_LMA | EFER_NX | EFER_SCE) )
287 {
288 printk("trying to set reserved bit in EFER\n");
289 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
290 return 0;
291 }
293 /* LME: 0 -> 1 */
294 if ( msr_content & EFER_LME &&
295 !test_bit(VMX_CPU_STATE_LME_ENABLED, &v->arch.hvm_vmx.cpu_state) )
296 {
297 if ( vmx_paging_enabled(v) ||
298 !test_bit(VMX_CPU_STATE_PAE_ENABLED,
299 &v->arch.hvm_vmx.cpu_state) )
300 {
301 printk("trying to set LME bit when "
302 "in paging mode or PAE bit is not set\n");
303 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
304 return 0;
305 }
307 set_bit(VMX_CPU_STATE_LME_ENABLED, &v->arch.hvm_vmx.cpu_state);
308 }
310 msr->msr_items[VMX_INDEX_MSR_EFER] = msr_content;
311 break;
313 case MSR_FS_BASE:
314 case MSR_GS_BASE:
315 if ( !(VMX_LONG_GUEST(v)) )
316 domain_crash_synchronous();
318 if ( !IS_CANO_ADDRESS(msr_content) )
319 {
320 HVM_DBG_LOG(DBG_LEVEL_1, "Not cano address of msr write\n");
321 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
322 return 0;
323 }
325 if ( regs->ecx == MSR_FS_BASE )
326 __vmwrite(GUEST_FS_BASE, msr_content);
327 else
328 __vmwrite(GUEST_GS_BASE, msr_content);
330 break;
332 case MSR_SHADOW_GS_BASE:
333 if ( !(VMX_LONG_GUEST(v)) )
334 domain_crash_synchronous();
336 v->arch.hvm_vmx.msr_content.shadow_gs = msr_content;
337 wrmsrl(MSR_SHADOW_GS_BASE, msr_content);
338 break;
340 CASE_WRITE_MSR(STAR);
341 CASE_WRITE_MSR(LSTAR);
342 CASE_WRITE_MSR(CSTAR);
343 CASE_WRITE_MSR(SYSCALL_MASK);
345 default:
346 return 0;
347 }
349 return 1;
350 }
352 static void vmx_restore_msrs(struct vcpu *v)
353 {
354 int i = 0;
355 struct vmx_msr_state *guest_state;
356 struct vmx_msr_state *host_state;
357 unsigned long guest_flags ;
359 guest_state = &v->arch.hvm_vmx.msr_content;;
360 host_state = &percpu_msr[smp_processor_id()];
362 wrmsrl(MSR_SHADOW_GS_BASE, guest_state->shadow_gs);
363 guest_flags = guest_state->flags;
364 if (!guest_flags)
365 return;
367 while (guest_flags){
368 i = find_first_set_bit(guest_flags);
370 HVM_DBG_LOG(DBG_LEVEL_2,
371 "restore guest's index %d msr %lx with %lx\n",
372 i, (unsigned long)msr_data_index[i],
373 (unsigned long)guest_state->msr_items[i]);
374 set_bit(i, &host_state->flags);
375 wrmsrl(msr_data_index[i], guest_state->msr_items[i]);
376 clear_bit(i, &guest_flags);
377 }
378 }
380 #else /* __i386__ */
382 #define vmx_save_segments(v) ((void)0)
383 #define vmx_load_msrs() ((void)0)
384 #define vmx_restore_msrs(v) ((void)0)
385 #define vmx_save_init_msrs() ((void)0)
387 static inline int long_mode_do_msr_read(struct cpu_user_regs *regs)
388 {
389 return 0;
390 }
392 static inline int long_mode_do_msr_write(struct cpu_user_regs *regs)
393 {
394 return 0;
395 }
397 #endif /* __i386__ */
399 #define loaddebug(_v,_reg) \
400 __asm__ __volatile__ ("mov %0,%%db" #_reg : : "r" ((_v)->debugreg[_reg]))
401 #define savedebug(_v,_reg) \
402 __asm__ __volatile__ ("mov %%db" #_reg ",%0" : : "r" ((_v)->debugreg[_reg]))
404 static inline void vmx_save_dr(struct vcpu *v)
405 {
406 if ( v->arch.hvm_vcpu.flag_dr_dirty )
407 {
408 savedebug(&v->arch.guest_context, 0);
409 savedebug(&v->arch.guest_context, 1);
410 savedebug(&v->arch.guest_context, 2);
411 savedebug(&v->arch.guest_context, 3);
412 savedebug(&v->arch.guest_context, 6);
414 v->arch.hvm_vcpu.flag_dr_dirty = 0;
416 v->arch.hvm_vcpu.u.vmx.exec_control |= CPU_BASED_MOV_DR_EXITING;
417 __vmwrite(CPU_BASED_VM_EXEC_CONTROL,
418 v->arch.hvm_vcpu.u.vmx.exec_control);
419 }
420 }
422 static inline void __restore_debug_registers(struct vcpu *v)
423 {
424 loaddebug(&v->arch.guest_context, 0);
425 loaddebug(&v->arch.guest_context, 1);
426 loaddebug(&v->arch.guest_context, 2);
427 loaddebug(&v->arch.guest_context, 3);
428 /* No 4 and 5 */
429 loaddebug(&v->arch.guest_context, 6);
430 /* DR7 is loaded from the vmcs. */
431 }
433 /*
434 * DR7 is saved and restored on every vmexit. Other debug registers only
435 * need to be restored if their value is going to affect execution -- i.e.,
436 * if one of the breakpoints is enabled. So mask out all bits that don't
437 * enable some breakpoint functionality.
438 *
439 * This is in part necessary because bit 10 of DR7 is hardwired to 1, so a
440 * simple if( guest_dr7 ) will always return true. As long as we're masking,
441 * we might as well do it right.
442 */
443 #define DR7_ACTIVE_MASK 0xff
445 static inline void vmx_restore_dr(struct vcpu *v)
446 {
447 unsigned long guest_dr7;
449 __vmread(GUEST_DR7, &guest_dr7);
451 /* Assumes guest does not have DR access at time of context switch. */
452 if ( unlikely(guest_dr7 & DR7_ACTIVE_MASK) )
453 __restore_debug_registers(v);
454 }
456 static void vmx_freeze_time(struct vcpu *v)
457 {
458 struct periodic_time *pt=&v->domain->arch.hvm_domain.pl_time.periodic_tm;
460 if ( pt->enabled && pt->first_injected && !v->arch.hvm_vcpu.guest_time ) {
461 v->arch.hvm_vcpu.guest_time = hvm_get_guest_time(v);
462 stop_timer(&(pt->timer));
463 }
464 }
466 static void vmx_ctxt_switch_from(struct vcpu *v)
467 {
468 vmx_freeze_time(v);
469 vmx_save_segments(v);
470 vmx_load_msrs();
471 vmx_save_dr(v);
472 }
474 static void vmx_ctxt_switch_to(struct vcpu *v)
475 {
476 vmx_restore_msrs(v);
477 vmx_restore_dr(v);
478 }
480 static void stop_vmx(void)
481 {
482 if (read_cr4() & X86_CR4_VMXE)
483 __vmxoff();
484 }
486 void vmx_migrate_timers(struct vcpu *v)
487 {
488 struct periodic_time *pt = &(v->domain->arch.hvm_domain.pl_time.periodic_tm);
490 if ( pt->enabled ) {
491 migrate_timer(&pt->timer, v->processor);
492 migrate_timer(&v->arch.hvm_vmx.hlt_timer, v->processor);
493 }
494 if ( hvm_apic_support(v->domain) && VLAPIC(v))
495 migrate_timer(&(VLAPIC(v)->vlapic_timer), v->processor);
496 }
498 static void vmx_store_cpu_guest_regs(
499 struct vcpu *v, struct cpu_user_regs *regs, unsigned long *crs)
500 {
501 vmx_vmcs_enter(v);
503 if ( regs != NULL )
504 {
505 __vmread(GUEST_RFLAGS, &regs->eflags);
506 __vmread(GUEST_SS_SELECTOR, &regs->ss);
507 __vmread(GUEST_CS_SELECTOR, &regs->cs);
508 __vmread(GUEST_DS_SELECTOR, &regs->ds);
509 __vmread(GUEST_ES_SELECTOR, &regs->es);
510 __vmread(GUEST_GS_SELECTOR, &regs->gs);
511 __vmread(GUEST_FS_SELECTOR, &regs->fs);
512 __vmread(GUEST_RIP, &regs->eip);
513 __vmread(GUEST_RSP, &regs->esp);
514 }
516 if ( crs != NULL )
517 {
518 __vmread(CR0_READ_SHADOW, &crs[0]);
519 __vmread(GUEST_CR3, &crs[3]);
520 __vmread(CR4_READ_SHADOW, &crs[4]);
521 }
523 vmx_vmcs_exit(v);
524 }
526 /*
527 * The VMX spec (section 4.3.1.2, Checks on Guest Segment
528 * Registers) says that virtual-8086 mode guests' segment
529 * base-address fields in the VMCS must be equal to their
530 * corresponding segment selector field shifted right by
531 * four bits upon vmentry.
532 *
533 * This function (called only for VM86-mode guests) fixes
534 * the bases to be consistent with the selectors in regs
535 * if they're not already. Without this, we can fail the
536 * vmentry check mentioned above.
537 */
538 static void fixup_vm86_seg_bases(struct cpu_user_regs *regs)
539 {
540 int err = 0;
541 unsigned long base;
543 err |= __vmread(GUEST_ES_BASE, &base);
544 if (regs->es << 4 != base)
545 err |= __vmwrite(GUEST_ES_BASE, regs->es << 4);
546 err |= __vmread(GUEST_CS_BASE, &base);
547 if (regs->cs << 4 != base)
548 err |= __vmwrite(GUEST_CS_BASE, regs->cs << 4);
549 err |= __vmread(GUEST_SS_BASE, &base);
550 if (regs->ss << 4 != base)
551 err |= __vmwrite(GUEST_SS_BASE, regs->ss << 4);
552 err |= __vmread(GUEST_DS_BASE, &base);
553 if (regs->ds << 4 != base)
554 err |= __vmwrite(GUEST_DS_BASE, regs->ds << 4);
555 err |= __vmread(GUEST_FS_BASE, &base);
556 if (regs->fs << 4 != base)
557 err |= __vmwrite(GUEST_FS_BASE, regs->fs << 4);
558 err |= __vmread(GUEST_GS_BASE, &base);
559 if (regs->gs << 4 != base)
560 err |= __vmwrite(GUEST_GS_BASE, regs->gs << 4);
562 BUG_ON(err);
563 }
565 static void vmx_load_cpu_guest_regs(struct vcpu *v, struct cpu_user_regs *regs)
566 {
567 vmx_vmcs_enter(v);
569 __vmwrite(GUEST_SS_SELECTOR, regs->ss);
570 __vmwrite(GUEST_DS_SELECTOR, regs->ds);
571 __vmwrite(GUEST_ES_SELECTOR, regs->es);
572 __vmwrite(GUEST_GS_SELECTOR, regs->gs);
573 __vmwrite(GUEST_FS_SELECTOR, regs->fs);
575 __vmwrite(GUEST_RSP, regs->esp);
577 __vmwrite(GUEST_RFLAGS, regs->eflags);
578 if (regs->eflags & EF_TF)
579 __vm_set_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);
580 else
581 __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);
582 if (regs->eflags & EF_VM)
583 fixup_vm86_seg_bases(regs);
585 __vmwrite(GUEST_CS_SELECTOR, regs->cs);
586 __vmwrite(GUEST_RIP, regs->eip);
588 vmx_vmcs_exit(v);
589 }
591 static int vmx_realmode(struct vcpu *v)
592 {
593 unsigned long rflags;
595 __vmread(GUEST_RFLAGS, &rflags);
596 return rflags & X86_EFLAGS_VM;
597 }
599 static int vmx_instruction_length(struct vcpu *v)
600 {
601 unsigned long inst_len;
603 if (__vmread(VM_EXIT_INSTRUCTION_LEN, &inst_len))
604 return 0;
605 return inst_len;
606 }
608 static unsigned long vmx_get_ctrl_reg(struct vcpu *v, unsigned int num)
609 {
610 switch ( num )
611 {
612 case 0:
613 return v->arch.hvm_vmx.cpu_cr0;
614 case 2:
615 return v->arch.hvm_vmx.cpu_cr2;
616 case 3:
617 return v->arch.hvm_vmx.cpu_cr3;
618 default:
619 BUG();
620 }
621 return 0; /* dummy */
622 }
624 /* SMP VMX guest support */
625 static void vmx_init_ap_context(struct vcpu_guest_context *ctxt,
626 int vcpuid, int trampoline_vector)
627 {
628 int i;
630 memset(ctxt, 0, sizeof(*ctxt));
632 /*
633 * Initial register values:
634 */
635 ctxt->user_regs.eip = VMXASSIST_BASE;
636 ctxt->user_regs.edx = vcpuid;
637 ctxt->user_regs.ebx = trampoline_vector;
639 ctxt->flags = VGCF_HVM_GUEST;
641 /* Virtual IDT is empty at start-of-day. */
642 for ( i = 0; i < 256; i++ )
643 {
644 ctxt->trap_ctxt[i].vector = i;
645 ctxt->trap_ctxt[i].cs = FLAT_KERNEL_CS;
646 }
648 /* No callback handlers. */
649 #if defined(__i386__)
650 ctxt->event_callback_cs = FLAT_KERNEL_CS;
651 ctxt->failsafe_callback_cs = FLAT_KERNEL_CS;
652 #endif
653 }
655 void do_nmi(struct cpu_user_regs *);
657 static int check_vmx_controls(u32 ctrls, u32 msr)
658 {
659 u32 vmx_msr_low, vmx_msr_high;
661 rdmsr(msr, vmx_msr_low, vmx_msr_high);
662 if ( (ctrls < vmx_msr_low) || (ctrls > vmx_msr_high) )
663 {
664 printk("Insufficient VMX capability 0x%x, "
665 "msr=0x%x,low=0x%8x,high=0x%x\n",
666 ctrls, msr, vmx_msr_low, vmx_msr_high);
667 return 0;
668 }
669 return 1;
670 }
672 /* Setup HVM interfaces */
673 static void vmx_setup_hvm_funcs(void)
674 {
675 if ( hvm_enabled )
676 return;
678 hvm_funcs.disable = stop_vmx;
680 hvm_funcs.initialize_guest_resources = vmx_initialize_guest_resources;
681 hvm_funcs.relinquish_guest_resources = vmx_relinquish_guest_resources;
683 hvm_funcs.store_cpu_guest_regs = vmx_store_cpu_guest_regs;
684 hvm_funcs.load_cpu_guest_regs = vmx_load_cpu_guest_regs;
686 hvm_funcs.realmode = vmx_realmode;
687 hvm_funcs.paging_enabled = vmx_paging_enabled;
688 hvm_funcs.instruction_length = vmx_instruction_length;
689 hvm_funcs.get_guest_ctrl_reg = vmx_get_ctrl_reg;
691 hvm_funcs.init_ap_context = vmx_init_ap_context;
692 }
694 static void vmx_init_hypercall_page(struct domain *d, void *hypercall_page)
695 {
696 char *p;
697 int i;
699 memset(hypercall_page, 0, PAGE_SIZE);
701 for ( i = 0; i < (PAGE_SIZE / 32); i++ )
702 {
703 p = (char *)(hypercall_page + (i * 32));
704 *(u8 *)(p + 0) = 0xb8; /* mov imm32, %eax */
705 *(u32 *)(p + 1) = i;
706 *(u8 *)(p + 5) = 0x0f; /* vmcall */
707 *(u8 *)(p + 6) = 0x01;
708 *(u8 *)(p + 7) = 0xc1;
709 *(u8 *)(p + 8) = 0xc3; /* ret */
710 }
712 /* Don't support HYPERVISOR_iret at the moment */
713 *(u16 *)(hypercall_page + (__HYPERVISOR_iret * 32)) = 0x0b0f; /* ud2 */
714 }
716 int start_vmx(void)
717 {
718 u32 eax, edx;
719 struct vmcs_struct *vmcs;
721 /*
722 * Xen does not fill x86_capability words except 0.
723 */
724 boot_cpu_data.x86_capability[4] = cpuid_ecx(1);
726 if (!(test_bit(X86_FEATURE_VMXE, &boot_cpu_data.x86_capability)))
727 return 0;
729 rdmsr(IA32_FEATURE_CONTROL_MSR, eax, edx);
731 if ( eax & IA32_FEATURE_CONTROL_MSR_LOCK )
732 {
733 if ( (eax & IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON) == 0x0 )
734 {
735 printk("VMX disabled by Feature Control MSR.\n");
736 return 0;
737 }
738 }
739 else
740 {
741 wrmsr(IA32_FEATURE_CONTROL_MSR,
742 IA32_FEATURE_CONTROL_MSR_LOCK |
743 IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON, 0);
744 }
746 if ( !check_vmx_controls(MONITOR_PIN_BASED_EXEC_CONTROLS,
747 MSR_IA32_VMX_PINBASED_CTLS_MSR) )
748 return 0;
749 if ( !check_vmx_controls(MONITOR_CPU_BASED_EXEC_CONTROLS,
750 MSR_IA32_VMX_PROCBASED_CTLS_MSR) )
751 return 0;
752 if ( !check_vmx_controls(MONITOR_VM_EXIT_CONTROLS,
753 MSR_IA32_VMX_EXIT_CTLS_MSR) )
754 return 0;
755 if ( !check_vmx_controls(MONITOR_VM_ENTRY_CONTROLS,
756 MSR_IA32_VMX_ENTRY_CTLS_MSR) )
757 return 0;
759 set_in_cr4(X86_CR4_VMXE);
761 vmx_init_vmcs_config();
763 if ( (vmcs = vmx_alloc_host_vmcs()) == NULL )
764 {
765 printk("Failed to allocate host VMCS\n");
766 return 0;
767 }
769 if ( __vmxon(virt_to_maddr(vmcs)) )
770 {
771 printk("VMXON failed\n");
772 vmx_free_host_vmcs(vmcs);
773 return 0;
774 }
776 printk("VMXON is done\n");
778 vmx_save_init_msrs();
780 vmx_setup_hvm_funcs();
782 hvm_funcs.init_hypercall_page = vmx_init_hypercall_page;
784 hvm_enabled = 1;
786 return 1;
787 }
789 /*
790 * Not all cases receive valid value in the VM-exit instruction length field.
791 */
792 #define __get_instruction_length(len) \
793 __vmread(VM_EXIT_INSTRUCTION_LEN, &(len)); \
794 if ((len) < 1 || (len) > 15) \
795 __hvm_bug(&regs);
797 static void inline __update_guest_eip(unsigned long inst_len)
798 {
799 unsigned long current_eip;
801 __vmread(GUEST_RIP, &current_eip);
802 __vmwrite(GUEST_RIP, current_eip + inst_len);
803 __vmwrite(GUEST_INTERRUPTIBILITY_INFO, 0);
804 }
807 static int vmx_do_page_fault(unsigned long va, struct cpu_user_regs *regs)
808 {
809 unsigned long gpa; /* FIXME: PAE */
810 int result;
812 #if 0 /* keep for debugging */
813 {
814 unsigned long eip;
816 __vmread(GUEST_RIP, &eip);
817 HVM_DBG_LOG(DBG_LEVEL_VMMU,
818 "vmx_do_page_fault = 0x%lx, eip = %lx, error_code = %lx",
819 va, eip, (unsigned long)regs->error_code);
820 }
821 #endif
823 if ( !vmx_paging_enabled(current) )
824 {
825 /* construct 1-to-1 direct mapping */
826 if ( shadow_direct_map_fault(va, regs) )
827 return 1;
829 handle_mmio(va, va);
830 TRACE_VMEXIT (2,2);
831 return 1;
832 }
833 gpa = gva_to_gpa(va);
835 /* Use 1:1 page table to identify MMIO address space */
836 if ( mmio_space(gpa) ){
837 struct vcpu *v = current;
838 /* No support for APIC */
839 if (!hvm_apic_support(v->domain) && gpa >= 0xFEC00000) {
840 u32 inst_len;
841 __vmread(VM_EXIT_INSTRUCTION_LEN, &(inst_len));
842 __update_guest_eip(inst_len);
843 return 1;
844 }
845 TRACE_VMEXIT (2,2);
846 /* in the case of MMIO, we are more interested in gpa than in va */
847 TRACE_VMEXIT (4,gpa);
848 handle_mmio(va, gpa);
849 return 1;
850 }
852 result = shadow_fault(va, regs);
853 TRACE_VMEXIT (2,result);
854 #if 0
855 if ( !result )
856 {
857 __vmread(GUEST_RIP, &eip);
858 printk("vmx pgfault to guest va=%lx eip=%lx\n", va, eip);
859 }
860 #endif
862 return result;
863 }
865 static void vmx_do_no_device_fault(void)
866 {
867 unsigned long cr0;
868 struct vcpu *v = current;
870 setup_fpu(current);
871 __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_NM);
873 /* Disable TS in guest CR0 unless the guest wants the exception too. */
874 __vmread_vcpu(v, CR0_READ_SHADOW, &cr0);
875 if ( !(cr0 & X86_CR0_TS) )
876 {
877 __vmread_vcpu(v, GUEST_CR0, &cr0);
878 cr0 &= ~X86_CR0_TS;
879 __vmwrite(GUEST_CR0, cr0);
880 }
881 }
883 #define bitmaskof(idx) (1U << ((idx)&31))
884 static void vmx_vmexit_do_cpuid(struct cpu_user_regs *regs)
885 {
886 unsigned int input = (unsigned int)regs->eax;
887 unsigned int count = (unsigned int)regs->ecx;
888 unsigned int eax, ebx, ecx, edx;
889 unsigned long eip;
890 struct vcpu *v = current;
892 __vmread(GUEST_RIP, &eip);
894 HVM_DBG_LOG(DBG_LEVEL_3, "(eax) 0x%08lx, (ebx) 0x%08lx, "
895 "(ecx) 0x%08lx, (edx) 0x%08lx, (esi) 0x%08lx, (edi) 0x%08lx",
896 (unsigned long)regs->eax, (unsigned long)regs->ebx,
897 (unsigned long)regs->ecx, (unsigned long)regs->edx,
898 (unsigned long)regs->esi, (unsigned long)regs->edi);
900 if ( input == CPUID_LEAF_0x4 )
901 {
902 cpuid_count(input, count, &eax, &ebx, &ecx, &edx);
903 eax &= NUM_CORES_RESET_MASK;
904 }
905 else if ( !cpuid_hypervisor_leaves(input, &eax, &ebx, &ecx, &edx) )
906 {
907 cpuid(input, &eax, &ebx, &ecx, &edx);
909 if ( input == CPUID_LEAF_0x1 )
910 {
911 /* mask off reserved bits */
912 ecx &= ~VMX_VCPU_CPUID_L1_ECX_RESERVED;
914 if ( !hvm_apic_support(v->domain) ||
915 !vlapic_global_enabled((VLAPIC(v))) )
916 {
917 /* Since the apic is disabled, avoid any
918 confusion about SMP cpus being available */
920 clear_bit(X86_FEATURE_APIC, &edx);
921 }
923 #if CONFIG_PAGING_LEVELS < 3
924 edx &= ~(bitmaskof(X86_FEATURE_PAE) |
925 bitmaskof(X86_FEATURE_PSE) |
926 bitmaskof(X86_FEATURE_PSE36));
927 #else
928 if ( v->domain->arch.ops->guest_paging_levels == PAGING_L2 )
929 {
930 if ( v->domain->arch.hvm_domain.pae_enabled )
931 clear_bit(X86_FEATURE_PSE36, &edx);
932 else
933 {
934 clear_bit(X86_FEATURE_PAE, &edx);
935 clear_bit(X86_FEATURE_PSE, &edx);
936 clear_bit(X86_FEATURE_PSE36, &edx);
937 }
938 }
939 #endif
941 ebx &= NUM_THREADS_RESET_MASK;
943 /* Unsupportable for virtualised CPUs. */
944 ecx &= ~(bitmaskof(X86_FEATURE_VMXE) |
945 bitmaskof(X86_FEATURE_EST) |
946 bitmaskof(X86_FEATURE_TM2) |
947 bitmaskof(X86_FEATURE_CID) |
948 bitmaskof(X86_FEATURE_MWAIT) );
950 edx &= ~( bitmaskof(X86_FEATURE_HT) |
951 bitmaskof(X86_FEATURE_MCA) |
952 bitmaskof(X86_FEATURE_MCE) |
953 bitmaskof(X86_FEATURE_ACPI) |
954 bitmaskof(X86_FEATURE_ACC) );
955 }
956 else if ( ( input == CPUID_LEAF_0x6 )
957 || ( input == CPUID_LEAF_0x9 )
958 || ( input == CPUID_LEAF_0xA ))
959 {
960 eax = ebx = ecx = edx = 0x0;
961 }
962 #ifdef __i386__
963 else if ( input == CPUID_LEAF_0x80000001 )
964 {
965 clear_bit(X86_FEATURE_LAHF_LM & 31, &ecx);
967 clear_bit(X86_FEATURE_LM & 31, &edx);
968 clear_bit(X86_FEATURE_SYSCALL & 31, &edx);
969 }
970 #endif
971 }
973 regs->eax = (unsigned long) eax;
974 regs->ebx = (unsigned long) ebx;
975 regs->ecx = (unsigned long) ecx;
976 regs->edx = (unsigned long) edx;
978 HVM_DBG_LOG(DBG_LEVEL_3, "eip@%lx, input: 0x%lx, "
979 "output: eax = 0x%08lx, ebx = 0x%08lx, "
980 "ecx = 0x%08lx, edx = 0x%08lx",
981 (unsigned long)eip, (unsigned long)input,
982 (unsigned long)eax, (unsigned long)ebx,
983 (unsigned long)ecx, (unsigned long)edx);
984 }
986 #define CASE_GET_REG_P(REG, reg) \
987 case REG_ ## REG: reg_p = (unsigned long *)&(regs->reg); break
989 #ifdef __i386__
990 #define CASE_EXTEND_GET_REG_P
991 #else
992 #define CASE_EXTEND_GET_REG_P \
993 CASE_GET_REG_P(R8, r8); \
994 CASE_GET_REG_P(R9, r9); \
995 CASE_GET_REG_P(R10, r10); \
996 CASE_GET_REG_P(R11, r11); \
997 CASE_GET_REG_P(R12, r12); \
998 CASE_GET_REG_P(R13, r13); \
999 CASE_GET_REG_P(R14, r14); \
1000 CASE_GET_REG_P(R15, r15)
1001 #endif
1003 static void vmx_dr_access(unsigned long exit_qualification,
1004 struct cpu_user_regs *regs)
1006 struct vcpu *v = current;
1008 v->arch.hvm_vcpu.flag_dr_dirty = 1;
1010 /* We could probably be smarter about this */
1011 __restore_debug_registers(v);
1013 /* Allow guest direct access to DR registers */
1014 v->arch.hvm_vcpu.u.vmx.exec_control &= ~CPU_BASED_MOV_DR_EXITING;
1015 __vmwrite(CPU_BASED_VM_EXEC_CONTROL,
1016 v->arch.hvm_vcpu.u.vmx.exec_control);
1019 /*
1020 * Invalidate the TLB for va. Invalidate the shadow page corresponding
1021 * the address va.
1022 */
1023 static void vmx_vmexit_do_invlpg(unsigned long va)
1025 unsigned long eip;
1026 struct vcpu *v = current;
1028 __vmread(GUEST_RIP, &eip);
1030 HVM_DBG_LOG(DBG_LEVEL_VMMU, "vmx_vmexit_do_invlpg: eip=%lx, va=%lx",
1031 eip, va);
1033 /*
1034 * We do the safest things first, then try to update the shadow
1035 * copying from guest
1036 */
1037 shadow_invlpg(v, va);
1040 static int check_for_null_selector(unsigned long eip)
1042 unsigned char inst[MAX_INST_LEN];
1043 unsigned long sel;
1044 int i, inst_len;
1045 int inst_copy_from_guest(unsigned char *, unsigned long, int);
1047 __vmread(VM_EXIT_INSTRUCTION_LEN, &inst_len);
1048 memset(inst, 0, MAX_INST_LEN);
1049 if (inst_copy_from_guest(inst, eip, inst_len) != inst_len) {
1050 printf("check_for_null_selector: get guest instruction failed\n");
1051 domain_crash_synchronous();
1054 for (i = 0; i < inst_len; i++) {
1055 switch (inst[i]) {
1056 case 0xf3: /* REPZ */
1057 case 0xf2: /* REPNZ */
1058 case 0xf0: /* LOCK */
1059 case 0x66: /* data32 */
1060 case 0x67: /* addr32 */
1061 continue;
1062 case 0x2e: /* CS */
1063 __vmread(GUEST_CS_SELECTOR, &sel);
1064 break;
1065 case 0x36: /* SS */
1066 __vmread(GUEST_SS_SELECTOR, &sel);
1067 break;
1068 case 0x26: /* ES */
1069 __vmread(GUEST_ES_SELECTOR, &sel);
1070 break;
1071 case 0x64: /* FS */
1072 __vmread(GUEST_FS_SELECTOR, &sel);
1073 break;
1074 case 0x65: /* GS */
1075 __vmread(GUEST_GS_SELECTOR, &sel);
1076 break;
1077 case 0x3e: /* DS */
1078 /* FALLTHROUGH */
1079 default:
1080 /* DS is the default */
1081 __vmread(GUEST_DS_SELECTOR, &sel);
1083 return sel == 0 ? 1 : 0;
1086 return 0;
1089 extern void send_pio_req(struct cpu_user_regs *regs, unsigned long port,
1090 unsigned long count, int size, long value,
1091 int dir, int pvalid);
1093 static void vmx_io_instruction(unsigned long exit_qualification,
1094 unsigned long inst_len)
1096 struct cpu_user_regs *regs;
1097 struct hvm_io_op *pio_opp;
1098 unsigned long eip, cs, eflags;
1099 unsigned long port, size, dir;
1100 int vm86;
1102 pio_opp = &current->arch.hvm_vcpu.io_op;
1103 pio_opp->instr = INSTR_PIO;
1104 pio_opp->flags = 0;
1106 regs = &pio_opp->io_context;
1108 /* Copy current guest state into io instruction state structure. */
1109 memcpy(regs, guest_cpu_user_regs(), HVM_CONTEXT_STACK_BYTES);
1110 hvm_store_cpu_guest_regs(current, regs, NULL);
1112 __vmread(GUEST_RIP, &eip);
1113 __vmread(GUEST_CS_SELECTOR, &cs);
1114 __vmread(GUEST_RFLAGS, &eflags);
1115 vm86 = eflags & X86_EFLAGS_VM ? 1 : 0;
1117 HVM_DBG_LOG(DBG_LEVEL_IO,
1118 "vmx_io_instruction: vm86 %d, eip=%lx:%lx, "
1119 "exit_qualification = %lx",
1120 vm86, cs, eip, exit_qualification);
1122 if (test_bit(6, &exit_qualification))
1123 port = (exit_qualification >> 16) & 0xFFFF;
1124 else
1125 port = regs->edx & 0xffff;
1126 TRACE_VMEXIT(1, port);
1127 size = (exit_qualification & 7) + 1;
1128 dir = test_bit(3, &exit_qualification); /* direction */
1130 if (test_bit(4, &exit_qualification)) { /* string instruction */
1131 unsigned long addr, count = 1;
1132 int sign = regs->eflags & EF_DF ? -1 : 1;
1134 __vmread(GUEST_LINEAR_ADDRESS, &addr);
1136 /*
1137 * In protected mode, guest linear address is invalid if the
1138 * selector is null.
1139 */
1140 if (!vm86 && check_for_null_selector(eip))
1141 addr = dir == IOREQ_WRITE ? regs->esi : regs->edi;
1143 if (test_bit(5, &exit_qualification)) { /* "rep" prefix */
1144 pio_opp->flags |= REPZ;
1145 count = vm86 ? regs->ecx & 0xFFFF : regs->ecx;
1148 /*
1149 * Handle string pio instructions that cross pages or that
1150 * are unaligned. See the comments in hvm_domain.c/handle_mmio()
1151 */
1152 if ((addr & PAGE_MASK) != ((addr + size - 1) & PAGE_MASK)) {
1153 unsigned long value = 0;
1155 pio_opp->flags |= OVERLAP;
1156 if (dir == IOREQ_WRITE)
1157 hvm_copy(&value, addr, size, HVM_COPY_IN);
1158 send_pio_req(regs, port, 1, size, value, dir, 0);
1159 } else {
1160 if ((addr & PAGE_MASK) != ((addr + count * size - 1) & PAGE_MASK)) {
1161 if (sign > 0)
1162 count = (PAGE_SIZE - (addr & ~PAGE_MASK)) / size;
1163 else
1164 count = (addr & ~PAGE_MASK) / size;
1165 } else
1166 __update_guest_eip(inst_len);
1168 send_pio_req(regs, port, count, size, addr, dir, 1);
1170 } else {
1171 if (port == 0xe9 && dir == IOREQ_WRITE && size == 1)
1172 hvm_print_line(current, regs->eax); /* guest debug output */
1174 __update_guest_eip(inst_len);
1175 send_pio_req(regs, port, 1, size, regs->eax, dir, 0);
1179 int
1180 vmx_world_save(struct vcpu *v, struct vmx_assist_context *c)
1182 unsigned long inst_len;
1183 int error = 0;
1185 error |= __vmread(VM_EXIT_INSTRUCTION_LEN, &inst_len);
1186 error |= __vmread(GUEST_RIP, &c->eip);
1187 c->eip += inst_len; /* skip transition instruction */
1188 error |= __vmread(GUEST_RSP, &c->esp);
1189 error |= __vmread(GUEST_RFLAGS, &c->eflags);
1191 error |= __vmread(CR0_READ_SHADOW, &c->cr0);
1192 c->cr3 = v->arch.hvm_vmx.cpu_cr3;
1193 error |= __vmread(CR4_READ_SHADOW, &c->cr4);
1195 error |= __vmread(GUEST_IDTR_LIMIT, &c->idtr_limit);
1196 error |= __vmread(GUEST_IDTR_BASE, &c->idtr_base);
1198 error |= __vmread(GUEST_GDTR_LIMIT, &c->gdtr_limit);
1199 error |= __vmread(GUEST_GDTR_BASE, &c->gdtr_base);
1201 error |= __vmread(GUEST_CS_SELECTOR, &c->cs_sel);
1202 error |= __vmread(GUEST_CS_LIMIT, &c->cs_limit);
1203 error |= __vmread(GUEST_CS_BASE, &c->cs_base);
1204 error |= __vmread(GUEST_CS_AR_BYTES, &c->cs_arbytes.bytes);
1206 error |= __vmread(GUEST_DS_SELECTOR, &c->ds_sel);
1207 error |= __vmread(GUEST_DS_LIMIT, &c->ds_limit);
1208 error |= __vmread(GUEST_DS_BASE, &c->ds_base);
1209 error |= __vmread(GUEST_DS_AR_BYTES, &c->ds_arbytes.bytes);
1211 error |= __vmread(GUEST_ES_SELECTOR, &c->es_sel);
1212 error |= __vmread(GUEST_ES_LIMIT, &c->es_limit);
1213 error |= __vmread(GUEST_ES_BASE, &c->es_base);
1214 error |= __vmread(GUEST_ES_AR_BYTES, &c->es_arbytes.bytes);
1216 error |= __vmread(GUEST_SS_SELECTOR, &c->ss_sel);
1217 error |= __vmread(GUEST_SS_LIMIT, &c->ss_limit);
1218 error |= __vmread(GUEST_SS_BASE, &c->ss_base);
1219 error |= __vmread(GUEST_SS_AR_BYTES, &c->ss_arbytes.bytes);
1221 error |= __vmread(GUEST_FS_SELECTOR, &c->fs_sel);
1222 error |= __vmread(GUEST_FS_LIMIT, &c->fs_limit);
1223 error |= __vmread(GUEST_FS_BASE, &c->fs_base);
1224 error |= __vmread(GUEST_FS_AR_BYTES, &c->fs_arbytes.bytes);
1226 error |= __vmread(GUEST_GS_SELECTOR, &c->gs_sel);
1227 error |= __vmread(GUEST_GS_LIMIT, &c->gs_limit);
1228 error |= __vmread(GUEST_GS_BASE, &c->gs_base);
1229 error |= __vmread(GUEST_GS_AR_BYTES, &c->gs_arbytes.bytes);
1231 error |= __vmread(GUEST_TR_SELECTOR, &c->tr_sel);
1232 error |= __vmread(GUEST_TR_LIMIT, &c->tr_limit);
1233 error |= __vmread(GUEST_TR_BASE, &c->tr_base);
1234 error |= __vmread(GUEST_TR_AR_BYTES, &c->tr_arbytes.bytes);
1236 error |= __vmread(GUEST_LDTR_SELECTOR, &c->ldtr_sel);
1237 error |= __vmread(GUEST_LDTR_LIMIT, &c->ldtr_limit);
1238 error |= __vmread(GUEST_LDTR_BASE, &c->ldtr_base);
1239 error |= __vmread(GUEST_LDTR_AR_BYTES, &c->ldtr_arbytes.bytes);
1241 return !error;
1244 int
1245 vmx_world_restore(struct vcpu *v, struct vmx_assist_context *c)
1247 unsigned long mfn, old_cr4, old_base_mfn;
1248 int error = 0;
1250 error |= __vmwrite(GUEST_RIP, c->eip);
1251 error |= __vmwrite(GUEST_RSP, c->esp);
1252 error |= __vmwrite(GUEST_RFLAGS, c->eflags);
1254 error |= __vmwrite(CR0_READ_SHADOW, c->cr0);
1256 if (!vmx_paging_enabled(v)) {
1257 HVM_DBG_LOG(DBG_LEVEL_VMMU, "switching to vmxassist. use phys table");
1258 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->domain->arch.phys_table));
1259 goto skip_cr3;
1262 if (c->cr3 == v->arch.hvm_vmx.cpu_cr3) {
1263 /*
1264 * This is simple TLB flush, implying the guest has
1265 * removed some translation or changed page attributes.
1266 * We simply invalidate the shadow.
1267 */
1268 mfn = get_mfn_from_gpfn(c->cr3 >> PAGE_SHIFT);
1269 if (mfn != pagetable_get_pfn(v->arch.guest_table)) {
1270 printk("Invalid CR3 value=%x", c->cr3);
1271 domain_crash_synchronous();
1272 return 0;
1274 shadow_sync_all(v->domain);
1275 } else {
1276 /*
1277 * If different, make a shadow. Check if the PDBR is valid
1278 * first.
1279 */
1280 HVM_DBG_LOG(DBG_LEVEL_VMMU, "CR3 c->cr3 = %x", c->cr3);
1281 if ((c->cr3 >> PAGE_SHIFT) > v->domain->max_pages) {
1282 printk("Invalid CR3 value=%x", c->cr3);
1283 domain_crash_synchronous();
1284 return 0;
1286 mfn = get_mfn_from_gpfn(c->cr3 >> PAGE_SHIFT);
1287 if(!get_page(mfn_to_page(mfn), v->domain))
1288 return 0;
1289 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1290 v->arch.guest_table = pagetable_from_pfn(mfn);
1291 if (old_base_mfn)
1292 put_page(mfn_to_page(old_base_mfn));
1293 /*
1294 * arch.shadow_table should now hold the next CR3 for shadow
1295 */
1296 v->arch.hvm_vmx.cpu_cr3 = c->cr3;
1297 update_pagetables(v);
1298 HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %x", c->cr3);
1299 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->arch.shadow_table));
1302 skip_cr3:
1304 error |= __vmread(CR4_READ_SHADOW, &old_cr4);
1305 error |= __vmwrite(GUEST_CR4, (c->cr4 | VMX_CR4_HOST_MASK));
1306 error |= __vmwrite(CR4_READ_SHADOW, c->cr4);
1308 error |= __vmwrite(GUEST_IDTR_LIMIT, c->idtr_limit);
1309 error |= __vmwrite(GUEST_IDTR_BASE, c->idtr_base);
1311 error |= __vmwrite(GUEST_GDTR_LIMIT, c->gdtr_limit);
1312 error |= __vmwrite(GUEST_GDTR_BASE, c->gdtr_base);
1314 error |= __vmwrite(GUEST_CS_SELECTOR, c->cs_sel);
1315 error |= __vmwrite(GUEST_CS_LIMIT, c->cs_limit);
1316 error |= __vmwrite(GUEST_CS_BASE, c->cs_base);
1317 error |= __vmwrite(GUEST_CS_AR_BYTES, c->cs_arbytes.bytes);
1319 error |= __vmwrite(GUEST_DS_SELECTOR, c->ds_sel);
1320 error |= __vmwrite(GUEST_DS_LIMIT, c->ds_limit);
1321 error |= __vmwrite(GUEST_DS_BASE, c->ds_base);
1322 error |= __vmwrite(GUEST_DS_AR_BYTES, c->ds_arbytes.bytes);
1324 error |= __vmwrite(GUEST_ES_SELECTOR, c->es_sel);
1325 error |= __vmwrite(GUEST_ES_LIMIT, c->es_limit);
1326 error |= __vmwrite(GUEST_ES_BASE, c->es_base);
1327 error |= __vmwrite(GUEST_ES_AR_BYTES, c->es_arbytes.bytes);
1329 error |= __vmwrite(GUEST_SS_SELECTOR, c->ss_sel);
1330 error |= __vmwrite(GUEST_SS_LIMIT, c->ss_limit);
1331 error |= __vmwrite(GUEST_SS_BASE, c->ss_base);
1332 error |= __vmwrite(GUEST_SS_AR_BYTES, c->ss_arbytes.bytes);
1334 error |= __vmwrite(GUEST_FS_SELECTOR, c->fs_sel);
1335 error |= __vmwrite(GUEST_FS_LIMIT, c->fs_limit);
1336 error |= __vmwrite(GUEST_FS_BASE, c->fs_base);
1337 error |= __vmwrite(GUEST_FS_AR_BYTES, c->fs_arbytes.bytes);
1339 error |= __vmwrite(GUEST_GS_SELECTOR, c->gs_sel);
1340 error |= __vmwrite(GUEST_GS_LIMIT, c->gs_limit);
1341 error |= __vmwrite(GUEST_GS_BASE, c->gs_base);
1342 error |= __vmwrite(GUEST_GS_AR_BYTES, c->gs_arbytes.bytes);
1344 error |= __vmwrite(GUEST_TR_SELECTOR, c->tr_sel);
1345 error |= __vmwrite(GUEST_TR_LIMIT, c->tr_limit);
1346 error |= __vmwrite(GUEST_TR_BASE, c->tr_base);
1347 error |= __vmwrite(GUEST_TR_AR_BYTES, c->tr_arbytes.bytes);
1349 error |= __vmwrite(GUEST_LDTR_SELECTOR, c->ldtr_sel);
1350 error |= __vmwrite(GUEST_LDTR_LIMIT, c->ldtr_limit);
1351 error |= __vmwrite(GUEST_LDTR_BASE, c->ldtr_base);
1352 error |= __vmwrite(GUEST_LDTR_AR_BYTES, c->ldtr_arbytes.bytes);
1354 return !error;
1357 enum { VMX_ASSIST_INVOKE = 0, VMX_ASSIST_RESTORE };
1359 int
1360 vmx_assist(struct vcpu *v, int mode)
1362 struct vmx_assist_context c;
1363 u32 magic;
1364 u32 cp;
1366 /* make sure vmxassist exists (this is not an error) */
1367 if (!hvm_copy(&magic, VMXASSIST_MAGIC_OFFSET, sizeof(magic), HVM_COPY_IN))
1368 return 0;
1369 if (magic != VMXASSIST_MAGIC)
1370 return 0;
1372 switch (mode) {
1373 /*
1374 * Transfer control to vmxassist.
1375 * Store the current context in VMXASSIST_OLD_CONTEXT and load
1376 * the new VMXASSIST_NEW_CONTEXT context. This context was created
1377 * by vmxassist and will transfer control to it.
1378 */
1379 case VMX_ASSIST_INVOKE:
1380 /* save the old context */
1381 if (!hvm_copy(&cp, VMXASSIST_OLD_CONTEXT, sizeof(cp), HVM_COPY_IN))
1382 goto error;
1383 if (cp != 0) {
1384 if (!vmx_world_save(v, &c))
1385 goto error;
1386 if (!hvm_copy(&c, cp, sizeof(c), HVM_COPY_OUT))
1387 goto error;
1390 /* restore the new context, this should activate vmxassist */
1391 if (!hvm_copy(&cp, VMXASSIST_NEW_CONTEXT, sizeof(cp), HVM_COPY_IN))
1392 goto error;
1393 if (cp != 0) {
1394 if (!hvm_copy(&c, cp, sizeof(c), HVM_COPY_IN))
1395 goto error;
1396 if (!vmx_world_restore(v, &c))
1397 goto error;
1398 return 1;
1400 break;
1402 /*
1403 * Restore the VMXASSIST_OLD_CONTEXT that was saved by VMX_ASSIST_INVOKE
1404 * above.
1405 */
1406 case VMX_ASSIST_RESTORE:
1407 /* save the old context */
1408 if (!hvm_copy(&cp, VMXASSIST_OLD_CONTEXT, sizeof(cp), HVM_COPY_IN))
1409 goto error;
1410 if (cp != 0) {
1411 if (!hvm_copy(&c, cp, sizeof(c), HVM_COPY_IN))
1412 goto error;
1413 if (!vmx_world_restore(v, &c))
1414 goto error;
1415 return 1;
1417 break;
1420 error:
1421 printf("Failed to transfer to vmxassist\n");
1422 domain_crash_synchronous();
1423 return 0;
1426 static int vmx_set_cr0(unsigned long value)
1428 struct vcpu *v = current;
1429 unsigned long mfn;
1430 unsigned long eip;
1431 int paging_enabled;
1432 unsigned long vm_entry_value;
1433 unsigned long old_cr0;
1435 /*
1436 * CR0: We don't want to lose PE and PG.
1437 */
1438 __vmread_vcpu(v, CR0_READ_SHADOW, &old_cr0);
1439 paging_enabled = (old_cr0 & X86_CR0_PE) && (old_cr0 & X86_CR0_PG);
1441 /* TS cleared? Then initialise FPU now. */
1442 if ( !(value & X86_CR0_TS) )
1444 setup_fpu(v);
1445 __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_NM);
1448 __vmwrite(GUEST_CR0, value | X86_CR0_PE | X86_CR0_PG | X86_CR0_NE);
1449 __vmwrite(CR0_READ_SHADOW, value);
1451 HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR0 value = %lx\n", value);
1453 if ( (value & X86_CR0_PE) && (value & X86_CR0_PG) && !paging_enabled )
1455 /*
1456 * Trying to enable guest paging.
1457 * The guest CR3 must be pointing to the guest physical.
1458 */
1459 if ( !VALID_MFN(mfn = get_mfn_from_gpfn(
1460 v->arch.hvm_vmx.cpu_cr3 >> PAGE_SHIFT)) ||
1461 !get_page(mfn_to_page(mfn), v->domain) )
1463 printk("Invalid CR3 value = %lx", v->arch.hvm_vmx.cpu_cr3);
1464 domain_crash_synchronous(); /* need to take a clean path */
1467 #if defined(__x86_64__)
1468 if ( test_bit(VMX_CPU_STATE_LME_ENABLED,
1469 &v->arch.hvm_vmx.cpu_state) &&
1470 !test_bit(VMX_CPU_STATE_PAE_ENABLED,
1471 &v->arch.hvm_vmx.cpu_state) )
1473 HVM_DBG_LOG(DBG_LEVEL_1, "Enable paging before PAE enabled\n");
1474 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
1477 if ( test_bit(VMX_CPU_STATE_LME_ENABLED,
1478 &v->arch.hvm_vmx.cpu_state) )
1480 /* Here the PAE is should be opened */
1481 HVM_DBG_LOG(DBG_LEVEL_1, "Enable long mode\n");
1482 set_bit(VMX_CPU_STATE_LMA_ENABLED,
1483 &v->arch.hvm_vmx.cpu_state);
1485 __vmread(VM_ENTRY_CONTROLS, &vm_entry_value);
1486 vm_entry_value |= VM_ENTRY_CONTROLS_IA32E_MODE;
1487 __vmwrite(VM_ENTRY_CONTROLS, vm_entry_value);
1489 if ( !shadow_set_guest_paging_levels(v->domain, PAGING_L4) )
1491 printk("Unsupported guest paging levels\n");
1492 domain_crash_synchronous(); /* need to take a clean path */
1495 else
1496 #endif /* __x86_64__ */
1498 #if CONFIG_PAGING_LEVELS >= 3
1499 /* seems it's a 32-bit or 32-bit PAE guest */
1501 if ( test_bit(VMX_CPU_STATE_PAE_ENABLED,
1502 &v->arch.hvm_vmx.cpu_state) )
1504 /* The guest enables PAE first and then it enables PG, it is
1505 * really a PAE guest */
1506 if ( !shadow_set_guest_paging_levels(v->domain, PAGING_L3) )
1508 printk("Unsupported guest paging levels\n");
1509 domain_crash_synchronous();
1512 else
1514 if ( !shadow_set_guest_paging_levels(v->domain, PAGING_L2) )
1516 printk("Unsupported guest paging levels\n");
1517 domain_crash_synchronous(); /* need to take a clean path */
1520 #endif
1523 /*
1524 * Now arch.guest_table points to machine physical.
1525 */
1526 v->arch.guest_table = pagetable_from_pfn(mfn);
1527 update_pagetables(v);
1529 HVM_DBG_LOG(DBG_LEVEL_VMMU, "New arch.guest_table = %lx",
1530 (unsigned long) (mfn << PAGE_SHIFT));
1532 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->arch.shadow_table));
1533 /*
1534 * arch->shadow_table should hold the next CR3 for shadow
1535 */
1536 HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %lx, mfn = %lx",
1537 v->arch.hvm_vmx.cpu_cr3, mfn);
1540 if ( !((value & X86_CR0_PE) && (value & X86_CR0_PG)) && paging_enabled )
1541 if ( v->arch.hvm_vmx.cpu_cr3 ) {
1542 put_page(mfn_to_page(get_mfn_from_gpfn(
1543 v->arch.hvm_vmx.cpu_cr3 >> PAGE_SHIFT)));
1544 v->arch.guest_table = pagetable_null();
1547 /*
1548 * VMX does not implement real-mode virtualization. We emulate
1549 * real-mode by performing a world switch to VMXAssist whenever
1550 * a partition disables the CR0.PE bit.
1551 */
1552 if ( (value & X86_CR0_PE) == 0 )
1554 if ( value & X86_CR0_PG ) {
1555 /* inject GP here */
1556 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
1557 return 0;
1558 } else {
1559 /*
1560 * Disable paging here.
1561 * Same to PE == 1 && PG == 0
1562 */
1563 if ( test_bit(VMX_CPU_STATE_LMA_ENABLED,
1564 &v->arch.hvm_vmx.cpu_state) )
1566 clear_bit(VMX_CPU_STATE_LMA_ENABLED,
1567 &v->arch.hvm_vmx.cpu_state);
1568 __vmread(VM_ENTRY_CONTROLS, &vm_entry_value);
1569 vm_entry_value &= ~VM_ENTRY_CONTROLS_IA32E_MODE;
1570 __vmwrite(VM_ENTRY_CONTROLS, vm_entry_value);
1574 clear_all_shadow_status(v->domain);
1575 if ( vmx_assist(v, VMX_ASSIST_INVOKE) ) {
1576 set_bit(VMX_CPU_STATE_ASSIST_ENABLED, &v->arch.hvm_vmx.cpu_state);
1577 __vmread(GUEST_RIP, &eip);
1578 HVM_DBG_LOG(DBG_LEVEL_1,
1579 "Transfering control to vmxassist %%eip 0x%lx\n", eip);
1580 return 0; /* do not update eip! */
1582 } else if ( test_bit(VMX_CPU_STATE_ASSIST_ENABLED,
1583 &v->arch.hvm_vmx.cpu_state) )
1585 __vmread(GUEST_RIP, &eip);
1586 HVM_DBG_LOG(DBG_LEVEL_1,
1587 "Enabling CR0.PE at %%eip 0x%lx\n", eip);
1588 if ( vmx_assist(v, VMX_ASSIST_RESTORE) )
1590 clear_bit(VMX_CPU_STATE_ASSIST_ENABLED,
1591 &v->arch.hvm_vmx.cpu_state);
1592 __vmread(GUEST_RIP, &eip);
1593 HVM_DBG_LOG(DBG_LEVEL_1,
1594 "Restoring to %%eip 0x%lx\n", eip);
1595 return 0; /* do not update eip! */
1598 else if ( (value & (X86_CR0_PE | X86_CR0_PG)) == X86_CR0_PE )
1600 /* we should take care of this kind of situation */
1601 clear_all_shadow_status(v->domain);
1602 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->domain->arch.phys_table));
1605 return 1;
1608 #define CASE_SET_REG(REG, reg) \
1609 case REG_ ## REG: regs->reg = value; break
1610 #define CASE_GET_REG(REG, reg) \
1611 case REG_ ## REG: value = regs->reg; break
1613 #define CASE_EXTEND_SET_REG \
1614 CASE_EXTEND_REG(S)
1615 #define CASE_EXTEND_GET_REG \
1616 CASE_EXTEND_REG(G)
1618 #ifdef __i386__
1619 #define CASE_EXTEND_REG(T)
1620 #else
1621 #define CASE_EXTEND_REG(T) \
1622 CASE_ ## T ## ET_REG(R8, r8); \
1623 CASE_ ## T ## ET_REG(R9, r9); \
1624 CASE_ ## T ## ET_REG(R10, r10); \
1625 CASE_ ## T ## ET_REG(R11, r11); \
1626 CASE_ ## T ## ET_REG(R12, r12); \
1627 CASE_ ## T ## ET_REG(R13, r13); \
1628 CASE_ ## T ## ET_REG(R14, r14); \
1629 CASE_ ## T ## ET_REG(R15, r15)
1630 #endif
1632 /*
1633 * Write to control registers
1634 */
1635 static int mov_to_cr(int gp, int cr, struct cpu_user_regs *regs)
1637 unsigned long value;
1638 unsigned long old_cr;
1639 struct vcpu *v = current;
1641 switch ( gp ) {
1642 CASE_GET_REG(EAX, eax);
1643 CASE_GET_REG(ECX, ecx);
1644 CASE_GET_REG(EDX, edx);
1645 CASE_GET_REG(EBX, ebx);
1646 CASE_GET_REG(EBP, ebp);
1647 CASE_GET_REG(ESI, esi);
1648 CASE_GET_REG(EDI, edi);
1649 CASE_EXTEND_GET_REG;
1650 case REG_ESP:
1651 __vmread(GUEST_RSP, &value);
1652 break;
1653 default:
1654 printk("invalid gp: %d\n", gp);
1655 __hvm_bug(regs);
1658 HVM_DBG_LOG(DBG_LEVEL_1, "CR%d, value = %lx", cr, value);
1660 switch ( cr ) {
1661 case 0:
1662 return vmx_set_cr0(value);
1663 case 3:
1665 unsigned long old_base_mfn, mfn;
1667 /*
1668 * If paging is not enabled yet, simply copy the value to CR3.
1669 */
1670 if (!vmx_paging_enabled(v)) {
1671 v->arch.hvm_vmx.cpu_cr3 = value;
1672 break;
1675 /*
1676 * We make a new one if the shadow does not exist.
1677 */
1678 if (value == v->arch.hvm_vmx.cpu_cr3) {
1679 /*
1680 * This is simple TLB flush, implying the guest has
1681 * removed some translation or changed page attributes.
1682 * We simply invalidate the shadow.
1683 */
1684 mfn = get_mfn_from_gpfn(value >> PAGE_SHIFT);
1685 if (mfn != pagetable_get_pfn(v->arch.guest_table))
1686 __hvm_bug(regs);
1687 shadow_sync_all(v->domain);
1688 } else {
1689 /*
1690 * If different, make a shadow. Check if the PDBR is valid
1691 * first.
1692 */
1693 HVM_DBG_LOG(DBG_LEVEL_VMMU, "CR3 value = %lx", value);
1694 if ( ((value >> PAGE_SHIFT) > v->domain->max_pages ) ||
1695 !VALID_MFN(mfn = get_mfn_from_gpfn(value >> PAGE_SHIFT)) ||
1696 !get_page(mfn_to_page(mfn), v->domain) )
1698 printk("Invalid CR3 value=%lx", value);
1699 domain_crash_synchronous(); /* need to take a clean path */
1701 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1702 v->arch.guest_table = pagetable_from_pfn(mfn);
1703 if (old_base_mfn)
1704 put_page(mfn_to_page(old_base_mfn));
1705 /*
1706 * arch.shadow_table should now hold the next CR3 for shadow
1707 */
1708 #if CONFIG_PAGING_LEVELS >= 3
1709 if ( v->domain->arch.ops->guest_paging_levels == PAGING_L3 )
1710 shadow_sync_all(v->domain);
1711 #endif
1713 v->arch.hvm_vmx.cpu_cr3 = value;
1714 update_pagetables(v);
1715 HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %lx",
1716 value);
1717 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->arch.shadow_table));
1719 break;
1721 case 4: /* CR4 */
1723 __vmread(CR4_READ_SHADOW, &old_cr);
1725 if ( value & X86_CR4_PAE && !(old_cr & X86_CR4_PAE) )
1727 set_bit(VMX_CPU_STATE_PAE_ENABLED, &v->arch.hvm_vmx.cpu_state);
1729 if ( vmx_pgbit_test(v) )
1731 /* The guest is a 32-bit PAE guest. */
1732 #if CONFIG_PAGING_LEVELS >= 3
1733 unsigned long mfn, old_base_mfn;
1735 if( !shadow_set_guest_paging_levels(v->domain, PAGING_L3) )
1737 printk("Unsupported guest paging levels\n");
1738 domain_crash_synchronous(); /* need to take a clean path */
1741 if ( !VALID_MFN(mfn = get_mfn_from_gpfn(
1742 v->arch.hvm_vmx.cpu_cr3 >> PAGE_SHIFT)) ||
1743 !get_page(mfn_to_page(mfn), v->domain) )
1745 printk("Invalid CR3 value = %lx", v->arch.hvm_vmx.cpu_cr3);
1746 domain_crash_synchronous(); /* need to take a clean path */
1749 old_base_mfn = pagetable_get_pfn(v->arch.guest_table);
1750 if ( old_base_mfn )
1751 put_page(mfn_to_page(old_base_mfn));
1753 /*
1754 * Now arch.guest_table points to machine physical.
1755 */
1757 v->arch.guest_table = pagetable_from_pfn(mfn);
1758 update_pagetables(v);
1760 HVM_DBG_LOG(DBG_LEVEL_VMMU, "New arch.guest_table = %lx",
1761 (unsigned long) (mfn << PAGE_SHIFT));
1763 __vmwrite(GUEST_CR3, pagetable_get_paddr(v->arch.shadow_table));
1765 /*
1766 * arch->shadow_table should hold the next CR3 for shadow
1767 */
1769 HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %lx, mfn = %lx",
1770 v->arch.hvm_vmx.cpu_cr3, mfn);
1771 #endif
1773 else
1775 /* The guest is a 64 bit or 32-bit PAE guest. */
1776 #if CONFIG_PAGING_LEVELS >= 3
1777 if ( (v->domain->arch.ops != NULL) &&
1778 v->domain->arch.ops->guest_paging_levels == PAGING_L2)
1780 /* Seems the guest first enables PAE without enabling PG,
1781 * it must enable PG after that, and it is a 32-bit PAE
1782 * guest */
1784 if ( !shadow_set_guest_paging_levels(v->domain,
1785 PAGING_L3) )
1787 printk("Unsupported guest paging levels\n");
1788 /* need to take a clean path */
1789 domain_crash_synchronous();
1792 #endif
1795 else if ( value & X86_CR4_PAE )
1796 set_bit(VMX_CPU_STATE_PAE_ENABLED, &v->arch.hvm_vmx.cpu_state);
1797 else
1799 if ( test_bit(VMX_CPU_STATE_LMA_ENABLED, &v->arch.hvm_vmx.cpu_state) )
1800 vmx_inject_hw_exception(v, TRAP_gp_fault, 0);
1802 clear_bit(VMX_CPU_STATE_PAE_ENABLED, &v->arch.hvm_vmx.cpu_state);
1805 __vmwrite(GUEST_CR4, value| VMX_CR4_HOST_MASK);
1806 __vmwrite(CR4_READ_SHADOW, value);
1808 /*
1809 * Writing to CR4 to modify the PSE, PGE, or PAE flag invalidates
1810 * all TLB entries except global entries.
1811 */
1812 if ( (old_cr ^ value) & (X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE) )
1813 shadow_sync_all(v->domain);
1815 break;
1817 default:
1818 printk("invalid cr: %d\n", gp);
1819 __hvm_bug(regs);
1822 return 1;
1825 /*
1826 * Read from control registers. CR0 and CR4 are read from the shadow.
1827 */
1828 static void mov_from_cr(int cr, int gp, struct cpu_user_regs *regs)
1830 unsigned long value;
1831 struct vcpu *v = current;
1833 if ( cr != 3 )
1834 __hvm_bug(regs);
1836 value = (unsigned long) v->arch.hvm_vmx.cpu_cr3;
1838 switch ( gp ) {
1839 CASE_SET_REG(EAX, eax);
1840 CASE_SET_REG(ECX, ecx);
1841 CASE_SET_REG(EDX, edx);
1842 CASE_SET_REG(EBX, ebx);
1843 CASE_SET_REG(EBP, ebp);
1844 CASE_SET_REG(ESI, esi);
1845 CASE_SET_REG(EDI, edi);
1846 CASE_EXTEND_SET_REG;
1847 case REG_ESP:
1848 __vmwrite(GUEST_RSP, value);
1849 regs->esp = value;
1850 break;
1851 default:
1852 printk("invalid gp: %d\n", gp);
1853 __hvm_bug(regs);
1856 HVM_DBG_LOG(DBG_LEVEL_VMMU, "CR%d, value = %lx", cr, value);
1859 static int vmx_cr_access(unsigned long exit_qualification, struct cpu_user_regs *regs)
1861 unsigned int gp, cr;
1862 unsigned long value;
1863 struct vcpu *v = current;
1865 switch (exit_qualification & CONTROL_REG_ACCESS_TYPE) {
1866 case TYPE_MOV_TO_CR:
1867 gp = exit_qualification & CONTROL_REG_ACCESS_REG;
1868 cr = exit_qualification & CONTROL_REG_ACCESS_NUM;
1869 TRACE_VMEXIT(1,TYPE_MOV_TO_CR);
1870 TRACE_VMEXIT(2,cr);
1871 TRACE_VMEXIT(3,gp);
1872 return mov_to_cr(gp, cr, regs);
1873 case TYPE_MOV_FROM_CR:
1874 gp = exit_qualification & CONTROL_REG_ACCESS_REG;
1875 cr = exit_qualification & CONTROL_REG_ACCESS_NUM;
1876 TRACE_VMEXIT(1,TYPE_MOV_FROM_CR);
1877 TRACE_VMEXIT(2,cr);
1878 TRACE_VMEXIT(3,gp);
1879 mov_from_cr(cr, gp, regs);
1880 break;
1881 case TYPE_CLTS:
1882 TRACE_VMEXIT(1,TYPE_CLTS);
1884 /* We initialise the FPU now, to avoid needing another vmexit. */
1885 setup_fpu(v);
1886 __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_NM);
1888 __vmread_vcpu(v, GUEST_CR0, &value);
1889 value &= ~X86_CR0_TS; /* clear TS */
1890 __vmwrite(GUEST_CR0, value);
1892 __vmread_vcpu(v, CR0_READ_SHADOW, &value);
1893 value &= ~X86_CR0_TS; /* clear TS */
1894 __vmwrite(CR0_READ_SHADOW, value);
1895 break;
1896 case TYPE_LMSW:
1897 TRACE_VMEXIT(1,TYPE_LMSW);
1898 __vmread_vcpu(v, CR0_READ_SHADOW, &value);
1899 value = (value & ~0xF) |
1900 (((exit_qualification & LMSW_SOURCE_DATA) >> 16) & 0xF);
1901 return vmx_set_cr0(value);
1902 break;
1903 default:
1904 __hvm_bug(regs);
1905 break;
1907 return 1;
1910 static inline void vmx_do_msr_read(struct cpu_user_regs *regs)
1912 u64 msr_content = 0;
1913 u32 eax, edx;
1914 struct vcpu *v = current;
1916 HVM_DBG_LOG(DBG_LEVEL_1, "vmx_do_msr_read: ecx=%lx, eax=%lx, edx=%lx",
1917 (unsigned long)regs->ecx, (unsigned long)regs->eax,
1918 (unsigned long)regs->edx);
1919 switch (regs->ecx) {
1920 case MSR_IA32_TIME_STAMP_COUNTER:
1921 msr_content = hvm_get_guest_time(v);
1922 break;
1923 case MSR_IA32_SYSENTER_CS:
1924 __vmread(GUEST_SYSENTER_CS, (u32 *)&msr_content);
1925 break;
1926 case MSR_IA32_SYSENTER_ESP:
1927 __vmread(GUEST_SYSENTER_ESP, &msr_content);
1928 break;
1929 case MSR_IA32_SYSENTER_EIP:
1930 __vmread(GUEST_SYSENTER_EIP, &msr_content);
1931 break;
1932 case MSR_IA32_APICBASE:
1933 msr_content = VLAPIC(v) ? VLAPIC(v)->apic_base_msr : 0;
1934 break;
1935 default:
1936 if (long_mode_do_msr_read(regs))
1937 return;
1939 if ( rdmsr_hypervisor_regs(regs->ecx, &eax, &edx) )
1941 regs->eax = eax;
1942 regs->edx = edx;
1943 return;
1946 rdmsr_safe(regs->ecx, regs->eax, regs->edx);
1947 break;
1950 regs->eax = msr_content & 0xFFFFFFFF;
1951 regs->edx = msr_content >> 32;
1953 HVM_DBG_LOG(DBG_LEVEL_1, "vmx_do_msr_read returns: "
1954 "ecx=%lx, eax=%lx, edx=%lx",
1955 (unsigned long)regs->ecx, (unsigned long)regs->eax,
1956 (unsigned long)regs->edx);
1959 static inline void vmx_do_msr_write(struct cpu_user_regs *regs)
1961 u64 msr_content;
1962 struct vcpu *v = current;
1964 HVM_DBG_LOG(DBG_LEVEL_1, "vmx_do_msr_write: ecx=%lx, eax=%lx, edx=%lx",
1965 (unsigned long)regs->ecx, (unsigned long)regs->eax,
1966 (unsigned long)regs->edx);
1968 msr_content = (regs->eax & 0xFFFFFFFF) | ((u64)regs->edx << 32);
1970 switch (regs->ecx) {
1971 case MSR_IA32_TIME_STAMP_COUNTER:
1972 set_guest_time(v, msr_content);
1973 break;
1974 case MSR_IA32_SYSENTER_CS:
1975 __vmwrite(GUEST_SYSENTER_CS, msr_content);
1976 break;
1977 case MSR_IA32_SYSENTER_ESP:
1978 __vmwrite(GUEST_SYSENTER_ESP, msr_content);
1979 break;
1980 case MSR_IA32_SYSENTER_EIP:
1981 __vmwrite(GUEST_SYSENTER_EIP, msr_content);
1982 break;
1983 case MSR_IA32_APICBASE:
1984 vlapic_msr_set(VLAPIC(v), msr_content);
1985 break;
1986 default:
1987 if ( !long_mode_do_msr_write(regs) )
1988 wrmsr_hypervisor_regs(regs->ecx, regs->eax, regs->edx);
1989 break;
1992 HVM_DBG_LOG(DBG_LEVEL_1, "vmx_do_msr_write returns: "
1993 "ecx=%lx, eax=%lx, edx=%lx",
1994 (unsigned long)regs->ecx, (unsigned long)regs->eax,
1995 (unsigned long)regs->edx);
1998 /*
1999 * Need to use this exit to reschedule
2000 */
2001 void vmx_vmexit_do_hlt(void)
2003 struct vcpu *v=current;
2004 struct periodic_time *pt = &(v->domain->arch.hvm_domain.pl_time.periodic_tm);
2005 s_time_t next_pit=-1,next_wakeup;
2007 if ( !v->vcpu_id )
2008 next_pit = get_scheduled(v, pt->irq, pt);
2009 next_wakeup = get_apictime_scheduled(v);
2010 if ( (next_pit != -1 && next_pit < next_wakeup) || next_wakeup == -1 )
2011 next_wakeup = next_pit;
2012 if ( next_wakeup != - 1 )
2013 set_timer(&current->arch.hvm_vmx.hlt_timer, next_wakeup);
2014 hvm_safe_block();
2017 static inline void vmx_vmexit_do_extint(struct cpu_user_regs *regs)
2019 unsigned int vector;
2020 int error;
2022 asmlinkage void do_IRQ(struct cpu_user_regs *);
2023 fastcall void smp_apic_timer_interrupt(struct cpu_user_regs *);
2024 fastcall void smp_event_check_interrupt(void);
2025 fastcall void smp_invalidate_interrupt(void);
2026 fastcall void smp_call_function_interrupt(void);
2027 fastcall void smp_spurious_interrupt(struct cpu_user_regs *regs);
2028 fastcall void smp_error_interrupt(struct cpu_user_regs *regs);
2029 #ifdef CONFIG_X86_MCE_P4THERMAL
2030 fastcall void smp_thermal_interrupt(struct cpu_user_regs *regs);
2031 #endif
2033 if ((error = __vmread(VM_EXIT_INTR_INFO, &vector))
2034 && !(vector & INTR_INFO_VALID_MASK))
2035 __hvm_bug(regs);
2037 vector &= INTR_INFO_VECTOR_MASK;
2038 TRACE_VMEXIT(1,vector);
2040 switch(vector) {
2041 case LOCAL_TIMER_VECTOR:
2042 smp_apic_timer_interrupt(regs);
2043 break;
2044 case EVENT_CHECK_VECTOR:
2045 smp_event_check_interrupt();
2046 break;
2047 case INVALIDATE_TLB_VECTOR:
2048 smp_invalidate_interrupt();
2049 break;
2050 case CALL_FUNCTION_VECTOR:
2051 smp_call_function_interrupt();
2052 break;
2053 case SPURIOUS_APIC_VECTOR:
2054 smp_spurious_interrupt(regs);
2055 break;
2056 case ERROR_APIC_VECTOR:
2057 smp_error_interrupt(regs);
2058 break;
2059 #ifdef CONFIG_X86_MCE_P4THERMAL
2060 case THERMAL_APIC_VECTOR:
2061 smp_thermal_interrupt(regs);
2062 break;
2063 #endif
2064 default:
2065 regs->entry_vector = vector;
2066 do_IRQ(regs);
2067 break;
2071 #if defined (__x86_64__)
2072 void store_cpu_user_regs(struct cpu_user_regs *regs)
2074 __vmread(GUEST_SS_SELECTOR, &regs->ss);
2075 __vmread(GUEST_RSP, &regs->rsp);
2076 __vmread(GUEST_RFLAGS, &regs->rflags);
2077 __vmread(GUEST_CS_SELECTOR, &regs->cs);
2078 __vmread(GUEST_DS_SELECTOR, &regs->ds);
2079 __vmread(GUEST_ES_SELECTOR, &regs->es);
2080 __vmread(GUEST_RIP, &regs->rip);
2082 #elif defined (__i386__)
2083 void store_cpu_user_regs(struct cpu_user_regs *regs)
2085 __vmread(GUEST_SS_SELECTOR, &regs->ss);
2086 __vmread(GUEST_RSP, &regs->esp);
2087 __vmread(GUEST_RFLAGS, &regs->eflags);
2088 __vmread(GUEST_CS_SELECTOR, &regs->cs);
2089 __vmread(GUEST_DS_SELECTOR, &regs->ds);
2090 __vmread(GUEST_ES_SELECTOR, &regs->es);
2091 __vmread(GUEST_RIP, &regs->eip);
2093 #endif
2095 #ifdef XEN_DEBUGGER
2096 void save_cpu_user_regs(struct cpu_user_regs *regs)
2098 __vmread(GUEST_SS_SELECTOR, &regs->xss);
2099 __vmread(GUEST_RSP, &regs->esp);
2100 __vmread(GUEST_RFLAGS, &regs->eflags);
2101 __vmread(GUEST_CS_SELECTOR, &regs->xcs);
2102 __vmread(GUEST_RIP, &regs->eip);
2104 __vmread(GUEST_GS_SELECTOR, &regs->xgs);
2105 __vmread(GUEST_FS_SELECTOR, &regs->xfs);
2106 __vmread(GUEST_ES_SELECTOR, &regs->xes);
2107 __vmread(GUEST_DS_SELECTOR, &regs->xds);
2110 void restore_cpu_user_regs(struct cpu_user_regs *regs)
2112 __vmwrite(GUEST_SS_SELECTOR, regs->xss);
2113 __vmwrite(GUEST_RSP, regs->esp);
2114 __vmwrite(GUEST_RFLAGS, regs->eflags);
2115 __vmwrite(GUEST_CS_SELECTOR, regs->xcs);
2116 __vmwrite(GUEST_RIP, regs->eip);
2118 __vmwrite(GUEST_GS_SELECTOR, regs->xgs);
2119 __vmwrite(GUEST_FS_SELECTOR, regs->xfs);
2120 __vmwrite(GUEST_ES_SELECTOR, regs->xes);
2121 __vmwrite(GUEST_DS_SELECTOR, regs->xds);
2123 #endif
2125 asmlinkage void vmx_vmexit_handler(struct cpu_user_regs regs)
2127 unsigned int exit_reason;
2128 unsigned long exit_qualification, eip, inst_len = 0;
2129 struct vcpu *v = current;
2130 int error;
2132 error = __vmread(VM_EXIT_REASON, &exit_reason);
2133 BUG_ON(error);
2135 perfc_incra(vmexits, exit_reason);
2137 if ( (exit_reason != EXIT_REASON_EXTERNAL_INTERRUPT) &&
2138 (exit_reason != EXIT_REASON_VMCALL) &&
2139 (exit_reason != EXIT_REASON_IO_INSTRUCTION) )
2140 HVM_DBG_LOG(DBG_LEVEL_0, "exit reason = %x", exit_reason);
2142 if ( exit_reason != EXIT_REASON_EXTERNAL_INTERRUPT )
2143 local_irq_enable();
2145 if ( unlikely(exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) )
2147 unsigned int failed_vmentry_reason = exit_reason & 0xFFFF;
2149 __vmread(EXIT_QUALIFICATION, &exit_qualification);
2150 printk("Failed vm entry (exit reason 0x%x) ", exit_reason);
2151 switch ( failed_vmentry_reason ) {
2152 case EXIT_REASON_INVALID_GUEST_STATE:
2153 printk("caused by invalid guest state (%ld).\n", exit_qualification);
2154 break;
2155 case EXIT_REASON_MSR_LOADING:
2156 printk("caused by MSR entry %ld loading.\n", exit_qualification);
2157 break;
2158 case EXIT_REASON_MACHINE_CHECK:
2159 printk("caused by machine check.\n");
2160 break;
2161 default:
2162 printk("reason not known yet!");
2163 break;
2166 printk("************* VMCS Area **************\n");
2167 vmcs_dump_vcpu();
2168 printk("**************************************\n");
2169 domain_crash_synchronous();
2172 __vmread(GUEST_RIP, &eip);
2173 TRACE_VMEXIT(0,exit_reason);
2175 switch ( exit_reason )
2177 case EXIT_REASON_EXCEPTION_NMI:
2179 /*
2180 * We don't set the software-interrupt exiting (INT n).
2181 * (1) We can get an exception (e.g. #PG) in the guest, or
2182 * (2) NMI
2183 */
2184 int error;
2185 unsigned int vector;
2186 unsigned long va;
2188 if ((error = __vmread(VM_EXIT_INTR_INFO, &vector))
2189 || !(vector & INTR_INFO_VALID_MASK))
2190 __hvm_bug(&regs);
2191 vector &= INTR_INFO_VECTOR_MASK;
2193 TRACE_VMEXIT(1,vector);
2194 perfc_incra(cause_vector, vector);
2196 switch (vector) {
2197 #ifdef XEN_DEBUGGER
2198 case TRAP_debug:
2200 save_cpu_user_regs(&regs);
2201 pdb_handle_exception(1, &regs, 1);
2202 restore_cpu_user_regs(&regs);
2203 break;
2205 case TRAP_int3:
2207 save_cpu_user_regs(&regs);
2208 pdb_handle_exception(3, &regs, 1);
2209 restore_cpu_user_regs(&regs);
2210 break;
2212 #else
2213 case TRAP_debug:
2215 void store_cpu_user_regs(struct cpu_user_regs *regs);
2217 if ( test_bit(_DOMF_debugging, &v->domain->domain_flags) )
2219 store_cpu_user_regs(&regs);
2220 domain_pause_for_debugger();
2221 __vm_clear_bit(GUEST_PENDING_DBG_EXCEPTIONS,
2222 PENDING_DEBUG_EXC_BS);
2224 else
2226 vmx_reflect_exception(v);
2227 __vm_clear_bit(GUEST_PENDING_DBG_EXCEPTIONS,
2228 PENDING_DEBUG_EXC_BS);
2231 break;
2233 case TRAP_int3:
2235 if ( test_bit(_DOMF_debugging, &v->domain->domain_flags) )
2236 domain_pause_for_debugger();
2237 else
2238 vmx_reflect_exception(v);
2239 break;
2241 #endif
2242 case TRAP_no_device:
2244 vmx_do_no_device_fault();
2245 break;
2247 case TRAP_page_fault:
2249 __vmread(EXIT_QUALIFICATION, &va);
2250 __vmread(VM_EXIT_INTR_ERROR_CODE, &regs.error_code);
2252 TRACE_VMEXIT(3,regs.error_code);
2253 TRACE_VMEXIT(4,va);
2255 HVM_DBG_LOG(DBG_LEVEL_VMMU,
2256 "eax=%lx, ebx=%lx, ecx=%lx, edx=%lx, esi=%lx, edi=%lx",
2257 (unsigned long)regs.eax, (unsigned long)regs.ebx,
2258 (unsigned long)regs.ecx, (unsigned long)regs.edx,
2259 (unsigned long)regs.esi, (unsigned long)regs.edi);
2261 if (!(error = vmx_do_page_fault(va, &regs))) {
2262 /*
2263 * Inject #PG using Interruption-Information Fields
2264 */
2265 vmx_inject_hw_exception(v, TRAP_page_fault, regs.error_code);
2266 v->arch.hvm_vmx.cpu_cr2 = va;
2267 TRACE_3D(TRC_VMX_INT, v->domain->domain_id, TRAP_page_fault, va);
2269 break;
2271 case TRAP_nmi:
2272 do_nmi(&regs);
2273 break;
2274 default:
2275 vmx_reflect_exception(v);
2276 break;
2278 break;
2280 case EXIT_REASON_EXTERNAL_INTERRUPT:
2281 vmx_vmexit_do_extint(&regs);
2282 break;
2283 case EXIT_REASON_PENDING_INTERRUPT:
2284 /*
2285 * Not sure exactly what the purpose of this is. The only bits set
2286 * and cleared at this point are CPU_BASED_VIRTUAL_INTR_PENDING.
2287 * (in io.c:{enable,disable}_irq_window(). So presumably we want to
2288 * set it to the original value...
2289 */
2290 v->arch.hvm_vcpu.u.vmx.exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2291 v->arch.hvm_vcpu.u.vmx.exec_control |=
2292 (MONITOR_CPU_BASED_EXEC_CONTROLS & CPU_BASED_VIRTUAL_INTR_PENDING);
2293 __vmwrite(CPU_BASED_VM_EXEC_CONTROL,
2294 v->arch.hvm_vcpu.u.vmx.exec_control);
2295 break;
2296 case EXIT_REASON_TASK_SWITCH:
2297 __hvm_bug(&regs);
2298 break;
2299 case EXIT_REASON_CPUID:
2300 vmx_vmexit_do_cpuid(&regs);
2301 __get_instruction_length(inst_len);
2302 __update_guest_eip(inst_len);
2303 break;
2304 case EXIT_REASON_HLT:
2305 __get_instruction_length(inst_len);
2306 __update_guest_eip(inst_len);
2307 vmx_vmexit_do_hlt();
2308 break;
2309 case EXIT_REASON_INVLPG:
2311 unsigned long va;
2313 __vmread(EXIT_QUALIFICATION, &va);
2314 vmx_vmexit_do_invlpg(va);
2315 __get_instruction_length(inst_len);
2316 __update_guest_eip(inst_len);
2317 break;
2319 case EXIT_REASON_VMCALL:
2321 __get_instruction_length(inst_len);
2322 __vmread(GUEST_RIP, &eip);
2323 __vmread(EXIT_QUALIFICATION, &exit_qualification);
2325 hvm_do_hypercall(&regs);
2326 __update_guest_eip(inst_len);
2327 break;
2329 case EXIT_REASON_CR_ACCESS:
2331 __vmread(GUEST_RIP, &eip);
2332 __get_instruction_length(inst_len);
2333 __vmread(EXIT_QUALIFICATION, &exit_qualification);
2335 HVM_DBG_LOG(DBG_LEVEL_1, "eip = %lx, inst_len =%lx, exit_qualification = %lx",
2336 eip, inst_len, exit_qualification);
2337 if (vmx_cr_access(exit_qualification, &regs))
2338 __update_guest_eip(inst_len);
2339 TRACE_VMEXIT(3,regs.error_code);
2340 TRACE_VMEXIT(4,exit_qualification);
2341 break;
2343 case EXIT_REASON_DR_ACCESS:
2344 __vmread(EXIT_QUALIFICATION, &exit_qualification);
2345 vmx_dr_access(exit_qualification, &regs);
2346 __get_instruction_length(inst_len);
2347 __update_guest_eip(inst_len);
2348 break;
2349 case EXIT_REASON_IO_INSTRUCTION:
2350 __vmread(EXIT_QUALIFICATION, &exit_qualification);
2351 __get_instruction_length(inst_len);
2352 vmx_io_instruction(exit_qualification, inst_len);
2353 TRACE_VMEXIT(4,exit_qualification);
2354 break;
2355 case EXIT_REASON_MSR_READ:
2356 __get_instruction_length(inst_len);
2357 vmx_do_msr_read(&regs);
2358 __update_guest_eip(inst_len);
2359 break;
2360 case EXIT_REASON_MSR_WRITE:
2361 __vmread(GUEST_RIP, &eip);
2362 vmx_do_msr_write(&regs);
2363 __get_instruction_length(inst_len);
2364 __update_guest_eip(inst_len);
2365 break;
2366 case EXIT_REASON_MWAIT_INSTRUCTION:
2367 __hvm_bug(&regs);
2368 break;
2369 case EXIT_REASON_VMCLEAR:
2370 case EXIT_REASON_VMLAUNCH:
2371 case EXIT_REASON_VMPTRLD:
2372 case EXIT_REASON_VMPTRST:
2373 case EXIT_REASON_VMREAD:
2374 case EXIT_REASON_VMRESUME:
2375 case EXIT_REASON_VMWRITE:
2376 case EXIT_REASON_VMOFF:
2377 case EXIT_REASON_VMON:
2378 /* Report invalid opcode exception when a VMX guest tries to execute
2379 any of the VMX instructions */
2380 vmx_inject_hw_exception(v, TRAP_invalid_op, VMX_DELIVER_NO_ERROR_CODE);
2381 break;
2383 default:
2384 __hvm_bug(&regs); /* should not happen */
2388 asmlinkage void vmx_load_cr2(void)
2390 struct vcpu *v = current;
2392 local_irq_disable();
2393 asm volatile("mov %0,%%cr2": :"r" (v->arch.hvm_vmx.cpu_cr2));
2396 asmlinkage void vmx_trace_vmentry (void)
2398 TRACE_5D(TRC_VMX_VMENTRY,
2399 trace_values[smp_processor_id()][0],
2400 trace_values[smp_processor_id()][1],
2401 trace_values[smp_processor_id()][2],
2402 trace_values[smp_processor_id()][3],
2403 trace_values[smp_processor_id()][4]);
2404 TRACE_VMEXIT(0,9);
2405 TRACE_VMEXIT(1,9);
2406 TRACE_VMEXIT(2,9);
2407 TRACE_VMEXIT(3,9);
2408 TRACE_VMEXIT(4,9);
2409 return;
2412 asmlinkage void vmx_trace_vmexit (void)
2414 TRACE_3D(TRC_VMX_VMEXIT,0,0,0);
2415 return;
2418 /*
2419 * Local variables:
2420 * mode: C
2421 * c-set-style: "BSD"
2422 * c-basic-offset: 4
2423 * tab-width: 4
2424 * indent-tabs-mode: nil
2425 * End:
2426 */