2 * Copyright (c) 2010-2013, 2016, 2019-2020 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.
41 #ifndef __ARCH_ARM_TLB_HH__
42 #define __ARCH_ARM_TLB_HH__
45 #include "arch/arm/faults.hh"
46 #include "arch/arm/isa_traits.hh"
47 #include "arch/arm/pagetable.hh"
48 #include "arch/arm/utility.hh"
49 #include "arch/generic/tlb.hh"
50 #include "base/statistics.hh"
51 #include "mem/request.hh"
52 #include "params/ArmTLB.hh"
53 #include "sim/probe/pmu.hh"
73 class TlbTestInterface
77 virtual ~TlbTestInterface() {}
80 * Check if a TLB translation should be forced to fail.
82 * @param req Request requiring a translation.
83 * @param is_priv Access from a privileged mode (i.e., not EL0)
84 * @param mode Access type
85 * @param domain Domain type
87 virtual Fault translationCheck(const RequestPtr &req, bool is_priv,
89 TlbEntry::DomainType domain) = 0;
92 * Check if a page table walker access should be forced to fail.
94 * @param pa Physical address the walker is accessing
95 * @param size Walker access size
96 * @param va Virtual address that initiated the walk
97 * @param is_secure Access from secure state
98 * @param is_priv Access from a privileged mode (i.e., not EL0)
99 * @param mode Access type
100 * @param domain Domain type
101 * @param lookup_level Page table walker level
103 virtual Fault walkCheck(Addr pa, Addr size, Addr va, bool is_secure,
104 Addr is_priv, BaseTLB::Mode mode,
105 TlbEntry::DomainType domain,
106 LookupLevel lookup_level) = 0;
109 class TLB : public BaseTLB
118 AlignDoubleWord = 0x3,
122 AllowUnaligned = 0x8,
123 // Priv code operating as if it wasn't
127 enum ArmTranslationType {
131 // Secure code operating as if it wasn't (required by some Address
132 // Translate operations)
134 // Address translation instructions (eg AT S1E0R_Xt) need to be handled
135 // in special ways during translation because they could need to act
136 // like a different EL than the current EL. The following flags are
137 // for these instructions
147 * Determine the EL to use for the purpose of a translation given
148 * a specific translation type. If the translation type doesn't
149 * specify an EL, we use the current EL.
151 static ExceptionLevel tranTypeEL(CPSR cpsr, ArmTranslationType type);
154 TlbEntry* table; // the Page Table
155 int size; // TLB Size
156 bool isStage2; // Indicates this TLB is part of the second stage MMU
157 bool stage2Req; // Indicates whether a stage 2 lookup is also required
158 // Indicates whether a stage 2 lookup of the table descriptors is required.
159 // Certain address translation instructions will intercept the IPA but the
160 // table descriptors still need to be translated by the stage2.
162 uint64_t _attr; // Memory attributes for last accessed TLB entry
163 bool directToStage2; // Indicates whether all translation requests should
164 // be routed directly to the stage 2 TLB
166 TableWalker *tableWalker;
168 Stage2MMU *stage2Mmu;
170 TlbTestInterface *test;
172 struct TlbStats : public Stats::Group
174 TlbStats(Stats::Group *parent);
176 mutable Stats::Scalar instHits;
177 mutable Stats::Scalar instMisses;
178 mutable Stats::Scalar readHits;
179 mutable Stats::Scalar readMisses;
180 mutable Stats::Scalar writeHits;
181 mutable Stats::Scalar writeMisses;
182 mutable Stats::Scalar inserts;
183 mutable Stats::Scalar flushTlb;
184 mutable Stats::Scalar flushTlbMva;
185 mutable Stats::Scalar flushTlbMvaAsid;
186 mutable Stats::Scalar flushTlbAsid;
187 mutable Stats::Scalar flushedEntries;
188 mutable Stats::Scalar alignFaults;
189 mutable Stats::Scalar prefetchFaults;
190 mutable Stats::Scalar domainFaults;
191 mutable Stats::Scalar permsFaults;
193 Stats::Formula readAccesses;
194 Stats::Formula writeAccesses;
195 Stats::Formula instAccesses;
197 Stats::Formula misses;
198 Stats::Formula accesses;
201 /** PMU probe for TLB refills */
202 ProbePoints::PMUUPtr ppRefills;
204 int rangeMRU; //On lookup, only move entries ahead when outside rangeMRU
207 TLB(const ArmTLBParams &p);
208 TLB(const Params &p, int _size, TableWalker *_walker);
210 /** Lookup an entry in the TLB
211 * @param vpn virtual address
212 * @param asn context id/address space id to use
213 * @param vmid The virtual machine ID used for stage 2 translation
214 * @param secure if the lookup is secure
215 * @param hyp if the lookup is done from hyp mode
216 * @param functional if the lookup should modify state
217 * @param ignore_asn if on lookup asn should be ignored
218 * @return pointer to TLB entry if it exists
220 TlbEntry *lookup(Addr vpn, uint16_t asn, uint8_t vmid, bool hyp,
221 bool secure, bool functional,
222 bool ignore_asn, ExceptionLevel target_el,
227 void takeOverFrom(BaseTLB *otlb) override;
229 /// setup all the back pointers
230 void init() override;
232 void setTestInterface(SimObject *ti);
234 TableWalker *getTableWalker() { return tableWalker; }
236 void setMMU(Stage2MMU *m, RequestorID requestor_id);
238 int getsize() const { return size; }
240 void insert(Addr vaddr, TlbEntry &pte);
242 Fault getTE(TlbEntry **te, const RequestPtr &req,
243 ThreadContext *tc, Mode mode,
244 Translation *translation, bool timing, bool functional,
245 bool is_secure, ArmTranslationType tranType);
247 Fault getResultTe(TlbEntry **te, const RequestPtr &req,
248 ThreadContext *tc, Mode mode,
249 Translation *translation, bool timing,
250 bool functional, TlbEntry *mergeTe);
252 Fault checkPermissions(TlbEntry *te, const RequestPtr &req, Mode mode);
253 Fault checkPermissions64(TlbEntry *te, const RequestPtr &req, Mode mode,
255 bool checkPAN(ThreadContext *tc, uint8_t ap, const RequestPtr &req,
258 /** Reset the entire TLB. Used for CPU switching to prevent stale
259 * translations after multiple switches
261 void flushAll() override;
264 /** Reset the entire TLB
266 void flush(const TLBIALL &tlbi_op);
268 /** Implementaton of AArch64 TLBI ALLE1(IS), ALLE2(IS), ALLE3(IS)
271 void flush(const TLBIALLEL &tlbi_op);
273 /** Implementaton of AArch64 TLBI VMALLE1(IS)/VMALLS112E1(IS)
276 void flush(const TLBIVMALL &tlbi_op);
278 /** Remove all entries in the non secure world, depending on whether they
279 * were allocated in hyp mode or not
281 void flush(const TLBIALLN &tlbi_op);
283 /** Remove any entries that match both a va and asn
285 void flush(const TLBIMVA &tlbi_op);
287 /** Remove any entries that match the asn
289 void flush(const TLBIASID &tlbi_op);
291 /** Remove all entries that match the va regardless of asn
293 void flush(const TLBIMVAA &tlbi_op);
296 * Invalidate all entries in the stage 2 TLB that match the given ipa
297 * and the current VMID
299 void flush(const TLBIIPA &tlbi_op);
301 Fault trickBoxCheck(const RequestPtr &req, Mode mode,
302 TlbEntry::DomainType domain);
304 Fault walkTrickBoxCheck(Addr pa, bool is_secure, Addr va, Addr sz,
305 bool is_exec, bool is_write,
306 TlbEntry::DomainType domain,
307 LookupLevel lookup_level);
309 void printTlb() const;
311 void demapPage(Addr vaddr, uint64_t asn) override
313 // needed for x86 only
314 panic("demapPage() is not implemented.\n");
318 * Do a functional lookup on the TLB (for debugging)
319 * and don't modify any internal state
320 * @param tc thread context to get the context id from
321 * @param vaddr virtual address to translate
322 * @param pa returned physical address
323 * @return if the translation was successful
325 bool translateFunctional(ThreadContext *tc, Addr vaddr, Addr &paddr);
328 * Do a functional lookup on the TLB (for checker cpu) that
329 * behaves like a normal lookup without modifying any page table state.
331 Fault translateFunctional(const RequestPtr &req, ThreadContext *tc,
332 Mode mode, ArmTranslationType tranType);
334 translateFunctional(const RequestPtr &req,
335 ThreadContext *tc, Mode mode) override
337 return translateFunctional(req, tc, mode, NormalTran);
340 /** Accessor functions for memory attributes for last accessed TLB entry
343 setAttr(uint64_t attr)
354 Fault translateMmuOff(ThreadContext *tc, const RequestPtr &req, Mode mode,
355 TLB::ArmTranslationType tranType, Addr vaddr, bool long_desc_format);
356 Fault translateMmuOn(ThreadContext *tc, const RequestPtr &req, Mode mode,
357 Translation *translation, bool &delay, bool timing, bool functional,
358 Addr vaddr, ArmFault::TranMethod tranMethod);
360 Fault translateFs(const RequestPtr &req, ThreadContext *tc, Mode mode,
361 Translation *translation, bool &delay,
362 bool timing, ArmTranslationType tranType, bool functional = false);
363 Fault translateSe(const RequestPtr &req, ThreadContext *tc, Mode mode,
364 Translation *translation, bool &delay, bool timing);
365 Fault translateAtomic(const RequestPtr &req, ThreadContext *tc, Mode mode,
366 ArmTranslationType tranType);
368 translateAtomic(const RequestPtr &req,
369 ThreadContext *tc, Mode mode) override
371 return translateAtomic(req, tc, mode, NormalTran);
373 void translateTiming(
374 const RequestPtr &req, ThreadContext *tc,
375 Translation *translation, Mode mode,
376 ArmTranslationType tranType);
378 translateTiming(const RequestPtr &req, ThreadContext *tc,
379 Translation *translation, Mode mode) override
381 translateTiming(req, tc, translation, mode, NormalTran);
383 Fault translateComplete(const RequestPtr &req, ThreadContext *tc,
384 Translation *translation, Mode mode, ArmTranslationType tranType,
386 Fault finalizePhysical(
387 const RequestPtr &req,
388 ThreadContext *tc, Mode mode) const override;
390 void drainResume() override;
392 void regProbePoints() override;
395 * Get the table walker port. This is used for migrating
396 * port connections during a CPU takeOverFrom() call. For
397 * architectures that do not have a table walker, NULL is
398 * returned, hence the use of a pointer rather than a
399 * reference. For ARM this method will always return a valid port
402 * @return A pointer to the walker request port
404 Port *getTableWalkerPort() override;
406 // Caching misc register values here.
407 // Writing to misc registers needs to invalidate them.
408 // translateFunctional/translateSe/translateFs checks if they are
409 // invalid and call updateMiscReg if necessary.
413 ExceptionLevel aarch64EL;
427 ContextID miscRegContext;
428 ArmTranslationType curTranType;
430 // Cached copies of system-level properties
432 bool haveVirtualization;
433 bool haveLargeAsid64;
434 uint8_t physAddrRange;
438 void updateMiscReg(ThreadContext *tc,
439 ArmTranslationType tranType = NormalTran);
445 return dynamic_cast<const Params &>(_params);
447 void invalidateMiscReg() { miscRegValid = false; }
450 /** Remove any entries that match both a va and asn
451 * @param mva virtual address to flush
452 * @param asn contextid/asn to flush on match
453 * @param secure_lookup if the operation affects the secure world
454 * @param ignore_asn if the flush should ignore the asn
455 * @param in_host if hcr.e2h == 1 and hcr.tge == 1 for VHE.
457 void _flushMva(Addr mva, uint64_t asn, bool secure_lookup,
458 bool ignore_asn, ExceptionLevel target_el,
461 public: /* Testing */
462 Fault testTranslation(const RequestPtr &req, Mode mode,
463 TlbEntry::DomainType domain);
464 Fault testWalk(Addr pa, Addr size, Addr va, bool is_secure, Mode mode,
465 TlbEntry::DomainType domain,
466 LookupLevel lookup_level);
469 } // namespace ArmISA
471 #endif // __ARCH_ARM_TLB_HH__