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"
77 SimpleCPU::TickEvent::TickEvent(SimpleCPU
*c
)
78 : Event(&mainEventQueue
, 100), cpu(c
)
83 SimpleCPU::TickEvent::process()
89 SimpleCPU::TickEvent::description()
91 return "SimpleCPU tick event";
95 SimpleCPU::CacheCompletionEvent::CacheCompletionEvent(SimpleCPU
*_cpu
)
96 : Event(&mainEventQueue
),
101 void SimpleCPU::CacheCompletionEvent::process()
103 cpu
->processCacheCompletion();
107 SimpleCPU::CacheCompletionEvent::description()
109 return "SimpleCPU cache completion event";
113 SimpleCPU::SimpleCPU(const string
&_name
,
115 Counter max_insts_any_thread
,
116 Counter max_insts_all_threads
,
117 Counter max_loads_any_thread
,
118 Counter max_loads_all_threads
,
119 AlphaItb
*itb
, AlphaDtb
*dtb
,
120 FunctionalMemory
*mem
,
121 MemInterface
*icache_interface
,
122 MemInterface
*dcache_interface
,
124 : BaseCPU(_name
, /* number_of_threads */ 1,
125 max_insts_any_thread
, max_insts_all_threads
,
126 max_loads_any_thread
, max_loads_all_threads
,
129 SimpleCPU::SimpleCPU(const string
&_name
, Process
*_process
,
130 Counter max_insts_any_thread
,
131 Counter max_insts_all_threads
,
132 Counter max_loads_any_thread
,
133 Counter max_loads_all_threads
,
134 MemInterface
*icache_interface
,
135 MemInterface
*dcache_interface
)
136 : BaseCPU(_name
, /* number_of_threads */ 1,
137 max_insts_any_thread
, max_insts_all_threads
,
138 max_loads_any_thread
, max_loads_all_threads
),
140 tickEvent(this), xc(NULL
), cacheCompletionEvent(this)
144 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
);
146 // initialize CPU, including PC
147 TheISA::initCPU(&xc
->regs
);
149 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
150 #endif // !FULL_SYSTEM
152 icacheInterface
= icache_interface
;
153 dcacheInterface
= dcache_interface
;
155 memReq
= new MemReq();
158 memReq
->data
= new uint8_t[64];
167 execContexts
.push_back(xc
);
170 SimpleCPU::~SimpleCPU()
175 SimpleCPU::switchOut()
177 _status
= SwitchedOut
;
178 if (tickEvent
.scheduled())
184 SimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
186 BaseCPU::takeOverFrom(oldCPU
);
188 assert(!tickEvent
.scheduled());
190 // if any of this CPU's ExecContexts are active, mark the CPU as
191 // running and schedule its tick event.
192 for (int i
= 0; i
< execContexts
.size(); ++i
) {
193 ExecContext
*xc
= execContexts
[i
];
194 if (xc
->status() == ExecContext::Active
&& _status
!= Running
) {
196 // the CpuSwitchEvent has a low priority, so it's
197 // scheduled *after* the current cycle's tick event. Thus
198 // the first tick event for the new context should take
199 // place on the *next* cycle.
200 tickEvent
.schedule(curTick
+1);
209 SimpleCPU::execCtxStatusChg(int thread_num
) {
210 assert(thread_num
== 0);
213 if (xc
->status() == ExecContext::Active
)
220 SimpleCPU::setStatus(Status new_status
)
222 Status old_status
= status();
224 // We should never even get here if the CPU has been switched out.
225 assert(old_status
!= SwitchedOut
);
227 _status
= new_status
;
230 case IcacheMissStall
:
231 assert(old_status
== Running
);
232 lastIcacheStall
= curTick
;
233 if (tickEvent
.scheduled())
237 case IcacheMissComplete
:
238 assert(old_status
== IcacheMissStall
);
239 if (tickEvent
.squashed())
240 tickEvent
.reschedule(curTick
+ 1);
241 else if (!tickEvent
.scheduled())
242 tickEvent
.schedule(curTick
+ 1);
245 case DcacheMissStall
:
246 assert(old_status
== Running
);
247 lastDcacheStall
= curTick
;
248 if (tickEvent
.scheduled())
253 assert(old_status
== Running
);
255 if (tickEvent
.scheduled())
260 assert(old_status
== Idle
||
261 old_status
== DcacheMissStall
||
262 old_status
== IcacheMissComplete
);
263 if (old_status
== Idle
)
266 if (tickEvent
.squashed())
267 tickEvent
.reschedule(curTick
+ 1);
268 else if (!tickEvent
.scheduled())
269 tickEvent
.schedule(curTick
+ 1);
273 panic("can't get here");
278 SimpleCPU::regStats()
280 using namespace Statistics
;
285 .name(name() + ".num_insts")
286 .desc("Number of instructions executed")
290 .name(name() + ".num_refs")
291 .desc("Number of memory references")
295 .name(name() + ".idle_fraction")
296 .desc("Percentage of idle cycles")
300 .name(name() + ".icache_stall_cycles")
301 .desc("ICache total stall cycles")
302 .prereq(icacheStallCycles
)
306 .name(name() + ".dcache_stall_cycles")
307 .desc("DCache total stall cycles")
308 .prereq(dcacheStallCycles
)
311 idleFraction
= constant(1.0) - notIdleFraction
;
312 numInsts
= Statistics::scalar(numInst
) - Statistics::scalar(startNumInst
);
313 simInsts
+= numInsts
;
317 SimpleCPU::resetStats()
319 startNumInst
= numInst
;
320 notIdleFraction
= (_status
!= Idle
);
324 SimpleCPU::serialize(ostream
&os
)
326 SERIALIZE_ENUM(_status
);
327 SERIALIZE_SCALAR(inst
);
328 nameOut(os
, csprintf("%s.xc", name()));
330 nameOut(os
, csprintf("%s.tickEvent", name()));
331 tickEvent
.serialize(os
);
332 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
333 cacheCompletionEvent
.serialize(os
);
337 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
339 UNSERIALIZE_ENUM(_status
);
340 UNSERIALIZE_SCALAR(inst
);
341 xc
->unserialize(cp
, csprintf("%s.xc", section
));
342 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
344 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
348 change_thread_state(int thread_number
, int activate
, int priority
)
352 // precise architected memory state accessor macros
355 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
357 memReq
->reset(addr
, sizeof(T
), flags
);
359 // translate to physical address
360 Fault fault
= xc
->translateDataReadReq(memReq
);
362 // do functional access
363 if (fault
== No_Fault
)
364 fault
= xc
->read(memReq
, data
);
367 traceData
->setAddr(addr
);
368 if (fault
== No_Fault
)
369 traceData
->setData(data
);
372 // if we have a cache, do cache access too
373 if (fault
== No_Fault
&& dcacheInterface
) {
375 memReq
->completionEvent
= NULL
;
376 memReq
->time
= curTick
;
377 memReq
->flags
&= ~UNCACHEABLE
;
378 MemAccessResult result
= dcacheInterface
->access(memReq
);
380 // Ugly hack to get an event scheduled *only* if the access is
381 // a miss. We really should add first-class support for this
383 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
384 memReq
->completionEvent
= &cacheCompletionEvent
;
385 setStatus(DcacheMissStall
);
392 #ifndef DOXYGEN_SHOULD_SKIP_THIS
396 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
400 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
404 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
408 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
410 #endif //DOXYGEN_SHOULD_SKIP_THIS
414 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
416 return read(addr
, *(uint64_t*)&data
, flags
);
421 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
423 return read(addr
, *(uint32_t*)&data
, flags
);
429 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
431 return read(addr
, (uint32_t&)data
, flags
);
437 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
440 traceData
->setAddr(addr
);
441 traceData
->setData(data
);
444 memReq
->reset(addr
, sizeof(T
), flags
);
446 // translate to physical address
447 Fault fault
= xc
->translateDataWriteReq(memReq
);
449 // do functional access
450 if (fault
== No_Fault
)
451 fault
= xc
->write(memReq
, data
);
453 if (fault
== No_Fault
&& dcacheInterface
) {
455 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
456 memReq
->completionEvent
= NULL
;
457 memReq
->time
= curTick
;
458 memReq
->flags
&= ~UNCACHEABLE
;
459 MemAccessResult result
= dcacheInterface
->access(memReq
);
461 // Ugly hack to get an event scheduled *only* if the access is
462 // a miss. We really should add first-class support for this
464 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
465 memReq
->completionEvent
= &cacheCompletionEvent
;
466 setStatus(DcacheMissStall
);
470 if (res
&& (fault
== No_Fault
))
471 *res
= memReq
->result
;
477 #ifndef DOXYGEN_SHOULD_SKIP_THIS
480 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
484 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
488 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
492 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
494 #endif //DOXYGEN_SHOULD_SKIP_THIS
498 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
500 return write(*(uint64_t*)&data
, addr
, flags
, res
);
505 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
507 return write(*(uint32_t*)&data
, addr
, flags
, res
);
513 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
515 return write((uint32_t)data
, addr
, flags
, res
);
521 SimpleCPU::dbg_vtophys(Addr addr
)
523 return vtophys(xc
, addr
);
525 #endif // FULL_SYSTEM
531 SimpleCPU::processCacheCompletion()
534 case IcacheMissStall
:
535 icacheStallCycles
+= curTick
- lastIcacheStall
;
536 setStatus(IcacheMissComplete
);
538 case DcacheMissStall
:
539 dcacheStallCycles
+= curTick
- lastDcacheStall
;
543 // If this CPU has been switched out due to sampling/warm-up,
544 // ignore any further status changes (e.g., due to cache
545 // misses outstanding at the time of the switch).
548 panic("SimpleCPU::processCacheCompletion: bad state");
555 SimpleCPU::post_interrupt(int int_num
, int index
)
557 BaseCPU::post_interrupt(int_num
, index
);
559 if (xc
->status() == ExecContext::Suspended
) {
560 DPRINTF(IPI
,"Suspended Processor awoke\n");
561 xc
->setStatus(ExecContext::Active
);
562 Annotate::Resume(xc
);
565 #endif // FULL_SYSTEM
567 /* start simulation, program loaded, processor precise state initialized */
573 Fault fault
= No_Fault
;
576 if (AlphaISA::check_interrupts
&&
577 xc
->cpu
->check_interrupts() &&
578 !PC_PAL(xc
->regs
.pc
) &&
579 status() != IcacheMissComplete
) {
582 AlphaISA::check_interrupts
= 0;
583 IntReg
*ipr
= xc
->regs
.ipr
;
585 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
586 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
587 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
588 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
589 // See table 4-19 of 21164 hardware reference
590 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
591 summary
|= (ULL(1) << i
);
596 uint64_t interrupts
= xc
->cpu
->intr_status();
597 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
598 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
599 if (interrupts
& (ULL(1) << i
)) {
600 // See table 4-19 of 21164 hardware reference
602 summary
|= (ULL(1) << i
);
606 if (ipr
[TheISA::IPR_ASTRR
])
607 panic("asynchronous traps not implemented\n");
609 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
610 ipr
[TheISA::IPR_ISR
] = summary
;
611 ipr
[TheISA::IPR_INTID
] = ipl
;
612 xc
->ev5_trap(Interrupt_Fault
);
614 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
615 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
620 // maintain $r0 semantics
621 xc
->regs
.intRegFile
[ZeroReg
] = 0;
623 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
624 #endif // TARGET_ALPHA
626 if (status() == IcacheMissComplete
) {
627 // We've already fetched an instruction and were stalled on an
628 // I-cache miss. No need to fetch it again.
633 // Try to fetch an instruction
635 // set up memory request for instruction fetch
637 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
639 #define IFETCH_FLAGS(pc) 0
643 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
644 IFETCH_FLAGS(xc
->regs
.pc
));
646 fault
= xc
->translateInstReq(memReq
);
648 if (fault
== No_Fault
)
649 fault
= xc
->mem
->read(memReq
, inst
);
651 if (icacheInterface
&& fault
== No_Fault
) {
652 memReq
->completionEvent
= NULL
;
654 memReq
->time
= curTick
;
655 memReq
->flags
&= ~UNCACHEABLE
;
656 MemAccessResult result
= icacheInterface
->access(memReq
);
658 // Ugly hack to get an event scheduled *only* if the access is
659 // a miss. We really should add first-class support for this
661 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
662 memReq
->completionEvent
= &cacheCompletionEvent
;
663 setStatus(IcacheMissStall
);
669 // If we've got a valid instruction (i.e., no fault on instruction
670 // fetch), then execute it.
671 if (fault
== No_Fault
) {
673 // keep an instruction count
676 // check for instruction-count-based events
677 comInsnEventQueue
[0]->serviceEvents(numInst
);
679 // decode the instruction
680 StaticInstPtr
<TheISA
> si(inst
);
682 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
686 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
687 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
688 #endif // FULL_SYSTEM
692 fault
= si
->execute(this, xc
, traceData
);
694 if (!(xc
->misspeculating()) && (xc
->system
->bin
)) {
695 SWContext
*ctx
= xc
->swCtx
;
696 if (ctx
&& !ctx
->callStack
.empty()) {
700 if (si
->isReturn()) {
701 if (ctx
->calls
== 0) {
702 fnCall
*top
= ctx
->callStack
.top();
703 DPRINTF(TCPIP
, "Removing %s from callstack.\n", top
->name
);
705 ctx
->callStack
.pop();
706 if (ctx
->callStack
.empty())
707 xc
->system
->nonPath
->activate();
709 ctx
->callStack
.top()->myBin
->activate();
711 xc
->system
->dumpState(xc
);
719 if (si
->isMemRef()) {
725 comLoadEventQueue
[0]->serviceEvents(numLoad
);
729 traceData
->finalize();
731 } // if (fault == No_Fault)
733 if (fault
!= No_Fault
) {
736 #else // !FULL_SYSTEM
737 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
738 #endif // FULL_SYSTEM
741 // go to the next instruction
742 xc
->regs
.pc
= xc
->regs
.npc
;
743 xc
->regs
.npc
+= sizeof(MachInst
);
750 system
->pcEventQueue
.service(xc
);
751 } while (oldpc
!= xc
->regs
.pc
);
754 assert(status() == Running
||
756 status() == DcacheMissStall
);
758 if (status() == Running
&& !tickEvent
.scheduled())
759 tickEvent
.schedule(curTick
+ 1);
763 ////////////////////////////////////////////////////////////////////////
765 // SimpleCPU Simulation Object
767 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
769 Param
<Counter
> max_insts_any_thread
;
770 Param
<Counter
> max_insts_all_threads
;
771 Param
<Counter
> max_loads_any_thread
;
772 Param
<Counter
> max_loads_all_threads
;
775 SimObjectParam
<AlphaItb
*> itb
;
776 SimObjectParam
<AlphaDtb
*> dtb
;
777 SimObjectParam
<FunctionalMemory
*> mem
;
778 SimObjectParam
<System
*> system
;
781 SimObjectParam
<Process
*> workload
;
782 #endif // FULL_SYSTEM
784 SimObjectParam
<BaseMem
*> icache
;
785 SimObjectParam
<BaseMem
*> dcache
;
787 Param
<bool> defer_registration
;
789 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
791 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
793 INIT_PARAM_DFLT(max_insts_any_thread
,
794 "terminate when any thread reaches this insn count",
796 INIT_PARAM_DFLT(max_insts_all_threads
,
797 "terminate when all threads have reached this insn count",
799 INIT_PARAM_DFLT(max_loads_any_thread
,
800 "terminate when any thread reaches this load count",
802 INIT_PARAM_DFLT(max_loads_all_threads
,
803 "terminate when all threads have reached this load count",
807 INIT_PARAM(itb
, "Instruction TLB"),
808 INIT_PARAM(dtb
, "Data TLB"),
809 INIT_PARAM(mem
, "memory"),
810 INIT_PARAM(system
, "system object"),
811 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
813 INIT_PARAM(workload
, "processes to run"),
814 #endif // FULL_SYSTEM
816 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
817 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
818 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
819 "(for sampling)", false)
821 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
824 CREATE_SIM_OBJECT(SimpleCPU
)
829 panic("processor clock multiplier must be 1\n");
831 cpu
= new SimpleCPU(getInstanceName(), system
,
832 max_insts_any_thread
, max_insts_all_threads
,
833 max_loads_any_thread
, max_loads_all_threads
,
835 (icache
) ? icache
->getInterface() : NULL
,
836 (dcache
) ? dcache
->getInterface() : NULL
,
837 ticksPerSecond
* mult
);
840 cpu
= new SimpleCPU(getInstanceName(), workload
,
841 max_insts_any_thread
, max_insts_all_threads
,
842 max_loads_any_thread
, max_loads_all_threads
,
843 (icache
) ? icache
->getInterface() : NULL
,
844 (dcache
) ? dcache
->getInterface() : NULL
);
846 #endif // FULL_SYSTEM
848 if (!defer_registration
) {
849 cpu
->registerExecContexts();
855 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)