pageattr.c 5.8 KB

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