2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
36 #include "arch/alpha/pagetable.hh"
37 #include "arch/alpha/tlb.hh"
38 #include "arch/alpha/faults.hh"
39 #include "base/inifile.hh"
40 #include "base/str.hh"
41 #include "base/trace.hh"
42 #include "cpu/thread_context.hh"
48 ///////////////////////////////////////////////////////////////////////
54 bool uncacheBit39
= false;
55 bool uncacheBit40
= false;
58 #define MODE2MASK(X) (1 << (X))
60 TLB::TLB(const Params
*p
)
61 : BaseTLB(p
), size(p
->size
), nlu(0)
63 table
= new TlbEntry
[size
];
64 memset(table
, 0, sizeof(TlbEntry
[size
]));
78 .name(name() + ".fetch_hits")
81 .name(name() + ".fetch_misses")
84 .name(name() + ".fetch_acv")
87 .name(name() + ".fetch_accesses")
88 .desc("ITB accesses");
90 fetch_accesses
= fetch_hits
+ fetch_misses
;
93 .name(name() + ".read_hits")
94 .desc("DTB read hits")
98 .name(name() + ".read_misses")
99 .desc("DTB read misses")
103 .name(name() + ".read_acv")
104 .desc("DTB read access violations")
108 .name(name() + ".read_accesses")
109 .desc("DTB read accesses")
113 .name(name() + ".write_hits")
114 .desc("DTB write hits")
118 .name(name() + ".write_misses")
119 .desc("DTB write misses")
123 .name(name() + ".write_acv")
124 .desc("DTB write access violations")
128 .name(name() + ".write_accesses")
129 .desc("DTB write accesses")
133 .name(name() + ".data_hits")
138 .name(name() + ".data_misses")
143 .name(name() + ".data_acv")
144 .desc("DTB access violations")
148 .name(name() + ".data_accesses")
149 .desc("DTB accesses")
152 data_hits
= read_hits
+ write_hits
;
153 data_misses
= read_misses
+ write_misses
;
154 data_acv
= read_acv
+ write_acv
;
155 data_accesses
= read_accesses
+ write_accesses
;
158 // look up an entry in the TLB
160 TLB::lookup(Addr vpn
, uint8_t asn
)
162 // assume not found...
163 TlbEntry
*retval
= NULL
;
166 if (vpn
== EntryCache
[0]->tag
&&
167 (EntryCache
[0]->asma
|| EntryCache
[0]->asn
== asn
))
168 retval
= EntryCache
[0];
169 else if (EntryCache
[1]) {
170 if (vpn
== EntryCache
[1]->tag
&&
171 (EntryCache
[1]->asma
|| EntryCache
[1]->asn
== asn
))
172 retval
= EntryCache
[1];
173 else if (EntryCache
[2] && vpn
== EntryCache
[2]->tag
&&
174 (EntryCache
[2]->asma
|| EntryCache
[2]->asn
== asn
))
175 retval
= EntryCache
[2];
179 if (retval
== NULL
) {
180 PageTable::const_iterator i
= lookupTable
.find(vpn
);
181 if (i
!= lookupTable
.end()) {
182 while (i
->first
== vpn
) {
183 int index
= i
->second
;
184 TlbEntry
*entry
= &table
[index
];
185 assert(entry
->valid
);
186 if (vpn
== entry
->tag
&& (entry
->asma
|| entry
->asn
== asn
)) {
187 retval
= updateCache(entry
);
196 DPRINTF(TLB
, "lookup %#x, asn %#x -> %s ppn %#x\n", vpn
, (int)asn
,
197 retval
? "hit" : "miss", retval
? retval
->ppn
: 0);
202 TLB::checkCacheability(RequestPtr
&req
, bool itb
)
204 // in Alpha, cacheability is controlled by upper-level bits of the
208 * We support having the uncacheable bit in either bit 39 or bit
209 * 40. The Turbolaser platform (and EV5) support having the bit
210 * in 39, but Tsunami (which Linux assumes uses an EV6) generates
211 * accesses with the bit in 40. So we must check for both, but we
212 * have debug flags to catch a weird case where both are used,
213 * which shouldn't happen.
217 if (req
->getPaddr() & PAddrUncachedBit43
) {
218 // IPR memory space not implemented
219 if (PAddrIprSpace(req
->getPaddr())) {
220 return new UnimpFault("IPR memory space not implemented!");
222 // mark request as uncacheable
223 req
->setFlags(Request::UNCACHEABLE
);
225 // Clear bits 42:35 of the physical address (10-2 in
227 req
->setPaddr(req
->getPaddr() & PAddrUncachedMask
);
229 // We shouldn't be able to read from an uncachable address in Alpha as
230 // we don't have a ROM and we don't want to try to fetch from a device
231 // register as we destroy any data that is clear-on-read.
232 if (req
->isUncacheable() && itb
)
233 return new UnimpFault("CPU trying to fetch from uncached I/O");
240 // insert a new TLB entry
242 TLB::insert(Addr addr
, TlbEntry
&entry
)
246 if (table
[nlu
].valid
) {
247 Addr oldvpn
= table
[nlu
].tag
;
248 PageTable::iterator i
= lookupTable
.find(oldvpn
);
250 if (i
== lookupTable
.end())
251 panic("TLB entry not found in lookupTable");
254 while ((index
= i
->second
) != nlu
) {
255 if (table
[index
].tag
!= oldvpn
)
256 panic("TLB entry not found in lookupTable");
261 DPRINTF(TLB
, "remove @%d: %#x -> %#x\n", nlu
, oldvpn
, table
[nlu
].ppn
);
263 lookupTable
.erase(i
);
266 DPRINTF(TLB
, "insert @%d: %#x -> %#x\n", nlu
, vaddr
.vpn(), entry
.ppn
);
269 table
[nlu
].tag
= vaddr
.vpn();
270 table
[nlu
].valid
= true;
272 lookupTable
.insert(make_pair(vaddr
.vpn(), nlu
));
279 DPRINTF(TLB
, "flushAll\n");
280 memset(table
, 0, sizeof(TlbEntry
[size
]));
287 TLB::flushProcesses()
290 PageTable::iterator i
= lookupTable
.begin();
291 PageTable::iterator end
= lookupTable
.end();
293 int index
= i
->second
;
294 TlbEntry
*entry
= &table
[index
];
295 assert(entry
->valid
);
297 // we can't increment i after we erase it, so save a copy and
298 // increment it to get the next entry now
299 PageTable::iterator cur
= i
;
303 DPRINTF(TLB
, "flush @%d: %#x -> %#x\n", index
,
304 entry
->tag
, entry
->ppn
);
305 entry
->valid
= false;
306 lookupTable
.erase(cur
);
312 TLB::flushAddr(Addr addr
, uint8_t asn
)
317 PageTable::iterator i
= lookupTable
.find(vaddr
.vpn());
318 if (i
== lookupTable
.end())
321 while (i
!= lookupTable
.end() && i
->first
== vaddr
.vpn()) {
322 int index
= i
->second
;
323 TlbEntry
*entry
= &table
[index
];
324 assert(entry
->valid
);
326 if (vaddr
.vpn() == entry
->tag
&& (entry
->asma
|| entry
->asn
== asn
)) {
327 DPRINTF(TLB
, "flushaddr @%d: %#x -> %#x\n", index
, vaddr
.vpn(),
330 // invalidate this entry
331 entry
->valid
= false;
333 lookupTable
.erase(i
++);
342 TLB::serialize(ostream
&os
)
344 SERIALIZE_SCALAR(size
);
345 SERIALIZE_SCALAR(nlu
);
347 for (int i
= 0; i
< size
; i
++) {
348 nameOut(os
, csprintf("%s.Entry%d", name(), i
));
349 table
[i
].serialize(os
);
354 TLB::unserialize(Checkpoint
*cp
, const string
§ion
)
356 UNSERIALIZE_SCALAR(size
);
357 UNSERIALIZE_SCALAR(nlu
);
359 for (int i
= 0; i
< size
; i
++) {
360 table
[i
].unserialize(cp
, csprintf("%s.Entry%d", section
, i
));
361 if (table
[i
].valid
) {
362 lookupTable
.insert(make_pair(table
[i
].tag
, i
));
368 TLB::translateInst(RequestPtr req
, ThreadContext
*tc
)
370 //If this is a pal pc, then set PHYSICAL
371 if (FULL_SYSTEM
&& PcPAL(req
->getPC()))
372 req
->setFlags(Request::PHYSICAL
);
374 if (PcPAL(req
->getPC())) {
375 // strip off PAL PC marker (lsb is 1)
376 req
->setPaddr((req
->getVaddr() & ~3) & PAddrImplMask
);
381 if (req
->getFlags() & Request::PHYSICAL
) {
382 req
->setPaddr(req
->getVaddr());
384 // verify that this is a good virtual address
385 if (!validVirtualAddress(req
->getVaddr())) {
387 return new ItbAcvFault(req
->getVaddr());
391 // VA<42:41> == 2, VA<39:13> maps directly to PA<39:13> for EV5
392 // VA<47:41> == 0x7e, VA<40:13> maps directly to PA<40:13> for EV6
393 if (VAddrSpaceEV6(req
->getVaddr()) == 0x7e) {
394 // only valid in kernel mode
395 if (ICM_CM(tc
->readMiscRegNoEffect(IPR_ICM
)) !=
398 return new ItbAcvFault(req
->getVaddr());
401 req
->setPaddr(req
->getVaddr() & PAddrImplMask
);
403 // sign extend the physical address properly
404 if (req
->getPaddr() & PAddrUncachedBit40
)
405 req
->setPaddr(req
->getPaddr() | ULL(0xf0000000000));
407 req
->setPaddr(req
->getPaddr() & ULL(0xffffffffff));
409 // not a physical address: need to look up pte
410 int asn
= DTB_ASN_ASN(tc
->readMiscRegNoEffect(IPR_DTB_ASN
));
411 TlbEntry
*entry
= lookup(VAddr(req
->getVaddr()).vpn(),
416 return new ItbPageFault(req
->getVaddr());
419 req
->setPaddr((entry
->ppn
<< PageShift
) +
420 (VAddr(req
->getVaddr()).offset()
423 // check permissions for this access
425 (1 << ICM_CM(tc
->readMiscRegNoEffect(IPR_ICM
))))) {
426 // instruction access fault
428 return new ItbAcvFault(req
->getVaddr());
435 // check that the physical address is ok (catch bad physical addresses)
436 if (req
->getPaddr() & ~PAddrImplMask
)
437 return genMachineCheckFault();
439 return checkCacheability(req
, true);
444 TLB::translateData(RequestPtr req
, ThreadContext
*tc
, bool write
)
446 Addr pc
= tc
->readPC();
449 (mode_type
)DTB_CM_CM(tc
->readMiscRegNoEffect(IPR_DTB_CM
));
452 * Check for alignment faults
454 if (req
->getVaddr() & (req
->getSize() - 1)) {
455 DPRINTF(TLB
, "Alignment Fault on %#x, size = %d", req
->getVaddr(),
457 uint64_t flags
= write
? MM_STAT_WR_MASK
: 0;
458 return new DtbAlignmentFault(req
->getVaddr(), req
->getFlags(), flags
);
462 mode
= (req
->getFlags() & Request::ALTMODE
) ?
463 (mode_type
)ALT_MODE_AM(
464 tc
->readMiscRegNoEffect(IPR_ALT_MODE
))
468 if (req
->getFlags() & Request::PHYSICAL
) {
469 req
->setPaddr(req
->getVaddr());
471 // verify that this is a good virtual address
472 if (!validVirtualAddress(req
->getVaddr())) {
473 if (write
) { write_acv
++; } else { read_acv
++; }
474 uint64_t flags
= (write
? MM_STAT_WR_MASK
: 0) |
475 MM_STAT_BAD_VA_MASK
|
477 return new DtbPageFault(req
->getVaddr(), req
->getFlags(), flags
);
480 // Check for "superpage" mapping
481 if (VAddrSpaceEV6(req
->getVaddr()) == 0x7e) {
482 // only valid in kernel mode
483 if (DTB_CM_CM(tc
->readMiscRegNoEffect(IPR_DTB_CM
)) !=
485 if (write
) { write_acv
++; } else { read_acv
++; }
486 uint64_t flags
= ((write
? MM_STAT_WR_MASK
: 0) |
489 return new DtbAcvFault(req
->getVaddr(), req
->getFlags(),
493 req
->setPaddr(req
->getVaddr() & PAddrImplMask
);
495 // sign extend the physical address properly
496 if (req
->getPaddr() & PAddrUncachedBit40
)
497 req
->setPaddr(req
->getPaddr() | ULL(0xf0000000000));
499 req
->setPaddr(req
->getPaddr() & ULL(0xffffffffff));
506 int asn
= DTB_ASN_ASN(tc
->readMiscRegNoEffect(IPR_DTB_ASN
));
508 // not a physical address: need to look up pte
509 TlbEntry
*entry
= lookup(VAddr(req
->getVaddr()).vpn(), asn
);
513 if (write
) { write_misses
++; } else { read_misses
++; }
514 uint64_t flags
= (write
? MM_STAT_WR_MASK
: 0) |
515 MM_STAT_DTB_MISS_MASK
;
516 return (req
->getFlags() & Request::VPTE
) ?
517 (Fault
)(new PDtbMissFault(req
->getVaddr(), req
->getFlags(),
519 (Fault
)(new NDtbMissFault(req
->getVaddr(), req
->getFlags(),
523 req
->setPaddr((entry
->ppn
<< PageShift
) +
524 VAddr(req
->getVaddr()).offset());
527 if (!(entry
->xwe
& MODE2MASK(mode
))) {
528 // declare the instruction access fault
530 uint64_t flags
= MM_STAT_WR_MASK
|
532 (entry
->fonw
? MM_STAT_FONW_MASK
: 0);
533 return new DtbPageFault(req
->getVaddr(), req
->getFlags(),
538 uint64_t flags
= MM_STAT_WR_MASK
| MM_STAT_FONW_MASK
;
539 return new DtbPageFault(req
->getVaddr(), req
->getFlags(),
543 if (!(entry
->xre
& MODE2MASK(mode
))) {
545 uint64_t flags
= MM_STAT_ACV_MASK
|
546 (entry
->fonr
? MM_STAT_FONR_MASK
: 0);
547 return new DtbAcvFault(req
->getVaddr(), req
->getFlags(),
552 uint64_t flags
= MM_STAT_FONR_MASK
;
553 return new DtbPageFault(req
->getVaddr(), req
->getFlags(),
565 // check that the physical address is ok (catch bad physical addresses)
566 if (req
->getPaddr() & ~PAddrImplMask
)
567 return genMachineCheckFault();
569 return checkCacheability(req
);
573 TLB::index(bool advance
)
575 TlbEntry
*entry
= &table
[nlu
];
584 TLB::translateAtomic(RequestPtr req
, ThreadContext
*tc
, Mode mode
)
587 return translateInst(req
, tc
);
589 return translateData(req
, tc
, mode
== Write
);
593 TLB::translateTiming(RequestPtr req
, ThreadContext
*tc
,
594 Translation
*translation
, Mode mode
)
597 translation
->finish(translateAtomic(req
, tc
, mode
), req
, tc
, mode
);
600 /* end namespace AlphaISA */ }
603 AlphaTLBParams::create()
605 return new AlphaISA::TLB(this);