2 * Copyright (c) 2003 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.
33 #include "base/inifile.hh"
34 #include "base/str.hh"
35 #include "base/trace.hh"
36 #include "cpu/exec_context.hh"
37 #include "sim/builder.hh"
38 #include "targetarch/alpha_memory.hh"
39 #include "targetarch/ev5.hh"
43 ///////////////////////////////////////////////////////////////////////
47 AlphaTlb::AlphaTlb(const string
&name
, int s
)
48 : SimObject(name
), size(s
), nlu(0)
50 table
= new AlphaISA::PTE
[size
];
51 memset(table
, 0, sizeof(AlphaISA::PTE
[size
]));
60 // look up an entry in the TLB
62 AlphaTlb::lookup(Addr vpn
, uint8_t asn
) const
64 DPRINTF(TLB
, "lookup %#x\n", vpn
);
66 PageTable::const_iterator i
= lookupTable
.find(vpn
);
67 if (i
== lookupTable
.end())
70 while (i
->first
== vpn
) {
71 int index
= i
->second
;
72 AlphaISA::PTE
*pte
= &table
[index
];
74 if (vpn
== pte
->tag
&& (pte
->asma
|| pte
->asn
== asn
))
86 AlphaTlb::checkCacheability(MemReqPtr
&req
)
88 // in Alpha, cacheability is controlled by upper-level bits of the
90 if (req
->paddr
& PA_UNCACHED_BIT
) {
91 if (PA_IPR_SPACE(req
->paddr
)) {
92 // IPR memory space not implemented
93 if (!req
->xc
->misspeculating()) {
100 panic("IPR memory space not implemented! PA=%x\n",
105 // mark request as uncacheable
106 req
->flags
|= UNCACHEABLE
;
112 // insert a new TLB entry
114 AlphaTlb::insert(Addr vaddr
, AlphaISA::PTE
&pte
)
116 if (table
[nlu
].valid
) {
117 Addr oldvpn
= table
[nlu
].tag
;
118 PageTable::iterator i
= lookupTable
.find(oldvpn
);
120 if (i
== lookupTable
.end())
121 panic("TLB entry not found in lookupTable");
124 while ((index
= i
->second
) != nlu
) {
125 if (table
[index
].tag
!= oldvpn
)
126 panic("TLB entry not found in lookupTable");
131 DPRINTF(TLB
, "remove @%d: %#x -> %#x\n", nlu
, oldvpn
, table
[nlu
].ppn
);
133 lookupTable
.erase(i
);
136 Addr vpn
= VA_VPN(vaddr
);
137 DPRINTF(TLB
, "insert @%d: %#x -> %#x\n", nlu
, vpn
, pte
.ppn
);
140 table
[nlu
].tag
= vpn
;
141 table
[nlu
].valid
= true;
143 lookupTable
.insert(make_pair(vpn
, nlu
));
150 memset(table
, 0, sizeof(AlphaISA::PTE
[size
]));
156 AlphaTlb::flushProcesses()
158 PageTable::iterator i
= lookupTable
.begin();
159 PageTable::iterator end
= lookupTable
.end();
161 int index
= i
->second
;
162 AlphaISA::PTE
*pte
= &table
[index
];
166 DPRINTF(TLB
, "flush @%d: %#x -> %#x\n", index
, pte
->tag
, pte
->ppn
);
168 lookupTable
.erase(i
);
176 AlphaTlb::flushAddr(Addr vaddr
, uint8_t asn
)
178 Addr vpn
= VA_VPN(vaddr
);
180 PageTable::iterator i
= lookupTable
.find(vpn
);
181 if (i
== lookupTable
.end())
184 while (i
->first
== vpn
) {
185 int index
= i
->second
;
186 AlphaISA::PTE
*pte
= &table
[index
];
189 if (vpn
== pte
->tag
&& (pte
->asma
|| pte
->asn
== asn
)) {
190 DPRINTF(TLB
, "flushaddr @%d: %#x -> %#x\n", index
, vpn
, pte
->ppn
);
192 // invalidate this entry
195 lookupTable
.erase(i
);
204 AlphaTlb::serialize(ostream
&os
)
206 SERIALIZE_SCALAR(size
);
207 SERIALIZE_SCALAR(nlu
);
209 for (int i
= 0; i
< size
; i
++) {
210 nameOut(os
, csprintf("%s.PTE%d", name(), i
));
211 table
[i
].serialize(os
);
216 AlphaTlb::unserialize(Checkpoint
*cp
, const string
§ion
)
218 UNSERIALIZE_SCALAR(size
);
219 UNSERIALIZE_SCALAR(nlu
);
221 for (int i
= 0; i
< size
; i
++) {
222 table
[i
].unserialize(cp
, csprintf("%s.PTE%d", section
, i
));
223 if (table
[i
].valid
) {
224 lookupTable
.insert(make_pair(table
[i
].tag
, i
));
230 ///////////////////////////////////////////////////////////////////////
234 AlphaItb::AlphaItb(const std::string
&name
, int size
)
235 : AlphaTlb(name
, size
)
243 .name(name() + ".hits")
246 .name(name() + ".misses")
249 .name(name() + ".acv")
252 .name(name() + ".accesses")
253 .desc("ITB accesses");
255 accesses
= hits
+ misses
;
259 AlphaItb::fault(Addr pc
, ExecContext
*xc
) const
261 uint64_t *ipr
= xc
->regs
.ipr
;
263 if (!xc
->misspeculating()) {
264 ipr
[AlphaISA::IPR_ITB_TAG
] = pc
;
265 ipr
[AlphaISA::IPR_IFAULT_VA_FORM
] =
266 ipr
[AlphaISA::IPR_IVPTBR
] | (VA_VPN(pc
) << 3);
272 AlphaItb::translate(MemReqPtr
&req
) const
274 InternalProcReg
*ipr
= req
->xc
->regs
.ipr
;
276 if (PC_PAL(req
->vaddr
)) {
277 // strip off PAL PC marker (lsb is 1)
278 req
->paddr
= (req
->vaddr
& ~3) & PA_IMPL_MASK
;
283 if (req
->flags
& PHYSICAL
) {
284 req
->paddr
= req
->vaddr
;
285 } else if ((MCSR_SP(ipr
[AlphaISA::IPR_MCSR
]) & 2) &&
286 VA_SPACE(req
->vaddr
) == 2) {
287 // Check for "superpage" mapping: when SP<1> is set, and
288 // VA<42:41> == 2, VA<39:13> maps directly to PA<39:13>.
290 // only valid in kernel mode
291 if (ICM_CM(ipr
[AlphaISA::IPR_ICM
]) != AlphaISA::mode_kernel
) {
292 fault(req
->vaddr
, req
->xc
);
294 return Itb_Acv_Fault
;
297 req
->paddr
= req
->vaddr
& PA_IMPL_MASK
;
299 // verify that this is a good virtual address
300 if (!validVirtualAddress(req
->vaddr
)) {
301 fault(req
->vaddr
, req
->xc
);
303 return Itb_Acv_Fault
;
306 // not a physical address: need to look up pte
307 AlphaISA::PTE
*pte
= lookup(VA_VPN(req
->vaddr
),
308 DTB_ASN_ASN(ipr
[AlphaISA::IPR_DTB_ASN
]));
311 fault(req
->vaddr
, req
->xc
);
313 return Itb_Fault_Fault
;
316 req
->paddr
= PA_PFN2PA(pte
->ppn
) + VA_POFS(req
->vaddr
& ~3);
318 // check permissions for this access
319 if (!(pte
->xre
& (1 << ICM_CM(ipr
[AlphaISA::IPR_ICM
])))) {
320 // instruction access fault
321 fault(req
->vaddr
, req
->xc
);
323 return Itb_Acv_Fault
;
327 // check that the physical address is ok (catch bad physical addresses)
328 if (req
->paddr
& ~PA_IMPL_MASK
)
329 return Machine_Check_Fault
;
331 checkCacheability(req
);
337 ///////////////////////////////////////////////////////////////////////
341 AlphaDtb::AlphaDtb(const std::string
&name
, int size
)
342 : AlphaTlb(name
, size
)
349 .name(name() + ".read_hits")
350 .desc("DTB read hits")
354 .name(name() + ".read_misses")
355 .desc("DTB read misses")
359 .name(name() + ".read_acv")
360 .desc("DTB read access violations")
364 .name(name() + ".read_accesses")
365 .desc("DTB read accesses")
369 .name(name() + ".write_hits")
370 .desc("DTB write hits")
374 .name(name() + ".write_misses")
375 .desc("DTB write misses")
379 .name(name() + ".write_acv")
380 .desc("DTB write access violations")
384 .name(name() + ".write_accesses")
385 .desc("DTB write accesses")
389 .name(name() + ".hits")
394 .name(name() + ".misses")
399 .name(name() + ".acv")
400 .desc("DTB access violations")
404 .name(name() + ".accesses")
405 .desc("DTB accesses")
408 hits
= read_hits
+ write_hits
;
409 misses
= read_misses
+ write_misses
;
410 acv
= read_acv
+ write_acv
;
411 accesses
= read_accesses
+ write_accesses
;
415 AlphaDtb::fault(Addr vaddr
, uint64_t flags
, ExecContext
*xc
) const
417 uint64_t *ipr
= xc
->regs
.ipr
;
419 // set fault address and flags
420 if (!xc
->misspeculating() && !xc
->regs
.intrlock
) {
421 // set VA register with faulting address
422 ipr
[AlphaISA::IPR_VA
] = vaddr
;
424 // set MM_STAT register flags
425 ipr
[AlphaISA::IPR_MM_STAT
] = (((xc
->regs
.opcode
& 0x3f) << 11)
426 | ((xc
->regs
.ra
& 0x1f) << 6)
429 // set VA_FORM register with faulting formatted address
430 ipr
[AlphaISA::IPR_VA_FORM
] =
431 ipr
[AlphaISA::IPR_MVPTBR
] | (VA_VPN(vaddr
) << 3);
433 // lock these registers until the VA register is read
434 xc
->regs
.intrlock
= true;
439 AlphaDtb::translate(MemReqPtr
&req
, bool write
) const
441 RegFile
*regs
= &req
->xc
->regs
;
443 InternalProcReg
*ipr
= regs
->ipr
;
445 AlphaISA::mode_type mode
=
446 (AlphaISA::mode_type
)DTB_CM_CM(ipr
[AlphaISA::IPR_DTB_CM
]);
449 mode
= (req
->flags
& ALTMODE
) ?
450 (AlphaISA::mode_type
)ALT_MODE_AM(ipr
[AlphaISA::IPR_ALT_MODE
])
451 : AlphaISA::mode_kernel
;
454 if (req
->flags
& PHYSICAL
) {
455 req
->paddr
= req
->vaddr
;
456 } else if ((MCSR_SP(ipr
[AlphaISA::IPR_MCSR
]) & 2) &&
457 VA_SPACE(req
->vaddr
) == 2) {
458 // Check for "superpage" mapping: when SP<1> is set, and
459 // VA<42:41> == 2, VA<39:13> maps directly to PA<39:13>.
461 // only valid in kernel mode
462 if (DTB_CM_CM(ipr
[AlphaISA::IPR_DTB_CM
]) != AlphaISA::mode_kernel
) {
464 ((write
? MM_STAT_WR_MASK
: 0) | MM_STAT_ACV_MASK
),
466 if (write
) { write_acv
++; } else { read_acv
++; }
467 return Dtb_Acv_Fault
;
470 req
->paddr
= req
->vaddr
& PA_IMPL_MASK
;
477 // verify that this is a good virtual address
478 if (!validVirtualAddress(req
->vaddr
)) {
480 ((write
? MM_STAT_WR_MASK
: 0) | MM_STAT_BAD_VA_MASK
|
484 if (write
) { write_acv
++; } else { read_acv
++; }
485 return Dtb_Fault_Fault
;
488 // not a physical address: need to look up pte
489 AlphaISA::PTE
*pte
= lookup(VA_VPN(req
->vaddr
),
490 DTB_ASN_ASN(ipr
[AlphaISA::IPR_DTB_ASN
]));
495 ((write
? MM_STAT_WR_MASK
: 0) | MM_STAT_DTB_MISS_MASK
),
497 if (write
) { write_misses
++; } else { read_misses
++; }
498 return (req
->flags
& VPTE
) ? Pdtb_Miss_Fault
: Ndtb_Miss_Fault
;
501 req
->paddr
= PA_PFN2PA(pte
->ppn
) | VA_POFS(req
->vaddr
);
504 if (!(pte
->xwe
& MODE2MASK(mode
))) {
505 // declare the instruction access fault
506 fault(req
->vaddr
, MM_STAT_WR_MASK
| MM_STAT_ACV_MASK
|
507 (pte
->fonw
? MM_STAT_FONW_MASK
: 0),
510 return Dtb_Fault_Fault
;
513 fault(req
->vaddr
, MM_STAT_WR_MASK
| MM_STAT_FONW_MASK
,
516 return Dtb_Fault_Fault
;
519 if (!(pte
->xre
& MODE2MASK(mode
))) {
521 MM_STAT_ACV_MASK
| (pte
->fonr
? MM_STAT_FONR_MASK
: 0),
524 return Dtb_Acv_Fault
;
527 fault(req
->vaddr
, MM_STAT_FONR_MASK
, req
->xc
);
529 return Dtb_Fault_Fault
;
539 // check that the physical address is ok (catch bad physical addresses)
540 if (req
->paddr
& ~PA_IMPL_MASK
)
541 return Machine_Check_Fault
;
543 checkCacheability(req
);
551 AlphaISA::PTE
*pte
= &table
[nlu
];
557 BEGIN_DECLARE_SIM_OBJECT_PARAMS(AlphaItb
)
561 END_DECLARE_SIM_OBJECT_PARAMS(AlphaItb
)
563 BEGIN_INIT_SIM_OBJECT_PARAMS(AlphaItb
)
565 INIT_PARAM_DFLT(size
, "TLB size", 48)
567 END_INIT_SIM_OBJECT_PARAMS(AlphaItb
)
570 CREATE_SIM_OBJECT(AlphaItb
)
572 return new AlphaItb(getInstanceName(), size
);
575 REGISTER_SIM_OBJECT("AlphaITB", AlphaItb
)
577 BEGIN_DECLARE_SIM_OBJECT_PARAMS(AlphaDtb
)
581 END_DECLARE_SIM_OBJECT_PARAMS(AlphaDtb
)
583 BEGIN_INIT_SIM_OBJECT_PARAMS(AlphaDtb
)
585 INIT_PARAM_DFLT(size
, "TLB size", 64)
587 END_INIT_SIM_OBJECT_PARAMS(AlphaDtb
)
590 CREATE_SIM_OBJECT(AlphaDtb
)
592 return new AlphaDtb(getInstanceName(), size
);
595 REGISTER_SIM_OBJECT("AlphaDTB", AlphaDtb
)