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.
38 #include "base/cprintf.hh"
39 #include "base/inifile.hh"
40 #include "base/loader/symtab.hh"
41 #include "base/misc.hh"
42 #include "base/pollevent.hh"
43 #include "base/range.hh"
44 #include "base/trace.hh"
45 #include "cpu/base_cpu.hh"
46 #include "cpu/exec_context.hh"
47 #include "cpu/exetrace.hh"
48 #include "cpu/full_cpu/smt.hh"
49 #include "cpu/simple_cpu/simple_cpu.hh"
50 #include "cpu/static_inst.hh"
51 #include "mem/base_mem.hh"
52 #include "mem/mem_interface.hh"
53 #include "sim/annotation.hh"
54 #include "sim/builder.hh"
55 #include "sim/debug.hh"
56 #include "sim/host.hh"
57 #include "sim/sim_events.hh"
58 #include "sim/sim_object.hh"
59 #include "sim/sim_stats.hh"
62 #include "base/remote_gdb.hh"
63 #include "dev/alpha_access.h"
64 #include "dev/pciareg.h"
65 #include "mem/functional_mem/memory_control.hh"
66 #include "mem/functional_mem/physical_memory.hh"
67 #include "sim/system.hh"
68 #include "targetarch/alpha_memory.hh"
69 #include "targetarch/vtophys.hh"
72 #include "mem/functional_mem/functional_memory.hh"
73 #include "sim/prog.hh"
78 SimpleCPU::CacheCompletionEvent::CacheCompletionEvent(SimpleCPU
*_cpu
)
79 : Event(&mainEventQueue
),
84 void SimpleCPU::CacheCompletionEvent::process()
86 cpu
->processCacheCompletion();
90 SimpleCPU::CacheCompletionEvent::description()
92 return "cache completion event";
96 SimpleCPU::SimpleCPU(const string
&_name
,
98 Counter max_insts_any_thread
,
99 Counter max_insts_all_threads
,
100 Counter max_loads_any_thread
,
101 Counter max_loads_all_threads
,
102 AlphaItb
*itb
, AlphaDtb
*dtb
,
103 FunctionalMemory
*mem
,
104 MemInterface
*icache_interface
,
105 MemInterface
*dcache_interface
,
107 : BaseCPU(_name
, /* number_of_threads */ 1,
108 max_insts_any_thread
, max_insts_all_threads
,
109 max_loads_any_thread
, max_loads_all_threads
,
112 SimpleCPU::SimpleCPU(const string
&_name
, Process
*_process
,
113 Counter max_insts_any_thread
,
114 Counter max_insts_all_threads
,
115 Counter max_loads_any_thread
,
116 Counter max_loads_all_threads
,
117 MemInterface
*icache_interface
,
118 MemInterface
*dcache_interface
)
119 : BaseCPU(_name
, /* number_of_threads */ 1,
120 max_insts_any_thread
, max_insts_all_threads
,
121 max_loads_any_thread
, max_loads_all_threads
),
123 tickEvent(this), xc(NULL
), cacheCompletionEvent(this)
127 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
);
129 TheISA::initCPU(&xc
->regs
);
131 IntReg
*ipr
= xc
->regs
.ipr
;
132 ipr
[TheISA::IPR_MCSR
] = 0x6;
134 AlphaISA::swap_palshadow(&xc
->regs
, true);
137 xc
->regs
.pc
= ipr
[TheISA::IPR_PAL_BASE
] + AlphaISA::fault_addr
[fault
];
138 xc
->regs
.npc
= xc
->regs
.pc
+ sizeof(MachInst
);
140 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
142 #endif // !FULL_SYSTEM
144 icacheInterface
= icache_interface
;
145 dcacheInterface
= dcache_interface
;
147 memReq
= new MemReq();
150 memReq
->data
= new uint8_t[64];
158 execContexts
.push_back(xc
);
161 SimpleCPU::~SimpleCPU()
167 SimpleCPU::switchOut()
169 _status
= SwitchedOut
;
170 if (tickEvent
.scheduled())
176 SimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
178 BaseCPU::takeOverFrom(oldCPU
);
180 assert(!tickEvent
.scheduled());
182 // if any of this CPU's ExecContexts are active, mark the CPU as
183 // running and schedule its tick event.
184 for (int i
= 0; i
< execContexts
.size(); ++i
) {
185 ExecContext
*xc
= execContexts
[i
];
186 if (xc
->status() == ExecContext::Active
&& _status
!= Running
) {
188 tickEvent
.schedule(curTick
);
197 SimpleCPU::execCtxStatusChg(int thread_num
) {
198 assert(thread_num
== 0);
201 if (xc
->status() == ExecContext::Active
)
209 SimpleCPU::regStats()
214 .name(name() + ".num_insts")
215 .desc("Number of instructions executed")
219 .name(name() + ".num_refs")
220 .desc("Number of memory references")
224 .name(name() + ".idle_cycles")
225 .desc("Number of idle cycles")
229 .name(name() + ".idle_fraction")
230 .desc("Percentage of idle cycles")
234 .name(name() + ".icache_stall_cycles")
235 .desc("ICache total stall cycles")
236 .prereq(icacheStallCycles
)
240 .name(name() + ".dcache_stall_cycles")
241 .desc("DCache total stall cycles")
242 .prereq(dcacheStallCycles
)
245 idleFraction
= idleCycles
/ simTicks
;
247 numInsts
= Statistics::scalar(numInst
);
248 simInsts
+= numInsts
;
252 SimpleCPU::serialize()
258 // do we need this anymore?? egh
259 childOut("itb", xc
->itb
);
260 childOut("dtb", xc
->dtb
);
261 childOut("physmem", physmem
);
265 for (int i
= 0; i
< NumIntRegs
; i
++) {
267 ccprintf(buf
, "R%02d", i
);
268 paramOut(buf
.str(), xc
->regs
.intRegFile
[i
]);
270 for (int i
= 0; i
< NumFloatRegs
; i
++) {
272 ccprintf(buf
, "F%02d", i
);
273 paramOut(buf
.str(), xc
->regs
.floatRegFile
.d
[i
]);
275 // CPUTraitsType::serializeSpecialRegs(getProxy(), xc->regs);
279 SimpleCPU::unserialize(IniFile
&db
, const string
&category
, ConfigNode
*node
)
283 for (int i
= 0; i
< NumIntRegs
; i
++) {
285 ccprintf(buf
, "R%02d", i
);
286 db
.findDefault(category
, buf
.str(), data
);
287 to_number(data
,xc
->regs
.intRegFile
[i
]);
289 for (int i
= 0; i
< NumFloatRegs
; i
++) {
291 ccprintf(buf
, "F%02d", i
);
292 db
.findDefault(category
, buf
.str(), data
);
293 xc
->regs
.floatRegFile
.d
[i
] = strtod(data
.c_str(),NULL
);
296 // Read in Special registers
298 // CPUTraitsType::unserializeSpecialRegs(db,category,node,xc->regs);
302 change_thread_state(int thread_number
, int activate
, int priority
)
306 // precise architected memory state accessor macros
309 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
311 memReq
->reset(addr
, sizeof(T
), flags
);
313 // translate to physical address
314 Fault fault
= xc
->translateDataReadReq(memReq
);
316 // do functional access
317 if (fault
== No_Fault
)
318 fault
= xc
->read(memReq
, data
);
321 traceData
->setAddr(addr
);
322 if (fault
== No_Fault
)
323 traceData
->setData(data
);
326 // if we have a cache, do cache access too
327 if (fault
== No_Fault
&& dcacheInterface
) {
329 memReq
->completionEvent
= NULL
;
330 memReq
->time
= curTick
;
331 memReq
->flags
&= ~UNCACHEABLE
;
332 MemAccessResult result
= dcacheInterface
->access(memReq
);
334 // Ugly hack to get an event scheduled *only* if the access is
335 // a miss. We really should add first-class support for this
337 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
338 memReq
->completionEvent
= &cacheCompletionEvent
;
339 setStatus(DcacheMissStall
);
346 #ifndef DOXYGEN_SHOULD_SKIP_THIS
350 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
354 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
358 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
362 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
364 #endif //DOXYGEN_SHOULD_SKIP_THIS
368 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
370 return read(addr
, *(uint64_t*)&data
, flags
);
375 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
377 return read(addr
, *(uint32_t*)&data
, flags
);
383 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
385 return read(addr
, (uint32_t&)data
, flags
);
391 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
394 traceData
->setAddr(addr
);
395 traceData
->setData(data
);
398 memReq
->reset(addr
, sizeof(T
), flags
);
400 // translate to physical address
401 Fault fault
= xc
->translateDataWriteReq(memReq
);
403 // do functional access
404 if (fault
== No_Fault
)
405 fault
= xc
->write(memReq
, data
);
407 if (fault
== No_Fault
&& dcacheInterface
) {
409 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
410 memReq
->completionEvent
= NULL
;
411 memReq
->time
= curTick
;
412 memReq
->flags
&= ~UNCACHEABLE
;
413 MemAccessResult result
= dcacheInterface
->access(memReq
);
415 // Ugly hack to get an event scheduled *only* if the access is
416 // a miss. We really should add first-class support for this
418 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
419 memReq
->completionEvent
= &cacheCompletionEvent
;
420 setStatus(DcacheMissStall
);
424 if (res
&& (fault
== No_Fault
))
425 *res
= memReq
->result
;
431 #ifndef DOXYGEN_SHOULD_SKIP_THIS
434 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
438 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
442 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
446 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
448 #endif //DOXYGEN_SHOULD_SKIP_THIS
452 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
454 return write(*(uint64_t*)&data
, addr
, flags
, res
);
459 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
461 return write(*(uint32_t*)&data
, addr
, flags
, res
);
467 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
469 return write((uint32_t)data
, addr
, flags
, res
);
475 SimpleCPU::dbg_vtophys(Addr addr
)
477 return vtophys(xc
, addr
);
479 #endif // FULL_SYSTEM
485 SimpleCPU::processCacheCompletion()
488 case IcacheMissStall
:
489 icacheStallCycles
+= curTick
- lastIcacheStall
;
490 setStatus(IcacheMissComplete
);
492 case DcacheMissStall
:
493 dcacheStallCycles
+= curTick
- lastDcacheStall
;
497 // If this CPU has been switched out due to sampling/warm-up,
498 // ignore any further status changes (e.g., due to cache
499 // misses outstanding at the time of the switch).
502 panic("SimpleCPU::processCacheCompletion: bad state");
509 SimpleCPU::post_interrupt(int int_num
, int index
)
511 BaseCPU::post_interrupt(int_num
, index
);
513 if (xc
->status() == ExecContext::Suspended
) {
514 DPRINTF(IPI
,"Suspended Processor awoke\n");
515 xc
->setStatus(ExecContext::Active
);
516 Annotate::Resume(xc
);
519 #endif // FULL_SYSTEM
521 /* start simulation, program loaded, processor precise state initialized */
528 if (fault
== No_Fault
&& AlphaISA::check_interrupts
&&
529 xc
->cpu
->check_interrupts() &&
530 !PC_PAL(xc
->regs
.pc
) &&
531 status() != IcacheMissComplete
) {
534 AlphaISA::check_interrupts
= 0;
535 IntReg
*ipr
= xc
->regs
.ipr
;
537 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
538 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
539 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
540 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
541 // See table 4-19 of 21164 hardware reference
542 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
543 summary
|= (ULL(1) << i
);
548 uint64_t interrupts
= xc
->cpu
->intr_status();
549 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
550 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
551 if (interrupts
& (ULL(1) << i
)) {
552 // See table 4-19 of 21164 hardware reference
554 summary
|= (ULL(1) << i
);
558 if (ipr
[TheISA::IPR_ASTRR
])
559 panic("asynchronous traps not implemented\n");
561 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
562 ipr
[TheISA::IPR_ISR
] = summary
;
563 ipr
[TheISA::IPR_INTID
] = ipl
;
564 xc
->ev5_trap(Interrupt_Fault
);
566 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
567 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
572 // maintain $r0 semantics
573 xc
->regs
.intRegFile
[ZeroReg
] = 0;
575 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
576 #endif // TARGET_ALPHA
578 if (status() == IcacheMissComplete
) {
579 // We've already fetched an instruction and were stalled on an
580 // I-cache miss. No need to fetch it again.
585 // Try to fetch an instruction
587 // set up memory request for instruction fetch
589 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
591 #define IFETCH_FLAGS(pc) 0
595 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
596 IFETCH_FLAGS(xc
->regs
.pc
));
598 fault
= xc
->translateInstReq(memReq
);
600 if (fault
== No_Fault
)
601 fault
= xc
->mem
->read(memReq
, inst
);
603 if (icacheInterface
&& fault
== No_Fault
) {
604 memReq
->completionEvent
= NULL
;
606 memReq
->time
= curTick
;
607 memReq
->flags
&= ~UNCACHEABLE
;
608 MemAccessResult result
= icacheInterface
->access(memReq
);
610 // Ugly hack to get an event scheduled *only* if the access is
611 // a miss. We really should add first-class support for this
613 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
614 memReq
->completionEvent
= &cacheCompletionEvent
;
615 setStatus(IcacheMissStall
);
621 // If we've got a valid instruction (i.e., no fault on instruction
622 // fetch), then execute it.
623 if (fault
== No_Fault
) {
625 // keep an instruction count
628 // check for instruction-count-based events
629 comInsnEventQueue
[0]->serviceEvents(numInst
);
631 // decode the instruction
632 StaticInstPtr
<TheISA
> si(inst
);
634 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
638 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
639 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
640 #endif // FULL_SYSTEM
644 fault
= si
->execute(this, xc
, traceData
);
646 if (si
->isMemRef()) {
652 comLoadEventQueue
[0]->serviceEvents(numLoad
);
656 traceData
->finalize();
658 } // if (fault == No_Fault)
660 if (fault
!= No_Fault
) {
663 #else // !FULL_SYSTEM
664 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
665 #endif // FULL_SYSTEM
668 // go to the next instruction
669 xc
->regs
.pc
= xc
->regs
.npc
;
670 xc
->regs
.npc
+= sizeof(MachInst
);
677 system
->pcEventQueue
.service(xc
);
678 } while (oldpc
!= xc
->regs
.pc
);
681 assert(status() == Running
||
683 status() == DcacheMissStall
);
685 if (status() == Running
&& !tickEvent
.scheduled())
686 tickEvent
.schedule(curTick
+ 1);
690 ////////////////////////////////////////////////////////////////////////
692 // SimpleCPU Simulation Object
694 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
696 Param
<Counter
> max_insts_any_thread
;
697 Param
<Counter
> max_insts_all_threads
;
698 Param
<Counter
> max_loads_any_thread
;
699 Param
<Counter
> max_loads_all_threads
;
702 SimObjectParam
<AlphaItb
*> itb
;
703 SimObjectParam
<AlphaDtb
*> dtb
;
704 SimObjectParam
<FunctionalMemory
*> mem
;
705 SimObjectParam
<System
*> system
;
708 SimObjectParam
<Process
*> workload
;
709 #endif // FULL_SYSTEM
711 SimObjectParam
<BaseMem
*> icache
;
712 SimObjectParam
<BaseMem
*> dcache
;
714 Param
<bool> defer_registration
;
716 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
718 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
720 INIT_PARAM_DFLT(max_insts_any_thread
,
721 "terminate when any thread reaches this insn count",
723 INIT_PARAM_DFLT(max_insts_all_threads
,
724 "terminate when all threads have reached this insn count",
726 INIT_PARAM_DFLT(max_loads_any_thread
,
727 "terminate when any thread reaches this load count",
729 INIT_PARAM_DFLT(max_loads_all_threads
,
730 "terminate when all threads have reached this load count",
734 INIT_PARAM(itb
, "Instruction TLB"),
735 INIT_PARAM(dtb
, "Data TLB"),
736 INIT_PARAM(mem
, "memory"),
737 INIT_PARAM(system
, "system object"),
738 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
740 INIT_PARAM(workload
, "processes to run"),
741 #endif // FULL_SYSTEM
743 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
744 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
745 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
746 "(for sampling)", false)
748 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
751 CREATE_SIM_OBJECT(SimpleCPU
)
756 panic("processor clock multiplier must be 1\n");
758 cpu
= new SimpleCPU(getInstanceName(), system
,
759 max_insts_any_thread
, max_insts_all_threads
,
760 max_loads_any_thread
, max_loads_all_threads
,
762 (icache
) ? icache
->getInterface() : NULL
,
763 (dcache
) ? dcache
->getInterface() : NULL
,
764 ticksPerSecond
* mult
);
767 cpu
= new SimpleCPU(getInstanceName(), workload
,
768 max_insts_any_thread
, max_insts_all_threads
,
769 max_loads_any_thread
, max_loads_all_threads
,
770 (icache
) ? icache
->getInterface() : NULL
,
771 (dcache
) ? dcache
->getInterface() : NULL
);
773 #endif // FULL_SYSTEM
775 if (!defer_registration
) {
776 cpu
->registerExecContexts();
782 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)