diff options
author | neel <neel@FreeBSD.org> | 2014-02-20 01:48:25 +0000 |
---|---|---|
committer | neel <neel@FreeBSD.org> | 2014-02-20 01:48:25 +0000 |
commit | 4626d164b84dea9d2eb544118119180724c0f483 (patch) | |
tree | 37dff768027a85ed7316d1b5ebfad48a7dc4cc6e /sys/amd64/vmm | |
parent | ef25a6c63e4275bebb4cbeaf0ff6dfd32136d879 (diff) | |
download | FreeBSD-src-4626d164b84dea9d2eb544118119180724c0f483.zip FreeBSD-src-4626d164b84dea9d2eb544118119180724c0f483.tar.gz |
Simplify APIC mode switching from MMIO to x2APIC. In part this is done to
simplify the implementation of the x2APIC virtualization assist in VT-x.
Prior to this change the vlapic allowed the guest to change its mode from
xAPIC to x2APIC. We don't allow that any more and the vlapic mode is locked
when the virtual machine is created. This is not very constraining because
operating systems already have to deal with BIOS setting up the APIC in
x2APIC mode at boot.
Fix a bug in the CPUID emulation where the x2APIC capability was leaking
from the host to the guest.
Ignore MMIO reads and writes to the vlapic in x2APIC mode. Similarly, ignore
MSR accesses to the vlapic when it is in xAPIC mode.
The default configuration of the vlapic is xAPIC. The "-x" option to bhyve(8)
can be used to change the mode to x2APIC instead.
Discussed with: grehan@
Diffstat (limited to 'sys/amd64/vmm')
-rw-r--r-- | sys/amd64/vmm/io/vlapic.c | 101 | ||||
-rw-r--r-- | sys/amd64/vmm/io/vlapic.h | 10 | ||||
-rw-r--r-- | sys/amd64/vmm/vmm.c | 2 | ||||
-rw-r--r-- | sys/amd64/vmm/vmm_lapic.c | 11 | ||||
-rw-r--r-- | sys/amd64/vmm/x86.c | 2 |
5 files changed, 80 insertions, 46 deletions
diff --git a/sys/amd64/vmm/io/vlapic.c b/sys/amd64/vmm/io/vlapic.c index f855f73..d1f7234 100644 --- a/sys/amd64/vmm/io/vlapic.c +++ b/sys/amd64/vmm/io/vlapic.c @@ -1119,12 +1119,31 @@ vlapic_svr_write_handler(struct vlapic *vlapic) } int -vlapic_read(struct vlapic *vlapic, uint64_t offset, uint64_t *data, bool *retu) +vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, + uint64_t *data, bool *retu) { struct LAPIC *lapic = vlapic->apic_page; uint32_t *reg; int i; + /* Ignore MMIO accesses in x2APIC mode */ + if (x2apic(vlapic) && mmio_access) { + VLAPIC_CTR1(vlapic, "MMIO read from offset %#lx in x2APIC mode", + offset); + *data = 0; + goto done; + } + + if (!x2apic(vlapic) && !mmio_access) { + /* + * XXX Generate GP fault for MSR accesses in xAPIC mode + */ + VLAPIC_CTR1(vlapic, "x2APIC MSR read from offset %#lx in " + "xAPIC mode", offset); + *data = 0; + goto done; + } + if (offset > sizeof(*lapic)) { *data = 0; goto done; @@ -1221,7 +1240,8 @@ done: } int -vlapic_write(struct vlapic *vlapic, uint64_t offset, uint64_t data, bool *retu) +vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, + uint64_t data, bool *retu) { struct LAPIC *lapic = vlapic->apic_page; uint32_t *regptr; @@ -1230,10 +1250,26 @@ vlapic_write(struct vlapic *vlapic, uint64_t offset, uint64_t data, bool *retu) KASSERT((offset & 0xf) == 0 && offset < PAGE_SIZE, ("vlapic_write: invalid offset %#lx", offset)); - VLAPIC_CTR2(vlapic, "vlapic write offset %#x, data %#lx", offset, data); + VLAPIC_CTR2(vlapic, "vlapic write offset %#lx, data %#lx", + offset, data); - if (offset > sizeof(*lapic)) { - return 0; + if (offset > sizeof(*lapic)) + return (0); + + /* Ignore MMIO accesses in x2APIC mode */ + if (x2apic(vlapic) && mmio_access) { + VLAPIC_CTR2(vlapic, "MMIO write of %#lx to offset %#lx " + "in x2APIC mode", data, offset); + return (0); + } + + /* + * XXX Generate GP fault for MSR accesses in xAPIC mode + */ + if (!x2apic(vlapic) && !mmio_access) { + VLAPIC_CTR2(vlapic, "x2APIC MSR write of %#lx to offset %#lx " + "in xAPIC mode", data, offset); + return (0); } retval = 0; @@ -1380,50 +1416,47 @@ vlapic_get_apicbase(struct vlapic *vlapic) return (vlapic->msr_apicbase); } -void +int vlapic_set_apicbase(struct vlapic *vlapic, uint64_t new) { - struct LAPIC *lapic; - enum x2apic_state state; - uint64_t old; - int err; - - err = vm_get_x2apic_state(vlapic->vm, vlapic->vcpuid, &state); - if (err) - panic("vlapic_set_apicbase: err %d fetching x2apic state", err); - if (state == X2APIC_DISABLED) - new &= ~APICBASE_X2APIC; - - old = vlapic->msr_apicbase; - vlapic->msr_apicbase = new; - - /* - * If the vlapic is switching between xAPIC and x2APIC modes then - * reset the mode-dependent registers. - */ - if ((old ^ new) & APICBASE_X2APIC) { - lapic = vlapic->apic_page; - lapic->id = vlapic_get_id(vlapic); - if (x2apic(vlapic)) { - lapic->ldr = x2apic_ldr(vlapic); - lapic->dfr = 0; - } else { - lapic->ldr = 0; - lapic->dfr = 0xffffffff; - } + if (vlapic->msr_apicbase != new) { + VLAPIC_CTR2(vlapic, "Changing APIC_BASE MSR from %#lx to %#lx " + "not supported", vlapic->msr_apicbase, new); + return (-1); } + + return (0); } void vlapic_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) { struct vlapic *vlapic; + struct LAPIC *lapic; vlapic = vm_lapic(vm, vcpuid); if (state == X2APIC_DISABLED) vlapic->msr_apicbase &= ~APICBASE_X2APIC; + else + vlapic->msr_apicbase |= APICBASE_X2APIC; + + /* + * Reset the local APIC registers whose values are mode-dependent. + * + * XXX this works because the APIC mode can be changed only at vcpu + * initialization time. + */ + lapic = vlapic->apic_page; + lapic->id = vlapic_get_id(vlapic); + if (x2apic(vlapic)) { + lapic->ldr = x2apic_ldr(vlapic); + lapic->dfr = 0; + } else { + lapic->ldr = 0; + lapic->dfr = 0xffffffff; + } } void diff --git a/sys/amd64/vmm/io/vlapic.h b/sys/amd64/vmm/io/vlapic.h index d2fc6d9..b215e57 100644 --- a/sys/amd64/vmm/io/vlapic.h +++ b/sys/amd64/vmm/io/vlapic.h @@ -32,10 +32,10 @@ struct vm; enum x2apic_state; -int vlapic_write(struct vlapic *vlapic, uint64_t offset, uint64_t data, - bool *retu); -int vlapic_read(struct vlapic *vlapic, uint64_t offset, uint64_t *data, - bool *retu); +int vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, + uint64_t data, bool *retu); +int vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, + uint64_t *data, bool *retu); /* * Returns 0 if there is no eligible vector that can be delivered to the @@ -74,7 +74,7 @@ void vlapic_fire_cmci(struct vlapic *vlapic); int vlapic_trigger_lvt(struct vlapic *vlapic, int vector); uint64_t vlapic_get_apicbase(struct vlapic *vlapic); -void vlapic_set_apicbase(struct vlapic *vlapic, uint64_t val); +int vlapic_set_apicbase(struct vlapic *vlapic, uint64_t val); void vlapic_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state s); bool vlapic_enabled(struct vlapic *vlapic); diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c index 4ae6915..80ff5be 100644 --- a/sys/amd64/vmm/vmm.c +++ b/sys/amd64/vmm/vmm.c @@ -206,7 +206,7 @@ vcpu_init(struct vm *vm, uint32_t vcpu_id) vcpu->hostcpu = NOCPU; vcpu->vcpuid = vcpu_id; vcpu->vlapic = VLAPIC_INIT(vm->cookie, vcpu_id); - vm_set_x2apic_state(vm, vcpu_id, X2APIC_ENABLED); + vm_set_x2apic_state(vm, vcpu_id, X2APIC_DISABLED); vcpu->guest_xcr0 = XFEATURE_ENABLED_X87; vcpu->guestfpu = fpu_save_area_alloc(); fpu_save_area_reset(vcpu->guestfpu); diff --git a/sys/amd64/vmm/vmm_lapic.c b/sys/amd64/vmm/vmm_lapic.c index 47e04da..640c779 100644 --- a/sys/amd64/vmm/vmm_lapic.c +++ b/sys/amd64/vmm/vmm_lapic.c @@ -172,7 +172,7 @@ lapic_rdmsr(struct vm *vm, int cpu, u_int msr, uint64_t *rval, bool *retu) error = 0; } else { offset = x2apic_msr_to_regoff(msr); - error = vlapic_read(vlapic, offset, rval, retu); + error = vlapic_read(vlapic, 0, offset, rval, retu); } return (error); @@ -188,11 +188,10 @@ lapic_wrmsr(struct vm *vm, int cpu, u_int msr, uint64_t val, bool *retu) vlapic = vm_lapic(vm, cpu); if (msr == MSR_APICBASE) { - vlapic_set_apicbase(vlapic, val); - error = 0; + error = vlapic_set_apicbase(vlapic, val); } else { offset = x2apic_msr_to_regoff(msr); - error = vlapic_write(vlapic, offset, val, retu); + error = vlapic_write(vlapic, 0, offset, val, retu); } return (error); @@ -216,7 +215,7 @@ lapic_mmio_write(void *vm, int cpu, uint64_t gpa, uint64_t wval, int size, return (EINVAL); vlapic = vm_lapic(vm, cpu); - error = vlapic_write(vlapic, off, wval, arg); + error = vlapic_write(vlapic, 1, off, wval, arg); return (error); } @@ -238,6 +237,6 @@ lapic_mmio_read(void *vm, int cpu, uint64_t gpa, uint64_t *rval, int size, return (EINVAL); vlapic = vm_lapic(vm, cpu); - error = vlapic_read(vlapic, off, rval, arg); + error = vlapic_read(vlapic, 1, off, rval, arg); return (error); } diff --git a/sys/amd64/vmm/x86.c b/sys/amd64/vmm/x86.c index 7ae32ec..d3a0248 100644 --- a/sys/amd64/vmm/x86.c +++ b/sys/amd64/vmm/x86.c @@ -149,6 +149,8 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, if (x2apic_state != X2APIC_DISABLED) regs[2] |= CPUID2_X2APIC; + else + regs[2] &= ~CPUID2_X2APIC; /* * Only advertise CPUID2_XSAVE in the guest if |