1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/sched.h>
17#include <linux/vmalloc.h>
18
19#include <asm/pgtable.h>
20#include <asm/set_memory.h>
21#include <asm/tlbflush.h>
22
23struct page_change_data {
24 pgprot_t set_mask;
25 pgprot_t clear_mask;
26};
27
28static int change_page_range(pte_t *ptep, pgtable_t token, unsigned long addr,
29 void *data)
30{
31 struct page_change_data *cdata = data;
32 pte_t pte = READ_ONCE(*ptep);
33
34 pte = clear_pte_bit(pte, cdata->clear_mask);
35 pte = set_pte_bit(pte, cdata->set_mask);
36
37 set_pte(ptep, pte);
38 return 0;
39}
40
41
42
43
44static int __change_memory_common(unsigned long start, unsigned long size,
45 pgprot_t set_mask, pgprot_t clear_mask)
46{
47 struct page_change_data data;
48 int ret;
49
50 data.set_mask = set_mask;
51 data.clear_mask = clear_mask;
52
53 ret = apply_to_page_range(&init_mm, start, size, change_page_range,
54 &data);
55
56 flush_tlb_kernel_range(start, start + size);
57 return ret;
58}
59
60static int change_memory_common(unsigned long addr, int numpages,
61 pgprot_t set_mask, pgprot_t clear_mask)
62{
63 unsigned long start = addr;
64 unsigned long size = PAGE_SIZE*numpages;
65 unsigned long end = start + size;
66 struct vm_struct *area;
67
68 if (!PAGE_ALIGNED(addr)) {
69 start &= PAGE_MASK;
70 end = start + size;
71 WARN_ON_ONCE(1);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 area = find_vm_area((void *)addr);
88 if (!area ||
89 end > (unsigned long)area->addr + area->size ||
90 !(area->flags & VM_ALLOC))
91 return -EINVAL;
92
93 if (!numpages)
94 return 0;
95
96 return __change_memory_common(start, size, set_mask, clear_mask);
97}
98
99int set_memory_ro(unsigned long addr, int numpages)
100{
101 return change_memory_common(addr, numpages,
102 __pgprot(PTE_RDONLY),
103 __pgprot(PTE_WRITE));
104}
105
106int set_memory_rw(unsigned long addr, int numpages)
107{
108 return change_memory_common(addr, numpages,
109 __pgprot(PTE_WRITE),
110 __pgprot(PTE_RDONLY));
111}
112
113int set_memory_nx(unsigned long addr, int numpages)
114{
115 return change_memory_common(addr, numpages,
116 __pgprot(PTE_PXN),
117 __pgprot(0));
118}
119EXPORT_SYMBOL_GPL(set_memory_nx);
120
121int set_memory_x(unsigned long addr, int numpages)
122{
123 return change_memory_common(addr, numpages,
124 __pgprot(0),
125 __pgprot(PTE_PXN));
126}
127EXPORT_SYMBOL_GPL(set_memory_x);
128
129int set_memory_valid(unsigned long addr, int numpages, int enable)
130{
131 if (enable)
132 return __change_memory_common(addr, PAGE_SIZE * numpages,
133 __pgprot(PTE_VALID),
134 __pgprot(0));
135 else
136 return __change_memory_common(addr, PAGE_SIZE * numpages,
137 __pgprot(0),
138 __pgprot(PTE_VALID));
139}
140
141#ifdef CONFIG_DEBUG_PAGEALLOC
142void __kernel_map_pages(struct page *page, int numpages, int enable)
143{
144 set_memory_valid((unsigned long)page_address(page), numpages, enable);
145}
146#ifdef CONFIG_HIBERNATION
147
148
149
150
151
152
153
154
155
156
157bool kernel_page_present(struct page *page)
158{
159 pgd_t *pgdp;
160 pud_t *pudp, pud;
161 pmd_t *pmdp, pmd;
162 pte_t *ptep;
163 unsigned long addr = (unsigned long)page_address(page);
164
165 pgdp = pgd_offset_k(addr);
166 if (pgd_none(READ_ONCE(*pgdp)))
167 return false;
168
169 pudp = pud_offset(pgdp, addr);
170 pud = READ_ONCE(*pudp);
171 if (pud_none(pud))
172 return false;
173 if (pud_sect(pud))
174 return true;
175
176 pmdp = pmd_offset(pudp, addr);
177 pmd = READ_ONCE(*pmdp);
178 if (pmd_none(pmd))
179 return false;
180 if (pmd_sect(pmd))
181 return true;
182
183 ptep = pte_offset_kernel(pmdp, addr);
184 return pte_valid(READ_ONCE(*ptep));
185}
186#endif
187#endif
188