0c001b0bc58d286a0d935bcf3bc949eea4cdbd92
[gem5.git] / src / arch / arm / tlb.cc
1 /*
2 * Copyright (c) 2010-2013, 2016-2019 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 */
40
41 #include "arch/arm/tlb.hh"
42
43 #include <memory>
44 #include <string>
45 #include <vector>
46
47 #include "arch/arm/faults.hh"
48 #include "arch/arm/isa.hh"
49 #include "arch/arm/pagetable.hh"
50 #include "arch/arm/self_debug.hh"
51 #include "arch/arm/stage2_lookup.hh"
52 #include "arch/arm/stage2_mmu.hh"
53 #include "arch/arm/system.hh"
54 #include "arch/arm/table_walker.hh"
55 #include "arch/arm/utility.hh"
56 #include "base/inifile.hh"
57 #include "base/str.hh"
58 #include "base/trace.hh"
59 #include "cpu/base.hh"
60 #include "cpu/thread_context.hh"
61 #include "debug/Checkpoint.hh"
62 #include "debug/TLB.hh"
63 #include "debug/TLBVerbose.hh"
64 #include "mem/packet_access.hh"
65 #include "mem/page_table.hh"
66 #include "mem/request.hh"
67 #include "params/ArmTLB.hh"
68 #include "sim/full_system.hh"
69 #include "sim/process.hh"
70 #include "sim/pseudo_inst.hh"
71
72 using namespace std;
73 using namespace ArmISA;
74
75 TLB::TLB(const ArmTLBParams *p)
76 : BaseTLB(p), table(new TlbEntry[p->size]), size(p->size),
77 isStage2(p->is_stage2), stage2Req(false), stage2DescReq(false), _attr(0),
78 directToStage2(false), tableWalker(p->walker), stage2Tlb(NULL),
79 stage2Mmu(NULL), test(nullptr), rangeMRU(1),
80 aarch64(false), aarch64EL(EL0), isPriv(false), isSecure(false),
81 isHyp(false), asid(0), vmid(0), hcr(0), dacr(0),
82 miscRegValid(false), miscRegContext(0), curTranType(NormalTran)
83 {
84 const ArmSystem *sys = dynamic_cast<const ArmSystem *>(p->sys);
85
86 tableWalker->setTlb(this);
87
88 // Cache system-level properties
89 haveLPAE = tableWalker->haveLPAE();
90 haveVirtualization = tableWalker->haveVirtualization();
91 haveLargeAsid64 = tableWalker->haveLargeAsid64();
92
93 if (sys)
94 m5opRange = sys->m5opRange();
95 }
96
97 TLB::~TLB()
98 {
99 delete[] table;
100 }
101
102 void
103 TLB::init()
104 {
105 if (stage2Mmu && !isStage2)
106 stage2Tlb = stage2Mmu->stage2Tlb();
107 }
108
109 void
110 TLB::setMMU(Stage2MMU *m, MasterID master_id)
111 {
112 stage2Mmu = m;
113 tableWalker->setMMU(m, master_id);
114 }
115
116 bool
117 TLB::translateFunctional(ThreadContext *tc, Addr va, Addr &pa)
118 {
119 updateMiscReg(tc);
120
121 if (directToStage2) {
122 assert(stage2Tlb);
123 return stage2Tlb->translateFunctional(tc, va, pa);
124 }
125
126 TlbEntry *e = lookup(va, asid, vmid, isHyp, isSecure, true, false,
127 aarch64 ? aarch64EL : EL1, false);
128 if (!e)
129 return false;
130 pa = e->pAddr(va);
131 return true;
132 }
133
134 Fault
135 TLB::finalizePhysical(const RequestPtr &req,
136 ThreadContext *tc, Mode mode) const
137 {
138 const Addr paddr = req->getPaddr();
139
140 if (m5opRange.contains(paddr)) {
141 uint8_t func;
142 PseudoInst::decodeAddrOffset(paddr - m5opRange.start(), func);
143 req->setLocalAccessor(
144 [func, mode](ThreadContext *tc, PacketPtr pkt) -> Cycles
145 {
146 uint64_t ret;
147 PseudoInst::pseudoInst<PseudoInstABI>(tc, func, ret);
148 if (mode == Read)
149 pkt->setLE(ret);
150 return Cycles(1);
151 }
152 );
153 }
154
155 return NoFault;
156 }
157
158 TlbEntry*
159 TLB::lookup(Addr va, uint16_t asn, uint8_t vmid, bool hyp, bool secure,
160 bool functional, bool ignore_asn, ExceptionLevel target_el,
161 bool in_host)
162 {
163
164 TlbEntry *retval = NULL;
165
166 // Maintaining LRU array
167 int x = 0;
168 while (retval == NULL && x < size) {
169 if ((!ignore_asn && table[x].match(va, asn, vmid, hyp, secure, false,
170 target_el, in_host)) ||
171 (ignore_asn && table[x].match(va, vmid, hyp, secure, target_el,
172 in_host))) {
173 // We only move the hit entry ahead when the position is higher
174 // than rangeMRU
175 if (x > rangeMRU && !functional) {
176 TlbEntry tmp_entry = table[x];
177 for (int i = x; i > 0; i--)
178 table[i] = table[i - 1];
179 table[0] = tmp_entry;
180 retval = &table[0];
181 } else {
182 retval = &table[x];
183 }
184 break;
185 }
186 ++x;
187 }
188
189 DPRINTF(TLBVerbose, "Lookup %#x, asn %#x -> %s vmn 0x%x hyp %d secure %d "
190 "ppn %#x size: %#x pa: %#x ap:%d ns:%d nstid:%d g:%d asid: %d "
191 "el: %d\n",
192 va, asn, retval ? "hit" : "miss", vmid, hyp, secure,
193 retval ? retval->pfn : 0, retval ? retval->size : 0,
194 retval ? retval->pAddr(va) : 0, retval ? retval->ap : 0,
195 retval ? retval->ns : 0, retval ? retval->nstid : 0,
196 retval ? retval->global : 0, retval ? retval->asid : 0,
197 retval ? retval->el : 0);
198
199 return retval;
200 }
201
202 // insert a new TLB entry
203 void
204 TLB::insert(Addr addr, TlbEntry &entry)
205 {
206 DPRINTF(TLB, "Inserting entry into TLB with pfn:%#x size:%#x vpn: %#x"
207 " asid:%d vmid:%d N:%d global:%d valid:%d nc:%d xn:%d"
208 " ap:%#x domain:%#x ns:%d nstid:%d isHyp:%d\n", entry.pfn,
209 entry.size, entry.vpn, entry.asid, entry.vmid, entry.N,
210 entry.global, entry.valid, entry.nonCacheable, entry.xn,
211 entry.ap, static_cast<uint8_t>(entry.domain), entry.ns, entry.nstid,
212 entry.isHyp);
213
214 if (table[size - 1].valid)
215 DPRINTF(TLB, " - Replacing Valid entry %#x, asn %d vmn %d ppn %#x "
216 "size: %#x ap:%d ns:%d nstid:%d g:%d isHyp:%d el: %d\n",
217 table[size-1].vpn << table[size-1].N, table[size-1].asid,
218 table[size-1].vmid, table[size-1].pfn << table[size-1].N,
219 table[size-1].size, table[size-1].ap, table[size-1].ns,
220 table[size-1].nstid, table[size-1].global, table[size-1].isHyp,
221 table[size-1].el);
222
223 //inserting to MRU position and evicting the LRU one
224
225 for (int i = size - 1; i > 0; --i)
226 table[i] = table[i-1];
227 table[0] = entry;
228
229 inserts++;
230 ppRefills->notify(1);
231 }
232
233 void
234 TLB::printTlb() const
235 {
236 int x = 0;
237 TlbEntry *te;
238 DPRINTF(TLB, "Current TLB contents:\n");
239 while (x < size) {
240 te = &table[x];
241 if (te->valid)
242 DPRINTF(TLB, " * %s\n", te->print());
243 ++x;
244 }
245 }
246
247 void
248 TLB::flushAllSecurity(bool secure_lookup, ExceptionLevel target_el,
249 bool ignore_el, bool in_host)
250 {
251 DPRINTF(TLB, "Flushing all TLB entries (%s lookup)\n",
252 (secure_lookup ? "secure" : "non-secure"));
253 int x = 0;
254 TlbEntry *te;
255 while (x < size) {
256 te = &table[x];
257 const bool el_match = ignore_el ?
258 true : te->checkELMatch(target_el, in_host);
259 if (te->valid && secure_lookup == !te->nstid &&
260 (te->vmid == vmid || secure_lookup) && el_match) {
261
262 DPRINTF(TLB, " - %s\n", te->print());
263 te->valid = false;
264 flushedEntries++;
265 }
266 ++x;
267 }
268
269 flushTlb++;
270
271 // If there's a second stage TLB (and we're not it) then flush it as well
272 // if we're currently in hyp mode
273 if (!isStage2 && isHyp) {
274 stage2Tlb->flushAllSecurity(secure_lookup, EL1, true, false);
275 }
276 }
277
278 void
279 TLB::flushAllNs(ExceptionLevel target_el, bool ignore_el)
280 {
281 bool hyp = target_el == EL2;
282
283 DPRINTF(TLB, "Flushing all NS TLB entries (%s lookup)\n",
284 (hyp ? "hyp" : "non-hyp"));
285 int x = 0;
286 TlbEntry *te;
287 while (x < size) {
288 te = &table[x];
289 const bool el_match = ignore_el ?
290 true : te->checkELMatch(target_el, false);
291
292 if (te->valid && te->nstid && te->isHyp == hyp && el_match) {
293
294 DPRINTF(TLB, " - %s\n", te->print());
295 flushedEntries++;
296 te->valid = false;
297 }
298 ++x;
299 }
300
301 flushTlb++;
302
303 // If there's a second stage TLB (and we're not it) then flush it as well
304 if (!isStage2 && !hyp) {
305 stage2Tlb->flushAllNs(EL1, true);
306 }
307 }
308
309 void
310 TLB::flushMvaAsid(Addr mva, uint64_t asn, bool secure_lookup,
311 ExceptionLevel target_el, bool in_host)
312 {
313 DPRINTF(TLB, "Flushing TLB entries with mva: %#x, asid: %#x "
314 "(%s lookup)\n", mva, asn, (secure_lookup ?
315 "secure" : "non-secure"));
316 _flushMva(mva, asn, secure_lookup, false, target_el, in_host);
317 flushTlbMvaAsid++;
318 }
319
320 void
321 TLB::flushAsid(uint64_t asn, bool secure_lookup, ExceptionLevel target_el,
322 bool in_host)
323 {
324 DPRINTF(TLB, "Flushing TLB entries with asid: %#x (%s lookup)\n", asn,
325 (secure_lookup ? "secure" : "non-secure"));
326
327 int x = 0 ;
328 TlbEntry *te;
329
330 while (x < size) {
331 te = &table[x];
332 if (te->valid && te->asid == asn && secure_lookup == !te->nstid &&
333 (te->vmid == vmid || secure_lookup) &&
334 te->checkELMatch(target_el, in_host)) {
335
336 te->valid = false;
337 DPRINTF(TLB, " - %s\n", te->print());
338 flushedEntries++;
339 }
340 ++x;
341 }
342 flushTlbAsid++;
343 }
344
345 void
346 TLB::flushMva(Addr mva, bool secure_lookup, ExceptionLevel target_el,
347 bool in_host) {
348
349 DPRINTF(TLB, "Flushing TLB entries with mva: %#x (%s lookup)\n", mva,
350 (secure_lookup ? "secure" : "non-secure"));
351 _flushMva(mva, 0xbeef, secure_lookup, true, target_el, in_host);
352 flushTlbMva++;
353 }
354
355 void
356 TLB::_flushMva(Addr mva, uint64_t asn, bool secure_lookup,
357 bool ignore_asn, ExceptionLevel target_el, bool in_host)
358 {
359 TlbEntry *te;
360 // D5.7.2: Sign-extend address to 64 bits
361 mva = sext<56>(mva);
362
363 bool hyp = target_el == EL2;
364
365 te = lookup(mva, asn, vmid, hyp, secure_lookup, false, ignore_asn,
366 target_el, in_host);
367 while (te != NULL) {
368 if (secure_lookup == !te->nstid) {
369 DPRINTF(TLB, " - %s\n", te->print());
370 te->valid = false;
371 flushedEntries++;
372 }
373 te = lookup(mva, asn, vmid, hyp, secure_lookup, false, ignore_asn,
374 target_el, in_host);
375 }
376 }
377
378 void
379 TLB::flushIpaVmid(Addr ipa, bool secure_lookup, ExceptionLevel target_el)
380 {
381 assert(!isStage2);
382 stage2Tlb->_flushMva(ipa, 0xbeef, secure_lookup, true, target_el, false);
383 }
384
385 void
386 TLB::drainResume()
387 {
388 // We might have unserialized something or switched CPUs, so make
389 // sure to re-read the misc regs.
390 miscRegValid = false;
391 }
392
393 void
394 TLB::takeOverFrom(BaseTLB *_otlb)
395 {
396 TLB *otlb = dynamic_cast<TLB*>(_otlb);
397 /* Make sure we actually have a valid type */
398 if (otlb) {
399 _attr = otlb->_attr;
400 haveLPAE = otlb->haveLPAE;
401 directToStage2 = otlb->directToStage2;
402 stage2Req = otlb->stage2Req;
403 stage2DescReq = otlb->stage2DescReq;
404
405 /* Sync the stage2 MMU if they exist in both
406 * the old CPU and the new
407 */
408 if (!isStage2 &&
409 stage2Tlb && otlb->stage2Tlb) {
410 stage2Tlb->takeOverFrom(otlb->stage2Tlb);
411 }
412 } else {
413 panic("Incompatible TLB type!");
414 }
415 }
416
417 void
418 TLB::regStats()
419 {
420 BaseTLB::regStats();
421 instHits
422 .name(name() + ".inst_hits")
423 .desc("ITB inst hits")
424 ;
425
426 instMisses
427 .name(name() + ".inst_misses")
428 .desc("ITB inst misses")
429 ;
430
431 instAccesses
432 .name(name() + ".inst_accesses")
433 .desc("ITB inst accesses")
434 ;
435
436 readHits
437 .name(name() + ".read_hits")
438 .desc("DTB read hits")
439 ;
440
441 readMisses
442 .name(name() + ".read_misses")
443 .desc("DTB read misses")
444 ;
445
446 readAccesses
447 .name(name() + ".read_accesses")
448 .desc("DTB read accesses")
449 ;
450
451 writeHits
452 .name(name() + ".write_hits")
453 .desc("DTB write hits")
454 ;
455
456 writeMisses
457 .name(name() + ".write_misses")
458 .desc("DTB write misses")
459 ;
460
461 writeAccesses
462 .name(name() + ".write_accesses")
463 .desc("DTB write accesses")
464 ;
465
466 hits
467 .name(name() + ".hits")
468 .desc("DTB hits")
469 ;
470
471 misses
472 .name(name() + ".misses")
473 .desc("DTB misses")
474 ;
475
476 accesses
477 .name(name() + ".accesses")
478 .desc("DTB accesses")
479 ;
480
481 flushTlb
482 .name(name() + ".flush_tlb")
483 .desc("Number of times complete TLB was flushed")
484 ;
485
486 flushTlbMva
487 .name(name() + ".flush_tlb_mva")
488 .desc("Number of times TLB was flushed by MVA")
489 ;
490
491 flushTlbMvaAsid
492 .name(name() + ".flush_tlb_mva_asid")
493 .desc("Number of times TLB was flushed by MVA & ASID")
494 ;
495
496 flushTlbAsid
497 .name(name() + ".flush_tlb_asid")
498 .desc("Number of times TLB was flushed by ASID")
499 ;
500
501 flushedEntries
502 .name(name() + ".flush_entries")
503 .desc("Number of entries that have been flushed from TLB")
504 ;
505
506 alignFaults
507 .name(name() + ".align_faults")
508 .desc("Number of TLB faults due to alignment restrictions")
509 ;
510
511 prefetchFaults
512 .name(name() + ".prefetch_faults")
513 .desc("Number of TLB faults due to prefetch")
514 ;
515
516 domainFaults
517 .name(name() + ".domain_faults")
518 .desc("Number of TLB faults due to domain restrictions")
519 ;
520
521 permsFaults
522 .name(name() + ".perms_faults")
523 .desc("Number of TLB faults due to permissions restrictions")
524 ;
525
526 instAccesses = instHits + instMisses;
527 readAccesses = readHits + readMisses;
528 writeAccesses = writeHits + writeMisses;
529 hits = readHits + writeHits + instHits;
530 misses = readMisses + writeMisses + instMisses;
531 accesses = readAccesses + writeAccesses + instAccesses;
532 }
533
534 void
535 TLB::regProbePoints()
536 {
537 ppRefills.reset(new ProbePoints::PMU(getProbeManager(), "Refills"));
538 }
539
540 Fault
541 TLB::translateSe(const RequestPtr &req, ThreadContext *tc, Mode mode,
542 Translation *translation, bool &delay, bool timing)
543 {
544 updateMiscReg(tc);
545 Addr vaddr_tainted = req->getVaddr();
546 Addr vaddr = 0;
547 if (aarch64)
548 vaddr = purifyTaggedAddr(vaddr_tainted, tc, aarch64EL, (TCR)ttbcr,
549 mode==Execute);
550 else
551 vaddr = vaddr_tainted;
552 Request::Flags flags = req->getFlags();
553
554 bool is_fetch = (mode == Execute);
555 bool is_write = (mode == Write);
556
557 if (!is_fetch) {
558 if (sctlr.a || !(flags & AllowUnaligned)) {
559 if (vaddr & mask(flags & AlignmentMask)) {
560 // LPAE is always disabled in SE mode
561 return std::make_shared<DataAbort>(
562 vaddr_tainted,
563 TlbEntry::DomainType::NoAccess, is_write,
564 ArmFault::AlignmentFault, isStage2,
565 ArmFault::VmsaTran);
566 }
567 }
568 }
569
570 Addr paddr;
571 Process *p = tc->getProcessPtr();
572
573 if (!p->pTable->translate(vaddr, paddr))
574 return std::make_shared<GenericPageTableFault>(vaddr_tainted);
575 req->setPaddr(paddr);
576
577 return finalizePhysical(req, tc, mode);
578 }
579
580 Fault
581 TLB::checkPermissions(TlbEntry *te, const RequestPtr &req, Mode mode)
582 {
583 // a data cache maintenance instruction that operates by MVA does
584 // not generate a Data Abort exeception due to a Permission fault
585 if (req->isCacheMaintenance()) {
586 return NoFault;
587 }
588
589 Addr vaddr = req->getVaddr(); // 32-bit don't have to purify
590 Request::Flags flags = req->getFlags();
591 bool is_fetch = (mode == Execute);
592 bool is_write = (mode == Write);
593 bool is_priv = isPriv && !(flags & UserMode);
594
595 // Get the translation type from the actuall table entry
596 ArmFault::TranMethod tranMethod = te->longDescFormat ? ArmFault::LpaeTran
597 : ArmFault::VmsaTran;
598
599 // If this is the second stage of translation and the request is for a
600 // stage 1 page table walk then we need to check the HCR.PTW bit. This
601 // allows us to generate a fault if the request targets an area marked
602 // as a device or strongly ordered.
603 if (isStage2 && req->isPTWalk() && hcr.ptw &&
604 (te->mtype != TlbEntry::MemoryType::Normal)) {
605 return std::make_shared<DataAbort>(
606 vaddr, te->domain, is_write,
607 ArmFault::PermissionLL + te->lookupLevel,
608 isStage2, tranMethod);
609 }
610
611 // Generate an alignment fault for unaligned data accesses to device or
612 // strongly ordered memory
613 if (!is_fetch) {
614 if (te->mtype != TlbEntry::MemoryType::Normal) {
615 if (vaddr & mask(flags & AlignmentMask)) {
616 alignFaults++;
617 return std::make_shared<DataAbort>(
618 vaddr, TlbEntry::DomainType::NoAccess, is_write,
619 ArmFault::AlignmentFault, isStage2,
620 tranMethod);
621 }
622 }
623 }
624
625 if (te->nonCacheable) {
626 // Prevent prefetching from I/O devices.
627 if (req->isPrefetch()) {
628 // Here we can safely use the fault status for the short
629 // desc. format in all cases
630 return std::make_shared<PrefetchAbort>(
631 vaddr, ArmFault::PrefetchUncacheable,
632 isStage2, tranMethod);
633 }
634 }
635
636 if (!te->longDescFormat) {
637 switch ((dacr >> (static_cast<uint8_t>(te->domain) * 2)) & 0x3) {
638 case 0:
639 domainFaults++;
640 DPRINTF(TLB, "TLB Fault: Data abort on domain. DACR: %#x"
641 " domain: %#x write:%d\n", dacr,
642 static_cast<uint8_t>(te->domain), is_write);
643 if (is_fetch) {
644 // Use PC value instead of vaddr because vaddr might
645 // be aligned to cache line and should not be the
646 // address reported in FAR
647 return std::make_shared<PrefetchAbort>(
648 req->getPC(),
649 ArmFault::DomainLL + te->lookupLevel,
650 isStage2, tranMethod);
651 } else
652 return std::make_shared<DataAbort>(
653 vaddr, te->domain, is_write,
654 ArmFault::DomainLL + te->lookupLevel,
655 isStage2, tranMethod);
656 case 1:
657 // Continue with permissions check
658 break;
659 case 2:
660 panic("UNPRED domain\n");
661 case 3:
662 return NoFault;
663 }
664 }
665
666 // The 'ap' variable is AP[2:0] or {AP[2,1],1b'0}, i.e. always three bits
667 uint8_t ap = te->longDescFormat ? te->ap << 1 : te->ap;
668 uint8_t hap = te->hap;
669
670 if (sctlr.afe == 1 || te->longDescFormat)
671 ap |= 1;
672
673 bool abt;
674 bool isWritable = true;
675 // If this is a stage 2 access (eg for reading stage 1 page table entries)
676 // then don't perform the AP permissions check, we stil do the HAP check
677 // below.
678 if (isStage2) {
679 abt = false;
680 } else {
681 switch (ap) {
682 case 0:
683 DPRINTF(TLB, "Access permissions 0, checking rs:%#x\n",
684 (int)sctlr.rs);
685 if (!sctlr.xp) {
686 switch ((int)sctlr.rs) {
687 case 2:
688 abt = is_write;
689 break;
690 case 1:
691 abt = is_write || !is_priv;
692 break;
693 case 0:
694 case 3:
695 default:
696 abt = true;
697 break;
698 }
699 } else {
700 abt = true;
701 }
702 break;
703 case 1:
704 abt = !is_priv;
705 break;
706 case 2:
707 abt = !is_priv && is_write;
708 isWritable = is_priv;
709 break;
710 case 3:
711 abt = false;
712 break;
713 case 4:
714 panic("UNPRED premissions\n");
715 case 5:
716 abt = !is_priv || is_write;
717 isWritable = false;
718 break;
719 case 6:
720 case 7:
721 abt = is_write;
722 isWritable = false;
723 break;
724 default:
725 panic("Unknown permissions %#x\n", ap);
726 }
727 }
728
729 bool hapAbt = is_write ? !(hap & 2) : !(hap & 1);
730 bool xn = te->xn || (isWritable && sctlr.wxn) ||
731 (ap == 3 && sctlr.uwxn && is_priv);
732 if (is_fetch && (abt || xn ||
733 (te->longDescFormat && te->pxn && is_priv) ||
734 (isSecure && te->ns && scr.sif))) {
735 permsFaults++;
736 DPRINTF(TLB, "TLB Fault: Prefetch abort on permission check. AP:%d "
737 "priv:%d write:%d ns:%d sif:%d sctlr.afe: %d \n",
738 ap, is_priv, is_write, te->ns, scr.sif,sctlr.afe);
739 // Use PC value instead of vaddr because vaddr might be aligned to
740 // cache line and should not be the address reported in FAR
741 return std::make_shared<PrefetchAbort>(
742 req->getPC(),
743 ArmFault::PermissionLL + te->lookupLevel,
744 isStage2, tranMethod);
745 } else if (abt | hapAbt) {
746 permsFaults++;
747 DPRINTF(TLB, "TLB Fault: Data abort on permission check. AP:%d priv:%d"
748 " write:%d\n", ap, is_priv, is_write);
749 return std::make_shared<DataAbort>(
750 vaddr, te->domain, is_write,
751 ArmFault::PermissionLL + te->lookupLevel,
752 isStage2 | !abt, tranMethod);
753 }
754 return NoFault;
755 }
756
757
758 Fault
759 TLB::checkPermissions64(TlbEntry *te, const RequestPtr &req, Mode mode,
760 ThreadContext *tc)
761 {
762 assert(aarch64);
763
764 // A data cache maintenance instruction that operates by VA does
765 // not generate a Permission fault unless:
766 // * It is a data cache invalidate (dc ivac) which requires write
767 // permissions to the VA, or
768 // * It is executed from EL0
769 if (req->isCacheClean() && aarch64EL != EL0 && !isStage2) {
770 return NoFault;
771 }
772
773 Addr vaddr_tainted = req->getVaddr();
774 Addr vaddr = purifyTaggedAddr(vaddr_tainted, tc, aarch64EL, (TCR)ttbcr,
775 mode==Execute);
776
777 Request::Flags flags = req->getFlags();
778 bool is_fetch = (mode == Execute);
779 // Cache clean operations require read permissions to the specified VA
780 bool is_write = !req->isCacheClean() && mode == Write;
781 bool is_atomic = req->isAtomic();
782 bool is_priv M5_VAR_USED = isPriv && !(flags & UserMode);
783
784 updateMiscReg(tc, curTranType);
785
786 // If this is the second stage of translation and the request is for a
787 // stage 1 page table walk then we need to check the HCR.PTW bit. This
788 // allows us to generate a fault if the request targets an area marked
789 // as a device or strongly ordered.
790 if (isStage2 && req->isPTWalk() && hcr.ptw &&
791 (te->mtype != TlbEntry::MemoryType::Normal)) {
792 return std::make_shared<DataAbort>(
793 vaddr_tainted, te->domain, is_write,
794 ArmFault::PermissionLL + te->lookupLevel,
795 isStage2, ArmFault::LpaeTran);
796 }
797
798 // Generate an alignment fault for unaligned accesses to device or
799 // strongly ordered memory
800 if (!is_fetch) {
801 if (te->mtype != TlbEntry::MemoryType::Normal) {
802 if (vaddr & mask(flags & AlignmentMask)) {
803 alignFaults++;
804 return std::make_shared<DataAbort>(
805 vaddr_tainted,
806 TlbEntry::DomainType::NoAccess,
807 is_atomic ? false : is_write,
808 ArmFault::AlignmentFault, isStage2,
809 ArmFault::LpaeTran);
810 }
811 }
812 }
813
814 if (te->nonCacheable) {
815 // Prevent prefetching from I/O devices.
816 if (req->isPrefetch()) {
817 // Here we can safely use the fault status for the short
818 // desc. format in all cases
819 return std::make_shared<PrefetchAbort>(
820 vaddr_tainted,
821 ArmFault::PrefetchUncacheable,
822 isStage2, ArmFault::LpaeTran);
823 }
824 }
825
826 uint8_t ap = 0x3 & (te->ap); // 2-bit access protection field
827 bool grant = false;
828
829 bool wxn = sctlr.wxn;
830 uint8_t xn = te->xn;
831 uint8_t pxn = te->pxn;
832 bool r = (!is_write && !is_fetch);
833 bool w = is_write;
834 bool x = is_fetch;
835
836 // grant_read is used for faults from an atomic instruction that
837 // both reads and writes from a memory location. From a ISS point
838 // of view they count as read if a read to that address would have
839 // generated the fault; they count as writes otherwise
840 bool grant_read = true;
841 DPRINTF(TLBVerbose, "Checking permissions: ap:%d, xn:%d, pxn:%d, r:%d, "
842 "w:%d, x:%d, is_priv: %d, wxn: %d\n", ap, xn,
843 pxn, r, w, x, is_priv, wxn);
844
845 if (isStage2) {
846 assert(ArmSystem::haveVirtualization(tc) && aarch64EL != EL2);
847 // In stage 2 we use the hypervisor access permission bits.
848 // The following permissions are described in ARM DDI 0487A.f
849 // D4-1802
850 uint8_t hap = 0x3 & te->hap;
851 grant_read = hap & 0x1;
852 if (is_fetch) {
853 // sctlr.wxn overrides the xn bit
854 grant = !wxn && !xn;
855 } else if (is_atomic) {
856 grant = r && w;
857 grant_read = r;
858 } else if (is_write) {
859 grant = hap & 0x2;
860 } else { // is_read
861 grant = grant_read;
862 }
863 } else {
864 switch (aarch64EL) {
865 case EL0:
866 {
867 grant_read = ap & 0x1;
868 uint8_t perm = (ap << 2) | (xn << 1) | pxn;
869 switch (perm) {
870 case 0:
871 case 1:
872 case 8:
873 case 9:
874 grant = x;
875 break;
876 case 4:
877 case 5:
878 grant = r || w || (x && !wxn);
879 break;
880 case 6:
881 case 7:
882 grant = r || w;
883 break;
884 case 12:
885 case 13:
886 grant = r || x;
887 break;
888 case 14:
889 case 15:
890 grant = r;
891 break;
892 default:
893 grant = false;
894 }
895 }
896 break;
897 case EL1:
898 {
899 if (checkPAN(tc, ap, req, mode)) {
900 grant = false;
901 grant_read = false;
902 break;
903 }
904
905 uint8_t perm = (ap << 2) | (xn << 1) | pxn;
906 switch (perm) {
907 case 0:
908 case 2:
909 grant = r || w || (x && !wxn);
910 break;
911 case 1:
912 case 3:
913 case 4:
914 case 5:
915 case 6:
916 case 7:
917 // regions that are writeable at EL0 should not be
918 // executable at EL1
919 grant = r || w;
920 break;
921 case 8:
922 case 10:
923 case 12:
924 case 14:
925 grant = r || x;
926 break;
927 case 9:
928 case 11:
929 case 13:
930 case 15:
931 grant = r;
932 break;
933 default:
934 grant = false;
935 }
936 }
937 break;
938 case EL2:
939 if (hcr.e2h && checkPAN(tc, ap, req, mode)) {
940 grant = false;
941 grant_read = false;
942 break;
943 }
944 M5_FALLTHROUGH;
945 case EL3:
946 {
947 uint8_t perm = (ap & 0x2) | xn;
948 switch (perm) {
949 case 0:
950 grant = r || w || (x && !wxn);
951 break;
952 case 1:
953 grant = r || w;
954 break;
955 case 2:
956 grant = r || x;
957 break;
958 case 3:
959 grant = r;
960 break;
961 default:
962 grant = false;
963 }
964 }
965 break;
966 }
967 }
968
969 if (!grant) {
970 if (is_fetch) {
971 permsFaults++;
972 DPRINTF(TLB, "TLB Fault: Prefetch abort on permission check. "
973 "AP:%d priv:%d write:%d ns:%d sif:%d "
974 "sctlr.afe: %d\n",
975 ap, is_priv, is_write, te->ns, scr.sif, sctlr.afe);
976 // Use PC value instead of vaddr because vaddr might be aligned to
977 // cache line and should not be the address reported in FAR
978 return std::make_shared<PrefetchAbort>(
979 req->getPC(),
980 ArmFault::PermissionLL + te->lookupLevel,
981 isStage2, ArmFault::LpaeTran);
982 } else {
983 permsFaults++;
984 DPRINTF(TLB, "TLB Fault: Data abort on permission check. AP:%d "
985 "priv:%d write:%d\n", ap, is_priv, is_write);
986 return std::make_shared<DataAbort>(
987 vaddr_tainted, te->domain,
988 (is_atomic && !grant_read) ? false : is_write,
989 ArmFault::PermissionLL + te->lookupLevel,
990 isStage2, ArmFault::LpaeTran);
991 }
992 }
993
994 return NoFault;
995 }
996
997 bool
998 TLB::checkPAN(ThreadContext *tc, uint8_t ap, const RequestPtr &req, Mode mode)
999 {
1000 // The PAN bit has no effect on:
1001 // 1) Instruction accesses.
1002 // 2) Data Cache instructions other than DC ZVA
1003 // 3) Address translation instructions, other than ATS1E1RP and
1004 // ATS1E1WP when ARMv8.2-ATS1E1 is implemented. (Unimplemented in
1005 // gem5)
1006 // 4) Unprivileged instructions (Unimplemented in gem5)
1007 AA64MMFR1 mmfr1 = tc->readMiscReg(MISCREG_ID_AA64MMFR1_EL1);
1008 if (mmfr1.pan && cpsr.pan && (ap & 0x1) && mode != Execute &&
1009 (!req->isCacheMaintenance() ||
1010 (req->getFlags() & Request::CACHE_BLOCK_ZERO))) {
1011 return true;
1012 } else {
1013 return false;
1014 }
1015 }
1016
1017 Fault
1018 TLB::translateMmuOff(ThreadContext *tc, const RequestPtr &req, Mode mode,
1019 TLB::ArmTranslationType tranType, Addr vaddr, bool long_desc_format)
1020 {
1021 bool is_fetch = (mode == Execute);
1022 bool is_atomic = req->isAtomic();
1023 req->setPaddr(vaddr);
1024 // When the MMU is off the security attribute corresponds to the
1025 // security state of the processor
1026 if (isSecure)
1027 req->setFlags(Request::SECURE);
1028
1029 if (aarch64) {
1030 bool selbit = bits(vaddr, 55);
1031 TCR tcr1 = tc->readMiscReg(MISCREG_TCR_EL1);
1032 int topbit = computeAddrTop(tc, selbit, is_fetch, tcr1, currEL(tc));
1033 int addr_sz = bits(vaddr, topbit, MaxPhysAddrRange);
1034 if (addr_sz != 0){
1035 Fault f;
1036 if (is_fetch)
1037 f = std::make_shared<PrefetchAbort>(vaddr,
1038 ArmFault::AddressSizeLL, isStage2, ArmFault::LpaeTran);
1039 else
1040 f = std::make_shared<DataAbort>( vaddr,
1041 TlbEntry::DomainType::NoAccess,
1042 is_atomic ? false : mode==Write,
1043 ArmFault::AddressSizeLL, isStage2, ArmFault::LpaeTran);
1044 return f;
1045 }
1046 }
1047
1048 // @todo: double check this (ARM ARM issue C B3.2.1)
1049 if (long_desc_format || sctlr.tre == 0 || nmrr.ir0 == 0 ||
1050 nmrr.or0 == 0 || prrr.tr0 != 0x2) {
1051 if (!req->isCacheMaintenance()) {
1052 req->setFlags(Request::UNCACHEABLE);
1053 }
1054 req->setFlags(Request::STRICT_ORDER);
1055 }
1056
1057 // Set memory attributes
1058 TlbEntry temp_te;
1059 temp_te.ns = !isSecure;
1060 bool dc = (HaveVirtHostExt(tc)
1061 && hcr.e2h == 1 && hcr.tge == 1) ? 0: hcr.dc;
1062 bool i_cacheability = sctlr.i && !sctlr.m;
1063 if (isStage2 || !dc || isSecure ||
1064 (isHyp && !(tranType & S1CTran))) {
1065
1066 temp_te.mtype = is_fetch ? TlbEntry::MemoryType::Normal
1067 : TlbEntry::MemoryType::StronglyOrdered;
1068 temp_te.innerAttrs = i_cacheability? 0x2: 0x0;
1069 temp_te.outerAttrs = i_cacheability? 0x2: 0x0;
1070 temp_te.shareable = true;
1071 temp_te.outerShareable = true;
1072 } else {
1073 temp_te.mtype = TlbEntry::MemoryType::Normal;
1074 temp_te.innerAttrs = 0x3;
1075 temp_te.outerAttrs = 0x3;
1076 temp_te.shareable = false;
1077 temp_te.outerShareable = false;
1078 }
1079 temp_te.setAttributes(long_desc_format);
1080 DPRINTF(TLBVerbose, "(No MMU) setting memory attributes: shareable: "
1081 "%d, innerAttrs: %d, outerAttrs: %d, isStage2: %d\n",
1082 temp_te.shareable, temp_te.innerAttrs, temp_te.outerAttrs,
1083 isStage2);
1084 setAttr(temp_te.attributes);
1085
1086 return testTranslation(req, mode, TlbEntry::DomainType::NoAccess);
1087 }
1088
1089 Fault
1090 TLB::translateMmuOn(ThreadContext* tc, const RequestPtr &req, Mode mode,
1091 Translation *translation, bool &delay, bool timing,
1092 bool functional, Addr vaddr,
1093 ArmFault::TranMethod tranMethod)
1094 {
1095 TlbEntry *te = NULL;
1096 bool is_fetch = (mode == Execute);
1097 TlbEntry mergeTe;
1098
1099 Request::Flags flags = req->getFlags();
1100 Addr vaddr_tainted = req->getVaddr();
1101
1102 Fault fault = getResultTe(&te, req, tc, mode, translation, timing,
1103 functional, &mergeTe);
1104 // only proceed if we have a valid table entry
1105 if ((te == NULL) && (fault == NoFault)) delay = true;
1106
1107 // If we have the table entry transfer some of the attributes to the
1108 // request that triggered the translation
1109 if (te != NULL) {
1110 // Set memory attributes
1111 DPRINTF(TLBVerbose,
1112 "Setting memory attributes: shareable: %d, innerAttrs: %d, "
1113 "outerAttrs: %d, mtype: %d, isStage2: %d\n",
1114 te->shareable, te->innerAttrs, te->outerAttrs,
1115 static_cast<uint8_t>(te->mtype), isStage2);
1116 setAttr(te->attributes);
1117
1118 if (te->nonCacheable && !req->isCacheMaintenance())
1119 req->setFlags(Request::UNCACHEABLE);
1120
1121 // Require requests to be ordered if the request goes to
1122 // strongly ordered or device memory (i.e., anything other
1123 // than normal memory requires strict order).
1124 if (te->mtype != TlbEntry::MemoryType::Normal)
1125 req->setFlags(Request::STRICT_ORDER);
1126
1127 Addr pa = te->pAddr(vaddr);
1128 req->setPaddr(pa);
1129
1130 if (isSecure && !te->ns) {
1131 req->setFlags(Request::SECURE);
1132 }
1133 if (!is_fetch && fault == NoFault &&
1134 (vaddr & mask(flags & AlignmentMask)) &&
1135 (te->mtype != TlbEntry::MemoryType::Normal)) {
1136 // Unaligned accesses to Device memory should always cause an
1137 // abort regardless of sctlr.a
1138 alignFaults++;
1139 bool is_write = (mode == Write);
1140 return std::make_shared<DataAbort>(
1141 vaddr_tainted,
1142 TlbEntry::DomainType::NoAccess, is_write,
1143 ArmFault::AlignmentFault, isStage2,
1144 tranMethod);
1145 }
1146
1147 // Check for a trickbox generated address fault
1148 if (fault == NoFault)
1149 fault = testTranslation(req, mode, te->domain);
1150 }
1151
1152 if (fault == NoFault) {
1153 // Don't try to finalize a physical address unless the
1154 // translation has completed (i.e., there is a table entry).
1155 return te ? finalizePhysical(req, tc, mode) : NoFault;
1156 } else {
1157 return fault;
1158 }
1159 }
1160
1161 Fault
1162 TLB::translateFs(const RequestPtr &req, ThreadContext *tc, Mode mode,
1163 Translation *translation, bool &delay, bool timing,
1164 TLB::ArmTranslationType tranType, bool functional)
1165 {
1166 // No such thing as a functional timing access
1167 assert(!(timing && functional));
1168
1169 updateMiscReg(tc, tranType);
1170
1171 Addr vaddr_tainted = req->getVaddr();
1172 Addr vaddr = 0;
1173 if (aarch64)
1174 vaddr = purifyTaggedAddr(vaddr_tainted, tc, aarch64EL, (TCR)ttbcr,
1175 mode==Execute);
1176 else
1177 vaddr = vaddr_tainted;
1178 Request::Flags flags = req->getFlags();
1179
1180 bool is_fetch = (mode == Execute);
1181 bool is_write = (mode == Write);
1182 bool long_desc_format = aarch64 || longDescFormatInUse(tc);
1183 ArmFault::TranMethod tranMethod = long_desc_format ? ArmFault::LpaeTran
1184 : ArmFault::VmsaTran;
1185
1186 DPRINTF(TLBVerbose,
1187 "CPSR is priv:%d UserMode:%d secure:%d S1S2NsTran:%d\n",
1188 isPriv, flags & UserMode, isSecure, tranType & S1S2NsTran);
1189
1190 DPRINTF(TLB, "translateFs addr %#x, mode %d, st2 %d, scr %#x sctlr %#x "
1191 "flags %#lx tranType 0x%x\n", vaddr_tainted, mode, isStage2,
1192 scr, sctlr, flags, tranType);
1193
1194 if ((req->isInstFetch() && (!sctlr.i)) ||
1195 ((!req->isInstFetch()) && (!sctlr.c))){
1196 if (!req->isCacheMaintenance()) {
1197 req->setFlags(Request::UNCACHEABLE);
1198 }
1199 req->setFlags(Request::STRICT_ORDER);
1200 }
1201 if (!is_fetch) {
1202 if (sctlr.a || !(flags & AllowUnaligned)) {
1203 if (vaddr & mask(flags & AlignmentMask)) {
1204 alignFaults++;
1205 return std::make_shared<DataAbort>(
1206 vaddr_tainted,
1207 TlbEntry::DomainType::NoAccess, is_write,
1208 ArmFault::AlignmentFault, isStage2,
1209 tranMethod);
1210 }
1211 }
1212 }
1213
1214 bool vm = hcr.vm;
1215 if (HaveVirtHostExt(tc) && hcr.e2h == 1 && hcr.tge ==1)
1216 vm = 0;
1217 else if (hcr.dc == 1)
1218 vm = 1;
1219
1220 Fault fault = NoFault;
1221 // If guest MMU is off or hcr.vm=0 go straight to stage2
1222 if ((isStage2 && !vm) || (!isStage2 && !sctlr.m)) {
1223 fault = translateMmuOff(tc, req, mode, tranType, vaddr,
1224 long_desc_format);
1225 } else {
1226 DPRINTF(TLBVerbose, "Translating %s=%#x context=%d\n",
1227 isStage2 ? "IPA" : "VA", vaddr_tainted, asid);
1228 // Translation enabled
1229 fault = translateMmuOn(tc, req, mode, translation, delay, timing,
1230 functional, vaddr, tranMethod);
1231 }
1232
1233 //Check for Debug Exceptions
1234 if (fault == NoFault) {
1235 auto *isa = static_cast<ArmISA::ISA *>(tc->getIsaPtr());
1236 SelfDebug *sd = isa->getSelfDebug();
1237
1238 fault = sd->testDebug(tc, req, mode);
1239 }
1240
1241 return fault;
1242 }
1243
1244 Fault
1245 TLB::translateAtomic(const RequestPtr &req, ThreadContext *tc, Mode mode,
1246 TLB::ArmTranslationType tranType)
1247 {
1248 updateMiscReg(tc, tranType);
1249
1250 if (directToStage2) {
1251 assert(stage2Tlb);
1252 return stage2Tlb->translateAtomic(req, tc, mode, tranType);
1253 }
1254
1255 bool delay = false;
1256 Fault fault;
1257 if (FullSystem)
1258 fault = translateFs(req, tc, mode, NULL, delay, false, tranType);
1259 else
1260 fault = translateSe(req, tc, mode, NULL, delay, false);
1261 assert(!delay);
1262 return fault;
1263 }
1264
1265 Fault
1266 TLB::translateFunctional(const RequestPtr &req, ThreadContext *tc, Mode mode,
1267 TLB::ArmTranslationType tranType)
1268 {
1269 updateMiscReg(tc, tranType);
1270
1271 if (directToStage2) {
1272 assert(stage2Tlb);
1273 return stage2Tlb->translateFunctional(req, tc, mode, tranType);
1274 }
1275
1276 bool delay = false;
1277 Fault fault;
1278 if (FullSystem)
1279 fault = translateFs(req, tc, mode, NULL, delay, false, tranType, true);
1280 else
1281 fault = translateSe(req, tc, mode, NULL, delay, false);
1282 assert(!delay);
1283 return fault;
1284 }
1285
1286 void
1287 TLB::translateTiming(const RequestPtr &req, ThreadContext *tc,
1288 Translation *translation, Mode mode, TLB::ArmTranslationType tranType)
1289 {
1290 updateMiscReg(tc, tranType);
1291
1292 if (directToStage2) {
1293 assert(stage2Tlb);
1294 stage2Tlb->translateTiming(req, tc, translation, mode, tranType);
1295 return;
1296 }
1297
1298 assert(translation);
1299
1300 translateComplete(req, tc, translation, mode, tranType, isStage2);
1301 }
1302
1303 Fault
1304 TLB::translateComplete(const RequestPtr &req, ThreadContext *tc,
1305 Translation *translation, Mode mode, TLB::ArmTranslationType tranType,
1306 bool callFromS2)
1307 {
1308 bool delay = false;
1309 Fault fault;
1310 if (FullSystem)
1311 fault = translateFs(req, tc, mode, translation, delay, true, tranType);
1312 else
1313 fault = translateSe(req, tc, mode, translation, delay, true);
1314 DPRINTF(TLBVerbose, "Translation returning delay=%d fault=%d\n", delay, fault !=
1315 NoFault);
1316 // If we have a translation, and we're not in the middle of doing a stage
1317 // 2 translation tell the translation that we've either finished or its
1318 // going to take a while. By not doing this when we're in the middle of a
1319 // stage 2 translation we prevent marking the translation as delayed twice,
1320 // one when the translation starts and again when the stage 1 translation
1321 // completes.
1322
1323 if (translation && (callFromS2 || !stage2Req || req->hasPaddr() ||
1324 fault != NoFault)) {
1325 if (!delay)
1326 translation->finish(fault, req, tc, mode);
1327 else
1328 translation->markDelayed();
1329 }
1330 return fault;
1331 }
1332
1333 Port *
1334 TLB::getTableWalkerPort()
1335 {
1336 return &stage2Mmu->getDMAPort();
1337 }
1338
1339 void
1340 TLB::updateMiscReg(ThreadContext *tc, ArmTranslationType tranType)
1341 {
1342 // check if the regs have changed, or the translation mode is different.
1343 // NOTE: the tran type doesn't affect stage 2 TLB's as they only handle
1344 // one type of translation anyway
1345 if (miscRegValid && miscRegContext == tc->contextId() &&
1346 ((tranType == curTranType) || isStage2)) {
1347 return;
1348 }
1349
1350 DPRINTF(TLBVerbose, "TLB variables changed!\n");
1351 cpsr = tc->readMiscReg(MISCREG_CPSR);
1352
1353 // Dependencies: SCR/SCR_EL3, CPSR
1354 isSecure = inSecureState(tc) &&
1355 !(tranType & HypMode) && !(tranType & S1S2NsTran);
1356
1357 aarch64EL = tranTypeEL(cpsr, tranType);
1358 aarch64 = isStage2 ?
1359 ELIs64(tc, EL2) :
1360 ELIs64(tc, aarch64EL == EL0 ? EL1 : aarch64EL);
1361
1362 hcr = tc->readMiscReg(MISCREG_HCR_EL2);
1363 if (aarch64) { // AArch64
1364 // determine EL we need to translate in
1365 switch (aarch64EL) {
1366 case EL0:
1367 if (HaveVirtHostExt(tc) && hcr.tge == 1 && hcr.e2h == 1) {
1368 // VHE code for EL2&0 regime
1369 sctlr = tc->readMiscReg(MISCREG_SCTLR_EL2);
1370 ttbcr = tc->readMiscReg(MISCREG_TCR_EL2);
1371 uint64_t ttbr_asid = ttbcr.a1 ?
1372 tc->readMiscReg(MISCREG_TTBR1_EL2) :
1373 tc->readMiscReg(MISCREG_TTBR0_EL2);
1374 asid = bits(ttbr_asid,
1375 (haveLargeAsid64 && ttbcr.as) ? 63 : 55, 48);
1376
1377 } else {
1378 sctlr = tc->readMiscReg(MISCREG_SCTLR_EL1);
1379 ttbcr = tc->readMiscReg(MISCREG_TCR_EL1);
1380 uint64_t ttbr_asid = ttbcr.a1 ?
1381 tc->readMiscReg(MISCREG_TTBR1_EL1) :
1382 tc->readMiscReg(MISCREG_TTBR0_EL1);
1383 asid = bits(ttbr_asid,
1384 (haveLargeAsid64 && ttbcr.as) ? 63 : 55, 48);
1385
1386 }
1387 break;
1388 case EL1:
1389 {
1390 sctlr = tc->readMiscReg(MISCREG_SCTLR_EL1);
1391 ttbcr = tc->readMiscReg(MISCREG_TCR_EL1);
1392 uint64_t ttbr_asid = ttbcr.a1 ?
1393 tc->readMiscReg(MISCREG_TTBR1_EL1) :
1394 tc->readMiscReg(MISCREG_TTBR0_EL1);
1395 asid = bits(ttbr_asid,
1396 (haveLargeAsid64 && ttbcr.as) ? 63 : 55, 48);
1397 }
1398 break;
1399 case EL2:
1400 sctlr = tc->readMiscReg(MISCREG_SCTLR_EL2);
1401 ttbcr = tc->readMiscReg(MISCREG_TCR_EL2);
1402 if (hcr.e2h == 1) {
1403 // VHE code for EL2&0 regime
1404 uint64_t ttbr_asid = ttbcr.a1 ?
1405 tc->readMiscReg(MISCREG_TTBR1_EL2) :
1406 tc->readMiscReg(MISCREG_TTBR0_EL2);
1407 asid = bits(ttbr_asid,
1408 (haveLargeAsid64 && ttbcr.as) ? 63 : 55, 48);
1409 } else {
1410 asid = -1;
1411 }
1412 break;
1413 case EL3:
1414 sctlr = tc->readMiscReg(MISCREG_SCTLR_EL3);
1415 ttbcr = tc->readMiscReg(MISCREG_TCR_EL3);
1416 asid = -1;
1417 break;
1418 }
1419
1420 scr = tc->readMiscReg(MISCREG_SCR_EL3);
1421 isPriv = aarch64EL != EL0;
1422 if (haveVirtualization) {
1423 vmid = bits(tc->readMiscReg(MISCREG_VTTBR_EL2), 55, 48);
1424 isHyp = aarch64EL == EL2;
1425 isHyp |= tranType & HypMode;
1426 isHyp &= (tranType & S1S2NsTran) == 0;
1427 isHyp &= (tranType & S1CTran) == 0;
1428
1429 if (hcr.e2h == 1 && (aarch64EL == EL2
1430 || (hcr.tge ==1 && aarch64EL == EL0))) {
1431 isHyp = true;
1432 directToStage2 = false;
1433 stage2Req = false;
1434 stage2DescReq = false;
1435 } else {
1436 // Work out if we should skip the first stage of translation and go
1437 // directly to stage 2. This value is cached so we don't have to
1438 // compute it for every translation.
1439 bool vm = hcr.vm;
1440 if (HaveVirtHostExt(tc) && hcr.e2h == 1 && hcr.tge == 1) {
1441 vm = 0;
1442 }
1443
1444 stage2Req = isStage2 ||
1445 (vm && !isHyp && !isSecure &&
1446 !(tranType & S1CTran) && (aarch64EL < EL2) &&
1447 !(tranType & S1E1Tran)); // <--- FIX THIS HACK
1448 stage2DescReq = isStage2 || (vm && !isHyp && !isSecure &&
1449 (aarch64EL < EL2));
1450 directToStage2 = !isStage2 && stage2Req && !sctlr.m;
1451 }
1452 } else {
1453 vmid = 0;
1454 isHyp = false;
1455 directToStage2 = false;
1456 stage2Req = false;
1457 stage2DescReq = false;
1458 }
1459 } else { // AArch32
1460 sctlr = tc->readMiscReg(snsBankedIndex(MISCREG_SCTLR, tc,
1461 !isSecure));
1462 ttbcr = tc->readMiscReg(snsBankedIndex(MISCREG_TTBCR, tc,
1463 !isSecure));
1464 scr = tc->readMiscReg(MISCREG_SCR);
1465 isPriv = cpsr.mode != MODE_USER;
1466 if (longDescFormatInUse(tc)) {
1467 uint64_t ttbr_asid = tc->readMiscReg(
1468 snsBankedIndex(ttbcr.a1 ? MISCREG_TTBR1 :
1469 MISCREG_TTBR0,
1470 tc, !isSecure));
1471 asid = bits(ttbr_asid, 55, 48);
1472 } else { // Short-descriptor translation table format in use
1473 CONTEXTIDR context_id = tc->readMiscReg(snsBankedIndex(
1474 MISCREG_CONTEXTIDR, tc,!isSecure));
1475 asid = context_id.asid;
1476 }
1477 prrr = tc->readMiscReg(snsBankedIndex(MISCREG_PRRR, tc,
1478 !isSecure));
1479 nmrr = tc->readMiscReg(snsBankedIndex(MISCREG_NMRR, tc,
1480 !isSecure));
1481 dacr = tc->readMiscReg(snsBankedIndex(MISCREG_DACR, tc,
1482 !isSecure));
1483 hcr = tc->readMiscReg(MISCREG_HCR);
1484
1485 if (haveVirtualization) {
1486 vmid = bits(tc->readMiscReg(MISCREG_VTTBR), 55, 48);
1487 isHyp = cpsr.mode == MODE_HYP;
1488 isHyp |= tranType & HypMode;
1489 isHyp &= (tranType & S1S2NsTran) == 0;
1490 isHyp &= (tranType & S1CTran) == 0;
1491 if (isHyp) {
1492 sctlr = tc->readMiscReg(MISCREG_HSCTLR);
1493 }
1494 // Work out if we should skip the first stage of translation and go
1495 // directly to stage 2. This value is cached so we don't have to
1496 // compute it for every translation.
1497 stage2Req = hcr.vm && !isStage2 && !isHyp && !isSecure &&
1498 !(tranType & S1CTran);
1499 stage2DescReq = hcr.vm && !isStage2 && !isHyp && !isSecure;
1500 directToStage2 = stage2Req && !sctlr.m;
1501 } else {
1502 vmid = 0;
1503 stage2Req = false;
1504 isHyp = false;
1505 directToStage2 = false;
1506 stage2DescReq = false;
1507 }
1508 }
1509 miscRegValid = true;
1510 miscRegContext = tc->contextId();
1511 curTranType = tranType;
1512 }
1513
1514 ExceptionLevel
1515 TLB::tranTypeEL(CPSR cpsr, ArmTranslationType type)
1516 {
1517 switch (type) {
1518 case S1E0Tran:
1519 case S12E0Tran:
1520 return EL0;
1521
1522 case S1E1Tran:
1523 case S12E1Tran:
1524 return EL1;
1525
1526 case S1E2Tran:
1527 return EL2;
1528
1529 case S1E3Tran:
1530 return EL3;
1531
1532 case NormalTran:
1533 case S1CTran:
1534 case S1S2NsTran:
1535 case HypMode:
1536 return currEL(cpsr);
1537
1538 default:
1539 panic("Unknown translation mode!\n");
1540 }
1541 }
1542
1543 Fault
1544 TLB::getTE(TlbEntry **te, const RequestPtr &req, ThreadContext *tc, Mode mode,
1545 Translation *translation, bool timing, bool functional,
1546 bool is_secure, TLB::ArmTranslationType tranType)
1547 {
1548 // In a 2-stage system, the IPA->PA translation can be started via this
1549 // call so make sure the miscRegs are correct.
1550 if (isStage2) {
1551 updateMiscReg(tc, tranType);
1552 }
1553 bool is_fetch = (mode == Execute);
1554 bool is_write = (mode == Write);
1555
1556 Addr vaddr_tainted = req->getVaddr();
1557 Addr vaddr = 0;
1558 ExceptionLevel target_el = aarch64 ? aarch64EL : EL1;
1559 if (aarch64) {
1560 vaddr = purifyTaggedAddr(vaddr_tainted, tc, target_el, (TCR)ttbcr,
1561 mode==Execute);
1562 } else {
1563 vaddr = vaddr_tainted;
1564 }
1565 *te = lookup(vaddr, asid, vmid, isHyp, is_secure, false, false, target_el,
1566 false);
1567 if (*te == NULL) {
1568 if (req->isPrefetch()) {
1569 // if the request is a prefetch don't attempt to fill the TLB or go
1570 // any further with the memory access (here we can safely use the
1571 // fault status for the short desc. format in all cases)
1572 prefetchFaults++;
1573 return std::make_shared<PrefetchAbort>(
1574 vaddr_tainted, ArmFault::PrefetchTLBMiss, isStage2);
1575 }
1576
1577 if (is_fetch)
1578 instMisses++;
1579 else if (is_write)
1580 writeMisses++;
1581 else
1582 readMisses++;
1583
1584 // start translation table walk, pass variables rather than
1585 // re-retreaving in table walker for speed
1586 DPRINTF(TLB, "TLB Miss: Starting hardware table walker for %#x(%d:%d)\n",
1587 vaddr_tainted, asid, vmid);
1588 Fault fault;
1589 fault = tableWalker->walk(req, tc, asid, vmid, isHyp, mode,
1590 translation, timing, functional, is_secure,
1591 tranType, stage2DescReq);
1592 // for timing mode, return and wait for table walk,
1593 if (timing || fault != NoFault) {
1594 return fault;
1595 }
1596
1597 *te = lookup(vaddr, asid, vmid, isHyp, is_secure, false, false,
1598 target_el, false);
1599 if (!*te)
1600 printTlb();
1601 assert(*te);
1602 } else {
1603 if (is_fetch)
1604 instHits++;
1605 else if (is_write)
1606 writeHits++;
1607 else
1608 readHits++;
1609 }
1610 return NoFault;
1611 }
1612
1613 Fault
1614 TLB::getResultTe(TlbEntry **te, const RequestPtr &req,
1615 ThreadContext *tc, Mode mode,
1616 Translation *translation, bool timing, bool functional,
1617 TlbEntry *mergeTe)
1618 {
1619 Fault fault;
1620
1621 if (isStage2) {
1622 // We are already in the stage 2 TLB. Grab the table entry for stage
1623 // 2 only. We are here because stage 1 translation is disabled.
1624 TlbEntry *s2Te = NULL;
1625 // Get the stage 2 table entry
1626 fault = getTE(&s2Te, req, tc, mode, translation, timing, functional,
1627 isSecure, curTranType);
1628 // Check permissions of stage 2
1629 if ((s2Te != NULL) && (fault == NoFault)) {
1630 if (aarch64)
1631 fault = checkPermissions64(s2Te, req, mode, tc);
1632 else
1633 fault = checkPermissions(s2Te, req, mode);
1634 }
1635 *te = s2Te;
1636 return fault;
1637 }
1638
1639 TlbEntry *s1Te = NULL;
1640
1641 Addr vaddr_tainted = req->getVaddr();
1642
1643 // Get the stage 1 table entry
1644 fault = getTE(&s1Te, req, tc, mode, translation, timing, functional,
1645 isSecure, curTranType);
1646 // only proceed if we have a valid table entry
1647 if ((s1Te != NULL) && (fault == NoFault)) {
1648 // Check stage 1 permissions before checking stage 2
1649 if (aarch64)
1650 fault = checkPermissions64(s1Te, req, mode, tc);
1651 else
1652 fault = checkPermissions(s1Te, req, mode);
1653 if (stage2Req & (fault == NoFault)) {
1654 Stage2LookUp *s2Lookup = new Stage2LookUp(this, stage2Tlb, *s1Te,
1655 req, translation, mode, timing, functional, curTranType);
1656 fault = s2Lookup->getTe(tc, mergeTe);
1657 if (s2Lookup->isComplete()) {
1658 *te = mergeTe;
1659 // We've finished with the lookup so delete it
1660 delete s2Lookup;
1661 } else {
1662 // The lookup hasn't completed, so we can't delete it now. We
1663 // get round this by asking the object to self delete when the
1664 // translation is complete.
1665 s2Lookup->setSelfDelete();
1666 }
1667 } else {
1668 // This case deals with an S1 hit (or bypass), followed by
1669 // an S2 hit-but-perms issue
1670 if (isStage2) {
1671 DPRINTF(TLBVerbose, "s2TLB: reqVa %#x, reqPa %#x, fault %p\n",
1672 vaddr_tainted, req->hasPaddr() ? req->getPaddr() : ~0, fault);
1673 if (fault != NoFault) {
1674 ArmFault *armFault = reinterpret_cast<ArmFault *>(fault.get());
1675 armFault->annotate(ArmFault::S1PTW, false);
1676 armFault->annotate(ArmFault::OVA, vaddr_tainted);
1677 }
1678 }
1679 *te = s1Te;
1680 }
1681 }
1682 return fault;
1683 }
1684
1685 void
1686 TLB::setTestInterface(SimObject *_ti)
1687 {
1688 if (!_ti) {
1689 test = nullptr;
1690 } else {
1691 TlbTestInterface *ti(dynamic_cast<TlbTestInterface *>(_ti));
1692 fatal_if(!ti, "%s is not a valid ARM TLB tester\n", _ti->name());
1693 test = ti;
1694 }
1695 }
1696
1697 Fault
1698 TLB::testTranslation(const RequestPtr &req, Mode mode,
1699 TlbEntry::DomainType domain)
1700 {
1701 if (!test || !req->hasSize() || req->getSize() == 0 ||
1702 req->isCacheMaintenance()) {
1703 return NoFault;
1704 } else {
1705 return test->translationCheck(req, isPriv, mode, domain);
1706 }
1707 }
1708
1709 Fault
1710 TLB::testWalk(Addr pa, Addr size, Addr va, bool is_secure, Mode mode,
1711 TlbEntry::DomainType domain, LookupLevel lookup_level)
1712 {
1713 if (!test) {
1714 return NoFault;
1715 } else {
1716 return test->walkCheck(pa, size, va, is_secure, isPriv, mode,
1717 domain, lookup_level);
1718 }
1719 }
1720
1721
1722 ArmISA::TLB *
1723 ArmTLBParams::create()
1724 {
1725 return new ArmISA::TLB(this);
1726 }