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.
29 * Authors: Nathan Binkert
37 #include "arch/mips/pra_constants.hh"
38 #include "arch/mips/pagetable.hh"
39 #include "arch/mips/tlb.hh"
40 #include "arch/mips/faults.hh"
41 #include "arch/mips/utility.hh"
42 #include "base/inifile.hh"
43 #include "base/str.hh"
44 #include "base/trace.hh"
45 #include "cpu/thread_context.hh"
46 #include "sim/process.hh"
47 #include "mem/page_table.hh"
48 #include "params/MipsDTB.hh"
49 #include "params/MipsITB.hh"
50 #include "params/MipsTLB.hh"
51 #include "params/MipsUTB.hh"
55 using namespace MipsISA
;
57 ///////////////////////////////////////////////////////////////////////
62 #define MODE2MASK(X) (1 << (X))
64 TLB::TLB(const Params
*p
)
65 : BaseTLB(p
), size(p
->size
), nlu(0)
67 table
= new MipsISA::PTE
[size
];
68 memset(table
, 0, sizeof(MipsISA::PTE
[size
]));
78 // look up an entry in the TLB
80 TLB::lookup(Addr vpn
, uint8_t asn
) const
82 // assume not found...
83 MipsISA::PTE
*retval
= NULL
;
84 PageTable::const_iterator i
= lookupTable
.find(vpn
);
85 if (i
!= lookupTable
.end()) {
86 while (i
->first
== vpn
) {
87 int index
= i
->second
;
88 MipsISA::PTE
*pte
= &table
[index
];
90 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
91 Addr Mask
= pte
->Mask
;
94 // warn("Valid: %d - %d\n",pte->V0,pte->V1);
95 if(((vpn
& InvMask
) == (VPN
& InvMask
)) && (pte
->G
|| (asn
== pte
->asid
)))
96 { // We have a VPN + ASID Match
104 DPRINTF(TLB
, "lookup %#x, asn %#x -> %s ppn %#x\n", vpn
, (int)asn
,
105 retval
? "hit" : "miss", retval
? retval
->PFN1
: 0);
109 MipsISA::PTE
* TLB::getEntry(unsigned Index
) const
111 // Make sure that Index is valid
113 return &table
[Index
];
116 int TLB::probeEntry(Addr vpn
,uint8_t asn
) const
118 // assume not found...
119 MipsISA::PTE
*retval
= NULL
;
121 PageTable::const_iterator i
= lookupTable
.find(vpn
);
122 if (i
!= lookupTable
.end()) {
123 while (i
->first
== vpn
) {
124 int index
= i
->second
;
125 MipsISA::PTE
*pte
= &table
[index
];
127 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
128 Addr Mask
= pte
->Mask
;
129 Addr InvMask
= ~Mask
;
131 if(((vpn
& InvMask
) == (VPN
& InvMask
)) && (pte
->G
|| (asn
== pte
->asid
)))
132 { // We have a VPN + ASID Match
141 DPRINTF(MipsPRA
,"VPN: %x, asid: %d, Result of TLBP: %d\n",vpn
,asn
,Ind
);
145 TLB::checkCacheability(RequestPtr
&req
)
147 Addr VAddrUncacheable
= 0xA0000000;
148 // In MIPS, cacheability is controlled by certain bits of the virtual address
149 // or by the TLB entry
150 if((req
->getVaddr() & VAddrUncacheable
) == VAddrUncacheable
) {
151 // mark request as uncacheable
152 req
->setFlags(Request::UNCACHEABLE
);
156 void TLB::insertAt(MipsISA::PTE
&pte
, unsigned Index
, int _smallPages
)
158 smallPages
=_smallPages
;
160 warn("Attempted to write at index (%d) beyond TLB size (%d)",Index
,size
);
163 DPRINTF(TLB
,"TLB[%d]: %x %x %x %x\n",Index
,pte
.Mask
<<11,((pte
.VPN
<< 11) | pte
.asid
),((pte
.PFN0
<<6) | (pte
.C0
<< 3) | (pte
.D0
<< 2) | (pte
.V0
<<1) | pte
.G
),
164 ((pte
.PFN1
<<6) | (pte
.C1
<< 3) | (pte
.D1
<< 2) | (pte
.V1
<<1) | pte
.G
));
165 if(table
[Index
].V0
== true || table
[Index
].V1
== true){ // Previous entry is valid
166 PageTable::iterator i
= lookupTable
.find(table
[Index
].VPN
);
167 lookupTable
.erase(i
);
170 // Update fast lookup table
171 lookupTable
.insert(make_pair(table
[Index
].VPN
, Index
));
172 // int TestIndex=probeEntry(pte.VPN,pte.asid);
173 // warn("Inserted at: %d, Found at: %d (%x)\n",Index,TestIndex,pte.Mask);
178 // insert a new TLB entry
180 TLB::insert(Addr addr
, MipsISA::PTE
&pte
)
182 fatal("TLB Insert not yet implemented\n");
185 /* MipsISA::VAddr vaddr = addr;
186 if (table[nlu].valid) {
187 Addr oldvpn = table[nlu].tag;
188 PageTable::iterator i = lookupTable.find(oldvpn);
190 if (i == lookupTable.end())
191 panic("TLB entry not found in lookupTable");
194 while ((index = i->second) != nlu) {
195 if (table[index].tag != oldvpn)
196 panic("TLB entry not found in lookupTable");
201 DPRINTF(TLB, "remove @%d: %#x -> %#x\n", nlu, oldvpn, table[nlu].ppn);
203 lookupTable.erase(i);
206 DPRINTF(TLB, "insert @%d: %#x -> %#x\n", nlu, vaddr.vpn(), pte.ppn);
209 table[nlu].tag = vaddr.vpn();
210 table[nlu].valid = true;
212 lookupTable.insert(make_pair(vaddr.vpn(), nlu));
220 DPRINTF(TLB
, "flushAll\n");
221 memset(table
, 0, sizeof(MipsISA::PTE
[size
]));
227 TLB::serialize(ostream
&os
)
229 SERIALIZE_SCALAR(size
);
230 SERIALIZE_SCALAR(nlu
);
232 for (int i
= 0; i
< size
; i
++) {
233 nameOut(os
, csprintf("%s.PTE%d", name(), i
));
234 table
[i
].serialize(os
);
239 TLB::unserialize(Checkpoint
*cp
, const string
§ion
)
241 UNSERIALIZE_SCALAR(size
);
242 UNSERIALIZE_SCALAR(nlu
);
244 for (int i
= 0; i
< size
; i
++) {
245 table
[i
].unserialize(cp
, csprintf("%s.PTE%d", section
, i
));
246 if (table
[i
].V0
|| table
[i
].V1
) {
247 lookupTable
.insert(make_pair(table
[i
].VPN
, i
));
256 .name(name() + ".read_hits")
257 .desc("DTB read hits")
261 .name(name() + ".read_misses")
262 .desc("DTB read misses")
267 .name(name() + ".read_accesses")
268 .desc("DTB read accesses")
272 .name(name() + ".write_hits")
273 .desc("DTB write hits")
277 .name(name() + ".write_misses")
278 .desc("DTB write misses")
283 .name(name() + ".write_accesses")
284 .desc("DTB write accesses")
288 .name(name() + ".hits")
293 .name(name() + ".misses")
298 .name(name() + ".invalids")
299 .desc("DTB access violations")
303 .name(name() + ".accesses")
304 .desc("DTB accesses")
307 hits
= read_hits
+ write_hits
;
308 misses
= read_misses
+ write_misses
;
309 accesses
= read_accesses
+ write_accesses
;
313 ITB::translate(RequestPtr
&req
, ThreadContext
*tc
)
316 Process
* p
= tc
->getProcessPtr();
318 Fault fault
= p
->pTable
->translate(req
);
324 if(MipsISA::IsKSeg0(req
->getVaddr()))
326 // Address will not be translated through TLB, set response, and go!
327 req
->setPaddr(MipsISA::KSeg02Phys(req
->getVaddr()));
328 if(MipsISA::getOperatingMode(tc
->readMiscReg(MipsISA::Status
)) != mode_kernel
|| req
->isMisaligned())
330 AddressErrorFault
*Flt
= new AddressErrorFault();
331 /* BadVAddr must be set */
332 Flt
->BadVAddr
= req
->getVaddr();
336 else if(MipsISA::IsKSeg1(req
->getVaddr()))
338 // Address will not be translated through TLB, set response, and go!
339 req
->setPaddr(MipsISA::KSeg02Phys(req
->getVaddr()));
343 /* This is an optimization - smallPages is updated every time a TLB operation is performed
344 That way, we don't need to look at Config3 _ SP and PageGrain _ ESP every time we
348 VPN
=((req
->getVaddr() >> 11));
350 VPN
=((req
->getVaddr() >> 11) & 0xFFFFFFFC);
352 uint8_t Asid
= req
->getAsid();
353 if(req
->isMisaligned()){ // Unaligned address!
354 AddressErrorFault
*Flt
= new AddressErrorFault();
355 /* BadVAddr must be set */
356 Flt
->BadVAddr
= req
->getVaddr();
359 MipsISA::PTE
*pte
= lookup(VPN
,Asid
);
361 {// Ok, found something
362 /* Check for valid bits */
365 if((((req
->getVaddr()) >> pte
->AddrShiftAmount
) & 1) ==0){
377 ItbInvalidFault
*Flt
= new ItbInvalidFault();
378 /* EntryHi VPN, ASID fields must be set */
379 Flt
->EntryHi_Asid
= Asid
;
380 Flt
->EntryHi_VPN2
= (VPN
>>2);
381 Flt
->EntryHi_VPN2X
= (VPN
& 0x3);
383 /* BadVAddr must be set */
384 Flt
->BadVAddr
= req
->getVaddr();
386 /* Context must be set */
387 Flt
->Context_BadVPN2
= (VPN
>> 2);
391 {// Ok, this is really a match, set paddr
399 PAddr
>>= (pte
->AddrShiftAmount
-12);
400 PAddr
<<= pte
->AddrShiftAmount
;
401 PAddr
|= ((req
->getVaddr()) & pte
->OffsetMask
);
402 req
->setPaddr(PAddr
);
408 { // Didn't find any match, return a TLB Refill Exception
410 ItbRefillFault
*Flt
=new ItbRefillFault();
411 /* EntryHi VPN, ASID fields must be set */
412 Flt
->EntryHi_Asid
= Asid
;
413 Flt
->EntryHi_VPN2
= (VPN
>>2);
414 Flt
->EntryHi_VPN2X
= (VPN
& 0x3);
417 /* BadVAddr must be set */
418 Flt
->BadVAddr
= req
->getVaddr();
420 /* Context must be set */
421 Flt
->Context_BadVPN2
= (VPN
>> 2);
425 return checkCacheability(req
);
430 DTB::translate(RequestPtr
&req
, ThreadContext
*tc
, bool write
)
433 Process
* p
= tc
->getProcessPtr();
435 Fault fault
= p
->pTable
->translate(req
);
441 if(MipsISA::IsKSeg0(req
->getVaddr()))
443 // Address will not be translated through TLB, set response, and go!
444 req
->setPaddr(MipsISA::KSeg02Phys(req
->getVaddr()));
445 if(MipsISA::getOperatingMode(tc
->readMiscReg(MipsISA::Status
)) != mode_kernel
|| req
->isMisaligned())
447 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
448 /* BadVAddr must be set */
449 Flt
->BadVAddr
= req
->getVaddr();
454 else if(MipsISA::IsKSeg1(req
->getVaddr()))
456 // Address will not be translated through TLB, set response, and go!
457 req
->setPaddr(MipsISA::KSeg02Phys(req
->getVaddr()));
461 /* This is an optimization - smallPages is updated every time a TLB operation is performed
462 That way, we don't need to look at Config3 _ SP and PageGrain _ ESP every time we
464 Addr VPN
=((req
->getVaddr() >> 11) & 0xFFFFFFFC);
466 VPN
=((req
->getVaddr() >> 11));
468 uint8_t Asid
= req
->getAsid();
469 MipsISA::PTE
*pte
= lookup(VPN
,Asid
);
470 if(req
->isMisaligned()){ // Unaligned address!
471 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
472 /* BadVAddr must be set */
473 Flt
->BadVAddr
= req
->getVaddr();
477 {// Ok, found something
478 /* Check for valid bits */
482 if(((((req
->getVaddr()) >> pte
->AddrShiftAmount
) & 1)) ==0){
498 DtbInvalidFault
*Flt
= new DtbInvalidFault();
499 /* EntryHi VPN, ASID fields must be set */
500 Flt
->EntryHi_Asid
= Asid
;
501 Flt
->EntryHi_VPN2
= (VPN
>>2);
502 Flt
->EntryHi_VPN2X
= (VPN
& 0x3);
505 /* BadVAddr must be set */
506 Flt
->BadVAddr
= req
->getVaddr();
508 /* Context must be set */
509 Flt
->Context_BadVPN2
= (VPN
>> 2);
514 {// Ok, this is really a match, set paddr
518 TLBModifiedFault
*Flt
= new TLBModifiedFault();
519 /* EntryHi VPN, ASID fields must be set */
520 Flt
->EntryHi_Asid
= Asid
;
521 Flt
->EntryHi_VPN2
= (VPN
>>2);
522 Flt
->EntryHi_VPN2X
= (VPN
& 0x3);
525 /* BadVAddr must be set */
526 Flt
->BadVAddr
= req
->getVaddr();
528 /* Context must be set */
529 Flt
->Context_BadVPN2
= (VPN
>> 2);
539 PAddr
>>= (pte
->AddrShiftAmount
-12);
540 PAddr
<<= pte
->AddrShiftAmount
;
541 PAddr
|= ((req
->getVaddr()) & pte
->OffsetMask
);
542 req
->setPaddr(PAddr
);
546 { // Didn't find any match, return a TLB Refill Exception
548 DtbRefillFault
*Flt
=new DtbRefillFault();
549 /* EntryHi VPN, ASID fields must be set */
550 Flt
->EntryHi_Asid
= Asid
;
551 Flt
->EntryHi_VPN2
= (VPN
>>2);
552 Flt
->EntryHi_VPN2X
= (VPN
& 0x3);
555 /* BadVAddr must be set */
556 Flt
->BadVAddr
= req
->getVaddr();
558 /* Context must be set */
559 Flt
->Context_BadVPN2
= (VPN
>> 2);
563 return checkCacheability(req
);
567 ///////////////////////////////////////////////////////////////////////
571 ITB::ITB(const Params
*p
)
579 // /* hits - causes failure for some reason
580 // .name(name() + ".hits")
581 // .desc("ITB hits");
583 // .name(name() + ".misses")
584 // .desc("ITB misses");
586 // .name(name() + ".acv")
589 // .name(name() + ".accesses")
590 // .desc("ITB accesses");
592 // accesses = hits + misses + invalids; */
597 ///////////////////////////////////////////////////////////////////////
601 DTB::DTB(const Params
*p
)
605 ///////////////////////////////////////////////////////////////////////
609 UTB::UTB(const Params
*p
)
616 TLB::index(bool advance
)
618 MipsISA::PTE
*pte
= &table
[nlu
];
627 MipsITBParams::create()
629 return new MipsISA::ITB(this);
633 MipsDTBParams::create()
635 return new MipsISA::DTB(this);
639 MipsUTBParams::create()
641 return new MipsISA::UTB(this);