18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_PROCESSOR_FLAGS_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_PROCESSOR_FLAGS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <uapi/asm/processor-flags.h>
68c2ecf20Sopenharmony_ci#include <linux/mem_encrypt.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifdef CONFIG_VM86
98c2ecf20Sopenharmony_ci#define X86_VM_MASK	X86_EFLAGS_VM
108c2ecf20Sopenharmony_ci#else
118c2ecf20Sopenharmony_ci#define X86_VM_MASK	0 /* No VM86 support */
128c2ecf20Sopenharmony_ci#endif
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * CR3's layout varies depending on several things.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * If CR4.PCIDE is set (64-bit only), then CR3[11:0] is the address space ID.
188c2ecf20Sopenharmony_ci * If PAE is enabled, then CR3[11:5] is part of the PDPT address
198c2ecf20Sopenharmony_ci * (i.e. it's 32-byte aligned, not page-aligned) and CR3[4:0] is ignored.
208c2ecf20Sopenharmony_ci * Otherwise (non-PAE, non-PCID), CR3[3] is PWT, CR3[4] is PCD, and
218c2ecf20Sopenharmony_ci * CR3[2:0] and CR3[11:5] are ignored.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * In all cases, Linux puts zeros in the low ignored bits and in PWT and PCD.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * CR3[63] is always read as zero.  If CR4.PCIDE is set, then CR3[63] may be
268c2ecf20Sopenharmony_ci * written as 1 to prevent the write to CR3 from flushing the TLB.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * On systems with SME, one bit (in a variable position!) is stolen to indicate
298c2ecf20Sopenharmony_ci * that the top-level paging structure is encrypted.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * All of the remaining bits indicate the physical address of the top-level
328c2ecf20Sopenharmony_ci * paging structure.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * CR3_ADDR_MASK is the mask used by read_cr3_pa().
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
378c2ecf20Sopenharmony_ci/* Mask off the address space ID and SME encryption bits. */
388c2ecf20Sopenharmony_ci#define CR3_ADDR_MASK	__sme_clr(0x7FFFFFFFFFFFF000ull)
398c2ecf20Sopenharmony_ci#define CR3_PCID_MASK	0xFFFull
408c2ecf20Sopenharmony_ci#define CR3_NOFLUSH	BIT_ULL(63)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#else
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * CR3_ADDR_MASK needs at least bits 31:5 set on PAE systems, and we save
458c2ecf20Sopenharmony_ci * a tiny bit of code size by setting all the bits.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci#define CR3_ADDR_MASK	0xFFFFFFFFull
488c2ecf20Sopenharmony_ci#define CR3_PCID_MASK	0ull
498c2ecf20Sopenharmony_ci#define CR3_NOFLUSH	0
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#ifdef CONFIG_PAGE_TABLE_ISOLATION
538c2ecf20Sopenharmony_ci# define X86_CR3_PTI_PCID_USER_BIT	11
548c2ecf20Sopenharmony_ci#endif
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#endif /* _ASM_X86_PROCESSOR_FLAGS_H */
57