[914e063] | 1 | /*
|
---|
[d4a829e] | 2 | * Copyright (c) 2007 Pavel Jancik, Michal Kebrt
|
---|
[914e063] | 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 |
|
---|
[c5429fe] | 30 | /** @addtogroup kernel_arm32_mm
|
---|
[914e063] | 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 |
|
---|
[7328ff4] | 40 | #include <arch/cache.h>
|
---|
| 41 |
|
---|
[0747468] | 42 | #ifndef KERN_arm32_PAGE_H_
|
---|
| 43 | #error "Do not include arch specific page.h directly use generic page.h instead"
|
---|
| 44 | #endif
|
---|
[914e063] | 45 |
|
---|
| 46 | /* Macros for querying the last-level PTE entries. */
|
---|
| 47 | #define PTE_VALID_ARCH(pte) \
|
---|
[dc05a9a] | 48 | (((pte_t *) (pte))->l0.should_be_zero != 0 || PTE_PRESENT_ARCH(pte))
|
---|
[914e063] | 49 | #define PTE_PRESENT_ARCH(pte) \
|
---|
| 50 | (((pte_t *) (pte))->l0.descriptor_type != 0)
|
---|
| 51 | #define PTE_GET_FRAME_ARCH(pte) \
|
---|
[b5851913] | 52 | (((uintptr_t) ((pte_t *) (pte))->l1.frame_base_addr) << FRAME_WIDTH)
|
---|
[914e063] | 53 | #define PTE_WRITABLE_ARCH(pte) \
|
---|
| 54 | (((pte_t *) (pte))->l1.access_permission_0 == PTE_AP_USER_RW_KERNEL_RW)
|
---|
| 55 | #define PTE_EXECUTABLE_ARCH(pte) \
|
---|
| 56 | 1
|
---|
| 57 |
|
---|
[53ad43c] | 58 | #ifndef __ASSEMBLER__
|
---|
[914e063] | 59 |
|
---|
| 60 | /** Level 0 page table entry. */
|
---|
| 61 | typedef struct {
|
---|
| 62 | /* 0b01 for coarse tables, see below for details */
|
---|
| 63 | unsigned descriptor_type : 2;
|
---|
| 64 | unsigned impl_specific : 3;
|
---|
| 65 | unsigned domain : 4;
|
---|
| 66 | unsigned should_be_zero : 1;
|
---|
| 67 |
|
---|
[7c3fb9b] | 68 | /*
|
---|
| 69 | * Pointer to the coarse 2nd level page table (holding entries for small
|
---|
[914e063] | 70 | * (4KB) or large (64KB) pages. ARM also supports fine 2nd level page
|
---|
| 71 | * tables that may hold even tiny pages (1KB) but they are bigger (4KB
|
---|
| 72 | * per table in comparison with 1KB per the coarse table)
|
---|
| 73 | */
|
---|
| 74 | unsigned coarse_table_addr : 22;
|
---|
| 75 | } ATTRIBUTE_PACKED pte_level0_t;
|
---|
| 76 |
|
---|
| 77 | /** Level 1 page table entry (small (4KB) pages used). */
|
---|
| 78 | typedef struct {
|
---|
| 79 |
|
---|
| 80 | /* 0b10 for small pages */
|
---|
| 81 | unsigned descriptor_type : 2;
|
---|
| 82 | unsigned bufferable : 1;
|
---|
| 83 | unsigned cacheable : 1;
|
---|
| 84 |
|
---|
[7c3fb9b] | 85 | /*
|
---|
| 86 | * access permissions for each of 4 subparts of a page
|
---|
| 87 | * (for each 1KB when small pages used
|
---|
| 88 | */
|
---|
[914e063] | 89 | unsigned access_permission_0 : 2;
|
---|
| 90 | unsigned access_permission_1 : 2;
|
---|
| 91 | unsigned access_permission_2 : 2;
|
---|
| 92 | unsigned access_permission_3 : 2;
|
---|
| 93 | unsigned frame_base_addr : 20;
|
---|
| 94 | } ATTRIBUTE_PACKED pte_level1_t;
|
---|
| 95 |
|
---|
| 96 | typedef union {
|
---|
| 97 | pte_level0_t l0;
|
---|
| 98 | pte_level1_t l1;
|
---|
| 99 | } pte_t;
|
---|
| 100 |
|
---|
| 101 | /* Level 1 page tables access permissions */
|
---|
| 102 |
|
---|
| 103 | /** User mode: no access, privileged mode: no access. */
|
---|
| 104 | #define PTE_AP_USER_NO_KERNEL_NO 0
|
---|
| 105 |
|
---|
| 106 | /** User mode: no access, privileged mode: read/write. */
|
---|
| 107 | #define PTE_AP_USER_NO_KERNEL_RW 1
|
---|
| 108 |
|
---|
| 109 | /** User mode: read only, privileged mode: read/write. */
|
---|
| 110 | #define PTE_AP_USER_RO_KERNEL_RW 2
|
---|
| 111 |
|
---|
| 112 | /** User mode: read/write, privileged mode: read/write. */
|
---|
| 113 | #define PTE_AP_USER_RW_KERNEL_RW 3
|
---|
| 114 |
|
---|
| 115 | /* pte_level0_t and pte_level1_t descriptor_type flags */
|
---|
| 116 |
|
---|
| 117 | /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
|
---|
| 118 | #define PTE_DESCRIPTOR_NOT_PRESENT 0
|
---|
| 119 |
|
---|
| 120 | /** pte_level0_t coarse page table flag (used in descriptor_type). */
|
---|
| 121 | #define PTE_DESCRIPTOR_COARSE_TABLE 1
|
---|
| 122 |
|
---|
| 123 | /** pte_level1_t small page table flag (used in descriptor type). */
|
---|
| 124 | #define PTE_DESCRIPTOR_SMALL_PAGE 2
|
---|
| 125 |
|
---|
[0c40fd5] | 126 | #define pt_coherence_m(pt, count) \
|
---|
| 127 | do { \
|
---|
| 128 | for (unsigned i = 0; i < count; ++i) \
|
---|
[d5610b9] | 129 | dcache_clean_mva_pou((uintptr_t)(pt + i)); \
|
---|
[0c40fd5] | 130 | read_barrier(); \
|
---|
| 131 | } while (0)
|
---|
[914e063] | 132 |
|
---|
| 133 | /** Returns level 0 page table entry flags.
|
---|
| 134 | *
|
---|
| 135 | * @param pt Level 0 page table.
|
---|
| 136 | * @param i Index of the entry to return.
|
---|
| 137 | *
|
---|
| 138 | */
|
---|
[8df5f20] | 139 | _NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
---|
[914e063] | 140 | {
|
---|
| 141 | pte_level0_t *p = &pt[i].l0;
|
---|
| 142 | int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
---|
[a35b458] | 143 |
|
---|
[914e063] | 144 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
---|
| 145 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
---|
| 146 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** Returns level 1 page table entry flags.
|
---|
| 150 | *
|
---|
| 151 | * @param pt Level 1 page table.
|
---|
| 152 | * @param i Index of the entry to return.
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
[8df5f20] | 155 | _NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
---|
[914e063] | 156 | {
|
---|
| 157 | pte_level1_t *p = &pt[i].l1;
|
---|
[a35b458] | 158 |
|
---|
[914e063] | 159 | int dt = p->descriptor_type;
|
---|
| 160 | int ap = p->access_permission_0;
|
---|
[a35b458] | 161 |
|
---|
[914e063] | 162 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
---|
| 163 | ((ap == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 164 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 165 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
| 166 | ((ap != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT) |
|
---|
| 167 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 168 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
| 169 | (1 << PAGE_EXEC_SHIFT) |
|
---|
| 170 | (p->bufferable << PAGE_CACHEABLE);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** Sets flags of level 0 page table entry.
|
---|
| 174 | *
|
---|
| 175 | * @param pt level 0 page table
|
---|
| 176 | * @param i index of the entry to be changed
|
---|
| 177 | * @param flags new flags
|
---|
| 178 | *
|
---|
| 179 | */
|
---|
[8df5f20] | 180 | _NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
---|
[914e063] | 181 | {
|
---|
| 182 | pte_level0_t *p = &pt[i].l0;
|
---|
[a35b458] | 183 |
|
---|
[914e063] | 184 | if (flags & PAGE_NOT_PRESENT) {
|
---|
| 185 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
---|
| 186 | /*
|
---|
| 187 | * Ensures that the entry will be recognized as valid when
|
---|
| 188 | * PTE_VALID_ARCH applied.
|
---|
| 189 | */
|
---|
| 190 | p->should_be_zero = 1;
|
---|
| 191 | } else {
|
---|
| 192 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
| 193 | p->should_be_zero = 0;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /** Sets flags of level 1 page table entry.
|
---|
| 198 | *
|
---|
| 199 | * We use same access rights for the whole page. When page
|
---|
| 200 | * is not preset we store 1 in acess_rigts_3 so that at least
|
---|
| 201 | * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
|
---|
| 202 | *
|
---|
| 203 | * @param pt Level 1 page table.
|
---|
| 204 | * @param i Index of the entry to be changed.
|
---|
| 205 | * @param flags New flags.
|
---|
| 206 | *
|
---|
| 207 | */
|
---|
[8df5f20] | 208 | _NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
|
---|
[914e063] | 209 | {
|
---|
| 210 | pte_level1_t *p = &pt[i].l1;
|
---|
[a35b458] | 211 |
|
---|
[97c7682] | 212 | if (flags & PAGE_NOT_PRESENT)
|
---|
[914e063] | 213 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
---|
[97c7682] | 214 | else
|
---|
[914e063] | 215 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
[a35b458] | 216 |
|
---|
[914e063] | 217 | p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
|
---|
[a35b458] | 218 |
|
---|
[914e063] | 219 | /* default access permission */
|
---|
[e006ba5] | 220 | p->access_permission_0 = p->access_permission_1 =
|
---|
[914e063] | 221 | p->access_permission_2 = p->access_permission_3 =
|
---|
| 222 | PTE_AP_USER_NO_KERNEL_RW;
|
---|
[a35b458] | 223 |
|
---|
[914e063] | 224 | if (flags & PAGE_USER) {
|
---|
| 225 | if (flags & PAGE_READ) {
|
---|
[e006ba5] | 226 | p->access_permission_0 = p->access_permission_1 =
|
---|
| 227 | p->access_permission_2 = p->access_permission_3 =
|
---|
[914e063] | 228 | PTE_AP_USER_RO_KERNEL_RW;
|
---|
| 229 | }
|
---|
| 230 | if (flags & PAGE_WRITE) {
|
---|
[e006ba5] | 231 | p->access_permission_0 = p->access_permission_1 =
|
---|
| 232 | p->access_permission_2 = p->access_permission_3 =
|
---|
| 233 | PTE_AP_USER_RW_KERNEL_RW;
|
---|
[914e063] | 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[8df5f20] | 238 | _NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
---|
[804d9b6] | 239 | {
|
---|
| 240 | pte_level0_t *p = &pt[i].l0;
|
---|
| 241 |
|
---|
| 242 | p->should_be_zero = 0;
|
---|
| 243 | write_barrier();
|
---|
| 244 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[8df5f20] | 247 | _NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
---|
[804d9b6] | 248 | {
|
---|
| 249 | pte_level1_t *p = &pt[i].l1;
|
---|
| 250 |
|
---|
| 251 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[914e063] | 254 | extern void page_arch_init(void);
|
---|
| 255 |
|
---|
[53ad43c] | 256 | #endif /* __ASSEMBLER__ */
|
---|
[914e063] | 257 |
|
---|
| 258 | #endif
|
---|
| 259 |
|
---|
| 260 | /** @}
|
---|
| 261 | */
|
---|