source: mainline/kernel/arch/sparc64/src/mm/sun4v/tlb.c@ 1f5714e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1f5714e was 9aed144, checked in by Jakub Jermar <jakub@…>, 14 years ago

Finish transition to lock-free page_mapping_find() in TLB miss handlers.

  • Simply drop the calls to page_table_lock/unlock() in ia64 and sparc64 TLB miss handlers.

As for why this is possible:

  • page_mapping_find() cannot race with page_table_remove() because page_mapping_find() is called only when interrupts are disabled, which prevents TLB shootdown from starting, which in turn prevents page_table_remove() from running
  • page_mapping_insert() will not negatively interfere with page_mapping_find() because page_mapping_insert() adds a fully initialized pte_t to the end of the hash bucket
  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*
2 * Copyright (c) 2005 Jakub Jermar
3 * Copyright (c) 2008 Pavel Rimsky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup sparc64mm
31 * @{
32 */
33/** @file
34 */
35
36#include <mm/tlb.h>
37#include <mm/as.h>
38#include <mm/asid.h>
39#include <arch/sun4v/hypercall.h>
40#include <arch/mm/frame.h>
41#include <arch/mm/page.h>
42#include <arch/mm/tte.h>
43#include <arch/mm/tlb.h>
44#include <arch/interrupt.h>
45#include <interrupt.h>
46#include <arch.h>
47#include <print.h>
48#include <typedefs.h>
49#include <config.h>
50#include <arch/trap/trap.h>
51#include <arch/trap/exception.h>
52#include <panic.h>
53#include <arch/asm.h>
54#include <arch/cpu.h>
55#include <arch/mm/pagesize.h>
56#include <genarch/mm/page_ht.h>
57
58#ifdef CONFIG_TSB
59#include <arch/mm/tsb.h>
60#endif
61
62static void itlb_pte_copy(pte_t *);
63static void dtlb_pte_copy(pte_t *, bool);
64static void do_fast_instruction_access_mmu_miss_fault(istate_t *, uintptr_t,
65 const char *);
66static void do_fast_data_access_mmu_miss_fault(istate_t *, uint64_t,
67 const char *);
68static void do_fast_data_access_protection_fault(istate_t *,
69 uint64_t, const char *);
70
71/*
72 * The assembly language routine passes a 64-bit parameter to the Data Access
73 * MMU Miss and Data Access protection handlers, the parameter encapsulates
74 * a virtual address of the faulting page and the faulting context. The most
75 * significant 51 bits represent the VA of the faulting page and the least
76 * significant 13 vits represent the faulting context. The following macros
77 * extract the page and context out of the 64-bit parameter:
78 */
79
80/* extracts the VA of the faulting page */
81#define DMISS_ADDRESS(page_and_ctx) (((page_and_ctx) >> 13) << 13)
82
83/* extracts the faulting context */
84#define DMISS_CONTEXT(page_and_ctx) ((page_and_ctx) & 0x1fff)
85
86/**
87 * Descriptions of fault types from the MMU Fault status area.
88 *
89 * fault_type[i] contains description of error for which the IFT or DFT
90 * field of the MMU fault status area is i.
91 */
92static const char *fault_types[] = {
93 "unknown",
94 "fast miss",
95 "fast protection",
96 "MMU miss",
97 "invalid RA",
98 "privileged violation",
99 "protection violation",
100 "NFO access",
101 "so page/NFO side effect",
102 "invalid VA",
103 "invalid ASI",
104 "nc atomic",
105 "privileged action",
106 "unknown",
107 "unaligned access",
108 "invalid page size"
109 };
110
111/** Array of MMU fault status areas. */
112extern mmu_fault_status_area_t mmu_fsas[MAX_NUM_STRANDS];
113
114/*
115 * Invalidate all non-locked DTLB and ITLB entries.
116 */
117void tlb_arch_init(void)
118{
119 tlb_invalidate_all();
120}
121
122/** Insert privileged mapping into DMMU TLB.
123 *
124 * @param page Virtual page address.
125 * @param frame Physical frame address.
126 * @param pagesize Page size.
127 * @param locked True for permanent mappings, false otherwise.
128 * @param cacheable True if the mapping is cacheable, false otherwise.
129 */
130void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
131 bool locked, bool cacheable)
132{
133 tte_data_t data;
134
135 data.value = 0;
136 data.v = true;
137 data.nfo = false;
138 data.ra = frame >> FRAME_WIDTH;
139 data.ie = false;
140 data.e = false;
141 data.cp = cacheable;
142#ifdef CONFIG_VIRT_IDX_DCACHE
143 data.cv = cacheable;
144#endif
145 data.p = true;
146 data.x = false;
147 data.w = true;
148 data.size = pagesize;
149
150 if (locked) {
151 __hypercall_fast4(
152 MMU_MAP_PERM_ADDR, page, 0, data.value, MMU_FLAG_DTLB);
153 } else {
154 __hypercall_hyperfast(
155 page, ASID_KERNEL, data.value, MMU_FLAG_DTLB, 0,
156 MMU_MAP_ADDR);
157 }
158}
159
160/** Copy PTE to TLB.
161 *
162 * @param t Page Table Entry to be copied.
163 * @param ro If true, the entry will be created read-only, regardless
164 * of its w field.
165 */
166void dtlb_pte_copy(pte_t *t, bool ro)
167{
168 tte_data_t data;
169
170 data.value = 0;
171 data.v = true;
172 data.nfo = false;
173 data.ra = (t->frame) >> FRAME_WIDTH;
174 data.ie = false;
175 data.e = false;
176 data.cp = t->c;
177#ifdef CONFIG_VIRT_IDX_DCACHE
178 data.cv = t->c;
179#endif
180 data.p = t->k;
181 data.x = false;
182 data.w = ro ? false : t->w;
183 data.size = PAGESIZE_8K;
184
185 __hypercall_hyperfast(
186 t->page, t->as->asid, data.value, MMU_FLAG_DTLB, 0, MMU_MAP_ADDR);
187}
188
189/** Copy PTE to ITLB.
190 *
191 * @param t Page Table Entry to be copied.
192 */
193void itlb_pte_copy(pte_t *t)
194{
195 tte_data_t data;
196
197 data.value = 0;
198 data.v = true;
199 data.nfo = false;
200 data.ra = (t->frame) >> FRAME_WIDTH;
201 data.ie = false;
202 data.e = false;
203 data.cp = t->c;
204 data.cv = false;
205 data.p = t->k;
206 data.x = true;
207 data.w = false;
208 data.size = PAGESIZE_8K;
209
210 __hypercall_hyperfast(
211 t->page, t->as->asid, data.value, MMU_FLAG_ITLB, 0, MMU_MAP_ADDR);
212}
213
214/** ITLB miss handler. */
215void fast_instruction_access_mmu_miss(sysarg_t unused, istate_t *istate)
216{
217 uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
218 pte_t *t;
219
220 t = page_mapping_find(AS, va, true);
221
222 if (t && PTE_EXECUTABLE(t)) {
223 /*
224 * The mapping was found in the software page hash table.
225 * Insert it into ITLB.
226 */
227 t->a = true;
228 itlb_pte_copy(t);
229#ifdef CONFIG_TSB
230 itsb_pte_copy(t);
231#endif
232 } else {
233 /*
234 * Forward the page fault to the address space page fault
235 * handler.
236 */
237 if (as_page_fault(va, PF_ACCESS_EXEC, istate) == AS_PF_FAULT) {
238 do_fast_instruction_access_mmu_miss_fault(istate,
239 istate->tpc, __func__);
240 }
241 }
242}
243
244/** DTLB miss handler.
245 *
246 * Note that some faults (e.g. kernel faults) were already resolved by the
247 * low-level, assembly language part of the fast_data_access_mmu_miss handler.
248 *
249 * @param page_and_ctx A 64-bit value describing the fault. The most
250 * significant 51 bits of the value contain the virtual
251 * address which caused the fault truncated to the page
252 * boundary. The least significant 13 bits of the value
253 * contain the number of the context in which the fault
254 * occurred.
255 * @param istate Interrupted state saved on the stack.
256 */
257void fast_data_access_mmu_miss(uint64_t page_and_ctx, istate_t *istate)
258{
259 pte_t *t;
260 uintptr_t va = DMISS_ADDRESS(page_and_ctx);
261 uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
262
263 if (ctx == ASID_KERNEL) {
264 if (va == 0) {
265 /* NULL access in kernel */
266 do_fast_data_access_mmu_miss_fault(istate, page_and_ctx,
267 __func__);
268 }
269 do_fast_data_access_mmu_miss_fault(istate, page_and_ctx, "Unexpected "
270 "kernel page fault.");
271 }
272
273 t = page_mapping_find(AS, va, true);
274 if (t) {
275 /*
276 * The mapping was found in the software page hash table.
277 * Insert it into DTLB.
278 */
279 t->a = true;
280 dtlb_pte_copy(t, true);
281#ifdef CONFIG_TSB
282 dtsb_pte_copy(t, true);
283#endif
284 } else {
285 /*
286 * Forward the page fault to the address space page fault
287 * handler.
288 */
289 if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) {
290 do_fast_data_access_mmu_miss_fault(istate, page_and_ctx,
291 __func__);
292 }
293 }
294}
295
296/** DTLB protection fault handler.
297 *
298 * @param page_and_ctx A 64-bit value describing the fault. The most
299 * significant 51 bits of the value contain the virtual
300 * address which caused the fault truncated to the page
301 * boundary. The least significant 13 bits of the value
302 * contain the number of the context in which the fault
303 * occurred.
304 * @param istate Interrupted state saved on the stack.
305 */
306void fast_data_access_protection(uint64_t page_and_ctx, istate_t *istate)
307{
308 pte_t *t;
309 uintptr_t va = DMISS_ADDRESS(page_and_ctx);
310 uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
311
312 t = page_mapping_find(AS, va, true);
313 if (t && PTE_WRITABLE(t)) {
314 /*
315 * The mapping was found in the software page hash table and is
316 * writable. Demap the old mapping and insert an updated mapping
317 * into DTLB.
318 */
319 t->a = true;
320 t->d = true;
321 mmu_demap_page(va, ctx, MMU_FLAG_DTLB);
322 dtlb_pte_copy(t, false);
323#ifdef CONFIG_TSB
324 dtsb_pte_copy(t, false);
325#endif
326 } else {
327 /*
328 * Forward the page fault to the address space page fault
329 * handler.
330 */
331 if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) {
332 do_fast_data_access_protection_fault(istate, page_and_ctx,
333 __func__);
334 }
335 }
336}
337
338/*
339 * On Niagara this function does not work, as supervisor software is isolated
340 * from the TLB by the hypervisor and has no chance to investigate the TLB
341 * entries.
342 */
343void tlb_print(void)
344{
345 printf("Operation not possible on Niagara.\n");
346}
347
348void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, uintptr_t va,
349 const char *str)
350{
351 fault_if_from_uspace(istate, "%s, address=%p.", str,
352 (void *) va);
353 panic_memtrap(istate, PF_ACCESS_EXEC, va, str);
354}
355
356void do_fast_data_access_mmu_miss_fault(istate_t *istate,
357 uint64_t page_and_ctx, const char *str)
358{
359 fault_if_from_uspace(istate, "%s, page=%p (asid=%" PRId64 ").", str,
360 (void *) DMISS_ADDRESS(page_and_ctx), DMISS_CONTEXT(page_and_ctx));
361 panic_memtrap(istate, PF_ACCESS_UNKNOWN, DMISS_ADDRESS(page_and_ctx),
362 str);
363}
364
365void do_fast_data_access_protection_fault(istate_t *istate,
366 uint64_t page_and_ctx, const char *str)
367{
368 fault_if_from_uspace(istate, "%s, page=%p (asid=%" PRId64 ").", str,
369 (void *) DMISS_ADDRESS(page_and_ctx), DMISS_CONTEXT(page_and_ctx));
370 panic_memtrap(istate, PF_ACCESS_WRITE, DMISS_ADDRESS(page_and_ctx),
371 str);
372}
373
374/**
375 * Describes the exact condition which caused the last DMMU fault.
376 */
377void describe_dmmu_fault(void)
378{
379 uint64_t myid;
380 __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
381
382 ASSERT(mmu_fsas[myid].dft < 16);
383
384 printf("condition which caused the fault: %s\n",
385 fault_types[mmu_fsas[myid].dft]);
386}
387
388/** Invalidate all unlocked ITLB and DTLB entries. */
389void tlb_invalidate_all(void)
390{
391 uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
392 MMU_FLAG_DTLB | MMU_FLAG_ITLB);
393 if (errno != HV_EOK)
394 panic("Error code = %" PRIu64 ".\n", errno);
395}
396
397/** Invalidate all ITLB and DTLB entries that belong to specified ASID
398 * (Context).
399 *
400 * @param asid Address Space ID.
401 */
402void tlb_invalidate_asid(asid_t asid)
403{
404 /* switch to nucleus because we are mapped by the primary context */
405 nucleus_enter();
406
407 __hypercall_fast4(MMU_DEMAP_CTX, 0, 0, asid,
408 MMU_FLAG_ITLB | MMU_FLAG_DTLB);
409
410 nucleus_leave();
411}
412
413/** Invalidate all ITLB and DTLB entries for specified page range in specified
414 * address space.
415 *
416 * @param asid Address Space ID.
417 * @param page First page which to sweep out from ITLB and DTLB.
418 * @param cnt Number of ITLB and DTLB entries to invalidate.
419 */
420void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
421{
422 unsigned int i;
423
424 /* switch to nucleus because we are mapped by the primary context */
425 nucleus_enter();
426
427 for (i = 0; i < cnt; i++) {
428 __hypercall_fast5(MMU_DEMAP_PAGE, 0, 0, page, asid,
429 MMU_FLAG_DTLB | MMU_FLAG_ITLB);
430 }
431
432 nucleus_leave();
433}
434
435/** @}
436 */
Note: See TracBrowser for help on using the repository browser.