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 |
|
---|
40 | #ifndef KERN_arm32_PAGE_H_
|
---|
41 | #error "Do not include arch specific page.h directly use generic page.h instead"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <arch/cp15.h>
|
---|
45 |
|
---|
46 | /* Macros for querying the last-level PTE entries. */
|
---|
47 | #define PTE_VALID_ARCH(pte) \
|
---|
48 | (*((uint32_t *) (pte)) != 0)
|
---|
49 | #define PTE_PRESENT_ARCH(pte) \
|
---|
50 | (((pte_t *) (pte))->l0.descriptor_type != 0)
|
---|
51 | #define PTE_GET_FRAME_ARCH(pte) \
|
---|
52 | (((pte_t *) (pte))->l1.frame_base_addr << FRAME_WIDTH)
|
---|
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 |
|
---|
58 | #ifndef __ASM__
|
---|
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 |
|
---|
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 */
|
---|
80 | unsigned descriptor_type : 2;
|
---|
81 | unsigned bufferable : 1;
|
---|
82 | unsigned cacheable : 1;
|
---|
83 |
|
---|
84 | /* access permissions for each of 4 subparts of a page
|
---|
85 | * (for each 1KB when small pages used */
|
---|
86 | unsigned access_permission_0 : 2;
|
---|
87 | unsigned access_permission_1 : 2;
|
---|
88 | unsigned access_permission_2 : 2;
|
---|
89 | unsigned access_permission_3 : 2;
|
---|
90 | unsigned frame_base_addr : 20;
|
---|
91 | } ATTRIBUTE_PACKED pte_level1_t;
|
---|
92 |
|
---|
93 | typedef union {
|
---|
94 | pte_level0_t l0;
|
---|
95 | pte_level1_t l1;
|
---|
96 | } pte_t;
|
---|
97 |
|
---|
98 | /* Level 1 page tables access permissions */
|
---|
99 |
|
---|
100 | /** User mode: no access, privileged mode: no access. */
|
---|
101 | #define PTE_AP_USER_NO_KERNEL_NO 0
|
---|
102 |
|
---|
103 | /** User mode: no access, privileged mode: read/write. */
|
---|
104 | #define PTE_AP_USER_NO_KERNEL_RW 1
|
---|
105 |
|
---|
106 | /** User mode: read only, privileged mode: read/write. */
|
---|
107 | #define PTE_AP_USER_RO_KERNEL_RW 2
|
---|
108 |
|
---|
109 | /** User mode: read/write, privileged mode: read/write. */
|
---|
110 | #define PTE_AP_USER_RW_KERNEL_RW 3
|
---|
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 |
|
---|
125 | /** Sets the address of level 0 page table.
|
---|
126 | *
|
---|
127 | * @param pt Pointer to the page table to set.
|
---|
128 | *
|
---|
129 | */
|
---|
130 | NO_TRACE static inline void set_ptl0_addr(pte_t *pt)
|
---|
131 | {
|
---|
132 | TTBR0_write((uint32_t)pt);
|
---|
133 | }
|
---|
134 |
|
---|
135 | NO_TRACE static inline void set_ptl1_addr(pte_t *pt, size_t i, uintptr_t address)
|
---|
136 | {
|
---|
137 | pt[i].l0.coarse_table_addr = address >> 10;
|
---|
138 | }
|
---|
139 |
|
---|
140 | NO_TRACE static inline void set_ptl3_addr(pte_t *pt, size_t i, uintptr_t address)
|
---|
141 | {
|
---|
142 | pt[i].l1.frame_base_addr = address >> 12;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /** Returns level 0 page table entry flags.
|
---|
147 | *
|
---|
148 | * @param pt Level 0 page table.
|
---|
149 | * @param i Index of the entry to return.
|
---|
150 | *
|
---|
151 | */
|
---|
152 | NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
|
---|
153 | {
|
---|
154 | pte_level0_t *p = &pt[i].l0;
|
---|
155 | int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
|
---|
156 |
|
---|
157 | return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
|
---|
158 | (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
|
---|
159 | (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Returns level 1 page table entry flags.
|
---|
163 | *
|
---|
164 | * @param pt Level 1 page table.
|
---|
165 | * @param i Index of the entry to return.
|
---|
166 | *
|
---|
167 | */
|
---|
168 | NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
|
---|
169 | {
|
---|
170 | pte_level1_t *p = &pt[i].l1;
|
---|
171 |
|
---|
172 | int dt = p->descriptor_type;
|
---|
173 | int ap = p->access_permission_0;
|
---|
174 |
|
---|
175 | return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
|
---|
176 | ((ap == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
177 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
178 | ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
179 | ((ap != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT) |
|
---|
180 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT) |
|
---|
181 | ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT) |
|
---|
182 | (1 << PAGE_EXEC_SHIFT) |
|
---|
183 | (p->bufferable << PAGE_CACHEABLE);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** Sets flags of level 0 page table entry.
|
---|
187 | *
|
---|
188 | * @param pt level 0 page table
|
---|
189 | * @param i index of the entry to be changed
|
---|
190 | * @param flags new flags
|
---|
191 | *
|
---|
192 | */
|
---|
193 | NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
|
---|
194 | {
|
---|
195 | pte_level0_t *p = &pt[i].l0;
|
---|
196 |
|
---|
197 | if (flags & PAGE_NOT_PRESENT) {
|
---|
198 | p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
|
---|
199 | /*
|
---|
200 | * Ensures that the entry will be recognized as valid when
|
---|
201 | * PTE_VALID_ARCH applied.
|
---|
202 | */
|
---|
203 | p->should_be_zero = 1;
|
---|
204 | } else {
|
---|
205 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
206 | p->should_be_zero = 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 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
230 |
|
---|
231 | p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
|
---|
232 |
|
---|
233 | /* default access permission */
|
---|
234 | p->access_permission_0 = p->access_permission_1 =
|
---|
235 | p->access_permission_2 = p->access_permission_3 =
|
---|
236 | PTE_AP_USER_NO_KERNEL_RW;
|
---|
237 |
|
---|
238 | if (flags & PAGE_USER) {
|
---|
239 | if (flags & PAGE_READ) {
|
---|
240 | p->access_permission_0 = p->access_permission_1 =
|
---|
241 | p->access_permission_2 = p->access_permission_3 =
|
---|
242 | PTE_AP_USER_RO_KERNEL_RW;
|
---|
243 | }
|
---|
244 | if (flags & PAGE_WRITE) {
|
---|
245 | p->access_permission_0 = p->access_permission_1 =
|
---|
246 | p->access_permission_2 = p->access_permission_3 =
|
---|
247 | PTE_AP_USER_RW_KERNEL_RW;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
|
---|
253 | {
|
---|
254 | pte_level0_t *p = &pt[i].l0;
|
---|
255 |
|
---|
256 | p->should_be_zero = 0;
|
---|
257 | write_barrier();
|
---|
258 | p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
|
---|
263 | {
|
---|
264 | pte_level1_t *p = &pt[i].l1;
|
---|
265 |
|
---|
266 | p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | extern void page_arch_init(void);
|
---|
271 |
|
---|
272 |
|
---|
273 | #endif /* __ASM__ */
|
---|
274 |
|
---|
275 | #endif
|
---|
276 |
|
---|
277 | /** @}
|
---|
278 | */
|
---|