2019-06-04 16:11:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2017-07-18 05:10:32 +08:00
|
|
|
/*
|
|
|
|
* AMD Memory Encryption Support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Author: Tom Lendacky <thomas.lendacky@amd.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/linkage.h>
|
2020-06-09 12:32:38 +08:00
|
|
|
#include <linux/pgtable.h>
|
2017-07-18 05:10:32 +08:00
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/processor-flags.h>
|
|
|
|
#include <asm/msr-index.h>
|
2018-01-23 23:18:50 +08:00
|
|
|
#include <asm/nospec-branch.h>
|
2017-07-18 05:10:32 +08:00
|
|
|
|
|
|
|
.text
|
|
|
|
.code64
|
2019-10-11 19:51:04 +08:00
|
|
|
SYM_FUNC_START(sme_encrypt_execute)
|
2017-07-18 05:10:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry parameters:
|
2018-01-11 03:26:34 +08:00
|
|
|
* RDI - virtual address for the encrypted mapping
|
|
|
|
* RSI - virtual address for the decrypted mapping
|
|
|
|
* RDX - length to encrypt
|
2017-07-18 05:10:32 +08:00
|
|
|
* RCX - virtual address of the encryption workarea, including:
|
|
|
|
* - stack page (PAGE_SIZE)
|
|
|
|
* - encryption routine page (PAGE_SIZE)
|
|
|
|
* - intermediate copy buffer (PMD_PAGE_SIZE)
|
|
|
|
* R8 - physcial address of the pagetables to use for encryption
|
|
|
|
*/
|
|
|
|
|
2017-08-28 00:39:24 +08:00
|
|
|
push %rbp
|
|
|
|
movq %rsp, %rbp /* RBP now has original stack pointer */
|
2017-07-18 05:10:32 +08:00
|
|
|
|
|
|
|
/* Set up a one page stack in the non-encrypted memory area */
|
|
|
|
movq %rcx, %rax /* Workarea stack page */
|
|
|
|
leaq PAGE_SIZE(%rax), %rsp /* Set new stack pointer */
|
|
|
|
addq $PAGE_SIZE, %rax /* Workarea encryption routine */
|
|
|
|
|
|
|
|
push %r12
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %rdi, %r10 /* Encrypted area */
|
|
|
|
movq %rsi, %r11 /* Decrypted area */
|
|
|
|
movq %rdx, %r12 /* Area length */
|
2017-07-18 05:10:32 +08:00
|
|
|
|
|
|
|
/* Copy encryption routine into the workarea */
|
|
|
|
movq %rax, %rdi /* Workarea encryption routine */
|
|
|
|
leaq __enc_copy(%rip), %rsi /* Encryption routine */
|
|
|
|
movq $(.L__enc_copy_end - __enc_copy), %rcx /* Encryption routine length */
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
/* Setup registers for call */
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %r10, %rdi /* Encrypted area */
|
|
|
|
movq %r11, %rsi /* Decrypted area */
|
2017-07-18 05:10:32 +08:00
|
|
|
movq %r8, %rdx /* Pagetables used for encryption */
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %r12, %rcx /* Area length */
|
2017-07-18 05:10:32 +08:00
|
|
|
movq %rax, %r8 /* Workarea encryption routine */
|
|
|
|
addq $PAGE_SIZE, %r8 /* Workarea intermediate copy buffer */
|
|
|
|
|
2018-01-23 23:18:50 +08:00
|
|
|
ANNOTATE_RETPOLINE_SAFE
|
2017-07-18 05:10:32 +08:00
|
|
|
call *%rax /* Call the encryption routine */
|
|
|
|
|
|
|
|
pop %r12
|
|
|
|
|
|
|
|
movq %rbp, %rsp /* Restore original stack pointer */
|
2017-08-28 00:39:24 +08:00
|
|
|
pop %rbp
|
2017-07-18 05:10:32 +08:00
|
|
|
|
|
|
|
ret
|
2019-10-11 19:51:04 +08:00
|
|
|
SYM_FUNC_END(sme_encrypt_execute)
|
2017-07-18 05:10:32 +08:00
|
|
|
|
2019-10-11 19:51:04 +08:00
|
|
|
SYM_FUNC_START(__enc_copy)
|
2017-07-18 05:10:32 +08:00
|
|
|
/*
|
2018-01-11 03:26:34 +08:00
|
|
|
* Routine used to encrypt memory in place.
|
2017-07-18 05:10:32 +08:00
|
|
|
* This routine must be run outside of the kernel proper since
|
|
|
|
* the kernel will be encrypted during the process. So this
|
|
|
|
* routine is defined here and then copied to an area outside
|
|
|
|
* of the kernel where it will remain and run decrypted
|
|
|
|
* during execution.
|
|
|
|
*
|
|
|
|
* On entry the registers must be:
|
2018-01-11 03:26:34 +08:00
|
|
|
* RDI - virtual address for the encrypted mapping
|
|
|
|
* RSI - virtual address for the decrypted mapping
|
2017-07-18 05:10:32 +08:00
|
|
|
* RDX - address of the pagetables to use for encryption
|
2018-01-11 03:26:34 +08:00
|
|
|
* RCX - length of area
|
2017-07-18 05:10:32 +08:00
|
|
|
* R8 - intermediate copy buffer
|
|
|
|
*
|
|
|
|
* RAX - points to this routine
|
|
|
|
*
|
2018-01-11 03:26:34 +08:00
|
|
|
* The area will be encrypted by copying from the non-encrypted
|
|
|
|
* memory space to an intermediate buffer and then copying from the
|
|
|
|
* intermediate buffer back to the encrypted memory space. The physical
|
|
|
|
* addresses of the two mappings are the same which results in the area
|
|
|
|
* being encrypted "in place".
|
2017-07-18 05:10:32 +08:00
|
|
|
*/
|
|
|
|
/* Enable the new page tables */
|
|
|
|
mov %rdx, %cr3
|
|
|
|
|
|
|
|
/* Flush any global TLBs */
|
|
|
|
mov %cr4, %rdx
|
|
|
|
andq $~X86_CR4_PGE, %rdx
|
|
|
|
mov %rdx, %cr4
|
|
|
|
orq $X86_CR4_PGE, %rdx
|
|
|
|
mov %rdx, %cr4
|
|
|
|
|
2018-01-11 03:25:56 +08:00
|
|
|
push %r15
|
2018-01-11 03:26:26 +08:00
|
|
|
push %r12
|
2018-01-11 03:25:56 +08:00
|
|
|
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %rcx, %r9 /* Save area length */
|
|
|
|
movq %rdi, %r10 /* Save encrypted area address */
|
|
|
|
movq %rsi, %r11 /* Save decrypted area address */
|
2018-01-11 03:25:56 +08:00
|
|
|
|
2017-07-18 05:10:32 +08:00
|
|
|
/* Set the PAT register PA5 entry to write-protect */
|
|
|
|
movl $MSR_IA32_CR_PAT, %ecx
|
|
|
|
rdmsr
|
2018-01-11 03:25:56 +08:00
|
|
|
mov %rdx, %r15 /* Save original PAT value */
|
2017-07-18 05:10:32 +08:00
|
|
|
andl $0xffff00ff, %edx /* Clear PA5 */
|
|
|
|
orl $0x00000500, %edx /* Set PA5 to WP */
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
wbinvd /* Invalidate any cache entries */
|
|
|
|
|
2018-01-11 03:26:26 +08:00
|
|
|
/* Copy/encrypt up to 2MB at a time */
|
|
|
|
movq $PMD_PAGE_SIZE, %r12
|
2017-07-18 05:10:32 +08:00
|
|
|
1:
|
2018-01-11 03:26:26 +08:00
|
|
|
cmpq %r12, %r9
|
|
|
|
jnb 2f
|
|
|
|
movq %r9, %r12
|
|
|
|
|
|
|
|
2:
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %r11, %rsi /* Source - decrypted area */
|
2017-07-18 05:10:32 +08:00
|
|
|
movq %r8, %rdi /* Dest - intermediate copy buffer */
|
2018-01-11 03:26:26 +08:00
|
|
|
movq %r12, %rcx
|
2017-07-18 05:10:32 +08:00
|
|
|
rep movsb
|
|
|
|
|
|
|
|
movq %r8, %rsi /* Source - intermediate copy buffer */
|
2018-01-11 03:26:34 +08:00
|
|
|
movq %r10, %rdi /* Dest - encrypted area */
|
2018-01-11 03:26:26 +08:00
|
|
|
movq %r12, %rcx
|
2017-07-18 05:10:32 +08:00
|
|
|
rep movsb
|
|
|
|
|
2018-01-11 03:26:26 +08:00
|
|
|
addq %r12, %r11
|
|
|
|
addq %r12, %r10
|
|
|
|
subq %r12, %r9 /* Kernel length decrement */
|
2017-07-18 05:10:32 +08:00
|
|
|
jnz 1b /* Kernel length not zero? */
|
|
|
|
|
|
|
|
/* Restore PAT register */
|
|
|
|
movl $MSR_IA32_CR_PAT, %ecx
|
|
|
|
rdmsr
|
2018-01-11 03:25:56 +08:00
|
|
|
mov %r15, %rdx /* Restore original PAT value */
|
2017-07-18 05:10:32 +08:00
|
|
|
wrmsr
|
|
|
|
|
2018-01-11 03:26:26 +08:00
|
|
|
pop %r12
|
2018-01-11 03:25:56 +08:00
|
|
|
pop %r15
|
|
|
|
|
2017-07-18 05:10:32 +08:00
|
|
|
ret
|
|
|
|
.L__enc_copy_end:
|
2019-10-11 19:51:04 +08:00
|
|
|
SYM_FUNC_END(__enc_copy)
|