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/faults.hh"
38 #include "arch/mips/pagetable.hh"
39 #include "arch/mips/pra_constants.hh"
40 #include "arch/mips/tlb.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 "mem/page_table.hh"
47 #include "params/MipsTLB.hh"
48 #include "sim/process.hh"
51 using namespace MipsISA
;
53 ///////////////////////////////////////////////////////////////////////
58 static inline mode_type
59 getOperatingMode(MiscReg Stat
)
61 if ((Stat
& 0x10000006) != 0 || (Stat
& 0x18) ==0) {
63 } else if ((Stat
& 0x18) == 0x8) {
64 return mode_supervisor
;
65 } else if ((Stat
& 0x18) == 0x10) {
73 TLB::TLB(const Params
*p
)
74 : BaseTLB(p
), size(p
->size
), nlu(0)
76 table
= new PTE
[size
];
77 memset(table
, 0, sizeof(PTE
[size
]));
87 // look up an entry in the TLB
89 TLB::lookup(Addr vpn
, uint8_t asn
) const
91 // assume not found...
93 PageTable::const_iterator i
= lookupTable
.find(vpn
);
94 if (i
!= lookupTable
.end()) {
95 while (i
->first
== vpn
) {
96 int index
= i
->second
;
97 PTE
*pte
= &table
[index
];
99 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
100 Addr Mask
= pte
->Mask
;
101 Addr InvMask
= ~Mask
;
103 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
104 (pte
->G
|| (asn
== pte
->asid
))) {
105 // We have a VPN + ASID Match
113 DPRINTF(TLB
, "lookup %#x, asn %#x -> %s ppn %#x\n", vpn
, (int)asn
,
114 retval
? "hit" : "miss", retval
? retval
->PFN1
: 0);
119 TLB::getEntry(unsigned Index
) const
121 // Make sure that Index is valid
123 return &table
[Index
];
127 TLB::probeEntry(Addr vpn
, uint8_t asn
) const
129 // assume not found...
132 PageTable::const_iterator i
= lookupTable
.find(vpn
);
133 if (i
!= lookupTable
.end()) {
134 while (i
->first
== vpn
) {
135 int index
= i
->second
;
136 PTE
*pte
= &table
[index
];
138 /* 1KB TLB Lookup code - from MIPS ARM Volume III - Rev. 2.50 */
139 Addr Mask
= pte
->Mask
;
140 Addr InvMask
= ~Mask
;
142 if (((vpn
& InvMask
) == (VPN
& InvMask
)) &&
143 (pte
->G
|| (asn
== pte
->asid
))) {
144 // We have a VPN + ASID Match
152 DPRINTF(MipsPRA
,"VPN: %x, asid: %d, Result of TLBP: %d\n",vpn
,asn
,Ind
);
157 TLB::checkCacheability(RequestPtr
&req
)
159 Addr VAddrUncacheable
= 0xA0000000;
160 // In MIPS, cacheability is controlled by certain bits of the virtual
161 // address or by the TLB entry
162 if ((req
->getVaddr() & VAddrUncacheable
) == VAddrUncacheable
) {
163 // mark request as uncacheable
164 req
->setFlags(Request::UNCACHEABLE
);
170 TLB::insertAt(PTE
&pte
, unsigned Index
, int _smallPages
)
172 smallPages
= _smallPages
;
174 warn("Attempted to write at index (%d) beyond TLB size (%d)",
178 DPRINTF(TLB
, "TLB[%d]: %x %x %x %x\n",
179 Index
, pte
.Mask
<< 11,
180 ((pte
.VPN
<< 11) | pte
.asid
),
181 ((pte
.PFN0
<< 6) | (pte
.C0
<< 3) |
182 (pte
.D0
<< 2) | (pte
.V0
<<1) | pte
.G
),
183 ((pte
.PFN1
<<6) | (pte
.C1
<< 3) |
184 (pte
.D1
<< 2) | (pte
.V1
<<1) | pte
.G
));
185 if (table
[Index
].V0
== true || table
[Index
].V1
== true) {
186 // Previous entry is valid
187 PageTable::iterator i
= lookupTable
.find(table
[Index
].VPN
);
188 lookupTable
.erase(i
);
191 // Update fast lookup table
192 lookupTable
.insert(make_pair(table
[Index
].VPN
, Index
));
196 // insert a new TLB entry
198 TLB::insert(Addr addr
, PTE
&pte
)
200 fatal("TLB Insert not yet implemented\n");
206 DPRINTF(TLB
, "flushAll\n");
207 memset(table
, 0, sizeof(PTE
[size
]));
213 TLB::serialize(ostream
&os
)
215 SERIALIZE_SCALAR(size
);
216 SERIALIZE_SCALAR(nlu
);
218 for (int i
= 0; i
< size
; i
++) {
219 nameOut(os
, csprintf("%s.PTE%d", name(), i
));
220 table
[i
].serialize(os
);
225 TLB::unserialize(Checkpoint
*cp
, const string
§ion
)
227 UNSERIALIZE_SCALAR(size
);
228 UNSERIALIZE_SCALAR(nlu
);
230 for (int i
= 0; i
< size
; i
++) {
231 table
[i
].unserialize(cp
, csprintf("%s.PTE%d", section
, i
));
232 if (table
[i
].V0
|| table
[i
].V1
) {
233 lookupTable
.insert(make_pair(table
[i
].VPN
, i
));
242 .name(name() + ".read_hits")
243 .desc("DTB read hits")
247 .name(name() + ".read_misses")
248 .desc("DTB read misses")
253 .name(name() + ".read_accesses")
254 .desc("DTB read accesses")
258 .name(name() + ".write_hits")
259 .desc("DTB write hits")
263 .name(name() + ".write_misses")
264 .desc("DTB write misses")
269 .name(name() + ".write_accesses")
270 .desc("DTB write accesses")
274 .name(name() + ".hits")
279 .name(name() + ".misses")
284 .name(name() + ".accesses")
285 .desc("DTB accesses")
288 hits
= read_hits
+ write_hits
;
289 misses
= read_misses
+ write_misses
;
290 accesses
= read_accesses
+ write_accesses
;
294 TLB::translateInst(RequestPtr req
, ThreadContext
*tc
)
297 Process
* p
= tc
->getProcessPtr();
299 Fault fault
= p
->pTable
->translate(req
);
300 if (fault
!= NoFault
)
305 Addr vaddr
= req
->getVaddr();
307 bool misaligned
= (req
->getSize() - 1) & vaddr
;
309 if (IsKSeg0(vaddr
)) {
310 // Address will not be translated through TLB, set response, and go!
311 req
->setPaddr(KSeg02Phys(vaddr
));
312 if (getOperatingMode(tc
->readMiscReg(MISCREG_STATUS
)) != mode_kernel
||
314 AddressErrorFault
*Flt
= new AddressErrorFault();
315 /* BadVAddr must be set */
316 Flt
->badVAddr
= vaddr
;
319 } else if(IsKSeg1(vaddr
)) {
320 // Address will not be translated through TLB, set response, and go!
321 req
->setPaddr(KSeg02Phys(vaddr
));
324 * This is an optimization - smallPages is updated every time a TLB
325 * operation is performed. That way, we don't need to look at
326 * Config3 _ SP and PageGrain _ ESP every time we do a TLB lookup
329 if (smallPages
== 1) {
332 VPN
= ((vaddr
>> 11) & 0xFFFFFFFC);
334 uint8_t Asid
= req
->getAsid();
336 // Unaligned address!
337 AddressErrorFault
*Flt
= new AddressErrorFault();
338 /* BadVAddr must be set */
339 Flt
->badVAddr
= vaddr
;
342 PTE
*pte
= lookup(VPN
,Asid
);
344 // Ok, found something
345 /* Check for valid bits */
348 if ((((vaddr
) >> pte
->AddrShiftAmount
) & 1) == 0) {
358 if (Valid
== false) {
360 ItbInvalidFault
*Flt
= new ItbInvalidFault();
361 /* EntryHi VPN, ASID fields must be set */
362 Flt
->entryHiAsid
= Asid
;
363 Flt
->entryHiVPN2
= (VPN
>> 2);
364 Flt
->entryHiVPN2X
= (VPN
& 0x3);
366 /* BadVAddr must be set */
367 Flt
->badVAddr
= vaddr
;
369 /* Context must be set */
370 Flt
->contextBadVPN2
= (VPN
>> 2);
373 // Ok, this is really a match, set paddr
380 PAddr
>>= (pte
->AddrShiftAmount
- 12);
381 PAddr
<<= pte
->AddrShiftAmount
;
382 PAddr
|= (vaddr
& pte
->OffsetMask
);
383 req
->setPaddr(PAddr
);
386 // Didn't find any match, return a TLB Refill Exception
387 ItbRefillFault
*Flt
= new ItbRefillFault();
388 /* EntryHi VPN, ASID fields must be set */
389 Flt
->entryHiAsid
= Asid
;
390 Flt
->entryHiVPN2
= (VPN
>> 2);
391 Flt
->entryHiVPN2X
= (VPN
& 0x3);
393 /* BadVAddr must be set */
394 Flt
->badVAddr
= vaddr
;
396 /* Context must be set */
397 Flt
->contextBadVPN2
= (VPN
>> 2);
401 return checkCacheability(req
);
406 TLB::translateData(RequestPtr req
, ThreadContext
*tc
, bool write
)
409 //@TODO: This should actually use TLB instead of going directly
410 // to the page table in syscall mode.
412 * Check for alignment faults
414 if (req
->getVaddr() & (req
->getSize() - 1)) {
415 DPRINTF(TLB
, "Alignment Fault on %#x, size = %d", req
->getVaddr(),
417 return new AlignmentFault();
421 Process
* p
= tc
->getProcessPtr();
423 Fault fault
= p
->pTable
->translate(req
);
424 if (fault
!= NoFault
)
429 Addr vaddr
= req
->getVaddr();
431 bool misaligned
= (req
->getSize() - 1) & vaddr
;
433 if (IsKSeg0(vaddr
)) {
434 // Address will not be translated through TLB, set response, and go!
435 req
->setPaddr(KSeg02Phys(vaddr
));
436 if (getOperatingMode(tc
->readMiscReg(MISCREG_STATUS
)) != mode_kernel
||
438 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
439 /* BadVAddr must be set */
440 Flt
->badVAddr
= vaddr
;
444 } else if(IsKSeg1(vaddr
)) {
445 // Address will not be translated through TLB, set response, and go!
446 req
->setPaddr(KSeg02Phys(vaddr
));
449 * This is an optimization - smallPages is updated every time a TLB
450 * operation is performed. That way, we don't need to look at
451 * Config3 _ SP and PageGrain _ ESP every time we do a TLB lookup
453 Addr VPN
= (vaddr
>> 11) & 0xFFFFFFFC;
454 if (smallPages
== 1) {
457 uint8_t Asid
= req
->getAsid();
458 PTE
*pte
= lookup(VPN
, Asid
);
460 // Unaligned address!
461 StoreAddressErrorFault
*Flt
= new StoreAddressErrorFault();
462 /* BadVAddr must be set */
463 Flt
->badVAddr
= vaddr
;
467 // Ok, found something
468 /* Check for valid bits */
472 if ((((vaddr
>> pte
->AddrShiftAmount
) & 1)) == 0) {
484 if (Valid
== false) {
486 DtbInvalidFault
*Flt
= new DtbInvalidFault();
487 /* EntryHi VPN, ASID fields must be set */
488 Flt
->entryHiAsid
= Asid
;
489 Flt
->entryHiVPN2
= (VPN
>>2);
490 Flt
->entryHiVPN2X
= (VPN
& 0x3);
492 /* BadVAddr must be set */
493 Flt
->badVAddr
= vaddr
;
495 /* Context must be set */
496 Flt
->contextBadVPN2
= (VPN
>> 2);
500 // Ok, this is really a match, set paddr
502 TLBModifiedFault
*Flt
= new TLBModifiedFault();
503 /* EntryHi VPN, ASID fields must be set */
504 Flt
->entryHiAsid
= Asid
;
505 Flt
->entryHiVPN2
= (VPN
>> 2);
506 Flt
->entryHiVPN2X
= (VPN
& 0x3);
508 /* BadVAddr must be set */
509 Flt
->badVAddr
= vaddr
;
511 /* Context must be set */
512 Flt
->contextBadVPN2
= (VPN
>> 2);
521 PAddr
>>= (pte
->AddrShiftAmount
- 12);
522 PAddr
<<= pte
->AddrShiftAmount
;
523 PAddr
|= (vaddr
& pte
->OffsetMask
);
524 req
->setPaddr(PAddr
);
527 // Didn't find any match, return a TLB Refill Exception
528 DtbRefillFault
*Flt
= new DtbRefillFault();
529 /* EntryHi VPN, ASID fields must be set */
530 Flt
->entryHiAsid
= Asid
;
531 Flt
->entryHiVPN2
= (VPN
>> 2);
532 Flt
->entryHiVPN2X
= (VPN
& 0x3);
534 /* BadVAddr must be set */
535 Flt
->badVAddr
= vaddr
;
537 /* Context must be set */
538 Flt
->contextBadVPN2
= (VPN
>> 2);
542 return checkCacheability(req
);
547 TLB::translateAtomic(RequestPtr req
, ThreadContext
*tc
, Mode mode
)
550 return translateInst(req
, tc
);
552 return translateData(req
, tc
, mode
== Write
);
556 TLB::translateTiming(RequestPtr req
, ThreadContext
*tc
,
557 Translation
*translation
, Mode mode
)
560 translation
->finish(translateAtomic(req
, tc
, mode
), req
, tc
, mode
);
565 TLB::index(bool advance
)
567 PTE
*pte
= &table
[nlu
];
576 MipsTLBParams::create()
578 return new TLB(this);