1 | /*
|
---|
2 | * Copyright (c) 2006 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 ppc32mm
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch/mm/pht.h>
|
---|
36 | #include <arch/mm/tlb.h>
|
---|
37 | #include <interrupt.h>
|
---|
38 | #include <mm/as.h>
|
---|
39 | #include <mm/page.h>
|
---|
40 | #include <macros.h>
|
---|
41 | #include <typedefs.h>
|
---|
42 |
|
---|
43 | static unsigned int seed = 42;
|
---|
44 |
|
---|
45 | /** Try to find PTE for faulting address
|
---|
46 | *
|
---|
47 | * @param as Address space.
|
---|
48 | * @param badvaddr Faulting virtual address.
|
---|
49 | * @param access Access mode that caused the fault.
|
---|
50 | * @param istate Pointer to interrupted state.
|
---|
51 | * @param pfrc Pointer to variable where as_page_fault() return code
|
---|
52 | * will be stored.
|
---|
53 | *
|
---|
54 | * @return PTE on success, NULL otherwise.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | static pte_t *find_mapping_and_check(as_t *as, uintptr_t badvaddr, int access,
|
---|
58 | istate_t *istate, int *pfrc)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Check if the mapping exists in page tables.
|
---|
62 | */
|
---|
63 | pte_t *pte = page_mapping_find(as, badvaddr, true);
|
---|
64 | if ((pte) && (pte->present)) {
|
---|
65 | /*
|
---|
66 | * Mapping found in page tables.
|
---|
67 | * Immediately succeed.
|
---|
68 | */
|
---|
69 | return pte;
|
---|
70 | } else {
|
---|
71 | /*
|
---|
72 | * Mapping not found in page tables.
|
---|
73 | * Resort to higher-level page fault handler.
|
---|
74 | */
|
---|
75 | int rc = as_page_fault(badvaddr, access, istate);
|
---|
76 | switch (rc) {
|
---|
77 | case AS_PF_OK:
|
---|
78 | /*
|
---|
79 | * The higher-level page fault handler succeeded,
|
---|
80 | * The mapping ought to be in place.
|
---|
81 | */
|
---|
82 | pte = page_mapping_find(as, badvaddr, true);
|
---|
83 | ASSERT((pte) && (pte->present));
|
---|
84 | *pfrc = 0;
|
---|
85 | return pte;
|
---|
86 | case AS_PF_DEFER:
|
---|
87 | *pfrc = rc;
|
---|
88 | return NULL;
|
---|
89 | case AS_PF_FAULT:
|
---|
90 | *pfrc = rc;
|
---|
91 | return NULL;
|
---|
92 | default:
|
---|
93 | panic("Unexpected rc (%d).", rc);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void pht_refill_fail(uintptr_t badvaddr, istate_t *istate)
|
---|
99 | {
|
---|
100 | fault_if_from_uspace(istate, "PHT Refill Exception on %p.",
|
---|
101 | (void *) badvaddr);
|
---|
102 | panic_memtrap(istate, PF_ACCESS_UNKNOWN, badvaddr,
|
---|
103 | "PHT Refill Exception.");
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void pht_insert(const uintptr_t vaddr, const pte_t *pte)
|
---|
107 | {
|
---|
108 | uint32_t page = (vaddr >> 12) & 0xffff;
|
---|
109 | uint32_t api = (vaddr >> 22) & 0x3f;
|
---|
110 |
|
---|
111 | uint32_t vsid = sr_get(vaddr);
|
---|
112 | uint32_t sdr1 = sdr1_get();
|
---|
113 |
|
---|
114 | // FIXME: compute size of PHT exactly
|
---|
115 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
---|
116 |
|
---|
117 | /* Primary hash (xor) */
|
---|
118 | uint32_t h = 0;
|
---|
119 | uint32_t hash = vsid ^ page;
|
---|
120 | uint32_t base = (hash & 0x3ff) << 3;
|
---|
121 | uint32_t i;
|
---|
122 | bool found = false;
|
---|
123 |
|
---|
124 | /* Find colliding PTE in PTEG */
|
---|
125 | for (i = 0; i < 8; i++) {
|
---|
126 | if ((phte[base + i].v)
|
---|
127 | && (phte[base + i].vsid == vsid)
|
---|
128 | && (phte[base + i].api == api)
|
---|
129 | && (phte[base + i].h == 0)) {
|
---|
130 | found = true;
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!found) {
|
---|
136 | /* Find unused PTE in PTEG */
|
---|
137 | for (i = 0; i < 8; i++) {
|
---|
138 | if (!phte[base + i].v) {
|
---|
139 | found = true;
|
---|
140 | break;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (!found) {
|
---|
146 | /* Secondary hash (not) */
|
---|
147 | uint32_t base2 = (~hash & 0x3ff) << 3;
|
---|
148 |
|
---|
149 | /* Find colliding PTE in PTEG */
|
---|
150 | for (i = 0; i < 8; i++) {
|
---|
151 | if ((phte[base2 + i].v)
|
---|
152 | && (phte[base2 + i].vsid == vsid)
|
---|
153 | && (phte[base2 + i].api == api)
|
---|
154 | && (phte[base2 + i].h == 1)) {
|
---|
155 | found = true;
|
---|
156 | base = base2;
|
---|
157 | h = 1;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (!found) {
|
---|
163 | /* Find unused PTE in PTEG */
|
---|
164 | for (i = 0; i < 8; i++) {
|
---|
165 | if (!phte[base2 + i].v) {
|
---|
166 | found = true;
|
---|
167 | base = base2;
|
---|
168 | h = 1;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (!found)
|
---|
175 | i = RANDI(seed) % 8;
|
---|
176 | }
|
---|
177 |
|
---|
178 | phte[base + i].v = 1;
|
---|
179 | phte[base + i].vsid = vsid;
|
---|
180 | phte[base + i].h = h;
|
---|
181 | phte[base + i].api = api;
|
---|
182 | phte[base + i].rpn = pte->pfn;
|
---|
183 | phte[base + i].r = 0;
|
---|
184 | phte[base + i].c = 0;
|
---|
185 | phte[base + i].wimg = (pte->page_cache_disable ? WIMG_NO_CACHE : 0);
|
---|
186 | phte[base + i].pp = 2; // FIXME
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Process Instruction/Data Storage Exception
|
---|
190 | *
|
---|
191 | * @param n Exception vector number.
|
---|
192 | * @param istate Interrupted register context.
|
---|
193 | *
|
---|
194 | */
|
---|
195 | void pht_refill(unsigned int n, istate_t *istate)
|
---|
196 | {
|
---|
197 | uintptr_t badvaddr;
|
---|
198 |
|
---|
199 | if (n == VECTOR_DATA_STORAGE)
|
---|
200 | badvaddr = istate->dar;
|
---|
201 | else
|
---|
202 | badvaddr = istate->pc;
|
---|
203 |
|
---|
204 | int pfrc;
|
---|
205 | pte_t *pte = find_mapping_and_check(AS, badvaddr,
|
---|
206 | PF_ACCESS_READ /* FIXME */, istate, &pfrc);
|
---|
207 |
|
---|
208 | if (!pte) {
|
---|
209 | switch (pfrc) {
|
---|
210 | case AS_PF_FAULT:
|
---|
211 | pht_refill_fail(badvaddr, istate);
|
---|
212 | return;
|
---|
213 | case AS_PF_DEFER:
|
---|
214 | /*
|
---|
215 | * The page fault came during copy_from_uspace()
|
---|
216 | * or copy_to_uspace().
|
---|
217 | */
|
---|
218 | return;
|
---|
219 | default:
|
---|
220 | panic("Unexpected pfrc (%d).", pfrc);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* Record access to PTE */
|
---|
225 | pte->accessed = 1;
|
---|
226 | pht_insert(badvaddr, pte);
|
---|
227 | }
|
---|
228 |
|
---|
229 | void pht_invalidate(as_t *as, uintptr_t page, size_t pages)
|
---|
230 | {
|
---|
231 | uint32_t sdr1 = sdr1_get();
|
---|
232 |
|
---|
233 | // FIXME: compute size of PHT exactly
|
---|
234 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
---|
235 |
|
---|
236 | // FIXME: this invalidates all PHT entries,
|
---|
237 | // which is an overkill, invalidate only
|
---|
238 | // selectively
|
---|
239 | for (size_t i = 0; i < 8192; i++) {
|
---|
240 | phte[i].v = 0;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** @}
|
---|
245 | */
|
---|