flds_emulation.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef SELFTEST_KVM_FLDS_EMULATION_H
  3. #define SELFTEST_KVM_FLDS_EMULATION_H
  4. #include "kvm_util.h"
  5. #define FLDS_MEM_EAX ".byte 0xd9, 0x00"
  6. /*
  7. * flds is an instruction that the KVM instruction emulator is known not to
  8. * support. This can be used in guest code along with a mechanism to force
  9. * KVM to emulate the instruction (e.g. by providing an MMIO address) to
  10. * exercise emulation failures.
  11. */
  12. static inline void flds(uint64_t address)
  13. {
  14. __asm__ __volatile__(FLDS_MEM_EAX :: "a"(address));
  15. }
  16. static inline void handle_flds_emulation_failure_exit(struct kvm_vcpu *vcpu)
  17. {
  18. struct kvm_run *run = vcpu->run;
  19. struct kvm_regs regs;
  20. uint8_t *insn_bytes;
  21. uint64_t flags;
  22. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
  23. TEST_ASSERT(run->emulation_failure.suberror == KVM_INTERNAL_ERROR_EMULATION,
  24. "Unexpected suberror: %u",
  25. run->emulation_failure.suberror);
  26. flags = run->emulation_failure.flags;
  27. TEST_ASSERT(run->emulation_failure.ndata >= 3 &&
  28. flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES,
  29. "run->emulation_failure is missing instruction bytes");
  30. TEST_ASSERT(run->emulation_failure.insn_size >= 2,
  31. "Expected a 2-byte opcode for 'flds', got %d bytes",
  32. run->emulation_failure.insn_size);
  33. insn_bytes = run->emulation_failure.insn_bytes;
  34. TEST_ASSERT(insn_bytes[0] == 0xd9 && insn_bytes[1] == 0,
  35. "Expected 'flds [eax]', opcode '0xd9 0x00', got opcode 0x%02x 0x%02x",
  36. insn_bytes[0], insn_bytes[1]);
  37. vcpu_regs_get(vcpu, &regs);
  38. regs.rip += 2;
  39. vcpu_regs_set(vcpu, &regs);
  40. }
  41. #endif /* !SELFTEST_KVM_FLDS_EMULATION_H */