162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_X86_PROCESSOR_FLAGS_H
362306a36Sopenharmony_ci#define _ASM_X86_PROCESSOR_FLAGS_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <uapi/asm/processor-flags.h>
662306a36Sopenharmony_ci#include <linux/mem_encrypt.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifdef CONFIG_VM86
962306a36Sopenharmony_ci#define X86_VM_MASK	X86_EFLAGS_VM
1062306a36Sopenharmony_ci#else
1162306a36Sopenharmony_ci#define X86_VM_MASK	0 /* No VM86 support */
1262306a36Sopenharmony_ci#endif
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * CR3's layout varies depending on several things.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * If CR4.PCIDE is set (64-bit only), then CR3[11:0] is the address space ID.
1862306a36Sopenharmony_ci * If PAE is enabled, then CR3[11:5] is part of the PDPT address
1962306a36Sopenharmony_ci * (i.e. it's 32-byte aligned, not page-aligned) and CR3[4:0] is ignored.
2062306a36Sopenharmony_ci * Otherwise (non-PAE, non-PCID), CR3[3] is PWT, CR3[4] is PCD, and
2162306a36Sopenharmony_ci * CR3[2:0] and CR3[11:5] are ignored.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * In all cases, Linux puts zeros in the low ignored bits and in PWT and PCD.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * CR3[63] is always read as zero.  If CR4.PCIDE is set, then CR3[63] may be
2662306a36Sopenharmony_ci * written as 1 to prevent the write to CR3 from flushing the TLB.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * On systems with SME, one bit (in a variable position!) is stolen to indicate
2962306a36Sopenharmony_ci * that the top-level paging structure is encrypted.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * On systemms with LAM, bits 61 and 62 are used to indicate LAM mode.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * All of the remaining bits indicate the physical address of the top-level
3462306a36Sopenharmony_ci * paging structure.
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * CR3_ADDR_MASK is the mask used by read_cr3_pa().
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_ci#ifdef CONFIG_X86_64
3962306a36Sopenharmony_ci/* Mask off the address space ID and SME encryption bits. */
4062306a36Sopenharmony_ci#define CR3_ADDR_MASK	__sme_clr(PHYSICAL_PAGE_MASK)
4162306a36Sopenharmony_ci#define CR3_PCID_MASK	0xFFFull
4262306a36Sopenharmony_ci#define CR3_NOFLUSH	BIT_ULL(63)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#else
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * CR3_ADDR_MASK needs at least bits 31:5 set on PAE systems, and we save
4762306a36Sopenharmony_ci * a tiny bit of code size by setting all the bits.
4862306a36Sopenharmony_ci */
4962306a36Sopenharmony_ci#define CR3_ADDR_MASK	0xFFFFFFFFull
5062306a36Sopenharmony_ci#define CR3_PCID_MASK	0ull
5162306a36Sopenharmony_ci#define CR3_NOFLUSH	0
5262306a36Sopenharmony_ci#endif
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#ifdef CONFIG_PAGE_TABLE_ISOLATION
5562306a36Sopenharmony_ci# define X86_CR3_PTI_PCID_USER_BIT	11
5662306a36Sopenharmony_ci#endif
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#endif /* _ASM_X86_PROCESSOR_FLAGS_H */
59