Rev 1378 | Rev 1384 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1378 | Rev 1383 | ||
---|---|---|---|
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 | #include <arch/mm/page.h> |
29 | #include <arch/mm/page.h> |
30 | #include <genarch/mm/page_pt.h> |
30 | #include <genarch/mm/page_pt.h> |
31 | #include <arch/mm/frame.h> |
31 | #include <arch/mm/frame.h> |
32 | #include <arch/asm.h> |
32 | #include <arch/asm.h> |
33 | #include <mm/frame.h> |
33 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
34 | #include <mm/page.h> |
35 | #include <mm/as.h> |
35 | #include <mm/as.h> |
36 | #include <arch.h> |
36 | #include <arch.h> |
37 | #include <arch/types.h> |
37 | #include <arch/types.h> |
38 | #include <arch/exception.h> |
38 | #include <arch/exception.h> |
- | 39 | #include <align.h> |
|
39 | #include <config.h> |
40 | #include <config.h> |
40 | #include <print.h> |
41 | #include <print.h> |
41 | #include <symtab.h> |
42 | #include <symtab.h> |
42 | 43 | ||
43 | static phte_t *phte; |
44 | static phte_t *phte; |
44 | 45 | ||
45 | 46 | ||
46 | /** Try to find PTE for faulting address |
47 | /** Try to find PTE for faulting address |
47 | * |
48 | * |
48 | * Try to find PTE for faulting address. |
49 | * Try to find PTE for faulting address. |
49 | * The AS->lock must be held on entry to this function. |
50 | * The as->lock must be held on entry to this function |
- | 51 | * if lock is true. |
|
50 | * |
52 | * |
- | 53 | * @param as Address space. |
|
- | 54 | * @param lock Lock/unlock the address space. |
|
51 | * @param badvaddr Faulting virtual address. |
55 | * @param badvaddr Faulting virtual address. |
52 | * @param istate Pointer to interrupted state. |
56 | * @param istate Pointer to interrupted state. |
53 | * @param pfrc Pointer to variable where as_page_fault() return code will be stored. |
57 | * @param pfrc Pointer to variable where as_page_fault() return code will be stored. |
54 | * @return PTE on success, NULL otherwise. |
58 | * @return PTE on success, NULL otherwise. |
55 | * |
59 | * |
56 | */ |
60 | */ |
57 | static pte_t *find_mapping_and_check(__address badvaddr, istate_t *istate, int *pfcr) |
61 | static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, istate_t *istate, int *pfcr) |
58 | { |
62 | { |
59 | /* |
63 | /* |
60 | * Check if the mapping exists in page tables. |
64 | * Check if the mapping exists in page tables. |
61 | */ |
65 | */ |
62 | pte_t *pte = page_mapping_find(AS, badvaddr); |
66 | pte_t *pte = page_mapping_find(as, badvaddr); |
63 | if ((pte) && (pte->p)) { |
67 | if ((pte) && (pte->p)) { |
64 | /* |
68 | /* |
65 | * Mapping found in page tables. |
69 | * Mapping found in page tables. |
66 | * Immediately succeed. |
70 | * Immediately succeed. |
67 | */ |
71 | */ |
68 | return pte; |
72 | return pte; |
69 | } else { |
73 | } else { |
70 | int rc; |
74 | int rc; |
71 | 75 | ||
72 | /* |
76 | /* |
73 | * Mapping not found in page tables. |
77 | * Mapping not found in page tables. |
74 | * Resort to higher-level page fault handler. |
78 | * Resort to higher-level page fault handler. |
75 | */ |
79 | */ |
76 | page_table_unlock(AS, true); |
80 | page_table_unlock(as, lock); |
77 | switch (rc = as_page_fault(badvaddr, istate)) { |
81 | switch (rc = as_page_fault(badvaddr, istate)) { |
78 | case AS_PF_OK: |
82 | case AS_PF_OK: |
79 | /* |
83 | /* |
80 | * The higher-level page fault handler succeeded, |
84 | * The higher-level page fault handler succeeded, |
81 | * The mapping ought to be in place. |
85 | * The mapping ought to be in place. |
82 | */ |
86 | */ |
83 | page_table_lock(AS, true); |
87 | page_table_lock(as, lock); |
84 | pte = page_mapping_find(AS, badvaddr); |
88 | pte = page_mapping_find(as, badvaddr); |
85 | ASSERT((pte) && (pte->p)); |
89 | ASSERT((pte) && (pte->p)); |
86 | return pte; |
90 | return pte; |
87 | case AS_PF_DEFER: |
91 | case AS_PF_DEFER: |
88 | page_table_lock(AS, true); |
92 | page_table_lock(as, lock); |
89 | *pfcr = rc; |
93 | *pfcr = rc; |
90 | return NULL; |
94 | return NULL; |
91 | case AS_PF_FAULT: |
95 | case AS_PF_FAULT: |
92 | page_table_lock(AS, true); |
96 | page_table_lock(as, lock); |
93 | printf("Page fault.\n"); |
97 | printf("Page fault.\n"); |
94 | *pfcr = rc; |
98 | *pfcr = rc; |
95 | return NULL; |
99 | return NULL; |
96 | default: |
100 | default: |
97 | panic("unexpected rc (%d)\n", rc); |
101 | panic("unexpected rc (%d)\n", rc); |
98 | } |
102 | } |
99 | } |
103 | } |
100 | } |
104 | } |
101 | 105 | ||
102 | 106 | ||
103 | static void pht_refill_fail(__address badvaddr, istate_t *istate) |
107 | static void pht_refill_fail(__address badvaddr, istate_t *istate) |
104 | { |
108 | { |
105 | char *symbol = ""; |
109 | char *symbol = ""; |
106 | char *sym2 = ""; |
110 | char *sym2 = ""; |
107 | 111 | ||
108 | char *s = get_symtab_entry(istate->pc); |
112 | char *s = get_symtab_entry(istate->pc); |
109 | if (s) |
113 | if (s) |
110 | symbol = s; |
114 | symbol = s; |
111 | s = get_symtab_entry(istate->lr); |
115 | s = get_symtab_entry(istate->lr); |
112 | if (s) |
116 | if (s) |
113 | sym2 = s; |
117 | sym2 = s; |
114 | panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2); |
118 | panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2); |
115 | } |
119 | } |
116 | 120 | ||
117 | 121 | ||
118 | static void pht_insert(const __address vaddr, const pfn_t pfn) |
122 | static void pht_insert(const __address vaddr, const pfn_t pfn) |
119 | { |
123 | { |
120 | __u32 page = (vaddr >> 12) & 0xffff; |
124 | __u32 page = (vaddr >> 12) & 0xffff; |
121 | __u32 api = (vaddr >> 22) & 0x3f; |
125 | __u32 api = (vaddr >> 22) & 0x3f; |
122 | __u32 vsid; |
126 | __u32 vsid; |
123 | 127 | ||
124 | asm volatile ( |
128 | asm volatile ( |
125 | "mfsrin %0, %1\n" |
129 | "mfsrin %0, %1\n" |
126 | : "=r" (vsid) |
130 | : "=r" (vsid) |
127 | : "r" (vaddr) |
131 | : "r" (vaddr) |
128 | ); |
132 | ); |
129 | 133 | ||
130 | /* Primary hash (xor) */ |
134 | /* Primary hash (xor) */ |
131 | __u32 hash = ((vsid ^ page) & 0x3ff) << 3; |
135 | __u32 hash = ((vsid ^ page) & 0x3ff) << 3; |
132 | 136 | ||
133 | __u32 i; |
137 | __u32 i; |
134 | bool found = false; |
138 | bool found = false; |
135 | /* Find unused PTE in PTEG */ |
139 | /* Find unused PTE in PTEG */ |
136 | for (i = 0; i < 8; i++) { |
140 | for (i = 0; i < 8; i++) { |
137 | if (!phte[hash + i].v) { |
141 | if (!phte[hash + i].v) { |
138 | found = true; |
142 | found = true; |
139 | break; |
143 | break; |
140 | } |
144 | } |
141 | } |
145 | } |
142 | 146 | ||
143 | if (!found) { |
147 | if (!found) { |
144 | /* Secondary hash (not) */ |
148 | /* Secondary hash (not) */ |
145 | hash = ~hash; |
149 | hash = ~hash; |
146 | 150 | ||
147 | /* Find unused PTE in PTEG */ |
151 | /* Find unused PTE in PTEG */ |
148 | for (i = 0; i < 8; i++) { |
152 | for (i = 0; i < 8; i++) { |
149 | if (!phte[hash + i].v) { |
153 | if (!phte[hash + i].v) { |
150 | found = true; |
154 | found = true; |
151 | break; |
155 | break; |
152 | } |
156 | } |
153 | } |
157 | } |
154 | 158 | ||
155 | if (!found) { |
159 | if (!found) { |
156 | // TODO: A/C precedence groups |
160 | // TODO: A/C precedence groups |
157 | i = page % 8; |
161 | i = page % 8; |
158 | } |
162 | } |
159 | } |
163 | } |
160 | 164 | ||
161 | phte[hash + i].v = 1; |
165 | phte[hash + i].v = 1; |
162 | phte[hash + i].vsid = vsid; |
166 | phte[hash + i].vsid = vsid; |
163 | phte[hash + i].h = 0; |
167 | phte[hash + i].h = 0; |
164 | phte[hash + i].api = api; |
168 | phte[hash + i].api = api; |
165 | phte[hash + i].rpn = pfn; |
169 | phte[hash + i].rpn = pfn; |
166 | phte[hash + i].r = 0; |
170 | phte[hash + i].r = 0; |
167 | phte[hash + i].c = 0; |
171 | phte[hash + i].c = 0; |
168 | phte[hash + i].pp = 2; // FIXME |
172 | phte[hash + i].pp = 2; // FIXME |
169 | } |
173 | } |
170 | 174 | ||
171 | 175 | ||
172 | /** Process Instruction/Data Storage Interrupt |
176 | /** Process Instruction/Data Storage Interrupt |
173 | * |
177 | * |
174 | * @param data True if Data Storage Interrupt. |
178 | * @param data True if Data Storage Interrupt. |
175 | * @param istate Interrupted register context. |
179 | * @param istate Interrupted register context. |
176 | * |
180 | * |
177 | */ |
181 | */ |
178 | void pht_refill(bool data, istate_t *istate) |
182 | void pht_refill(bool data, istate_t *istate) |
179 | { |
183 | { |
180 | asid_t asid; |
184 | asid_t asid; |
181 | __address badvaddr; |
185 | __address badvaddr; |
182 | pte_t *pte; |
186 | pte_t *pte; |
183 | - | ||
184 | int pfcr; |
187 | int pfcr; |
- | 188 | as_t *as; |
|
- | 189 | bool lock; |
|
- | 190 | ||
- | 191 | if (AS == NULL) { |
|
- | 192 | as = AS_KERNEL; |
|
- | 193 | lock = false; |
|
- | 194 | } else { |
|
- | 195 | as = AS; |
|
- | 196 | lock = true; |
|
- | 197 | } |
|
185 | 198 | ||
186 | if (data) { |
199 | if (data) { |
187 | asm volatile ( |
200 | asm volatile ( |
188 | "mfdar %0\n" |
201 | "mfdar %0\n" |
189 | : "=r" (badvaddr) |
202 | : "=r" (badvaddr) |
190 | ); |
203 | ); |
191 | } else |
204 | } else |
192 | badvaddr = istate->pc; |
205 | badvaddr = istate->pc; |
193 | 206 | ||
194 | spinlock_lock(&AS->lock); |
207 | spinlock_lock(&as->lock); |
195 | asid = AS->asid; |
208 | asid = as->asid; |
196 | spinlock_unlock(&AS->lock); |
209 | spinlock_unlock(&as->lock); |
197 | 210 | ||
198 | page_table_lock(AS, true); |
211 | page_table_lock(as, lock); |
199 | 212 | ||
200 | pte = find_mapping_and_check(badvaddr, istate, &pfcr); |
213 | pte = find_mapping_and_check(as, lock, badvaddr, istate, &pfcr); |
201 | if (!pte) { |
214 | if (!pte) { |
202 | switch (pfcr) { |
215 | switch (pfcr) { |
203 | case AS_PF_FAULT: |
216 | case AS_PF_FAULT: |
204 | goto fail; |
217 | goto fail; |
205 | break; |
218 | break; |
206 | case AS_PF_DEFER: |
219 | case AS_PF_DEFER: |
207 | /* |
220 | /* |
208 | * The page fault came during copy_from_uspace() |
221 | * The page fault came during copy_from_uspace() |
209 | * or copy_to_uspace(). |
222 | * or copy_to_uspace(). |
210 | */ |
223 | */ |
211 | page_table_unlock(AS, true); |
224 | page_table_unlock(as, lock); |
212 | return; |
225 | return; |
213 | default: |
226 | default: |
214 | panic("Unexpected pfrc (%d)\n", pfcr); |
227 | panic("Unexpected pfrc (%d)\n", pfcr); |
215 | } |
228 | } |
216 | } |
229 | } |
217 | 230 | ||
218 | pte->a = 1; /* Record access to PTE */ |
231 | pte->a = 1; /* Record access to PTE */ |
219 | pht_insert(badvaddr, pte->pfn); |
232 | pht_insert(badvaddr, pte->pfn); |
220 | 233 | ||
221 | page_table_unlock(AS, true); |
234 | page_table_unlock(as, lock); |
222 | return; |
235 | return; |
223 | 236 | ||
224 | fail: |
237 | fail: |
225 | page_table_unlock(AS, true); |
238 | page_table_unlock(as, lock); |
226 | pht_refill_fail(badvaddr, istate); |
239 | pht_refill_fail(badvaddr, istate); |
227 | } |
240 | } |
228 | 241 | ||
229 | 242 | ||
230 | void pht_init(void) |
243 | void pht_init(void) |
231 | { |
244 | { |
232 | memsetb((__address) phte, 1 << PHT_BITS, 0); |
245 | memsetb((__address) phte, 1 << PHT_BITS, 0); |
233 | } |
246 | } |
234 | 247 | ||
235 | 248 | ||
236 | void page_arch_init(void) |
249 | void page_arch_init(void) |
237 | { |
250 | { |
238 | if (config.cpu_active == 1) { |
251 | if (config.cpu_active == 1) { |
239 | page_mapping_operations = &pt_mapping_operations; |
252 | page_mapping_operations = &pt_mapping_operations; |
240 | 253 | ||
241 | /* |
- | |
242 | * PA2KA(identity) mapping for all frames until last_frame. |
- | |
243 | */ |
- | |
244 | __address cur; |
- | |
245 | int flags; |
- | |
246 | - | ||
247 | for (cur = 0; cur < last_frame; cur += FRAME_SIZE) { |
- | |
248 | flags = PAGE_CACHEABLE; |
- | |
249 | if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size)) |
- | |
250 | flags |= PAGE_GLOBAL; |
- | |
251 | page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags); |
- | |
252 | } |
- | |
253 | - | ||
254 | /* Allocate page hash table */ |
254 | /* Allocate page hash table */ |
255 | phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC)); |
255 | phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC)); |
256 | phte = (phte_t *) PA2KA((__address) physical_phte); |
256 | phte = (phte_t *) PA2KA((__address) physical_phte); |
257 | 257 | ||
258 | ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0); |
258 | ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0); |
259 | pht_init(); |
259 | pht_init(); |
260 | 260 | ||
261 | asm volatile ( |
261 | asm volatile ( |
262 | "mtsdr1 %0\n" |
262 | "mtsdr1 %0\n" |
263 | : |
263 | : |
264 | : "r" ((__address) physical_phte) |
264 | : "r" ((__address) physical_phte) |
265 | ); |
265 | ); |
266 | } |
266 | } |
267 | } |
267 | } |
- | 268 | ||
- | 269 | ||
- | 270 | __address hw_map(__address physaddr, size_t size) |
|
- | 271 | { |
|
- | 272 | if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH)) |
|
- | 273 | panic("Unable to map physical memory %p (%d bytes)", physaddr, size) |
|
- | 274 | ||
- | 275 | __address virtaddr = PA2KA(last_frame); |
|
- | 276 | pfn_t i; |
|
- | 277 | for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++) |
|
- | 278 | page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE); |
|
- | 279 | ||
- | 280 | last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE); |
|
- | 281 | ||
- | 282 | return virtaddr; |
|
- | 283 | } |
|
268 | 284 |