ivm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyper-V Isolation VM interface with paravisor and hypervisor
  4. *
  5. * Author:
  6. * Tianyu Lan <[email protected]>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/hyperv.h>
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <asm/svm.h>
  13. #include <asm/sev.h>
  14. #include <asm/io.h>
  15. #include <asm/mshyperv.h>
  16. #include <asm/hypervisor.h>
  17. #ifdef CONFIG_AMD_MEM_ENCRYPT
  18. #define GHCB_USAGE_HYPERV_CALL 1
  19. union hv_ghcb {
  20. struct ghcb ghcb;
  21. struct {
  22. u64 hypercalldata[509];
  23. u64 outputgpa;
  24. union {
  25. union {
  26. struct {
  27. u32 callcode : 16;
  28. u32 isfast : 1;
  29. u32 reserved1 : 14;
  30. u32 isnested : 1;
  31. u32 countofelements : 12;
  32. u32 reserved2 : 4;
  33. u32 repstartindex : 12;
  34. u32 reserved3 : 4;
  35. };
  36. u64 asuint64;
  37. } hypercallinput;
  38. union {
  39. struct {
  40. u16 callstatus;
  41. u16 reserved1;
  42. u32 elementsprocessed : 12;
  43. u32 reserved2 : 20;
  44. };
  45. u64 asunit64;
  46. } hypercalloutput;
  47. };
  48. u64 reserved2;
  49. } hypercall;
  50. } __packed __aligned(HV_HYP_PAGE_SIZE);
  51. static u16 hv_ghcb_version __ro_after_init;
  52. u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
  53. {
  54. union hv_ghcb *hv_ghcb;
  55. void **ghcb_base;
  56. unsigned long flags;
  57. u64 status;
  58. if (!hv_ghcb_pg)
  59. return -EFAULT;
  60. WARN_ON(in_nmi());
  61. local_irq_save(flags);
  62. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  63. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  64. if (!hv_ghcb) {
  65. local_irq_restore(flags);
  66. return -EFAULT;
  67. }
  68. hv_ghcb->ghcb.protocol_version = GHCB_PROTOCOL_MAX;
  69. hv_ghcb->ghcb.ghcb_usage = GHCB_USAGE_HYPERV_CALL;
  70. hv_ghcb->hypercall.outputgpa = (u64)output;
  71. hv_ghcb->hypercall.hypercallinput.asuint64 = 0;
  72. hv_ghcb->hypercall.hypercallinput.callcode = control;
  73. if (input_size)
  74. memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size);
  75. VMGEXIT();
  76. hv_ghcb->ghcb.ghcb_usage = 0xffffffff;
  77. memset(hv_ghcb->ghcb.save.valid_bitmap, 0,
  78. sizeof(hv_ghcb->ghcb.save.valid_bitmap));
  79. status = hv_ghcb->hypercall.hypercalloutput.callstatus;
  80. local_irq_restore(flags);
  81. return status;
  82. }
  83. static inline u64 rd_ghcb_msr(void)
  84. {
  85. return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
  86. }
  87. static inline void wr_ghcb_msr(u64 val)
  88. {
  89. native_wrmsrl(MSR_AMD64_SEV_ES_GHCB, val);
  90. }
  91. static enum es_result hv_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code,
  92. u64 exit_info_1, u64 exit_info_2)
  93. {
  94. /* Fill in protocol and format specifiers */
  95. ghcb->protocol_version = hv_ghcb_version;
  96. ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
  97. ghcb_set_sw_exit_code(ghcb, exit_code);
  98. ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
  99. ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
  100. VMGEXIT();
  101. if (ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0))
  102. return ES_VMM_ERROR;
  103. else
  104. return ES_OK;
  105. }
  106. void hv_ghcb_terminate(unsigned int set, unsigned int reason)
  107. {
  108. u64 val = GHCB_MSR_TERM_REQ;
  109. /* Tell the hypervisor what went wrong. */
  110. val |= GHCB_SEV_TERM_REASON(set, reason);
  111. /* Request Guest Termination from Hypvervisor */
  112. wr_ghcb_msr(val);
  113. VMGEXIT();
  114. while (true)
  115. asm volatile("hlt\n" : : : "memory");
  116. }
  117. bool hv_ghcb_negotiate_protocol(void)
  118. {
  119. u64 ghcb_gpa;
  120. u64 val;
  121. /* Save ghcb page gpa. */
  122. ghcb_gpa = rd_ghcb_msr();
  123. /* Do the GHCB protocol version negotiation */
  124. wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
  125. VMGEXIT();
  126. val = rd_ghcb_msr();
  127. if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
  128. return false;
  129. if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
  130. GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
  131. return false;
  132. hv_ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val),
  133. GHCB_PROTOCOL_MAX);
  134. /* Write ghcb page back after negotiating protocol. */
  135. wr_ghcb_msr(ghcb_gpa);
  136. VMGEXIT();
  137. return true;
  138. }
  139. void hv_ghcb_msr_write(u64 msr, u64 value)
  140. {
  141. union hv_ghcb *hv_ghcb;
  142. void **ghcb_base;
  143. unsigned long flags;
  144. if (!hv_ghcb_pg)
  145. return;
  146. WARN_ON(in_nmi());
  147. local_irq_save(flags);
  148. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  149. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  150. if (!hv_ghcb) {
  151. local_irq_restore(flags);
  152. return;
  153. }
  154. ghcb_set_rcx(&hv_ghcb->ghcb, msr);
  155. ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value));
  156. ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value));
  157. if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 1, 0))
  158. pr_warn("Fail to write msr via ghcb %llx.\n", msr);
  159. local_irq_restore(flags);
  160. }
  161. EXPORT_SYMBOL_GPL(hv_ghcb_msr_write);
  162. void hv_ghcb_msr_read(u64 msr, u64 *value)
  163. {
  164. union hv_ghcb *hv_ghcb;
  165. void **ghcb_base;
  166. unsigned long flags;
  167. /* Check size of union hv_ghcb here. */
  168. BUILD_BUG_ON(sizeof(union hv_ghcb) != HV_HYP_PAGE_SIZE);
  169. if (!hv_ghcb_pg)
  170. return;
  171. WARN_ON(in_nmi());
  172. local_irq_save(flags);
  173. ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
  174. hv_ghcb = (union hv_ghcb *)*ghcb_base;
  175. if (!hv_ghcb) {
  176. local_irq_restore(flags);
  177. return;
  178. }
  179. ghcb_set_rcx(&hv_ghcb->ghcb, msr);
  180. if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0))
  181. pr_warn("Fail to read msr via ghcb %llx.\n", msr);
  182. else
  183. *value = (u64)lower_32_bits(hv_ghcb->ghcb.save.rax)
  184. | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32);
  185. local_irq_restore(flags);
  186. }
  187. EXPORT_SYMBOL_GPL(hv_ghcb_msr_read);
  188. #endif
  189. enum hv_isolation_type hv_get_isolation_type(void)
  190. {
  191. if (!(ms_hyperv.priv_high & HV_ISOLATION))
  192. return HV_ISOLATION_TYPE_NONE;
  193. return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
  194. }
  195. EXPORT_SYMBOL_GPL(hv_get_isolation_type);
  196. /*
  197. * hv_is_isolation_supported - Check system runs in the Hyper-V
  198. * isolation VM.
  199. */
  200. bool hv_is_isolation_supported(void)
  201. {
  202. if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
  203. return false;
  204. if (!hypervisor_is_type(X86_HYPER_MS_HYPERV))
  205. return false;
  206. return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
  207. }
  208. DEFINE_STATIC_KEY_FALSE(isolation_type_snp);
  209. /*
  210. * hv_isolation_type_snp - Check system runs in the AMD SEV-SNP based
  211. * isolation VM.
  212. */
  213. bool hv_isolation_type_snp(void)
  214. {
  215. return static_branch_unlikely(&isolation_type_snp);
  216. }
  217. /*
  218. * hv_mark_gpa_visibility - Set pages visible to host via hvcall.
  219. *
  220. * In Isolation VM, all guest memory is encrypted from host and guest
  221. * needs to set memory visible to host via hvcall before sharing memory
  222. * with host.
  223. */
  224. static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
  225. enum hv_mem_host_visibility visibility)
  226. {
  227. struct hv_gpa_range_for_visibility **input_pcpu, *input;
  228. u16 pages_processed;
  229. u64 hv_status;
  230. unsigned long flags;
  231. /* no-op if partition isolation is not enabled */
  232. if (!hv_is_isolation_supported())
  233. return 0;
  234. if (count > HV_MAX_MODIFY_GPA_REP_COUNT) {
  235. pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count,
  236. HV_MAX_MODIFY_GPA_REP_COUNT);
  237. return -EINVAL;
  238. }
  239. local_irq_save(flags);
  240. input_pcpu = (struct hv_gpa_range_for_visibility **)
  241. this_cpu_ptr(hyperv_pcpu_input_arg);
  242. input = *input_pcpu;
  243. if (unlikely(!input)) {
  244. local_irq_restore(flags);
  245. return -EINVAL;
  246. }
  247. input->partition_id = HV_PARTITION_ID_SELF;
  248. input->host_visibility = visibility;
  249. input->reserved0 = 0;
  250. input->reserved1 = 0;
  251. memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn));
  252. hv_status = hv_do_rep_hypercall(
  253. HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count,
  254. 0, input, &pages_processed);
  255. local_irq_restore(flags);
  256. if (hv_result_success(hv_status))
  257. return 0;
  258. else
  259. return -EFAULT;
  260. }
  261. /*
  262. * hv_set_mem_host_visibility - Set specified memory visible to host.
  263. *
  264. * In Isolation VM, all guest memory is encrypted from host and guest
  265. * needs to set memory visible to host via hvcall before sharing memory
  266. * with host. This function works as wrap of hv_mark_gpa_visibility()
  267. * with memory base and size.
  268. */
  269. int hv_set_mem_host_visibility(unsigned long kbuffer, int pagecount, bool visible)
  270. {
  271. enum hv_mem_host_visibility visibility = visible ?
  272. VMBUS_PAGE_VISIBLE_READ_WRITE : VMBUS_PAGE_NOT_VISIBLE;
  273. u64 *pfn_array;
  274. int ret = 0;
  275. int i, pfn;
  276. if (!hv_is_isolation_supported() || !hv_hypercall_pg)
  277. return 0;
  278. pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
  279. if (!pfn_array)
  280. return -ENOMEM;
  281. for (i = 0, pfn = 0; i < pagecount; i++) {
  282. pfn_array[pfn] = virt_to_hvpfn((void *)kbuffer + i * HV_HYP_PAGE_SIZE);
  283. pfn++;
  284. if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) {
  285. ret = hv_mark_gpa_visibility(pfn, pfn_array,
  286. visibility);
  287. if (ret)
  288. goto err_free_pfn_array;
  289. pfn = 0;
  290. }
  291. }
  292. err_free_pfn_array:
  293. kfree(pfn_array);
  294. return ret;
  295. }
  296. /*
  297. * hv_map_memory - map memory to extra space in the AMD SEV-SNP Isolation VM.
  298. */
  299. void *hv_map_memory(void *addr, unsigned long size)
  300. {
  301. unsigned long *pfns = kcalloc(size / PAGE_SIZE,
  302. sizeof(unsigned long), GFP_KERNEL);
  303. void *vaddr;
  304. int i;
  305. if (!pfns)
  306. return NULL;
  307. for (i = 0; i < size / PAGE_SIZE; i++)
  308. pfns[i] = vmalloc_to_pfn(addr + i * PAGE_SIZE) +
  309. (ms_hyperv.shared_gpa_boundary >> PAGE_SHIFT);
  310. vaddr = vmap_pfn(pfns, size / PAGE_SIZE, PAGE_KERNEL_IO);
  311. kfree(pfns);
  312. return vaddr;
  313. }
  314. void hv_unmap_memory(void *addr)
  315. {
  316. vunmap(addr);
  317. }