mem_encrypt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * AMD Memory Encryption Support
  3. *
  4. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define DISABLE_BRANCH_PROFILING
  13. #include <linux/linkage.h>
  14. #include <linux/init.h>
  15. #include <linux/mm.h>
  16. #include <linux/dma-direct.h>
  17. #include <linux/swiotlb.h>
  18. #include <linux/mem_encrypt.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/fixmap.h>
  21. #include <asm/setup.h>
  22. #include <asm/bootparam.h>
  23. #include <asm/set_memory.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/processor-flags.h>
  26. #include <asm/msr.h>
  27. #include <asm/cmdline.h>
  28. #include "mm_internal.h"
  29. /*
  30. * Since SME related variables are set early in the boot process they must
  31. * reside in the .data section so as not to be zeroed out when the .bss
  32. * section is later cleared.
  33. */
  34. u64 sme_me_mask __section(.data) = 0;
  35. EXPORT_SYMBOL(sme_me_mask);
  36. DEFINE_STATIC_KEY_FALSE(sev_enable_key);
  37. EXPORT_SYMBOL_GPL(sev_enable_key);
  38. bool sev_enabled __section(.data);
  39. /* Buffer used for early in-place encryption by BSP, no locking needed */
  40. static char sme_early_buffer[PAGE_SIZE] __aligned(PAGE_SIZE);
  41. /*
  42. * This routine does not change the underlying encryption setting of the
  43. * page(s) that map this memory. It assumes that eventually the memory is
  44. * meant to be accessed as either encrypted or decrypted but the contents
  45. * are currently not in the desired state.
  46. *
  47. * This routine follows the steps outlined in the AMD64 Architecture
  48. * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
  49. */
  50. static void __init __sme_early_enc_dec(resource_size_t paddr,
  51. unsigned long size, bool enc)
  52. {
  53. void *src, *dst;
  54. size_t len;
  55. if (!sme_me_mask)
  56. return;
  57. wbinvd();
  58. /*
  59. * There are limited number of early mapping slots, so map (at most)
  60. * one page at time.
  61. */
  62. while (size) {
  63. len = min_t(size_t, sizeof(sme_early_buffer), size);
  64. /*
  65. * Create mappings for the current and desired format of
  66. * the memory. Use a write-protected mapping for the source.
  67. */
  68. src = enc ? early_memremap_decrypted_wp(paddr, len) :
  69. early_memremap_encrypted_wp(paddr, len);
  70. dst = enc ? early_memremap_encrypted(paddr, len) :
  71. early_memremap_decrypted(paddr, len);
  72. /*
  73. * If a mapping can't be obtained to perform the operation,
  74. * then eventual access of that area in the desired mode
  75. * will cause a crash.
  76. */
  77. BUG_ON(!src || !dst);
  78. /*
  79. * Use a temporary buffer, of cache-line multiple size, to
  80. * avoid data corruption as documented in the APM.
  81. */
  82. memcpy(sme_early_buffer, src, len);
  83. memcpy(dst, sme_early_buffer, len);
  84. early_memunmap(dst, len);
  85. early_memunmap(src, len);
  86. paddr += len;
  87. size -= len;
  88. }
  89. }
  90. void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
  91. {
  92. __sme_early_enc_dec(paddr, size, true);
  93. }
  94. void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
  95. {
  96. __sme_early_enc_dec(paddr, size, false);
  97. }
  98. static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
  99. bool map)
  100. {
  101. unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
  102. pmdval_t pmd_flags, pmd;
  103. /* Use early_pmd_flags but remove the encryption mask */
  104. pmd_flags = __sme_clr(early_pmd_flags);
  105. do {
  106. pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
  107. __early_make_pgtable((unsigned long)vaddr, pmd);
  108. vaddr += PMD_SIZE;
  109. paddr += PMD_SIZE;
  110. size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
  111. } while (size);
  112. __native_flush_tlb();
  113. }
  114. void __init sme_unmap_bootdata(char *real_mode_data)
  115. {
  116. struct boot_params *boot_data;
  117. unsigned long cmdline_paddr;
  118. if (!sme_active())
  119. return;
  120. /* Get the command line address before unmapping the real_mode_data */
  121. boot_data = (struct boot_params *)real_mode_data;
  122. cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
  123. __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
  124. if (!cmdline_paddr)
  125. return;
  126. __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
  127. }
  128. void __init sme_map_bootdata(char *real_mode_data)
  129. {
  130. struct boot_params *boot_data;
  131. unsigned long cmdline_paddr;
  132. if (!sme_active())
  133. return;
  134. __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
  135. /* Get the command line address after mapping the real_mode_data */
  136. boot_data = (struct boot_params *)real_mode_data;
  137. cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
  138. if (!cmdline_paddr)
  139. return;
  140. __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
  141. }
  142. void __init sme_early_init(void)
  143. {
  144. unsigned int i;
  145. if (!sme_me_mask)
  146. return;
  147. early_pmd_flags = __sme_set(early_pmd_flags);
  148. __supported_pte_mask = __sme_set(__supported_pte_mask);
  149. /* Update the protection map with memory encryption mask */
  150. for (i = 0; i < ARRAY_SIZE(protection_map); i++)
  151. protection_map[i] = pgprot_encrypted(protection_map[i]);
  152. if (sev_active())
  153. swiotlb_force = SWIOTLB_FORCE;
  154. }
  155. static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
  156. {
  157. pgprot_t old_prot, new_prot;
  158. unsigned long pfn, pa, size;
  159. pte_t new_pte;
  160. switch (level) {
  161. case PG_LEVEL_4K:
  162. pfn = pte_pfn(*kpte);
  163. old_prot = pte_pgprot(*kpte);
  164. break;
  165. case PG_LEVEL_2M:
  166. pfn = pmd_pfn(*(pmd_t *)kpte);
  167. old_prot = pmd_pgprot(*(pmd_t *)kpte);
  168. break;
  169. case PG_LEVEL_1G:
  170. pfn = pud_pfn(*(pud_t *)kpte);
  171. old_prot = pud_pgprot(*(pud_t *)kpte);
  172. break;
  173. default:
  174. return;
  175. }
  176. new_prot = old_prot;
  177. if (enc)
  178. pgprot_val(new_prot) |= _PAGE_ENC;
  179. else
  180. pgprot_val(new_prot) &= ~_PAGE_ENC;
  181. /* If prot is same then do nothing. */
  182. if (pgprot_val(old_prot) == pgprot_val(new_prot))
  183. return;
  184. pa = pfn << page_level_shift(level);
  185. size = page_level_size(level);
  186. /*
  187. * We are going to perform in-place en-/decryption and change the
  188. * physical page attribute from C=1 to C=0 or vice versa. Flush the
  189. * caches to ensure that data gets accessed with the correct C-bit.
  190. */
  191. clflush_cache_range(__va(pa), size);
  192. /* Encrypt/decrypt the contents in-place */
  193. if (enc)
  194. sme_early_encrypt(pa, size);
  195. else
  196. sme_early_decrypt(pa, size);
  197. /* Change the page encryption mask. */
  198. new_pte = pfn_pte(pfn, new_prot);
  199. set_pte_atomic(kpte, new_pte);
  200. }
  201. static int __init early_set_memory_enc_dec(unsigned long vaddr,
  202. unsigned long size, bool enc)
  203. {
  204. unsigned long vaddr_end, vaddr_next;
  205. unsigned long psize, pmask;
  206. int split_page_size_mask;
  207. int level, ret;
  208. pte_t *kpte;
  209. vaddr_next = vaddr;
  210. vaddr_end = vaddr + size;
  211. for (; vaddr < vaddr_end; vaddr = vaddr_next) {
  212. kpte = lookup_address(vaddr, &level);
  213. if (!kpte || pte_none(*kpte)) {
  214. ret = 1;
  215. goto out;
  216. }
  217. if (level == PG_LEVEL_4K) {
  218. __set_clr_pte_enc(kpte, level, enc);
  219. vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
  220. continue;
  221. }
  222. psize = page_level_size(level);
  223. pmask = page_level_mask(level);
  224. /*
  225. * Check whether we can change the large page in one go.
  226. * We request a split when the address is not aligned and
  227. * the number of pages to set/clear encryption bit is smaller
  228. * than the number of pages in the large page.
  229. */
  230. if (vaddr == (vaddr & pmask) &&
  231. ((vaddr_end - vaddr) >= psize)) {
  232. __set_clr_pte_enc(kpte, level, enc);
  233. vaddr_next = (vaddr & pmask) + psize;
  234. continue;
  235. }
  236. /*
  237. * The virtual address is part of a larger page, create the next
  238. * level page table mapping (4K or 2M). If it is part of a 2M
  239. * page then we request a split of the large page into 4K
  240. * chunks. A 1GB large page is split into 2M pages, resp.
  241. */
  242. if (level == PG_LEVEL_2M)
  243. split_page_size_mask = 0;
  244. else
  245. split_page_size_mask = 1 << PG_LEVEL_2M;
  246. kernel_physical_mapping_init(__pa(vaddr & pmask),
  247. __pa((vaddr_end & pmask) + psize),
  248. split_page_size_mask);
  249. }
  250. ret = 0;
  251. out:
  252. __flush_tlb_all();
  253. return ret;
  254. }
  255. int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
  256. {
  257. return early_set_memory_enc_dec(vaddr, size, false);
  258. }
  259. int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
  260. {
  261. return early_set_memory_enc_dec(vaddr, size, true);
  262. }
  263. /*
  264. * SME and SEV are very similar but they are not the same, so there are
  265. * times that the kernel will need to distinguish between SME and SEV. The
  266. * sme_active() and sev_active() functions are used for this. When a
  267. * distinction isn't needed, the mem_encrypt_active() function can be used.
  268. *
  269. * The trampoline code is a good example for this requirement. Before
  270. * paging is activated, SME will access all memory as decrypted, but SEV
  271. * will access all memory as encrypted. So, when APs are being brought
  272. * up under SME the trampoline area cannot be encrypted, whereas under SEV
  273. * the trampoline area must be encrypted.
  274. */
  275. bool sme_active(void)
  276. {
  277. return sme_me_mask && !sev_enabled;
  278. }
  279. EXPORT_SYMBOL(sme_active);
  280. bool sev_active(void)
  281. {
  282. return sme_me_mask && sev_enabled;
  283. }
  284. EXPORT_SYMBOL(sev_active);
  285. /* Architecture __weak replacement functions */
  286. void __init mem_encrypt_free_decrypted_mem(void)
  287. {
  288. unsigned long vaddr, vaddr_end, npages;
  289. int r;
  290. vaddr = (unsigned long)__start_bss_decrypted_unused;
  291. vaddr_end = (unsigned long)__end_bss_decrypted;
  292. npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
  293. /*
  294. * The unused memory range was mapped decrypted, change the encryption
  295. * attribute from decrypted to encrypted before freeing it.
  296. */
  297. if (mem_encrypt_active()) {
  298. r = set_memory_encrypted(vaddr, npages);
  299. if (r) {
  300. pr_warn("failed to free unused decrypted pages\n");
  301. return;
  302. }
  303. }
  304. free_init_pages("unused decrypted", vaddr, vaddr_end);
  305. }
  306. void __init mem_encrypt_init(void)
  307. {
  308. if (!sme_me_mask)
  309. return;
  310. /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
  311. swiotlb_update_mem_attributes();
  312. /*
  313. * With SEV, DMA operations cannot use encryption, we need to use
  314. * SWIOTLB to bounce buffer DMA operation.
  315. */
  316. if (sev_active())
  317. dma_ops = &swiotlb_dma_ops;
  318. /*
  319. * With SEV, we need to unroll the rep string I/O instructions.
  320. */
  321. if (sev_active())
  322. static_branch_enable(&sev_enable_key);
  323. pr_info("AMD %s active\n",
  324. sev_active() ? "Secure Encrypted Virtualization (SEV)"
  325. : "Secure Memory Encryption (SME)");
  326. }