Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  *  arch/arm/include/asm/pgtable-2level.h
0004  *
0005  *  Copyright (C) 1995-2002 Russell King
0006  */
0007 #ifndef _ASM_PGTABLE_2LEVEL_H
0008 #define _ASM_PGTABLE_2LEVEL_H
0009 
0010 #define __PAGETABLE_PMD_FOLDED 1
0011 
0012 /*
0013  * Hardware-wise, we have a two level page table structure, where the first
0014  * level has 4096 entries, and the second level has 256 entries.  Each entry
0015  * is one 32-bit word.  Most of the bits in the second level entry are used
0016  * by hardware, and there aren't any "accessed" and "dirty" bits.
0017  *
0018  * Linux on the other hand has a three level page table structure, which can
0019  * be wrapped to fit a two level page table structure easily - using the PGD
0020  * and PTE only.  However, Linux also expects one "PTE" table per page, and
0021  * at least a "dirty" bit.
0022  *
0023  * Therefore, we tweak the implementation slightly - we tell Linux that we
0024  * have 2048 entries in the first level, each of which is 8 bytes (iow, two
0025  * hardware pointers to the second level.)  The second level contains two
0026  * hardware PTE tables arranged contiguously, preceded by Linux versions
0027  * which contain the state information Linux needs.  We, therefore, end up
0028  * with 512 entries in the "PTE" level.
0029  *
0030  * This leads to the page tables having the following layout:
0031  *
0032  *    pgd             pte
0033  * |        |
0034  * +--------+
0035  * |        |       +------------+ +0
0036  * +- - - - +       | Linux pt 0 |
0037  * |        |       +------------+ +1024
0038  * +--------+ +0    | Linux pt 1 |
0039  * |        |-----> +------------+ +2048
0040  * +- - - - + +4    |  h/w pt 0  |
0041  * |        |-----> +------------+ +3072
0042  * +--------+ +8    |  h/w pt 1  |
0043  * |        |       +------------+ +4096
0044  *
0045  * See L_PTE_xxx below for definitions of bits in the "Linux pt", and
0046  * PTE_xxx for definitions of bits appearing in the "h/w pt".
0047  *
0048  * PMD_xxx definitions refer to bits in the first level page table.
0049  *
0050  * The "dirty" bit is emulated by only granting hardware write permission
0051  * iff the page is marked "writable" and "dirty" in the Linux PTE.  This
0052  * means that a write to a clean page will cause a permission fault, and
0053  * the Linux MM layer will mark the page dirty via handle_pte_fault().
0054  * For the hardware to notice the permission change, the TLB entry must
0055  * be flushed, and ptep_set_access_flags() does that for us.
0056  *
0057  * The "accessed" or "young" bit is emulated by a similar method; we only
0058  * allow accesses to the page if the "young" bit is set.  Accesses to the
0059  * page will cause a fault, and handle_pte_fault() will set the young bit
0060  * for us as long as the page is marked present in the corresponding Linux
0061  * PTE entry.  Again, ptep_set_access_flags() will ensure that the TLB is
0062  * up to date.
0063  *
0064  * However, when the "young" bit is cleared, we deny access to the page
0065  * by clearing the hardware PTE.  Currently Linux does not flush the TLB
0066  * for us in this case, which means the TLB will retain the transation
0067  * until either the TLB entry is evicted under pressure, or a context
0068  * switch which changes the user space mapping occurs.
0069  */
0070 #define PTRS_PER_PTE        512
0071 #define PTRS_PER_PMD        1
0072 #define PTRS_PER_PGD        2048
0073 
0074 #define PTE_HWTABLE_PTRS    (PTRS_PER_PTE)
0075 #define PTE_HWTABLE_OFF     (PTE_HWTABLE_PTRS * sizeof(pte_t))
0076 #define PTE_HWTABLE_SIZE    (PTRS_PER_PTE * sizeof(u32))
0077 
0078 #define MAX_POSSIBLE_PHYSMEM_BITS   32
0079 
0080 /*
0081  * PMD_SHIFT determines the size of the area a second-level page table can map
0082  * PGDIR_SHIFT determines what a third-level page table entry can map
0083  */
0084 #define PMD_SHIFT       21
0085 #define PGDIR_SHIFT     21
0086 
0087 #define PMD_SIZE        (1UL << PMD_SHIFT)
0088 #define PMD_MASK        (~(PMD_SIZE-1))
0089 #define PGDIR_SIZE      (1UL << PGDIR_SHIFT)
0090 #define PGDIR_MASK      (~(PGDIR_SIZE-1))
0091 
0092 /*
0093  * section address mask and size definitions.
0094  */
0095 #define SECTION_SHIFT       20
0096 #define SECTION_SIZE        (1UL << SECTION_SHIFT)
0097 #define SECTION_MASK        (~(SECTION_SIZE-1))
0098 
0099 /*
0100  * ARMv6 supersection address mask and size definitions.
0101  */
0102 #define SUPERSECTION_SHIFT  24
0103 #define SUPERSECTION_SIZE   (1UL << SUPERSECTION_SHIFT)
0104 #define SUPERSECTION_MASK   (~(SUPERSECTION_SIZE-1))
0105 
0106 #define USER_PTRS_PER_PGD   (TASK_SIZE / PGDIR_SIZE)
0107 
0108 /*
0109  * "Linux" PTE definitions.
0110  *
0111  * We keep two sets of PTEs - the hardware and the linux version.
0112  * This allows greater flexibility in the way we map the Linux bits
0113  * onto the hardware tables, and allows us to have YOUNG and DIRTY
0114  * bits.
0115  *
0116  * The PTE table pointer refers to the hardware entries; the "Linux"
0117  * entries are stored 1024 bytes below.
0118  */
0119 #define L_PTE_VALID     (_AT(pteval_t, 1) << 0)     /* Valid */
0120 #define L_PTE_PRESENT       (_AT(pteval_t, 1) << 0)
0121 #define L_PTE_YOUNG     (_AT(pteval_t, 1) << 1)
0122 #define L_PTE_DIRTY     (_AT(pteval_t, 1) << 6)
0123 #define L_PTE_RDONLY        (_AT(pteval_t, 1) << 7)
0124 #define L_PTE_USER      (_AT(pteval_t, 1) << 8)
0125 #define L_PTE_XN        (_AT(pteval_t, 1) << 9)
0126 #define L_PTE_SHARED        (_AT(pteval_t, 1) << 10)    /* shared(v6), coherent(xsc3) */
0127 #define L_PTE_NONE      (_AT(pteval_t, 1) << 11)
0128 
0129 /*
0130  * These are the memory types, defined to be compatible with
0131  * pre-ARMv6 CPUs cacheable and bufferable bits: n/a,n/a,C,B
0132  * ARMv6+ without TEX remapping, they are a table index.
0133  * ARMv6+ with TEX remapping, they correspond to n/a,TEX(0),C,B
0134  *
0135  * MT type      Pre-ARMv6   ARMv6+ type / cacheable status
0136  * UNCACHED     Uncached    Strongly ordered
0137  * BUFFERABLE       Bufferable  Normal memory / non-cacheable
0138  * WRITETHROUGH     Writethrough    Normal memory / write through
0139  * WRITEBACK        Writeback   Normal memory / write back, read alloc
0140  * MINICACHE        Minicache   N/A
0141  * WRITEALLOC       Writeback   Normal memory / write back, write alloc
0142  * DEV_SHARED       Uncached    Device memory (shared)
0143  * DEV_NONSHARED    Uncached    Device memory (non-shared)
0144  * DEV_WC       Bufferable  Normal memory / non-cacheable
0145  * DEV_CACHED       Writeback   Normal memory / write back, read alloc
0146  * VECTORS      Variable    Normal memory / variable
0147  *
0148  * All normal memory mappings have the following properties:
0149  * - reads can be repeated with no side effects
0150  * - repeated reads return the last value written
0151  * - reads can fetch additional locations without side effects
0152  * - writes can be repeated (in certain cases) with no side effects
0153  * - writes can be merged before accessing the target
0154  * - unaligned accesses can be supported
0155  *
0156  * All device mappings have the following properties:
0157  * - no access speculation
0158  * - no repetition (eg, on return from an exception)
0159  * - number, order and size of accesses are maintained
0160  * - unaligned accesses are "unpredictable"
0161  */
0162 #define L_PTE_MT_UNCACHED   (_AT(pteval_t, 0x00) << 2)  /* 0000 */
0163 #define L_PTE_MT_BUFFERABLE (_AT(pteval_t, 0x01) << 2)  /* 0001 */
0164 #define L_PTE_MT_WRITETHROUGH   (_AT(pteval_t, 0x02) << 2)  /* 0010 */
0165 #define L_PTE_MT_WRITEBACK  (_AT(pteval_t, 0x03) << 2)  /* 0011 */
0166 #define L_PTE_MT_MINICACHE  (_AT(pteval_t, 0x06) << 2)  /* 0110 (sa1100, xscale) */
0167 #define L_PTE_MT_WRITEALLOC (_AT(pteval_t, 0x07) << 2)  /* 0111 */
0168 #define L_PTE_MT_DEV_SHARED (_AT(pteval_t, 0x04) << 2)  /* 0100 */
0169 #define L_PTE_MT_DEV_NONSHARED  (_AT(pteval_t, 0x0c) << 2)  /* 1100 */
0170 #define L_PTE_MT_DEV_WC     (_AT(pteval_t, 0x09) << 2)  /* 1001 */
0171 #define L_PTE_MT_DEV_CACHED (_AT(pteval_t, 0x0b) << 2)  /* 1011 */
0172 #define L_PTE_MT_VECTORS    (_AT(pteval_t, 0x0f) << 2)  /* 1111 */
0173 #define L_PTE_MT_MASK       (_AT(pteval_t, 0x0f) << 2)
0174 
0175 #ifndef __ASSEMBLY__
0176 
0177 /*
0178  * The "pud_xxx()" functions here are trivial when the pmd is folded into
0179  * the pud: the pud entry is never bad, always exists, and can't be set or
0180  * cleared.
0181  */
0182 static inline int pud_none(pud_t pud)
0183 {
0184     return 0;
0185 }
0186 
0187 static inline int pud_bad(pud_t pud)
0188 {
0189     return 0;
0190 }
0191 
0192 static inline int pud_present(pud_t pud)
0193 {
0194     return 1;
0195 }
0196 
0197 static inline void pud_clear(pud_t *pudp)
0198 {
0199 }
0200 
0201 static inline void set_pud(pud_t *pudp, pud_t pud)
0202 {
0203 }
0204 
0205 static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
0206 {
0207     return (pmd_t *)pud;
0208 }
0209 #define pmd_offset pmd_offset
0210 
0211 #define pmd_pfn(pmd)        (__phys_to_pfn(pmd_val(pmd) & PHYS_MASK))
0212 
0213 #define pmd_large(pmd)      (pmd_val(pmd) & 2)
0214 #define pmd_leaf(pmd)       (pmd_val(pmd) & 2)
0215 #define pmd_bad(pmd)        (pmd_val(pmd) & 2)
0216 #define pmd_present(pmd)    (pmd_val(pmd))
0217 
0218 #define copy_pmd(pmdpd,pmdps)       \
0219     do {                \
0220         pmdpd[0] = pmdps[0];    \
0221         pmdpd[1] = pmdps[1];    \
0222         flush_pmd_entry(pmdpd); \
0223     } while (0)
0224 
0225 #define pmd_clear(pmdp)         \
0226     do {                \
0227         pmdp[0] = __pmd(0); \
0228         pmdp[1] = __pmd(0); \
0229         clean_pmd_entry(pmdp);  \
0230     } while (0)
0231 
0232 /* we don't need complex calculations here as the pmd is folded into the pgd */
0233 #define pmd_addr_end(addr,end) (end)
0234 
0235 #define set_pte_ext(ptep,pte,ext) cpu_set_pte_ext(ptep,pte,ext)
0236 
0237 /*
0238  * We don't have huge page support for short descriptors, for the moment
0239  * define empty stubs for use by pin_page_for_write.
0240  */
0241 #define pmd_hugewillfault(pmd)  (0)
0242 #define pmd_thp_or_huge(pmd)    (0)
0243 
0244 #endif /* __ASSEMBLY__ */
0245 
0246 #endif /* _ASM_PGTABLE_2LEVEL_H */