2 * Copyright (c) 2010-2013, 2016, 2019 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.
43 #ifndef __ARCH_ARM_TLB_HH__
44 #define __ARCH_ARM_TLB_HH__
47 #include "arch/arm/faults.hh"
48 #include "arch/arm/isa_traits.hh"
49 #include "arch/arm/pagetable.hh"
50 #include "arch/arm/utility.hh"
51 #include "arch/arm/vtophys.hh"
52 #include "arch/generic/tlb.hh"
53 #include "base/statistics.hh"
54 #include "mem/request.hh"
55 #include "params/ArmTLB.hh"
56 #include "sim/probe/pmu.hh"
67 class TlbTestInterface
71 virtual ~TlbTestInterface() {}
74 * Check if a TLB translation should be forced to fail.
76 * @param req Request requiring a translation.
77 * @param is_priv Access from a privileged mode (i.e., not EL0)
78 * @param mode Access type
79 * @param domain Domain type
81 virtual Fault translationCheck(const RequestPtr &req, bool is_priv,
83 TlbEntry::DomainType domain) = 0;
86 * Check if a page table walker access should be forced to fail.
88 * @param pa Physical address the walker is accessing
89 * @param size Walker access size
90 * @param va Virtual address that initiated the walk
91 * @param is_secure Access from secure state
92 * @param is_priv Access from a privileged mode (i.e., not EL0)
93 * @param mode Access type
94 * @param domain Domain type
95 * @param lookup_level Page table walker level
97 virtual Fault walkCheck(Addr pa, Addr size, Addr va, bool is_secure,
98 Addr is_priv, BaseTLB::Mode mode,
99 TlbEntry::DomainType domain,
100 LookupLevel lookup_level) = 0;
103 class TLB : public BaseTLB
112 AlignDoubleWord = 0x3,
116 AllowUnaligned = 0x8,
117 // Priv code operating as if it wasn't
119 // Because zero otherwise looks like a valid setting and may be used
120 // accidentally, this bit must be non-zero to show it was used on
125 enum ArmTranslationType {
129 // Secure code operating as if it wasn't (required by some Address
130 // Translate operations)
132 // Address translation instructions (eg AT S1E0R_Xt) need to be handled
133 // in special ways during translation because they could need to act
134 // like a different EL than the current EL. The following flags are
135 // for these instructions
145 * Determine the EL to use for the purpose of a translation given
146 * a specific translation type. If the translation type doesn't
147 * specify an EL, we use the current EL.
149 static ExceptionLevel tranTypeEL(CPSR cpsr, ArmTranslationType type);
152 TlbEntry* table; // the Page Table
153 int size; // TLB Size
154 bool isStage2; // Indicates this TLB is part of the second stage MMU
155 bool stage2Req; // Indicates whether a stage 2 lookup is also required
156 // Indicates whether a stage 2 lookup of the table descriptors is required.
157 // Certain address translation instructions will intercept the IPA but the
158 // table descriptors still need to be translated by the stage2.
160 uint64_t _attr; // Memory attributes for last accessed TLB entry
161 bool directToStage2; // Indicates whether all translation requests should
162 // be routed directly to the stage 2 TLB
164 TableWalker *tableWalker;
166 Stage2MMU *stage2Mmu;
168 TlbTestInterface *test;
171 mutable Stats::Scalar instHits;
172 mutable Stats::Scalar instMisses;
173 mutable Stats::Scalar readHits;
174 mutable Stats::Scalar readMisses;
175 mutable Stats::Scalar writeHits;
176 mutable Stats::Scalar writeMisses;
177 mutable Stats::Scalar inserts;
178 mutable Stats::Scalar flushTlb;
179 mutable Stats::Scalar flushTlbMva;
180 mutable Stats::Scalar flushTlbMvaAsid;
181 mutable Stats::Scalar flushTlbAsid;
182 mutable Stats::Scalar flushedEntries;
183 mutable Stats::Scalar alignFaults;
184 mutable Stats::Scalar prefetchFaults;
185 mutable Stats::Scalar domainFaults;
186 mutable Stats::Scalar permsFaults;
188 Stats::Formula readAccesses;
189 Stats::Formula writeAccesses;
190 Stats::Formula instAccesses;
192 Stats::Formula misses;
193 Stats::Formula accesses;
195 /** PMU probe for TLB refills */
196 ProbePoints::PMUUPtr ppRefills;
198 int rangeMRU; //On lookup, only move entries ahead when outside rangeMRU
201 TLB(const ArmTLBParams *p);
202 TLB(const Params *p, int _size, TableWalker *_walker);
204 /** Lookup an entry in the TLB
205 * @param vpn virtual address
206 * @param asn context id/address space id to use
207 * @param vmid The virtual machine ID used for stage 2 translation
208 * @param secure if the lookup is secure
209 * @param hyp if the lookup is done from hyp mode
210 * @param functional if the lookup should modify state
211 * @param ignore_asn if on lookup asn should be ignored
212 * @return pointer to TLB entry if it exists
214 TlbEntry *lookup(Addr vpn, uint16_t asn, uint8_t vmid, bool hyp,
215 bool secure, bool functional,
216 bool ignore_asn, ExceptionLevel target_el);
220 void takeOverFrom(BaseTLB *otlb) override;
222 /// setup all the back pointers
223 void init() override;
225 void setTestInterface(SimObject *ti);
227 TableWalker *getTableWalker() { return tableWalker; }
229 void setMMU(Stage2MMU *m, MasterID master_id);
231 int getsize() const { return size; }
233 void insert(Addr vaddr, TlbEntry &pte);
235 Fault getTE(TlbEntry **te, const RequestPtr &req,
236 ThreadContext *tc, Mode mode,
237 Translation *translation, bool timing, bool functional,
238 bool is_secure, ArmTranslationType tranType);
240 Fault getResultTe(TlbEntry **te, const RequestPtr &req,
241 ThreadContext *tc, Mode mode,
242 Translation *translation, bool timing,
243 bool functional, TlbEntry *mergeTe);
245 Fault checkPermissions(TlbEntry *te, const RequestPtr &req, Mode mode);
246 Fault checkPermissions64(TlbEntry *te, const RequestPtr &req, Mode mode,
248 bool checkPAN(ThreadContext *tc, uint8_t ap, const RequestPtr &req,
252 /** Reset the entire TLB
253 * @param secure_lookup if the operation affects the secure world
255 void flushAllSecurity(bool secure_lookup, ExceptionLevel target_el,
256 bool ignore_el = false);
258 /** Remove all entries in the non secure world, depending on whether they
259 * were allocated in hyp mode or not
261 void flushAllNs(ExceptionLevel target_el, bool ignore_el = false);
264 /** Reset the entire TLB. Used for CPU switching to prevent stale
265 * translations after multiple switches
267 void flushAll() override
269 flushAllSecurity(false, EL0, true);
270 flushAllSecurity(true, EL0, true);
273 /** Remove any entries that match both a va and asn
274 * @param mva virtual address to flush
275 * @param asn contextid/asn to flush on match
276 * @param secure_lookup if the operation affects the secure world
278 void flushMvaAsid(Addr mva, uint64_t asn, bool secure_lookup,
279 ExceptionLevel target_el);
281 /** Remove any entries that match the asn
282 * @param asn contextid/asn to flush on match
283 * @param secure_lookup if the operation affects the secure world
285 void flushAsid(uint64_t asn, bool secure_lookup,
286 ExceptionLevel target_el);
288 /** Remove all entries that match the va regardless of asn
289 * @param mva address to flush from cache
290 * @param secure_lookup if the operation affects the secure world
292 void flushMva(Addr mva, bool secure_lookup, ExceptionLevel target_el);
295 * Invalidate all entries in the stage 2 TLB that match the given ipa
296 * and the current VMID
297 * @param ipa the address to invalidate
298 * @param secure_lookup if the operation affects the secure world
300 void flushIpaVmid(Addr ipa, bool secure_lookup, ExceptionLevel target_el);
302 Fault trickBoxCheck(const RequestPtr &req, Mode mode,
303 TlbEntry::DomainType domain);
305 Fault walkTrickBoxCheck(Addr pa, bool is_secure, Addr va, Addr sz,
306 bool is_exec, bool is_write,
307 TlbEntry::DomainType domain,
308 LookupLevel lookup_level);
310 void printTlb() const;
312 void demapPage(Addr vaddr, uint64_t asn) override
314 // needed for x86 only
315 panic("demapPage() is not implemented.\n");
319 * Do a functional lookup on the TLB (for debugging)
320 * and don't modify any internal state
321 * @param tc thread context to get the context id from
322 * @param vaddr virtual address to translate
323 * @param pa returned physical address
324 * @return if the translation was successful
326 bool translateFunctional(ThreadContext *tc, Addr vaddr, Addr &paddr);
329 * Do a functional lookup on the TLB (for checker cpu) that
330 * behaves like a normal lookup without modifying any page table state.
332 Fault translateFunctional(const RequestPtr &req, ThreadContext *tc,
333 Mode mode, ArmTranslationType tranType);
335 translateFunctional(const RequestPtr &req,
336 ThreadContext *tc, Mode mode) override
338 return translateFunctional(req, tc, mode, NormalTran);
341 /** Accessor functions for memory attributes for last accessed TLB entry
344 setAttr(uint64_t attr)
355 Fault translateMmuOff(ThreadContext *tc, const RequestPtr &req, Mode mode,
356 TLB::ArmTranslationType tranType, Addr vaddr, bool long_desc_format);
357 Fault translateMmuOn(ThreadContext *tc, const RequestPtr &req, Mode mode,
358 Translation *translation, bool &delay, bool timing, bool functional,
359 Addr vaddr, ArmFault::TranMethod tranMethod);
361 Fault translateFs(const RequestPtr &req, ThreadContext *tc, Mode mode,
362 Translation *translation, bool &delay,
363 bool timing, ArmTranslationType tranType, bool functional = false);
364 Fault translateSe(const RequestPtr &req, ThreadContext *tc, Mode mode,
365 Translation *translation, bool &delay, bool timing);
366 Fault translateAtomic(const RequestPtr &req, ThreadContext *tc, Mode mode,
367 ArmTranslationType tranType);
369 translateAtomic(const RequestPtr &req,
370 ThreadContext *tc, Mode mode) override
372 return translateAtomic(req, tc, mode, NormalTran);
374 void translateTiming(
375 const RequestPtr &req, ThreadContext *tc,
376 Translation *translation, Mode mode,
377 ArmTranslationType tranType);
379 translateTiming(const RequestPtr &req, ThreadContext *tc,
380 Translation *translation, Mode mode) override
382 translateTiming(req, tc, translation, mode, NormalTran);
384 Fault translateComplete(const RequestPtr &req, ThreadContext *tc,
385 Translation *translation, Mode mode, ArmTranslationType tranType,
387 Fault finalizePhysical(
388 const RequestPtr &req,
389 ThreadContext *tc, Mode mode) const override;
391 void drainResume() override;
393 void regStats() override;
395 void regProbePoints() override;
398 * Get the table walker port. This is used for migrating
399 * port connections during a CPU takeOverFrom() call. For
400 * architectures that do not have a table walker, NULL is
401 * returned, hence the use of a pointer rather than a
402 * reference. For ARM this method will always return a valid port
405 * @return A pointer to the walker master port
407 Port *getTableWalkerPort() override;
409 // Caching misc register values here.
410 // Writing to misc registers needs to invalidate them.
411 // translateFunctional/translateSe/translateFs checks if they are
412 // invalid and call updateMiscReg if necessary.
416 ExceptionLevel aarch64EL;
430 ContextID miscRegContext;
431 ArmTranslationType curTranType;
433 // Cached copies of system-level properties
435 bool haveVirtualization;
436 bool haveLargeAsid64;
440 void updateMiscReg(ThreadContext *tc,
441 ArmTranslationType tranType = NormalTran);
447 return dynamic_cast<const Params *>(_params);
449 inline void invalidateMiscReg() { miscRegValid = false; }
452 /** Remove any entries that match both a va and asn
453 * @param mva virtual address to flush
454 * @param asn contextid/asn to flush on match
455 * @param secure_lookup if the operation affects the secure world
456 * @param ignore_asn if the flush should ignore the asn
458 void _flushMva(Addr mva, uint64_t asn, bool secure_lookup,
459 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);
473 auto tlb = static_cast<TLB *>(tc->getITBPtr());
482 auto tlb = static_cast<TLB *>(tc->getDTBPtr());
487 } // namespace ArmISA
489 #endif // __ARCH_ARM_TLB_HH__