xref: /openbmc/linux/arch/s390/mm/pageattr.c (revision 78700c0a)
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/module.h>
7 #include <linux/mm.h>
8 #include <asm/cacheflush.h>
9 #include <asm/facility.h>
10 #include <asm/pgtable.h>
11 #include <asm/page.h>
12 
13 #if PAGE_DEFAULT_KEY
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 	while (start < end) {
26 		if (MACHINE_HAS_EDAT1) {
27 			/* set storage keys for a 1MB frame */
28 			size = 1UL << 20;
29 			boundary = (start + size) & ~(size - 1);
30 			if (boundary <= end) {
31 				do {
32 					start = sske_frame(start, PAGE_DEFAULT_KEY);
33 				} while (start < boundary);
34 				continue;
35 			}
36 		}
37 		page_set_storage_key(start, PAGE_DEFAULT_KEY, 0);
38 		start += PAGE_SIZE;
39 	}
40 }
41 #endif
42 
43 static pte_t *walk_page_table(unsigned long addr)
44 {
45 	pgd_t *pgdp;
46 	pud_t *pudp;
47 	pmd_t *pmdp;
48 	pte_t *ptep;
49 
50 	pgdp = pgd_offset_k(addr);
51 	if (pgd_none(*pgdp))
52 		return NULL;
53 	pudp = pud_offset(pgdp, addr);
54 	if (pud_none(*pudp) || pud_large(*pudp))
55 		return NULL;
56 	pmdp = pmd_offset(pudp, addr);
57 	if (pmd_none(*pmdp) || pmd_large(*pmdp))
58 		return NULL;
59 	ptep = pte_offset_kernel(pmdp, addr);
60 	if (pte_none(*ptep))
61 		return NULL;
62 	return ptep;
63 }
64 
65 static void change_page_attr(unsigned long addr, int numpages,
66 			     pte_t (*set) (pte_t))
67 {
68 	pte_t *ptep;
69 	int i;
70 
71 	for (i = 0; i < numpages; i++) {
72 		ptep = walk_page_table(addr);
73 		if (WARN_ON_ONCE(!ptep))
74 			break;
75 		*ptep = set(*ptep);
76 		addr += PAGE_SIZE;
77 	}
78 	__tlb_flush_kernel();
79 }
80 
81 int set_memory_ro(unsigned long addr, int numpages)
82 {
83 	change_page_attr(addr, numpages, pte_wrprotect);
84 	return 0;
85 }
86 
87 int set_memory_rw(unsigned long addr, int numpages)
88 {
89 	change_page_attr(addr, numpages, pte_mkwrite);
90 	return 0;
91 }
92 
93 /* not possible */
94 int set_memory_nx(unsigned long addr, int numpages)
95 {
96 	return 0;
97 }
98 
99 int set_memory_x(unsigned long addr, int numpages)
100 {
101 	return 0;
102 }
103 
104 #ifdef CONFIG_DEBUG_PAGEALLOC
105 
106 static void ipte_range(pte_t *pte, unsigned long address, int nr)
107 {
108 	int i;
109 
110 	if (test_facility(13)) {
111 		__ptep_ipte_range(address, nr - 1, pte);
112 		return;
113 	}
114 	for (i = 0; i < nr; i++) {
115 		__ptep_ipte(address, pte);
116 		address += PAGE_SIZE;
117 		pte++;
118 	}
119 }
120 
121 void __kernel_map_pages(struct page *page, int numpages, int enable)
122 {
123 	unsigned long address;
124 	int nr, i, j;
125 	pgd_t *pgd;
126 	pud_t *pud;
127 	pmd_t *pmd;
128 	pte_t *pte;
129 
130 	for (i = 0; i < numpages;) {
131 		address = page_to_phys(page + i);
132 		pgd = pgd_offset_k(address);
133 		pud = pud_offset(pgd, address);
134 		pmd = pmd_offset(pud, address);
135 		pte = pte_offset_kernel(pmd, address);
136 		nr = (unsigned long)pte >> ilog2(sizeof(long));
137 		nr = PTRS_PER_PTE - (nr & (PTRS_PER_PTE - 1));
138 		nr = min(numpages - i, nr);
139 		if (enable) {
140 			for (j = 0; j < nr; j++) {
141 				pte_val(*pte) = __pa(address);
142 				address += PAGE_SIZE;
143 				pte++;
144 			}
145 		} else {
146 			ipte_range(pte, address, nr);
147 		}
148 		i += nr;
149 	}
150 }
151 
152 #ifdef CONFIG_HIBERNATION
153 bool kernel_page_present(struct page *page)
154 {
155 	unsigned long addr;
156 	int cc;
157 
158 	addr = page_to_phys(page);
159 	asm volatile(
160 		"	lra	%1,0(%1)\n"
161 		"	ipm	%0\n"
162 		"	srl	%0,28"
163 		: "=d" (cc), "+a" (addr) : : "cc");
164 	return cc == 0;
165 }
166 #endif /* CONFIG_HIBERNATION */
167 
168 #endif /* CONFIG_DEBUG_PAGEALLOC */
169