pageattr_32.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/pgalloc.h>
  14. #include <asm/sections.h>
  15. static DEFINE_SPINLOCK(cpa_lock);
  16. static struct list_head df_list = LIST_HEAD_INIT(df_list);
  17. pte_t *lookup_address(unsigned long address)
  18. {
  19. pgd_t *pgd = pgd_offset_k(address);
  20. pud_t *pud;
  21. pmd_t *pmd;
  22. if (pgd_none(*pgd))
  23. return NULL;
  24. pud = pud_offset(pgd, address);
  25. if (pud_none(*pud))
  26. return NULL;
  27. pmd = pmd_offset(pud, address);
  28. if (pmd_none(*pmd))
  29. return NULL;
  30. if (pmd_large(*pmd))
  31. return (pte_t *)pmd;
  32. return pte_offset_kernel(pmd, address);
  33. }
  34. static struct page *split_large_page(unsigned long address, pgprot_t prot,
  35. pgprot_t ref_prot)
  36. {
  37. int i;
  38. unsigned long addr;
  39. struct page *base;
  40. pte_t *pbase;
  41. spin_unlock_irq(&cpa_lock);
  42. base = alloc_pages(GFP_KERNEL, 0);
  43. spin_lock_irq(&cpa_lock);
  44. if (!base)
  45. return NULL;
  46. /*
  47. * page_private is used to track the number of entries in
  48. * the page table page that have non standard attributes.
  49. */
  50. SetPagePrivate(base);
  51. page_private(base) = 0;
  52. address = __pa(address);
  53. addr = address & LARGE_PAGE_MASK;
  54. pbase = (pte_t *)page_address(base);
  55. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  56. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
  57. set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT,
  58. addr == address ? prot : ref_prot));
  59. }
  60. return base;
  61. }
  62. static void cache_flush_page(struct page *p)
  63. {
  64. unsigned long adr = (unsigned long)page_address(p);
  65. int i;
  66. for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
  67. asm volatile("clflush (%0)" :: "r" (adr + i));
  68. }
  69. static void flush_kernel_map(void *arg)
  70. {
  71. struct list_head *lh = (struct list_head *)arg;
  72. struct page *p;
  73. /* High level code is not ready for clflush yet */
  74. if (0 && cpu_has_clflush) {
  75. list_for_each_entry (p, lh, lru)
  76. cache_flush_page(p);
  77. } else if (boot_cpu_data.x86_model >= 4)
  78. wbinvd();
  79. /* Flush all to work around Errata in early athlons regarding
  80. * large page flushing.
  81. */
  82. __flush_tlb_all();
  83. }
  84. static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  85. {
  86. struct page *page;
  87. unsigned long flags;
  88. set_pte_atomic(kpte, pte); /* change init_mm */
  89. if (SHARED_KERNEL_PMD)
  90. return;
  91. spin_lock_irqsave(&pgd_lock, flags);
  92. for (page = pgd_list; page; page = (struct page *)page->index) {
  93. pgd_t *pgd;
  94. pud_t *pud;
  95. pmd_t *pmd;
  96. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  97. pud = pud_offset(pgd, address);
  98. pmd = pmd_offset(pud, address);
  99. set_pte_atomic((pte_t *)pmd, pte);
  100. }
  101. spin_unlock_irqrestore(&pgd_lock, flags);
  102. }
  103. /*
  104. * No more special protections in this 2/4MB area - revert to a
  105. * large page again.
  106. */
  107. static inline void revert_page(struct page *kpte_page, unsigned long address)
  108. {
  109. pgprot_t ref_prot;
  110. pte_t *linear;
  111. ref_prot =
  112. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  113. ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
  114. linear = (pte_t *)
  115. pmd_offset(pud_offset(pgd_offset_k(address), address), address);
  116. set_pmd_pte(linear, address,
  117. pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
  118. ref_prot));
  119. }
  120. static inline void save_page(struct page *kpte_page)
  121. {
  122. if (!test_and_set_bit(PG_arch_1, &kpte_page->flags))
  123. list_add(&kpte_page->lru, &df_list);
  124. }
  125. static int
  126. __change_page_attr(struct page *page, pgprot_t prot)
  127. {
  128. pte_t *kpte;
  129. unsigned long address;
  130. struct page *kpte_page;
  131. BUG_ON(PageHighMem(page));
  132. address = (unsigned long)page_address(page);
  133. kpte = lookup_address(address);
  134. if (!kpte)
  135. return -EINVAL;
  136. kpte_page = virt_to_page(kpte);
  137. BUG_ON(PageLRU(kpte_page));
  138. BUG_ON(PageCompound(kpte_page));
  139. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) {
  140. if (!pte_huge(*kpte)) {
  141. set_pte_atomic(kpte, mk_pte(page, prot));
  142. } else {
  143. pgprot_t ref_prot;
  144. struct page *split;
  145. ref_prot =
  146. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  147. ? PAGE_KERNEL_EXEC : PAGE_KERNEL;
  148. split = split_large_page(address, prot, ref_prot);
  149. if (!split)
  150. return -ENOMEM;
  151. set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
  152. kpte_page = split;
  153. }
  154. page_private(kpte_page)++;
  155. } else if (!pte_huge(*kpte)) {
  156. set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
  157. BUG_ON(page_private(kpte_page) == 0);
  158. page_private(kpte_page)--;
  159. } else
  160. BUG();
  161. /*
  162. * If the pte was reserved, it means it was created at boot
  163. * time (not via split_large_page) and in turn we must not
  164. * replace it with a largepage.
  165. */
  166. save_page(kpte_page);
  167. if (!PageReserved(kpte_page)) {
  168. if (cpu_has_pse && (page_private(kpte_page) == 0)) {
  169. paravirt_release_pt(page_to_pfn(kpte_page));
  170. revert_page(kpte_page, address);
  171. }
  172. }
  173. return 0;
  174. }
  175. static inline void flush_map(struct list_head *l)
  176. {
  177. on_each_cpu(flush_kernel_map, l, 1, 1);
  178. }
  179. /*
  180. * Change the page attributes of an page in the linear mapping.
  181. *
  182. * This should be used when a page is mapped with a different caching policy
  183. * than write-back somewhere - some CPUs do not like it when mappings with
  184. * different caching policies exist. This changes the page attributes of the
  185. * in kernel linear mapping too.
  186. *
  187. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  188. * This function only deals with the kernel linear map.
  189. *
  190. * Caller must call global_flush_tlb() after this.
  191. */
  192. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  193. {
  194. int err = 0;
  195. int i;
  196. unsigned long flags;
  197. spin_lock_irqsave(&cpa_lock, flags);
  198. for (i = 0; i < numpages; i++, page++) {
  199. err = __change_page_attr(page, prot);
  200. if (err)
  201. break;
  202. }
  203. spin_unlock_irqrestore(&cpa_lock, flags);
  204. return err;
  205. }
  206. void global_flush_tlb(void)
  207. {
  208. struct list_head l;
  209. struct page *pg, *next;
  210. BUG_ON(irqs_disabled());
  211. spin_lock_irq(&cpa_lock);
  212. list_replace_init(&df_list, &l);
  213. spin_unlock_irq(&cpa_lock);
  214. flush_map(&l);
  215. list_for_each_entry_safe(pg, next, &l, lru) {
  216. list_del(&pg->lru);
  217. clear_bit(PG_arch_1, &pg->flags);
  218. if (PageReserved(pg) || !cpu_has_pse || page_private(pg) != 0)
  219. continue;
  220. ClearPagePrivate(pg);
  221. __free_page(pg);
  222. }
  223. }
  224. #ifdef CONFIG_DEBUG_PAGEALLOC
  225. void kernel_map_pages(struct page *page, int numpages, int enable)
  226. {
  227. if (PageHighMem(page))
  228. return;
  229. if (!enable)
  230. debug_check_no_locks_freed(page_address(page),
  231. numpages * PAGE_SIZE);
  232. /* the return value is ignored - the calls cannot fail,
  233. * large pages are disabled at boot time.
  234. */
  235. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  236. /* we should perform an IPI and flush all tlbs,
  237. * but that can deadlock->flush only current cpu.
  238. */
  239. __flush_tlb_all();
  240. }
  241. #endif
  242. EXPORT_SYMBOL(change_page_attr);
  243. EXPORT_SYMBOL(global_flush_tlb);