xref: /openbmc/linux/arch/s390/mm/pageattr.c (revision 8ee90c5c)
1 /*
2  * Copyright IBM Corp. 2011
3  * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
4  */
5 #include <linux/hugetlb.h>
6 #include <linux/mm.h>
7 #include <asm/cacheflush.h>
8 #include <asm/facility.h>
9 #include <asm/pgtable.h>
10 #include <asm/pgalloc.h>
11 #include <asm/page.h>
12 #include <asm/set_memory.h>
13 
14 static inline unsigned long sske_frame(unsigned long addr, unsigned char skey)
15 {
16 	asm volatile(".insn rrf,0xb22b0000,%[skey],%[addr],9,0"
17 		     : [addr] "+a" (addr) : [skey] "d" (skey));
18 	return addr;
19 }
20 
21 void __storage_key_init_range(unsigned long start, unsigned long end)
22 {
23 	unsigned long boundary, size;
24 
25 	if (!PAGE_DEFAULT_KEY)
26 		return;
27 	while (start < end) {
28 		if (MACHINE_HAS_EDAT1) {
29 			/* set storage keys for a 1MB frame */
30 			size = 1UL << 20;
31 			boundary = (start + size) & ~(size - 1);
32 			if (boundary <= end) {
33 				do {
34 					start = sske_frame(start, PAGE_DEFAULT_KEY);
35 				} while (start < boundary);
36 				continue;
37 			}
38 		}
39 		page_set_storage_key(start, PAGE_DEFAULT_KEY, 0);
40 		start += PAGE_SIZE;
41 	}
42 }
43 
44 #ifdef CONFIG_PROC_FS
45 atomic_long_t direct_pages_count[PG_DIRECT_MAP_MAX];
46 
47 void arch_report_meminfo(struct seq_file *m)
48 {
49 	seq_printf(m, "DirectMap4k:    %8lu kB\n",
50 		   atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_4K]) << 2);
51 	seq_printf(m, "DirectMap1M:    %8lu kB\n",
52 		   atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_1M]) << 10);
53 	seq_printf(m, "DirectMap2G:    %8lu kB\n",
54 		   atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_2G]) << 21);
55 }
56 #endif /* CONFIG_PROC_FS */
57 
58 static void pgt_set(unsigned long *old, unsigned long new, unsigned long addr,
59 		    unsigned long dtt)
60 {
61 	unsigned long table, mask;
62 
63 	mask = 0;
64 	if (MACHINE_HAS_EDAT2) {
65 		switch (dtt) {
66 		case CRDTE_DTT_REGION3:
67 			mask = ~(PTRS_PER_PUD * sizeof(pud_t) - 1);
68 			break;
69 		case CRDTE_DTT_SEGMENT:
70 			mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
71 			break;
72 		case CRDTE_DTT_PAGE:
73 			mask = ~(PTRS_PER_PTE * sizeof(pte_t) - 1);
74 			break;
75 		}
76 		table = (unsigned long)old & mask;
77 		crdte(*old, new, table, dtt, addr, S390_lowcore.kernel_asce);
78 	} else if (MACHINE_HAS_IDTE) {
79 		cspg(old, *old, new);
80 	} else {
81 		csp((unsigned int *)old + 1, *old, new);
82 	}
83 }
84 
85 static int walk_pte_level(pmd_t *pmdp, unsigned long addr, unsigned long end,
86 			  unsigned long flags)
87 {
88 	pte_t *ptep, new;
89 
90 	ptep = pte_offset(pmdp, addr);
91 	do {
92 		new = *ptep;
93 		if (pte_none(new))
94 			return -EINVAL;
95 		if (flags & SET_MEMORY_RO)
96 			new = pte_wrprotect(new);
97 		else if (flags & SET_MEMORY_RW)
98 			new = pte_mkwrite(pte_mkdirty(new));
99 		if (flags & SET_MEMORY_NX)
100 			pte_val(new) |= _PAGE_NOEXEC;
101 		else if (flags & SET_MEMORY_X)
102 			pte_val(new) &= ~_PAGE_NOEXEC;
103 		pgt_set((unsigned long *)ptep, pte_val(new), addr, CRDTE_DTT_PAGE);
104 		ptep++;
105 		addr += PAGE_SIZE;
106 		cond_resched();
107 	} while (addr < end);
108 	return 0;
109 }
110 
111 static int split_pmd_page(pmd_t *pmdp, unsigned long addr)
112 {
113 	unsigned long pte_addr, prot;
114 	pte_t *pt_dir, *ptep;
115 	pmd_t new;
116 	int i, ro, nx;
117 
118 	pt_dir = vmem_pte_alloc();
119 	if (!pt_dir)
120 		return -ENOMEM;
121 	pte_addr = pmd_pfn(*pmdp) << PAGE_SHIFT;
122 	ro = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT);
123 	nx = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_NOEXEC);
124 	prot = pgprot_val(ro ? PAGE_KERNEL_RO : PAGE_KERNEL);
125 	if (!nx)
126 		prot &= ~_PAGE_NOEXEC;
127 	ptep = pt_dir;
128 	for (i = 0; i < PTRS_PER_PTE; i++) {
129 		pte_val(*ptep) = pte_addr | prot;
130 		pte_addr += PAGE_SIZE;
131 		ptep++;
132 	}
133 	pmd_val(new) = __pa(pt_dir) | _SEGMENT_ENTRY;
134 	pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT);
135 	update_page_count(PG_DIRECT_MAP_4K, PTRS_PER_PTE);
136 	update_page_count(PG_DIRECT_MAP_1M, -1);
137 	return 0;
138 }
139 
140 static void modify_pmd_page(pmd_t *pmdp, unsigned long addr,
141 			    unsigned long flags)
142 {
143 	pmd_t new = *pmdp;
144 
145 	if (flags & SET_MEMORY_RO)
146 		new = pmd_wrprotect(new);
147 	else if (flags & SET_MEMORY_RW)
148 		new = pmd_mkwrite(pmd_mkdirty(new));
149 	if (flags & SET_MEMORY_NX)
150 		pmd_val(new) |= _SEGMENT_ENTRY_NOEXEC;
151 	else if (flags & SET_MEMORY_X)
152 		pmd_val(new) &= ~_SEGMENT_ENTRY_NOEXEC;
153 	pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT);
154 }
155 
156 static int walk_pmd_level(pud_t *pudp, unsigned long addr, unsigned long end,
157 			  unsigned long flags)
158 {
159 	unsigned long next;
160 	pmd_t *pmdp;
161 	int rc = 0;
162 
163 	pmdp = pmd_offset(pudp, addr);
164 	do {
165 		if (pmd_none(*pmdp))
166 			return -EINVAL;
167 		next = pmd_addr_end(addr, end);
168 		if (pmd_large(*pmdp)) {
169 			if (addr & ~PMD_MASK || addr + PMD_SIZE > next) {
170 				rc = split_pmd_page(pmdp, addr);
171 				if (rc)
172 					return rc;
173 				continue;
174 			}
175 			modify_pmd_page(pmdp, addr, flags);
176 		} else {
177 			rc = walk_pte_level(pmdp, addr, next, flags);
178 			if (rc)
179 				return rc;
180 		}
181 		pmdp++;
182 		addr = next;
183 		cond_resched();
184 	} while (addr < end);
185 	return rc;
186 }
187 
188 static int split_pud_page(pud_t *pudp, unsigned long addr)
189 {
190 	unsigned long pmd_addr, prot;
191 	pmd_t *pm_dir, *pmdp;
192 	pud_t new;
193 	int i, ro, nx;
194 
195 	pm_dir = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY);
196 	if (!pm_dir)
197 		return -ENOMEM;
198 	pmd_addr = pud_pfn(*pudp) << PAGE_SHIFT;
199 	ro = !!(pud_val(*pudp) & _REGION_ENTRY_PROTECT);
200 	nx = !!(pud_val(*pudp) & _REGION_ENTRY_NOEXEC);
201 	prot = pgprot_val(ro ? SEGMENT_KERNEL_RO : SEGMENT_KERNEL);
202 	if (!nx)
203 		prot &= ~_SEGMENT_ENTRY_NOEXEC;
204 	pmdp = pm_dir;
205 	for (i = 0; i < PTRS_PER_PMD; i++) {
206 		pmd_val(*pmdp) = pmd_addr | prot;
207 		pmd_addr += PMD_SIZE;
208 		pmdp++;
209 	}
210 	pud_val(new) = __pa(pm_dir) | _REGION3_ENTRY;
211 	pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3);
212 	update_page_count(PG_DIRECT_MAP_1M, PTRS_PER_PMD);
213 	update_page_count(PG_DIRECT_MAP_2G, -1);
214 	return 0;
215 }
216 
217 static void modify_pud_page(pud_t *pudp, unsigned long addr,
218 			    unsigned long flags)
219 {
220 	pud_t new = *pudp;
221 
222 	if (flags & SET_MEMORY_RO)
223 		new = pud_wrprotect(new);
224 	else if (flags & SET_MEMORY_RW)
225 		new = pud_mkwrite(pud_mkdirty(new));
226 	if (flags & SET_MEMORY_NX)
227 		pud_val(new) |= _REGION_ENTRY_NOEXEC;
228 	else if (flags & SET_MEMORY_X)
229 		pud_val(new) &= ~_REGION_ENTRY_NOEXEC;
230 	pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3);
231 }
232 
233 static int walk_pud_level(p4d_t *p4d, unsigned long addr, unsigned long end,
234 			  unsigned long flags)
235 {
236 	unsigned long next;
237 	pud_t *pudp;
238 	int rc = 0;
239 
240 	pudp = pud_offset(p4d, addr);
241 	do {
242 		if (pud_none(*pudp))
243 			return -EINVAL;
244 		next = pud_addr_end(addr, end);
245 		if (pud_large(*pudp)) {
246 			if (addr & ~PUD_MASK || addr + PUD_SIZE > next) {
247 				rc = split_pud_page(pudp, addr);
248 				if (rc)
249 					break;
250 				continue;
251 			}
252 			modify_pud_page(pudp, addr, flags);
253 		} else {
254 			rc = walk_pmd_level(pudp, addr, next, flags);
255 		}
256 		pudp++;
257 		addr = next;
258 		cond_resched();
259 	} while (addr < end && !rc);
260 	return rc;
261 }
262 
263 static int walk_p4d_level(pgd_t *pgd, unsigned long addr, unsigned long end,
264 			  unsigned long flags)
265 {
266 	unsigned long next;
267 	p4d_t *p4dp;
268 	int rc = 0;
269 
270 	p4dp = p4d_offset(pgd, addr);
271 	do {
272 		if (p4d_none(*p4dp))
273 			return -EINVAL;
274 		next = p4d_addr_end(addr, end);
275 		rc = walk_pud_level(p4dp, addr, next, flags);
276 		p4dp++;
277 		addr = next;
278 		cond_resched();
279 	} while (addr < end && !rc);
280 	return rc;
281 }
282 
283 static DEFINE_MUTEX(cpa_mutex);
284 
285 static int change_page_attr(unsigned long addr, unsigned long end,
286 			    unsigned long flags)
287 {
288 	unsigned long next;
289 	int rc = -EINVAL;
290 	pgd_t *pgdp;
291 
292 	if (addr == end)
293 		return 0;
294 	if (end >= MODULES_END)
295 		return -EINVAL;
296 	mutex_lock(&cpa_mutex);
297 	pgdp = pgd_offset_k(addr);
298 	do {
299 		if (pgd_none(*pgdp))
300 			break;
301 		next = pgd_addr_end(addr, end);
302 		rc = walk_p4d_level(pgdp, addr, next, flags);
303 		if (rc)
304 			break;
305 		cond_resched();
306 	} while (pgdp++, addr = next, addr < end && !rc);
307 	mutex_unlock(&cpa_mutex);
308 	return rc;
309 }
310 
311 int __set_memory(unsigned long addr, int numpages, unsigned long flags)
312 {
313 	if (!MACHINE_HAS_NX)
314 		flags &= ~(SET_MEMORY_NX | SET_MEMORY_X);
315 	if (!flags)
316 		return 0;
317 	addr &= PAGE_MASK;
318 	return change_page_attr(addr, addr + numpages * PAGE_SIZE, flags);
319 }
320 
321 #ifdef CONFIG_DEBUG_PAGEALLOC
322 
323 static void ipte_range(pte_t *pte, unsigned long address, int nr)
324 {
325 	int i;
326 
327 	if (test_facility(13)) {
328 		__ptep_ipte_range(address, nr - 1, pte, IPTE_GLOBAL);
329 		return;
330 	}
331 	for (i = 0; i < nr; i++) {
332 		__ptep_ipte(address, pte, 0, 0, IPTE_GLOBAL);
333 		address += PAGE_SIZE;
334 		pte++;
335 	}
336 }
337 
338 void __kernel_map_pages(struct page *page, int numpages, int enable)
339 {
340 	unsigned long address;
341 	int nr, i, j;
342 	pgd_t *pgd;
343 	p4d_t *p4d;
344 	pud_t *pud;
345 	pmd_t *pmd;
346 	pte_t *pte;
347 
348 	for (i = 0; i < numpages;) {
349 		address = page_to_phys(page + i);
350 		pgd = pgd_offset_k(address);
351 		p4d = p4d_offset(pgd, address);
352 		pud = pud_offset(p4d, address);
353 		pmd = pmd_offset(pud, address);
354 		pte = pte_offset_kernel(pmd, address);
355 		nr = (unsigned long)pte >> ilog2(sizeof(long));
356 		nr = PTRS_PER_PTE - (nr & (PTRS_PER_PTE - 1));
357 		nr = min(numpages - i, nr);
358 		if (enable) {
359 			for (j = 0; j < nr; j++) {
360 				pte_val(*pte) &= ~_PAGE_INVALID;
361 				address += PAGE_SIZE;
362 				pte++;
363 			}
364 		} else {
365 			ipte_range(pte, address, nr);
366 		}
367 		i += nr;
368 	}
369 }
370 
371 #ifdef CONFIG_HIBERNATION
372 bool kernel_page_present(struct page *page)
373 {
374 	unsigned long addr;
375 	int cc;
376 
377 	addr = page_to_phys(page);
378 	asm volatile(
379 		"	lra	%1,0(%1)\n"
380 		"	ipm	%0\n"
381 		"	srl	%0,28"
382 		: "=d" (cc), "+a" (addr) : : "cc");
383 	return cc == 0;
384 }
385 #endif /* CONFIG_HIBERNATION */
386 
387 #endif /* CONFIG_DEBUG_PAGEALLOC */
388