@@ -56,6 +56,7 @@
#define TDX_GET_TD_VM_CALL_INFO 0x10000
#define TDX_REPORT_FATAL_ERROR 0x10003
+#define TDX_INSTRUCTION_HLT 12
#define TDX_INSTRUCTION_IO 30
#define TDX_INSTRUCTION_RDMSR 31
#define TDX_INSTRUCTION_WRMSR 32
@@ -292,6 +293,21 @@ static inline uint64_t tdvmcall_wrmsr(uint64_t index, uint64_t value)
return regs.r10;
}
+/*
+ * Execute HLT instruction.
+ */
+static inline uint64_t tdvmcall_hlt(uint64_t interrupt_blocked_flag)
+{
+ struct kvm_regs regs;
+
+ memset(®s, 0, sizeof(regs));
+ regs.r11 = TDX_INSTRUCTION_HLT;
+ regs.r12 = interrupt_blocked_flag;
+ regs.rcx = 0x1C00;
+ tdcall(®s);
+ return regs.r10;
+}
+
/*
* Reports a 32 bit value from the guest to user space using a TDVM IO call.
* Data is reported on port TDX_DATA_REPORT_PORT.
@@ -883,6 +883,93 @@ void verify_guest_msr_writes(void)
printf("\t ... PASSED\n");
}
+/*
+ * Verifies HLT functionality.
+ */
+TDX_GUEST_FUNCTION(guest_hlt)
+{
+ uint64_t ret;
+ uint64_t interrupt_blocked_flag;
+
+ interrupt_blocked_flag = 0;
+ ret = tdvmcall_hlt(interrupt_blocked_flag);
+ if (ret)
+ tdvmcall_fatal(ret);
+
+ tdvmcall_success();
+}
+
+void _verify_guest_hlt(int signum);
+
+void wake_me(int interval)
+{
+ struct sigaction action;
+
+ action.sa_handler = _verify_guest_hlt;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = 0;
+
+ TEST_ASSERT(sigaction(SIGALRM, &action, NULL) == 0,
+ "Could not set the alarm handler!");
+
+ alarm(interval);
+}
+
+void _verify_guest_hlt(int signum)
+{
+ static struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+
+ /*
+ * This function will also be called by SIGALRM handler to check the
+ * vCPU MP State. If vm has been initialized, then we are in the signal
+ * handler. Check the MP state and let the guest run again.
+ */
+ if (vcpu != NULL) {
+ struct kvm_mp_state mp_state;
+
+ vcpu_mp_state_get(vcpu, &mp_state);
+ ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_HALTED);
+
+ /* Let the guest to run and finish the test.*/
+ mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
+ vcpu_mp_state_set(vcpu, &mp_state);
+ return;
+ }
+
+ printf("Verifying HLT:\n");
+
+ /* Create a TD VM with no memory.*/
+ vm = vm_create_tdx();
+
+ /* Allocate TD guest memory and initialize the TD.*/
+ initialize_td(vm);
+
+ /* Initialize the TD vcpu and copy the test code to the guest memory.*/
+ vcpu = vm_vcpu_add_tdx(vm, 0);
+
+ /* Setup and initialize VM memory */
+ prepare_source_image(vm, guest_hlt,
+ TDX_FUNCTION_SIZE(guest_hlt), 0);
+ finalize_td_memory(vm);
+
+ printf("\t ... Running guest\n");
+
+ /* Wait 1 second for guest to execute HLT */
+ wake_me(1);
+ vcpu_run(vcpu);
+
+ CHECK_GUEST_COMPLETION(vcpu);
+
+ kvm_vm_free(vm);
+ printf("\t ... PASSED\n");
+}
+
+void verify_guest_hlt(void)
+{
+ _verify_guest_hlt(0);
+}
+
int main(int argc, char **argv)
{
if (!is_tdx_enabled()) {
@@ -899,6 +986,7 @@ int main(int argc, char **argv)
run_in_new_process(&verify_guest_reads);
run_in_new_process(&verify_guest_msr_reads);
run_in_new_process(&verify_guest_msr_writes);
+ run_in_new_process(&verify_guest_hlt);
return 0;
}