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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _I386_PGTABLE_H
  2. #define _I386_PGTABLE_H
  3. #include <linux/config.h>
  4. /*
  5.  * The Linux memory management assumes a three-level page table setup. On
  6.  * the i386, we use that, but "fold" the mid level into the top-level page
  7.  * table, so that we physically have the same two-level page table as the
  8.  * i386 mmu expects.
  9.  *
  10.  * This file contains the functions and defines necessary to modify and use
  11.  * the i386 page table tree.
  12.  */
  13. #ifndef __ASSEMBLY__
  14. #include <asm/processor.h>
  15. #include <asm/fixmap.h>
  16. #include <linux/threads.h>
  17. #ifndef _I386_BITOPS_H
  18. #include <asm/bitops.h>
  19. #endif
  20. extern pgd_t swapper_pg_dir[1024];
  21. extern void paging_init(void);
  22. /* Caches aren't brain-dead on the intel. */
  23. #define flush_cache_all() do { } while (0)
  24. #define flush_cache_mm(mm) do { } while (0)
  25. #define flush_cache_range(mm, start, end) do { } while (0)
  26. #define flush_cache_page(vma, vmaddr) do { } while (0)
  27. #define flush_page_to_ram(page) do { } while (0)
  28. #define flush_dcache_page(page) do { } while (0)
  29. #define flush_icache_range(start, end) do { } while (0)
  30. #define flush_icache_page(vma,pg) do { } while (0)
  31. #define flush_icache_user_range(vma,pg,adr,len) do { } while (0)
  32. #define __flush_tlb()
  33. do {
  34. unsigned int tmpreg;
  35. __asm__ __volatile__(
  36. "movl %%cr3, %0;  # flush TLB n"
  37. "movl %0, %%cr3;              n"
  38. : "=r" (tmpreg)
  39. :: "memory");
  40. } while (0)
  41. /*
  42.  * Global pages have to be flushed a bit differently. Not a real
  43.  * performance problem because this does not happen often.
  44.  */
  45. #define __flush_tlb_global()
  46. do {
  47. unsigned int tmpreg;
  48. __asm__ __volatile__(
  49. "movl %1, %%cr4;  # turn off PGE     n"
  50. "movl %%cr3, %0;  # flush TLB        n"
  51. "movl %0, %%cr3;                     n"
  52. "movl %2, %%cr4;  # turn PGE back on n"
  53. : "=&r" (tmpreg)
  54. : "r" (mmu_cr4_features & ~X86_CR4_PGE),
  55.   "r" (mmu_cr4_features)
  56. : "memory");
  57. } while (0)
  58. extern unsigned long pgkern_mask;
  59. /*
  60.  * Do not check the PGE bit unnecesserily if this is a PPro+ kernel.
  61.  */
  62. #ifdef CONFIG_X86_PGE
  63. # define __flush_tlb_all() __flush_tlb_global()
  64. #else
  65. # define __flush_tlb_all()
  66. do {
  67. if (cpu_has_pge)
  68. __flush_tlb_global();
  69. else
  70. __flush_tlb();
  71. } while (0)
  72. #endif
  73. #define __flush_tlb_single(addr) 
  74. __asm__ __volatile__("invlpg %0": :"m" (*(char *) addr))
  75. #ifdef CONFIG_X86_INVLPG
  76. # define __flush_tlb_one(addr) __flush_tlb_single(addr)
  77. #else
  78. # define __flush_tlb_one(addr)
  79. do {
  80. if (cpu_has_pge)
  81. __flush_tlb_single(addr);
  82. else
  83. __flush_tlb();
  84. } while (0)
  85. #endif
  86. /*
  87.  * ZERO_PAGE is a global shared page that is always zero: used
  88.  * for zero-mapped memory areas etc..
  89.  */
  90. extern unsigned long empty_zero_page[1024];
  91. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  92. #endif /* !__ASSEMBLY__ */
  93. /*
  94.  * The Linux x86 paging architecture is 'compile-time dual-mode', it
  95.  * implements both the traditional 2-level x86 page tables and the
  96.  * newer 3-level PAE-mode page tables.
  97.  */
  98. #ifndef __ASSEMBLY__
  99. #if CONFIG_X86_PAE
  100. # include <asm/pgtable-3level.h>
  101. /*
  102.  * Need to initialise the X86 PAE caches
  103.  */
  104. extern void pgtable_cache_init(void);
  105. #else
  106. # include <asm/pgtable-2level.h>
  107. /*
  108.  * No page table caches to initialise
  109.  */
  110. #define pgtable_cache_init() do { } while (0)
  111. #endif
  112. #endif
  113. #define __beep() asm("movb $0x3,%al; outb %al,$0x61")
  114. #define PMD_SIZE (1UL << PMD_SHIFT)
  115. #define PMD_MASK (~(PMD_SIZE-1))
  116. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  117. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  118. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  119. #define FIRST_USER_PGD_NR 0
  120. #define USER_PGD_PTRS (PAGE_OFFSET >> PGDIR_SHIFT)
  121. #define KERNEL_PGD_PTRS (PTRS_PER_PGD-USER_PGD_PTRS)
  122. #define TWOLEVEL_PGDIR_SHIFT 22
  123. #define BOOT_USER_PGD_PTRS (__PAGE_OFFSET >> TWOLEVEL_PGDIR_SHIFT)
  124. #define BOOT_KERNEL_PGD_PTRS (1024-BOOT_USER_PGD_PTRS)
  125. #ifndef __ASSEMBLY__
  126. /* Just any arbitrary offset to the start of the vmalloc VM area: the
  127.  * current 8MB value just means that there will be a 8MB "hole" after the
  128.  * physical memory until the kernel virtual memory starts.  That means that
  129.  * any out-of-bounds memory accesses will hopefully be caught.
  130.  * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  131.  * area for the same reason. ;)
  132.  */
  133. #define VMALLOC_OFFSET (8*1024*1024)
  134. #define VMALLOC_START (((unsigned long) high_memory + 2*VMALLOC_OFFSET-1) & 
  135. ~(VMALLOC_OFFSET-1))
  136. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  137. #if CONFIG_HIGHMEM
  138. # define VMALLOC_END (PKMAP_BASE-2*PAGE_SIZE)
  139. #else
  140. # define VMALLOC_END (FIXADDR_START-2*PAGE_SIZE)
  141. #endif
  142. /*
  143.  * The 4MB page is guessing..  Detailed in the infamous "Chapter H"
  144.  * of the Pentium details, but assuming intel did the straightforward
  145.  * thing, this bit set in the page directory entry just means that
  146.  * the page directory entry points directly to a 4MB-aligned block of
  147.  * memory. 
  148.  */
  149. #define _PAGE_BIT_PRESENT 0
  150. #define _PAGE_BIT_RW 1
  151. #define _PAGE_BIT_USER 2
  152. #define _PAGE_BIT_PWT 3
  153. #define _PAGE_BIT_PCD 4
  154. #define _PAGE_BIT_ACCESSED 5
  155. #define _PAGE_BIT_DIRTY 6
  156. #define _PAGE_BIT_PSE 7 /* 4 MB (or 2MB) page, Pentium+, if present.. */
  157. #define _PAGE_BIT_GLOBAL 8 /* Global TLB entry PPro+ */
  158. #define _PAGE_PRESENT 0x001
  159. #define _PAGE_RW 0x002
  160. #define _PAGE_USER 0x004
  161. #define _PAGE_PWT 0x008
  162. #define _PAGE_PCD 0x010
  163. #define _PAGE_ACCESSED 0x020
  164. #define _PAGE_DIRTY 0x040
  165. #define _PAGE_PSE 0x080 /* 4 MB (or 2MB) page, Pentium+, if present.. */
  166. #define _PAGE_GLOBAL 0x100 /* Global TLB entry PPro+ */
  167. #define _PAGE_PROTNONE 0x080 /* If not present */
  168. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
  169. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  170. #define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  171. #define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  172. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  173. #define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  174. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  175. #define __PAGE_KERNEL 
  176. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  177. #define __PAGE_KERNEL_NOCACHE 
  178. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_PCD | _PAGE_ACCESSED)
  179. #define __PAGE_KERNEL_RO 
  180. (_PAGE_PRESENT | _PAGE_DIRTY | _PAGE_ACCESSED)
  181. #ifdef CONFIG_X86_PGE
  182. # define MAKE_GLOBAL(x) __pgprot((x) | _PAGE_GLOBAL)
  183. #else
  184. # define MAKE_GLOBAL(x)
  185. ({
  186. pgprot_t __ret;
  187. if (cpu_has_pge)
  188. __ret = __pgprot((x) | _PAGE_GLOBAL);
  189. else
  190. __ret = __pgprot(x);
  191. __ret;
  192. })
  193. #endif
  194. #define PAGE_KERNEL MAKE_GLOBAL(__PAGE_KERNEL)
  195. #define PAGE_KERNEL_RO MAKE_GLOBAL(__PAGE_KERNEL_RO)
  196. #define PAGE_KERNEL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_NOCACHE)
  197. /*
  198.  * The i386 can't do page protection for execute, and considers that
  199.  * the same are read. Also, write permissions imply read permissions.
  200.  * This is the closest we can get..
  201.  */
  202. #define __P000 PAGE_NONE
  203. #define __P001 PAGE_READONLY
  204. #define __P010 PAGE_COPY
  205. #define __P011 PAGE_COPY
  206. #define __P100 PAGE_READONLY
  207. #define __P101 PAGE_READONLY
  208. #define __P110 PAGE_COPY
  209. #define __P111 PAGE_COPY
  210. #define __S000 PAGE_NONE
  211. #define __S001 PAGE_READONLY
  212. #define __S010 PAGE_SHARED
  213. #define __S011 PAGE_SHARED
  214. #define __S100 PAGE_READONLY
  215. #define __S101 PAGE_READONLY
  216. #define __S110 PAGE_SHARED
  217. #define __S111 PAGE_SHARED
  218. /*
  219.  * Define this if things work differently on an i386 and an i486:
  220.  * it will (on an i486) warn about kernel memory accesses that are
  221.  * done without a 'verify_area(VERIFY_WRITE,..)'
  222.  */
  223. #undef TEST_VERIFY_AREA
  224. /* page table for 0-4MB for everybody */
  225. extern unsigned long pg0[1024];
  226. #define pte_present(x) ((x).pte_low & (_PAGE_PRESENT | _PAGE_PROTNONE))
  227. #define pte_clear(xp) do { set_pte(xp, __pte(0)); } while (0)
  228. #define pmd_none(x) (!pmd_val(x))
  229. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  230. #define pmd_clear(xp) do { set_pmd(xp, __pmd(0)); } while (0)
  231. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
  232. #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))
  233. /*
  234.  * The following only work if pte_present() is true.
  235.  * Undefined behaviour if not..
  236.  */
  237. static inline int pte_read(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
  238. static inline int pte_exec(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
  239. static inline int pte_dirty(pte_t pte) { return (pte).pte_low & _PAGE_DIRTY; }
  240. static inline int pte_young(pte_t pte) { return (pte).pte_low & _PAGE_ACCESSED; }
  241. static inline int pte_write(pte_t pte) { return (pte).pte_low & _PAGE_RW; }
  242. static inline pte_t pte_rdprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_USER; return pte; }
  243. static inline pte_t pte_exprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_USER; return pte; }
  244. static inline pte_t pte_mkclean(pte_t pte) { (pte).pte_low &= ~_PAGE_DIRTY; return pte; }
  245. static inline pte_t pte_mkold(pte_t pte) { (pte).pte_low &= ~_PAGE_ACCESSED; return pte; }
  246. static inline pte_t pte_wrprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_RW; return pte; }
  247. static inline pte_t pte_mkread(pte_t pte) { (pte).pte_low |= _PAGE_USER; return pte; }
  248. static inline pte_t pte_mkexec(pte_t pte) { (pte).pte_low |= _PAGE_USER; return pte; }
  249. static inline pte_t pte_mkdirty(pte_t pte) { (pte).pte_low |= _PAGE_DIRTY; return pte; }
  250. static inline pte_t pte_mkyoung(pte_t pte) { (pte).pte_low |= _PAGE_ACCESSED; return pte; }
  251. static inline pte_t pte_mkwrite(pte_t pte) { (pte).pte_low |= _PAGE_RW; return pte; }
  252. static inline  int ptep_test_and_clear_dirty(pte_t *ptep) { return test_and_clear_bit(_PAGE_BIT_DIRTY, ptep); }
  253. static inline  int ptep_test_and_clear_young(pte_t *ptep) { return test_and_clear_bit(_PAGE_BIT_ACCESSED, ptep); }
  254. static inline void ptep_set_wrprotect(pte_t *ptep) { clear_bit(_PAGE_BIT_RW, ptep); }
  255. static inline void ptep_mkdirty(pte_t *ptep) { set_bit(_PAGE_BIT_DIRTY, ptep); }
  256. /*
  257.  * Conversion functions: convert a page and protection to a page entry,
  258.  * and a page entry and page directory to the page they refer to.
  259.  */
  260. #define mk_pte(page, pgprot) __mk_pte((page) - mem_map, (pgprot))
  261. /* This takes a physical page address that is used by the remapping functions */
  262. #define mk_pte_phys(physpage, pgprot) __mk_pte((physpage) >> PAGE_SHIFT, pgprot)
  263. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  264. {
  265. pte.pte_low &= _PAGE_CHG_MASK;
  266. pte.pte_low |= pgprot_val(newprot);
  267. return pte;
  268. }
  269. #define page_pte(page) page_pte_prot(page, __pgprot(0))
  270. #define pmd_page(pmd) 
  271. ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  272. /* to find an entry in a page-table-directory. */
  273. #define pgd_index(address) ((address >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  274. #define __pgd_offset(address) pgd_index(address)
  275. #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
  276. /* to find an entry in a kernel page-table-directory */
  277. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  278. #define __pmd_offset(address) 
  279. (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  280. /* Find an entry in the third-level page table.. */
  281. #define __pte_offset(address) 
  282. ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  283. #define pte_offset(dir, address) ((pte_t *) pmd_page(*(dir)) + 
  284. __pte_offset(address))
  285. /*
  286.  * The i386 doesn't have any external MMU info: the kernel page
  287.  * tables contain all the necessary information.
  288.  */
  289. #define update_mmu_cache(vma,address,pte) do { } while (0)
  290. /* Encode and de-code a swap entry */
  291. #define SWP_TYPE(x) (((x).val >> 1) & 0x3f)
  292. #define SWP_OFFSET(x) ((x).val >> 8)
  293. #define SWP_ENTRY(type, offset) ((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
  294. #define pte_to_swp_entry(pte) ((swp_entry_t) { (pte).pte_low })
  295. #define swp_entry_to_pte(x) ((pte_t) { (x).val })
  296. struct page;
  297. int change_page_attr(struct page *, int, pgprot_t prot);
  298. #endif /* !__ASSEMBLY__ */
  299. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  300. #define PageSkip(page) (0)
  301. #define kern_addr_valid(addr) (1)
  302. #define io_remap_page_range remap_page_range
  303. #endif /* _I386_PGTABLE_H */