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
, CPU_Tick_Pri
), 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 tickEvent
.schedule(curTick
);
205 SimpleCPU::activateContext(int thread_num
, int delay
)
207 assert(thread_num
== 0);
210 assert(_status
== Idle
);
212 scheduleTickEvent(delay
);
218 SimpleCPU::suspendContext(int thread_num
)
220 assert(thread_num
== 0);
223 assert(_status
== Running
);
225 unscheduleTickEvent();
231 SimpleCPU::deallocateContext(int thread_num
)
233 // for now, these are equivalent
234 suspendContext(thread_num
);
239 SimpleCPU::haltContext(int thread_num
)
241 // for now, these are equivalent
242 suspendContext(thread_num
);
247 SimpleCPU::regStats()
249 using namespace Statistics
;
254 .name(name() + ".num_insts")
255 .desc("Number of instructions executed")
259 .name(name() + ".num_refs")
260 .desc("Number of memory references")
264 .name(name() + ".idle_fraction")
265 .desc("Percentage of idle cycles")
269 .name(name() + ".icache_stall_cycles")
270 .desc("ICache total stall cycles")
271 .prereq(icacheStallCycles
)
275 .name(name() + ".dcache_stall_cycles")
276 .desc("DCache total stall cycles")
277 .prereq(dcacheStallCycles
)
280 idleFraction
= constant(1.0) - notIdleFraction
;
281 numInsts
= Statistics::scalar(numInst
) - Statistics::scalar(startNumInst
);
282 simInsts
+= numInsts
;
286 SimpleCPU::resetStats()
288 startNumInst
= numInst
;
289 notIdleFraction
= (_status
!= Idle
);
293 SimpleCPU::serialize(ostream
&os
)
295 SERIALIZE_ENUM(_status
);
296 SERIALIZE_SCALAR(inst
);
297 nameOut(os
, csprintf("%s.xc", name()));
299 nameOut(os
, csprintf("%s.tickEvent", name()));
300 tickEvent
.serialize(os
);
301 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
302 cacheCompletionEvent
.serialize(os
);
306 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
308 UNSERIALIZE_ENUM(_status
);
309 UNSERIALIZE_SCALAR(inst
);
310 xc
->unserialize(cp
, csprintf("%s.xc", section
));
311 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
313 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
317 change_thread_state(int thread_number
, int activate
, int priority
)
321 // precise architected memory state accessor macros
324 SimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
326 memReq
->reset(addr
, sizeof(T
), flags
);
328 // translate to physical address
329 Fault fault
= xc
->translateDataReadReq(memReq
);
331 // do functional access
332 if (fault
== No_Fault
)
333 fault
= xc
->read(memReq
, data
);
336 traceData
->setAddr(addr
);
337 if (fault
== No_Fault
)
338 traceData
->setData(data
);
341 // if we have a cache, do cache access too
342 if (fault
== No_Fault
&& dcacheInterface
) {
344 memReq
->completionEvent
= NULL
;
345 memReq
->time
= curTick
;
346 memReq
->flags
&= ~UNCACHEABLE
;
347 MemAccessResult result
= dcacheInterface
->access(memReq
);
349 // Ugly hack to get an event scheduled *only* if the access is
350 // a miss. We really should add first-class support for this
352 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
353 memReq
->completionEvent
= &cacheCompletionEvent
;
354 lastDcacheStall
= curTick
;
355 unscheduleTickEvent();
356 _status
= DcacheMissStall
;
363 #ifndef DOXYGEN_SHOULD_SKIP_THIS
367 SimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
371 SimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
375 SimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
379 SimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
381 #endif //DOXYGEN_SHOULD_SKIP_THIS
385 SimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
387 return read(addr
, *(uint64_t*)&data
, flags
);
392 SimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
394 return read(addr
, *(uint32_t*)&data
, flags
);
400 SimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
402 return read(addr
, (uint32_t&)data
, flags
);
408 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
411 traceData
->setAddr(addr
);
412 traceData
->setData(data
);
415 memReq
->reset(addr
, sizeof(T
), flags
);
417 // translate to physical address
418 Fault fault
= xc
->translateDataWriteReq(memReq
);
420 // do functional access
421 if (fault
== No_Fault
)
422 fault
= xc
->write(memReq
, data
);
424 if (fault
== No_Fault
&& dcacheInterface
) {
426 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
427 memReq
->completionEvent
= NULL
;
428 memReq
->time
= curTick
;
429 memReq
->flags
&= ~UNCACHEABLE
;
430 MemAccessResult result
= dcacheInterface
->access(memReq
);
432 // Ugly hack to get an event scheduled *only* if the access is
433 // a miss. We really should add first-class support for this
435 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
436 memReq
->completionEvent
= &cacheCompletionEvent
;
437 lastDcacheStall
= curTick
;
438 unscheduleTickEvent();
439 _status
= DcacheMissStall
;
443 if (res
&& (fault
== No_Fault
))
444 *res
= memReq
->result
;
450 #ifndef DOXYGEN_SHOULD_SKIP_THIS
453 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
457 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
461 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
465 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
467 #endif //DOXYGEN_SHOULD_SKIP_THIS
471 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
473 return write(*(uint64_t*)&data
, addr
, flags
, res
);
478 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
480 return write(*(uint32_t*)&data
, addr
, flags
, res
);
486 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
488 return write((uint32_t)data
, addr
, flags
, res
);
494 SimpleCPU::dbg_vtophys(Addr addr
)
496 return vtophys(xc
, addr
);
498 #endif // FULL_SYSTEM
504 SimpleCPU::processCacheCompletion()
507 case IcacheMissStall
:
508 icacheStallCycles
+= curTick
- lastIcacheStall
;
509 _status
= IcacheMissComplete
;
510 scheduleTickEvent(1);
512 case DcacheMissStall
:
513 dcacheStallCycles
+= curTick
- lastDcacheStall
;
515 scheduleTickEvent(1);
518 // If this CPU has been switched out due to sampling/warm-up,
519 // ignore any further status changes (e.g., due to cache
520 // misses outstanding at the time of the switch).
523 panic("SimpleCPU::processCacheCompletion: bad state");
530 SimpleCPU::post_interrupt(int int_num
, int index
)
532 BaseCPU::post_interrupt(int_num
, index
);
534 if (xc
->status() == ExecContext::Suspended
) {
535 DPRINTF(IPI
,"Suspended Processor awoke\n");
537 Annotate::Resume(xc
);
540 #endif // FULL_SYSTEM
542 /* start simulation, program loaded, processor precise state initialized */
548 Fault fault
= No_Fault
;
551 if (AlphaISA::check_interrupts
&&
552 xc
->cpu
->check_interrupts() &&
553 !PC_PAL(xc
->regs
.pc
) &&
554 status() != IcacheMissComplete
) {
557 AlphaISA::check_interrupts
= 0;
558 IntReg
*ipr
= xc
->regs
.ipr
;
560 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
561 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
562 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
563 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
564 // See table 4-19 of 21164 hardware reference
565 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
566 summary
|= (ULL(1) << i
);
571 uint64_t interrupts
= xc
->cpu
->intr_status();
572 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
573 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
574 if (interrupts
& (ULL(1) << i
)) {
575 // See table 4-19 of 21164 hardware reference
577 summary
|= (ULL(1) << i
);
581 if (ipr
[TheISA::IPR_ASTRR
])
582 panic("asynchronous traps not implemented\n");
584 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
585 ipr
[TheISA::IPR_ISR
] = summary
;
586 ipr
[TheISA::IPR_INTID
] = ipl
;
587 xc
->ev5_trap(Interrupt_Fault
);
589 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
590 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
595 // maintain $r0 semantics
596 xc
->regs
.intRegFile
[ZeroReg
] = 0;
598 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
599 #endif // TARGET_ALPHA
601 if (status() == IcacheMissComplete
) {
602 // We've already fetched an instruction and were stalled on an
603 // I-cache miss. No need to fetch it again.
605 // Set status to running; tick event will get rescheduled if
606 // necessary at end of tick() function.
610 // Try to fetch an instruction
612 // set up memory request for instruction fetch
614 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
616 #define IFETCH_FLAGS(pc) 0
620 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
621 IFETCH_FLAGS(xc
->regs
.pc
));
623 fault
= xc
->translateInstReq(memReq
);
625 if (fault
== No_Fault
)
626 fault
= xc
->mem
->read(memReq
, inst
);
628 if (icacheInterface
&& fault
== No_Fault
) {
629 memReq
->completionEvent
= NULL
;
631 memReq
->time
= curTick
;
632 memReq
->flags
&= ~UNCACHEABLE
;
633 MemAccessResult result
= icacheInterface
->access(memReq
);
635 // Ugly hack to get an event scheduled *only* if the access is
636 // a miss. We really should add first-class support for this
638 if (result
!= MA_HIT
&& icacheInterface
->doEvents()) {
639 memReq
->completionEvent
= &cacheCompletionEvent
;
640 lastIcacheStall
= curTick
;
641 unscheduleTickEvent();
642 _status
= IcacheMissStall
;
648 // If we've got a valid instruction (i.e., no fault on instruction
649 // fetch), then execute it.
650 if (fault
== No_Fault
) {
652 // keep an instruction count
655 // check for instruction-count-based events
656 comInstEventQueue
[0]->serviceEvents(numInst
);
658 // decode the instruction
659 StaticInstPtr
<TheISA
> si(inst
);
661 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
665 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
666 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
667 #endif // FULL_SYSTEM
671 fault
= si
->execute(this, xc
, traceData
);
673 if (!(xc
->misspeculating()) && (xc
->system
->bin
)) {
674 SWContext
*ctx
= xc
->swCtx
;
675 if (ctx
&& !ctx
->callStack
.empty()) {
679 if (si
->isReturn()) {
680 if (ctx
->calls
== 0) {
681 fnCall
*top
= ctx
->callStack
.top();
682 DPRINTF(TCPIP
, "Removing %s from callstack.\n", top
->name
);
684 ctx
->callStack
.pop();
685 if (ctx
->callStack
.empty())
686 xc
->system
->nonPath
->activate();
688 ctx
->callStack
.top()->myBin
->activate();
690 xc
->system
->dumpState(xc
);
698 if (si
->isMemRef()) {
704 comLoadEventQueue
[0]->serviceEvents(numLoad
);
708 traceData
->finalize();
710 } // if (fault == No_Fault)
712 if (fault
!= No_Fault
) {
715 #else // !FULL_SYSTEM
716 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
717 #endif // FULL_SYSTEM
720 // go to the next instruction
721 xc
->regs
.pc
= xc
->regs
.npc
;
722 xc
->regs
.npc
+= sizeof(MachInst
);
729 system
->pcEventQueue
.service(xc
);
730 } while (oldpc
!= xc
->regs
.pc
);
733 assert(status() == Running
||
735 status() == DcacheMissStall
);
737 if (status() == Running
&& !tickEvent
.scheduled())
738 tickEvent
.schedule(curTick
+ 1);
742 ////////////////////////////////////////////////////////////////////////
744 // SimpleCPU Simulation Object
746 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
748 Param
<Counter
> max_insts_any_thread
;
749 Param
<Counter
> max_insts_all_threads
;
750 Param
<Counter
> max_loads_any_thread
;
751 Param
<Counter
> max_loads_all_threads
;
754 SimObjectParam
<AlphaItb
*> itb
;
755 SimObjectParam
<AlphaDtb
*> dtb
;
756 SimObjectParam
<FunctionalMemory
*> mem
;
757 SimObjectParam
<System
*> system
;
760 SimObjectParam
<Process
*> workload
;
761 #endif // FULL_SYSTEM
763 SimObjectParam
<BaseMem
*> icache
;
764 SimObjectParam
<BaseMem
*> dcache
;
766 Param
<bool> defer_registration
;
768 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
770 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
772 INIT_PARAM_DFLT(max_insts_any_thread
,
773 "terminate when any thread reaches this inst count",
775 INIT_PARAM_DFLT(max_insts_all_threads
,
776 "terminate when all threads have reached this inst count",
778 INIT_PARAM_DFLT(max_loads_any_thread
,
779 "terminate when any thread reaches this load count",
781 INIT_PARAM_DFLT(max_loads_all_threads
,
782 "terminate when all threads have reached this load count",
786 INIT_PARAM(itb
, "Instruction TLB"),
787 INIT_PARAM(dtb
, "Data TLB"),
788 INIT_PARAM(mem
, "memory"),
789 INIT_PARAM(system
, "system object"),
790 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
792 INIT_PARAM(workload
, "processes to run"),
793 #endif // FULL_SYSTEM
795 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
796 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
797 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
798 "(for sampling)", false)
800 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
803 CREATE_SIM_OBJECT(SimpleCPU
)
808 panic("processor clock multiplier must be 1\n");
810 cpu
= new SimpleCPU(getInstanceName(), system
,
811 max_insts_any_thread
, max_insts_all_threads
,
812 max_loads_any_thread
, max_loads_all_threads
,
814 (icache
) ? icache
->getInterface() : NULL
,
815 (dcache
) ? dcache
->getInterface() : NULL
,
816 ticksPerSecond
* mult
);
819 cpu
= new SimpleCPU(getInstanceName(), workload
,
820 max_insts_any_thread
, max_insts_all_threads
,
821 max_loads_any_thread
, max_loads_all_threads
,
822 (icache
) ? icache
->getInterface() : NULL
,
823 (dcache
) ? dcache
->getInterface() : NULL
);
825 #endif // FULL_SYSTEM
827 if (!defer_registration
) {
828 cpu
->registerExecContexts();
834 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)