2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include "arch/arm/faults.hh"
49 #include "arch/arm/pagetable.hh"
50 #include "arch/arm/tlb.hh"
51 #include "arch/arm/utility.hh"
52 #include "base/inifile.hh"
53 #include "base/str.hh"
54 #include "base/trace.hh"
55 #include "cpu/thread_context.hh"
56 #include "mem/page_table.hh"
57 #include "params/ArmTLB.hh"
58 #include "sim/process.hh"
61 #include "arch/arm/table_walker.hh"
65 using namespace ArmISA
;
67 TLB::TLB(const Params
*p
)
68 : BaseTLB(p
), size(p
->size
)
70 , tableWalker(p
->walker
)
74 table
= new TlbEntry
[size
];
75 memset(table
, 0, sizeof(TlbEntry
[size
]));
78 tableWalker
->setTlb(this);
89 TLB::translateFunctional(ThreadContext
*tc
, Addr va
, Addr
&pa
)
91 uint32_t context_id
= tc
->readMiscReg(MISCREG_CONTEXTIDR
);
92 TlbEntry
*e
= lookup(va
, context_id
, true);
100 TLB::lookup(Addr va
, uint8_t cid
, bool functional
)
103 TlbEntry
*retval
= NULL
;
105 // Maitaining LRU array
108 while (retval
== NULL
&& x
< size
) {
109 if (table
[x
].match(va
, cid
)) {
111 // We only move the hit entry ahead when the position is higher than rangeMRU
113 TlbEntry tmp_entry
= table
[x
];
114 for(int i
= x
; i
> 0; i
--)
115 table
[i
] = table
[i
-1];
116 table
[0] = tmp_entry
;
126 DPRINTF(TLBVerbose
, "Lookup %#x, cid %#x -> %s ppn %#x size: %#x pa: %#x ap:%d\n",
127 va
, cid
, retval
? "hit" : "miss", retval
? retval
->pfn
: 0,
128 retval
? retval
->size
: 0, retval
? retval
->pAddr(va
) : 0,
129 retval
? retval
->ap
: 0);
134 // insert a new TLB entry
136 TLB::insert(Addr addr
, TlbEntry
&entry
)
138 DPRINTF(TLB
, "Inserting entry into TLB with pfn:%#x size:%#x vpn: %#x"
139 " asid:%d N:%d global:%d valid:%d nc:%d sNp:%d xn:%d ap:%#x"
140 " domain:%#x\n", entry
.pfn
, entry
.size
, entry
.vpn
, entry
.asid
,
141 entry
.N
, entry
.global
, entry
.valid
, entry
.nonCacheable
, entry
.sNp
,
142 entry
.xn
, entry
.ap
, entry
.domain
);
144 if (table
[size
-1].valid
)
145 DPRINTF(TLB
, " - Replacing Valid entry %#x, asn %d ppn %#x size: %#x ap:%d\n",
146 table
[size
-1].vpn
<< table
[size
-1].N
, table
[size
-1].asid
,
147 table
[size
-1].pfn
<< table
[size
-1].N
, table
[size
-1].size
,
150 //inserting to MRU position and evicting the LRU one
152 for(int i
= size
-1; i
> 0; i
--)
153 table
[i
] = table
[i
-1];
162 DPRINTF(TLB
, "Current TLB contents:\n");
166 DPRINTF(TLB
, " * %#x, asn %d ppn %#x size: %#x ap:%d\n",
167 te
->vpn
<< te
->N
, te
->asid
, te
->pfn
<< te
->N
, te
->size
, te
->ap
);
176 DPRINTF(TLB
, "Flushing all TLB entries\n");
182 DPRINTF(TLB
, " - %#x, asn %d ppn %#x size: %#x ap:%d\n",
183 te
->vpn
<< te
->N
, te
->asid
, te
->pfn
<< te
->N
, te
->size
, te
->ap
);
187 memset(table
, 0, sizeof(TlbEntry
[size
]));
192 TLB::flushMvaAsid(Addr mva
, uint64_t asn
)
194 DPRINTF(TLB
, "Flushing mva %#x asid: %#x\n", mva
, asn
);
197 te
= lookup(mva
, asn
);
199 DPRINTF(TLB
, " - %#x, asn %d ppn %#x size: %#x ap:%d\n",
200 te
->vpn
<< te
->N
, te
->asid
, te
->pfn
<< te
->N
, te
->size
, te
->ap
);
202 te
= lookup(mva
,asn
);
207 TLB::flushAsid(uint64_t asn
)
209 DPRINTF(TLB
, "Flushing all entries with asid: %#x\n", asn
);
216 if (te
->asid
== asn
) {
218 DPRINTF(TLB
, " - %#x, asn %d ppn %#x size: %#x ap:%d\n",
219 te
->vpn
<< te
->N
, te
->asid
, te
->pfn
<< te
->N
, te
->size
, te
->ap
);
226 TLB::flushMva(Addr mva
)
228 DPRINTF(TLB
, "Flushing all entries with mva: %#x\n", mva
);
235 Addr v
= te
->vpn
<< te
->N
;
236 if (mva
>= v
&& mva
< v
+ te
->size
) {
238 DPRINTF(TLB
, " - %#x, asn %d ppn %#x size: %#x ap:%d\n",
239 te
->vpn
<< te
->N
, te
->asid
, te
->pfn
<< te
->N
, te
->size
, te
->ap
);
246 TLB::serialize(ostream
&os
)
248 panic("Implement Serialize\n");
252 TLB::unserialize(Checkpoint
*cp
, const string
§ion
)
255 panic("Need to properly unserialize TLB\n");
262 .name(name() + ".read_hits")
263 .desc("DTB read hits")
267 .name(name() + ".read_misses")
268 .desc("DTB read misses")
273 .name(name() + ".read_accesses")
274 .desc("DTB read accesses")
278 .name(name() + ".write_hits")
279 .desc("DTB write hits")
283 .name(name() + ".write_misses")
284 .desc("DTB write misses")
289 .name(name() + ".write_accesses")
290 .desc("DTB write accesses")
294 .name(name() + ".hits")
299 .name(name() + ".misses")
304 .name(name() + ".accesses")
305 .desc("DTB accesses")
308 hits
= read_hits
+ write_hits
;
309 misses
= read_misses
+ write_misses
;
310 accesses
= read_accesses
+ write_accesses
;
315 TLB::translateSe(RequestPtr req
, ThreadContext
*tc
, Mode mode
,
316 Translation
*translation
, bool &delay
, bool timing
)
318 // XXX Cache misc registers and have miscreg write function inv cache
319 Addr vaddr
= req
->getVaddr() & ~PcModeMask
;
320 SCTLR sctlr
= tc
->readMiscReg(MISCREG_SCTLR
);
321 uint32_t flags
= req
->getFlags();
323 bool is_fetch
= (mode
== Execute
);
324 bool is_write
= (mode
== Write
);
327 assert(flags
& MustBeOne
);
328 if (sctlr
.a
|| !(flags
& AllowUnaligned
)) {
329 if (vaddr
& flags
& AlignmentMask
) {
330 return new DataAbort(vaddr
, 0, is_write
, ArmFault::AlignmentFault
);
336 Process
*p
= tc
->getProcessPtr();
338 if (!p
->pTable
->translate(vaddr
, paddr
))
339 return Fault(new GenericPageTableFault(vaddr
));
340 req
->setPaddr(paddr
);
348 TLB::trickBoxCheck(RequestPtr req
, Mode mode
, uint8_t domain
, bool sNp
)
354 TLB::walkTrickBoxCheck(Addr pa
, Addr va
, Addr sz
, bool is_exec
,
355 bool is_write
, uint8_t domain
, bool sNp
)
361 TLB::translateFs(RequestPtr req
, ThreadContext
*tc
, Mode mode
,
362 Translation
*translation
, bool &delay
, bool timing
)
364 // XXX Cache misc registers and have miscreg write function inv cache
365 Addr vaddr
= req
->getVaddr() & ~PcModeMask
;
366 SCTLR sctlr
= tc
->readMiscReg(MISCREG_SCTLR
);
367 CPSR cpsr
= tc
->readMiscReg(MISCREG_CPSR
);
368 uint32_t flags
= req
->getFlags();
370 bool is_fetch
= (mode
== Execute
);
371 bool is_write
= (mode
== Write
);
372 bool is_priv
= (cpsr
.mode
!= MODE_USER
) && !(flags
& UserMode
);
374 DPRINTF(TLBVerbose
, "CPSR is user:%d UserMode:%d\n", cpsr
.mode
== MODE_USER
, flags
376 // If this is a clrex instruction, provide a PA of 0 with no fault
377 // This will force the monitor to set the tracked address to 0
378 // a bit of a hack but this effectively clrears this processors monitor
379 if (flags
& Request::CLEAR_LL
){
381 req
->setFlags(Request::UNCACHEABLE
);
382 req
->setFlags(Request::CLEAR_LL
);
385 if ((req
->isInstFetch() && (!sctlr
.i
)) ||
386 ((!req
->isInstFetch()) && (!sctlr
.c
))){
387 req
->setFlags(Request::UNCACHEABLE
);
390 assert(flags
& MustBeOne
);
391 if (sctlr
.a
|| !(flags
& AllowUnaligned
)) {
392 if (vaddr
& flags
& AlignmentMask
) {
393 return new DataAbort(vaddr
, 0, is_write
, ArmFault::AlignmentFault
);
398 uint32_t context_id
= tc
->readMiscReg(MISCREG_CONTEXTIDR
);
403 req
->setPaddr(vaddr
);
404 if (sctlr
.tre
== 0) {
405 req
->setFlags(Request::UNCACHEABLE
);
407 PRRR prrr
= tc
->readMiscReg(MISCREG_PRRR
);
408 NMRR nmrr
= tc
->readMiscReg(MISCREG_NMRR
);
410 if (nmrr
.ir0
== 0 || nmrr
.or0
== 0 || prrr
.tr0
!= 0x2)
411 req
->setFlags(Request::UNCACHEABLE
);
414 // Set memory attributes
416 tableWalker
->memAttrs(tc
, temp_te
, sctlr
, 0, 1);
417 temp_te
.shareable
= true;
418 DPRINTF(TLBVerbose
, "(No MMU) setting memory attributes: shareable:\
419 %d, innerAttrs: %d, outerAttrs: %d\n", temp_te
.shareable
,
420 temp_te
.innerAttrs
, temp_te
.outerAttrs
);
421 setAttr(temp_te
.attributes
);
423 return trickBoxCheck(req
, mode
, 0, false);
426 DPRINTF(TLBVerbose
, "Translating vaddr=%#x context=%d\n", vaddr
, context_id
);
427 // Translation enabled
429 TlbEntry
*te
= lookup(vaddr
, context_id
);
431 if (req
->isPrefetch()){
432 //if the request is a prefetch don't attempt to fill the TLB
433 //or go any further with the memory access
434 return new PrefetchAbort(vaddr
, ArmFault::PrefetchTLBMiss
);
436 // start translation table walk, pass variables rather than
437 // re-retreaving in table walker for speed
438 DPRINTF(TLB
, "TLB Miss: Starting hardware table walker for %#x(%d)\n",
440 fault
= tableWalker
->walk(req
, tc
, context_id
, mode
, translation
,
444 // for timing mode, return and wait for table walk
450 te
= lookup(vaddr
, context_id
);
456 // Set memory attributes
458 "Setting memory attributes: shareable: %d, innerAttrs: %d, \
460 te
->shareable
, te
->innerAttrs
, te
->outerAttrs
);
461 setAttr(te
->attributes
);
462 if (te
->nonCacheable
)
463 req
->setFlags(Request::UNCACHEABLE
);
464 uint32_t dacr
= tc
->readMiscReg(MISCREG_DACR
);
465 switch ( (dacr
>> (te
->domain
* 2)) & 0x3) {
467 DPRINTF(TLB
, "TLB Fault: Data abort on domain. DACR: %#x domain: %#x"
468 " write:%d sNp:%d\n", dacr
, te
->domain
, is_write
, te
->sNp
);
470 return new PrefetchAbort(vaddr
,
471 (te
->sNp
? ArmFault::Domain0
: ArmFault::Domain1
));
473 return new DataAbort(vaddr
, te
->domain
, is_write
,
474 (te
->sNp
? ArmFault::Domain0
: ArmFault::Domain1
));
476 // Continue with permissions check
479 panic("UNPRED domain\n");
481 req
->setPaddr(te
->pAddr(vaddr
));
482 fault
= trickBoxCheck(req
, mode
, te
->domain
, te
->sNp
);
500 DPRINTF(TLB
, "Access permissions 0, checking rs:%#x\n", (int)sctlr
.rs
);
502 switch ((int)sctlr
.rs
) {
507 abt
= is_write
|| !is_priv
;
523 abt
= !is_priv
&& is_write
;
529 panic("UNPRED premissions\n");
531 abt
= !is_priv
|| is_write
;
538 panic("Unknown permissions\n");
540 if ((is_fetch
) && (abt
|| te
->xn
)) {
541 DPRINTF(TLB
, "TLB Fault: Prefetch abort on permission check. AP:%d priv:%d"
542 " write:%d sNp:%d\n", ap
, is_priv
, is_write
, te
->sNp
);
543 return new PrefetchAbort(vaddr
,
544 (te
->sNp
? ArmFault::Permission0
:
545 ArmFault::Permission1
));
547 DPRINTF(TLB
, "TLB Fault: Data abort on permission check. AP:%d priv:%d"
548 " write:%d sNp:%d\n", ap
, is_priv
, is_write
, te
->sNp
);
549 return new DataAbort(vaddr
, te
->domain
, is_write
,
550 (te
->sNp
? ArmFault::Permission0
:
551 ArmFault::Permission1
));
554 req
->setPaddr(te
->pAddr(vaddr
));
555 // Check for a trickbox generated address fault
556 fault
= trickBoxCheck(req
, mode
, te
->domain
, te
->sNp
);
566 TLB::translateAtomic(RequestPtr req
, ThreadContext
*tc
, Mode mode
)
571 fault
= translateFs(req
, tc
, mode
, NULL
, delay
, false);
573 fault
= translateSe(req
, tc
, mode
, NULL
, delay
, false);
580 TLB::translateTiming(RequestPtr req
, ThreadContext
*tc
,
581 Translation
*translation
, Mode mode
)
587 fault
= translateFs(req
, tc
, mode
, translation
, delay
, true);
589 fault
= translateSe(req
, tc
, mode
, translation
, delay
, true);
592 translation
->finish(fault
, req
, tc
, mode
);
597 ArmTLBParams::create()
599 return new ArmISA::TLB(this);