Index: sys/amd64/include/vmm.h =================================================================== --- sys/amd64/include/vmm.h (revision 265166) +++ sys/amd64/include/vmm.h (working copy) @@ -33,6 +33,7 @@ VM_SUSPEND_NONE, VM_SUSPEND_RESET, VM_SUSPEND_POWEROFF, + VM_SUSPEND_HALT, VM_SUSPEND_LAST }; Index: sys/amd64/vmm/vmm.c =================================================================== --- sys/amd64/vmm/vmm.c (revision 265166) +++ sys/amd64/vmm/vmm.c (working copy) @@ -142,6 +142,8 @@ int suspend; volatile cpuset_t suspended_cpus; + + volatile cpuset_t halted_cpus; }; static int vmm_initialized; @@ -1006,9 +1008,13 @@ { struct vcpu *vcpu; const char *wmesg; - int t; + int t, vcpu_halted, vm_halted; + KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted")); + vcpu = &vm->vcpu[vcpuid]; + vcpu_halted = 0; + vm_halted = 0; vcpu_lock(vcpu); while (1) { @@ -1032,10 +1038,26 @@ } } - if (vlapic_enabled(vcpu->vlapic)) + /* + * Some Linux guests implement "halt" by having all vcpus + * execute HLT with interrupts disabled. 'halted_cpus' keeps + * track of the vcpus that have entered this state. When all + * vcpus enter the halted state the virtual machine is halted. + */ + if (intr_disabled) { + wmesg = "vmhalt"; + VCPU_CTR0(vm, vcpuid, "Halted"); + if (!vcpu_halted) { + vcpu_halted = 1; + CPU_SET_ATOMIC(vcpuid, &vm->halted_cpus); + } + if (CPU_CMP(&vm->halted_cpus, &vm->active_cpus) == 0) { + vm_halted = 1; + break; + } + } else { wmesg = "vmidle"; - else - wmesg = "vmhalt"; + } t = ticks; vcpu_require_state_locked(vcpu, VCPU_SLEEPING); @@ -1043,8 +1065,15 @@ vcpu_require_state_locked(vcpu, VCPU_FROZEN); vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); } + + if (vcpu_halted) + CPU_CLR_ATOMIC(vcpuid, &vm->halted_cpus); + vcpu_unlock(vcpu); + if (vm_halted) + vm_suspend(vm, VM_SUSPEND_HALT); + return (0); } Index: usr.sbin/bhyve/bhyverun.c =================================================================== --- usr.sbin/bhyve/bhyverun.c (revision 265166) +++ usr.sbin/bhyve/bhyverun.c (working copy) @@ -453,7 +453,6 @@ enum vm_suspend_how how; how = vmexit->u.suspended.how; - assert(how == VM_SUSPEND_RESET || how == VM_SUSPEND_POWEROFF); fbsdrun_deletecpu(ctx, *pvcpu); @@ -470,10 +469,17 @@ } pthread_mutex_unlock(&resetcpu_mtx); - if (how == VM_SUSPEND_RESET) + switch (how) { + case VM_SUSPEND_RESET: exit(0); - if (how == VM_SUSPEND_POWEROFF) + case VM_SUSPEND_POWEROFF: exit(1); + case VM_SUSPEND_HALT: + exit(2); + default: + fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how); + exit(100); + } return (0); /* NOTREACHED */ }