powerpc/mm/hash64: Map all the kernel regions in the same 0xc range
[linux-2.6-block.git] / arch / powerpc / mm / subpage-prot.c
CommitLineData
fa28237c
PM
1/*
2 * Copyright 2007-2008 Paul Mackerras, IBM Corp.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include <linux/errno.h>
11#include <linux/kernel.h>
12#include <linux/gfp.h>
fa28237c
PM
13#include <linux/types.h>
14#include <linux/mm.h>
15#include <linux/hugetlb.h>
3691d614 16#include <linux/syscalls.h>
fa28237c
PM
17
18#include <asm/pgtable.h>
7c0f6ba6 19#include <linux/uaccess.h>
fa28237c
PM
20
21/*
22 * Free all pages allocated for subpage protection maps and pointers.
23 * Also makes sure that the subpage_prot_table structure is
24 * reinitialized for the next user.
25 */
d28513bc 26void subpage_prot_free(struct mm_struct *mm)
fa28237c 27{
60458fba 28 struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
fa28237c
PM
29 unsigned long i, j, addr;
30 u32 **p;
31
ef629cc5
AK
32 if (!spt)
33 return;
34
fa28237c
PM
35 for (i = 0; i < 4; ++i) {
36 if (spt->low_prot[i]) {
37 free_page((unsigned long)spt->low_prot[i]);
38 spt->low_prot[i] = NULL;
39 }
40 }
41 addr = 0;
0da12a7a 42 for (i = 0; i < (TASK_SIZE_USER64 >> 43); ++i) {
fa28237c
PM
43 p = spt->protptrs[i];
44 if (!p)
45 continue;
46 spt->protptrs[i] = NULL;
47 for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
48 ++j, addr += PAGE_SIZE)
49 if (p[j])
50 free_page((unsigned long)p[j]);
51 free_page((unsigned long)p);
52 }
53 spt->maxaddr = 0;
ef629cc5 54 kfree(spt);
d28513bc
DG
55}
56
fa28237c
PM
57static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
58 int npages)
59{
60 pgd_t *pgd;
61 pud_t *pud;
62 pmd_t *pmd;
63 pte_t *pte;
64 spinlock_t *ptl;
65
66 pgd = pgd_offset(mm, addr);
67 if (pgd_none(*pgd))
68 return;
69 pud = pud_offset(pgd, addr);
70 if (pud_none(*pud))
71 return;
72 pmd = pmd_offset(pud, addr);
73 if (pmd_none(*pmd))
74 return;
75 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
76 arch_enter_lazy_mmu_mode();
77 for (; npages > 0; --npages) {
88247e8d 78 pte_update(mm, addr, pte, 0, 0, 0);
fa28237c
PM
79 addr += PAGE_SIZE;
80 ++pte;
81 }
82 arch_leave_lazy_mmu_mode();
83 pte_unmap_unlock(pte - 1, ptl);
84}
85
86/*
87 * Clear the subpage protection map for an address range, allowing
88 * all accesses that are allowed by the pte permissions.
89 */
90static void subpage_prot_clear(unsigned long addr, unsigned long len)
91{
92 struct mm_struct *mm = current->mm;
60458fba 93 struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
fa28237c 94 u32 **spm, *spp;
6b5e7229
JM
95 unsigned long i;
96 size_t nw;
fa28237c
PM
97 unsigned long next, limit;
98
ef629cc5
AK
99 if (!spt)
100 return ;
101
fa28237c
PM
102 down_write(&mm->mmap_sem);
103 limit = addr + len;
104 if (limit > spt->maxaddr)
105 limit = spt->maxaddr;
106 for (; addr < limit; addr = next) {
107 next = pmd_addr_end(addr, limit);
b0d436c7 108 if (addr < 0x100000000UL) {
fa28237c
PM
109 spm = spt->low_prot;
110 } else {
111 spm = spt->protptrs[addr >> SBP_L3_SHIFT];
112 if (!spm)
113 continue;
114 }
115 spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
116 if (!spp)
117 continue;
118 spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
119
120 i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
121 nw = PTRS_PER_PTE - i;
122 if (addr + (nw << PAGE_SHIFT) > next)
123 nw = (next - addr) >> PAGE_SHIFT;
124
125 memset(spp, 0, nw * sizeof(u32));
126
127 /* now flush any existing HPTEs for the range */
128 hpte_flush_range(mm, addr, nw);
129 }
130 up_write(&mm->mmap_sem);
131}
132
d8e355a2
AK
133#ifdef CONFIG_TRANSPARENT_HUGEPAGE
134static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
135 unsigned long end, struct mm_walk *walk)
136{
1757bbd9 137 struct vm_area_struct *vma = walk->vma;
78ddc534 138 split_huge_pmd(vma, pmd, addr);
d8e355a2
AK
139 return 0;
140}
141
142static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
143 unsigned long len)
144{
145 struct vm_area_struct *vma;
146 struct mm_walk subpage_proto_walk = {
147 .mm = mm,
148 .pmd_entry = subpage_walk_pmd_entry,
149 };
150
151 /*
152 * We don't try too hard, we just mark all the vma in that range
153 * VM_NOHUGEPAGE and split them.
154 */
155 vma = find_vma(mm, addr);
156 /*
157 * If the range is in unmapped range, just return
158 */
159 if (vma && ((addr + len) <= vma->vm_start))
160 return;
161
162 while (vma) {
163 if (vma->vm_start >= (addr + len))
164 break;
165 vma->vm_flags |= VM_NOHUGEPAGE;
1757bbd9 166 walk_page_vma(vma, &subpage_proto_walk);
d8e355a2
AK
167 vma = vma->vm_next;
168 }
169}
170#else
171static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
172 unsigned long len)
173{
174 return;
175}
176#endif
177
fa28237c
PM
178/*
179 * Copy in a subpage protection map for an address range.
180 * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
181 * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
182 * 2 or 3 to prevent all accesses.
183 * Note that the normal page protections also apply; the subpage
184 * protection mechanism is an additional constraint, so putting 0
185 * in a 2-bit field won't allow writes to a page that is otherwise
186 * write-protected.
187 */
3691d614
AV
188SYSCALL_DEFINE3(subpage_prot, unsigned long, addr,
189 unsigned long, len, u32 __user *, map)
fa28237c
PM
190{
191 struct mm_struct *mm = current->mm;
60458fba 192 struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
fa28237c 193 u32 **spm, *spp;
6b5e7229
JM
194 unsigned long i;
195 size_t nw;
fa28237c
PM
196 unsigned long next, limit;
197 int err;
198
5b2b8071
AK
199 if (radix_enabled())
200 return -ENOENT;
201
fa28237c
PM
202 /* Check parameters */
203 if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
be77e999
AK
204 addr >= mm->task_size || len >= mm->task_size ||
205 addr + len > mm->task_size)
fa28237c
PM
206 return -EINVAL;
207
208 if (is_hugepage_only_range(mm, addr, len))
209 return -EINVAL;
210
211 if (!map) {
212 /* Clear out the protection map for the address range */
213 subpage_prot_clear(addr, len);
214 return 0;
215 }
216
96d4f267 217 if (!access_ok(map, (len >> PAGE_SHIFT) * sizeof(u32)))
fa28237c
PM
218 return -EFAULT;
219
220 down_write(&mm->mmap_sem);
ef629cc5
AK
221
222 if (!spt) {
223 /*
224 * Allocate subpage prot table if not already done.
225 * Do this with mmap_sem held
226 */
227 spt = kzalloc(sizeof(struct subpage_prot_table), GFP_KERNEL);
228 if (!spt) {
229 err = -ENOMEM;
230 goto out;
231 }
232 mm->context.hash_context->spt = spt;
233 }
234
d8e355a2 235 subpage_mark_vma_nohuge(mm, addr, len);
fa28237c
PM
236 for (limit = addr + len; addr < limit; addr = next) {
237 next = pmd_addr_end(addr, limit);
238 err = -ENOMEM;
b0d436c7 239 if (addr < 0x100000000UL) {
fa28237c
PM
240 spm = spt->low_prot;
241 } else {
242 spm = spt->protptrs[addr >> SBP_L3_SHIFT];
243 if (!spm) {
244 spm = (u32 **)get_zeroed_page(GFP_KERNEL);
245 if (!spm)
246 goto out;
247 spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
248 }
249 }
250 spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
251 spp = *spm;
252 if (!spp) {
253 spp = (u32 *)get_zeroed_page(GFP_KERNEL);
254 if (!spp)
255 goto out;
256 *spm = spp;
257 }
258 spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
259
260 local_irq_disable();
261 demote_segment_4k(mm, addr);
262 local_irq_enable();
263
264 i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
265 nw = PTRS_PER_PTE - i;
266 if (addr + (nw << PAGE_SHIFT) > next)
267 nw = (next - addr) >> PAGE_SHIFT;
268
269 up_write(&mm->mmap_sem);
fa28237c 270 if (__copy_from_user(spp, map, nw * sizeof(u32)))
a967f161 271 return -EFAULT;
fa28237c
PM
272 map += nw;
273 down_write(&mm->mmap_sem);
274
275 /* now flush any existing HPTEs for the range */
276 hpte_flush_range(mm, addr, nw);
277 }
278 if (limit > spt->maxaddr)
279 spt->maxaddr = limit;
280 err = 0;
281 out:
282 up_write(&mm->mmap_sem);
fa28237c
PM
283 return err;
284}