@@ -790,26 +790,55 @@ static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);
static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
{
+ pteval_t _page_chg_mask_no_dirty = _PAGE_CHG_MASK & ~_PAGE_DIRTY;
pteval_t val = pte_val(pte), oldval = val;
+ pte_t pte_result;
/*
* Chop off the NX bit (if present), and add the NX portion of
* the newprot (if present):
*/
- val &= _PAGE_CHG_MASK;
- val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK;
+ val &= _page_chg_mask_no_dirty;
+ val |= check_pgprot(newprot) & ~_page_chg_mask_no_dirty;
val = flip_protnone_guard(oldval, val, PTE_PFN_MASK);
- return __pte(val);
+
+ pte_result = __pte(val);
+
+ /*
+ * Dirty bit is not preserved above so it can be done
+ * in a special way for the shadow stack case, where it
+ * needs to set _PAGE_COW. pte_mkdirty() will do this in
+ * the case of shadow stack.
+ */
+ if (pte_dirty(pte))
+ pte_result = pte_mkdirty(pte_result);
+
+ return pte_result;
}
static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
{
+ pteval_t _hpage_chg_mask_no_dirty = _HPAGE_CHG_MASK & ~_PAGE_DIRTY;
pmdval_t val = pmd_val(pmd), oldval = val;
+ pmd_t pmd_result;
- val &= _HPAGE_CHG_MASK;
- val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK;
+ val &= _hpage_chg_mask_no_dirty;
+ val |= check_pgprot(newprot) & ~_hpage_chg_mask_no_dirty;
val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK);
- return __pmd(val);
+
+
+ pmd_result = __pmd(val);
+
+ /*
+ * Dirty bit is not preserved above so it can be done
+ * specially for the shadow stack case. It needs to move
+ * the HW dirty bit to the software COW bit. Set in the
+ * result if it was set in the original value.
+ */
+ if (pmd_dirty(pmd))
+ pmd_result = pmd_mkdirty(pmd_result);
+
+ return pmd_result;
}
/*