1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_PGTABLE_INVERT_H 3 #define _ASM_PGTABLE_INVERT_H 1 4 5 #ifndef __ASSEMBLY__ 6 7 static inline bool __pte_needs_invert(u64 val) 8 { 9 return !(val & _PAGE_PRESENT); 10 } 11 12 /* Get a mask to xor with the page table entry to get the correct pfn. */ 13 static inline u64 protnone_mask(u64 val) 14 { 15 return __pte_needs_invert(val) ? ~0ull : 0; 16 } 17 18 static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask) 19 { 20 /* 21 * When a PTE transitions from NONE to !NONE or vice-versa 22 * invert the PFN part to stop speculation. 23 * pte_pfn undoes this when needed. 24 */ 25 if (__pte_needs_invert(oldval) != __pte_needs_invert(val)) 26 val = (val & ~mask) | (~val & mask); 27 return val; 28 } 29 30 #endif /* __ASSEMBLY__ */ 31 32 #endif 33