pageattr_64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/highmem.h>
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. void clflush_cache_range(void *addr, int size)
  11. {
  12. int i;
  13. for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
  14. clflush(addr+i);
  15. }
  16. #include <asm/processor.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/sections.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/pgalloc.h>
  21. pte_t *lookup_address(unsigned long address, int *level)
  22. {
  23. pgd_t *pgd = pgd_offset_k(address);
  24. pud_t *pud;
  25. pmd_t *pmd;
  26. if (pgd_none(*pgd))
  27. return NULL;
  28. pud = pud_offset(pgd, address);
  29. if (pud_none(*pud))
  30. return NULL;
  31. pmd = pmd_offset(pud, address);
  32. if (pmd_none(*pmd))
  33. return NULL;
  34. *level = 3;
  35. if (pmd_large(*pmd))
  36. return (pte_t *)pmd;
  37. *level = 4;
  38. return pte_offset_kernel(pmd, address);
  39. }
  40. static struct page *
  41. split_large_page(unsigned long address, pgprot_t ref_prot)
  42. {
  43. unsigned long addr;
  44. struct page *base;
  45. pte_t *pbase;
  46. int i;
  47. base = alloc_pages(GFP_KERNEL, 0);
  48. if (!base)
  49. return NULL;
  50. address = __pa(address);
  51. addr = address & LARGE_PAGE_MASK;
  52. pbase = (pte_t *)page_address(base);
  53. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
  54. pbase[i] = pfn_pte(addr >> PAGE_SHIFT, ref_prot);
  55. return base;
  56. }
  57. static int
  58. __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
  59. pgprot_t ref_prot)
  60. {
  61. struct page *kpte_page;
  62. pte_t *kpte;
  63. pgprot_t ref_prot2, oldprot;
  64. int level;
  65. repeat:
  66. kpte = lookup_address(address, &level);
  67. if (!kpte)
  68. return 0;
  69. kpte_page = virt_to_page(kpte);
  70. oldprot = pte_pgprot(*kpte);
  71. BUG_ON(PageLRU(kpte_page));
  72. BUG_ON(PageCompound(kpte_page));
  73. ref_prot = canon_pgprot(ref_prot);
  74. prot = canon_pgprot(prot);
  75. if (pgprot_val(prot) != pgprot_val(ref_prot)) {
  76. if (level == 4) {
  77. set_pte(kpte, pfn_pte(pfn, prot));
  78. } else {
  79. /*
  80. * split_large_page will take the reference for this
  81. * change_page_attr on the split page.
  82. */
  83. struct page *split;
  84. ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
  85. split = split_large_page(address, ref_prot2);
  86. if (!split)
  87. return -ENOMEM;
  88. pgprot_val(ref_prot2) &= ~_PAGE_NX;
  89. set_pte(kpte, mk_pte(split, ref_prot2));
  90. goto repeat;
  91. }
  92. } else {
  93. if (level == 4) {
  94. set_pte(kpte, pfn_pte(pfn, ref_prot));
  95. } else
  96. BUG();
  97. }
  98. return 0;
  99. }
  100. /**
  101. * change_page_attr_addr - Change page table attributes in linear mapping
  102. * @address: Virtual address in linear mapping.
  103. * @numpages: Number of pages to change
  104. * @prot: New page table attribute (PAGE_*)
  105. *
  106. * Change page attributes of a page in the direct mapping. This is a variant
  107. * of change_page_attr() that also works on memory holes that do not have
  108. * mem_map entry (pfn_valid() is false).
  109. *
  110. * See change_page_attr() documentation for more details.
  111. */
  112. int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
  113. {
  114. int err = 0, kernel_map = 0, i;
  115. if (address >= __START_KERNEL_map &&
  116. address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
  117. address = (unsigned long)__va(__pa(address));
  118. kernel_map = 1;
  119. }
  120. down_write(&init_mm.mmap_sem);
  121. for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
  122. unsigned long pfn = __pa(address) >> PAGE_SHIFT;
  123. if (!kernel_map || pte_present(pfn_pte(0, prot))) {
  124. err = __change_page_attr(address, pfn, prot,
  125. PAGE_KERNEL);
  126. if (err)
  127. break;
  128. }
  129. /* Handle kernel mapping too which aliases part of the
  130. * lowmem */
  131. if (__pa(address) < KERNEL_TEXT_SIZE) {
  132. unsigned long addr2;
  133. pgprot_t prot2;
  134. addr2 = __START_KERNEL_map + __pa(address);
  135. /* Make sure the kernel mappings stay executable */
  136. prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
  137. err = __change_page_attr(addr2, pfn, prot2,
  138. PAGE_KERNEL_EXEC);
  139. }
  140. }
  141. up_write(&init_mm.mmap_sem);
  142. return err;
  143. }
  144. /**
  145. * change_page_attr - Change page table attributes in the linear mapping.
  146. * @page: First page to change
  147. * @numpages: Number of pages to change
  148. * @prot: New protection/caching type (PAGE_*)
  149. *
  150. * Returns 0 on success, otherwise a negated errno.
  151. *
  152. * This should be used when a page is mapped with a different caching policy
  153. * than write-back somewhere - some CPUs do not like it when mappings with
  154. * different caching policies exist. This changes the page attributes of the
  155. * in kernel linear mapping too.
  156. *
  157. * Caller must call global_flush_tlb() later to make the changes active.
  158. *
  159. * The caller needs to ensure that there are no conflicting mappings elsewhere
  160. * (e.g. in user space) * This function only deals with the kernel linear map.
  161. *
  162. * For MMIO areas without mem_map use change_page_attr_addr() instead.
  163. */
  164. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  165. {
  166. unsigned long addr = (unsigned long)page_address(page);
  167. return change_page_attr_addr(addr, numpages, prot);
  168. }
  169. EXPORT_SYMBOL(change_page_attr);
  170. static void flush_kernel_map(void *arg)
  171. {
  172. /*
  173. * Flush all to work around Errata in early athlons regarding
  174. * large page flushing.
  175. */
  176. __flush_tlb_all();
  177. if (boot_cpu_data.x86_model >= 4)
  178. wbinvd();
  179. }
  180. void global_flush_tlb(void)
  181. {
  182. BUG_ON(irqs_disabled());
  183. on_each_cpu(flush_kernel_map, NULL, 1, 1);
  184. }
  185. EXPORT_SYMBOL(global_flush_tlb);