tdx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2021-2022 Intel Corporation */
  3. #undef pr_fmt
  4. #define pr_fmt(fmt) "tdx: " fmt
  5. #include <linux/cpufeature.h>
  6. #include <asm/coco.h>
  7. #include <asm/tdx.h>
  8. #include <asm/vmx.h>
  9. #include <asm/ia32.h>
  10. #include <asm/insn.h>
  11. #include <asm/insn-eval.h>
  12. #include <asm/pgtable.h>
  13. /* TDX module Call Leaf IDs */
  14. #define TDX_GET_INFO 1
  15. #define TDX_GET_VEINFO 3
  16. #define TDX_ACCEPT_PAGE 6
  17. /* TDX hypercall Leaf IDs */
  18. #define TDVMCALL_MAP_GPA 0x10001
  19. /* MMIO direction */
  20. #define EPT_READ 0
  21. #define EPT_WRITE 1
  22. /* Port I/O direction */
  23. #define PORT_READ 0
  24. #define PORT_WRITE 1
  25. /* See Exit Qualification for I/O Instructions in VMX documentation */
  26. #define VE_IS_IO_IN(e) ((e) & BIT(3))
  27. #define VE_GET_IO_SIZE(e) (((e) & GENMASK(2, 0)) + 1)
  28. #define VE_GET_PORT_NUM(e) ((e) >> 16)
  29. #define VE_IS_IO_STRING(e) ((e) & BIT(4))
  30. #define ATTR_SEPT_VE_DISABLE BIT(28)
  31. /*
  32. * Wrapper for standard use of __tdx_hypercall with no output aside from
  33. * return code.
  34. */
  35. static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
  36. {
  37. struct tdx_hypercall_args args = {
  38. .r10 = TDX_HYPERCALL_STANDARD,
  39. .r11 = fn,
  40. .r12 = r12,
  41. .r13 = r13,
  42. .r14 = r14,
  43. .r15 = r15,
  44. };
  45. return __tdx_hypercall(&args, 0);
  46. }
  47. /* Called from __tdx_hypercall() for unrecoverable failure */
  48. void __tdx_hypercall_failed(void)
  49. {
  50. panic("TDVMCALL failed. TDX module bug?");
  51. }
  52. /*
  53. * The TDG.VP.VMCALL-Instruction-execution sub-functions are defined
  54. * independently from but are currently matched 1:1 with VMX EXIT_REASONs.
  55. * Reusing the KVM EXIT_REASON macros makes it easier to connect the host and
  56. * guest sides of these calls.
  57. */
  58. static u64 hcall_func(u64 exit_reason)
  59. {
  60. return exit_reason;
  61. }
  62. #ifdef CONFIG_KVM_GUEST
  63. long tdx_kvm_hypercall(unsigned int nr, unsigned long p1, unsigned long p2,
  64. unsigned long p3, unsigned long p4)
  65. {
  66. struct tdx_hypercall_args args = {
  67. .r10 = nr,
  68. .r11 = p1,
  69. .r12 = p2,
  70. .r13 = p3,
  71. .r14 = p4,
  72. };
  73. return __tdx_hypercall(&args, 0);
  74. }
  75. EXPORT_SYMBOL_GPL(tdx_kvm_hypercall);
  76. #endif
  77. /*
  78. * Used for TDX guests to make calls directly to the TD module. This
  79. * should only be used for calls that have no legitimate reason to fail
  80. * or where the kernel can not survive the call failing.
  81. */
  82. static inline void tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
  83. struct tdx_module_output *out)
  84. {
  85. if (__tdx_module_call(fn, rcx, rdx, r8, r9, out))
  86. panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
  87. }
  88. static void tdx_parse_tdinfo(u64 *cc_mask)
  89. {
  90. struct tdx_module_output out;
  91. unsigned int gpa_width;
  92. u64 td_attr;
  93. /*
  94. * TDINFO TDX module call is used to get the TD execution environment
  95. * information like GPA width, number of available vcpus, debug mode
  96. * information, etc. More details about the ABI can be found in TDX
  97. * Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL
  98. * [TDG.VP.INFO].
  99. */
  100. tdx_module_call(TDX_GET_INFO, 0, 0, 0, 0, &out);
  101. /*
  102. * The highest bit of a guest physical address is the "sharing" bit.
  103. * Set it for shared pages and clear it for private pages.
  104. *
  105. * The GPA width that comes out of this call is critical. TDX guests
  106. * can not meaningfully run without it.
  107. */
  108. gpa_width = out.rcx & GENMASK(5, 0);
  109. *cc_mask = BIT_ULL(gpa_width - 1);
  110. /*
  111. * The kernel can not handle #VE's when accessing normal kernel
  112. * memory. Ensure that no #VE will be delivered for accesses to
  113. * TD-private memory. Only VMM-shared memory (MMIO) will #VE.
  114. */
  115. td_attr = out.rdx;
  116. if (!(td_attr & ATTR_SEPT_VE_DISABLE))
  117. panic("TD misconfiguration: SEPT_VE_DISABLE attibute must be set.\n");
  118. }
  119. /*
  120. * The TDX module spec states that #VE may be injected for a limited set of
  121. * reasons:
  122. *
  123. * - Emulation of the architectural #VE injection on EPT violation;
  124. *
  125. * - As a result of guest TD execution of a disallowed instruction,
  126. * a disallowed MSR access, or CPUID virtualization;
  127. *
  128. * - A notification to the guest TD about anomalous behavior;
  129. *
  130. * The last one is opt-in and is not used by the kernel.
  131. *
  132. * The Intel Software Developer's Manual describes cases when instruction
  133. * length field can be used in section "Information for VM Exits Due to
  134. * Instruction Execution".
  135. *
  136. * For TDX, it ultimately means GET_VEINFO provides reliable instruction length
  137. * information if #VE occurred due to instruction execution, but not for EPT
  138. * violations.
  139. */
  140. static int ve_instr_len(struct ve_info *ve)
  141. {
  142. switch (ve->exit_reason) {
  143. case EXIT_REASON_HLT:
  144. case EXIT_REASON_MSR_READ:
  145. case EXIT_REASON_MSR_WRITE:
  146. case EXIT_REASON_CPUID:
  147. case EXIT_REASON_IO_INSTRUCTION:
  148. /* It is safe to use ve->instr_len for #VE due instructions */
  149. return ve->instr_len;
  150. case EXIT_REASON_EPT_VIOLATION:
  151. /*
  152. * For EPT violations, ve->insn_len is not defined. For those,
  153. * the kernel must decode instructions manually and should not
  154. * be using this function.
  155. */
  156. WARN_ONCE(1, "ve->instr_len is not defined for EPT violations");
  157. return 0;
  158. default:
  159. WARN_ONCE(1, "Unexpected #VE-type: %lld\n", ve->exit_reason);
  160. return ve->instr_len;
  161. }
  162. }
  163. static u64 __cpuidle __halt(const bool irq_disabled, const bool do_sti)
  164. {
  165. struct tdx_hypercall_args args = {
  166. .r10 = TDX_HYPERCALL_STANDARD,
  167. .r11 = hcall_func(EXIT_REASON_HLT),
  168. .r12 = irq_disabled,
  169. };
  170. /*
  171. * Emulate HLT operation via hypercall. More info about ABI
  172. * can be found in TDX Guest-Host-Communication Interface
  173. * (GHCI), section 3.8 TDG.VP.VMCALL<Instruction.HLT>.
  174. *
  175. * The VMM uses the "IRQ disabled" param to understand IRQ
  176. * enabled status (RFLAGS.IF) of the TD guest and to determine
  177. * whether or not it should schedule the halted vCPU if an
  178. * IRQ becomes pending. E.g. if IRQs are disabled, the VMM
  179. * can keep the vCPU in virtual HLT, even if an IRQ is
  180. * pending, without hanging/breaking the guest.
  181. */
  182. return __tdx_hypercall(&args, do_sti ? TDX_HCALL_ISSUE_STI : 0);
  183. }
  184. static int handle_halt(struct ve_info *ve)
  185. {
  186. /*
  187. * Since non safe halt is mainly used in CPU offlining
  188. * and the guest will always stay in the halt state, don't
  189. * call the STI instruction (set do_sti as false).
  190. */
  191. const bool irq_disabled = irqs_disabled();
  192. const bool do_sti = false;
  193. if (__halt(irq_disabled, do_sti))
  194. return -EIO;
  195. return ve_instr_len(ve);
  196. }
  197. void __cpuidle tdx_safe_halt(void)
  198. {
  199. /*
  200. * For do_sti=true case, __tdx_hypercall() function enables
  201. * interrupts using the STI instruction before the TDCALL. So
  202. * set irq_disabled as false.
  203. */
  204. const bool irq_disabled = false;
  205. const bool do_sti = true;
  206. /*
  207. * Use WARN_ONCE() to report the failure.
  208. */
  209. if (__halt(irq_disabled, do_sti))
  210. WARN_ONCE(1, "HLT instruction emulation failed\n");
  211. }
  212. static int read_msr(struct pt_regs *regs, struct ve_info *ve)
  213. {
  214. struct tdx_hypercall_args args = {
  215. .r10 = TDX_HYPERCALL_STANDARD,
  216. .r11 = hcall_func(EXIT_REASON_MSR_READ),
  217. .r12 = regs->cx,
  218. };
  219. /*
  220. * Emulate the MSR read via hypercall. More info about ABI
  221. * can be found in TDX Guest-Host-Communication Interface
  222. * (GHCI), section titled "TDG.VP.VMCALL<Instruction.RDMSR>".
  223. */
  224. if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT))
  225. return -EIO;
  226. regs->ax = lower_32_bits(args.r11);
  227. regs->dx = upper_32_bits(args.r11);
  228. return ve_instr_len(ve);
  229. }
  230. static int write_msr(struct pt_regs *regs, struct ve_info *ve)
  231. {
  232. struct tdx_hypercall_args args = {
  233. .r10 = TDX_HYPERCALL_STANDARD,
  234. .r11 = hcall_func(EXIT_REASON_MSR_WRITE),
  235. .r12 = regs->cx,
  236. .r13 = (u64)regs->dx << 32 | regs->ax,
  237. };
  238. /*
  239. * Emulate the MSR write via hypercall. More info about ABI
  240. * can be found in TDX Guest-Host-Communication Interface
  241. * (GHCI) section titled "TDG.VP.VMCALL<Instruction.WRMSR>".
  242. */
  243. if (__tdx_hypercall(&args, 0))
  244. return -EIO;
  245. return ve_instr_len(ve);
  246. }
  247. static int handle_cpuid(struct pt_regs *regs, struct ve_info *ve)
  248. {
  249. struct tdx_hypercall_args args = {
  250. .r10 = TDX_HYPERCALL_STANDARD,
  251. .r11 = hcall_func(EXIT_REASON_CPUID),
  252. .r12 = regs->ax,
  253. .r13 = regs->cx,
  254. };
  255. /*
  256. * Only allow VMM to control range reserved for hypervisor
  257. * communication.
  258. *
  259. * Return all-zeros for any CPUID outside the range. It matches CPU
  260. * behaviour for non-supported leaf.
  261. */
  262. if (regs->ax < 0x40000000 || regs->ax > 0x4FFFFFFF) {
  263. regs->ax = regs->bx = regs->cx = regs->dx = 0;
  264. return ve_instr_len(ve);
  265. }
  266. /*
  267. * Emulate the CPUID instruction via a hypercall. More info about
  268. * ABI can be found in TDX Guest-Host-Communication Interface
  269. * (GHCI), section titled "VP.VMCALL<Instruction.CPUID>".
  270. */
  271. if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT))
  272. return -EIO;
  273. /*
  274. * As per TDX GHCI CPUID ABI, r12-r15 registers contain contents of
  275. * EAX, EBX, ECX, EDX registers after the CPUID instruction execution.
  276. * So copy the register contents back to pt_regs.
  277. */
  278. regs->ax = args.r12;
  279. regs->bx = args.r13;
  280. regs->cx = args.r14;
  281. regs->dx = args.r15;
  282. return ve_instr_len(ve);
  283. }
  284. static bool mmio_read(int size, unsigned long addr, unsigned long *val)
  285. {
  286. struct tdx_hypercall_args args = {
  287. .r10 = TDX_HYPERCALL_STANDARD,
  288. .r11 = hcall_func(EXIT_REASON_EPT_VIOLATION),
  289. .r12 = size,
  290. .r13 = EPT_READ,
  291. .r14 = addr,
  292. .r15 = *val,
  293. };
  294. if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT))
  295. return false;
  296. *val = args.r11;
  297. return true;
  298. }
  299. static bool mmio_write(int size, unsigned long addr, unsigned long val)
  300. {
  301. return !_tdx_hypercall(hcall_func(EXIT_REASON_EPT_VIOLATION), size,
  302. EPT_WRITE, addr, val);
  303. }
  304. static int handle_mmio(struct pt_regs *regs, struct ve_info *ve)
  305. {
  306. unsigned long *reg, val, vaddr;
  307. char buffer[MAX_INSN_SIZE];
  308. struct insn insn = {};
  309. enum mmio_type mmio;
  310. int size, extend_size;
  311. u8 extend_val = 0;
  312. /* Only in-kernel MMIO is supported */
  313. if (WARN_ON_ONCE(user_mode(regs)))
  314. return -EFAULT;
  315. if (copy_from_kernel_nofault(buffer, (void *)regs->ip, MAX_INSN_SIZE))
  316. return -EFAULT;
  317. if (insn_decode(&insn, buffer, MAX_INSN_SIZE, INSN_MODE_64))
  318. return -EINVAL;
  319. mmio = insn_decode_mmio(&insn, &size);
  320. if (WARN_ON_ONCE(mmio == MMIO_DECODE_FAILED))
  321. return -EINVAL;
  322. if (mmio != MMIO_WRITE_IMM && mmio != MMIO_MOVS) {
  323. reg = insn_get_modrm_reg_ptr(&insn, regs);
  324. if (!reg)
  325. return -EINVAL;
  326. }
  327. /*
  328. * Reject EPT violation #VEs that split pages.
  329. *
  330. * MMIO accesses are supposed to be naturally aligned and therefore
  331. * never cross page boundaries. Seeing split page accesses indicates
  332. * a bug or a load_unaligned_zeropad() that stepped into an MMIO page.
  333. *
  334. * load_unaligned_zeropad() will recover using exception fixups.
  335. */
  336. vaddr = (unsigned long)insn_get_addr_ref(&insn, regs);
  337. if (vaddr / PAGE_SIZE != (vaddr + size - 1) / PAGE_SIZE)
  338. return -EFAULT;
  339. /* Handle writes first */
  340. switch (mmio) {
  341. case MMIO_WRITE:
  342. memcpy(&val, reg, size);
  343. if (!mmio_write(size, ve->gpa, val))
  344. return -EIO;
  345. return insn.length;
  346. case MMIO_WRITE_IMM:
  347. val = insn.immediate.value;
  348. if (!mmio_write(size, ve->gpa, val))
  349. return -EIO;
  350. return insn.length;
  351. case MMIO_READ:
  352. case MMIO_READ_ZERO_EXTEND:
  353. case MMIO_READ_SIGN_EXTEND:
  354. /* Reads are handled below */
  355. break;
  356. case MMIO_MOVS:
  357. case MMIO_DECODE_FAILED:
  358. /*
  359. * MMIO was accessed with an instruction that could not be
  360. * decoded or handled properly. It was likely not using io.h
  361. * helpers or accessed MMIO accidentally.
  362. */
  363. return -EINVAL;
  364. default:
  365. WARN_ONCE(1, "Unknown insn_decode_mmio() decode value?");
  366. return -EINVAL;
  367. }
  368. /* Handle reads */
  369. if (!mmio_read(size, ve->gpa, &val))
  370. return -EIO;
  371. switch (mmio) {
  372. case MMIO_READ:
  373. /* Zero-extend for 32-bit operation */
  374. extend_size = size == 4 ? sizeof(*reg) : 0;
  375. break;
  376. case MMIO_READ_ZERO_EXTEND:
  377. /* Zero extend based on operand size */
  378. extend_size = insn.opnd_bytes;
  379. break;
  380. case MMIO_READ_SIGN_EXTEND:
  381. /* Sign extend based on operand size */
  382. extend_size = insn.opnd_bytes;
  383. if (size == 1 && val & BIT(7))
  384. extend_val = 0xFF;
  385. else if (size > 1 && val & BIT(15))
  386. extend_val = 0xFF;
  387. break;
  388. default:
  389. /* All other cases has to be covered with the first switch() */
  390. WARN_ON_ONCE(1);
  391. return -EINVAL;
  392. }
  393. if (extend_size)
  394. memset(reg, extend_val, extend_size);
  395. memcpy(reg, &val, size);
  396. return insn.length;
  397. }
  398. static bool handle_in(struct pt_regs *regs, int size, int port)
  399. {
  400. struct tdx_hypercall_args args = {
  401. .r10 = TDX_HYPERCALL_STANDARD,
  402. .r11 = hcall_func(EXIT_REASON_IO_INSTRUCTION),
  403. .r12 = size,
  404. .r13 = PORT_READ,
  405. .r14 = port,
  406. };
  407. u64 mask = GENMASK(BITS_PER_BYTE * size, 0);
  408. bool success;
  409. /*
  410. * Emulate the I/O read via hypercall. More info about ABI can be found
  411. * in TDX Guest-Host-Communication Interface (GHCI) section titled
  412. * "TDG.VP.VMCALL<Instruction.IO>".
  413. */
  414. success = !__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT);
  415. /* Update part of the register affected by the emulated instruction */
  416. regs->ax &= ~mask;
  417. if (success)
  418. regs->ax |= args.r11 & mask;
  419. return success;
  420. }
  421. static bool handle_out(struct pt_regs *regs, int size, int port)
  422. {
  423. u64 mask = GENMASK(BITS_PER_BYTE * size, 0);
  424. /*
  425. * Emulate the I/O write via hypercall. More info about ABI can be found
  426. * in TDX Guest-Host-Communication Interface (GHCI) section titled
  427. * "TDG.VP.VMCALL<Instruction.IO>".
  428. */
  429. return !_tdx_hypercall(hcall_func(EXIT_REASON_IO_INSTRUCTION), size,
  430. PORT_WRITE, port, regs->ax & mask);
  431. }
  432. /*
  433. * Emulate I/O using hypercall.
  434. *
  435. * Assumes the IO instruction was using ax, which is enforced
  436. * by the standard io.h macros.
  437. *
  438. * Return True on success or False on failure.
  439. */
  440. static int handle_io(struct pt_regs *regs, struct ve_info *ve)
  441. {
  442. u32 exit_qual = ve->exit_qual;
  443. int size, port;
  444. bool in, ret;
  445. if (VE_IS_IO_STRING(exit_qual))
  446. return -EIO;
  447. in = VE_IS_IO_IN(exit_qual);
  448. size = VE_GET_IO_SIZE(exit_qual);
  449. port = VE_GET_PORT_NUM(exit_qual);
  450. if (in)
  451. ret = handle_in(regs, size, port);
  452. else
  453. ret = handle_out(regs, size, port);
  454. if (!ret)
  455. return -EIO;
  456. return ve_instr_len(ve);
  457. }
  458. /*
  459. * Early #VE exception handler. Only handles a subset of port I/O.
  460. * Intended only for earlyprintk. If failed, return false.
  461. */
  462. __init bool tdx_early_handle_ve(struct pt_regs *regs)
  463. {
  464. struct ve_info ve;
  465. int insn_len;
  466. tdx_get_ve_info(&ve);
  467. if (ve.exit_reason != EXIT_REASON_IO_INSTRUCTION)
  468. return false;
  469. insn_len = handle_io(regs, &ve);
  470. if (insn_len < 0)
  471. return false;
  472. regs->ip += insn_len;
  473. return true;
  474. }
  475. void tdx_get_ve_info(struct ve_info *ve)
  476. {
  477. struct tdx_module_output out;
  478. /*
  479. * Called during #VE handling to retrieve the #VE info from the
  480. * TDX module.
  481. *
  482. * This has to be called early in #VE handling. A "nested" #VE which
  483. * occurs before this will raise a #DF and is not recoverable.
  484. *
  485. * The call retrieves the #VE info from the TDX module, which also
  486. * clears the "#VE valid" flag. This must be done before anything else
  487. * because any #VE that occurs while the valid flag is set will lead to
  488. * #DF.
  489. *
  490. * Note, the TDX module treats virtual NMIs as inhibited if the #VE
  491. * valid flag is set. It means that NMI=>#VE will not result in a #DF.
  492. */
  493. tdx_module_call(TDX_GET_VEINFO, 0, 0, 0, 0, &out);
  494. /* Transfer the output parameters */
  495. ve->exit_reason = out.rcx;
  496. ve->exit_qual = out.rdx;
  497. ve->gla = out.r8;
  498. ve->gpa = out.r9;
  499. ve->instr_len = lower_32_bits(out.r10);
  500. ve->instr_info = upper_32_bits(out.r10);
  501. }
  502. /*
  503. * Handle the user initiated #VE.
  504. *
  505. * On success, returns the number of bytes RIP should be incremented (>=0)
  506. * or -errno on error.
  507. */
  508. static int virt_exception_user(struct pt_regs *regs, struct ve_info *ve)
  509. {
  510. switch (ve->exit_reason) {
  511. case EXIT_REASON_CPUID:
  512. return handle_cpuid(regs, ve);
  513. default:
  514. pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
  515. return -EIO;
  516. }
  517. }
  518. /*
  519. * Handle the kernel #VE.
  520. *
  521. * On success, returns the number of bytes RIP should be incremented (>=0)
  522. * or -errno on error.
  523. */
  524. static int virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve)
  525. {
  526. switch (ve->exit_reason) {
  527. case EXIT_REASON_HLT:
  528. return handle_halt(ve);
  529. case EXIT_REASON_MSR_READ:
  530. return read_msr(regs, ve);
  531. case EXIT_REASON_MSR_WRITE:
  532. return write_msr(regs, ve);
  533. case EXIT_REASON_CPUID:
  534. return handle_cpuid(regs, ve);
  535. case EXIT_REASON_EPT_VIOLATION:
  536. return handle_mmio(regs, ve);
  537. case EXIT_REASON_IO_INSTRUCTION:
  538. return handle_io(regs, ve);
  539. default:
  540. pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
  541. return -EIO;
  542. }
  543. }
  544. bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve)
  545. {
  546. int insn_len;
  547. if (user_mode(regs))
  548. insn_len = virt_exception_user(regs, ve);
  549. else
  550. insn_len = virt_exception_kernel(regs, ve);
  551. if (insn_len < 0)
  552. return false;
  553. /* After successful #VE handling, move the IP */
  554. regs->ip += insn_len;
  555. return true;
  556. }
  557. static bool tdx_tlb_flush_required(bool private)
  558. {
  559. /*
  560. * TDX guest is responsible for flushing TLB on private->shared
  561. * transition. VMM is responsible for flushing on shared->private.
  562. *
  563. * The VMM _can't_ flush private addresses as it can't generate PAs
  564. * with the guest's HKID. Shared memory isn't subject to integrity
  565. * checking, i.e. the VMM doesn't need to flush for its own protection.
  566. *
  567. * There's no need to flush when converting from shared to private,
  568. * as flushing is the VMM's responsibility in this case, e.g. it must
  569. * flush to avoid integrity failures in the face of a buggy or
  570. * malicious guest.
  571. */
  572. return !private;
  573. }
  574. static bool tdx_cache_flush_required(void)
  575. {
  576. /*
  577. * AMD SME/SEV can avoid cache flushing if HW enforces cache coherence.
  578. * TDX doesn't have such capability.
  579. *
  580. * Flush cache unconditionally.
  581. */
  582. return true;
  583. }
  584. static bool try_accept_one(phys_addr_t *start, unsigned long len,
  585. enum pg_level pg_level)
  586. {
  587. unsigned long accept_size = page_level_size(pg_level);
  588. u64 tdcall_rcx;
  589. u8 page_size;
  590. if (!IS_ALIGNED(*start, accept_size))
  591. return false;
  592. if (len < accept_size)
  593. return false;
  594. /*
  595. * Pass the page physical address to the TDX module to accept the
  596. * pending, private page.
  597. *
  598. * Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G.
  599. */
  600. switch (pg_level) {
  601. case PG_LEVEL_4K:
  602. page_size = 0;
  603. break;
  604. case PG_LEVEL_2M:
  605. page_size = 1;
  606. break;
  607. case PG_LEVEL_1G:
  608. page_size = 2;
  609. break;
  610. default:
  611. return false;
  612. }
  613. tdcall_rcx = *start | page_size;
  614. if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL))
  615. return false;
  616. *start += accept_size;
  617. return true;
  618. }
  619. /*
  620. * Inform the VMM of the guest's intent for this physical page: shared with
  621. * the VMM or private to the guest. The VMM is expected to change its mapping
  622. * of the page in response.
  623. */
  624. static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
  625. {
  626. phys_addr_t start = __pa(vaddr);
  627. phys_addr_t end = __pa(vaddr + numpages * PAGE_SIZE);
  628. if (!enc) {
  629. /* Set the shared (decrypted) bits: */
  630. start |= cc_mkdec(0);
  631. end |= cc_mkdec(0);
  632. }
  633. /*
  634. * Notify the VMM about page mapping conversion. More info about ABI
  635. * can be found in TDX Guest-Host-Communication Interface (GHCI),
  636. * section "TDG.VP.VMCALL<MapGPA>"
  637. */
  638. if (_tdx_hypercall(TDVMCALL_MAP_GPA, start, end - start, 0, 0))
  639. return false;
  640. /* private->shared conversion requires only MapGPA call */
  641. if (!enc)
  642. return true;
  643. /*
  644. * For shared->private conversion, accept the page using
  645. * TDX_ACCEPT_PAGE TDX module call.
  646. */
  647. while (start < end) {
  648. unsigned long len = end - start;
  649. /*
  650. * Try larger accepts first. It gives chance to VMM to keep
  651. * 1G/2M SEPT entries where possible and speeds up process by
  652. * cutting number of hypercalls (if successful).
  653. */
  654. if (try_accept_one(&start, len, PG_LEVEL_1G))
  655. continue;
  656. if (try_accept_one(&start, len, PG_LEVEL_2M))
  657. continue;
  658. if (!try_accept_one(&start, len, PG_LEVEL_4K))
  659. return false;
  660. }
  661. return true;
  662. }
  663. static bool tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
  664. bool enc)
  665. {
  666. /*
  667. * Only handle shared->private conversion here.
  668. * See the comment in tdx_early_init().
  669. */
  670. if (enc)
  671. return tdx_enc_status_changed(vaddr, numpages, enc);
  672. return true;
  673. }
  674. static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
  675. bool enc)
  676. {
  677. /*
  678. * Only handle private->shared conversion here.
  679. * See the comment in tdx_early_init().
  680. */
  681. if (!enc)
  682. return tdx_enc_status_changed(vaddr, numpages, enc);
  683. return true;
  684. }
  685. void __init tdx_early_init(void)
  686. {
  687. u64 cc_mask;
  688. u32 eax, sig[3];
  689. cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]);
  690. if (memcmp(TDX_IDENT, sig, sizeof(sig)))
  691. return;
  692. setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
  693. cc_set_vendor(CC_VENDOR_INTEL);
  694. tdx_parse_tdinfo(&cc_mask);
  695. cc_set_mask(cc_mask);
  696. /*
  697. * All bits above GPA width are reserved and kernel treats shared bit
  698. * as flag, not as part of physical address.
  699. *
  700. * Adjust physical mask to only cover valid GPA bits.
  701. */
  702. physical_mask &= cc_mask - 1;
  703. /*
  704. * The kernel mapping should match the TDX metadata for the page.
  705. * load_unaligned_zeropad() can touch memory *adjacent* to that which is
  706. * owned by the caller and can catch even _momentary_ mismatches. Bad
  707. * things happen on mismatch:
  708. *
  709. * - Private mapping => Shared Page == Guest shutdown
  710. * - Shared mapping => Private Page == Recoverable #VE
  711. *
  712. * guest.enc_status_change_prepare() converts the page from
  713. * shared=>private before the mapping becomes private.
  714. *
  715. * guest.enc_status_change_finish() converts the page from
  716. * private=>shared after the mapping becomes private.
  717. *
  718. * In both cases there is a temporary shared mapping to a private page,
  719. * which can result in a #VE. But, there is never a private mapping to
  720. * a shared page.
  721. */
  722. x86_platform.guest.enc_status_change_prepare = tdx_enc_status_change_prepare;
  723. x86_platform.guest.enc_status_change_finish = tdx_enc_status_change_finish;
  724. x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required;
  725. x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required;
  726. pr_info("Guest detected\n");
  727. }