source: mainline/kernel/arch/arm32/include/arch/mm/page_armv6.h@ 070349e3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 070349e3 was 070349e3, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

armv6+: Make sure pt entries are at least at PoU.

PoU is coherence level for instructions, data, MMU, on uniprocessor systems.

  • Property mode set to 100644
File size: 8.5 KB
Line 
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#include <arch/cp15.h>
44
45/* Macros for querying the last-level PTE entries. */
46#define PTE_VALID_ARCH(pte) \
47 (*((uint32_t *) (pte)) != 0)
48#define PTE_PRESENT_ARCH(pte) \
49 (((pte_t *) (pte))->l0.descriptor_type != 0)
50#define PTE_GET_FRAME_ARCH(pte) \
51 (((pte_t *) (pte))->l1.frame_base_addr << FRAME_WIDTH)
52#define PTE_WRITABLE_ARCH(pte) \
53 (((pte_t *) (pte))->l1.access_permission_1 != PTE_AP1_RO)
54#define PTE_EXECUTABLE_ARCH(pte) \
55 (((pte_t *) (pte))->l1.descriptor_type != PTE_DESCRIPTOR_SMALL_PAGE_NX)
56
57#ifndef __ASM__
58
59/** Level 0 page table entry. */
60typedef struct {
61 /* 0b01 for coarse tables, see below for details */
62 unsigned descriptor_type : 2;
63 unsigned pxn : 1;
64 unsigned ns : 1;
65 unsigned should_be_zero_0 : 1;
66 unsigned domain : 4;
67 unsigned should_be_zero_1 : 1;
68
69 /* Pointer to the coarse 2nd level page table (holding entries for small
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). */
78typedef struct {
79
80 /* 0b10 for small pages, 0b11 for NX small pages */
81 unsigned descriptor_type : 2;
82 unsigned bufferable : 1;
83 unsigned cacheable : 1;
84 unsigned access_permission_0 : 2;
85 unsigned tex : 3;
86 unsigned access_permission_1 : 1;
87 unsigned shareable : 1;
88 unsigned non_global : 1;
89 unsigned frame_base_addr : 20;
90} ATTRIBUTE_PACKED pte_level1_t;
91
92typedef union {
93 pte_level0_t l0;
94 pte_level1_t l1;
95} pte_t;
96
97/* Level 1 page tables access permissions */
98
99/** User mode: no access, privileged mode: no access. */
100#define PTE_AP0_USER_NO_KERNEL_NO 0
101
102/** User mode: no access, privileged mode: read/write. */
103#define PTE_AP0_USER_NO_KERNEL_FULL 1
104
105/** User mode: read only, privileged mode: read/write. */
106#define PTE_AP0_USER_LIMITED_KERNEL_FULL 2
107
108/** User mode: read/write, privileged mode: read/write. */
109#define PTE_AP0_USER_FULL_KERNEL_FULL 3
110
111/** Allow writes */
112#define PTE_AP1_RO 1
113
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
126/** pte_level1_t small page table flag with NX (used in descriptor type). */
127#define PTE_DESCRIPTOR_SMALL_PAGE_NX 3
128
129/** Sets the address of level 0 page table.
130 *
131 * @param pt Pointer to the page table to set.
132 *
133 */
134NO_TRACE static inline void set_ptl0_addr(pte_t *pt)
135{
136 TTBR0_write((uint32_t)pt);
137}
138
139
140/** Returns level 0 page table entry flags.
141 *
142 * @param pt Level 0 page table.
143 * @param i Index of the entry to return.
144 *
145 */
146NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
147{
148 const pte_level0_t *p = &pt[i].l0;
149 const unsigned np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
150
151 return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
152 (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
153 (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
154}
155
156/** Returns level 1 page table entry flags.
157 *
158 * @param pt Level 1 page table.
159 * @param i Index of the entry to return.
160 *
161 */
162NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
163{
164 const pte_level1_t *p = &pt[i].l1;
165
166 const unsigned dt = p->descriptor_type;
167 const unsigned ap0 = p->access_permission_0;
168 const unsigned ap1 = p->access_permission_1;
169
170 return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
171 ((dt != PTE_DESCRIPTOR_SMALL_PAGE_NX) << PAGE_EXEC_SHIFT) |
172 ((ap0 == PTE_AP0_USER_LIMITED_KERNEL_FULL) << PAGE_READ_SHIFT) |
173 ((ap0 == PTE_AP0_USER_FULL_KERNEL_FULL) << PAGE_READ_SHIFT) |
174 ((ap0 == PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_READ_SHIFT) |
175 ((ap0 != PTE_AP0_USER_NO_KERNEL_FULL) << PAGE_USER_SHIFT) |
176 (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_FULL_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
177 (((ap1 != PTE_AP1_RO) && (ap0 == PTE_AP0_USER_NO_KERNEL_FULL)) << PAGE_WRITE_SHIFT) |
178 (p->bufferable << PAGE_CACHEABLE);
179}
180
181/** Sets flags of level 0 page table entry.
182 *
183 * @param pt level 0 page table
184 * @param i index of the entry to be changed
185 * @param flags new flags
186 *
187 */
188NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
189{
190 pte_level0_t *p = &pt[i].l0;
191
192 if (flags & PAGE_NOT_PRESENT) {
193 p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
194 /*
195 * Ensures that the entry will be recognized as valid when
196 * PTE_VALID_ARCH applied.
197 */
198 p->should_be_zero_0 = 1;
199 p->should_be_zero_1 = 1;
200 } else {
201 p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
202 p->should_be_zero_0 = 0;
203 p->should_be_zero_1 = 0;
204 p->domain = 0;
205 p->ns = 0;
206 }
207 DCCMVAU_write((uint32_t)p);
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 */
222NO_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 if (flags & PAGE_EXEC)
230 p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
231 else
232 p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE_NX;
233 }
234
235 /* tex=0 buf=1 and cache=1 => normal memory
236 * tex=0 buf=1 and cache=0 => shareable device mmio
237 */
238 p->cacheable = (flags & PAGE_CACHEABLE);
239 p->bufferable = 1;
240 p->tex = 0;
241
242 /* Shareable is ignored for devices (non-cacheable),
243 * turn it on for normal memory. */
244 p->shareable = 1;
245
246 p->non_global = !(flags & PAGE_GLOBAL);
247
248 /* default access permission: kernel only*/
249 p->access_permission_0 = PTE_AP0_USER_NO_KERNEL_FULL;
250
251 if (flags & PAGE_USER) {
252 p->access_permission_0 = PTE_AP0_USER_FULL_KERNEL_FULL;
253 // TODO Fix kernel to use PAGE_WRITE flag properly and
254 // apply this for kernel pages as well.
255 if (!(flags & PAGE_WRITE))
256 p->access_permission_1 = PTE_AP1_RO;
257 }
258 DCCMVAU_write((uint32_t)p);
259}
260
261NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
262{
263 pte_level0_t *p = &pt[i].l0;
264
265 p->should_be_zero_0 = 0;
266 p->should_be_zero_1 = 0;
267 write_barrier();
268 p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
269 DCCMVAU_write((uint32_t)p);
270}
271
272NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
273{
274 pte_level1_t *p = &pt[i].l1;
275
276 p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
277 DCCMVAU_write((uint32_t)p);
278}
279
280
281extern void page_arch_init(void);
282
283#endif /* __ASM__ */
284
285#endif
286
287/** @}
288 */
Note: See TracBrowser for help on using the repository browser.