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/MipsTLB.hh"
52 using namespace MipsISA
;
54 ///////////////////////////////////////////////////////////////////////
59 static inline mode_type
60 getOperatingMode(MiscReg Stat
)
62 if ((Stat
& 0x10000006) != 0 || (Stat
& 0x18) ==0) {
64 } else if ((Stat
& 0x18) == 0x8) {
65 return mode_supervisor
;
66 } else if ((Stat
& 0x18) == 0x10) {
74 TLB::TLB(const Params
*p
)
75 : BaseTLB(p
), size(p
->size
), nlu(0)
77 table
= new PTE
[size
];
78 memset(table
, 0, sizeof(PTE
[size
]));
88 // look up an entry in the TLB
90 TLB::lookup(Addr vpn
, uint8_t asn
) const
92 // assume not found...
94 PageTable::const_iterator i
= lookupTable
.find(vpn
);
95 if (i
!= lookupTable
.end()) {
96 while (i
->first
== vpn
) {
97 int index
= i
->second
;
98 PTE
*pte
= &table
[index
];
100 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
101 Addr Mask
= pte
->Mask
;
102 Addr InvMask
= ~Mask
;
104 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
105 (pte
->G
|| (asn
== pte
->asid
))) {
106 // We have a VPN + ASID Match
114 DPRINTF(TLB
, "lookup %#x, asn %#x -> %s ppn %#x\n", vpn
, (int)asn
,
115 retval
? "hit" : "miss", retval
? retval
->PFN1
: 0);
120 TLB::getEntry(unsigned Index
) const
122 // Make sure that Index is valid
124 return &table
[Index
];
128 TLB::probeEntry(Addr vpn
, uint8_t asn
) const
130 // assume not found...
133 PageTable::const_iterator i
= lookupTable
.find(vpn
);
134 if (i
!= lookupTable
.end()) {
135 while (i
->first
== vpn
) {
136 int index
= i
->second
;
137 PTE
*pte
= &table
[index
];
139 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
140 Addr Mask
= pte
->Mask
;
141 Addr InvMask
= ~Mask
;
143 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
144 (pte
->G
|| (asn
== pte
->asid
))) {
145 // We have a VPN + ASID Match
153 DPRINTF(MipsPRA
,"VPN: %x, asid: %d, Result of TLBP: %d\n",vpn
,asn
,Ind
);
158 TLB::checkCacheability(RequestPtr
&req
)
160 Addr VAddrUncacheable
= 0xA0000000;
161 // In MIPS, cacheability is controlled by certain bits of the virtual
162 // address or by the TLB entry
163 if ((req
->getVaddr() & VAddrUncacheable
) == VAddrUncacheable
) {
164 // mark request as uncacheable
165 req
->setFlags(Request::UNCACHEABLE
);
171 TLB::insertAt(PTE
&pte
, unsigned Index
, int _smallPages
)
173 smallPages
= _smallPages
;
175 warn("Attempted to write at index (%d) beyond TLB size (%d)",
179 DPRINTF(TLB
, "TLB[%d]: %x %x %x %x\n",
180 Index
, pte
.Mask
<< 11,
181 ((pte
.VPN
<< 11) | pte
.asid
),
182 ((pte
.PFN0
<< 6) | (pte
.C0
<< 3) |
183 (pte
.D0
<< 2) | (pte
.V0
<<1) | pte
.G
),
184 ((pte
.PFN1
<<6) | (pte
.C1
<< 3) |
185 (pte
.D1
<< 2) | (pte
.V1
<<1) | pte
.G
));
186 if (table
[Index
].V0
== true || table
[Index
].V1
== true) {
187 // Previous entry is valid
188 PageTable::iterator i
= lookupTable
.find(table
[Index
].VPN
);
189 lookupTable
.erase(i
);
192 // Update fast lookup table
193 lookupTable
.insert(make_pair(table
[Index
].VPN
, Index
));
197 // insert a new TLB entry
199 TLB::insert(Addr addr
, PTE
&pte
)
201 fatal("TLB Insert not yet implemented\n");
207 DPRINTF(TLB
, "flushAll\n");
208 memset(table
, 0, sizeof(PTE
[size
]));
214 TLB::serialize(ostream
&os
)
216 SERIALIZE_SCALAR(size
);
217 SERIALIZE_SCALAR(nlu
);
219 for (int i
= 0; i
< size
; i
++) {
220 nameOut(os
, csprintf("%s.PTE%d", name(), i
));
221 table
[i
].serialize(os
);
226 TLB::unserialize(Checkpoint
*cp
, const string
§ion
)
228 UNSERIALIZE_SCALAR(size
);
229 UNSERIALIZE_SCALAR(nlu
);
231 for (int i
= 0; i
< size
; i
++) {
232 table
[i
].unserialize(cp
, csprintf("%s.PTE%d", section
, i
));
233 if (table
[i
].V0
|| table
[i
].V1
) {
234 lookupTable
.insert(make_pair(table
[i
].VPN
, i
));
243 .name(name() + ".read_hits")
244 .desc("DTB read hits")
248 .name(name() + ".read_misses")
249 .desc("DTB read misses")
254 .name(name() + ".read_accesses")
255 .desc("DTB read accesses")
259 .name(name() + ".write_hits")
260 .desc("DTB write hits")
264 .name(name() + ".write_misses")
265 .desc("DTB write misses")
270 .name(name() + ".write_accesses")
271 .desc("DTB write accesses")
275 .name(name() + ".hits")
280 .name(name() + ".misses")
285 .name(name() + ".accesses")
286 .desc("DTB accesses")
289 hits
= read_hits
+ write_hits
;
290 misses
= read_misses
+ write_misses
;
291 accesses
= read_accesses
+ write_accesses
;
295 TLB::translateInst(RequestPtr req
, ThreadContext
*tc
)
298 Process
* p
= tc
->getProcessPtr();
300 Fault fault
= p
->pTable
->translate(req
);
301 if (fault
!= NoFault
)
306 if (IsKSeg0(req
->getVaddr())) {
307 // Address will not be translated through TLB, set response, and go!
308 req
->setPaddr(KSeg02Phys(req
->getVaddr()));
309 if (getOperatingMode(tc
->readMiscReg(MISCREG_STATUS
)) != mode_kernel
||
310 req
->isMisaligned()) {
311 AddressErrorFault
*Flt
= new AddressErrorFault();
312 /* BadVAddr must be set */
313 Flt
->badVAddr
= req
->getVaddr();
316 } else if(IsKSeg1(req
->getVaddr())) {
317 // Address will not be translated through TLB, set response, and go!
318 req
->setPaddr(KSeg02Phys(req
->getVaddr()));
321 * This is an optimization - smallPages is updated every time a TLB
322 * operation is performed. That way, we don't need to look at
323 * Config3 _ SP and PageGrain _ ESP every time we do a TLB lookup
326 if (smallPages
== 1) {
327 VPN
= ((req
->getVaddr() >> 11));
329 VPN
= ((req
->getVaddr() >> 11) & 0xFFFFFFFC);
331 uint8_t Asid
= req
->getAsid();
332 if (req
->isMisaligned()) {
333 // Unaligned address!
334 AddressErrorFault
*Flt
= new AddressErrorFault();
335 /* BadVAddr must be set */
336 Flt
->badVAddr
= req
->getVaddr();
339 PTE
*pte
= lookup(VPN
,Asid
);
341 // Ok, found something
342 /* Check for valid bits */
345 if ((((req
->getVaddr()) >> pte
->AddrShiftAmount
) & 1) == 0) {
355 if (Valid
== false) {
357 ItbInvalidFault
*Flt
= new ItbInvalidFault();
358 /* EntryHi VPN, ASID fields must be set */
359 Flt
->entryHiAsid
= Asid
;
360 Flt
->entryHiVPN2
= (VPN
>> 2);
361 Flt
->entryHiVPN2X
= (VPN
& 0x3);
363 /* BadVAddr must be set */
364 Flt
->badVAddr
= req
->getVaddr();
366 /* Context must be set */
367 Flt
->contextBadVPN2
= (VPN
>> 2);
370 // Ok, this is really a match, set paddr
377 PAddr
>>= (pte
->AddrShiftAmount
- 12);
378 PAddr
<<= pte
->AddrShiftAmount
;
379 PAddr
|= ((req
->getVaddr()) & pte
->OffsetMask
);
380 req
->setPaddr(PAddr
);
383 // Didn't find any match, return a TLB Refill Exception
384 ItbRefillFault
*Flt
= new ItbRefillFault();
385 /* EntryHi VPN, ASID fields must be set */
386 Flt
->entryHiAsid
= Asid
;
387 Flt
->entryHiVPN2
= (VPN
>> 2);
388 Flt
->entryHiVPN2X
= (VPN
& 0x3);
390 /* BadVAddr must be set */
391 Flt
->badVAddr
= req
->getVaddr();
393 /* Context must be set */
394 Flt
->contextBadVPN2
= (VPN
>> 2);
398 return checkCacheability(req
);
403 TLB::translateData(RequestPtr req
, ThreadContext
*tc
, bool write
)
406 //@TODO: This should actually use TLB instead of going directly
407 // to the page table in syscall mode.
409 * Check for alignment faults
411 if (req
->getVaddr() & (req
->getSize() - 1)) {
412 DPRINTF(TLB
, "Alignment Fault on %#x, size = %d", req
->getVaddr(),
414 return new AlignmentFault();
418 Process
* p
= tc
->getProcessPtr();
420 Fault fault
= p
->pTable
->translate(req
);
421 if (fault
!= NoFault
)
426 if (IsKSeg0(req
->getVaddr())) {
427 // Address will not be translated through TLB, set response, and go!
428 req
->setPaddr(KSeg02Phys(req
->getVaddr()));
429 if (getOperatingMode(tc
->readMiscReg(MISCREG_STATUS
)) != mode_kernel
||
430 req
->isMisaligned()) {
431 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
432 /* BadVAddr must be set */
433 Flt
->badVAddr
= req
->getVaddr();
437 } else if(IsKSeg1(req
->getVaddr())) {
438 // Address will not be translated through TLB, set response, and go!
439 req
->setPaddr(KSeg02Phys(req
->getVaddr()));
442 * This is an optimization - smallPages is updated every time a TLB
443 * operation is performed. That way, we don't need to look at
444 * Config3 _ SP and PageGrain _ ESP every time we do a TLB lookup
446 Addr VPN
= ((req
->getVaddr() >> 11) & 0xFFFFFFFC);
447 if (smallPages
== 1) {
448 VPN
= ((req
->getVaddr() >> 11));
450 uint8_t Asid
= req
->getAsid();
451 PTE
*pte
= lookup(VPN
, Asid
);
452 if (req
->isMisaligned()) {
453 // Unaligned address!
454 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
455 /* BadVAddr must be set */
456 Flt
->badVAddr
= req
->getVaddr();
460 // Ok, found something
461 /* Check for valid bits */
465 if (((((req
->getVaddr()) >> pte
->AddrShiftAmount
) & 1)) == 0) {
477 if (Valid
== false) {
479 DtbInvalidFault
*Flt
= new DtbInvalidFault();
480 /* EntryHi VPN, ASID fields must be set */
481 Flt
->entryHiAsid
= Asid
;
482 Flt
->entryHiVPN2
= (VPN
>>2);
483 Flt
->entryHiVPN2X
= (VPN
& 0x3);
485 /* BadVAddr must be set */
486 Flt
->badVAddr
= req
->getVaddr();
488 /* Context must be set */
489 Flt
->contextBadVPN2
= (VPN
>> 2);
493 // Ok, this is really a match, set paddr
495 TLBModifiedFault
*Flt
= new TLBModifiedFault();
496 /* EntryHi VPN, ASID fields must be set */
497 Flt
->entryHiAsid
= Asid
;
498 Flt
->entryHiVPN2
= (VPN
>> 2);
499 Flt
->entryHiVPN2X
= (VPN
& 0x3);
501 /* BadVAddr must be set */
502 Flt
->badVAddr
= req
->getVaddr();
504 /* Context must be set */
505 Flt
->contextBadVPN2
= (VPN
>> 2);
514 PAddr
>>= (pte
->AddrShiftAmount
- 12);
515 PAddr
<<= pte
->AddrShiftAmount
;
516 PAddr
|= ((req
->getVaddr()) & pte
->OffsetMask
);
517 req
->setPaddr(PAddr
);
520 // Didn't find any match, return a TLB Refill Exception
521 DtbRefillFault
*Flt
= new DtbRefillFault();
522 /* EntryHi VPN, ASID fields must be set */
523 Flt
->entryHiAsid
= Asid
;
524 Flt
->entryHiVPN2
= (VPN
>> 2);
525 Flt
->entryHiVPN2X
= (VPN
& 0x3);
527 /* BadVAddr must be set */
528 Flt
->badVAddr
= req
->getVaddr();
530 /* Context must be set */
531 Flt
->contextBadVPN2
= (VPN
>> 2);
535 return checkCacheability(req
);
540 TLB::translateAtomic(RequestPtr req
, ThreadContext
*tc
, Mode mode
)
543 return translateInst(req
, tc
);
545 return translateData(req
, tc
, mode
== Write
);
549 TLB::translateTiming(RequestPtr req
, ThreadContext
*tc
,
550 Translation
*translation
, Mode mode
)
553 translation
->finish(translateAtomic(req
, tc
, mode
), req
, tc
, mode
);
558 TLB::index(bool advance
)
560 PTE
*pte
= &table
[nlu
];
569 MipsTLBParams::create()
571 return new TLB(this);