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