2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * Copyright (c) 2007 MIPS Technologies, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * 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
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "arch/mips/tlb.hh"
35 #include "arch/mips/faults.hh"
36 #include "arch/mips/pagetable.hh"
37 #include "arch/mips/pra_constants.hh"
38 #include "arch/mips/utility.hh"
39 #include "base/inifile.hh"
40 #include "base/str.hh"
41 #include "base/trace.hh"
42 #include "cpu/thread_context.hh"
43 #include "debug/MipsPRA.hh"
44 #include "debug/TLB.hh"
45 #include "mem/page_table.hh"
46 #include "params/MipsTLB.hh"
47 #include "sim/process.hh"
50 using namespace MipsISA
;
52 ///////////////////////////////////////////////////////////////////////
57 TLB::TLB(const Params
*p
)
58 : BaseTLB(p
), size(p
->size
), nlu(0)
60 table
= new PTE
[size
];
61 memset(table
, 0, sizeof(PTE
[size
]));
71 // look up an entry in the TLB
73 TLB::lookup(Addr vpn
, uint8_t asn
) const
75 // assume not found...
77 PageTable::const_iterator i
= lookupTable
.find(vpn
);
78 if (i
!= lookupTable
.end()) {
79 while (i
->first
== vpn
) {
80 int index
= i
->second
;
81 PTE
*pte
= &table
[index
];
83 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
84 Addr Mask
= pte
->Mask
;
87 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
88 (pte
->G
|| (asn
== pte
->asid
))) {
89 // We have a VPN + ASID Match
97 DPRINTF(TLB
, "lookup %#x, asn %#x -> %s ppn %#x\n", vpn
, (int)asn
,
98 retval
? "hit" : "miss", retval
? retval
->PFN1
: 0);
103 TLB::getEntry(unsigned Index
) const
105 // Make sure that Index is valid
107 return &table
[Index
];
111 TLB::probeEntry(Addr vpn
, uint8_t asn
) const
113 // assume not found...
115 PageTable::const_iterator i
= lookupTable
.find(vpn
);
116 if (i
!= lookupTable
.end()) {
117 while (i
->first
== vpn
) {
118 int index
= i
->second
;
119 PTE
*pte
= &table
[index
];
121 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
122 Addr Mask
= pte
->Mask
;
123 Addr InvMask
= ~Mask
;
125 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
126 (pte
->G
|| (asn
== pte
->asid
))) {
127 // We have a VPN + ASID Match
134 DPRINTF(MipsPRA
,"VPN: %x, asid: %d, Result of TLBP: %d\n",vpn
,asn
,Ind
);
139 TLB::checkCacheability(const RequestPtr
&req
)
141 Addr VAddrUncacheable
= 0xA0000000;
142 // In MIPS, cacheability is controlled by certain bits of the virtual
143 // address or by the TLB entry
144 if ((req
->getVaddr() & VAddrUncacheable
) == VAddrUncacheable
) {
145 // mark request as uncacheable
146 req
->setFlags(Request::UNCACHEABLE
| Request::STRICT_ORDER
);
152 TLB::insertAt(PTE
&pte
, unsigned Index
, int _smallPages
)
154 smallPages
= _smallPages
;
156 warn("Attempted to write at index (%d) beyond TLB size (%d)",
160 DPRINTF(TLB
, "TLB[%d]: %x %x %x %x\n",
161 Index
, pte
.Mask
<< 11,
162 ((pte
.VPN
<< 11) | pte
.asid
),
163 ((pte
.PFN0
<< 6) | (pte
.C0
<< 3) |
164 (pte
.D0
<< 2) | (pte
.V0
<<1) | pte
.G
),
165 ((pte
.PFN1
<<6) | (pte
.C1
<< 3) |
166 (pte
.D1
<< 2) | (pte
.V1
<<1) | pte
.G
));
167 if (table
[Index
].V0
|| table
[Index
].V1
) {
168 // Previous entry is valid
169 PageTable::iterator i
= lookupTable
.find(table
[Index
].VPN
);
170 lookupTable
.erase(i
);
173 // Update fast lookup table
174 lookupTable
.insert(make_pair(table
[Index
].VPN
, Index
));
178 // insert a new TLB entry
180 TLB::insert(Addr addr
, PTE
&pte
)
182 fatal("TLB Insert not yet implemented\n");
188 DPRINTF(TLB
, "flushAll\n");
189 memset(table
, 0, sizeof(PTE
[size
]));
195 TLB::serialize(CheckpointOut
&cp
) const
197 SERIALIZE_SCALAR(size
);
198 SERIALIZE_SCALAR(nlu
);
200 for (int i
= 0; i
< size
; i
++) {
201 ScopedCheckpointSection
sec(cp
, csprintf("PTE%d", i
));
202 table
[i
].serialize(cp
);
207 TLB::unserialize(CheckpointIn
&cp
)
209 UNSERIALIZE_SCALAR(size
);
210 UNSERIALIZE_SCALAR(nlu
);
212 for (int i
= 0; i
< size
; i
++) {
213 ScopedCheckpointSection
sec(cp
, csprintf("PTE%d", i
));
214 table
[i
].unserialize(cp
);
215 if (table
[i
].V0
|| table
[i
].V1
) {
216 lookupTable
.insert(make_pair(table
[i
].VPN
, i
));
227 .name(name() + ".read_hits")
228 .desc("DTB read hits")
232 .name(name() + ".read_misses")
233 .desc("DTB read misses")
238 .name(name() + ".read_accesses")
239 .desc("DTB read accesses")
243 .name(name() + ".write_hits")
244 .desc("DTB write hits")
248 .name(name() + ".write_misses")
249 .desc("DTB write misses")
254 .name(name() + ".write_accesses")
255 .desc("DTB write accesses")
259 .name(name() + ".hits")
264 .name(name() + ".misses")
269 .name(name() + ".accesses")
270 .desc("DTB accesses")
273 hits
= read_hits
+ write_hits
;
274 misses
= read_misses
+ write_misses
;
275 accesses
= read_accesses
+ write_accesses
;
279 TLB::translateInst(const RequestPtr
&req
, ThreadContext
*tc
)
282 panic("translateInst not implemented in MIPS.\n");
284 Process
* p
= tc
->getProcessPtr();
286 Fault fault
= p
->pTable
->translate(req
);
287 if (fault
!= NoFault
)
294 TLB::translateData(const RequestPtr
&req
, ThreadContext
*tc
, bool write
)
297 panic("translateData not implemented in MIPS.\n");
299 Process
* p
= tc
->getProcessPtr();
301 Fault fault
= p
->pTable
->translate(req
);
302 if (fault
!= NoFault
)
309 TLB::translateAtomic(const RequestPtr
&req
, ThreadContext
*tc
, Mode mode
)
312 return translateInst(req
, tc
);
314 return translateData(req
, tc
, mode
== Write
);
318 TLB::translateTiming(const RequestPtr
&req
, ThreadContext
*tc
,
319 Translation
*translation
, Mode mode
)
322 translation
->finish(translateAtomic(req
, tc
, mode
), req
, tc
, mode
);
326 TLB::finalizePhysical(const RequestPtr
&req
,
327 ThreadContext
*tc
, Mode mode
) const
334 TLB::index(bool advance
)
336 PTE
*pte
= &table
[nlu
];
345 MipsTLBParams::create()
347 return new TLB(this);