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