mem_encrypt_boot.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <linux/linkage.h>
  13. #include <asm/pgtable.h>
  14. #include <asm/page.h>
  15. #include <asm/processor-flags.h>
  16. #include <asm/msr-index.h>
  17. #include <asm/nospec-branch.h>
  18. .text
  19. .code64
  20. ENTRY(sme_encrypt_execute)
  21. /*
  22. * Entry parameters:
  23. * RDI - virtual address for the encrypted mapping
  24. * RSI - virtual address for the decrypted mapping
  25. * RDX - length to encrypt
  26. * RCX - virtual address of the encryption workarea, including:
  27. * - stack page (PAGE_SIZE)
  28. * - encryption routine page (PAGE_SIZE)
  29. * - intermediate copy buffer (PMD_PAGE_SIZE)
  30. * R8 - physcial address of the pagetables to use for encryption
  31. */
  32. push %rbp
  33. movq %rsp, %rbp /* RBP now has original stack pointer */
  34. /* Set up a one page stack in the non-encrypted memory area */
  35. movq %rcx, %rax /* Workarea stack page */
  36. leaq PAGE_SIZE(%rax), %rsp /* Set new stack pointer */
  37. addq $PAGE_SIZE, %rax /* Workarea encryption routine */
  38. push %r12
  39. movq %rdi, %r10 /* Encrypted area */
  40. movq %rsi, %r11 /* Decrypted area */
  41. movq %rdx, %r12 /* Area length */
  42. /* Copy encryption routine into the workarea */
  43. movq %rax, %rdi /* Workarea encryption routine */
  44. leaq __enc_copy(%rip), %rsi /* Encryption routine */
  45. movq $(.L__enc_copy_end - __enc_copy), %rcx /* Encryption routine length */
  46. rep movsb
  47. /* Setup registers for call */
  48. movq %r10, %rdi /* Encrypted area */
  49. movq %r11, %rsi /* Decrypted area */
  50. movq %r8, %rdx /* Pagetables used for encryption */
  51. movq %r12, %rcx /* Area length */
  52. movq %rax, %r8 /* Workarea encryption routine */
  53. addq $PAGE_SIZE, %r8 /* Workarea intermediate copy buffer */
  54. ANNOTATE_RETPOLINE_SAFE
  55. call *%rax /* Call the encryption routine */
  56. pop %r12
  57. movq %rbp, %rsp /* Restore original stack pointer */
  58. pop %rbp
  59. ret
  60. ENDPROC(sme_encrypt_execute)
  61. ENTRY(__enc_copy)
  62. /*
  63. * Routine used to encrypt memory in place.
  64. * This routine must be run outside of the kernel proper since
  65. * the kernel will be encrypted during the process. So this
  66. * routine is defined here and then copied to an area outside
  67. * of the kernel where it will remain and run decrypted
  68. * during execution.
  69. *
  70. * On entry the registers must be:
  71. * RDI - virtual address for the encrypted mapping
  72. * RSI - virtual address for the decrypted mapping
  73. * RDX - address of the pagetables to use for encryption
  74. * RCX - length of area
  75. * R8 - intermediate copy buffer
  76. *
  77. * RAX - points to this routine
  78. *
  79. * The area will be encrypted by copying from the non-encrypted
  80. * memory space to an intermediate buffer and then copying from the
  81. * intermediate buffer back to the encrypted memory space. The physical
  82. * addresses of the two mappings are the same which results in the area
  83. * being encrypted "in place".
  84. */
  85. /* Enable the new page tables */
  86. mov %rdx, %cr3
  87. /* Flush any global TLBs */
  88. mov %cr4, %rdx
  89. andq $~X86_CR4_PGE, %rdx
  90. mov %rdx, %cr4
  91. orq $X86_CR4_PGE, %rdx
  92. mov %rdx, %cr4
  93. push %r15
  94. push %r12
  95. movq %rcx, %r9 /* Save area length */
  96. movq %rdi, %r10 /* Save encrypted area address */
  97. movq %rsi, %r11 /* Save decrypted area address */
  98. /* Set the PAT register PA5 entry to write-protect */
  99. movl $MSR_IA32_CR_PAT, %ecx
  100. rdmsr
  101. mov %rdx, %r15 /* Save original PAT value */
  102. andl $0xffff00ff, %edx /* Clear PA5 */
  103. orl $0x00000500, %edx /* Set PA5 to WP */
  104. wrmsr
  105. wbinvd /* Invalidate any cache entries */
  106. /* Copy/encrypt up to 2MB at a time */
  107. movq $PMD_PAGE_SIZE, %r12
  108. 1:
  109. cmpq %r12, %r9
  110. jnb 2f
  111. movq %r9, %r12
  112. 2:
  113. movq %r11, %rsi /* Source - decrypted area */
  114. movq %r8, %rdi /* Dest - intermediate copy buffer */
  115. movq %r12, %rcx
  116. rep movsb
  117. movq %r8, %rsi /* Source - intermediate copy buffer */
  118. movq %r10, %rdi /* Dest - encrypted area */
  119. movq %r12, %rcx
  120. rep movsb
  121. addq %r12, %r11
  122. addq %r12, %r10
  123. subq %r12, %r9 /* Kernel length decrement */
  124. jnz 1b /* Kernel length not zero? */
  125. /* Restore PAT register */
  126. movl $MSR_IA32_CR_PAT, %ecx
  127. rdmsr
  128. mov %r15, %rdx /* Restore original PAT value */
  129. wrmsr
  130. pop %r12
  131. pop %r15
  132. ret
  133. .L__enc_copy_end:
  134. ENDPROC(__enc_copy)