1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * Copyright (C) 2024 Loongson Technology Corp 4 */ 5 6 #include <linux/pagewalk.h> 7 #include <linux/pgtable.h> 8 #include <asm/set_memory.h> 9 #include <asm/tlbflush.h> 10 11 struct pageattr_masks { 12 pgprot_t set_mask; 13 pgprot_t clear_mask; 14 }; 15 16 static unsigned long set_pageattr_masks(unsign 17 { 18 unsigned long new_val = val; 19 struct pageattr_masks *masks = walk->p 20 21 new_val &= ~(pgprot_val(masks->clear_m 22 new_val |= (pgprot_val(masks->set_mask 23 24 return new_val; 25 } 26 27 static int pageattr_pgd_entry(pgd_t *pgd, unsi 28 unsigned long ne 29 { 30 pgd_t val = pgdp_get(pgd); 31 32 if (pgd_leaf(val)) { 33 val = __pgd(set_pageattr_masks 34 set_pgd(pgd, val); 35 } 36 37 return 0; 38 } 39 40 static int pageattr_p4d_entry(p4d_t *p4d, unsi 41 unsigned long ne 42 { 43 p4d_t val = p4dp_get(p4d); 44 45 if (p4d_leaf(val)) { 46 val = __p4d(set_pageattr_masks 47 set_p4d(p4d, val); 48 } 49 50 return 0; 51 } 52 53 static int pageattr_pud_entry(pud_t *pud, unsi 54 unsigned long ne 55 { 56 pud_t val = pudp_get(pud); 57 58 if (pud_leaf(val)) { 59 val = __pud(set_pageattr_masks 60 set_pud(pud, val); 61 } 62 63 return 0; 64 } 65 66 static int pageattr_pmd_entry(pmd_t *pmd, unsi 67 unsigned long ne 68 { 69 pmd_t val = pmdp_get(pmd); 70 71 if (pmd_leaf(val)) { 72 val = __pmd(set_pageattr_masks 73 set_pmd(pmd, val); 74 } 75 76 return 0; 77 } 78 79 static int pageattr_pte_entry(pte_t *pte, unsi 80 unsigned long ne 81 { 82 pte_t val = ptep_get(pte); 83 84 val = __pte(set_pageattr_masks(pte_val 85 set_pte(pte, val); 86 87 return 0; 88 } 89 90 static int pageattr_pte_hole(unsigned long add 91 int depth, struct 92 { 93 return 0; 94 } 95 96 static const struct mm_walk_ops pageattr_ops = 97 .pgd_entry = pageattr_pgd_entry, 98 .p4d_entry = pageattr_p4d_entry, 99 .pud_entry = pageattr_pud_entry, 100 .pmd_entry = pageattr_pmd_entry, 101 .pte_entry = pageattr_pte_entry, 102 .pte_hole = pageattr_pte_hole, 103 .walk_lock = PGWALK_RDLOCK, 104 }; 105 106 static int __set_memory(unsigned long addr, in 107 { 108 int ret; 109 unsigned long start = addr; 110 unsigned long end = start + PAGE_SIZE 111 struct pageattr_masks masks = { 112 .set_mask = set_mask, 113 .clear_mask = clear_mask 114 }; 115 116 if (!numpages) 117 return 0; 118 119 mmap_write_lock(&init_mm); 120 ret = walk_page_range_novma(&init_mm, 121 mmap_write_unlock(&init_mm); 122 123 flush_tlb_kernel_range(start, end); 124 125 return ret; 126 } 127 128 int set_memory_x(unsigned long addr, int numpa 129 { 130 if (addr < vm_map_base) 131 return 0; 132 133 return __set_memory(addr, numpages, __ 134 } 135 136 int set_memory_nx(unsigned long addr, int nump 137 { 138 if (addr < vm_map_base) 139 return 0; 140 141 return __set_memory(addr, numpages, __ 142 } 143 144 int set_memory_ro(unsigned long addr, int nump 145 { 146 if (addr < vm_map_base) 147 return 0; 148 149 return __set_memory(addr, numpages, __ 150 } 151 152 int set_memory_rw(unsigned long addr, int nump 153 { 154 if (addr < vm_map_base) 155 return 0; 156 157 return __set_memory(addr, numpages, __ 158 } 159 160 bool kernel_page_present(struct page *page) 161 { 162 pgd_t *pgd; 163 p4d_t *p4d; 164 pud_t *pud; 165 pmd_t *pmd; 166 pte_t *pte; 167 unsigned long addr = (unsigned long)pa 168 169 if (addr < vm_map_base) 170 return true; 171 172 pgd = pgd_offset_k(addr); 173 if (pgd_none(pgdp_get(pgd))) 174 return false; 175 if (pgd_leaf(pgdp_get(pgd))) 176 return true; 177 178 p4d = p4d_offset(pgd, addr); 179 if (p4d_none(p4dp_get(p4d))) 180 return false; 181 if (p4d_leaf(p4dp_get(p4d))) 182 return true; 183 184 pud = pud_offset(p4d, addr); 185 if (pud_none(pudp_get(pud))) 186 return false; 187 if (pud_leaf(pudp_get(pud))) 188 return true; 189 190 pmd = pmd_offset(pud, addr); 191 if (pmd_none(pmdp_get(pmd))) 192 return false; 193 if (pmd_leaf(pmdp_get(pmd))) 194 return true; 195 196 pte = pte_offset_kernel(pmd, addr); 197 return pte_present(ptep_get(pte)); 198 } 199 200 int set_direct_map_default_noflush(struct page 201 { 202 unsigned long addr = (unsigned long)pa 203 204 if (addr < vm_map_base) 205 return 0; 206 207 return __set_memory(addr, 1, PAGE_KERN 208 } 209 210 int set_direct_map_invalid_noflush(struct page 211 { 212 unsigned long addr = (unsigned long)pa 213 214 if (addr < vm_map_base) 215 return 0; 216 217 return __set_memory(addr, 1, __pgprot( 218 } 219
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.