1 | /*
|
---|
2 | * Copyright (c) 2010 Martin Decky
|
---|
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 abs32lemm
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef KERN_abs32le_PAGE_H_
|
---|
36 | #define KERN_abs32le_PAGE_H_
|
---|
37 |
|
---|
38 | #include <arch/mm/frame.h>
|
---|
39 |
|
---|
40 | #define PAGE_WIDTH FRAME_WIDTH
|
---|
41 | #define PAGE_SIZE FRAME_SIZE
|
---|
42 |
|
---|
43 | #ifdef KERNEL
|
---|
44 |
|
---|
45 | #define KA2PA(x) (((uintptr_t) (x)) - 0x80000000)
|
---|
46 | #define PA2KA(x) (((uintptr_t) (x)) + 0x80000000)
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * This is an example of 2-level page tables (PTL1 and PTL2 are left out)
|
---|
50 | * on top of the generic 4-level page table interface.
|
---|
51 | */
|
---|
52 |
|
---|
53 | /* Number of entries in each level. */
|
---|
54 | #define PTL0_ENTRIES_ARCH 1024
|
---|
55 | #define PTL1_ENTRIES_ARCH 0
|
---|
56 | #define PTL2_ENTRIES_ARCH 0
|
---|
57 | #define PTL3_ENTRIES_ARCH 1024
|
---|
58 |
|
---|
59 | /* Page table sizes for each level. */
|
---|
60 | #define PTL0_SIZE_ARCH ONE_FRAME
|
---|
61 | #define PTL1_SIZE_ARCH 0
|
---|
62 | #define PTL2_SIZE_ARCH 0
|
---|
63 | #define PTL3_SIZE_ARCH ONE_FRAME
|
---|
64 |
|
---|
65 | /* Macros calculating indices for each level. */
|
---|
66 | #define PTL0_INDEX_ARCH(vaddr) (((vaddr) >> 22) & 0x3ff)
|
---|
67 | #define PTL1_INDEX_ARCH(vaddr) 0
|
---|
68 | #define PTL2_INDEX_ARCH(vaddr) 0
|
---|
69 | #define PTL3_INDEX_ARCH(vaddr) (((vaddr) >> 12) & 0x3ff)
|
---|
70 |
|
---|
71 | /* Get PTE address accessors for each level. */
|
---|
72 | #define GET_PTL1_ADDRESS_ARCH(ptl0, i) \
|
---|
73 | ((pte_t *) ((((pte_t *) (ptl0))[(i)].frame_address) << 12))
|
---|
74 | #define GET_PTL2_ADDRESS_ARCH(ptl1, i) \
|
---|
75 | (ptl1)
|
---|
76 | #define GET_PTL3_ADDRESS_ARCH(ptl2, i) \
|
---|
77 | (ptl2)
|
---|
78 | #define GET_FRAME_ADDRESS_ARCH(ptl3, i) \
|
---|
79 | ((uintptr_t) ((((pte_t *) (ptl3))[(i)].frame_address) << 12))
|
---|
80 |
|
---|
81 | /* Set PTE address accessors for each level. */
|
---|
82 | #define SET_PTL0_ADDRESS_ARCH(ptl0)
|
---|
83 | #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) \
|
---|
84 | (((pte_t *) (ptl0))[(i)].frame_address = (a) >> 12)
|
---|
85 | #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
|
---|
86 | #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
|
---|
87 | #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) \
|
---|
88 | (((pte_t *) (ptl3))[(i)].frame_address = (a) >> 12)
|
---|
89 |
|
---|
90 | /* Get PTE flags accessors for each level. */
|
---|
91 | #define GET_PTL1_FLAGS_ARCH(ptl0, i) \
|
---|
92 | get_pt_flags((pte_t *) (ptl0), (size_t) (i))
|
---|
93 | #define GET_PTL2_FLAGS_ARCH(ptl1, i) \
|
---|
94 | PAGE_PRESENT
|
---|
95 | #define GET_PTL3_FLAGS_ARCH(ptl2, i) \
|
---|
96 | PAGE_PRESENT
|
---|
97 | #define GET_FRAME_FLAGS_ARCH(ptl3, i) \
|
---|
98 | get_pt_flags((pte_t *) (ptl3), (size_t) (i))
|
---|
99 |
|
---|
100 | /* Set PTE flags accessors for each level. */
|
---|
101 | #define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
|
---|
102 | set_pt_flags((pte_t *) (ptl0), (size_t) (i), (x))
|
---|
103 | #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
|
---|
104 | #define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
|
---|
105 | #define SET_FRAME_FLAGS_ARCH(ptl3, i, x) \
|
---|
106 | set_pt_flags((pte_t *) (ptl3), (size_t) (i), (x))
|
---|
107 |
|
---|
108 | /* Macros for querying the last level entries. */
|
---|
109 | #define PTE_VALID_ARCH(p) \
|
---|
110 | (*((uint32_t *) (p)) != 0)
|
---|
111 | #define PTE_PRESENT_ARCH(p) \
|
---|
112 | ((p)->present != 0)
|
---|
113 | #define PTE_GET_FRAME_ARCH(p) \
|
---|
114 | ((p)->frame_address << FRAME_WIDTH)
|
---|
115 | #define PTE_WRITABLE_ARCH(p) \
|
---|
116 | ((p)->writeable != 0)
|
---|
117 | #define PTE_EXECUTABLE_ARCH(p) 1
|
---|
118 |
|
---|
119 | #include <mm/mm.h>
|
---|
120 | #include <arch/interrupt.h>
|
---|
121 | #include <arch/types.h>
|
---|
122 | #include <typedefs.h>
|
---|
123 |
|
---|
124 | /** Page Table Entry. */
|
---|
125 | typedef struct {
|
---|
126 | unsigned int present : 1;
|
---|
127 | unsigned int writeable : 1;
|
---|
128 | unsigned int uaccessible : 1;
|
---|
129 | unsigned int page_write_through : 1;
|
---|
130 | unsigned int page_cache_disable : 1;
|
---|
131 | unsigned int accessed : 1;
|
---|
132 | unsigned int dirty : 1;
|
---|
133 | unsigned int pat : 1;
|
---|
134 | unsigned int global : 1;
|
---|
135 |
|
---|
136 | /** Valid content even if the present bit is not set. */
|
---|
137 | unsigned int soft_valid : 1;
|
---|
138 | unsigned int avl : 2;
|
---|
139 | unsigned int frame_address : 20;
|
---|
140 | } __attribute__((packed)) pte_t;
|
---|
141 |
|
---|
142 | static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
|
---|
143 | {
|
---|
144 | pte_t *p = &pt[i];
|
---|
145 |
|
---|
146 | return ((!p->page_cache_disable) << PAGE_CACHEABLE_SHIFT |
|
---|
147 | (!p->present) << PAGE_PRESENT_SHIFT |
|
---|
148 | p->uaccessible << PAGE_USER_SHIFT |
|
---|
149 | 1 << PAGE_READ_SHIFT |
|
---|
150 | p->writeable << PAGE_WRITE_SHIFT |
|
---|
151 | 1 << PAGE_EXEC_SHIFT |
|
---|
152 | p->global << PAGE_GLOBAL_SHIFT);
|
---|
153 | }
|
---|
154 |
|
---|
155 | static inline void set_pt_flags(pte_t *pt, size_t i, int flags)
|
---|
156 | {
|
---|
157 | pte_t *p = &pt[i];
|
---|
158 |
|
---|
159 | p->page_cache_disable = !(flags & PAGE_CACHEABLE);
|
---|
160 | p->present = !(flags & PAGE_NOT_PRESENT);
|
---|
161 | p->uaccessible = (flags & PAGE_USER) != 0;
|
---|
162 | p->writeable = (flags & PAGE_WRITE) != 0;
|
---|
163 | p->global = (flags & PAGE_GLOBAL) != 0;
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Ensure that there is at least one bit set even if the present bit is
|
---|
167 | * cleared.
|
---|
168 | */
|
---|
169 | p->soft_valid = true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | extern void page_arch_init(void);
|
---|
173 | extern void page_fault(int, istate_t *);
|
---|
174 |
|
---|
175 | #endif /* KERNEL */
|
---|
176 |
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | /** @}
|
---|
180 | */
|
---|