source: mainline/kernel/arch/sparc64/src/mm/sun4v/tlb.c@ 346b12a2

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

Add page_mapping_update()

page_mapping_update() can be used to safely update the accessed and dirty
bits of a PTE in the actual page tables.

  • Property mode set to 100644
File size: 9.9 KB
RevLine 
[0d04024]1/*
[df4ed85]2 * Copyright (c) 2005 Jakub Jermar
[3da11f37]3 * Copyright (c) 2008 Pavel Rimsky
[0d04024]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
[7008097]30/** @addtogroup sparc64mm
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[0d04024]36#include <mm/tlb.h>
[f47fd19]37#include <mm/as.h>
38#include <mm/asid.h>
[b4655da]39#include <arch/sun4v/hypercall.h>
[0cfc4d38]40#include <arch/mm/frame.h>
41#include <arch/mm/page.h>
[b4655da]42#include <arch/mm/tte.h>
43#include <arch/mm/tlb.h>
[f47fd19]44#include <arch/interrupt.h>
[e2bf639]45#include <interrupt.h>
[f47fd19]46#include <arch.h>
[0d04024]47#include <print.h>
[b2fa1204]48#include <log.h>
[d99c1d2]49#include <typedefs.h>
[0cfc4d38]50#include <config.h>
[49b6d32]51#include <arch/trap/trap.h>
[7bb6b06]52#include <arch/trap/exception.h>
[008029d]53#include <panic.h>
[b6fba84]54#include <arch/asm.h>
[3da11f37]55#include <arch/cpu.h>
56#include <arch/mm/pagesize.h>
[387416b]57#include <genarch/mm/page_ht.h>
[02f441c0]58
[29b2bbf]59#ifdef CONFIG_TSB
60#include <arch/mm/tsb.h>
61#endif
62
[ba50a34]63static void itlb_pte_copy(pte_t *);
[77f65df]64static void dtlb_pte_copy(pte_t *, bool);
[5e53e02]65
66/*
67 * The assembly language routine passes a 64-bit parameter to the Data Access
68 * MMU Miss and Data Access protection handlers, the parameter encapsulates
69 * a virtual address of the faulting page and the faulting context. The most
70 * significant 51 bits represent the VA of the faulting page and the least
71 * significant 13 vits represent the faulting context. The following macros
72 * extract the page and context out of the 64-bit parameter:
73 */
74
75/* extracts the VA of the faulting page */
76#define DMISS_ADDRESS(page_and_ctx) (((page_and_ctx) >> 13) << 13)
77
78/* extracts the faulting context */
79#define DMISS_CONTEXT(page_and_ctx) ((page_and_ctx) & 0x1fff)
[f47fd19]80
[77f65df]81/**
82 * Descriptions of fault types from the MMU Fault status area.
83 *
84 * fault_type[i] contains description of error for which the IFT or DFT
85 * field of the MMU fault status area is i.
86 */
[a000878c]87static const char *fault_types[] = {
[77f65df]88 "unknown",
89 "fast miss",
90 "fast protection",
91 "MMU miss",
92 "invalid RA",
93 "privileged violation",
94 "protection violation",
95 "NFO access",
96 "so page/NFO side effect",
97 "invalid VA",
98 "invalid ASI",
99 "nc atomic",
100 "privileged action",
101 "unknown",
102 "unaligned access",
103 "invalid page size"
104 };
[0d04024]105
[77f65df]106/** Array of MMU fault status areas. */
107extern mmu_fault_status_area_t mmu_fsas[MAX_NUM_STRANDS];
[b4655da]108
[77f65df]109/*
110 * Invalidate all non-locked DTLB and ITLB entries.
111 */
[0d04024]112void tlb_arch_init(void)
113{
[b4655da]114 tlb_invalidate_all();
[97f1691]115}
[b6fba84]116
[97f1691]117/** Insert privileged mapping into DMMU TLB.
118 *
[965dc18]119 * @param page Virtual page address.
120 * @param frame Physical frame address.
121 * @param pagesize Page size.
122 * @param locked True for permanent mappings, false otherwise.
123 * @param cacheable True if the mapping is cacheable, false otherwise.
[97f1691]124 */
[2057572]125void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
126 bool locked, bool cacheable)
[97f1691]127{
[77f65df]128 tte_data_t data;
129
[02f441c0]130 data.value = 0;
131 data.v = true;
[77f65df]132 data.nfo = false;
133 data.ra = frame >> FRAME_WIDTH;
134 data.ie = false;
135 data.e = false;
[97f1691]136 data.cp = cacheable;
[92778f2]137#ifdef CONFIG_VIRT_IDX_DCACHE
[97f1691]138 data.cv = cacheable;
[77f65df]139#endif
[02f441c0]140 data.p = true;
[77f65df]141 data.x = false;
[02f441c0]142 data.w = true;
[77f65df]143 data.size = pagesize;
144
145 if (locked) {
146 __hypercall_fast4(
147 MMU_MAP_PERM_ADDR, page, 0, data.value, MMU_FLAG_DTLB);
148 } else {
149 __hypercall_hyperfast(
150 page, ASID_KERNEL, data.value, MMU_FLAG_DTLB, 0,
151 MMU_MAP_ADDR);
152 }
[0d04024]153}
154
[a7961271]155/** Copy PTE to TLB.
156 *
[965dc18]157 * @param t Page Table Entry to be copied.
158 * @param ro If true, the entry will be created read-only, regardless
159 * of its w field.
[a7961271]160 */
[5e53e02]161void dtlb_pte_copy(pte_t *t, bool ro)
[a7961271]162{
[5e53e02]163 tte_data_t data;
164
[a7961271]165 data.value = 0;
166 data.v = true;
[5e53e02]167 data.nfo = false;
168 data.ra = (t->frame) >> FRAME_WIDTH;
169 data.ie = false;
170 data.e = false;
[a7961271]171 data.cp = t->c;
[92778f2]172#ifdef CONFIG_VIRT_IDX_DCACHE
[a7961271]173 data.cv = t->c;
[5e53e02]174#endif
175 data.p = t->k;
176 data.x = false;
[a7961271]177 data.w = ro ? false : t->w;
[5e53e02]178 data.size = PAGESIZE_8K;
179
180 __hypercall_hyperfast(
181 t->page, t->as->asid, data.value, MMU_FLAG_DTLB, 0, MMU_MAP_ADDR);
[a7961271]182}
[5e53e02]183
[29b2bbf]184/** Copy PTE to ITLB.
185 *
[965dc18]186 * @param t Page Table Entry to be copied.
[29b2bbf]187 */
[ba50a34]188void itlb_pte_copy(pte_t *t)
[f47fd19]189{
[ba50a34]190 tte_data_t data;
[a7961271]191
192 data.value = 0;
193 data.v = true;
[ba50a34]194 data.nfo = false;
195 data.ra = (t->frame) >> FRAME_WIDTH;
196 data.ie = false;
197 data.e = false;
[a7961271]198 data.cp = t->c;
[ba50a34]199 data.cv = false;
200 data.p = t->k;
201 data.x = true;
[a7961271]202 data.w = false;
[ba50a34]203 data.size = PAGESIZE_8K;
[a7961271]204
[ba50a34]205 __hypercall_hyperfast(
206 t->page, t->as->asid, data.value, MMU_FLAG_ITLB, 0, MMU_MAP_ADDR);
[f47fd19]207}
208
[008029d]209/** ITLB miss handler. */
[cade9c1]210void fast_instruction_access_mmu_miss(unsigned int tt, istate_t *istate)
[008029d]211{
[ba50a34]212 uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
[38dc82d]213 pte_t t;
[a7961271]214
[38dc82d]215 bool found = page_mapping_find(AS, va, true, &t);
216 if (found && PTE_EXECUTABLE(&t)) {
[a7961271]217 /*
218 * The mapping was found in the software page hash table.
219 * Insert it into ITLB.
220 */
[38dc82d]221 t.a = true;
222 itlb_pte_copy(&t);
[29b2bbf]223#ifdef CONFIG_TSB
[38dc82d]224 itsb_pte_copy(&t);
[29b2bbf]225#endif
[346b12a2]226 page_mapping_update(AS, va, true, &t);
[a7961271]227 } else {
228 /*
[771cd22]229 * Forward the page fault to the address space page fault
230 * handler.
[7008097]231 */
[1dbc43f]232 as_page_fault(va, PF_ACCESS_EXEC, istate);
[a7961271]233 }
[008029d]234}
235
[f47fd19]236/** DTLB miss handler.
237 *
[771cd22]238 * Note that some faults (e.g. kernel faults) were already resolved by the
239 * low-level, assembly language part of the fast_data_access_mmu_miss handler.
[36f19c0]240 *
[cade9c1]241 * @param tt Trap type.
[965dc18]242 * @param istate Interrupted state saved on the stack.
[f47fd19]243 */
[cade9c1]244void fast_data_access_mmu_miss(unsigned int tt, istate_t *istate)
[008029d]245{
[38dc82d]246 pte_t t;
[cade9c1]247 uintptr_t va = DMISS_ADDRESS(istate->tlb_tag_access);
248 uint16_t ctx = DMISS_CONTEXT(istate->tlb_tag_access);
[730ff63]249 as_t *as = AS;
[7cb53f62]250
[ba50a34]251 if (ctx == ASID_KERNEL) {
252 if (va == 0) {
[f47fd19]253 /* NULL access in kernel */
[1dbc43f]254 panic("NULL pointer dereference.");
[730ff63]255 } else if (va >= end_of_identity) {
256 /* Kernel non-identity */
257 as = AS_KERNEL;
258 } else {
259 panic("Unexpected kernel page fault.");
[f47fd19]260 }
[68656282]261 }
262
[38dc82d]263 bool found = page_mapping_find(as, va, true, &t);
264 if (found) {
[f47fd19]265 /*
266 * The mapping was found in the software page hash table.
267 * Insert it into DTLB.
268 */
[38dc82d]269 t.a = true;
270 dtlb_pte_copy(&t, true);
[29b2bbf]271#ifdef CONFIG_TSB
[38dc82d]272 dtsb_pte_copy(&t, true);
[29b2bbf]273#endif
[346b12a2]274 page_mapping_update(as, va, true, &t);
[f47fd19]275 } else {
276 /*
[2057572]277 * Forward the page fault to the address space page fault
278 * handler.
[f47fd19]279 */
[1dbc43f]280 as_page_fault(va, PF_ACCESS_READ, istate);
[f47fd19]281 }
[008029d]282}
283
[36f19c0]284/** DTLB protection fault handler.
285 *
[cade9c1]286 * @param tt Trap type.
[965dc18]287 * @param istate Interrupted state saved on the stack.
[36f19c0]288 */
[cade9c1]289void fast_data_access_protection(unsigned int tt, istate_t *istate)
[008029d]290{
[38dc82d]291 pte_t t;
[cade9c1]292 uintptr_t va = DMISS_ADDRESS(istate->tlb_tag_access);
293 uint16_t ctx = DMISS_CONTEXT(istate->tlb_tag_access);
[730ff63]294 as_t *as = AS;
[e0b241f]295
[730ff63]296 if (ctx == ASID_KERNEL)
297 as = AS_KERNEL;
298
[38dc82d]299 bool found = page_mapping_find(as, va, true, &t);
300 if (found && PTE_WRITABLE(&t)) {
[e0b241f]301 /*
[771cd22]302 * The mapping was found in the software page hash table and is
303 * writable. Demap the old mapping and insert an updated mapping
304 * into DTLB.
[e0b241f]305 */
[38dc82d]306 t.a = true;
307 t.d = true;
[ba50a34]308 mmu_demap_page(va, ctx, MMU_FLAG_DTLB);
[38dc82d]309 dtlb_pte_copy(&t, false);
[29b2bbf]310#ifdef CONFIG_TSB
[38dc82d]311 dtsb_pte_copy(&t, false);
[29b2bbf]312#endif
[346b12a2]313 page_mapping_update(as, va, true, &t);
[e0b241f]314 } else {
315 /*
[771cd22]316 * Forward the page fault to the address space page fault
317 * handler.
[e0b241f]318 */
[1dbc43f]319 as_page_fault(va, PF_ACCESS_WRITE, istate);
[e0b241f]320 }
[008029d]321}
322
[77f65df]323/*
324 * On Niagara this function does not work, as supervisor software is isolated
325 * from the TLB by the hypervisor and has no chance to investigate the TLB
326 * entries.
[965dc18]327 */
[0d04024]328void tlb_print(void)
329{
[b2fa1204]330 log(LF_ARCH, LVL_WARN, "Operation not possible on Niagara.");
[0d04024]331}
[dbb6886]332
[ba50a34]333/**
334 * Describes the exact condition which caused the last DMMU fault.
335 */
336void describe_dmmu_fault(void)
[8cee705]337{
[ba50a34]338 uint64_t myid;
339 __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
[8cee705]340
[ba50a34]341 ASSERT(mmu_fsas[myid].dft < 16);
342
343 printf("condition which caused the fault: %s\n",
344 fault_types[mmu_fsas[myid].dft]);
345}
346
347/** Invalidate all unlocked ITLB and DTLB entries. */
348void tlb_invalidate_all(void)
349{
350 uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
351 MMU_FLAG_DTLB | MMU_FLAG_ITLB);
[7e752b2]352 if (errno != HV_EOK)
353 panic("Error code = %" PRIu64 ".\n", errno);
[8cee705]354}
355
[771cd22]356/** Invalidate all ITLB and DTLB entries that belong to specified ASID
357 * (Context).
[dbb6886]358 *
359 * @param asid Address Space ID.
360 */
361void tlb_invalidate_asid(asid_t asid)
362{
[fd85ae5]363 /* switch to nucleus because we are mapped by the primary context */
364 nucleus_enter();
[77f65df]365
[3da11f37]366 __hypercall_fast4(MMU_DEMAP_CTX, 0, 0, asid,
367 MMU_FLAG_ITLB | MMU_FLAG_DTLB);
368
[fd85ae5]369 nucleus_leave();
[dbb6886]370}
371
[771cd22]372/** Invalidate all ITLB and DTLB entries for specified page range in specified
373 * address space.
[dbb6886]374 *
[965dc18]375 * @param asid Address Space ID.
376 * @param page First page which to sweep out from ITLB and DTLB.
377 * @param cnt Number of ITLB and DTLB entries to invalidate.
[dbb6886]378 */
[98000fb]379void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
[dbb6886]380{
[6c441cf8]381 unsigned int i;
[ed166f7]382
[fd85ae5]383 /* switch to nucleus because we are mapped by the primary context */
384 nucleus_enter();
[3da11f37]385
386 for (i = 0; i < cnt; i++) {
[7254df6]387 __hypercall_fast5(MMU_DEMAP_PAGE, 0, 0, page + i * PAGE_SIZE,
388 asid, MMU_FLAG_DTLB | MMU_FLAG_ITLB);
[4512d7e]389 }
[3da11f37]390
[fd85ae5]391 nucleus_leave();
[dbb6886]392}
[b45c443]393
[10b890b]394/** @}
[b45c443]395 */
Note: See TracBrowser for help on using the repository browser.