| 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 |
|
|---|
| 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_1 != PTE_AP1_RO)
|
|---|
| 53 | #define PTE_EXECUTABLE_ARCH(pte) \
|
|---|
| 54 | (((pte_t *) (pte))->l1.descriptor_type != PTE_DESCRIPTOR_SMALL_PAGE_NX)
|
|---|
| 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 pxn : 1;
|
|---|
| 63 | unsigned ns : 1;
|
|---|
| 64 | unsigned should_be_zero_0 : 1;
|
|---|
| 65 | unsigned domain : 4;
|
|---|
| 66 | unsigned should_be_zero_1 : 1;
|
|---|
| 67 |
|
|---|
| 68 | /* Pointer to the coarse 2nd level page table (holding entries for small
|
|---|
| 69 | * (4KB) or large (64KB) pages. ARM also supports fine 2nd level page
|
|---|
| 70 | * tables that may hold even tiny pages (1KB) but they are bigger (4KB
|
|---|
| 71 | * per table in comparison with 1KB per the coarse table)
|
|---|
| 72 | */
|
|---|
| 73 | unsigned coarse_table_addr : 22;
|
|---|
| 74 | } ATTRIBUTE_PACKED pte_level0_t;
|
|---|
| 75 |
|
|---|
| 76 | /** Level 1 page table entry (small (4KB) pages used). */
|
|---|
| 77 | typedef struct {
|
|---|
| 78 |
|
|---|
| 79 | /* 0b10 for small pages, 0b11 for NX small pages */
|
|---|
| 80 | unsigned descriptor_type : 2;
|
|---|
| 81 | unsigned bufferable : 1;
|
|---|
| 82 | unsigned cacheable : 1;
|
|---|
| 83 | unsigned access_permission_0 : 2;
|
|---|
| 84 | unsigned tex : 3;
|
|---|
| 85 | unsigned access_permission_1 : 1;
|
|---|
| 86 | unsigned shareable : 1;
|
|---|
| 87 | unsigned non_global : 1;
|
|---|
| 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_AP0_USER_NO_KERNEL_NO 0
|
|---|
| 100 |
|
|---|
| 101 | /** User mode: no access, privileged mode: read/write. */
|
|---|
| 102 | #define PTE_AP0_USER_NO_KERNEL_FULL 1
|
|---|
| 103 |
|
|---|
| 104 | /** User mode: read only, privileged mode: read/write. */
|
|---|
| 105 | #define PTE_AP0_USER_LIMITED_KERNEL_FULL 2
|
|---|
| 106 |
|
|---|
| 107 | /** User mode: read/write, privileged mode: read/write. */
|
|---|
| 108 | #define PTE_AP0_USER_FULL_KERNEL_FULL 3
|
|---|
| 109 |
|
|---|
| 110 | /** Allow writes */
|
|---|
| 111 | #define PTE_AP1_RO 1
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | /* pte_level0_t and pte_level1_t descriptor_type flags */
|
|---|
| 115 |
|
|---|
| 116 | /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
|
|---|
| 117 | #define PTE_DESCRIPTOR_NOT_PRESENT 0
|
|---|
| 118 |
|
|---|
| 119 | /** pte_level0_t coarse page table flag (used in descriptor_type). */
|
|---|
| 120 | #define PTE_DESCRIPTOR_COARSE_TABLE 1
|
|---|
| 121 |
|
|---|
| 122 | /** pte_level1_t small page table flag (used in descriptor type). */
|
|---|
| 123 | #define PTE_DESCRIPTOR_SMALL_PAGE 2
|
|---|
| 124 |
|
|---|
| 125 | /** pte_level1_t small page table flag with NX (used in descriptor type). */
|
|---|
| 126 | #define PTE_DESCRIPTOR_SMALL_PAGE_NX 3
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * For an ARMv7 implementation that does not include the Large Physical Address Extension,
|
|---|
| 131 | * and in implementations of architecture versions before ARMv7, if the translation tables
|
|---|
| 132 | * are held in Write-Back Cacheable memory, the caches must be cleaned to the point of
|
|---|
| 133 | * unification after writing to the translation tables and before the DSB instruction. This
|
|---|
| 134 | * ensures that the updated translation table are visible to a hardware translation table walk.
|
|---|
| 135 | *
|
|---|
| 136 | * Therefore, an example instruction sequence for writing a translation table entry,
|
|---|
| 137 | * covering changes to the instruction
|
|---|
| 138 | * or data mappings in a uniprocessor system is:
|
|---|
| 139 | * STR rx, [Translation table entry]
|
|---|
| 140 | * ; write new entry to the translation table
|
|---|
| 141 | * Clean cache line [Translation table entry] : This operation is not required with the
|
|---|
| 142 | * ; Multiprocessing Extensions.
|
|---|
| 143 | * DSB
|
|---|
| 144 | * ; ensures visibility of the data cleaned from the D Cache
|
|---|
| 145 | * Invalidate TLB entry by MVA (and ASID if non-global) [page address]
|
|---|
| 146 | * Invalidate BTC
|
|---|
| 147 | * DSB
|
|---|
| 148 | * ; ensure completion of the Invalidate TLB operation
|
|---|
| 149 | * ISB
|
|---|
| 150 | * ; ensure table changes visible to instruction fetch
|
|---|
| 151 | *
|
|---|
| 152 | * ARM Architecture reference chp. B3.10.1 p. B3-1375
|
|---|
| 153 | * @note: see TTRB0/1 for pt memory type
|
|---|
| 154 | */
|
|---|
| 155 | #define pt_coherence_m(pt, count) \
|
|---|
| 156 | do { \
|
|---|
| 157 | for (unsigned i = 0; i < count; ++i) \
|
|---|
| 158 | DCCMVAU_write((uintptr_t)(pt + i)); \
|
|---|
| 159 | read_barrier(); \
|
|---|
| 160 | } while (0)
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | /** Returns level 0 page table entry flags.
|
|---|
| 164 | *
|
|---|
| 165 | * @param pt Level 0 page table.
|
|---|
| 166 | * @param i Index of the entry to return.
|
|---|
| 167 | *
|
|---|
| 168 | */
|
|---|
| 169 | NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
|---|
| 170 | {
|
|---|
| 171 | const pte_level0_t *p = &pt[i].l0;
|
|---|
| 172 | const unsigned np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
|---|
| 173 |
|
|---|
| 174 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
|---|
| 175 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
|---|
| 176 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** Returns level 1 page table entry flags.
|
|---|
| 180 | *
|
|---|
| 181 | * @param pt Level 1 page table.
|
|---|
| 182 | * @param i Index of the entry to return.
|
|---|
| 183 | *
|
|---|
| 184 | */
|
|---|
| 185 | NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
|---|
| 186 | {
|
|---|
| 187 | const pte_level1_t *p = &pt[i].l1;
|
|---|
| 188 |
|
|---|
| 189 | const unsigned dt = p->descriptor_type;
|
|---|
| 190 | const unsigned ap0 = p->access_permission_0;
|
|---|
| 191 | const unsigned ap1 = p->access_permission_1;
|
|---|
| 192 |
|
|---|
| 193 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
|---|
| 194 | ((dt != PTE_DESCRIPTOR_SMALL_PAGE_NX) << PAGE_EXEC_SHIFT) |
|
|---|
| 195 | ((ap0 == PTE_AP0_USER_LIMITED_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 196 | ((ap0 == PTE_AP0_USER_FULL_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 197 | ((ap0 == PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_READ_SHIFT) |
|
|---|
| 198 | ((ap0 != PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_USER_SHIFT) |
|
|---|
| 199 | (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_FULL_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
|
|---|
| 200 | (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_NO_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
|
|---|
| 201 | (p->bufferable << PAGE_CACHEABLE);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /** Sets flags of level 0 page table entry.
|
|---|
| 205 | *
|
|---|
| 206 | * @param pt level 0 page table
|
|---|
| 207 | * @param i index of the entry to be changed
|
|---|
| 208 | * @param flags new flags
|
|---|
| 209 | *
|
|---|
| 210 | */
|
|---|
| 211 | NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 212 | {
|
|---|
| 213 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 214 |
|
|---|
| 215 | if (flags & PAGE_NOT_PRESENT) {
|
|---|
| 216 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 217 | /*
|
|---|
| 218 | * Ensures that the entry will be recognized as valid when
|
|---|
| 219 | * PTE_VALID_ARCH applied.
|
|---|
| 220 | */
|
|---|
| 221 | p->should_be_zero_0 = 1;
|
|---|
| 222 | p->should_be_zero_1 = 1;
|
|---|
| 223 | } else {
|
|---|
| 224 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 225 | p->should_be_zero_0 = 0;
|
|---|
| 226 | p->should_be_zero_1 = 0;
|
|---|
| 227 | p->domain = 0;
|
|---|
| 228 | p->ns = 0;
|
|---|
| 229 | }
|
|---|
| 230 | pt_coherence(p);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 | /** Sets flags of level 1 page table entry.
|
|---|
| 235 | *
|
|---|
| 236 | * We use same access rights for the whole page. When page
|
|---|
| 237 | * is not preset we store 1 in acess_rigts_3 so that at least
|
|---|
| 238 | * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
|
|---|
| 239 | *
|
|---|
| 240 | * @param pt Level 1 page table.
|
|---|
| 241 | * @param i Index of the entry to be changed.
|
|---|
| 242 | * @param flags New flags.
|
|---|
| 243 | *
|
|---|
| 244 | */
|
|---|
| 245 | NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
|
|---|
| 246 | {
|
|---|
| 247 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 248 |
|
|---|
| 249 | if (flags & PAGE_NOT_PRESENT) {
|
|---|
| 250 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
|---|
| 251 | } else {
|
|---|
| 252 | if (flags & PAGE_EXEC)
|
|---|
| 253 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 254 | else
|
|---|
| 255 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE_NX;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | if (flags & PAGE_CACHEABLE) {
|
|---|
| 259 | /*
|
|---|
| 260 | * Write-through, no write-allocate memory, see ch. B3.8.2
|
|---|
| 261 | * (p. B3-1358) of ARM Architecture reference manual.
|
|---|
| 262 | * Make sure the memory type is correct, and in sync with:
|
|---|
| 263 | * init_boot_pt (boot/arch/arm32/src/mm.c)
|
|---|
| 264 | * init_ptl0_section (boot/arch/arm32/src/mm.c)
|
|---|
| 265 | * set_ptl0_addr (kernel/arch/arm32/include/arch/mm/page.h)
|
|---|
| 266 | */
|
|---|
| 267 | p->tex = 5;
|
|---|
| 268 | p->cacheable = 0;
|
|---|
| 269 | p->bufferable = 1;
|
|---|
| 270 | } else {
|
|---|
| 271 | /*
|
|---|
| 272 | * Shareable device memory, see ch. B3.8.2 (p. B3-1358) of
|
|---|
| 273 | * ARM Architecture reference manual.
|
|---|
| 274 | */
|
|---|
| 275 | p->tex = 0;
|
|---|
| 276 | p->cacheable = 0;
|
|---|
| 277 | p->bufferable = 1;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /* Shareable is ignored for devices (non-cacheable),
|
|---|
| 281 | * turn it off for normal memory. */
|
|---|
| 282 | p->shareable = 0;
|
|---|
| 283 |
|
|---|
| 284 | p->non_global = !(flags & PAGE_GLOBAL);
|
|---|
| 285 |
|
|---|
| 286 | /* default access permission: kernel only*/
|
|---|
| 287 | p->access_permission_0 = PTE_AP0_USER_NO_KERNEL_FULL;
|
|---|
| 288 |
|
|---|
| 289 | if (flags & PAGE_USER) {
|
|---|
| 290 | p->access_permission_0 = PTE_AP0_USER_FULL_KERNEL_FULL;
|
|---|
| 291 | // TODO Fix kernel to use PAGE_WRITE flag properly and
|
|---|
| 292 | // apply this for kernel pages as well.
|
|---|
| 293 | if (!(flags & PAGE_WRITE))
|
|---|
| 294 | p->access_permission_1 = PTE_AP1_RO;
|
|---|
| 295 | }
|
|---|
| 296 | pt_coherence(p);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
|---|
| 300 | {
|
|---|
| 301 | pte_level0_t *p = &pt[i].l0;
|
|---|
| 302 |
|
|---|
| 303 | p->should_be_zero_0 = 0;
|
|---|
| 304 | p->should_be_zero_1 = 0;
|
|---|
| 305 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
|---|
| 306 | pt_coherence(p);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
|---|
| 310 | {
|
|---|
| 311 | pte_level1_t *p = &pt[i].l1;
|
|---|
| 312 |
|
|---|
| 313 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
|---|
| 314 | pt_coherence(p);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 | extern void page_arch_init(void);
|
|---|
| 319 |
|
|---|
| 320 | #endif /* __ASM__ */
|
|---|
| 321 |
|
|---|
| 322 | #endif
|
|---|
| 323 |
|
|---|
| 324 | /** @}
|
|---|
| 325 | */
|
|---|