pageattr_64.c 5.2 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 prot, 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,
  55. addr == address ? prot : ref_prot);
  56. }
  57. return base;
  58. }
  59. static int
  60. __change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
  61. pgprot_t ref_prot)
  62. {
  63. struct page *kpte_page;
  64. pte_t *kpte;
  65. pgprot_t ref_prot2, oldprot;
  66. int level;
  67. kpte = lookup_address(address, &level);
  68. if (!kpte)
  69. return 0;
  70. kpte_page = virt_to_page(kpte);
  71. oldprot = pte_pgprot(*kpte);
  72. BUG_ON(PageLRU(kpte_page));
  73. BUG_ON(PageCompound(kpte_page));
  74. ref_prot = canon_pgprot(ref_prot);
  75. prot = canon_pgprot(prot);
  76. if (pgprot_val(prot) != pgprot_val(ref_prot)) {
  77. if (level == 4) {
  78. set_pte(kpte, pfn_pte(pfn, prot));
  79. } else {
  80. /*
  81. * split_large_page will take the reference for this
  82. * change_page_attr on the split page.
  83. */
  84. struct page *split;
  85. ref_prot2 = pte_pgprot(pte_clrhuge(*kpte));
  86. split = split_large_page(address, prot, ref_prot2);
  87. if (!split)
  88. return -ENOMEM;
  89. pgprot_val(ref_prot2) &= ~_PAGE_NX;
  90. set_pte(kpte, mk_pte(split, ref_prot2));
  91. kpte_page = split;
  92. }
  93. } else {
  94. if (level == 4) {
  95. set_pte(kpte, pfn_pte(pfn, ref_prot));
  96. } else
  97. BUG();
  98. }
  99. return 0;
  100. }
  101. /**
  102. * change_page_attr_addr - Change page table attributes in linear mapping
  103. * @address: Virtual address in linear mapping.
  104. * @numpages: Number of pages to change
  105. * @prot: New page table attribute (PAGE_*)
  106. *
  107. * Change page attributes of a page in the direct mapping. This is a variant
  108. * of change_page_attr() that also works on memory holes that do not have
  109. * mem_map entry (pfn_valid() is false).
  110. *
  111. * See change_page_attr() documentation for more details.
  112. */
  113. int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
  114. {
  115. int err = 0, kernel_map = 0, i;
  116. if (address >= __START_KERNEL_map &&
  117. address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
  118. address = (unsigned long)__va(__pa(address));
  119. kernel_map = 1;
  120. }
  121. down_write(&init_mm.mmap_sem);
  122. for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
  123. unsigned long pfn = __pa(address) >> PAGE_SHIFT;
  124. if (!kernel_map || pte_present(pfn_pte(0, prot))) {
  125. err = __change_page_attr(address, pfn, prot,
  126. PAGE_KERNEL);
  127. if (err)
  128. break;
  129. }
  130. /* Handle kernel mapping too which aliases part of the
  131. * lowmem */
  132. if (__pa(address) < KERNEL_TEXT_SIZE) {
  133. unsigned long addr2;
  134. pgprot_t prot2;
  135. addr2 = __START_KERNEL_map + __pa(address);
  136. /* Make sure the kernel mappings stay executable */
  137. prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
  138. err = __change_page_attr(addr2, pfn, prot2,
  139. PAGE_KERNEL_EXEC);
  140. }
  141. }
  142. up_write(&init_mm.mmap_sem);
  143. return err;
  144. }
  145. /**
  146. * change_page_attr - Change page table attributes in the linear mapping.
  147. * @page: First page to change
  148. * @numpages: Number of pages to change
  149. * @prot: New protection/caching type (PAGE_*)
  150. *
  151. * Returns 0 on success, otherwise a negated errno.
  152. *
  153. * This should be used when a page is mapped with a different caching policy
  154. * than write-back somewhere - some CPUs do not like it when mappings with
  155. * different caching policies exist. This changes the page attributes of the
  156. * in kernel linear mapping too.
  157. *
  158. * Caller must call global_flush_tlb() later to make the changes active.
  159. *
  160. * The caller needs to ensure that there are no conflicting mappings elsewhere
  161. * (e.g. in user space) * This function only deals with the kernel linear map.
  162. *
  163. * For MMIO areas without mem_map use change_page_attr_addr() instead.
  164. */
  165. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  166. {
  167. unsigned long addr = (unsigned long)page_address(page);
  168. return change_page_attr_addr(addr, numpages, prot);
  169. }
  170. EXPORT_SYMBOL(change_page_attr);
  171. static void flush_kernel_map(void *arg)
  172. {
  173. /*
  174. * Flush all to work around Errata in early athlons regarding
  175. * large page flushing.
  176. */
  177. __flush_tlb_all();
  178. if (boot_cpu_data.x86_model >= 4)
  179. wbinvd();
  180. }
  181. void global_flush_tlb(void)
  182. {
  183. BUG_ON(irqs_disabled());
  184. on_each_cpu(flush_kernel_map, NULL, 1, 1);
  185. }
  186. EXPORT_SYMBOL(global_flush_tlb);