source: mainline/kernel/arch/arm32/include/mm/page.h@ f72ae3b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f72ae3b was 0d8269b, checked in by Jakub Jermar <jakub@…>, 13 years ago

Add macros for setting the present bit in PTEs separately.

  • Property mode set to 100644
File size: 10.3 KB
RevLine 
[d630139]1/*
[6b781c0]2 * Copyright (c) 2007 Pavel Jancik, Michal Kebrt
[d630139]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
[7a0359b]29/** @addtogroup arm32mm
[d630139]30 * @{
31 */
32/** @file
[6b781c0]33 * @brief Paging related declarations.
[d630139]34 */
35
36#ifndef KERN_arm32_PAGE_H_
37#define KERN_arm32_PAGE_H_
38
39#include <arch/mm/frame.h>
[6b781c0]40#include <mm/mm.h>
41#include <arch/exception.h>
[0d8269b]42#include <arch/barrier.h>
[7a0359b]43#include <trace.h>
[d630139]44
45#define PAGE_WIDTH FRAME_WIDTH
46#define PAGE_SIZE FRAME_SIZE
47
48#ifndef __ASM__
49# define KA2PA(x) (((uintptr_t) (x)) - 0x80000000)
50# define PA2KA(x) (((uintptr_t) (x)) + 0x80000000)
51#else
52# define KA2PA(x) ((x) - 0x80000000)
53# define PA2KA(x) ((x) + 0x80000000)
54#endif
55
[c03ee1c]56/* Number of entries in each level. */
[3b71e84d]57#define PTL0_ENTRIES_ARCH (1 << 12) /* 4096 */
[6b781c0]58#define PTL1_ENTRIES_ARCH 0
59#define PTL2_ENTRIES_ARCH 0
60/* coarse page tables used (256 * 4 = 1KB per page) */
[3b71e84d]61#define PTL3_ENTRIES_ARCH (1 << 8) /* 256 */
[6b781c0]62
[c03ee1c]63/* Page table sizes for each level. */
[6b781c0]64#define PTL0_SIZE_ARCH FOUR_FRAMES
65#define PTL1_SIZE_ARCH 0
66#define PTL2_SIZE_ARCH 0
67#define PTL3_SIZE_ARCH ONE_FRAME
[d630139]68
[c03ee1c]69/* Macros calculating indices into page tables for each level. */
[6b781c0]70#define PTL0_INDEX_ARCH(vaddr) (((vaddr) >> 20) & 0xfff)
71#define PTL1_INDEX_ARCH(vaddr) 0
72#define PTL2_INDEX_ARCH(vaddr) 0
73#define PTL3_INDEX_ARCH(vaddr) (((vaddr) >> 12) & 0x0ff)
[d630139]74
[c03ee1c]75/* Get PTE address accessors for each level. */
[6b781c0]76#define GET_PTL1_ADDRESS_ARCH(ptl0, i) \
[c09adc10]77 ((pte_t *) ((((pte_t *)(ptl0))[(i)].l0).coarse_table_addr << 10))
[6b781c0]78#define GET_PTL2_ADDRESS_ARCH(ptl1, i) \
79 (ptl1)
80#define GET_PTL3_ADDRESS_ARCH(ptl2, i) \
81 (ptl2)
82#define GET_FRAME_ADDRESS_ARCH(ptl3, i) \
[c09adc10]83 ((uintptr_t) ((((pte_t *)(ptl3))[(i)].l1).frame_base_addr << 12))
[d630139]84
[c03ee1c]85/* Set PTE address accessors for each level. */
[6b781c0]86#define SET_PTL0_ADDRESS_ARCH(ptl0) \
[c09adc10]87 (set_ptl0_addr((pte_t *) (ptl0)))
[6b781c0]88#define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) \
[c09adc10]89 (((pte_t *) (ptl0))[(i)].l0.coarse_table_addr = (a) >> 10)
[6b781c0]90#define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
91#define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
92#define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) \
[c09adc10]93 (((pte_t *) (ptl3))[(i)].l1.frame_base_addr = (a) >> 12)
[d630139]94
[c03ee1c]95/* Get PTE flags accessors for each level. */
[6b781c0]96#define GET_PTL1_FLAGS_ARCH(ptl0, i) \
[c09adc10]97 get_pt_level0_flags((pte_t *) (ptl0), (size_t) (i))
[6b781c0]98#define GET_PTL2_FLAGS_ARCH(ptl1, i) \
99 PAGE_PRESENT
100#define GET_PTL3_FLAGS_ARCH(ptl2, i) \
101 PAGE_PRESENT
102#define GET_FRAME_FLAGS_ARCH(ptl3, i) \
[c09adc10]103 get_pt_level1_flags((pte_t *) (ptl3), (size_t) (i))
[d630139]104
[c03ee1c]105/* Set PTE flags accessors for each level. */
[6b781c0]106#define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
[c09adc10]107 set_pt_level0_flags((pte_t *) (ptl0), (size_t) (i), (x))
[6b781c0]108#define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
109#define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
110#define SET_FRAME_FLAGS_ARCH(ptl3, i, x) \
[c09adc10]111 set_pt_level1_flags((pte_t *) (ptl3), (size_t) (i), (x))
[d630139]112
[0d8269b]113/* Set PTE present bit accessors for each level. */
114#define SET_PTL1_PRESENT_ARCH(ptl0, i) \
115 set_pt_level0_present((pte_t *) (ptl0), (size_t) (i))
116#define SET_PTL2_PRESENT_ARCH(ptl1, i)
117#define SET_PTL3_PRESENT_ARCH(ptl2, i)
118#define SET_FRAME_PRESENT_ARCH(ptl3, i) \
119 set_pt_level1_present((pte_t *) (ptl3), (size_t) (i))
120
[c03ee1c]121/* Macros for querying the last-level PTE entries. */
[6b781c0]122#define PTE_VALID_ARCH(pte) \
123 (*((uint32_t *) (pte)) != 0)
124#define PTE_PRESENT_ARCH(pte) \
[c09adc10]125 (((pte_t *) (pte))->l0.descriptor_type != 0)
[6b781c0]126#define PTE_GET_FRAME_ARCH(pte) \
[c09adc10]127 (((pte_t *) (pte))->l1.frame_base_addr << FRAME_WIDTH)
[6b781c0]128#define PTE_WRITABLE_ARCH(pte) \
[c09adc10]129 (((pte_t *) (pte))->l1.access_permission_0 == PTE_AP_USER_RW_KERNEL_RW)
[6b781c0]130#define PTE_EXECUTABLE_ARCH(pte) \
131 1
[d630139]132
133#ifndef __ASM__
134
[6b781c0]135/** Level 0 page table entry. */
136typedef struct {
137 /* 0b01 for coarse tables, see below for details */
[c03ee1c]138 unsigned descriptor_type : 2;
139 unsigned impl_specific : 3;
140 unsigned domain : 4;
141 unsigned should_be_zero : 1;
[6b781c0]142
143 /* Pointer to the coarse 2nd level page table (holding entries for small
144 * (4KB) or large (64KB) pages. ARM also supports fine 2nd level page
145 * tables that may hold even tiny pages (1KB) but they are bigger (4KB
146 * per table in comparison with 1KB per the coarse table)
147 */
[c03ee1c]148 unsigned coarse_table_addr : 22;
[6b781c0]149} ATTRIBUTE_PACKED pte_level0_t;
150
151/** Level 1 page table entry (small (4KB) pages used). */
152typedef struct {
153
154 /* 0b10 for small pages */
[c03ee1c]155 unsigned descriptor_type : 2;
156 unsigned bufferable : 1;
157 unsigned cacheable : 1;
[6b781c0]158
159 /* access permissions for each of 4 subparts of a page
160 * (for each 1KB when small pages used */
161 unsigned access_permission_0 : 2;
162 unsigned access_permission_1 : 2;
163 unsigned access_permission_2 : 2;
164 unsigned access_permission_3 : 2;
[c03ee1c]165 unsigned frame_base_addr : 20;
[6b781c0]166} ATTRIBUTE_PACKED pte_level1_t;
167
[c09adc10]168typedef union {
169 pte_level0_t l0;
170 pte_level1_t l1;
171} pte_t;
[6b781c0]172
173/* Level 1 page tables access permissions */
[d630139]174
[6b781c0]175/** User mode: no access, privileged mode: no access. */
176#define PTE_AP_USER_NO_KERNEL_NO 0
177
178/** User mode: no access, privileged mode: read/write. */
179#define PTE_AP_USER_NO_KERNEL_RW 1
180
181/** User mode: read only, privileged mode: read/write. */
182#define PTE_AP_USER_RO_KERNEL_RW 2
183
184/** User mode: read/write, privileged mode: read/write. */
185#define PTE_AP_USER_RW_KERNEL_RW 3
186
187
188/* pte_level0_t and pte_level1_t descriptor_type flags */
189
190/** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
191#define PTE_DESCRIPTOR_NOT_PRESENT 0
192
193/** pte_level0_t coarse page table flag (used in descriptor_type). */
194#define PTE_DESCRIPTOR_COARSE_TABLE 1
195
196/** pte_level1_t small page table flag (used in descriptor type). */
197#define PTE_DESCRIPTOR_SMALL_PAGE 2
198
199
200/** Sets the address of level 0 page table.
201 *
[7a0359b]202 * @param pt Pointer to the page table to set.
203 *
204 */
205NO_TRACE static inline void set_ptl0_addr(pte_t *pt)
[d630139]206{
[6b781c0]207 asm volatile (
[e762b43]208 "mcr p15, 0, %[pt], c2, c0, 0\n"
209 :: [pt] "r" (pt)
[6b781c0]210 );
[d630139]211}
212
[6b781c0]213
214/** Returns level 0 page table entry flags.
215 *
[7a0359b]216 * @param pt Level 0 page table.
217 * @param i Index of the entry to return.
218 *
[6b781c0]219 */
[7a0359b]220NO_TRACE static inline int get_pt_level0_flags(pte_t *pt, size_t i)
[6b781c0]221{
[c09adc10]222 pte_level0_t *p = &pt[i].l0;
[6b781c0]223 int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
[7a0359b]224
[6b781c0]225 return (np << PAGE_PRESENT_SHIFT) | (1 << PAGE_USER_SHIFT) |
226 (1 << PAGE_READ_SHIFT) | (1 << PAGE_WRITE_SHIFT) |
227 (1 << PAGE_EXEC_SHIFT) | (1 << PAGE_CACHEABLE_SHIFT);
228}
229
230/** Returns level 1 page table entry flags.
231 *
[7a0359b]232 * @param pt Level 1 page table.
233 * @param i Index of the entry to return.
234 *
[6b781c0]235 */
[7a0359b]236NO_TRACE static inline int get_pt_level1_flags(pte_t *pt, size_t i)
[6b781c0]237{
[c09adc10]238 pte_level1_t *p = &pt[i].l1;
[7a0359b]239
[6b781c0]240 int dt = p->descriptor_type;
241 int ap = p->access_permission_0;
[7a0359b]242
[6b781c0]243 return ((dt == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT) |
244 ((ap == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT) |
245 ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT) |
246 ((ap == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT) |
247 ((ap != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT) |
248 ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT) |
249 ((ap == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT) |
250 (1 << PAGE_EXEC_SHIFT) |
251 (p->bufferable << PAGE_CACHEABLE);
252}
253
254/** Sets flags of level 0 page table entry.
255 *
[7a0359b]256 * @param pt level 0 page table
257 * @param i index of the entry to be changed
258 * @param flags new flags
259 *
[6b781c0]260 */
[7a0359b]261NO_TRACE static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
[d630139]262{
[c09adc10]263 pte_level0_t *p = &pt[i].l0;
[7a0359b]264
[6b781c0]265 if (flags & PAGE_NOT_PRESENT) {
266 p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
267 /*
268 * Ensures that the entry will be recognized as valid when
269 * PTE_VALID_ARCH applied.
270 */
271 p->should_be_zero = 1;
272 } else {
273 p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
274 p->should_be_zero = 0;
[7a0359b]275 }
[d630139]276}
277
[0d8269b]278NO_TRACE static inline void set_pt_level0_present(pte_t *pt, size_t i)
279{
280 pte_level0_t *p = &pt[i].l0;
281
282 p->should_be_zero = 0;
283 write_barrier();
284 p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
285}
[6b781c0]286
287/** Sets flags of level 1 page table entry.
288 *
[7a0359b]289 * We use same access rights for the whole page. When page
290 * is not preset we store 1 in acess_rigts_3 so that at least
291 * one bit is 1 (to mark correct page entry, see #PAGE_VALID_ARCH).
292 *
293 * @param pt Level 1 page table.
294 * @param i Index of the entry to be changed.
295 * @param flags New flags.
[6b781c0]296 *
[7a0359b]297 */
298NO_TRACE static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
[6b781c0]299{
[c09adc10]300 pte_level1_t *p = &pt[i].l1;
[6b781c0]301
[0d8269b]302 if (flags & PAGE_NOT_PRESENT)
[6b781c0]303 p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
[0d8269b]304 else
[6b781c0]305 p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
[7a0359b]306
[6b781c0]307 p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
[7a0359b]308
[6b781c0]309 /* default access permission */
310 p->access_permission_0 = p->access_permission_1 =
311 p->access_permission_2 = p->access_permission_3 =
312 PTE_AP_USER_NO_KERNEL_RW;
[7a0359b]313
[6b781c0]314 if (flags & PAGE_USER) {
315 if (flags & PAGE_READ) {
316 p->access_permission_0 = p->access_permission_1 =
317 p->access_permission_2 = p->access_permission_3 =
318 PTE_AP_USER_RO_KERNEL_RW;
319 }
320 if (flags & PAGE_WRITE) {
321 p->access_permission_0 = p->access_permission_1 =
322 p->access_permission_2 = p->access_permission_3 =
323 PTE_AP_USER_RW_KERNEL_RW;
324 }
325 }
326}
327
[0d8269b]328NO_TRACE static inline void set_pt_level1_present(pte_t *pt, size_t i)
329{
330 pte_level1_t *p = &pt[i].l1;
[6b781c0]331
[0d8269b]332 p->descriptor_type = PTE_DESCRIPTOR_SMALL_PAGE;
333}
334
[d630139]335extern void page_arch_init(void);
336
337#endif /* __ASM__ */
338
339#endif
340
341/** @}
342 */
Note: See TracBrowser for help on using the repository browser.