tdx.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "../cpuflags.h"
  3. #include "../string.h"
  4. #include "../io.h"
  5. #include "error.h"
  6. #include <vdso/limits.h>
  7. #include <uapi/asm/vmx.h>
  8. #include <asm/shared/tdx.h>
  9. /* Called from __tdx_hypercall() for unrecoverable failure */
  10. void __tdx_hypercall_failed(void)
  11. {
  12. error("TDVMCALL failed. TDX module bug?");
  13. }
  14. static inline unsigned int tdx_io_in(int size, u16 port)
  15. {
  16. struct tdx_hypercall_args args = {
  17. .r10 = TDX_HYPERCALL_STANDARD,
  18. .r11 = EXIT_REASON_IO_INSTRUCTION,
  19. .r12 = size,
  20. .r13 = 0,
  21. .r14 = port,
  22. };
  23. if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT))
  24. return UINT_MAX;
  25. return args.r11;
  26. }
  27. static inline void tdx_io_out(int size, u16 port, u32 value)
  28. {
  29. struct tdx_hypercall_args args = {
  30. .r10 = TDX_HYPERCALL_STANDARD,
  31. .r11 = EXIT_REASON_IO_INSTRUCTION,
  32. .r12 = size,
  33. .r13 = 1,
  34. .r14 = port,
  35. .r15 = value,
  36. };
  37. __tdx_hypercall(&args, 0);
  38. }
  39. static inline u8 tdx_inb(u16 port)
  40. {
  41. return tdx_io_in(1, port);
  42. }
  43. static inline void tdx_outb(u8 value, u16 port)
  44. {
  45. tdx_io_out(1, port, value);
  46. }
  47. static inline void tdx_outw(u16 value, u16 port)
  48. {
  49. tdx_io_out(2, port, value);
  50. }
  51. void early_tdx_detect(void)
  52. {
  53. u32 eax, sig[3];
  54. cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]);
  55. if (memcmp(TDX_IDENT, sig, sizeof(sig)))
  56. return;
  57. /* Use hypercalls instead of I/O instructions */
  58. pio_ops.f_inb = tdx_inb;
  59. pio_ops.f_outb = tdx_outb;
  60. pio_ops.f_outw = tdx_outw;
  61. }