| 1 | /*
|
|---|
| 2 | * Copyright (c) 2007 Pavel Jancik
|
|---|
| 3 | * Copyright (c) 2007 Michal Kebrt
|
|---|
| 4 | * Copyright (c) 2012 Jan Vesely
|
|---|
| 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 11 | * - Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /** @addtogroup arm32mm
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /** @file
|
|---|
| 35 | * @brief Paging related declarations.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #ifndef KERN_arm32_PAGE_armv4_H_
|
|---|
| 39 | #define KERN_arm32_PAGE_armv4_H_
|
|---|
| 40 |
|
|---|
| 41 | #ifndef KERN_arm32_PAGE_H_
|
|---|
| 42 | #error "Do not include arch specific page.h directly use generic page.h instead"
|
|---|
| 43 | #endif
|
|---|
| 44 |
|
|---|
| 45 | /* Macros for querying the last-level PTE entries. */
|
|---|
| 46 | #define PTE_VALID_ARCH(pte) \
|
|---|
| 47 | (((pte_t *) (pte))->l0.should_be_zero != 0 || PTE_PRESENT_ARCH(pte))
|
|---|
| 48 | #define PTE_PRESENT_ARCH(pte) \
|
|---|
| 49 | (((pte_t *) (pte))->l0.descriptor_type != 0)
|
|---|
| 50 | #define PTE_GET_FRAME_ARCH(pte) \
|
|---|
| 51 | (((uintptr_t) ((pte_t *) (pte))->l1.frame_base_addr) << FRAME_WIDTH)
|
|---|
| 52 | #define PTE_WRITABLE_ARCH(pte) \
|
|---|
| 53 | (((pte_t *) (pte))->l1.access_permission_0 == PTE_AP_USER_RW_KERNEL_RW)
|
|---|
| 54 | #define PTE_EXECUTABLE_ARCH(pte) \
|
|---|
| 55 | 1
|
|---|
| 56 |
|
|---|
| 57 | #ifndef __ASM__
|
|---|
| 58 |
|
|---|
| 59 | /** Level 0 page table entry. */
|
|---|
| 60 | typedef struct {
|
|---|
| 61 | /* 0b01 for coarse tables, see below for details */
|
|---|
| 62 | unsigned descriptor_type : 2;
|
|---|
| 63 | unsigned impl_specific : 3;
|
|---|
| 64 | unsigned domain : 4;
|
|---|
| 65 | unsigned should_be_zero : 1;
|
|---|
| 66 |
|
|---|
| 67 | /* Pointer to the coarse 2nd level page table (holding entries for small
|
|---|
| 68 | * (4KB) or large (64KB) pages. ARM also supports fine 2nd level page
|
|---|
| 69 | * tables that may hold even tiny pages (1KB) but they are bigger (4KB
|
|---|
| 70 | * per table in comparison with 1KB per the coarse table)
|
|---|
| 71 | */
|
|---|
| 72 | unsigned coarse_table_addr : 22;
|
|---|
| 73 | } ATTRIBUTE_PACKED pte_level0_t;
|
|---|
| 74 |
|
|---|
| 75 | /** Level 1 page table entry (small (4KB) pages used). */
|
|---|
| 76 | typedef struct {
|
|---|
| 77 |
|
|---|
| 78 | /* 0b10 for small pages */
|
|---|
| 79 | unsigned descriptor_type : 2;
|
|---|
| 80 | unsigned bufferable : 1;
|
|---|
| 81 | unsigned cacheable : 1;
|
|---|
| 82 |
|
|---|
| 83 | /* access permissions for each of 4 subparts of a page
|
|---|
| 84 | * (for each 1KB when small pages used */
|
|---|
| 85 | unsigned access_permission_0 : 2;
|
|---|
| 86 | unsigned access_permission_1 : 2;
|
|---|
| 87 | unsigned access_permission_2 : 2;
|
|---|
| 88 | unsigned access_permission_3 : 2;
|
|---|
| 89 | unsigned frame_base_addr : 20;
|
|---|
| 90 | } ATTRIBUTE_PACKED pte_level1_t;
|
|---|
| 91 |
|
|---|
| 92 | typedef union {
|
|---|
| 93 | pte_level0_t l0;
|
|---|
| 94 | pte_level1_t l1;
|
|---|
| 95 | } pte_t;
|
|---|
| 96 |
|
|---|
| 97 | /* Level 1 page tables access permissions */
|
|---|
| 98 |
|
|---|
| 99 | /** User mode: no access, privileged mode: no access. */
|
|---|
| 100 | #define PTE_AP_USER_NO_KERNEL_NO 0
|
|---|
| 101 |
|
|---|
| 102 | /** User mode: no access, privileged mode: read/write. */
|
|---|
| 103 | #define PTE_AP_USER_NO_KERNEL_RW 1
|
|---|
| 104 |
|
|---|
| 105 | /** User mode: read only, privileged mode: read/write. */
|
|---|
| 106 | #define PTE_AP_USER_RO_KERNEL_RW 2
|
|---|
| 107 |
|
|---|
| 108 | /** User mode: read/write, privileged mode: read/write. */
|
|---|
| 109 | #define PTE_AP_USER_RW_KERNEL_RW 3
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | /* pte_level0_t and pte_level1_t descriptor_type flags */
|
|---|
| 113 |
|
|---|
| 114 | /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
|
|---|
| 115 | #define PTE_DESCRIPTOR_NOT_PRESENT 0
|
|---|
| 116 |
|
|---|
| 117 | /** pte_level0_t coarse page table flag (used in descriptor_type). */
|
|---|
| 118 | #define PTE_DESCRIPTOR_COARSE_TABLE 1
|
|---|
| 119 |
|
|---|
| 120 | /** pte_level1_t small page table flag (used in descriptor type). */
|
|---|
| 121 | #define PTE_DESCRIPTOR_SMALL_PAGE 2
|
|---|
| 122 |
|
|---|
| 123 | #define pt_coherence_m(pt, count) \
|
|---|
| 124 | do { \
|
|---|
| 125 | for (unsigned i = 0; i < count; ++i) \
|
|---|
| 126 | dcache_clean_mva_pou((uintptr_t)(pt + i)); \
|
|---|
| 127 | read_barrier(); \
|
|---|
| 128 | } while (0)
|
|---|
| 129 |
|
|---|
| 130 | /** Returns level 0 page table entry flags.
|
|---|
| 131 | *
|
|---|
| 132 | * @param pt Level 0 page table.
|
|---|
| 133 | * @param i Index of the entry to return.
|
|---|
| 134 | *
|
|---|
| 135 | */
|
|---|
| 136 | NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
|---|
| 137 | {
|
|---|
| 138 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 139 | int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
|---|
| 140 |
|
|---|
| 141 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
|---|
| 142 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
|---|
| 143 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Returns level 1 page table entry flags.
|
|---|
| 147 | *
|
|---|
| 148 | * @param pt Level 1 page table.
|
|---|
| 149 | * @param i Index of the entry to return.
|
|---|
| 150 | *
|
|---|
| 151 | */
|
|---|
| 152 | NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
|---|
| 153 | {
|
|---|
| 154 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 155 |
|
|---|
| 156 | int dt = p->descriptor_type;
|
|---|
| 157 | int ap = p->access_permission_0;
|
|---|
| 158 |
|
|---|
| 159 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
|---|
| 160 | ((ap == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
|---|
| 161 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT) |
|
|---|
| 162 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
|---|
| 163 | ((ap != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT) |
|
|---|
| 164 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
|---|
| 165 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
|---|
| 166 | (1 << PAGE_EXEC_SHIFT) |
|
|---|
| 167 | (p->bufferable << PAGE_CACHEABLE);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /** Sets flags of level 0 page table entry.
|
|---|
| 171 | *
|
|---|
| 172 | * @param pt level 0 page table
|
|---|
| 173 | * @param i index of the entry to be changed
|
|---|
| 174 | * @param flags new flags
|
|---|
| 175 | *
|
|---|
| 176 | */
|
|---|
| 177 | NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 178 | {
|
|---|
| 179 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 180 |
|
|---|
| 181 | if (flags & PAGE_NOT_PRESENT) {
|
|---|
| 182 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 183 | /*
|
|---|
| 184 | * Ensures that the entry will be recognized as valid when
|
|---|
| 185 | * PTE_VALID_ARCH applied.
|
|---|
| 186 | */
|
|---|
| 187 | p->should_be_zero = 1;
|
|---|
| 188 | } else {
|
|---|
| 189 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 190 | p->should_be_zero = 0;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | /** Sets flags of level 1 page table entry.
|
|---|
| 196 | *
|
|---|
| 197 | * We use same access rights for the whole page. When page
|
|---|
| 198 | * is not preset we store 1 in acess_rigts_3 so that at least
|
|---|
| 199 | * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
|
|---|
| 200 | *
|
|---|
| 201 | * @param pt Level 1 page table.
|
|---|
| 202 | * @param i Index of the entry to be changed.
|
|---|
| 203 | * @param flags New flags.
|
|---|
| 204 | *
|
|---|
| 205 | */
|
|---|
| 206 | NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 207 | {
|
|---|
| 208 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 209 |
|
|---|
| 210 | if (flags & PAGE_NOT_PRESENT)
|
|---|
| 211 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 212 | else
|
|---|
| 213 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 214 |
|
|---|
| 215 | p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
|
|---|
| 216 |
|
|---|
| 217 | /* default access permission */
|
|---|
| 218 | p->access_permission_0 = p->access_permission_1 =
|
|---|
| 219 | p->access_permission_2 = p->access_permission_3 =
|
|---|
| 220 | PTE_AP_USER_NO_KERNEL_RW;
|
|---|
| 221 |
|
|---|
| 222 | if (flags & PAGE_USER) {
|
|---|
| 223 | if (flags & PAGE_READ) {
|
|---|
| 224 | p->access_permission_0 = p->access_permission_1 =
|
|---|
| 225 | p->access_permission_2 = p->access_permission_3 =
|
|---|
| 226 | PTE_AP_USER_RO_KERNEL_RW;
|
|---|
| 227 | }
|
|---|
| 228 | if (flags & PAGE_WRITE) {
|
|---|
| 229 | p->access_permission_0 = p->access_permission_1 =
|
|---|
| 230 | p->access_permission_2 = p->access_permission_3 =
|
|---|
| 231 | PTE_AP_USER_RW_KERNEL_RW;
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
|---|
| 237 | {
|
|---|
| 238 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 239 |
|
|---|
| 240 | p->should_be_zero = 0;
|
|---|
| 241 | write_barrier();
|
|---|
| 242 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 | NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
|---|
| 247 | {
|
|---|
| 248 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 249 |
|
|---|
| 250 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 | extern void page_arch_init(void);
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 | #endif /* __ASM__ */
|
|---|
| 258 |
|
|---|
| 259 | #endif
|
|---|
| 260 |
|
|---|
| 261 | /** @}
|
|---|
| 262 | */
|
|---|