18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_PGTABLE_INVERT_H
38c2ecf20Sopenharmony_ci#define _ASM_PGTABLE_INVERT_H 1
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * A clear pte value is special, and doesn't get inverted.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Note that even users that only pass a pgprot_t (rather
118c2ecf20Sopenharmony_ci * than a full pte) won't trigger the special zero case,
128c2ecf20Sopenharmony_ci * because even PAGE_NONE has _PAGE_PROTNONE | _PAGE_ACCESSED
138c2ecf20Sopenharmony_ci * set. So the all zero case really is limited to just the
148c2ecf20Sopenharmony_ci * cleared page table entry case.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_cistatic inline bool __pte_needs_invert(u64 val)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	return val && !(val & _PAGE_PRESENT);
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Get a mask to xor with the page table entry to get the correct pfn. */
228c2ecf20Sopenharmony_cistatic inline u64 protnone_mask(u64 val)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return __pte_needs_invert(val) ?  ~0ull : 0;
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * When a PTE transitions from NONE to !NONE or vice-versa
318c2ecf20Sopenharmony_ci	 * invert the PFN part to stop speculation.
328c2ecf20Sopenharmony_ci	 * pte_pfn undoes this when needed.
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	if (__pte_needs_invert(oldval) != __pte_needs_invert(val))
358c2ecf20Sopenharmony_ci		val = (val & ~mask) | (~val & mask);
368c2ecf20Sopenharmony_ci	return val;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#endif
42