| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup arm32mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Paging related declarations.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef KERN_arm32_PAGE_armv7_H_
|
|---|
| 37 | #define KERN_arm32_PAGE_armv7_H_
|
|---|
| 38 |
|
|---|
| 39 | #ifndef KERN_arm32_PAGE_H_
|
|---|
| 40 | #error "Do not include arch specific page.h directly use generic page.h instead"
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | /* Macros for querying the last-level PTE entries. */
|
|---|
| 44 | #define PTE_VALID_ARCH(pte) \
|
|---|
| 45 | (*((uint32_t *) (pte)) != 0)
|
|---|
| 46 | #define PTE_PRESENT_ARCH(pte) \
|
|---|
| 47 | (((pte_t *) (pte))->l0.descriptor_type != 0)
|
|---|
| 48 | #define PTE_GET_FRAME_ARCH(pte) \
|
|---|
| 49 | (((pte_t *) (pte))->l1.frame_base_addr << FRAME_WIDTH)
|
|---|
| 50 | #define PTE_WRITABLE_ARCH(pte) \
|
|---|
| 51 | (((pte_t *) (pte))->l1.access_permission_1 != PTE_AP1_RO)
|
|---|
| 52 | #define PTE_EXECUTABLE_ARCH(pte) \
|
|---|
| 53 | (((pte_t *) (pte))->l1.descriptor_type != PTE_DESCRIPTOR_SMALL_PAGE_NX)
|
|---|
| 54 |
|
|---|
| 55 | #ifndef __ASM__
|
|---|
| 56 |
|
|---|
| 57 | /** Level 0 page table entry. */
|
|---|
| 58 | typedef struct {
|
|---|
| 59 | /* 0b01 for coarse tables, see below for details */
|
|---|
| 60 | unsigned descriptor_type : 2;
|
|---|
| 61 | unsigned pxn : 1;
|
|---|
| 62 | unsigned ns : 1;
|
|---|
| 63 | unsigned should_be_zero_0 : 1;
|
|---|
| 64 | unsigned domain : 4;
|
|---|
| 65 | unsigned should_be_zero_1 : 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, 0b11 for NX small pages */
|
|---|
| 79 | unsigned descriptor_type : 2;
|
|---|
| 80 | unsigned bufferable : 1;
|
|---|
| 81 | unsigned cacheable : 1;
|
|---|
| 82 | unsigned access_permission_0 : 2;
|
|---|
| 83 | unsigned tex : 3;
|
|---|
| 84 | unsigned access_permission_1 : 1;
|
|---|
| 85 | unsigned shareable : 1;
|
|---|
| 86 | unsigned non_global : 1;
|
|---|
| 87 | unsigned frame_base_addr : 20;
|
|---|
| 88 | } ATTRIBUTE_PACKED pte_level1_t;
|
|---|
| 89 |
|
|---|
| 90 | typedef union {
|
|---|
| 91 | pte_level0_t l0;
|
|---|
| 92 | pte_level1_t l1;
|
|---|
| 93 | } pte_t;
|
|---|
| 94 |
|
|---|
| 95 | /* Level 1 page tables access permissions */
|
|---|
| 96 |
|
|---|
| 97 | /** User mode: no access, privileged mode: no access. */
|
|---|
| 98 | #define PTE_AP0_USER_NO_KERNEL_NO 0
|
|---|
| 99 |
|
|---|
| 100 | /** User mode: no access, privileged mode: read/write. */
|
|---|
| 101 | #define PTE_AP0_USER_NO_KERNEL_FULL 1
|
|---|
| 102 |
|
|---|
| 103 | /** User mode: read only, privileged mode: read/write. */
|
|---|
| 104 | #define PTE_AP0_USER_LIMITED_KERNEL_FULL 2
|
|---|
| 105 |
|
|---|
| 106 | /** User mode: read/write, privileged mode: read/write. */
|
|---|
| 107 | #define PTE_AP0_USER_FULL_KERNEL_FULL 3
|
|---|
| 108 |
|
|---|
| 109 | /** Allow writes */
|
|---|
| 110 | #define PTE_AP1_RO 1
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | /* pte_level0_t and pte_level1_t descriptor_type flags */
|
|---|
| 114 |
|
|---|
| 115 | /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
|
|---|
| 116 | #define PTE_DESCRIPTOR_NOT_PRESENT 0
|
|---|
| 117 |
|
|---|
| 118 | /** pte_level0_t coarse page table flag (used in descriptor_type). */
|
|---|
| 119 | #define PTE_DESCRIPTOR_COARSE_TABLE 1
|
|---|
| 120 |
|
|---|
| 121 | /** pte_level1_t small page table flag (used in descriptor type). */
|
|---|
| 122 | #define PTE_DESCRIPTOR_SMALL_PAGE 2
|
|---|
| 123 |
|
|---|
| 124 | /** pte_level1_t small page table flag with NX (used in descriptor type). */
|
|---|
| 125 | #define PTE_DESCRIPTOR_SMALL_PAGE_NX 3
|
|---|
| 126 |
|
|---|
| 127 | /** Sets the address of level 0 page table.
|
|---|
| 128 | *
|
|---|
| 129 | * @param pt Pointer to the page table to set.
|
|---|
| 130 | *
|
|---|
| 131 | */
|
|---|
| 132 | NO_TRACE static inline void set_ptl0_addr(pte_t *pt)
|
|---|
| 133 | {
|
|---|
| 134 | asm volatile (
|
|---|
| 135 | "mcr p15, 0, %[pt], c2, c0, 0\n"
|
|---|
| 136 | :: [pt] "r" (pt)
|
|---|
| 137 | );
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | /** Returns level 0 page table entry flags.
|
|---|
| 142 | *
|
|---|
| 143 | * @param pt Level 0 page table.
|
|---|
| 144 | * @param i Index of the entry to return.
|
|---|
| 145 | *
|
|---|
| 146 | */
|
|---|
| 147 | NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
|---|
| 148 | {
|
|---|
| 149 | const pte_level0_t *p = &pt[i].l0;
|
|---|
| 150 | const unsigned np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
|---|
| 151 |
|
|---|
| 152 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
|---|
| 153 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
|---|
| 154 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /** Returns level 1 page table entry flags.
|
|---|
| 158 | *
|
|---|
| 159 | * @param pt Level 1 page table.
|
|---|
| 160 | * @param i Index of the entry to return.
|
|---|
| 161 | *
|
|---|
| 162 | */
|
|---|
| 163 | NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
|---|
| 164 | {
|
|---|
| 165 | const pte_level1_t *p = &pt[i].l1;
|
|---|
| 166 |
|
|---|
| 167 | const unsigned dt = p->descriptor_type;
|
|---|
| 168 | const unsigned ap0 = p->access_permission_0;
|
|---|
| 169 | const unsigned ap1 = p->access_permission_1;
|
|---|
| 170 |
|
|---|
| 171 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
|---|
| 172 | ((dt != PTE_DESCRIPTOR_SMALL_PAGE_NX) << PAGE_EXEC_SHIFT) |
|
|---|
| 173 | ((ap0 == PTE_AP0_USER_LIMITED_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 174 | ((ap0 == PTE_AP0_USER_FULL_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 175 | ((ap0 == PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 176 | ((ap0 != PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_USER_SHIFT) |
|
|---|
| 177 | (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_FULL_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
|
|---|
| 178 | (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_NO_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
|
|---|
| 179 | (p->bufferable << PAGE_CACHEABLE);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Sets flags of level 0 page table entry.
|
|---|
| 183 | *
|
|---|
| 184 | * @param pt level 0 page table
|
|---|
| 185 | * @param i index of the entry to be changed
|
|---|
| 186 | * @param flags new flags
|
|---|
| 187 | *
|
|---|
| 188 | */
|
|---|
| 189 | NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 190 | {
|
|---|
| 191 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 192 |
|
|---|
| 193 | if (flags & PAGE_NOT_PRESENT) {
|
|---|
| 194 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 195 | /*
|
|---|
| 196 | * Ensures that the entry will be recognized as valid when
|
|---|
| 197 | * PTE_VALID_ARCH applied.
|
|---|
| 198 | */
|
|---|
| 199 | p->should_be_zero_0 = 1;
|
|---|
| 200 | p->should_be_zero_1 = 1;
|
|---|
| 201 | } else {
|
|---|
| 202 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 203 | p->should_be_zero_0 = 0;
|
|---|
| 204 | p->should_be_zero_1 = 0;
|
|---|
| 205 | p->domain = 0;
|
|---|
| 206 | p->ns = 0;
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | /** Sets flags of level 1 page table entry.
|
|---|
| 212 | *
|
|---|
| 213 | * We use same access rights for the whole page. When page
|
|---|
| 214 | * is not preset we store 1 in acess_rigts_3 so that at least
|
|---|
| 215 | * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
|
|---|
| 216 | *
|
|---|
| 217 | * @param pt Level 1 page table.
|
|---|
| 218 | * @param i Index of the entry to be changed.
|
|---|
| 219 | * @param flags New flags.
|
|---|
| 220 | *
|
|---|
| 221 | */
|
|---|
| 222 | NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 223 | {
|
|---|
| 224 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 225 |
|
|---|
| 226 | if (flags & PAGE_NOT_PRESENT) {
|
|---|
| 227 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 228 | } else {
|
|---|
| 229 | if (flags & PAGE_EXEC)
|
|---|
| 230 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 231 | else
|
|---|
| 232 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE_NX;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /* tex=0 buf=1 and cache=1 => normal memory
|
|---|
| 236 | * tex=0 buf=1 and cache=0 => shareable device mmio
|
|---|
| 237 | */
|
|---|
| 238 | p->cacheable = (flags & PAGE_CACHEABLE);
|
|---|
| 239 | p->bufferable = 1;
|
|---|
| 240 | p->tex = 0;
|
|---|
| 241 |
|
|---|
| 242 | /* Shareable is ignored for devices (non-cacheable),
|
|---|
| 243 | * turn it on for normal memory. */
|
|---|
| 244 | p->shareable = 1;
|
|---|
| 245 |
|
|---|
| 246 | p->non_global = !(flags & PAGE_GLOBAL);
|
|---|
| 247 |
|
|---|
| 248 | /* default access permission: kernel only*/
|
|---|
| 249 | p->access_permission_0 = PTE_AP0_USER_NO_KERNEL_FULL;
|
|---|
| 250 |
|
|---|
| 251 | if (flags & PAGE_USER) {
|
|---|
| 252 | p->access_permission_0 = PTE_AP0_USER_FULL_KERNEL_FULL;
|
|---|
| 253 | // TODO Fix kernel to use PAGE_WRITE flag properly and
|
|---|
| 254 | // apply this for kernel pages as well.
|
|---|
| 255 | if (!(flags & PAGE_WRITE))
|
|---|
| 256 | p->access_permission_1 = PTE_AP1_RO;
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
|---|
| 261 | {
|
|---|
| 262 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 263 |
|
|---|
| 264 | p->should_be_zero_0 = 0;
|
|---|
| 265 | p->should_be_zero_1 = 0;
|
|---|
| 266 | write_barrier();
|
|---|
| 267 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
|---|
| 271 | {
|
|---|
| 272 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 273 |
|
|---|
| 274 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 | extern void page_arch_init(void);
|
|---|
| 279 |
|
|---|
| 280 | #endif /* __ASM__ */
|
|---|
| 281 |
|
|---|
| 282 | #endif
|
|---|
| 283 |
|
|---|
| 284 | /** @}
|
|---|
| 285 | */
|
|---|