ivm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyper-V Isolation VM interface with paravisor and hypervisor
  4. *
  5. * Author:
  6. * Tianyu Lan <Tianyu.Lan@microsoft.com>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/hyperv.h>
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/cpu.h>
  13. #include <asm/svm.h>
  14. #include <asm/sev.h>
  15. #include <asm/io.h>
  16. #include <asm/coco.h>
  17. #include <asm/mem_encrypt.h>
  18. #include <asm/set_memory.h>
  19. #include <asm/mshyperv.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/mtrr.h>
  22. #include <asm/io_apic.h>
  23. #include <asm/realmode.h>
  24. #include <asm/e820/api.h>
  25. #include <asm/desc.h>
  26. #include <uapi/asm/vmx.h>
  27. #ifdef CONFIG_AMD_MEM_ENCRYPT
  28. #define GHCB_USAGE_HYPERV_CALL 1
  29. union hv_ghcb {
  30. struct ghcb ghcb;
  31. struct {
  32. u64 hypercalldata[509];
  33. u64 outputgpa;
  34. union {
  35. union {
  36. struct {
  37. u32 callcode : 16;
  38. u32 isfast : 1;
  39. u32 reserved1 : 14;
  40. u32 isnested : 1;
  41. u32 countofelements : 12;
  42. u32 reserved2 : 4;
  43. u32 repstartindex : 12;
  44. u32 reserved3 : 4;
  45. };
  46. u64 asuint64;
  47. } hypercallinput;
  48. union {
  49. struct {
  50. u16 callstatus;
  51. u16 reserved1;
  52. u32 elementsprocessed : 12;
  53. u32 reserved2 : 20;
  54. };
  55. u64 asunit64;
  56. } hypercalloutput;
  57. };
  58. u64 reserved2;
  59. } hypercall;
  60. } __packed __aligned(HV_HYP_PAGE_SIZE);
  61. /* Only used in an SNP VM with the paravisor */
  62. static u16 hv_ghcb_version __ro_after_init;
  63. /* Functions only used in an SNP VM with the paravisor go here. */
  64. u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
  65. {
  66. union hv_ghcb *hv_ghcb;
  67. void **ghcb_base;
  68. unsigned long flags;
  69. u64 status;
  70. if (!hv_ghcb_pg)
  71. return -EFAULT;
  72. WARN_ON(in_nmi());
  73. local_irq_save(flags);
  74. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  75. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  76. if (!hv_ghcb) {
  77. local_irq_restore(flags);
  78. return -EFAULT;
  79. }
  80. hv_ghcb->ghcb.protocol_version = GHCB_PROTOCOL_MAX;
  81. hv_ghcb->ghcb.ghcb_usage = GHCB_USAGE_HYPERV_CALL;
  82. hv_ghcb->hypercall.outputgpa = (u64)output;
  83. hv_ghcb->hypercall.hypercallinput.asuint64 = 0;
  84. hv_ghcb->hypercall.hypercallinput.callcode = control;
  85. if (input_size)
  86. memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size);
  87. VMGEXIT();
  88. hv_ghcb->ghcb.ghcb_usage = 0xffffffff;
  89. memset(hv_ghcb->ghcb.save.valid_bitmap, 0,
  90. sizeof(hv_ghcb->ghcb.save.valid_bitmap));
  91. status = hv_ghcb->hypercall.hypercalloutput.callstatus;
  92. local_irq_restore(flags);
  93. return status;
  94. }
  95. static inline u64 rd_ghcb_msr(void)
  96. {
  97. return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
  98. }
  99. static inline void wr_ghcb_msr(u64 val)
  100. {
  101. native_wrmsrl(MSR_AMD64_SEV_ES_GHCB, val);
  102. }
  103. static enum es_result hv_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code,
  104. u64 exit_info_1, u64 exit_info_2)
  105. {
  106. /* Fill in protocol and format specifiers */
  107. ghcb->protocol_version = hv_ghcb_version;
  108. ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
  109. ghcb_set_sw_exit_code(ghcb, exit_code);
  110. ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
  111. ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
  112. VMGEXIT();
  113. if (ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0))
  114. return ES_VMM_ERROR;
  115. else
  116. return ES_OK;
  117. }
  118. void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason)
  119. {
  120. u64 val = GHCB_MSR_TERM_REQ;
  121. /* Tell the hypervisor what went wrong. */
  122. val |= GHCB_SEV_TERM_REASON(set, reason);
  123. /* Request Guest Termination from Hypervisor */
  124. wr_ghcb_msr(val);
  125. VMGEXIT();
  126. while (true)
  127. asm volatile("hlt\n" : : : "memory");
  128. }
  129. bool hv_ghcb_negotiate_protocol(void)
  130. {
  131. u64 ghcb_gpa;
  132. u64 val;
  133. /* Save ghcb page gpa. */
  134. ghcb_gpa = rd_ghcb_msr();
  135. /* Do the GHCB protocol version negotiation */
  136. wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
  137. VMGEXIT();
  138. val = rd_ghcb_msr();
  139. if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
  140. return false;
  141. if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
  142. GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
  143. return false;
  144. hv_ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val),
  145. GHCB_PROTOCOL_MAX);
  146. /* Write ghcb page back after negotiating protocol. */
  147. wr_ghcb_msr(ghcb_gpa);
  148. VMGEXIT();
  149. return true;
  150. }
  151. static void hv_ghcb_msr_write(u64 msr, u64 value)
  152. {
  153. union hv_ghcb *hv_ghcb;
  154. void **ghcb_base;
  155. unsigned long flags;
  156. if (!hv_ghcb_pg)
  157. return;
  158. WARN_ON(in_nmi());
  159. local_irq_save(flags);
  160. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  161. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  162. if (!hv_ghcb) {
  163. local_irq_restore(flags);
  164. return;
  165. }
  166. ghcb_set_rcx(&hv_ghcb->ghcb, msr);
  167. ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value));
  168. ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value));
  169. if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 1, 0))
  170. pr_warn("Fail to write msr via ghcb %llx.\n", msr);
  171. local_irq_restore(flags);
  172. }
  173. static void hv_ghcb_msr_read(u64 msr, u64 *value)
  174. {
  175. union hv_ghcb *hv_ghcb;
  176. void **ghcb_base;
  177. unsigned long flags;
  178. /* Check size of union hv_ghcb here. */
  179. BUILD_BUG_ON(sizeof(union hv_ghcb) != HV_HYP_PAGE_SIZE);
  180. if (!hv_ghcb_pg)
  181. return;
  182. WARN_ON(in_nmi());
  183. local_irq_save(flags);
  184. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  185. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  186. if (!hv_ghcb) {
  187. local_irq_restore(flags);
  188. return;
  189. }
  190. ghcb_set_rcx(&hv_ghcb->ghcb, msr);
  191. if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0))
  192. pr_warn("Fail to read msr via ghcb %llx.\n", msr);
  193. else
  194. *value = (u64)lower_32_bits(hv_ghcb->ghcb.save.rax)
  195. | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32);
  196. local_irq_restore(flags);
  197. }
  198. /* Only used in a fully enlightened SNP VM, i.e. without the paravisor */
  199. static u8 ap_start_input_arg[PAGE_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
  200. static u8 ap_start_stack[PAGE_SIZE] __aligned(PAGE_SIZE);
  201. static DEFINE_PER_CPU(struct sev_es_save_area *, hv_sev_vmsa);
  202. /* Functions only used in an SNP VM without the paravisor go here. */
  203. #define hv_populate_vmcb_seg(seg, gdtr_base) \
  204. do { \
  205. if (seg.selector) { \
  206. seg.base = 0; \
  207. seg.limit = HV_AP_SEGMENT_LIMIT; \
  208. seg.attrib = *(u16 *)(gdtr_base + seg.selector + 5); \
  209. seg.attrib = (seg.attrib & 0xFF) | ((seg.attrib >> 4) & 0xF00); \
  210. } \
  211. } while (0) \
  212. static int snp_set_vmsa(void *va, bool vmsa)
  213. {
  214. u64 attrs;
  215. /*
  216. * Running at VMPL0 allows the kernel to change the VMSA bit for a page
  217. * using the RMPADJUST instruction. However, for the instruction to
  218. * succeed it must target the permissions of a lesser privileged
  219. * (higher numbered) VMPL level, so use VMPL1 (refer to the RMPADJUST
  220. * instruction in the AMD64 APM Volume 3).
  221. */
  222. attrs = 1;
  223. if (vmsa)
  224. attrs |= RMPADJUST_VMSA_PAGE_BIT;
  225. return rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs);
  226. }
  227. static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
  228. {
  229. int err;
  230. err = snp_set_vmsa(vmsa, false);
  231. if (err)
  232. pr_err("clear VMSA page failed (%u), leaking page\n", err);
  233. else
  234. free_page((unsigned long)vmsa);
  235. }
  236. int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip)
  237. {
  238. struct sev_es_save_area *vmsa = (struct sev_es_save_area *)
  239. __get_free_page(GFP_KERNEL | __GFP_ZERO);
  240. struct sev_es_save_area *cur_vmsa;
  241. struct desc_ptr gdtr;
  242. u64 ret, retry = 5;
  243. struct hv_enable_vp_vtl *start_vp_input;
  244. unsigned long flags;
  245. int cpu, vp_index;
  246. if (!vmsa)
  247. return -ENOMEM;
  248. /* Find the Hyper-V VP index which might be not the same as APIC ID */
  249. vp_index = hv_apicid_to_vp_index(apic_id);
  250. if (vp_index < 0 || vp_index > ms_hyperv.max_vp_index)
  251. return -EINVAL;
  252. /*
  253. * Find the Linux CPU number for addressing the per-CPU data, and it
  254. * might not be the same as APIC ID.
  255. */
  256. for_each_present_cpu(cpu) {
  257. if (arch_match_cpu_phys_id(cpu, apic_id))
  258. break;
  259. }
  260. if (cpu >= nr_cpu_ids)
  261. return -EINVAL;
  262. native_store_gdt(&gdtr);
  263. vmsa->gdtr.base = gdtr.address;
  264. vmsa->gdtr.limit = gdtr.size;
  265. asm volatile("movl %%es, %%eax;" : "=a" (vmsa->es.selector));
  266. hv_populate_vmcb_seg(vmsa->es, vmsa->gdtr.base);
  267. asm volatile("movl %%cs, %%eax;" : "=a" (vmsa->cs.selector));
  268. hv_populate_vmcb_seg(vmsa->cs, vmsa->gdtr.base);
  269. asm volatile("movl %%ss, %%eax;" : "=a" (vmsa->ss.selector));
  270. hv_populate_vmcb_seg(vmsa->ss, vmsa->gdtr.base);
  271. asm volatile("movl %%ds, %%eax;" : "=a" (vmsa->ds.selector));
  272. hv_populate_vmcb_seg(vmsa->ds, vmsa->gdtr.base);
  273. vmsa->efer = native_read_msr(MSR_EFER);
  274. vmsa->cr4 = native_read_cr4();
  275. vmsa->cr3 = __native_read_cr3();
  276. vmsa->cr0 = native_read_cr0();
  277. vmsa->xcr0 = 1;
  278. vmsa->g_pat = HV_AP_INIT_GPAT_DEFAULT;
  279. vmsa->rip = (u64)secondary_startup_64_no_verify;
  280. vmsa->rsp = (u64)&ap_start_stack[PAGE_SIZE];
  281. /*
  282. * Set the SNP-specific fields for this VMSA:
  283. * VMPL level
  284. * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits)
  285. */
  286. vmsa->vmpl = 0;
  287. vmsa->sev_features = sev_status >> 2;
  288. ret = snp_set_vmsa(vmsa, true);
  289. if (ret) {
  290. pr_err("RMPADJUST(%llx) failed: %llx\n", (u64)vmsa, ret);
  291. free_page((u64)vmsa);
  292. return ret;
  293. }
  294. local_irq_save(flags);
  295. start_vp_input = (struct hv_enable_vp_vtl *)ap_start_input_arg;
  296. memset(start_vp_input, 0, sizeof(*start_vp_input));
  297. start_vp_input->partition_id = -1;
  298. start_vp_input->vp_index = vp_index;
  299. start_vp_input->target_vtl.target_vtl = ms_hyperv.vtl;
  300. *(u64 *)&start_vp_input->vp_context = __pa(vmsa) | 1;
  301. do {
  302. ret = hv_do_hypercall(HVCALL_START_VP,
  303. start_vp_input, NULL);
  304. } while (hv_result(ret) == HV_STATUS_TIME_OUT && retry--);
  305. local_irq_restore(flags);
  306. if (!hv_result_success(ret)) {
  307. pr_err("HvCallStartVirtualProcessor failed: %llx\n", ret);
  308. snp_cleanup_vmsa(vmsa);
  309. vmsa = NULL;
  310. }
  311. cur_vmsa = per_cpu(hv_sev_vmsa, cpu);
  312. /* Free up any previous VMSA page */
  313. if (cur_vmsa)
  314. snp_cleanup_vmsa(cur_vmsa);
  315. /* Record the current VMSA page */
  316. per_cpu(hv_sev_vmsa, cpu) = vmsa;
  317. return ret;
  318. }
  319. #else
  320. static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
  321. static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
  322. #endif /* CONFIG_AMD_MEM_ENCRYPT */
  323. #ifdef CONFIG_INTEL_TDX_GUEST
  324. static void hv_tdx_msr_write(u64 msr, u64 val)
  325. {
  326. struct tdx_module_args args = {
  327. .r10 = TDX_HYPERCALL_STANDARD,
  328. .r11 = EXIT_REASON_MSR_WRITE,
  329. .r12 = msr,
  330. .r13 = val,
  331. };
  332. u64 ret = __tdx_hypercall(&args);
  333. WARN_ONCE(ret, "Failed to emulate MSR write: %lld\n", ret);
  334. }
  335. static void hv_tdx_msr_read(u64 msr, u64 *val)
  336. {
  337. struct tdx_module_args args = {
  338. .r10 = TDX_HYPERCALL_STANDARD,
  339. .r11 = EXIT_REASON_MSR_READ,
  340. .r12 = msr,
  341. };
  342. u64 ret = __tdx_hypercall(&args);
  343. if (WARN_ONCE(ret, "Failed to emulate MSR read: %lld\n", ret))
  344. *val = 0;
  345. else
  346. *val = args.r11;
  347. }
  348. u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2)
  349. {
  350. struct tdx_module_args args = { };
  351. args.r10 = control;
  352. args.rdx = param1;
  353. args.r8 = param2;
  354. (void)__tdx_hypercall(&args);
  355. return args.r11;
  356. }
  357. #else
  358. static inline void hv_tdx_msr_write(u64 msr, u64 value) {}
  359. static inline void hv_tdx_msr_read(u64 msr, u64 *value) {}
  360. #endif /* CONFIG_INTEL_TDX_GUEST */
  361. #if defined(CONFIG_AMD_MEM_ENCRYPT) || defined(CONFIG_INTEL_TDX_GUEST)
  362. void hv_ivm_msr_write(u64 msr, u64 value)
  363. {
  364. if (!ms_hyperv.paravisor_present)
  365. return;
  366. if (hv_isolation_type_tdx())
  367. hv_tdx_msr_write(msr, value);
  368. else if (hv_isolation_type_snp())
  369. hv_ghcb_msr_write(msr, value);
  370. }
  371. void hv_ivm_msr_read(u64 msr, u64 *value)
  372. {
  373. if (!ms_hyperv.paravisor_present)
  374. return;
  375. if (hv_isolation_type_tdx())
  376. hv_tdx_msr_read(msr, value);
  377. else if (hv_isolation_type_snp())
  378. hv_ghcb_msr_read(msr, value);
  379. }
  380. /*
  381. * hv_mark_gpa_visibility - Set pages visible to host via hvcall.
  382. *
  383. * In Isolation VM, all guest memory is encrypted from host and guest
  384. * needs to set memory visible to host via hvcall before sharing memory
  385. * with host.
  386. */
  387. static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
  388. enum hv_mem_host_visibility visibility)
  389. {
  390. struct hv_gpa_range_for_visibility *input;
  391. u64 hv_status;
  392. unsigned long flags;
  393. /* no-op if partition isolation is not enabled */
  394. if (!hv_is_isolation_supported())
  395. return 0;
  396. if (count > HV_MAX_MODIFY_GPA_REP_COUNT) {
  397. pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count,
  398. HV_MAX_MODIFY_GPA_REP_COUNT);
  399. return -EINVAL;
  400. }
  401. local_irq_save(flags);
  402. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  403. if (unlikely(!input)) {
  404. local_irq_restore(flags);
  405. return -EINVAL;
  406. }
  407. input->partition_id = HV_PARTITION_ID_SELF;
  408. input->host_visibility = visibility;
  409. input->reserved0 = 0;
  410. input->reserved1 = 0;
  411. memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn));
  412. hv_status = hv_do_rep_hypercall(
  413. HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count,
  414. 0, input, NULL);
  415. local_irq_restore(flags);
  416. if (hv_result_success(hv_status))
  417. return 0;
  418. else
  419. return -EFAULT;
  420. }
  421. /*
  422. * When transitioning memory between encrypted and decrypted, the caller
  423. * of set_memory_encrypted() or set_memory_decrypted() is responsible for
  424. * ensuring that the memory isn't in use and isn't referenced while the
  425. * transition is in progress. The transition has multiple steps, and the
  426. * memory is in an inconsistent state until all steps are complete. A
  427. * reference while the state is inconsistent could result in an exception
  428. * that can't be cleanly fixed up.
  429. *
  430. * But the Linux kernel load_unaligned_zeropad() mechanism could cause a
  431. * stray reference that can't be prevented by the caller, so Linux has
  432. * specific code to handle this case. But when the #VC and #VE exceptions
  433. * routed to a paravisor, the specific code doesn't work. To avoid this
  434. * problem, mark the pages as "not present" while the transition is in
  435. * progress. If load_unaligned_zeropad() causes a stray reference, a normal
  436. * page fault is generated instead of #VC or #VE, and the page-fault-based
  437. * handlers for load_unaligned_zeropad() resolve the reference. When the
  438. * transition is complete, hv_vtom_set_host_visibility() marks the pages
  439. * as "present" again.
  440. */
  441. static int hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bool enc)
  442. {
  443. return set_memory_np(kbuffer, pagecount);
  444. }
  445. /*
  446. * hv_vtom_set_host_visibility - Set specified memory visible to host.
  447. *
  448. * In Isolation VM, all guest memory is encrypted from host and guest
  449. * needs to set memory visible to host via hvcall before sharing memory
  450. * with host. This function works as wrap of hv_mark_gpa_visibility()
  451. * with memory base and size.
  452. */
  453. static int hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bool enc)
  454. {
  455. enum hv_mem_host_visibility visibility = enc ?
  456. VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE;
  457. u64 *pfn_array;
  458. phys_addr_t paddr;
  459. int i, pfn, err;
  460. void *vaddr;
  461. int ret = 0;
  462. pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
  463. if (!pfn_array) {
  464. ret = -ENOMEM;
  465. goto err_set_memory_p;
  466. }
  467. for (i = 0, pfn = 0; i < pagecount; i++) {
  468. /*
  469. * Use slow_virt_to_phys() because the PRESENT bit has been
  470. * temporarily cleared in the PTEs. slow_virt_to_phys() works
  471. * without the PRESENT bit while virt_to_hvpfn() or similar
  472. * does not.
  473. */
  474. vaddr = (void *)kbuffer + (i * HV_HYP_PAGE_SIZE);
  475. paddr = slow_virt_to_phys(vaddr);
  476. pfn_array[pfn] = paddr >> HV_HYP_PAGE_SHIFT;
  477. pfn++;
  478. if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) {
  479. ret = hv_mark_gpa_visibility(pfn, pfn_array,
  480. visibility);
  481. if (ret)
  482. goto err_free_pfn_array;
  483. pfn = 0;
  484. }
  485. }
  486. err_free_pfn_array:
  487. kfree(pfn_array);
  488. err_set_memory_p:
  489. /*
  490. * Set the PTE PRESENT bits again to revert what hv_vtom_clear_present()
  491. * did. Do this even if there is an error earlier in this function in
  492. * order to avoid leaving the memory range in a "broken" state. Setting
  493. * the PRESENT bits shouldn't fail, but return an error if it does.
  494. */
  495. err = set_memory_p(kbuffer, pagecount);
  496. if (err && !ret)
  497. ret = err;
  498. return ret;
  499. }
  500. static bool hv_vtom_tlb_flush_required(bool private)
  501. {
  502. /*
  503. * Since hv_vtom_clear_present() marks the PTEs as "not present"
  504. * and flushes the TLB, they can't be in the TLB. That makes the
  505. * flush controlled by this function redundant, so return "false".
  506. */
  507. return false;
  508. }
  509. static bool hv_vtom_cache_flush_required(void)
  510. {
  511. return false;
  512. }
  513. static bool hv_is_private_mmio(u64 addr)
  514. {
  515. /*
  516. * Hyper-V always provides a single IO-APIC in a guest VM.
  517. * When a paravisor is used, it is emulated by the paravisor
  518. * in the guest context and must be mapped private.
  519. */
  520. if (addr >= HV_IOAPIC_BASE_ADDRESS &&
  521. addr < (HV_IOAPIC_BASE_ADDRESS + PAGE_SIZE))
  522. return true;
  523. /* Same with a vTPM */
  524. if (addr >= VTPM_BASE_ADDRESS &&
  525. addr < (VTPM_BASE_ADDRESS + PAGE_SIZE))
  526. return true;
  527. return false;
  528. }
  529. void __init hv_vtom_init(void)
  530. {
  531. enum hv_isolation_type type = hv_get_isolation_type();
  532. switch (type) {
  533. case HV_ISOLATION_TYPE_VBS:
  534. fallthrough;
  535. /*
  536. * By design, a VM using vTOM doesn't see the SEV setting,
  537. * so SEV initialization is bypassed and sev_status isn't set.
  538. * Set it here to indicate a vTOM VM.
  539. *
  540. * Note: if CONFIG_AMD_MEM_ENCRYPT is not set, sev_status is
  541. * defined as 0ULL, to which we can't assigned a value.
  542. */
  543. #ifdef CONFIG_AMD_MEM_ENCRYPT
  544. case HV_ISOLATION_TYPE_SNP:
  545. sev_status = MSR_AMD64_SNP_VTOM;
  546. cc_vendor = CC_VENDOR_AMD;
  547. break;
  548. #endif
  549. case HV_ISOLATION_TYPE_TDX:
  550. cc_vendor = CC_VENDOR_INTEL;
  551. break;
  552. default:
  553. panic("hv_vtom_init: unsupported isolation type %d\n", type);
  554. }
  555. cc_set_mask(ms_hyperv.shared_gpa_boundary);
  556. physical_mask &= ms_hyperv.shared_gpa_boundary - 1;
  557. x86_platform.hyper.is_private_mmio = hv_is_private_mmio;
  558. x86_platform.guest.enc_cache_flush_required = hv_vtom_cache_flush_required;
  559. x86_platform.guest.enc_tlb_flush_required = hv_vtom_tlb_flush_required;
  560. x86_platform.guest.enc_status_change_prepare = hv_vtom_clear_present;
  561. x86_platform.guest.enc_status_change_finish = hv_vtom_set_host_visibility;
  562. /* Set WB as the default cache mode. */
  563. guest_force_mtrr_state(NULL, 0, MTRR_TYPE_WRBACK);
  564. }
  565. #endif /* defined(CONFIG_AMD_MEM_ENCRYPT) || defined(CONFIG_INTEL_TDX_GUEST) */
  566. enum hv_isolation_type hv_get_isolation_type(void)
  567. {
  568. if (!(ms_hyperv.priv_high & HV_ISOLATION))
  569. return HV_ISOLATION_TYPE_NONE;
  570. return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
  571. }
  572. EXPORT_SYMBOL_GPL(hv_get_isolation_type);
  573. /*
  574. * hv_is_isolation_supported - Check system runs in the Hyper-V
  575. * isolation VM.
  576. */
  577. bool hv_is_isolation_supported(void)
  578. {
  579. if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
  580. return false;
  581. if (!hypervisor_is_type(X86_HYPER_MS_HYPERV))
  582. return false;
  583. return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
  584. }
  585. DEFINE_STATIC_KEY_FALSE(isolation_type_snp);
  586. /*
  587. * hv_isolation_type_snp - Check if the system runs in an AMD SEV-SNP based
  588. * isolation VM.
  589. */
  590. bool hv_isolation_type_snp(void)
  591. {
  592. return static_branch_unlikely(&isolation_type_snp);
  593. }
  594. DEFINE_STATIC_KEY_FALSE(isolation_type_tdx);
  595. /*
  596. * hv_isolation_type_tdx - Check if the system runs in an Intel TDX based
  597. * isolated VM.
  598. */
  599. bool hv_isolation_type_tdx(void)
  600. {
  601. return static_branch_unlikely(&isolation_type_tdx);
  602. }