Rev 2411 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2128 | jermar | 1 | /* |
2238 | kebrt | 2 | * Copyright (c) 2007 Pavel Jancik, Michal Kebrt |
2128 | jermar | 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 |
||
2410 | stepan | 33 | * @brief Paging related declarations. |
2128 | jermar | 34 | */ |
35 | |||
36 | #ifndef KERN_arm32_PAGE_H_ |
||
37 | #define KERN_arm32_PAGE_H_ |
||
38 | |||
39 | #include <arch/mm/frame.h> |
||
2182 | jancik | 40 | #include <mm/mm.h> |
41 | #include <arch/exception.h> |
||
2128 | jermar | 42 | |
2182 | jancik | 43 | |
2128 | jermar | 44 | #define PAGE_WIDTH FRAME_WIDTH |
45 | #define PAGE_SIZE FRAME_SIZE |
||
46 | |||
47 | #define PAGE_COLOR_BITS 0 /* dummy */ |
||
48 | |||
49 | #ifndef __ASM__ |
||
50 | # define KA2PA(x) (((uintptr_t) (x)) - 0x80000000) |
||
51 | # define PA2KA(x) (((uintptr_t) (x)) + 0x80000000) |
||
52 | #else |
||
53 | # define KA2PA(x) ((x) - 0x80000000) |
||
54 | # define PA2KA(x) ((x) + 0x80000000) |
||
55 | #endif |
||
56 | |||
57 | #ifdef KERNEL |
||
58 | |||
2276 | jancik | 59 | #define PTL0_ENTRIES_ARCH (2<<12) // 4096 |
60 | #define PTL1_ENTRIES_ARCH 0 |
||
61 | #define PTL2_ENTRIES_ARCH 0 |
||
2417 | kebrt | 62 | |
2241 | kebrt | 63 | /* coarse page tables used (256*4 = 1KB per page) */ |
2276 | jancik | 64 | #define PTL3_ENTRIES_ARCH (2<<8) // 256 |
2128 | jermar | 65 | |
2276 | jancik | 66 | #define PTL0_SIZE_ARCH FOUR_FRAMES |
67 | #define PTL1_SIZE_ARCH 0 |
||
68 | #define PTL2_SIZE_ARCH 0 |
||
69 | #define PTL3_SIZE_ARCH ONE_FRAME |
||
2242 | kebrt | 70 | |
2276 | jancik | 71 | #define PTL0_INDEX_ARCH(vaddr) (((vaddr) >> 20) & 0xfff) |
72 | #define PTL1_INDEX_ARCH(vaddr) 0 |
||
73 | #define PTL2_INDEX_ARCH(vaddr) 0 |
||
74 | #define PTL3_INDEX_ARCH(vaddr) (((vaddr) >> 12) & 0x0ff) |
||
2128 | jermar | 75 | |
2276 | jancik | 76 | #define GET_PTL1_ADDRESS_ARCH(ptl0, i) ((pte_t *)( (((pte_level0_t*)(ptl0))[(i)]).coarse_table_addr << 10 )) |
77 | #define GET_PTL2_ADDRESS_ARCH(ptl1, i) (ptl1) |
||
78 | #define GET_PTL3_ADDRESS_ARCH(ptl2, i) (ptl2) |
||
79 | #define GET_FRAME_ADDRESS_ARCH(ptl3, i) ((uintptr_t)( (((pte_level1_t*)(ptl3))[(i)]).frame_base_addr << 12 )) |
||
2128 | jermar | 80 | |
2276 | jancik | 81 | #define SET_PTL0_ADDRESS_ARCH(ptl0) (set_ptl0_addr((pte_level0_t *)(ptl0))) |
82 | #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) (((pte_level0_t *)(ptl0))[(i)].coarse_table_addr = (a)>>10) |
||
2147 | jancik | 83 | #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a) |
84 | #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a) |
||
2276 | jancik | 85 | #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) (((pte_level1_t *)(ptl3))[(i)].frame_base_addr = (a)>>12) |
2128 | jermar | 86 | |
2276 | jancik | 87 | #define GET_PTL1_FLAGS_ARCH(ptl0, i) get_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i)) |
88 | #define GET_PTL2_FLAGS_ARCH(ptl1, i) PAGE_PRESENT |
||
89 | #define GET_PTL3_FLAGS_ARCH(ptl2, i) PAGE_PRESENT |
||
90 | #define GET_FRAME_FLAGS_ARCH(ptl3, i) get_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i)) |
||
2128 | jermar | 91 | |
2276 | jancik | 92 | #define SET_PTL1_FLAGS_ARCH(ptl0, i, x) set_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i), (x)) |
2147 | jancik | 93 | #define SET_PTL2_FLAGS_ARCH(ptl1, i, x) |
94 | #define SET_PTL3_FLAGS_ARCH(ptl2, i, x) |
||
2276 | jancik | 95 | #define SET_FRAME_FLAGS_ARCH(ptl3, i, x) set_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i), (x)) |
2128 | jermar | 96 | |
2276 | jancik | 97 | #define PTE_VALID_ARCH(pte) (*((uint32_t *) (pte)) != 0) |
98 | #define PTE_PRESENT_ARCH(pte) ( ((pte_level0_t *)(pte))->descriptor_type != 0 ) |
||
2241 | kebrt | 99 | |
100 | /* pte should point into ptl3 */ |
||
2276 | jancik | 101 | #define PTE_GET_FRAME_ARCH(pte) ( ((pte_level1_t *)(pte))->frame_base_addr << FRAME_WIDTH) |
2417 | kebrt | 102 | |
2241 | kebrt | 103 | /* pte should point into ptl3 */ |
2276 | jancik | 104 | #define PTE_WRITABLE_ARCH(pte) ( ((pte_level1_t *)(pte))->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW ) |
2417 | kebrt | 105 | |
2276 | jancik | 106 | #define PTE_EXECUTABLE_ARCH(pte) 1 |
2241 | kebrt | 107 | |
2329 | kebrt | 108 | |
2128 | jermar | 109 | #ifndef __ASM__ |
110 | |||
2276 | jancik | 111 | /** Level 0 page table entry. */ |
112 | typedef struct { |
||
2411 | stepan | 113 | |
2276 | jancik | 114 | /* 01b for coarse tables, see below for details */ |
115 | unsigned descriptor_type : 2; |
||
116 | unsigned impl_specific : 3; |
||
117 | unsigned domain : 4; |
||
118 | unsigned should_be_zero : 1; |
||
2411 | stepan | 119 | |
2276 | jancik | 120 | /* Pointer to the coarse 2nd level page table (holding entries for small (4KB) |
121 | * or large (64KB) pages. ARM also supports fine 2nd level page tables that |
||
122 | * may hold even tiny pages (1KB) but they are bigger (4KB per table in comparison |
||
123 | * with 1KB per the coarse table) |
||
124 | */ |
||
125 | unsigned coarse_table_addr : 22; |
||
2417 | kebrt | 126 | } ATTRIBUTE_PACKED pte_level0_t; |
2276 | jancik | 127 | |
2329 | kebrt | 128 | |
129 | /** Level 1 page table entry (small (4KB) pages used). */ |
||
2276 | jancik | 130 | typedef struct { |
2411 | stepan | 131 | |
2276 | jancik | 132 | /* 0b10 for small pages */ |
133 | unsigned descriptor_type : 2; |
||
134 | unsigned bufferable : 1; |
||
135 | unsigned cacheable : 1; |
||
2411 | stepan | 136 | |
2276 | jancik | 137 | /* access permissions for each of 4 subparts of a page |
138 | * (for each 1KB when small pages used */ |
||
139 | unsigned access_permission_0 : 2; |
||
140 | unsigned access_permission_1 : 2; |
||
141 | unsigned access_permission_2 : 2; |
||
142 | unsigned access_permission_3 : 2; |
||
143 | unsigned frame_base_addr : 20; |
||
2417 | kebrt | 144 | } ATTRIBUTE_PACKED pte_level1_t; |
2276 | jancik | 145 | |
146 | |||
147 | /* Level 1 page tables access permissions */ |
||
148 | |||
2329 | kebrt | 149 | /** User mode: no access, privileged mode: no access. */ |
2276 | jancik | 150 | #define PTE_AP_USER_NO_KERNEL_NO 0 |
2411 | stepan | 151 | |
2329 | kebrt | 152 | /** User mode: no access, privileged mode: read/write. */ |
2276 | jancik | 153 | #define PTE_AP_USER_NO_KERNEL_RW 1 |
2411 | stepan | 154 | |
2329 | kebrt | 155 | /** User mode: read only, privileged mode: read/write. */ |
2276 | jancik | 156 | #define PTE_AP_USER_RO_KERNEL_RW 2 |
2411 | stepan | 157 | |
2329 | kebrt | 158 | /** User mode: read/write, privileged mode: read/write. */ |
2276 | jancik | 159 | #define PTE_AP_USER_RW_KERNEL_RW 3 |
160 | |||
161 | |||
162 | /* pte_level0_t and pte_level1_t descriptor_type flags */ |
||
163 | |||
2329 | kebrt | 164 | /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */ |
2276 | jancik | 165 | #define PTE_DESCRIPTOR_NOT_PRESENT 0 |
2411 | stepan | 166 | |
2329 | kebrt | 167 | /** pte_level0_t coarse page table flag (used in descriptor_type). */ |
2276 | jancik | 168 | #define PTE_DESCRIPTOR_COARSE_TABLE 1 |
2411 | stepan | 169 | |
2329 | kebrt | 170 | /** pte_level1_t small page table flag (used in descriptor type). */ |
2276 | jancik | 171 | #define PTE_DESCRIPTOR_SMALL_PAGE 2 |
172 | |||
173 | |||
2329 | kebrt | 174 | /** Sets the address of level 0 page table. |
2241 | kebrt | 175 | * |
2329 | kebrt | 176 | * @param pt Pointer to the page table to set. |
2182 | jancik | 177 | */ |
2238 | kebrt | 178 | static inline void set_ptl0_addr( pte_level0_t* pt) |
179 | { |
||
2276 | jancik | 180 | asm volatile ( |
181 | "mcr p15, 0, %0, c2, c0, 0 \n" |
||
182 | : |
||
183 | : "r"(pt) |
||
184 | ); |
||
2182 | jancik | 185 | } |
2128 | jermar | 186 | |
2329 | kebrt | 187 | |
2263 | kebrt | 188 | /** Returns level 0 page table entry flags. |
2241 | kebrt | 189 | * |
2329 | kebrt | 190 | * @param pt Level 0 page table. |
191 | * @param i Index of the entry to return. |
||
2241 | kebrt | 192 | */ |
2147 | jancik | 193 | static inline int get_pt_level0_flags(pte_level0_t *pt, index_t i) |
2128 | jermar | 194 | { |
2238 | kebrt | 195 | pte_level0_t *p = &pt[i]; |
2147 | jancik | 196 | |
2256 | kebrt | 197 | return |
198 | ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT ) | |
||
2241 | kebrt | 199 | ( 1 << PAGE_USER_SHIFT ) | |
200 | ( 1 << PAGE_READ_SHIFT ) | |
||
201 | ( 1 << PAGE_WRITE_SHIFT ) | |
||
202 | ( 1 << PAGE_EXEC_SHIFT ) | |
||
203 | ( 1 << PAGE_CACHEABLE_SHIFT ) |
||
2256 | kebrt | 204 | ; |
2238 | kebrt | 205 | } |
2147 | jancik | 206 | |
2329 | kebrt | 207 | |
2263 | kebrt | 208 | /** Returns level 1 page table entry flags. |
2241 | kebrt | 209 | * |
2329 | kebrt | 210 | * @param pt Level 1 page table. |
211 | * @param i Index of the entry to return. |
||
2241 | kebrt | 212 | */ |
2149 | jancik | 213 | static inline int get_pt_level1_flags(pte_level1_t *pt, index_t i) |
2147 | jancik | 214 | { |
2241 | kebrt | 215 | pte_level1_t *p = &pt[i]; |
2128 | jermar | 216 | |
2256 | kebrt | 217 | return |
218 | ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) | |
||
219 | ( (p->access_permission_0 == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT ) | |
||
220 | ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT ) | |
||
221 | ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT ) | |
||
2259 | jancik | 222 | ( (p->access_permission_0 != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT ) | |
223 | ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT ) | |
||
224 | ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT ) | |
||
2147 | jancik | 225 | ( 1 << PAGE_EXEC_SHIFT ) | |
2241 | kebrt | 226 | ( p->bufferable << PAGE_CACHEABLE ) |
2256 | kebrt | 227 | ; |
2147 | jancik | 228 | } |
229 | |||
2329 | kebrt | 230 | |
2263 | kebrt | 231 | /** Sets flags of level 0 page table entry. |
2241 | kebrt | 232 | * |
2329 | kebrt | 233 | * @param pt level 0 page table |
234 | * @param i index of the entry to be changed |
||
235 | * @param flags new flags |
||
2241 | kebrt | 236 | */ |
2147 | jancik | 237 | static inline void set_pt_level0_flags(pte_level0_t *pt, index_t i, int flags) |
2128 | jermar | 238 | { |
2147 | jancik | 239 | pte_level0_t *p = &pt[i]; |
2256 | kebrt | 240 | |
2241 | kebrt | 241 | if (flags & PAGE_NOT_PRESENT) { |
2256 | kebrt | 242 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT; |
2263 | kebrt | 243 | // ensures that the entry will be recognized as valid when PTE_VALID_ARCH applied |
2259 | jancik | 244 | p->should_be_zero = 1; |
2238 | kebrt | 245 | } else { |
2256 | kebrt | 246 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE; |
2259 | jancik | 247 | p->should_be_zero = 0; |
2147 | jancik | 248 | } |
2128 | jermar | 249 | } |
250 | |||
2329 | kebrt | 251 | |
2263 | kebrt | 252 | /** Sets flags of level 1 page table entry. |
2241 | kebrt | 253 | * |
2263 | kebrt | 254 | * We use same access rights for the whole page. When page is not preset we |
255 | * store 1 in acess_rigts_3 so that at least one bit is 1 (to mark correct |
||
256 | * page entry, see #PAGE_VALID_ARCH). |
||
2241 | kebrt | 257 | * |
2329 | kebrt | 258 | * @param pt Level 1 page table. |
259 | * @param i Index of the entry to be changed. |
||
260 | * @param flags New flags. |
||
2147 | jancik | 261 | */ |
262 | static inline void set_pt_level1_flags(pte_level1_t *pt, index_t i, int flags) |
||
263 | { |
||
264 | pte_level1_t *p = &pt[i]; |
||
265 | |||
2241 | kebrt | 266 | if (flags & PAGE_NOT_PRESENT) { |
2256 | kebrt | 267 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT; |
2259 | jancik | 268 | p->access_permission_3 = 1; |
2238 | kebrt | 269 | } else { |
2256 | kebrt | 270 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE; |
2259 | jancik | 271 | p->access_permission_3 = p->access_permission_0; |
2238 | kebrt | 272 | } |
2147 | jancik | 273 | |
2238 | kebrt | 274 | p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0; |
275 | |||
2241 | kebrt | 276 | /* default access permission */ |
2238 | kebrt | 277 | p->access_permission_0 = p->access_permission_1 = |
2256 | kebrt | 278 | p->access_permission_2 = p->access_permission_3 = PTE_AP_USER_NO_KERNEL_RW; |
2238 | kebrt | 279 | |
2241 | kebrt | 280 | if (flags & PAGE_USER) { |
281 | if (flags & PAGE_READ) { |
||
2238 | kebrt | 282 | p->access_permission_0 = p->access_permission_1 = |
283 | p->access_permission_2 = p->access_permission_3 = |
||
2256 | kebrt | 284 | PTE_AP_USER_RO_KERNEL_RW; |
2238 | kebrt | 285 | } |
2241 | kebrt | 286 | if (flags & PAGE_WRITE) { |
2238 | kebrt | 287 | p->access_permission_0 = p->access_permission_1 = |
288 | p->access_permission_2 = p->access_permission_3 = |
||
2256 | kebrt | 289 | PTE_AP_USER_RW_KERNEL_RW; |
2238 | kebrt | 290 | } |
291 | } |
||
2147 | jancik | 292 | } |
293 | |||
2258 | jancik | 294 | |
2128 | jermar | 295 | extern void page_arch_init(void); |
296 | |||
2258 | jancik | 297 | |
2128 | jermar | 298 | #endif /* __ASM__ */ |
299 | |||
300 | #endif /* KERNEL */ |
||
301 | |||
302 | #endif |
||
303 | |||
304 | /** @} |
||
305 | */ |
||
2147 | jancik | 306 |