pageattr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <asm/uaccess.h>
  11. #include <asm/processor.h>
  12. /* Should move most of this stuff into the appropiate includes */
  13. #define LARGE_PAGE_MASK (~(LARGE_PAGE_SIZE-1))
  14. #define LARGE_PAGE_SIZE (1UL << PMD_SHIFT)
  15. static inline pte_t *lookup_address(unsigned long address) 
  16. pmd_t *pmd;
  17. pgd_t *pgd = pgd_offset(&init_mm, address); 
  18. if (pgd_none(*pgd))
  19. return NULL; 
  20. if (pgd_val(*pgd) & _PAGE_PSE)
  21. return (pte_t *)pgd; 
  22. pmd = pmd_offset(pgd, address);         
  23. if (pmd_none(*pmd))
  24. return NULL; 
  25. if (pmd_val(*pmd) & _PAGE_PSE) 
  26. return (pte_t *)pmd; 
  27.     return pte_offset(pmd, address);
  28. static struct page *split_large_page(unsigned long address, pgprot_t prot)
  29. int i; 
  30. unsigned long addr;
  31. struct page *base = alloc_pages(GFP_KERNEL, 0);
  32. pte_t *pbase;
  33. if (!base) 
  34. return NULL;
  35. address = __pa(address);
  36. addr = address & LARGE_PAGE_MASK; 
  37. pbase = (pte_t *)page_address(base);
  38. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
  39. pbase[i] = mk_pte_phys(addr, 
  40.       addr == address ? prot : PAGE_KERNEL);
  41. }
  42. return base;
  43. static void flush_kernel_map(void * address) 
  44. if (!test_bit(X86_FEATURE_SELFSNOOP, boot_cpu_data.x86_capability)) {
  45. /* Could use CLFLUSH here if the CPU supports it (Hammer,P4) */
  46. if (boot_cpu_data.x86_model >= 4) 
  47. asm volatile("wbinvd":::"memory"); 
  48. /* Do global flush here to work around large page flushing errata 
  49.    in some early Athlons */
  50. __flush_tlb_all(); 
  51. }
  52. static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) 
  53. set_pte_atomic(kpte, pte);  /* change init_mm */
  54. #ifndef CONFIG_X86_PAE
  55. {
  56. struct list_head *l;
  57. spin_lock(&mmlist_lock);
  58. list_for_each(l, &init_mm.mmlist) { 
  59. struct mm_struct *mm = list_entry(l, struct mm_struct, mmlist);
  60. pmd_t *pmd = pmd_offset(pgd_offset(mm, address), address);
  61. set_pte_atomic((pte_t *)pmd, pte);
  62. spin_unlock(&mmlist_lock);
  63. }
  64. #endif
  65. }
  66. /* no more special protections in this 2/4MB area - revert to a
  67.    large page again. */
  68. static inline void revert_page(struct page *kpte_page, unsigned long address)
  69. {
  70. pte_t *linear = (pte_t *) 
  71. pmd_offset(pgd_offset(&init_mm, address), address);
  72. set_pmd_pte(linear,  address,
  73. mk_pte_phys(__pa(address & LARGE_PAGE_MASK),
  74.     MAKE_GLOBAL(_KERNPG_TABLE|_PAGE_PSE)));
  75. }
  76.  
  77. /*
  78.  * Change the page attributes of an page in the linear mapping.
  79.  *
  80.  * This should be used when a page is mapped with a different caching policy
  81.  * than write-back somewhere - some CPUs do not like it when mappings with
  82.  * different caching policies exist. This changes the page attributes of the
  83.  * in kernel linear mapping too.
  84.  * 
  85.  * The caller needs to ensure that there are no conflicting mappings elsewhere.
  86.  * This function only deals with the kernel linear map.
  87.  * When page is in highmem it must never be kmap'ed.
  88.  */
  89. static int 
  90. __change_page_attr(struct page *page, pgprot_t prot, struct page **oldpage) 
  91. pte_t *kpte; 
  92. unsigned long address;
  93. struct page *kpte_page;
  94. #ifdef CONFIG_HIGHMEM
  95. if (page >= highmem_start_page) 
  96. BUG(); 
  97. #endif
  98. address = (unsigned long)page_address(page);
  99. kpte = lookup_address(address);
  100. if (!kpte) 
  101. return -EINVAL; 
  102. kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
  103. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) { 
  104. if ((pte_val(*kpte) & _PAGE_PSE) == 0) {
  105. pte_t old = *kpte;
  106. pte_t standard = mk_pte(page, PAGE_KERNEL); 
  107. set_pte_atomic(kpte, mk_pte(page, prot)); 
  108. if (pte_same(old,standard))
  109. atomic_inc(&kpte_page->count);
  110. } else {
  111. struct page *split = split_large_page(address, prot); 
  112. if (!split)
  113. return -ENOMEM;
  114. atomic_inc(&kpte_page->count); 
  115. set_pmd_pte(kpte,address,mk_pte(split, PAGE_KERNEL));
  116. }
  117. } else if ((pte_val(*kpte) & _PAGE_PSE) == 0) { 
  118. set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
  119. atomic_dec(&kpte_page->count); 
  120. }
  121. if (cpu_has_pse && (atomic_read(&kpte_page->count) == 1)) { 
  122. *oldpage = kpte_page;
  123. revert_page(kpte_page, address);
  124. return 0;
  125. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  126. {
  127. int err = 0; 
  128. struct page *fpage; 
  129. int i; 
  130. down_write(&init_mm.mmap_sem);
  131. for (i = 0; i < numpages; i++, page++) { 
  132. fpage = NULL;
  133. err = __change_page_attr(page, prot, &fpage); 
  134. if (err) 
  135. break; 
  136. if (fpage || i == numpages-1) { 
  137. void *address = page_address(page);
  138. #ifdef CONFIG_SMP 
  139. smp_call_function(flush_kernel_map, address, 1, 1);
  140. #endif
  141. flush_kernel_map(address);
  142. if (fpage)
  143. __free_page(fpage);
  144. up_write(&init_mm.mmap_sem); 
  145. return err;
  146. }
  147. EXPORT_SYMBOL(change_page_attr);