mem_encrypt_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Memory Encryption Support
  4. *
  5. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <[email protected]>
  8. */
  9. #define DISABLE_BRANCH_PROFILING
  10. #include <linux/linkage.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/dma-direct.h>
  14. #include <linux/swiotlb.h>
  15. #include <linux/mem_encrypt.h>
  16. #include <linux/device.h>
  17. #include <linux/kernel.h>
  18. #include <linux/bitops.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/virtio_config.h>
  21. #include <linux/virtio_anchor.h>
  22. #include <linux/cc_platform.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/fixmap.h>
  25. #include <asm/setup.h>
  26. #include <asm/mem_encrypt.h>
  27. #include <asm/bootparam.h>
  28. #include <asm/set_memory.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/processor-flags.h>
  31. #include <asm/msr.h>
  32. #include <asm/cmdline.h>
  33. #include <asm/sev.h>
  34. #include <asm/ia32.h>
  35. #include "mm_internal.h"
  36. /*
  37. * Since SME related variables are set early in the boot process they must
  38. * reside in the .data section so as not to be zeroed out when the .bss
  39. * section is later cleared.
  40. */
  41. u64 sme_me_mask __section(".data") = 0;
  42. u64 sev_status __section(".data") = 0;
  43. u64 sev_check_data __section(".data") = 0;
  44. EXPORT_SYMBOL(sme_me_mask);
  45. /* Buffer used for early in-place encryption by BSP, no locking needed */
  46. static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
  47. /*
  48. * SNP-specific routine which needs to additionally change the page state from
  49. * private to shared before copying the data from the source to destination and
  50. * restore after the copy.
  51. */
  52. static inline void __init snp_memcpy(void *dst, void *src, size_t sz,
  53. unsigned long paddr, bool decrypt)
  54. {
  55. unsigned long npages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
  56. if (decrypt) {
  57. /*
  58. * @paddr needs to be accessed decrypted, mark the page shared in
  59. * the RMP table before copying it.
  60. */
  61. early_snp_set_memory_shared((unsigned long)__va(paddr), paddr, npages);
  62. memcpy(dst, src, sz);
  63. /* Restore the page state after the memcpy. */
  64. early_snp_set_memory_private((unsigned long)__va(paddr), paddr, npages);
  65. } else {
  66. /*
  67. * @paddr need to be accessed encrypted, no need for the page state
  68. * change.
  69. */
  70. memcpy(dst, src, sz);
  71. }
  72. }
  73. /*
  74. * This routine does not change the underlying encryption setting of the
  75. * page(s) that map this memory. It assumes that eventually the memory is
  76. * meant to be accessed as either encrypted or decrypted but the contents
  77. * are currently not in the desired state.
  78. *
  79. * This routine follows the steps outlined in the AMD64 Architecture
  80. * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
  81. */
  82. static void __init __sme_early_enc_dec(resource_size_t paddr,
  83. unsigned long size, bool enc)
  84. {
  85. void *src, *dst;
  86. size_t len;
  87. if (!sme_me_mask)
  88. return;
  89. wbinvd();
  90. /*
  91. * There are limited number of early mapping slots, so map (at most)
  92. * one page at time.
  93. */
  94. while (size) {
  95. len = min_t(size_t, sizeof(sme_early_buffer), size);
  96. /*
  97. * Create mappings for the current and desired format of
  98. * the memory. Use a write-protected mapping for the source.
  99. */
  100. src = enc ? early_memremap_decrypted_wp(paddr, len) :
  101. early_memremap_encrypted_wp(paddr, len);
  102. dst = enc ? early_memremap_encrypted(paddr, len) :
  103. early_memremap_decrypted(paddr, len);
  104. /*
  105. * If a mapping can't be obtained to perform the operation,
  106. * then eventual access of that area in the desired mode
  107. * will cause a crash.
  108. */
  109. BUG_ON(!src || !dst);
  110. /*
  111. * Use a temporary buffer, of cache-line multiple size, to
  112. * avoid data corruption as documented in the APM.
  113. */
  114. if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
  115. snp_memcpy(sme_early_buffer, src, len, paddr, enc);
  116. snp_memcpy(dst, sme_early_buffer, len, paddr, !enc);
  117. } else {
  118. memcpy(sme_early_buffer, src, len);
  119. memcpy(dst, sme_early_buffer, len);
  120. }
  121. early_memunmap(dst, len);
  122. early_memunmap(src, len);
  123. paddr += len;
  124. size -= len;
  125. }
  126. }
  127. void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
  128. {
  129. __sme_early_enc_dec(paddr, size, true);
  130. }
  131. void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
  132. {
  133. __sme_early_enc_dec(paddr, size, false);
  134. }
  135. static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
  136. bool map)
  137. {
  138. unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
  139. pmdval_t pmd_flags, pmd;
  140. /* Use early_pmd_flags but remove the encryption mask */
  141. pmd_flags = __sme_clr(early_pmd_flags);
  142. do {
  143. pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
  144. __early_make_pgtable((unsigned long)vaddr, pmd);
  145. vaddr += PMD_SIZE;
  146. paddr += PMD_SIZE;
  147. size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
  148. } while (size);
  149. flush_tlb_local();
  150. }
  151. void __init sme_unmap_bootdata(char *real_mode_data)
  152. {
  153. struct boot_params *boot_data;
  154. unsigned long cmdline_paddr;
  155. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  156. return;
  157. /* Get the command line address before unmapping the real_mode_data */
  158. boot_data = (struct boot_params *)real_mode_data;
  159. cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
  160. __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
  161. if (!cmdline_paddr)
  162. return;
  163. __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
  164. }
  165. void __init sme_map_bootdata(char *real_mode_data)
  166. {
  167. struct boot_params *boot_data;
  168. unsigned long cmdline_paddr;
  169. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  170. return;
  171. __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
  172. /* Get the command line address after mapping the real_mode_data */
  173. boot_data = (struct boot_params *)real_mode_data;
  174. cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
  175. if (!cmdline_paddr)
  176. return;
  177. __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
  178. }
  179. void __init sev_setup_arch(void)
  180. {
  181. phys_addr_t total_mem = memblock_phys_mem_size();
  182. unsigned long size;
  183. if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
  184. return;
  185. /*
  186. * For SEV, all DMA has to occur via shared/unencrypted pages.
  187. * SEV uses SWIOTLB to make this happen without changing device
  188. * drivers. However, depending on the workload being run, the
  189. * default 64MB of SWIOTLB may not be enough and SWIOTLB may
  190. * run out of buffers for DMA, resulting in I/O errors and/or
  191. * performance degradation especially with high I/O workloads.
  192. *
  193. * Adjust the default size of SWIOTLB for SEV guests using
  194. * a percentage of guest memory for SWIOTLB buffers.
  195. * Also, as the SWIOTLB bounce buffer memory is allocated
  196. * from low memory, ensure that the adjusted size is within
  197. * the limits of low available memory.
  198. *
  199. * The percentage of guest memory used here for SWIOTLB buffers
  200. * is more of an approximation of the static adjustment which
  201. * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
  202. */
  203. size = total_mem * 6 / 100;
  204. size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
  205. swiotlb_adjust_size(size);
  206. /* Set restricted memory access for virtio. */
  207. virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
  208. }
  209. static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
  210. {
  211. unsigned long pfn = 0;
  212. pgprot_t prot;
  213. switch (level) {
  214. case PG_LEVEL_4K:
  215. pfn = pte_pfn(*kpte);
  216. prot = pte_pgprot(*kpte);
  217. break;
  218. case PG_LEVEL_2M:
  219. pfn = pmd_pfn(*(pmd_t *)kpte);
  220. prot = pmd_pgprot(*(pmd_t *)kpte);
  221. break;
  222. case PG_LEVEL_1G:
  223. pfn = pud_pfn(*(pud_t *)kpte);
  224. prot = pud_pgprot(*(pud_t *)kpte);
  225. break;
  226. default:
  227. WARN_ONCE(1, "Invalid level for kpte\n");
  228. return 0;
  229. }
  230. if (ret_prot)
  231. *ret_prot = prot;
  232. return pfn;
  233. }
  234. static bool amd_enc_tlb_flush_required(bool enc)
  235. {
  236. return true;
  237. }
  238. static bool amd_enc_cache_flush_required(void)
  239. {
  240. return !cpu_feature_enabled(X86_FEATURE_SME_COHERENT);
  241. }
  242. static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
  243. {
  244. #ifdef CONFIG_PARAVIRT
  245. unsigned long vaddr_end = vaddr + size;
  246. while (vaddr < vaddr_end) {
  247. int psize, pmask, level;
  248. unsigned long pfn;
  249. pte_t *kpte;
  250. kpte = lookup_address(vaddr, &level);
  251. if (!kpte || pte_none(*kpte)) {
  252. WARN_ONCE(1, "kpte lookup for vaddr\n");
  253. return;
  254. }
  255. pfn = pg_level_to_pfn(level, kpte, NULL);
  256. if (!pfn)
  257. continue;
  258. psize = page_level_size(level);
  259. pmask = page_level_mask(level);
  260. notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT, enc);
  261. vaddr = (vaddr & pmask) + psize;
  262. }
  263. #endif
  264. }
  265. static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
  266. {
  267. /*
  268. * To maintain the security guarantees of SEV-SNP guests, make sure
  269. * to invalidate the memory before encryption attribute is cleared.
  270. */
  271. if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc)
  272. snp_set_memory_shared(vaddr, npages);
  273. return true;
  274. }
  275. /* Return true unconditionally: return value doesn't matter for the SEV side */
  276. static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc)
  277. {
  278. /*
  279. * After memory is mapped encrypted in the page table, validate it
  280. * so that it is consistent with the page table updates.
  281. */
  282. if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && enc)
  283. snp_set_memory_private(vaddr, npages);
  284. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  285. enc_dec_hypercall(vaddr, npages << PAGE_SHIFT, enc);
  286. return true;
  287. }
  288. static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
  289. {
  290. pgprot_t old_prot, new_prot;
  291. unsigned long pfn, pa, size;
  292. pte_t new_pte;
  293. pfn = pg_level_to_pfn(level, kpte, &old_prot);
  294. if (!pfn)
  295. return;
  296. new_prot = old_prot;
  297. if (enc)
  298. pgprot_val(new_prot) |= _PAGE_ENC;
  299. else
  300. pgprot_val(new_prot) &= ~_PAGE_ENC;
  301. /* If prot is same then do nothing. */
  302. if (pgprot_val(old_prot) == pgprot_val(new_prot))
  303. return;
  304. pa = pfn << PAGE_SHIFT;
  305. size = page_level_size(level);
  306. /*
  307. * We are going to perform in-place en-/decryption and change the
  308. * physical page attribute from C=1 to C=0 or vice versa. Flush the
  309. * caches to ensure that data gets accessed with the correct C-bit.
  310. */
  311. clflush_cache_range(__va(pa), size);
  312. /* Encrypt/decrypt the contents in-place */
  313. if (enc) {
  314. sme_early_encrypt(pa, size);
  315. } else {
  316. sme_early_decrypt(pa, size);
  317. /*
  318. * ON SNP, the page state in the RMP table must happen
  319. * before the page table updates.
  320. */
  321. early_snp_set_memory_shared((unsigned long)__va(pa), pa, 1);
  322. }
  323. /* Change the page encryption mask. */
  324. new_pte = pfn_pte(pfn, new_prot);
  325. set_pte_atomic(kpte, new_pte);
  326. /*
  327. * If page is set encrypted in the page table, then update the RMP table to
  328. * add this page as private.
  329. */
  330. if (enc)
  331. early_snp_set_memory_private((unsigned long)__va(pa), pa, 1);
  332. }
  333. static int __init early_set_memory_enc_dec(unsigned long vaddr,
  334. unsigned long size, bool enc)
  335. {
  336. unsigned long vaddr_end, vaddr_next, start;
  337. unsigned long psize, pmask;
  338. int split_page_size_mask;
  339. int level, ret;
  340. pte_t *kpte;
  341. start = vaddr;
  342. vaddr_next = vaddr;
  343. vaddr_end = vaddr + size;
  344. for (; vaddr < vaddr_end; vaddr = vaddr_next) {
  345. kpte = lookup_address(vaddr, &level);
  346. if (!kpte || pte_none(*kpte)) {
  347. ret = 1;
  348. goto out;
  349. }
  350. if (level == PG_LEVEL_4K) {
  351. __set_clr_pte_enc(kpte, level, enc);
  352. vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
  353. continue;
  354. }
  355. psize = page_level_size(level);
  356. pmask = page_level_mask(level);
  357. /*
  358. * Check whether we can change the large page in one go.
  359. * We request a split when the address is not aligned and
  360. * the number of pages to set/clear encryption bit is smaller
  361. * than the number of pages in the large page.
  362. */
  363. if (vaddr == (vaddr & pmask) &&
  364. ((vaddr_end - vaddr) >= psize)) {
  365. __set_clr_pte_enc(kpte, level, enc);
  366. vaddr_next = (vaddr & pmask) + psize;
  367. continue;
  368. }
  369. /*
  370. * The virtual address is part of a larger page, create the next
  371. * level page table mapping (4K or 2M). If it is part of a 2M
  372. * page then we request a split of the large page into 4K
  373. * chunks. A 1GB large page is split into 2M pages, resp.
  374. */
  375. if (level == PG_LEVEL_2M)
  376. split_page_size_mask = 0;
  377. else
  378. split_page_size_mask = 1 << PG_LEVEL_2M;
  379. /*
  380. * kernel_physical_mapping_change() does not flush the TLBs, so
  381. * a TLB flush is required after we exit from the for loop.
  382. */
  383. kernel_physical_mapping_change(__pa(vaddr & pmask),
  384. __pa((vaddr_end & pmask) + psize),
  385. split_page_size_mask);
  386. }
  387. ret = 0;
  388. early_set_mem_enc_dec_hypercall(start, size, enc);
  389. out:
  390. __flush_tlb_all();
  391. return ret;
  392. }
  393. int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
  394. {
  395. return early_set_memory_enc_dec(vaddr, size, false);
  396. }
  397. int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
  398. {
  399. return early_set_memory_enc_dec(vaddr, size, true);
  400. }
  401. void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
  402. {
  403. enc_dec_hypercall(vaddr, size, enc);
  404. }
  405. void __init sme_early_init(void)
  406. {
  407. if (!sme_me_mask)
  408. return;
  409. early_pmd_flags = __sme_set(early_pmd_flags);
  410. __supported_pte_mask = __sme_set(__supported_pte_mask);
  411. /* Update the protection map with memory encryption mask */
  412. add_encrypt_protection_map();
  413. x86_platform.guest.enc_status_change_prepare = amd_enc_status_change_prepare;
  414. x86_platform.guest.enc_status_change_finish = amd_enc_status_change_finish;
  415. x86_platform.guest.enc_tlb_flush_required = amd_enc_tlb_flush_required;
  416. x86_platform.guest.enc_cache_flush_required = amd_enc_cache_flush_required;
  417. /*
  418. * The VMM is capable of injecting interrupt 0x80 and triggering the
  419. * compatibility syscall path.
  420. *
  421. * By default, the 32-bit emulation is disabled in order to ensure
  422. * the safety of the VM.
  423. */
  424. if (sev_status & MSR_AMD64_SEV_ENABLED)
  425. ia32_disable();
  426. }
  427. void __init mem_encrypt_free_decrypted_mem(void)
  428. {
  429. unsigned long vaddr, vaddr_end, npages;
  430. int r;
  431. vaddr = (unsigned long)__start_bss_decrypted_unused;
  432. vaddr_end = (unsigned long)__end_bss_decrypted;
  433. npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
  434. /*
  435. * The unused memory range was mapped decrypted, change the encryption
  436. * attribute from decrypted to encrypted before freeing it.
  437. */
  438. if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  439. r = set_memory_encrypted(vaddr, npages);
  440. if (r) {
  441. pr_warn("failed to free unused decrypted pages\n");
  442. return;
  443. }
  444. }
  445. free_init_pages("unused decrypted", vaddr, vaddr_end);
  446. }