pageattr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. pgprot_t ref_prot2;
  120. kpte = lookup_address(address);
  121. if (!kpte) return 0;
  122. kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
  123. kpte_flags = pte_val(*kpte);
  124. if (pgprot_val(prot) != pgprot_val(ref_prot)) {
  125. if ((kpte_flags & _PAGE_PSE) == 0) {
  126. set_pte(kpte, pfn_pte(pfn, prot));
  127. } else {
  128. /*
  129. * split_large_page will take the reference for this change_page_attr
  130. * on the split page.
  131. */
  132. struct page *split;
  133. ref_prot2 = __pgprot(pgprot_val(pte_pgprot(*lookup_address(address))) & ~(1<<_PAGE_BIT_PSE));
  134. split = split_large_page(address, prot, ref_prot2);
  135. if (!split)
  136. return -ENOMEM;
  137. set_pte(kpte,mk_pte(split, ref_prot2));
  138. kpte_page = split;
  139. }
  140. get_page(kpte_page);
  141. } else if ((kpte_flags & _PAGE_PSE) == 0) {
  142. set_pte(kpte, pfn_pte(pfn, ref_prot));
  143. __put_page(kpte_page);
  144. } else
  145. BUG();
  146. /* on x86-64 the direct mapping set at boot is not using 4k pages */
  147. BUG_ON(PageReserved(kpte_page));
  148. switch (page_count(kpte_page)) {
  149. case 1:
  150. save_page(address, kpte_page);
  151. revert_page(address, ref_prot);
  152. break;
  153. case 0:
  154. BUG(); /* memleak and failed 2M page regeneration */
  155. }
  156. return 0;
  157. }
  158. /*
  159. * Change the page attributes of an page in the linear mapping.
  160. *
  161. * This should be used when a page is mapped with a different caching policy
  162. * than write-back somewhere - some CPUs do not like it when mappings with
  163. * different caching policies exist. This changes the page attributes of the
  164. * in kernel linear mapping too.
  165. *
  166. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  167. * This function only deals with the kernel linear map.
  168. *
  169. * Caller must call global_flush_tlb() after this.
  170. */
  171. int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
  172. {
  173. int err = 0;
  174. int i;
  175. down_write(&init_mm.mmap_sem);
  176. for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
  177. unsigned long pfn = __pa(address) >> PAGE_SHIFT;
  178. err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
  179. if (err)
  180. break;
  181. /* Handle kernel mapping too which aliases part of the
  182. * lowmem */
  183. if (__pa(address) < KERNEL_TEXT_SIZE) {
  184. unsigned long addr2;
  185. pgprot_t prot2 = prot;
  186. addr2 = __START_KERNEL_map + __pa(address);
  187. pgprot_val(prot2) &= ~_PAGE_NX;
  188. err = __change_page_attr(addr2, pfn, prot2, PAGE_KERNEL_EXEC);
  189. }
  190. }
  191. up_write(&init_mm.mmap_sem);
  192. return err;
  193. }
  194. /* Don't call this for MMIO areas that may not have a mem_map entry */
  195. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  196. {
  197. unsigned long addr = (unsigned long)page_address(page);
  198. return change_page_attr_addr(addr, numpages, prot);
  199. }
  200. void global_flush_tlb(void)
  201. {
  202. struct deferred_page *df, *next_df;
  203. down_read(&init_mm.mmap_sem);
  204. df = xchg(&df_list, NULL);
  205. up_read(&init_mm.mmap_sem);
  206. flush_map((df && !df->next) ? df->address : 0);
  207. for (; df; df = next_df) {
  208. next_df = df->next;
  209. if (df->fpage)
  210. __free_page(df->fpage);
  211. kfree(df);
  212. }
  213. }
  214. EXPORT_SYMBOL(change_page_attr);
  215. EXPORT_SYMBOL(global_flush_tlb);