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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f23341 was 7a0359b, checked in by Martin Decky <martin@…>, 15 years ago

improve kernel function tracing

  • add support for more generic kernel sources
  • replace attribute((no_instrument_function)) with NO_TRACE macro (shorter and for future compatibility with different compilers)
  • to be on the safe side, do not instrument most of the inline and static functions (plus some specific non-static functions)

collateral code cleanup (no change in functionality)

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