x86: add pgtable accessor functions for gbpages
[deliverable/linux.git] / arch / x86 / mm / pageattr.c
CommitLineData
9f4c815c
IM
1/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
1da177e4 3 * Thanks to Ben LaHaise for precious feedback.
9f4c815c 4 */
1da177e4 5#include <linux/highmem.h>
8192206d 6#include <linux/bootmem.h>
1da177e4 7#include <linux/module.h>
9f4c815c 8#include <linux/sched.h>
1da177e4 9#include <linux/slab.h>
9f4c815c
IM
10#include <linux/mm.h>
11
950f9d95 12#include <asm/e820.h>
1da177e4
LT
13#include <asm/processor.h>
14#include <asm/tlbflush.h>
f8af095d 15#include <asm/sections.h>
9f4c815c
IM
16#include <asm/uaccess.h>
17#include <asm/pgalloc.h>
1da177e4 18
72e458df
TG
19struct cpa_data {
20 unsigned long vaddr;
72e458df
TG
21 pgprot_t mask_set;
22 pgprot_t mask_clr;
65e074df 23 int numpages;
f4ae5da0 24 int flushtlb;
72e458df
TG
25};
26
65e074df
TG
27enum {
28 CPA_NO_SPLIT = 0,
29 CPA_SPLIT,
30};
31
ed724be6
AV
32static inline int
33within(unsigned long addr, unsigned long start, unsigned long end)
687c4825 34{
ed724be6
AV
35 return addr >= start && addr < end;
36}
37
d7c8f21a
TG
38/*
39 * Flushing functions
40 */
cd8ddf1a 41
cd8ddf1a
TG
42/**
43 * clflush_cache_range - flush a cache range with clflush
44 * @addr: virtual start address
45 * @size: number of bytes to flush
46 *
47 * clflush is an unordered instruction which needs fencing with mfence
48 * to avoid ordering issues.
49 */
4c61afcd 50void clflush_cache_range(void *vaddr, unsigned int size)
d7c8f21a 51{
4c61afcd 52 void *vend = vaddr + size - 1;
d7c8f21a 53
cd8ddf1a 54 mb();
4c61afcd
IM
55
56 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
57 clflush(vaddr);
58 /*
59 * Flush any possible final partial cacheline:
60 */
61 clflush(vend);
62
cd8ddf1a 63 mb();
d7c8f21a
TG
64}
65
af1e6844 66static void __cpa_flush_all(void *arg)
d7c8f21a 67{
6bb8383b
AK
68 unsigned long cache = (unsigned long)arg;
69
d7c8f21a
TG
70 /*
71 * Flush all to work around Errata in early athlons regarding
72 * large page flushing.
73 */
74 __flush_tlb_all();
75
6bb8383b 76 if (cache && boot_cpu_data.x86_model >= 4)
d7c8f21a
TG
77 wbinvd();
78}
79
6bb8383b 80static void cpa_flush_all(unsigned long cache)
d7c8f21a
TG
81{
82 BUG_ON(irqs_disabled());
83
6bb8383b 84 on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
d7c8f21a
TG
85}
86
57a6a46a
TG
87static void __cpa_flush_range(void *arg)
88{
57a6a46a
TG
89 /*
90 * We could optimize that further and do individual per page
91 * tlb invalidates for a low number of pages. Caveat: we must
92 * flush the high aliases on 64bit as well.
93 */
94 __flush_tlb_all();
57a6a46a
TG
95}
96
6bb8383b 97static void cpa_flush_range(unsigned long start, int numpages, int cache)
57a6a46a 98{
4c61afcd
IM
99 unsigned int i, level;
100 unsigned long addr;
101
57a6a46a 102 BUG_ON(irqs_disabled());
4c61afcd 103 WARN_ON(PAGE_ALIGN(start) != start);
57a6a46a 104
3b233e52 105 on_each_cpu(__cpa_flush_range, NULL, 1, 1);
57a6a46a 106
6bb8383b
AK
107 if (!cache)
108 return;
109
3b233e52
TG
110 /*
111 * We only need to flush on one CPU,
112 * clflush is a MESI-coherent instruction that
113 * will cause all other CPUs to flush the same
114 * cachelines:
115 */
4c61afcd
IM
116 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
117 pte_t *pte = lookup_address(addr, &level);
118
119 /*
120 * Only flush present addresses:
121 */
7bfb72e8 122 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
4c61afcd
IM
123 clflush_cache_range((void *) addr, PAGE_SIZE);
124 }
57a6a46a
TG
125}
126
cc0f21bb
AV
127#define HIGH_MAP_START __START_KERNEL_map
128#define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
129
130
131/*
132 * Converts a virtual address to a X86-64 highmap address
133 */
134static unsigned long virt_to_highmap(void *address)
135{
136#ifdef CONFIG_X86_64
137 return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
138#else
139 return (unsigned long)address;
140#endif
141}
142
ed724be6
AV
143/*
144 * Certain areas of memory on x86 require very specific protection flags,
145 * for example the BIOS area or kernel text. Callers don't always get this
146 * right (again, ioremap() on BIOS memory is not uncommon) so this function
147 * checks and fixes these known static required protection bits.
148 */
149static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
150{
151 pgprot_t forbidden = __pgprot(0);
152
687c4825 153 /*
ed724be6
AV
154 * The BIOS area between 640k and 1Mb needs to be executable for
155 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
687c4825 156 */
ed724be6
AV
157 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
158 pgprot_val(forbidden) |= _PAGE_NX;
159
160 /*
161 * The kernel text needs to be executable for obvious reasons
162 * Does not cover __inittext since that is gone later on
163 */
164 if (within(address, (unsigned long)_text, (unsigned long)_etext))
165 pgprot_val(forbidden) |= _PAGE_NX;
cc0f21bb
AV
166 /*
167 * Do the same for the x86-64 high kernel mapping
168 */
169 if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
170 pgprot_val(forbidden) |= _PAGE_NX;
171
ed724be6
AV
172
173#ifdef CONFIG_DEBUG_RODATA
174 /* The .rodata section needs to be read-only */
175 if (within(address, (unsigned long)__start_rodata,
176 (unsigned long)__end_rodata))
177 pgprot_val(forbidden) |= _PAGE_RW;
cc0f21bb
AV
178 /*
179 * Do the same for the x86-64 high kernel mapping
180 */
181 if (within(address, virt_to_highmap(__start_rodata),
182 virt_to_highmap(__end_rodata)))
183 pgprot_val(forbidden) |= _PAGE_RW;
ed724be6
AV
184#endif
185
186 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
687c4825
IM
187
188 return prot;
189}
190
9a14aefc
TG
191/*
192 * Lookup the page table entry for a virtual address. Return a pointer
193 * to the entry and the level of the mapping.
194 *
195 * Note: We return pud and pmd either when the entry is marked large
196 * or when the present bit is not set. Otherwise we would return a
197 * pointer to a nonexisting mapping.
198 */
f0646e43 199pte_t *lookup_address(unsigned long address, int *level)
9f4c815c 200{
1da177e4
LT
201 pgd_t *pgd = pgd_offset_k(address);
202 pud_t *pud;
203 pmd_t *pmd;
9f4c815c 204
30551bb3
TG
205 *level = PG_LEVEL_NONE;
206
1da177e4
LT
207 if (pgd_none(*pgd))
208 return NULL;
209 pud = pud_offset(pgd, address);
210 if (pud_none(*pud))
211 return NULL;
212 pmd = pmd_offset(pud, address);
213 if (pmd_none(*pmd))
214 return NULL;
30551bb3
TG
215
216 *level = PG_LEVEL_2M;
9a14aefc 217 if (pmd_large(*pmd) || !pmd_present(*pmd))
1da177e4 218 return (pte_t *)pmd;
1da177e4 219
30551bb3 220 *level = PG_LEVEL_4K;
9f4c815c
IM
221 return pte_offset_kernel(pmd, address);
222}
223
9a3dc780 224static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
9f4c815c 225{
9f4c815c
IM
226 /* change init_mm */
227 set_pte_atomic(kpte, pte);
44af6c41 228#ifdef CONFIG_X86_32
e4b71dcf 229 if (!SHARED_KERNEL_PMD) {
44af6c41
IM
230 struct page *page;
231
e3ed910d 232 list_for_each_entry(page, &pgd_list, lru) {
44af6c41
IM
233 pgd_t *pgd;
234 pud_t *pud;
235 pmd_t *pmd;
236
237 pgd = (pgd_t *)page_address(page) + pgd_index(address);
238 pud = pud_offset(pgd, address);
239 pmd = pmd_offset(pud, address);
240 set_pte_atomic((pte_t *)pmd, pte);
241 }
1da177e4 242 }
44af6c41 243#endif
1da177e4
LT
244}
245
65e074df
TG
246static int try_preserve_large_page(pte_t *kpte, unsigned long address,
247 struct cpa_data *cpa)
248{
249 unsigned long nextpage_addr, numpages, pmask, psize, flags;
250 pte_t new_pte, old_pte, *tmp;
251 pgprot_t old_prot, new_prot;
252 int level, res = CPA_SPLIT;
253
34508f66
IM
254 /*
255 * An Athlon 64 X2 showed hard hangs if we tried to preserve
256 * largepages and changed the PSE entry from RW to RO.
257 *
258 * As AMD CPUs have a long series of erratas in this area,
259 * (and none of the known ones seem to explain this hang),
260 * disable this code until the hang can be debugged:
261 */
262 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
263 return res;
264
65e074df
TG
265 spin_lock_irqsave(&pgd_lock, flags);
266 /*
267 * Check for races, another CPU might have split this page
268 * up already:
269 */
270 tmp = lookup_address(address, &level);
271 if (tmp != kpte)
272 goto out_unlock;
273
274 switch (level) {
275 case PG_LEVEL_2M:
31422c51
AK
276 psize = PMD_PAGE_SIZE;
277 pmask = PMD_PAGE_MASK;
65e074df
TG
278 break;
279 case PG_LEVEL_1G:
280 default:
281 res = -EINVAL;
282 goto out_unlock;
283 }
284
285 /*
286 * Calculate the number of pages, which fit into this large
287 * page starting at address:
288 */
289 nextpage_addr = (address + psize) & pmask;
290 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
291 if (numpages < cpa->numpages)
292 cpa->numpages = numpages;
293
294 /*
295 * We are safe now. Check whether the new pgprot is the same:
296 */
297 old_pte = *kpte;
298 old_prot = new_prot = pte_pgprot(old_pte);
299
300 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
301 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
302 new_prot = static_protections(new_prot, address);
303
304 /*
305 * If there are no changes, return. maxpages has been updated
306 * above:
307 */
308 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
309 res = CPA_NO_SPLIT;
310 goto out_unlock;
311 }
312
313 /*
314 * We need to change the attributes. Check, whether we can
315 * change the large page in one go. We request a split, when
316 * the address is not aligned and the number of pages is
317 * smaller than the number of pages in the large page. Note
318 * that we limited the number of possible pages already to
319 * the number of pages in the large page.
320 */
321 if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
322 /*
323 * The address is aligned and the number of pages
324 * covers the full page.
325 */
326 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
327 __set_pmd_pte(kpte, address, new_pte);
328 cpa->flushtlb = 1;
329 res = CPA_NO_SPLIT;
330 }
331
332out_unlock:
333 spin_unlock_irqrestore(&pgd_lock, flags);
334 return res;
335}
336
7afe15b9 337static int split_large_page(pte_t *kpte, unsigned long address)
bb5c2dbd 338{
07cf89c0 339 pgprot_t ref_prot;
12d6f21e 340 gfp_t gfp_flags = GFP_KERNEL;
63c1dcf4 341 unsigned long flags, addr, pfn;
bb5c2dbd
IM
342 pte_t *pbase, *tmp;
343 struct page *base;
86f03989 344 unsigned int i, level;
bb5c2dbd 345
12d6f21e 346#ifdef CONFIG_DEBUG_PAGEALLOC
86f03989 347 gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
12d6f21e
IM
348#endif
349 base = alloc_pages(gfp_flags, 0);
bb5c2dbd
IM
350 if (!base)
351 return -ENOMEM;
352
9a3dc780 353 spin_lock_irqsave(&pgd_lock, flags);
bb5c2dbd
IM
354 /*
355 * Check for races, another CPU might have split this page
356 * up for us already:
357 */
358 tmp = lookup_address(address, &level);
6ce9fc17 359 if (tmp != kpte)
bb5c2dbd
IM
360 goto out_unlock;
361
362 address = __pa(address);
31422c51 363 addr = address & PMD_PAGE_MASK;
bb5c2dbd 364 pbase = (pte_t *)page_address(base);
44af6c41 365#ifdef CONFIG_X86_32
bb5c2dbd 366 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
44af6c41 367#endif
07cf89c0 368 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
bb5c2dbd 369
63c1dcf4
TG
370 /*
371 * Get the target pfn from the original entry:
372 */
373 pfn = pte_pfn(*kpte);
374 for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
375 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
bb5c2dbd
IM
376
377 /*
07cf89c0 378 * Install the new, split up pagetable. Important details here:
4c881ca1
HY
379 *
380 * On Intel the NX bit of all levels must be cleared to make a
381 * page executable. See section 4.13.2 of Intel 64 and IA-32
382 * Architectures Software Developer's Manual).
07cf89c0
TG
383 *
384 * Mark the entry present. The current mapping might be
385 * set to not present, which we preserved above.
bb5c2dbd 386 */
4c881ca1 387 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
07cf89c0 388 pgprot_val(ref_prot) |= _PAGE_PRESENT;
9a3dc780 389 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
bb5c2dbd
IM
390 base = NULL;
391
392out_unlock:
9a3dc780 393 spin_unlock_irqrestore(&pgd_lock, flags);
bb5c2dbd
IM
394
395 if (base)
396 __free_pages(base, 0);
397
398 return 0;
399}
400
72e458df 401static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
9f4c815c 402{
1da177e4 403 struct page *kpte_page;
65e074df 404 int level, res;
9f4c815c 405 pte_t *kpte;
1da177e4 406
97f99fed 407repeat:
f0646e43 408 kpte = lookup_address(address, &level);
1da177e4
LT
409 if (!kpte)
410 return -EINVAL;
9f4c815c 411
1da177e4 412 kpte_page = virt_to_page(kpte);
65d2f0bc
AK
413 BUG_ON(PageLRU(kpte_page));
414 BUG_ON(PageCompound(kpte_page));
415
30551bb3 416 if (level == PG_LEVEL_4K) {
86f03989 417 pte_t new_pte, old_pte = *kpte;
626c2c9d
AV
418 pgprot_t new_prot = pte_pgprot(old_pte);
419
420 if(!pte_val(old_pte)) {
72e458df
TG
421 printk(KERN_WARNING "CPA: called for zero pte. "
422 "vaddr = %lx cpa->vaddr = %lx\n", address,
423 cpa->vaddr);
424 WARN_ON(1);
626c2c9d
AV
425 return -EINVAL;
426 }
86f03989 427
72e458df
TG
428 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
429 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
86f03989
IM
430
431 new_prot = static_protections(new_prot, address);
432
626c2c9d
AV
433 /*
434 * We need to keep the pfn from the existing PTE,
435 * after all we're only going to change it's attributes
436 * not the memory it points to
437 */
438 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
f4ae5da0
TG
439
440 /*
441 * Do we really change anything ?
442 */
443 if (pte_val(old_pte) != pte_val(new_pte)) {
444 set_pte_atomic(kpte, new_pte);
445 cpa->flushtlb = 1;
446 }
65e074df
TG
447 cpa->numpages = 1;
448 return 0;
1da177e4 449 }
65e074df
TG
450
451 /*
452 * Check, whether we can keep the large page intact
453 * and just change the pte:
454 */
455 res = try_preserve_large_page(kpte, address, cpa);
456 if (res < 0)
457 return res;
458
459 /*
460 * When the range fits into the existing large page,
461 * return. cp->numpages and cpa->tlbflush have been updated in
462 * try_large_page:
463 */
464 if (res == CPA_NO_SPLIT)
465 return 0;
466
467 /*
468 * We have to split the large page:
469 */
470 res = split_large_page(kpte, address);
471 if (res)
472 return res;
473 cpa->flushtlb = 1;
474 goto repeat;
9f4c815c 475}
1da177e4 476
44af6c41
IM
477/**
478 * change_page_attr_addr - Change page table attributes in linear mapping
479 * @address: Virtual address in linear mapping.
44af6c41 480 * @prot: New page table attribute (PAGE_*)
1da177e4 481 *
44af6c41
IM
482 * Change page attributes of a page in the direct mapping. This is a variant
483 * of change_page_attr() that also works on memory holes that do not have
484 * mem_map entry (pfn_valid() is false).
9f4c815c 485 *
44af6c41 486 * See change_page_attr() documentation for more details.
75cbade8
AV
487 *
488 * Modules and drivers should use the set_memory_* APIs instead.
1da177e4 489 */
44af6c41 490
72e458df 491static int change_page_attr_addr(struct cpa_data *cpa)
1da177e4 492{
0879750f 493 int err;
72e458df 494 unsigned long address = cpa->vaddr;
44af6c41
IM
495
496#ifdef CONFIG_X86_64
626c2c9d
AV
497 unsigned long phys_addr = __pa(address);
498
0879750f
TG
499 /*
500 * If we are inside the high mapped kernel range, then we
501 * fixup the low mapping first. __va() returns the virtual
502 * address in the linear mapping:
503 */
504 if (within(address, HIGH_MAP_START, HIGH_MAP_END))
505 address = (unsigned long) __va(phys_addr);
44af6c41
IM
506#endif
507
72e458df 508 err = __change_page_attr(address, cpa);
0879750f
TG
509 if (err)
510 return err;
44af6c41 511
44af6c41 512#ifdef CONFIG_X86_64
488fd995 513 /*
0879750f
TG
514 * If the physical address is inside the kernel map, we need
515 * to touch the high mapped kernel as well:
488fd995 516 */
0879750f
TG
517 if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
518 /*
519 * Calc the high mapping address. See __phys_addr()
520 * for the non obvious details.
cc0f21bb
AV
521 *
522 * Note that NX and other required permissions are
523 * checked in static_protections().
0879750f
TG
524 */
525 address = phys_addr + HIGH_MAP_START - phys_base;
0879750f 526
86f03989 527 /*
0879750f
TG
528 * Our high aliases are imprecise, because we check
529 * everything between 0 and KERNEL_TEXT_SIZE, so do
530 * not propagate lookup failures back to users:
86f03989 531 */
72e458df 532 __change_page_attr(address, cpa);
9f4c815c 533 }
488fd995 534#endif
1da177e4
LT
535 return err;
536}
537
72e458df 538static int __change_page_attr_set_clr(struct cpa_data *cpa)
ff31452b 539{
65e074df 540 int ret, numpages = cpa->numpages;
ff31452b 541
65e074df
TG
542 while (numpages) {
543 /*
544 * Store the remaining nr of pages for the large page
545 * preservation check.
546 */
547 cpa->numpages = numpages;
72e458df 548 ret = change_page_attr_addr(cpa);
ff31452b
TG
549 if (ret)
550 return ret;
ff31452b 551
65e074df
TG
552 /*
553 * Adjust the number of pages with the result of the
554 * CPA operation. Either a large page has been
555 * preserved or a single page update happened.
556 */
557 BUG_ON(cpa->numpages > numpages);
558 numpages -= cpa->numpages;
559 cpa->vaddr += cpa->numpages * PAGE_SIZE;
560 }
ff31452b
TG
561 return 0;
562}
563
6bb8383b
AK
564static inline int cache_attr(pgprot_t attr)
565{
566 return pgprot_val(attr) &
567 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
568}
569
ff31452b
TG
570static int change_page_attr_set_clr(unsigned long addr, int numpages,
571 pgprot_t mask_set, pgprot_t mask_clr)
572{
72e458df 573 struct cpa_data cpa;
6bb8383b 574 int ret, cache;
331e4065
TG
575
576 /*
577 * Check, if we are requested to change a not supported
578 * feature:
579 */
580 mask_set = canon_pgprot(mask_set);
581 mask_clr = canon_pgprot(mask_clr);
582 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
583 return 0;
584
72e458df
TG
585 cpa.vaddr = addr;
586 cpa.numpages = numpages;
587 cpa.mask_set = mask_set;
588 cpa.mask_clr = mask_clr;
f4ae5da0 589 cpa.flushtlb = 0;
72e458df
TG
590
591 ret = __change_page_attr_set_clr(&cpa);
ff31452b 592
f4ae5da0
TG
593 /*
594 * Check whether we really changed something:
595 */
596 if (!cpa.flushtlb)
597 return ret;
598
6bb8383b
AK
599 /*
600 * No need to flush, when we did not set any of the caching
601 * attributes:
602 */
603 cache = cache_attr(mask_set);
604
57a6a46a
TG
605 /*
606 * On success we use clflush, when the CPU supports it to
607 * avoid the wbindv. If the CPU does not support it and in the
af1e6844 608 * error case we fall back to cpa_flush_all (which uses
57a6a46a
TG
609 * wbindv):
610 */
611 if (!ret && cpu_has_clflush)
6bb8383b 612 cpa_flush_range(addr, numpages, cache);
57a6a46a 613 else
6bb8383b 614 cpa_flush_all(cache);
ff31452b
TG
615
616 return ret;
617}
618
56744546
TG
619static inline int change_page_attr_set(unsigned long addr, int numpages,
620 pgprot_t mask)
75cbade8 621{
56744546 622 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
75cbade8
AV
623}
624
56744546
TG
625static inline int change_page_attr_clear(unsigned long addr, int numpages,
626 pgprot_t mask)
72932c7a 627{
5827040d 628 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
72932c7a
TG
629}
630
631int set_memory_uc(unsigned long addr, int numpages)
632{
633 return change_page_attr_set(addr, numpages,
634 __pgprot(_PAGE_PCD | _PAGE_PWT));
75cbade8
AV
635}
636EXPORT_SYMBOL(set_memory_uc);
637
638int set_memory_wb(unsigned long addr, int numpages)
639{
72932c7a
TG
640 return change_page_attr_clear(addr, numpages,
641 __pgprot(_PAGE_PCD | _PAGE_PWT));
75cbade8
AV
642}
643EXPORT_SYMBOL(set_memory_wb);
644
645int set_memory_x(unsigned long addr, int numpages)
646{
72932c7a 647 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
75cbade8
AV
648}
649EXPORT_SYMBOL(set_memory_x);
650
651int set_memory_nx(unsigned long addr, int numpages)
652{
72932c7a 653 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
75cbade8
AV
654}
655EXPORT_SYMBOL(set_memory_nx);
656
657int set_memory_ro(unsigned long addr, int numpages)
658{
72932c7a 659 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
75cbade8 660}
75cbade8
AV
661
662int set_memory_rw(unsigned long addr, int numpages)
663{
72932c7a 664 return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
75cbade8 665}
f62d0f00
IM
666
667int set_memory_np(unsigned long addr, int numpages)
668{
72932c7a 669 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
f62d0f00 670}
75cbade8
AV
671
672int set_pages_uc(struct page *page, int numpages)
673{
674 unsigned long addr = (unsigned long)page_address(page);
75cbade8 675
d7c8f21a 676 return set_memory_uc(addr, numpages);
75cbade8
AV
677}
678EXPORT_SYMBOL(set_pages_uc);
679
680int set_pages_wb(struct page *page, int numpages)
681{
682 unsigned long addr = (unsigned long)page_address(page);
75cbade8 683
d7c8f21a 684 return set_memory_wb(addr, numpages);
75cbade8
AV
685}
686EXPORT_SYMBOL(set_pages_wb);
687
688int set_pages_x(struct page *page, int numpages)
689{
690 unsigned long addr = (unsigned long)page_address(page);
75cbade8 691
d7c8f21a 692 return set_memory_x(addr, numpages);
75cbade8
AV
693}
694EXPORT_SYMBOL(set_pages_x);
695
696int set_pages_nx(struct page *page, int numpages)
697{
698 unsigned long addr = (unsigned long)page_address(page);
75cbade8 699
d7c8f21a 700 return set_memory_nx(addr, numpages);
75cbade8
AV
701}
702EXPORT_SYMBOL(set_pages_nx);
703
704int set_pages_ro(struct page *page, int numpages)
705{
706 unsigned long addr = (unsigned long)page_address(page);
75cbade8 707
d7c8f21a 708 return set_memory_ro(addr, numpages);
75cbade8 709}
75cbade8
AV
710
711int set_pages_rw(struct page *page, int numpages)
712{
713 unsigned long addr = (unsigned long)page_address(page);
e81d5dc4 714
d7c8f21a 715 return set_memory_rw(addr, numpages);
78c94aba
IM
716}
717
1da177e4 718#ifdef CONFIG_DEBUG_PAGEALLOC
f62d0f00
IM
719
720static int __set_pages_p(struct page *page, int numpages)
721{
72e458df
TG
722 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
723 .numpages = numpages,
724 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
725 .mask_clr = __pgprot(0)};
72932c7a 726
72e458df 727 return __change_page_attr_set_clr(&cpa);
f62d0f00
IM
728}
729
730static int __set_pages_np(struct page *page, int numpages)
731{
72e458df
TG
732 struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
733 .numpages = numpages,
734 .mask_set = __pgprot(0),
735 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
72932c7a 736
72e458df 737 return __change_page_attr_set_clr(&cpa);
f62d0f00
IM
738}
739
1da177e4
LT
740void kernel_map_pages(struct page *page, int numpages, int enable)
741{
742 if (PageHighMem(page))
743 return;
9f4c815c 744 if (!enable) {
f9b8404c
IM
745 debug_check_no_locks_freed(page_address(page),
746 numpages * PAGE_SIZE);
9f4c815c 747 }
de5097c2 748
12d6f21e
IM
749 /*
750 * If page allocator is not up yet then do not call c_p_a():
751 */
752 if (!debug_pagealloc_enabled)
753 return;
754
9f4c815c 755 /*
e4b71dcf
IM
756 * The return value is ignored - the calls cannot fail,
757 * large pages are disabled at boot time:
1da177e4 758 */
f62d0f00
IM
759 if (enable)
760 __set_pages_p(page, numpages);
761 else
762 __set_pages_np(page, numpages);
9f4c815c
IM
763
764 /*
e4b71dcf
IM
765 * We should perform an IPI and flush all tlbs,
766 * but that can deadlock->flush only current cpu:
1da177e4
LT
767 */
768 __flush_tlb_all();
769}
770#endif
d1028a15
AV
771
772/*
773 * The testcases use internal knowledge of the implementation that shouldn't
774 * be exposed to the rest of the kernel. Include these directly here.
775 */
776#ifdef CONFIG_CPA_DEBUG
777#include "pageattr-test.c"
778#endif
This page took 0.473898 seconds and 5 git commands to generate.