pageattr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/highmem.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/processor.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/io.h>
  14. static inline pte_t *lookup_address(unsigned long address)
  15. {
  16. pgd_t *pgd = pgd_offset_k(address);
  17. pud_t *pud;
  18. pmd_t *pmd;
  19. pte_t *pte;
  20. if (pgd_none(*pgd))
  21. return NULL;
  22. pud = pud_offset(pgd, address);
  23. if (!pud_present(*pud))
  24. return NULL;
  25. pmd = pmd_offset(pud, address);
  26. if (!pmd_present(*pmd))
  27. return NULL;
  28. if (pmd_large(*pmd))
  29. return (pte_t *)pmd;
  30. pte = pte_offset_kernel(pmd, address);
  31. if (pte && !pte_present(*pte))
  32. pte = NULL;
  33. return pte;
  34. }
  35. static struct page *split_large_page(unsigned long address, pgprot_t prot,
  36. pgprot_t ref_prot)
  37. {
  38. int i;
  39. unsigned long addr;
  40. struct page *base = alloc_pages(GFP_KERNEL, 0);
  41. pte_t *pbase;
  42. if (!base)
  43. return NULL;
  44. /*
  45. * page_private is used to track the number of entries in
  46. * the page table page have non standard attributes.
  47. */
  48. SetPagePrivate(base);
  49. page_private(base) = 0;
  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,
  55. addr == address ? prot : ref_prot);
  56. }
  57. return base;
  58. }
  59. static void flush_kernel_map(void *address)
  60. {
  61. if (0 && address && cpu_has_clflush) {
  62. /* is this worth it? */
  63. int i;
  64. for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
  65. asm volatile("clflush (%0)" :: "r" (address + i));
  66. } else
  67. asm volatile("wbinvd":::"memory");
  68. if (address)
  69. __flush_tlb_one(address);
  70. else
  71. __flush_tlb_all();
  72. }
  73. static inline void flush_map(unsigned long address)
  74. {
  75. on_each_cpu(flush_kernel_map, (void *)address, 1, 1);
  76. }
  77. static struct page *deferred_pages; /* protected by init_mm.mmap_sem */
  78. static inline void save_page(struct page *fpage)
  79. {
  80. fpage->lru.next = (struct list_head *)deferred_pages;
  81. deferred_pages = fpage;
  82. }
  83. /*
  84. * No more special protections in this 2/4MB area - revert to a
  85. * large page again.
  86. */
  87. static void revert_page(unsigned long address, pgprot_t ref_prot)
  88. {
  89. pgd_t *pgd;
  90. pud_t *pud;
  91. pmd_t *pmd;
  92. pte_t large_pte;
  93. pgd = pgd_offset_k(address);
  94. BUG_ON(pgd_none(*pgd));
  95. pud = pud_offset(pgd,address);
  96. BUG_ON(pud_none(*pud));
  97. pmd = pmd_offset(pud, address);
  98. BUG_ON(pmd_val(*pmd) & _PAGE_PSE);
  99. large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
  100. large_pte = pte_mkhuge(large_pte);
  101. set_pte((pte_t *)pmd, large_pte);
  102. }
  103. static int
  104. __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
  105. pgprot_t ref_prot)
  106. {
  107. pte_t *kpte;
  108. struct page *kpte_page;
  109. pgprot_t ref_prot2;
  110. kpte = lookup_address(address);
  111. if (!kpte) return 0;
  112. kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
  113. if (pgprot_val(prot) != pgprot_val(ref_prot)) {
  114. if (!pte_huge(*kpte)) {
  115. set_pte(kpte, pfn_pte(pfn, prot));
  116. } else {
  117. /*
  118. * split_large_page will take the reference for this
  119. * change_page_attr on the split page.
  120. */
  121. struct page *split;
  122. ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
  123. split = split_large_page(address, prot, ref_prot2);
  124. if (!split)
  125. return -ENOMEM;
  126. set_pte(kpte, mk_pte(split, ref_prot2));
  127. kpte_page = split;
  128. }
  129. page_private(kpte_page)++;
  130. } else if (!pte_huge(*kpte)) {
  131. set_pte(kpte, pfn_pte(pfn, ref_prot));
  132. BUG_ON(page_private(kpte_page) == 0);
  133. page_private(kpte_page)--;
  134. } else
  135. BUG();
  136. /* on x86-64 the direct mapping set at boot is not using 4k pages */
  137. BUG_ON(PageReserved(kpte_page));
  138. if (page_private(kpte_page) == 0) {
  139. save_page(kpte_page);
  140. revert_page(address, ref_prot);
  141. }
  142. return 0;
  143. }
  144. /*
  145. * Change the page attributes of an page in the linear mapping.
  146. *
  147. * This should be used when a page is mapped with a different caching policy
  148. * than write-back somewhere - some CPUs do not like it when mappings with
  149. * different caching policies exist. This changes the page attributes of the
  150. * in kernel linear mapping too.
  151. *
  152. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  153. * This function only deals with the kernel linear map.
  154. *
  155. * Caller must call global_flush_tlb() after this.
  156. */
  157. int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
  158. {
  159. int err = 0;
  160. int i;
  161. down_write(&init_mm.mmap_sem);
  162. for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
  163. unsigned long pfn = __pa(address) >> PAGE_SHIFT;
  164. err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
  165. if (err)
  166. break;
  167. /* Handle kernel mapping too which aliases part of the
  168. * lowmem */
  169. if (__pa(address) < KERNEL_TEXT_SIZE) {
  170. unsigned long addr2;
  171. pgprot_t prot2;
  172. addr2 = __START_KERNEL_map + __pa(address);
  173. /* Make sure the kernel mappings stay executable */
  174. prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
  175. err = __change_page_attr(addr2, pfn, prot2,
  176. PAGE_KERNEL_EXEC);
  177. }
  178. }
  179. up_write(&init_mm.mmap_sem);
  180. return err;
  181. }
  182. /* Don't call this for MMIO areas that may not have a mem_map entry */
  183. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  184. {
  185. unsigned long addr = (unsigned long)page_address(page);
  186. return change_page_attr_addr(addr, numpages, prot);
  187. }
  188. void global_flush_tlb(void)
  189. {
  190. struct page *dpage;
  191. down_read(&init_mm.mmap_sem);
  192. dpage = xchg(&deferred_pages, NULL);
  193. up_read(&init_mm.mmap_sem);
  194. flush_map((dpage && !dpage->lru.next) ? (unsigned long)page_address(dpage) : 0);
  195. while (dpage) {
  196. struct page *tmp = dpage;
  197. dpage = (struct page *)dpage->lru.next;
  198. ClearPagePrivate(tmp);
  199. __free_page(tmp);
  200. }
  201. }
  202. EXPORT_SYMBOL(change_page_attr);
  203. EXPORT_SYMBOL(global_flush_tlb);