[914e063] | 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 |
|
---|
[0747468] | 40 | #ifndef KERN_arm32_PAGE_H_
|
---|
| 41 | #error "Do not include arch specific page.h directly use generic page.h instead"
|
---|
| 42 | #endif
|
---|
[914e063] | 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 |
|
---|
| 123 | /** Sets the address of level 0 page table.
|
---|
| 124 | *
|
---|
| 125 | * @param pt Pointer to the page table to set.
|
---|
| 126 | *
|
---|
| 127 | */
|
---|
| 128 | NO_TRACE static inline void set_ptl0_addr(pte_t *pt)
|
---|
| 129 | {
|
---|
| 130 | asm volatile (
|
---|
| 131 | "mcr p15, 0, %[pt], c2, c0, 0\n"
|
---|
| 132 | :: [pt] "r" (pt)
|
---|
| 133 | );
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | /** Returns level 0 page table entry flags.
|
---|
| 138 | *
|
---|
| 139 | * @param pt Level 0 page table.
|
---|
| 140 | * @param i Index of the entry to return.
|
---|
| 141 | *
|
---|
| 142 | */
|
---|
| 143 | NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
---|
| 144 | {
|
---|
| 145 | pte_level0_t *p = &pt[i].l0;
|
---|
| 146 | int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
---|
| 147 |
|
---|
| 148 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
---|
| 149 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
---|
| 150 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /** Returns level 1 page table entry flags.
|
---|
| 154 | *
|
---|
| 155 | * @param pt Level 1 page table.
|
---|
| 156 | * @param i Index of the entry to return.
|
---|
| 157 | *
|
---|
| 158 | */
|
---|
| 159 | NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
---|
| 160 | {
|
---|
| 161 | pte_level1_t *p = &pt[i].l1;
|
---|
| 162 |
|
---|
| 163 | int dt = p->descriptor_type;
|
---|
| 164 | int ap = p->access_permission_0;
|
---|
| 165 |
|
---|
| 166 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
---|
| 167 | ((ap == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 168 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 169 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
| 170 | ((ap != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT) |
|
---|
| 171 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
| 172 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
| 173 | (1 << PAGE_EXEC_SHIFT) |
|
---|
| 174 | (p->bufferable << PAGE_CACHEABLE);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Sets flags of level 0 page table entry.
|
---|
| 178 | *
|
---|
| 179 | * @param pt level 0 page table
|
---|
| 180 | * @param i index of the entry to be changed
|
---|
| 181 | * @param flags new flags
|
---|
| 182 | *
|
---|
| 183 | */
|
---|
| 184 | NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
---|
| 185 | {
|
---|
| 186 | pte_level0_t *p = &pt[i].l0;
|
---|
| 187 |
|
---|
| 188 | if (flags & PAGE_NOT_PRESENT) {
|
---|
| 189 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
---|
| 190 | /*
|
---|
| 191 | * Ensures that the entry will be recognized as valid when
|
---|
| 192 | * PTE_VALID_ARCH applied.
|
---|
| 193 | */
|
---|
| 194 | p->should_be_zero = 1;
|
---|
| 195 | } else {
|
---|
| 196 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
| 197 | p->should_be_zero = 0;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | /** Sets flags of level 1 page table entry.
|
---|
| 203 | *
|
---|
| 204 | * We use same access rights for the whole page. When page
|
---|
| 205 | * is not preset we store 1 in acess_rigts_3 so that at least
|
---|
| 206 | * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
|
---|
| 207 | *
|
---|
| 208 | * @param pt Level 1 page table.
|
---|
| 209 | * @param i Index of the entry to be changed.
|
---|
| 210 | * @param flags New flags.
|
---|
| 211 | *
|
---|
| 212 | */
|
---|
| 213 | NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
|
---|
| 214 | {
|
---|
| 215 | pte_level1_t *p = &pt[i].l1;
|
---|
| 216 |
|
---|
[97c7682] | 217 | if (flags & PAGE_NOT_PRESENT)
|
---|
[914e063] | 218 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
---|
[97c7682] | 219 | else
|
---|
[914e063] | 220 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
| 221 |
|
---|
| 222 | p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
|
---|
| 223 |
|
---|
| 224 | /* default access permission */
|
---|
| 225 | p->access_permission_0 = p->access_permission_1 =
|
---|
| 226 | p->access_permission_2 = p->access_permission_3 =
|
---|
| 227 | PTE_AP_USER_NO_KERNEL_RW;
|
---|
| 228 |
|
---|
| 229 | if (flags & PAGE_USER) {
|
---|
| 230 | if (flags & PAGE_READ) {
|
---|
| 231 | p->access_permission_0 = p->access_permission_1 =
|
---|
| 232 | p->access_permission_2 = p->access_permission_3 =
|
---|
| 233 | PTE_AP_USER_RO_KERNEL_RW;
|
---|
| 234 | }
|
---|
| 235 | if (flags & PAGE_WRITE) {
|
---|
| 236 | p->access_permission_0 = p->access_permission_1 =
|
---|
| 237 | p->access_permission_2 = p->access_permission_3 =
|
---|
| 238 | PTE_AP_USER_RW_KERNEL_RW;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[804d9b6] | 243 | NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
---|
| 244 | {
|
---|
| 245 | pte_level0_t *p = &pt[i].l0;
|
---|
| 246 |
|
---|
| 247 | p->should_be_zero = 0;
|
---|
| 248 | write_barrier();
|
---|
| 249 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
---|
| 254 | {
|
---|
| 255 | pte_level1_t *p = &pt[i].l1;
|
---|
| 256 |
|
---|
| 257 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[914e063] | 260 |
|
---|
| 261 | extern void page_arch_init(void);
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | #endif /* __ASM__ */
|
---|
| 265 |
|
---|
| 266 | #endif
|
---|
| 267 |
|
---|
| 268 | /** @}
|
---|
| 269 | */
|
---|