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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* CRIS pgtable.h - macros and functions to manipulate page tables
  2.  *
  3.  * HISTORY:
  4.  *
  5.  * $Log: pgtable.h,v $
  6.  * Revision 1.15  2002/04/23 15:37:52  bjornw
  7.  * Removed page_address and added flush_icache_user_range
  8.  *
  9.  * Revision 1.14  2001/12/10 03:08:50  bjornw
  10.  * Added pgtable_cache_init dummy
  11.  *
  12.  * Revision 1.13  2001/11/12 18:05:38  pkj
  13.  * Added declaration of paging_init().
  14.  *
  15.  * Revision 1.12  2001/08/11 00:28:00  bjornw
  16.  * PAGE_CHG_MASK and PAGE_NONE had somewhat untraditional values
  17.  *
  18.  * Revision 1.11  2001/04/04 14:38:36  bjornw
  19.  * Removed bad_pagetable handling and the _kernel functions
  20.  *
  21.  * Revision 1.10  2001/03/23 07:46:42  starvik
  22.  * Corrected according to review remarks
  23.  *
  24.  * Revision 1.9  2000/11/22 14:57:53  bjornw
  25.  * * extern inline -> static inline
  26.  * * include asm-generic/pgtable.h
  27.  *
  28.  * Revision 1.8  2000/11/21 13:56:16  bjornw
  29.  * Use CONFIG_CRIS_LOW_MAP for the low VM map instead of explicit CPU type
  30.  *
  31.  * Revision 1.7  2000/10/06 15:05:32  bjornw
  32.  * VMALLOC area changed in memory mapping change
  33.  *
  34.  * Revision 1.6  2000/10/04 16:59:14  bjornw
  35.  * Changed comments
  36.  *
  37.  * Revision 1.5  2000/09/13 14:39:53  bjornw
  38.  * New macros
  39.  *
  40.  * Revision 1.4  2000/08/17 15:38:48  bjornw
  41.  * 2.4.0-test6 modifications:
  42.  *   * flush_dcache_page added
  43.  *   * MAP_NR removed
  44.  *   * virt_to_page added
  45.  *
  46.  * Plus some comments and type-clarifications.
  47.  *
  48.  * Revision 1.3  2000/08/15 16:33:35  bjornw
  49.  * pmd_bad should recognize both kernel and user page-tables
  50.  *
  51.  * Revision 1.2  2000/07/10 17:06:01  bjornw
  52.  * Fixed warnings
  53.  *
  54.  * Revision 1.1.1.1  2000/07/10 16:32:31  bjornw
  55.  * CRIS architecture, working draft
  56.  *
  57.  *
  58.  * Revision 1.11  2000/05/29 14:55:56  bjornw
  59.  * Small tweaks of pte_mk routines
  60.  *
  61.  * Revision 1.10  2000/01/27 01:49:06  bjornw
  62.  * * Ooops. The physical frame number in a PTE entry needs to point to the
  63.  *   DRAM directly, not to what the kernel thinks is DRAM (due to KSEG mapping).
  64.  *   Hence we need to strip bit 31 so 0xcXXXXXXX -> 0x4XXXXXXX.
  65.  *
  66.  * Revision 1.9  2000/01/26 16:25:50  bjornw
  67.  * Fixed PAGE_KERNEL bits
  68.  *
  69.  * Revision 1.8  2000/01/23 22:53:22  bjornw
  70.  * Correct flush_tlb_* macros and externs
  71.  *
  72.  * Revision 1.7  2000/01/18 16:22:55  bjornw
  73.  * Use PAGE_MASK instead of PFN_MASK.
  74.  *
  75.  * Revision 1.6  2000/01/17 02:42:53  bjornw
  76.  * Added the pmd_set macro.
  77.  *
  78.  * Revision 1.5  2000/01/16 19:53:42  bjornw
  79.  * Removed VMALLOC_OFFSET. Changed definitions of swapper_pg_dir and zero_page.
  80.  *
  81.  * Revision 1.4  2000/01/14 16:38:20  bjornw
  82.  * PAGE_DIRTY -> PAGE_SILENT_WRITE, removed PAGE_COW from PAGE_COPY.
  83.  *
  84.  * Revision 1.3  1999/12/04 20:12:21  bjornw
  85.  * * PTE bits have moved to asm/mmu.h
  86.  * * Fixed definitions of the higher level page protection bits
  87.  * * Added the pte_* functions, including dirty/accessed SW simulation
  88.  *   (these are exactly the same as for the MIPS port)
  89.  *
  90.  * Revision 1.2  1999/12/04 00:41:54  bjornw
  91.  * * Fixed page table offsets, sizes and shifts
  92.  * * Removed reference to i386 SMP stuff
  93.  * * Added stray comments about Linux/CRIS mm design
  94.  * * Include asm/mmu.h which will contain MMU details
  95.  *
  96.  * Revision 1.1  1999/12/03 15:04:02  bjornw
  97.  * Copied from include/asm-etrax100. For the new CRIS architecture.
  98.  */
  99. #ifndef _CRIS_PGTABLE_H
  100. #define _CRIS_PGTABLE_H
  101. #include <linux/config.h>
  102. #include <asm/mmu.h>
  103. /*
  104.  * The Linux memory management assumes a three-level page table setup. On
  105.  * CRIS, we use that, but "fold" the mid level into the top-level page
  106.  * table. Since the MMU TLB is software loaded through an interrupt, it
  107.  * supports any page table structure, so we could have used a three-level
  108.  * setup, but for the amounts of memory we normally use, a two-level is
  109.  * probably more efficient.
  110.  *
  111.  * This file contains the functions and defines necessary to modify and use
  112.  * the CRIS page table tree.
  113.  */
  114. extern void paging_init(void);
  115. /* The cache doesn't need to be flushed when TLB entries change because 
  116.  * the cache is mapped to physical memory, not virtual memory
  117.  */
  118. #define flush_cache_all() do { } while (0)
  119. #define flush_cache_mm(mm) do { } while (0)
  120. #define flush_cache_range(mm, start, end) do { } while (0)
  121. #define flush_cache_page(vma, vmaddr) do { } while (0)
  122. #define flush_page_to_ram(page) do { } while (0)
  123. #define flush_dcache_page(page)                 do { } while (0)
  124. #define flush_icache_range(start, end)          do { } while (0)
  125. #define flush_icache_page(vma,pg)               do { } while (0)
  126. #define flush_icache_user_range(vma,pg,adr,len) do { } while (0)
  127. /*
  128.  * TLB flushing (implemented in arch/cris/mm/tlb.c):
  129.  *
  130.  *  - flush_tlb() flushes the current mm struct TLBs
  131.  *  - flush_tlb_all() flushes all processes TLBs
  132.  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
  133.  *  - flush_tlb_page(vma, vmaddr) flushes one page
  134.  *  - flush_tlb_range(mm, start, end) flushes a range of pages
  135.  *
  136.  */
  137. extern void flush_tlb_all(void);
  138. extern void flush_tlb_mm(struct mm_struct *mm);
  139. extern void flush_tlb_page(struct vm_area_struct *vma, 
  140.    unsigned long addr);
  141. extern void flush_tlb_range(struct mm_struct *mm,
  142.     unsigned long start,
  143.     unsigned long end);
  144. static inline void flush_tlb_pgtables(struct mm_struct *mm,
  145.                                       unsigned long start, unsigned long end)
  146. {
  147.         /* CRIS does not keep any page table caches in TLB */
  148. }
  149. static inline void flush_tlb(void) 
  150. {
  151. flush_tlb_mm(current->mm);
  152. }
  153. /* Certain architectures need to do special things when pte's
  154.  * within a page table are directly modified.  Thus, the following
  155.  * hook is made available.
  156.  */
  157. #define set_pte(pteptr, pteval) ((*(pteptr)) = (pteval))
  158. /*
  159.  * (pmds are folded into pgds so this doesn't get actually called,
  160.  * but the define is needed for a generic inline function.)
  161.  */
  162. #define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
  163. #define set_pgd(pgdptr, pgdval) (*(pgdptr) = pgdval)
  164. /* PMD_SHIFT determines the size of the area a second-level page table can
  165.  * map. It is equal to the page size times the number of PTE's that fit in
  166.  * a PMD page. A PTE is 4-bytes in CRIS. Hence the following number.
  167.  */
  168. #define PMD_SHIFT (PAGE_SHIFT + (PAGE_SHIFT-2))
  169. #define PMD_SIZE (1UL << PMD_SHIFT)
  170. #define PMD_MASK (~(PMD_SIZE-1))
  171. /* PGDIR_SHIFT determines what a third-level page table entry can map.
  172.  * Since we fold into a two-level structure, this is the same as PMD_SHIFT.
  173.  */
  174. #define PGDIR_SHIFT PMD_SHIFT
  175. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  176. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  177. /*
  178.  * entries per page directory level: we use a two-level, so
  179.  * we don't really have any PMD directory physically.
  180.  * pointers are 4 bytes so we can use the page size and 
  181.  * divide it by 4 (shift by 2).
  182.  */
  183. #define PTRS_PER_PTE (1UL << (PAGE_SHIFT-2))
  184. #define PTRS_PER_PMD 1
  185. #define PTRS_PER_PGD (1UL << (PAGE_SHIFT-2))
  186. /* calculate how many PGD entries a user-level program can use
  187.  * the first mappable virtual address is 0
  188.  * (TASK_SIZE is the maximum virtual address space)
  189.  */
  190. #define USER_PTRS_PER_PGD       (TASK_SIZE/PGDIR_SIZE)
  191. #define FIRST_USER_PGD_NR       0
  192. /*
  193.  * Kernels own virtual memory area. 
  194.  */
  195. #ifdef CONFIG_CRIS_LOW_MAP
  196. #define VMALLOC_START     KSEG_7
  197. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  198. #define VMALLOC_END       KSEG_8
  199. #else
  200. #define VMALLOC_START     KSEG_D
  201. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  202. #define VMALLOC_END       KSEG_E
  203. #endif
  204. /* Define some higher level generic page attributes. The PTE bits are
  205.  * defined in asm-cris/mmu.h, and these are just combinations of those.
  206.  */
  207. #define __READABLE      (_PAGE_READ | _PAGE_SILENT_READ | _PAGE_ACCESSED)
  208. #define __WRITEABLE     (_PAGE_WRITE | _PAGE_SILENT_WRITE | _PAGE_MODIFIED)
  209. #define _PAGE_TABLE     (_PAGE_PRESENT | __READABLE | __WRITEABLE)
  210. #define _PAGE_CHG_MASK  (PAGE_MASK | _PAGE_ACCESSED | _PAGE_MODIFIED)
  211. #define PAGE_NONE       __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED)
  212. #define PAGE_SHARED     __pgprot(_PAGE_PRESENT | __READABLE | _PAGE_WRITE | 
  213.  _PAGE_ACCESSED)
  214. #define PAGE_COPY       __pgprot(_PAGE_PRESENT | __READABLE)  // | _PAGE_COW
  215. #define PAGE_READONLY   __pgprot(_PAGE_PRESENT | __READABLE)
  216. #define PAGE_KERNEL     __pgprot(_PAGE_GLOBAL | _PAGE_KERNEL | 
  217.  _PAGE_PRESENT | __READABLE | __WRITEABLE)
  218. #define _KERNPG_TABLE   (_PAGE_TABLE | _PAGE_KERNEL)
  219. /*
  220.  * CRIS can't do page protection for execute, and considers read the same.
  221.  * Also, write permissions imply read permissions. This is the closest we can
  222.  * get..
  223.  */
  224. #define __P000 PAGE_NONE
  225. #define __P001 PAGE_READONLY
  226. #define __P010 PAGE_COPY
  227. #define __P011 PAGE_COPY
  228. #define __P100 PAGE_READONLY
  229. #define __P101 PAGE_READONLY
  230. #define __P110 PAGE_COPY
  231. #define __P111 PAGE_COPY
  232. #define __S000 PAGE_NONE
  233. #define __S001 PAGE_READONLY
  234. #define __S010 PAGE_SHARED
  235. #define __S011 PAGE_SHARED
  236. #define __S100 PAGE_READONLY
  237. #define __S101 PAGE_READONLY
  238. #define __S110 PAGE_SHARED
  239. #define __S111 PAGE_SHARED
  240. /* zero page used for uninitialized stuff */
  241. extern unsigned long empty_zero_page;
  242. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  243. /* number of bits that fit into a memory pointer */
  244. #define BITS_PER_PTR (8*sizeof(unsigned long))
  245. /* to align the pointer to a pointer address */
  246. #define PTR_MASK (~(sizeof(void*)-1))
  247. /* sizeof(void*)==1<<SIZEOF_PTR_LOG2 */
  248. /* 64-bit machines, beware!  SRB. */
  249. #define SIZEOF_PTR_LOG2 2
  250. /* to find an entry in a page-table */
  251. #define PAGE_PTR(address) 
  252. ((unsigned long)(address)>>(PAGE_SHIFT-SIZEOF_PTR_LOG2)&PTR_MASK&~PAGE_MASK)
  253. /* to set the page-dir */
  254. #define SET_PAGE_DIR(tsk,pgdir)
  255. #define pte_none(x) (!pte_val(x))
  256. #define pte_present(x) (pte_val(x) & _PAGE_PRESENT)
  257. #define pte_clear(xp) do { pte_val(*(xp)) = 0; } while (0)
  258. #define pmd_none(x) (!pmd_val(x))
  259. /* by removing the _PAGE_KERNEL bit from the comparision, the same pmd_bad
  260.  * works for both _PAGE_TABLE and _KERNPG_TABLE pmd entries.
  261.  */
  262. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_KERNEL)) != _PAGE_TABLE)
  263. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  264. #define pmd_clear(xp) do { pmd_val(*(xp)) = 0; } while (0)
  265. /*
  266.  * The "pgd_xxx()" functions here are trivial for a folded two-level
  267.  * setup: the pgd is never bad, and a pmd always exists (as it's folded
  268.  * into the pgd entry)
  269.  */
  270. static inline int pgd_none(pgd_t pgd) { return 0; }
  271. static inline int pgd_bad(pgd_t pgd) { return 0; }
  272. static inline int pgd_present(pgd_t pgd) { return 1; }
  273. static inline void pgd_clear(pgd_t * pgdp) { }
  274. /*
  275.  * The following only work if pte_present() is true.
  276.  * Undefined behaviour if not..
  277.  */
  278. static inline int pte_read(pte_t pte)           { return pte_val(pte) & _PAGE_READ; }
  279. static inline int pte_write(pte_t pte)          { return pte_val(pte) & _PAGE_WRITE; }
  280. static inline int pte_exec(pte_t pte)           { return pte_val(pte) & _PAGE_READ; }
  281. static inline int pte_dirty(pte_t pte)          { return pte_val(pte) & _PAGE_MODIFIED; }
  282. static inline int pte_young(pte_t pte)          { return pte_val(pte) & _PAGE_ACCESSED; }
  283. static inline pte_t pte_wrprotect(pte_t pte)
  284. {
  285.         pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
  286.         return pte;
  287. }
  288. static inline pte_t pte_rdprotect(pte_t pte)
  289. {
  290.         pte_val(pte) &= ~(_PAGE_READ | _PAGE_SILENT_READ);
  291. return pte;
  292. }
  293. static inline pte_t pte_exprotect(pte_t pte)
  294. {
  295.         pte_val(pte) &= ~(_PAGE_READ | _PAGE_SILENT_READ);
  296. return pte;
  297. }
  298. static inline pte_t pte_mkclean(pte_t pte)
  299. {
  300. pte_val(pte) &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE); 
  301. return pte; 
  302. }
  303. static inline pte_t pte_mkold(pte_t pte)
  304. {
  305. pte_val(pte) &= ~(_PAGE_ACCESSED | _PAGE_SILENT_READ);
  306. return pte;
  307. }
  308. static inline pte_t pte_mkwrite(pte_t pte)
  309. {
  310.         pte_val(pte) |= _PAGE_WRITE;
  311.         if (pte_val(pte) & _PAGE_MODIFIED)
  312.                 pte_val(pte) |= _PAGE_SILENT_WRITE;
  313.         return pte;
  314. }
  315. static inline pte_t pte_mkread(pte_t pte)
  316. {
  317.         pte_val(pte) |= _PAGE_READ;
  318.         if (pte_val(pte) & _PAGE_ACCESSED)
  319.                 pte_val(pte) |= _PAGE_SILENT_READ;
  320.         return pte;
  321. }
  322. static inline pte_t pte_mkexec(pte_t pte)
  323. {
  324.         pte_val(pte) |= _PAGE_READ;
  325.         if (pte_val(pte) & _PAGE_ACCESSED)
  326.                 pte_val(pte) |= _PAGE_SILENT_READ;
  327.         return pte;
  328. }
  329. static inline pte_t pte_mkdirty(pte_t pte)
  330. {
  331.         pte_val(pte) |= _PAGE_MODIFIED;
  332.         if (pte_val(pte) & _PAGE_WRITE)
  333.                 pte_val(pte) |= _PAGE_SILENT_WRITE;
  334.         return pte;
  335. }
  336. static inline pte_t pte_mkyoung(pte_t pte)
  337. {
  338.         pte_val(pte) |= _PAGE_ACCESSED;
  339.         if (pte_val(pte) & _PAGE_READ)
  340.         {
  341.                 pte_val(pte) |= _PAGE_SILENT_READ;
  342.                 if ((pte_val(pte) & (_PAGE_WRITE | _PAGE_MODIFIED)) ==
  343.     (_PAGE_WRITE | _PAGE_MODIFIED))
  344.                         pte_val(pte) |= _PAGE_SILENT_WRITE;
  345.         }
  346.         return pte;
  347. }
  348. /*
  349.  * Conversion functions: convert a page and protection to a page entry,
  350.  * and a page entry and page directory to the page they refer to.
  351.  */
  352. /* What actually goes as arguments to the various functions is less than
  353.  * obvious, but a rule of thumb is that struct page's goes as struct page *,
  354.  * really physical DRAM addresses are unsigned long's, and DRAM "virtual"
  355.  * addresses (the 0xc0xxxxxx's) goes as void *'s.
  356.  */
  357. static inline pte_t __mk_pte(void * page, pgprot_t pgprot)
  358. {
  359. pte_t pte;
  360. /* the PTE needs a physical address */
  361. pte_val(pte) = __pa(page) | pgprot_val(pgprot);
  362. return pte;
  363. }
  364. #define mk_pte(page, pgprot) __mk_pte(page_address(page), (pgprot))
  365. #define mk_pte_phys(physpage, pgprot) 
  366. ({                                                                      
  367.         pte_t __pte;                                                    
  368.                                                                         
  369.         pte_val(__pte) = (physpage) + pgprot_val(pgprot);               
  370.         __pte;                                                          
  371. })
  372. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  373. { pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot); return pte; }
  374. /* pte_val refers to a page in the 0x4xxxxxxx physical DRAM interval
  375.  * __pte_page(pte_val) refers to the "virtual" DRAM interval
  376.  * pte_pagenr refers to the page-number counted starting from the virtual DRAM start
  377.  */
  378. static inline unsigned long __pte_page(pte_t pte)
  379. {
  380. /* the PTE contains a physical address */
  381. return (unsigned long)__va(pte_val(pte) & PAGE_MASK);
  382. }
  383. #define pte_pagenr(pte)         ((__pte_page(pte) - PAGE_OFFSET) >> PAGE_SHIFT)
  384. /* permanent address of a page */
  385. #define __page_address(page)    (PAGE_OFFSET + (((page) - mem_map) << PAGE_SHIFT))
  386. #define pte_page(pte)           (mem_map+pte_pagenr(pte))
  387. /* only the pte's themselves need to point to physical DRAM (see above)
  388.  * the pagetable links are purely handled within the kernel SW and thus
  389.  * don't need the __pa and __va transformations.
  390.  */
  391. static inline unsigned long pmd_page(pmd_t pmd)
  392. { return pmd_val(pmd) & PAGE_MASK; }
  393. static inline void pmd_set(pmd_t * pmdp, pte_t * ptep)
  394. { pmd_val(*pmdp) = _PAGE_TABLE | (unsigned long) ptep; }
  395. /* to find an entry in a page-table-directory. */
  396. #define pgd_index(address) ((address >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  397. /* to find an entry in a page-table-directory */
  398. static inline pgd_t * pgd_offset(struct mm_struct * mm, unsigned long address)
  399. {
  400. return mm->pgd + pgd_index(address);
  401. }
  402. /* to find an entry in a kernel page-table-directory */
  403. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  404. /* Find an entry in the second-level page table.. */
  405. static inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
  406. {
  407. return (pmd_t *) dir;
  408. }
  409. /* Find an entry in the third-level page table.. */ 
  410. static inline pte_t * pte_offset(pmd_t * dir, unsigned long address)
  411. {
  412. return (pte_t *) pmd_page(*dir) + ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  413. }
  414. #define pte_ERROR(e) 
  415.         printk("%s:%d: bad pte %p(%08lx).n", __FILE__, __LINE__, &(e), pte_val(e))
  416. #define pmd_ERROR(e) 
  417.         printk("%s:%d: bad pmd %p(%08lx).n", __FILE__, __LINE__, &(e), pmd_val(e))
  418. #define pgd_ERROR(e) 
  419.         printk("%s:%d: bad pgd %p(%08lx).n", __FILE__, __LINE__, &(e), pgd_val(e))
  420. extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* defined in head.S */
  421. /*
  422.  * CRIS doesn't have any external MMU info: the kernel page
  423.  * tables contain all the necessary information.
  424.  * 
  425.  * Actually I am not sure on what this could be used for.
  426.  */
  427. static inline void update_mmu_cache(struct vm_area_struct * vma,
  428. unsigned long address, pte_t pte)
  429. {
  430. }
  431. /* Encode and de-code a swap entry (must be !pte_none(e) && !pte_present(e)) */
  432. /* Since the PAGE_PRESENT bit is bit 4, we can use the bits above */
  433. #define SWP_TYPE(x)                     (((x).val >> 5) & 0x7f)
  434. #define SWP_OFFSET(x)                   ((x).val >> 12)
  435. #define SWP_ENTRY(type, offset)         ((swp_entry_t) { ((type) << 5) | ((offset) << 12) })
  436. #define pte_to_swp_entry(pte)           ((swp_entry_t) { pte_val(pte) })
  437. #define swp_entry_to_pte(x)             ((pte_t) { (x).val })
  438. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  439. #define PageSkip(page)          (0)
  440. #define kern_addr_valid(addr)   (1)
  441. #include <asm-generic/pgtable.h>
  442. /*
  443.  * No page table caches to initialise
  444.  */
  445. #define pgtable_cache_init()   do { } while (0)
  446. #endif /* _CRIS_PGTABLE_H */