2 * AMD Memory Encryption Support
4 * Copyright (C) 2017 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
13 #include <linux/linkage.h>
15 #include <asm/processor-flags.h>
17 #include <asm/asm-offsets.h>
21 ENTRY(get_sev_encryption_bit)
24 #ifdef CONFIG_AMD_MEM_ENCRYPT
31 * RIP-relative addressing is needed to access the encryption bit
32 * variable. Since we are running in 32-bit mode we need this call/pop
33 * sequence to get the proper relative addressing.
39 movl enc_bit(%edi), %eax
43 /* Check if running under a hypervisor */
46 bt $31, %ecx /* Check the hypervisor bit */
49 movl $0x80000000, %eax /* CPUID to check the highest leaf */
51 cmpl $0x8000001f, %eax /* See if 0x8000001f is available */
55 * Check for the SEV feature:
56 * CPUID Fn8000_001F[EAX] - Bit 1
57 * CPUID Fn8000_001F[EBX] - Bits 5:0
58 * Pagetable bit position used to indicate encryption
60 movl $0x8000001f, %eax
62 bt $1, %eax /* Check if SEV is available */
65 movl $MSR_AMD64_SEV, %ecx /* Read the SEV MSR */
67 bt $MSR_AMD64_SEV_ENABLED_BIT, %eax /* Check if SEV is active */
71 andl $0x3f, %eax /* Return the encryption bit location */
72 movl %eax, enc_bit(%edi)
77 movl %eax, enc_bit(%edi)
85 #endif /* CONFIG_AMD_MEM_ENCRYPT */
88 ENDPROC(get_sev_encryption_bit)
91 ENTRY(set_sev_encryption_mask)
92 #ifdef CONFIG_AMD_MEM_ENCRYPT
96 movq %rsp, %rbp /* Save current stack pointer */
98 call get_sev_encryption_bit /* Get the encryption bit position */
102 bts %rax, sme_me_mask(%rip) /* Create the encryption mask */
105 movq %rbp, %rsp /* Restore original stack pointer */
113 ENDPROC(set_sev_encryption_mask)
119 #ifdef CONFIG_AMD_MEM_ENCRYPT