Rev 1880 | Rev 1905 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
570 | jermar | 1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
||
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 | |||
1792 | jermar | 29 | /** @addtogroup sparc64mm |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
570 | jermar | 35 | #include <arch/mm/tlb.h> |
36 | #include <mm/tlb.h> |
||
1851 | jermar | 37 | #include <mm/as.h> |
38 | #include <mm/asid.h> |
||
619 | jermar | 39 | #include <arch/mm/frame.h> |
40 | #include <arch/mm/page.h> |
||
41 | #include <arch/mm/mmu.h> |
||
1851 | jermar | 42 | #include <arch/interrupt.h> |
1870 | jermar | 43 | #include <interrupt.h> |
1851 | jermar | 44 | #include <arch.h> |
570 | jermar | 45 | #include <print.h> |
617 | jermar | 46 | #include <arch/types.h> |
47 | #include <typedefs.h> |
||
619 | jermar | 48 | #include <config.h> |
630 | jermar | 49 | #include <arch/trap/trap.h> |
1880 | jermar | 50 | #include <arch/trap/exception.h> |
863 | jermar | 51 | #include <panic.h> |
873 | jermar | 52 | #include <arch/asm.h> |
894 | jermar | 53 | |
1891 | jermar | 54 | #ifdef CONFIG_TSB |
55 | #include <arch/mm/tsb.h> |
||
56 | #endif |
||
57 | |||
1852 | jermar | 58 | static void dtlb_pte_copy(pte_t *t, bool ro); |
59 | static void itlb_pte_copy(pte_t *t); |
||
60 | static void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, const char *str); |
||
1865 | jermar | 61 | static void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str); |
62 | static void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str); |
||
1851 | jermar | 63 | |
873 | jermar | 64 | char *context_encoding[] = { |
65 | "Primary", |
||
66 | "Secondary", |
||
67 | "Nucleus", |
||
68 | "Reserved" |
||
69 | }; |
||
70 | |||
570 | jermar | 71 | void tlb_arch_init(void) |
72 | { |
||
1793 | jermar | 73 | /* |
1842 | jermar | 74 | * TLBs are actually initialized early |
1793 | jermar | 75 | * in start.S. |
76 | */ |
||
897 | jermar | 77 | } |
873 | jermar | 78 | |
897 | jermar | 79 | /** Insert privileged mapping into DMMU TLB. |
80 | * |
||
81 | * @param page Virtual page address. |
||
82 | * @param frame Physical frame address. |
||
83 | * @param pagesize Page size. |
||
84 | * @param locked True for permanent mappings, false otherwise. |
||
85 | * @param cacheable True if the mapping is cacheable, false otherwise. |
||
86 | */ |
||
1780 | jermar | 87 | void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize, bool locked, bool cacheable) |
897 | jermar | 88 | { |
89 | tlb_tag_access_reg_t tag; |
||
90 | tlb_data_t data; |
||
91 | page_address_t pg; |
||
92 | frame_address_t fr; |
||
873 | jermar | 93 | |
897 | jermar | 94 | pg.address = page; |
95 | fr.address = frame; |
||
873 | jermar | 96 | |
894 | jermar | 97 | tag.value = ASID_KERNEL; |
98 | tag.vpn = pg.vpn; |
||
99 | |||
100 | dtlb_tag_access_write(tag.value); |
||
101 | |||
102 | data.value = 0; |
||
103 | data.v = true; |
||
897 | jermar | 104 | data.size = pagesize; |
894 | jermar | 105 | data.pfn = fr.pfn; |
897 | jermar | 106 | data.l = locked; |
107 | data.cp = cacheable; |
||
108 | data.cv = cacheable; |
||
894 | jermar | 109 | data.p = true; |
110 | data.w = true; |
||
1868 | jermar | 111 | data.g = false; |
894 | jermar | 112 | |
113 | dtlb_data_in_write(data.value); |
||
570 | jermar | 114 | } |
115 | |||
1852 | jermar | 116 | /** Copy PTE to TLB. |
117 | * |
||
118 | * @param t Page Table Entry to be copied. |
||
119 | * @param ro If true, the entry will be created read-only, regardless of its w field. |
||
120 | */ |
||
121 | void dtlb_pte_copy(pte_t *t, bool ro) |
||
1851 | jermar | 122 | { |
1852 | jermar | 123 | tlb_tag_access_reg_t tag; |
124 | tlb_data_t data; |
||
125 | page_address_t pg; |
||
126 | frame_address_t fr; |
||
127 | |||
128 | pg.address = t->page; |
||
129 | fr.address = t->frame; |
||
130 | |||
131 | tag.value = 0; |
||
132 | tag.context = t->as->asid; |
||
133 | tag.vpn = pg.vpn; |
||
134 | |||
135 | dtlb_tag_access_write(tag.value); |
||
136 | |||
137 | data.value = 0; |
||
138 | data.v = true; |
||
139 | data.size = PAGESIZE_8K; |
||
140 | data.pfn = fr.pfn; |
||
141 | data.l = false; |
||
142 | data.cp = t->c; |
||
143 | data.cv = t->c; |
||
1864 | jermar | 144 | data.p = t->k; /* p like privileged */ |
1852 | jermar | 145 | data.w = ro ? false : t->w; |
146 | data.g = t->g; |
||
147 | |||
148 | dtlb_data_in_write(data.value); |
||
1851 | jermar | 149 | } |
150 | |||
1891 | jermar | 151 | /** Copy PTE to ITLB. |
152 | * |
||
153 | * @param t Page Table Entry to be copied. |
||
154 | */ |
||
1852 | jermar | 155 | void itlb_pte_copy(pte_t *t) |
156 | { |
||
157 | tlb_tag_access_reg_t tag; |
||
158 | tlb_data_t data; |
||
159 | page_address_t pg; |
||
160 | frame_address_t fr; |
||
161 | |||
162 | pg.address = t->page; |
||
163 | fr.address = t->frame; |
||
164 | |||
165 | tag.value = 0; |
||
166 | tag.context = t->as->asid; |
||
167 | tag.vpn = pg.vpn; |
||
168 | |||
169 | itlb_tag_access_write(tag.value); |
||
170 | |||
171 | data.value = 0; |
||
172 | data.v = true; |
||
173 | data.size = PAGESIZE_8K; |
||
174 | data.pfn = fr.pfn; |
||
175 | data.l = false; |
||
176 | data.cp = t->c; |
||
177 | data.cv = t->c; |
||
1864 | jermar | 178 | data.p = t->k; /* p like privileged */ |
1852 | jermar | 179 | data.w = false; |
180 | data.g = t->g; |
||
181 | |||
182 | itlb_data_in_write(data.value); |
||
183 | } |
||
184 | |||
863 | jermar | 185 | /** ITLB miss handler. */ |
1851 | jermar | 186 | void fast_instruction_access_mmu_miss(int n, istate_t *istate) |
863 | jermar | 187 | { |
1852 | jermar | 188 | uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE); |
189 | pte_t *t; |
||
190 | |||
191 | page_table_lock(AS, true); |
||
192 | t = page_mapping_find(AS, va); |
||
193 | if (t && PTE_EXECUTABLE(t)) { |
||
194 | /* |
||
195 | * The mapping was found in the software page hash table. |
||
196 | * Insert it into ITLB. |
||
197 | */ |
||
198 | t->a = true; |
||
199 | itlb_pte_copy(t); |
||
1891 | jermar | 200 | #ifdef CONFIG_TSB |
201 | itsb_pte_copy(t); |
||
202 | #endif |
||
1852 | jermar | 203 | page_table_unlock(AS, true); |
204 | } else { |
||
205 | /* |
||
206 | * Forward the page fault to the address space page fault handler. |
||
207 | */ |
||
208 | page_table_unlock(AS, true); |
||
209 | if (as_page_fault(va, PF_ACCESS_EXEC, istate) == AS_PF_FAULT) { |
||
210 | do_fast_instruction_access_mmu_miss_fault(istate, __FUNCTION__); |
||
211 | } |
||
212 | } |
||
863 | jermar | 213 | } |
214 | |||
1851 | jermar | 215 | /** DTLB miss handler. |
216 | * |
||
217 | * Note that some faults (e.g. kernel faults) were already resolved |
||
218 | * by the low-level, assembly language part of the fast_data_access_mmu_miss |
||
219 | * handler. |
||
220 | */ |
||
221 | void fast_data_access_mmu_miss(int n, istate_t *istate) |
||
863 | jermar | 222 | { |
877 | jermar | 223 | tlb_tag_access_reg_t tag; |
1851 | jermar | 224 | uintptr_t va; |
225 | pte_t *t; |
||
883 | jermar | 226 | |
877 | jermar | 227 | tag.value = dtlb_tag_access_read(); |
1865 | jermar | 228 | va = tag.vpn << PAGE_WIDTH; |
229 | |||
1851 | jermar | 230 | if (tag.context == ASID_KERNEL) { |
231 | if (!tag.vpn) { |
||
232 | /* NULL access in kernel */ |
||
1865 | jermar | 233 | do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__); |
1851 | jermar | 234 | } |
1865 | jermar | 235 | do_fast_data_access_mmu_miss_fault(istate, tag, "Unexpected kernel page fault."); |
1851 | jermar | 236 | } |
873 | jermar | 237 | |
1851 | jermar | 238 | page_table_lock(AS, true); |
239 | t = page_mapping_find(AS, va); |
||
240 | if (t) { |
||
241 | /* |
||
242 | * The mapping was found in the software page hash table. |
||
243 | * Insert it into DTLB. |
||
244 | */ |
||
1852 | jermar | 245 | t->a = true; |
246 | dtlb_pte_copy(t, true); |
||
1891 | jermar | 247 | #ifdef CONFIG_TSB |
248 | dtsb_pte_copy(t, true); |
||
249 | #endif |
||
1851 | jermar | 250 | page_table_unlock(AS, true); |
251 | } else { |
||
252 | /* |
||
253 | * Forward the page fault to the address space page fault handler. |
||
254 | */ |
||
255 | page_table_unlock(AS, true); |
||
256 | if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) { |
||
1865 | jermar | 257 | do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__); |
1851 | jermar | 258 | } |
877 | jermar | 259 | } |
863 | jermar | 260 | } |
261 | |||
262 | /** DTLB protection fault handler. */ |
||
1851 | jermar | 263 | void fast_data_access_protection(int n, istate_t *istate) |
863 | jermar | 264 | { |
1859 | jermar | 265 | tlb_tag_access_reg_t tag; |
266 | uintptr_t va; |
||
267 | pte_t *t; |
||
268 | |||
269 | tag.value = dtlb_tag_access_read(); |
||
1865 | jermar | 270 | va = tag.vpn << PAGE_WIDTH; |
1859 | jermar | 271 | |
272 | page_table_lock(AS, true); |
||
273 | t = page_mapping_find(AS, va); |
||
274 | if (t && PTE_WRITABLE(t)) { |
||
275 | /* |
||
276 | * The mapping was found in the software page hash table and is writable. |
||
277 | * Demap the old mapping and insert an updated mapping into DTLB. |
||
278 | */ |
||
279 | t->a = true; |
||
280 | t->d = true; |
||
281 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY, va); |
||
282 | dtlb_pte_copy(t, false); |
||
1891 | jermar | 283 | #ifdef CONFIG_TSB |
284 | dtsb_pte_copy(t, false); |
||
285 | #endif |
||
1859 | jermar | 286 | page_table_unlock(AS, true); |
287 | } else { |
||
288 | /* |
||
289 | * Forward the page fault to the address space page fault handler. |
||
290 | */ |
||
291 | page_table_unlock(AS, true); |
||
292 | if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) { |
||
1865 | jermar | 293 | do_fast_data_access_protection_fault(istate, tag, __FUNCTION__); |
1859 | jermar | 294 | } |
295 | } |
||
863 | jermar | 296 | } |
297 | |||
570 | jermar | 298 | /** Print contents of both TLBs. */ |
299 | void tlb_print(void) |
||
300 | { |
||
301 | int i; |
||
302 | tlb_data_t d; |
||
303 | tlb_tag_read_reg_t t; |
||
304 | |||
305 | printf("I-TLB contents:\n"); |
||
306 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
307 | d.value = itlb_data_access_read(i); |
||
613 | jermar | 308 | t.value = itlb_tag_read_read(i); |
570 | jermar | 309 | |
1735 | decky | 310 | printf("%d: vpn=%#llx, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%#x, diag=%#x, pfn=%#x, soft=%#x, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
617 | jermar | 311 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
570 | jermar | 312 | } |
313 | |||
314 | printf("D-TLB contents:\n"); |
||
315 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
316 | d.value = dtlb_data_access_read(i); |
||
613 | jermar | 317 | t.value = dtlb_tag_read_read(i); |
570 | jermar | 318 | |
1735 | decky | 319 | printf("%d: vpn=%#llx, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%#x, diag=%#x, pfn=%#x, soft=%#x, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
617 | jermar | 320 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
570 | jermar | 321 | } |
322 | |||
323 | } |
||
617 | jermar | 324 | |
1852 | jermar | 325 | void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, const char *str) |
326 | { |
||
1870 | jermar | 327 | fault_if_from_uspace(istate, "%s\n", str); |
1880 | jermar | 328 | dump_istate(istate); |
1852 | jermar | 329 | panic("%s\n", str); |
330 | } |
||
331 | |||
1865 | jermar | 332 | void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str) |
1851 | jermar | 333 | { |
334 | uintptr_t va; |
||
335 | |||
1865 | jermar | 336 | va = tag.vpn << PAGE_WIDTH; |
1851 | jermar | 337 | |
1870 | jermar | 338 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va, tag.context); |
1880 | jermar | 339 | dump_istate(istate); |
1851 | jermar | 340 | printf("Faulting page: %p, ASID=%d\n", va, tag.context); |
341 | panic("%s\n", str); |
||
342 | } |
||
343 | |||
1865 | jermar | 344 | void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str) |
1859 | jermar | 345 | { |
346 | uintptr_t va; |
||
347 | |||
1865 | jermar | 348 | va = tag.vpn << PAGE_WIDTH; |
1859 | jermar | 349 | |
1870 | jermar | 350 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va, tag.context); |
1859 | jermar | 351 | printf("Faulting page: %p, ASID=%d\n", va, tag.context); |
1880 | jermar | 352 | dump_istate(istate); |
1859 | jermar | 353 | panic("%s\n", str); |
354 | } |
||
355 | |||
617 | jermar | 356 | /** Invalidate all unlocked ITLB and DTLB entries. */ |
357 | void tlb_invalidate_all(void) |
||
358 | { |
||
359 | int i; |
||
360 | tlb_data_t d; |
||
361 | tlb_tag_read_reg_t t; |
||
362 | |||
363 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
364 | d.value = itlb_data_access_read(i); |
||
365 | if (!d.l) { |
||
366 | t.value = itlb_tag_read_read(i); |
||
367 | d.v = false; |
||
368 | itlb_tag_access_write(t.value); |
||
369 | itlb_data_access_write(i, d.value); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
374 | d.value = dtlb_data_access_read(i); |
||
375 | if (!d.l) { |
||
376 | t.value = dtlb_tag_read_read(i); |
||
377 | d.v = false; |
||
378 | dtlb_tag_access_write(t.value); |
||
379 | dtlb_data_access_write(i, d.value); |
||
380 | } |
||
381 | } |
||
382 | |||
383 | } |
||
384 | |||
385 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context). |
||
386 | * |
||
387 | * @param asid Address Space ID. |
||
388 | */ |
||
389 | void tlb_invalidate_asid(asid_t asid) |
||
390 | { |
||
1865 | jermar | 391 | tlb_context_reg_t pc_save, ctx; |
1860 | jermar | 392 | |
1865 | jermar | 393 | /* switch to nucleus because we are mapped by the primary context */ |
394 | nucleus_enter(); |
||
395 | |||
396 | ctx.v = pc_save.v = mmu_primary_context_read(); |
||
1860 | jermar | 397 | ctx.context = asid; |
1865 | jermar | 398 | mmu_primary_context_write(ctx.v); |
1860 | jermar | 399 | |
1865 | jermar | 400 | itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0); |
401 | dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0); |
||
1860 | jermar | 402 | |
1865 | jermar | 403 | mmu_primary_context_write(pc_save.v); |
404 | |||
405 | nucleus_leave(); |
||
617 | jermar | 406 | } |
407 | |||
727 | jermar | 408 | /** Invalidate all ITLB and DTLB entries for specified page range in specified address space. |
617 | jermar | 409 | * |
410 | * @param asid Address Space ID. |
||
727 | jermar | 411 | * @param page First page which to sweep out from ITLB and DTLB. |
412 | * @param cnt Number of ITLB and DTLB entries to invalidate. |
||
617 | jermar | 413 | */ |
1780 | jermar | 414 | void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt) |
617 | jermar | 415 | { |
727 | jermar | 416 | int i; |
1865 | jermar | 417 | tlb_context_reg_t pc_save, ctx; |
727 | jermar | 418 | |
1865 | jermar | 419 | /* switch to nucleus because we are mapped by the primary context */ |
420 | nucleus_enter(); |
||
421 | |||
422 | ctx.v = pc_save.v = mmu_primary_context_read(); |
||
1860 | jermar | 423 | ctx.context = asid; |
1865 | jermar | 424 | mmu_primary_context_write(ctx.v); |
1860 | jermar | 425 | |
727 | jermar | 426 | for (i = 0; i < cnt; i++) { |
1865 | jermar | 427 | itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE); |
428 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE); |
||
727 | jermar | 429 | } |
1860 | jermar | 430 | |
1865 | jermar | 431 | mmu_primary_context_write(pc_save.v); |
432 | |||
433 | nucleus_leave(); |
||
617 | jermar | 434 | } |
1702 | cejka | 435 | |
1792 | jermar | 436 | /** @} |
1702 | cejka | 437 | */ |