Rev 1411 | Rev 1708 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1411 | Rev 1702 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
2 | * Copyright (C) 2005 Martin Decky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
- | 29 | /** @addtogroup ppc64mm |
|
- | 30 | * @{ |
|
- | 31 | */ |
|
- | 32 | /** @file |
|
- | 33 | */ |
|
- | 34 | ||
29 | #include <arch/mm/page.h> |
35 | #include <arch/mm/page.h> |
30 | #include <genarch/mm/page_pt.h> |
36 | #include <genarch/mm/page_pt.h> |
31 | #include <arch/mm/frame.h> |
37 | #include <arch/mm/frame.h> |
32 | #include <arch/asm.h> |
38 | #include <arch/asm.h> |
33 | #include <mm/frame.h> |
39 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
40 | #include <mm/page.h> |
35 | #include <mm/as.h> |
41 | #include <mm/as.h> |
36 | #include <arch.h> |
42 | #include <arch.h> |
37 | #include <arch/types.h> |
43 | #include <arch/types.h> |
38 | #include <arch/exception.h> |
44 | #include <arch/exception.h> |
39 | #include <align.h> |
45 | #include <align.h> |
40 | #include <config.h> |
46 | #include <config.h> |
41 | #include <print.h> |
47 | #include <print.h> |
42 | #include <symtab.h> |
48 | #include <symtab.h> |
43 | 49 | ||
44 | static phte_t *phte; |
50 | static phte_t *phte; |
45 | 51 | ||
46 | 52 | ||
47 | /** Try to find PTE for faulting address |
53 | /** Try to find PTE for faulting address |
48 | * |
54 | * |
49 | * Try to find PTE for faulting address. |
55 | * Try to find PTE for faulting address. |
50 | * The as->lock must be held on entry to this function |
56 | * The as->lock must be held on entry to this function |
51 | * if lock is true. |
57 | * if lock is true. |
52 | * |
58 | * |
53 | * @param as Address space. |
59 | * @param as Address space. |
54 | * @param lock Lock/unlock the address space. |
60 | * @param lock Lock/unlock the address space. |
55 | * @param badvaddr Faulting virtual address. |
61 | * @param badvaddr Faulting virtual address. |
56 | * @param access Access mode that caused the fault. |
62 | * @param access Access mode that caused the fault. |
57 | * @param istate Pointer to interrupted state. |
63 | * @param istate Pointer to interrupted state. |
58 | * @param pfrc Pointer to variable where as_page_fault() return code will be stored. |
64 | * @param pfrc Pointer to variable where as_page_fault() return code will be stored. |
59 | * @return PTE on success, NULL otherwise. |
65 | * @return PTE on success, NULL otherwise. |
60 | * |
66 | * |
61 | */ |
67 | */ |
62 | static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, int access, |
68 | static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, int access, |
63 | istate_t *istate, int *pfcr) |
69 | istate_t *istate, int *pfcr) |
64 | { |
70 | { |
65 | /* |
71 | /* |
66 | * Check if the mapping exists in page tables. |
72 | * Check if the mapping exists in page tables. |
67 | */ |
73 | */ |
68 | pte_t *pte = page_mapping_find(as, badvaddr); |
74 | pte_t *pte = page_mapping_find(as, badvaddr); |
69 | if ((pte) && (pte->p)) { |
75 | if ((pte) && (pte->p)) { |
70 | /* |
76 | /* |
71 | * Mapping found in page tables. |
77 | * Mapping found in page tables. |
72 | * Immediately succeed. |
78 | * Immediately succeed. |
73 | */ |
79 | */ |
74 | return pte; |
80 | return pte; |
75 | } else { |
81 | } else { |
76 | int rc; |
82 | int rc; |
77 | 83 | ||
78 | /* |
84 | /* |
79 | * Mapping not found in page tables. |
85 | * Mapping not found in page tables. |
80 | * Resort to higher-level page fault handler. |
86 | * Resort to higher-level page fault handler. |
81 | */ |
87 | */ |
82 | page_table_unlock(as, lock); |
88 | page_table_unlock(as, lock); |
83 | switch (rc = as_page_fault(badvaddr, access, istate)) { |
89 | switch (rc = as_page_fault(badvaddr, access, istate)) { |
84 | case AS_PF_OK: |
90 | case AS_PF_OK: |
85 | /* |
91 | /* |
86 | * The higher-level page fault handler succeeded, |
92 | * The higher-level page fault handler succeeded, |
87 | * The mapping ought to be in place. |
93 | * The mapping ought to be in place. |
88 | */ |
94 | */ |
89 | page_table_lock(as, lock); |
95 | page_table_lock(as, lock); |
90 | pte = page_mapping_find(as, badvaddr); |
96 | pte = page_mapping_find(as, badvaddr); |
91 | ASSERT((pte) && (pte->p)); |
97 | ASSERT((pte) && (pte->p)); |
92 | return pte; |
98 | return pte; |
93 | case AS_PF_DEFER: |
99 | case AS_PF_DEFER: |
94 | page_table_lock(as, lock); |
100 | page_table_lock(as, lock); |
95 | *pfcr = rc; |
101 | *pfcr = rc; |
96 | return NULL; |
102 | return NULL; |
97 | case AS_PF_FAULT: |
103 | case AS_PF_FAULT: |
98 | page_table_lock(as, lock); |
104 | page_table_lock(as, lock); |
99 | printf("Page fault.\n"); |
105 | printf("Page fault.\n"); |
100 | *pfcr = rc; |
106 | *pfcr = rc; |
101 | return NULL; |
107 | return NULL; |
102 | default: |
108 | default: |
103 | panic("unexpected rc (%d)\n", rc); |
109 | panic("unexpected rc (%d)\n", rc); |
104 | } |
110 | } |
105 | } |
111 | } |
106 | } |
112 | } |
107 | 113 | ||
108 | 114 | ||
109 | static void pht_refill_fail(__address badvaddr, istate_t *istate) |
115 | static void pht_refill_fail(__address badvaddr, istate_t *istate) |
110 | { |
116 | { |
111 | char *symbol = ""; |
117 | char *symbol = ""; |
112 | char *sym2 = ""; |
118 | char *sym2 = ""; |
113 | 119 | ||
114 | char *s = get_symtab_entry(istate->pc); |
120 | char *s = get_symtab_entry(istate->pc); |
115 | if (s) |
121 | if (s) |
116 | symbol = s; |
122 | symbol = s; |
117 | s = get_symtab_entry(istate->lr); |
123 | s = get_symtab_entry(istate->lr); |
118 | if (s) |
124 | if (s) |
119 | sym2 = s; |
125 | sym2 = s; |
120 | panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2); |
126 | panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2); |
121 | } |
127 | } |
122 | 128 | ||
123 | 129 | ||
124 | static void pht_insert(const __address vaddr, const pfn_t pfn) |
130 | static void pht_insert(const __address vaddr, const pfn_t pfn) |
125 | { |
131 | { |
126 | __u32 page = (vaddr >> 12) & 0xffff; |
132 | __u32 page = (vaddr >> 12) & 0xffff; |
127 | __u32 api = (vaddr >> 22) & 0x3f; |
133 | __u32 api = (vaddr >> 22) & 0x3f; |
128 | __u32 vsid; |
134 | __u32 vsid; |
129 | 135 | ||
130 | asm volatile ( |
136 | asm volatile ( |
131 | "mfsrin %0, %1\n" |
137 | "mfsrin %0, %1\n" |
132 | : "=r" (vsid) |
138 | : "=r" (vsid) |
133 | : "r" (vaddr) |
139 | : "r" (vaddr) |
134 | ); |
140 | ); |
135 | 141 | ||
136 | /* Primary hash (xor) */ |
142 | /* Primary hash (xor) */ |
137 | __u32 h = 0; |
143 | __u32 h = 0; |
138 | __u32 hash = vsid ^ page; |
144 | __u32 hash = vsid ^ page; |
139 | __u32 base = (hash & 0x3ff) << 3; |
145 | __u32 base = (hash & 0x3ff) << 3; |
140 | __u32 i; |
146 | __u32 i; |
141 | bool found = false; |
147 | bool found = false; |
142 | 148 | ||
143 | /* Find unused or colliding |
149 | /* Find unused or colliding |
144 | PTE in PTEG */ |
150 | PTE in PTEG */ |
145 | for (i = 0; i < 8; i++) { |
151 | for (i = 0; i < 8; i++) { |
146 | if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) && (phte[base + i].api == api))) { |
152 | if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) && (phte[base + i].api == api))) { |
147 | found = true; |
153 | found = true; |
148 | break; |
154 | break; |
149 | } |
155 | } |
150 | } |
156 | } |
151 | 157 | ||
152 | if (!found) { |
158 | if (!found) { |
153 | /* Secondary hash (not) */ |
159 | /* Secondary hash (not) */ |
154 | __u32 base2 = (~hash & 0x3ff) << 3; |
160 | __u32 base2 = (~hash & 0x3ff) << 3; |
155 | 161 | ||
156 | /* Find unused or colliding |
162 | /* Find unused or colliding |
157 | PTE in PTEG */ |
163 | PTE in PTEG */ |
158 | for (i = 0; i < 8; i++) { |
164 | for (i = 0; i < 8; i++) { |
159 | if ((!phte[base2 + i].v) || ((phte[base2 + i].vsid == vsid) && (phte[base2 + i].api == api))) { |
165 | if ((!phte[base2 + i].v) || ((phte[base2 + i].vsid == vsid) && (phte[base2 + i].api == api))) { |
160 | found = true; |
166 | found = true; |
161 | base = base2; |
167 | base = base2; |
162 | h = 1; |
168 | h = 1; |
163 | break; |
169 | break; |
164 | } |
170 | } |
165 | } |
171 | } |
166 | 172 | ||
167 | if (!found) { |
173 | if (!found) { |
168 | // TODO: A/C precedence groups |
174 | // TODO: A/C precedence groups |
169 | i = page % 8; |
175 | i = page % 8; |
170 | } |
176 | } |
171 | } |
177 | } |
172 | 178 | ||
173 | phte[base + i].v = 1; |
179 | phte[base + i].v = 1; |
174 | phte[base + i].vsid = vsid; |
180 | phte[base + i].vsid = vsid; |
175 | phte[base + i].h = h; |
181 | phte[base + i].h = h; |
176 | phte[base + i].api = api; |
182 | phte[base + i].api = api; |
177 | phte[base + i].rpn = pfn; |
183 | phte[base + i].rpn = pfn; |
178 | phte[base + i].r = 0; |
184 | phte[base + i].r = 0; |
179 | phte[base + i].c = 0; |
185 | phte[base + i].c = 0; |
180 | phte[base + i].pp = 2; // FIXME |
186 | phte[base + i].pp = 2; // FIXME |
181 | } |
187 | } |
182 | 188 | ||
183 | 189 | ||
184 | /** Process Instruction/Data Storage Interrupt |
190 | /** Process Instruction/Data Storage Interrupt |
185 | * |
191 | * |
186 | * @param data True if Data Storage Interrupt. |
192 | * @param data True if Data Storage Interrupt. |
187 | * @param istate Interrupted register context. |
193 | * @param istate Interrupted register context. |
188 | * |
194 | * |
189 | */ |
195 | */ |
190 | void pht_refill(bool data, istate_t *istate) |
196 | void pht_refill(bool data, istate_t *istate) |
191 | { |
197 | { |
192 | __address badvaddr; |
198 | __address badvaddr; |
193 | pte_t *pte; |
199 | pte_t *pte; |
194 | int pfcr; |
200 | int pfcr; |
195 | as_t *as; |
201 | as_t *as; |
196 | bool lock; |
202 | bool lock; |
197 | 203 | ||
198 | if (AS == NULL) { |
204 | if (AS == NULL) { |
199 | as = AS_KERNEL; |
205 | as = AS_KERNEL; |
200 | lock = false; |
206 | lock = false; |
201 | } else { |
207 | } else { |
202 | as = AS; |
208 | as = AS; |
203 | lock = true; |
209 | lock = true; |
204 | } |
210 | } |
205 | 211 | ||
206 | if (data) { |
212 | if (data) { |
207 | asm volatile ( |
213 | asm volatile ( |
208 | "mfdar %0\n" |
214 | "mfdar %0\n" |
209 | : "=r" (badvaddr) |
215 | : "=r" (badvaddr) |
210 | ); |
216 | ); |
211 | } else |
217 | } else |
212 | badvaddr = istate->pc; |
218 | badvaddr = istate->pc; |
213 | 219 | ||
214 | page_table_lock(as, lock); |
220 | page_table_lock(as, lock); |
215 | 221 | ||
216 | pte = find_mapping_and_check(as, lock, badvaddr, PF_ACCESS_READ /* FIXME */, istate, &pfcr); |
222 | pte = find_mapping_and_check(as, lock, badvaddr, PF_ACCESS_READ /* FIXME */, istate, &pfcr); |
217 | if (!pte) { |
223 | if (!pte) { |
218 | switch (pfcr) { |
224 | switch (pfcr) { |
219 | case AS_PF_FAULT: |
225 | case AS_PF_FAULT: |
220 | goto fail; |
226 | goto fail; |
221 | break; |
227 | break; |
222 | case AS_PF_DEFER: |
228 | case AS_PF_DEFER: |
223 | /* |
229 | /* |
224 | * The page fault came during copy_from_uspace() |
230 | * The page fault came during copy_from_uspace() |
225 | * or copy_to_uspace(). |
231 | * or copy_to_uspace(). |
226 | */ |
232 | */ |
227 | page_table_unlock(as, lock); |
233 | page_table_unlock(as, lock); |
228 | return; |
234 | return; |
229 | default: |
235 | default: |
230 | panic("Unexpected pfrc (%d)\n", pfcr); |
236 | panic("Unexpected pfrc (%d)\n", pfcr); |
231 | } |
237 | } |
232 | } |
238 | } |
233 | 239 | ||
234 | pte->a = 1; /* Record access to PTE */ |
240 | pte->a = 1; /* Record access to PTE */ |
235 | pht_insert(badvaddr, pte->pfn); |
241 | pht_insert(badvaddr, pte->pfn); |
236 | 242 | ||
237 | page_table_unlock(as, lock); |
243 | page_table_unlock(as, lock); |
238 | return; |
244 | return; |
239 | 245 | ||
240 | fail: |
246 | fail: |
241 | page_table_unlock(as, lock); |
247 | page_table_unlock(as, lock); |
242 | pht_refill_fail(badvaddr, istate); |
248 | pht_refill_fail(badvaddr, istate); |
243 | } |
249 | } |
244 | 250 | ||
245 | 251 | ||
246 | void pht_init(void) |
252 | void pht_init(void) |
247 | { |
253 | { |
248 | memsetb((__address) phte, 1 << PHT_BITS, 0); |
254 | memsetb((__address) phte, 1 << PHT_BITS, 0); |
249 | } |
255 | } |
250 | 256 | ||
251 | 257 | ||
252 | void page_arch_init(void) |
258 | void page_arch_init(void) |
253 | { |
259 | { |
254 | if (config.cpu_active == 1) { |
260 | if (config.cpu_active == 1) { |
255 | page_mapping_operations = &pt_mapping_operations; |
261 | page_mapping_operations = &pt_mapping_operations; |
256 | 262 | ||
257 | __address cur; |
263 | __address cur; |
258 | int flags; |
264 | int flags; |
259 | 265 | ||
260 | /* Frames below 128 MB are mapped using BAT, |
266 | /* Frames below 128 MB are mapped using BAT, |
261 | map rest of the physical memory */ |
267 | map rest of the physical memory */ |
262 | for (cur = 128 << 20; cur < last_frame; cur += FRAME_SIZE) { |
268 | for (cur = 128 << 20; cur < last_frame; cur += FRAME_SIZE) { |
263 | flags = PAGE_CACHEABLE; |
269 | flags = PAGE_CACHEABLE; |
264 | if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size)) |
270 | if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size)) |
265 | flags |= PAGE_GLOBAL; |
271 | flags |= PAGE_GLOBAL; |
266 | page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags); |
272 | page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags); |
267 | } |
273 | } |
268 | 274 | ||
269 | /* Allocate page hash table */ |
275 | /* Allocate page hash table */ |
270 | phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC)); |
276 | phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC)); |
271 | phte = (phte_t *) PA2KA((__address) physical_phte); |
277 | phte = (phte_t *) PA2KA((__address) physical_phte); |
272 | 278 | ||
273 | ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0); |
279 | ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0); |
274 | pht_init(); |
280 | pht_init(); |
275 | 281 | ||
276 | asm volatile ( |
282 | asm volatile ( |
277 | "mtsdr1 %0\n" |
283 | "mtsdr1 %0\n" |
278 | : |
284 | : |
279 | : "r" ((__address) physical_phte) |
285 | : "r" ((__address) physical_phte) |
280 | ); |
286 | ); |
281 | } |
287 | } |
282 | } |
288 | } |
283 | 289 | ||
284 | 290 | ||
285 | __address hw_map(__address physaddr, size_t size) |
291 | __address hw_map(__address physaddr, size_t size) |
286 | { |
292 | { |
287 | if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH)) |
293 | if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH)) |
288 | panic("Unable to map physical memory %p (%d bytes)", physaddr, size) |
294 | panic("Unable to map physical memory %p (%d bytes)", physaddr, size) |
289 | 295 | ||
290 | __address virtaddr = PA2KA(last_frame); |
296 | __address virtaddr = PA2KA(last_frame); |
291 | pfn_t i; |
297 | pfn_t i; |
292 | for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++) |
298 | for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++) |
293 | page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE); |
299 | page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE); |
294 | 300 | ||
295 | last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE); |
301 | last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE); |
296 | 302 | ||
297 | return virtaddr; |
303 | return virtaddr; |
298 | } |
304 | } |
- | 305 | ||
- | 306 | /** @} |
|
- | 307 | */ |
|
- | 308 | ||
299 | 309 |