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
)
221 SimpleCPU::regStats()
223 using namespace Statistics
;
228 .name(name() + ".num_insts")
229 .desc("Number of instructions executed")
233 .name(name() + ".num_refs")
234 .desc("Number of memory references")
238 .name(name() + ".idle_fraction")
239 .desc("Percentage of idle cycles")
243 .name(name() + ".icache_stall_cycles")
244 .desc("ICache total stall cycles")
245 .prereq(icacheStallCycles
)
249 .name(name() + ".dcache_stall_cycles")
250 .desc("DCache total stall cycles")
251 .prereq(dcacheStallCycles
)
254 numInsts
= Statistics::scalar(numInst
) - Statistics::scalar(startNumInst
);
255 simInsts
+= numInsts
;
259 SimpleCPU::resetStats()
261 startNumInst
= numInst
;
265 SimpleCPU::serialize(ostream
&os
)
267 SERIALIZE_ENUM(_status
);
268 SERIALIZE_SCALAR(inst
);
269 nameOut(os
, csprintf("%s.xc", name()));
271 nameOut(os
, csprintf("%s.tickEvent", name()));
272 tickEvent
.serialize(os
);
273 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
274 cacheCompletionEvent
.serialize(os
);
278 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
280 UNSERIALIZE_ENUM(_status
);
281 UNSERIALIZE_SCALAR(inst
);
282 xc
->unserialize(cp
, csprintf("%s.xc", section
));
283 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
285 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
289 change_thread_state(int thread_number
, int activate
, int priority
)
293 // precise architected memory state accessor macros
296 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
298 memReq
->reset(addr
, sizeof(T
), flags
);
300 // translate to physical address
301 Fault fault
= xc
->translateDataReadReq(memReq
);
303 // do functional access
304 if (fault
== No_Fault
)
305 fault
= xc
->read(memReq
, data
);
308 traceData
->setAddr(addr
);
309 if (fault
== No_Fault
)
310 traceData
->setData(data
);
313 // if we have a cache, do cache access too
314 if (fault
== No_Fault
&& dcacheInterface
) {
316 memReq
->completionEvent
= NULL
;
317 memReq
->time
= curTick
;
318 memReq
->flags
&= ~UNCACHEABLE
;
319 MemAccessResult result
= dcacheInterface
->access(memReq
);
321 // Ugly hack to get an event scheduled *only* if the access is
322 // a miss. We really should add first-class support for this
324 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
325 memReq
->completionEvent
= &cacheCompletionEvent
;
326 setStatus(DcacheMissStall
);
333 #ifndef DOXYGEN_SHOULD_SKIP_THIS
337 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
341 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
345 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
349 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
351 #endif //DOXYGEN_SHOULD_SKIP_THIS
355 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
357 return read(addr
, *(uint64_t*)&data
, flags
);
362 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
364 return read(addr
, *(uint32_t*)&data
, flags
);
370 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
372 return read(addr
, (uint32_t&)data
, flags
);
378 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
381 traceData
->setAddr(addr
);
382 traceData
->setData(data
);
385 memReq
->reset(addr
, sizeof(T
), flags
);
387 // translate to physical address
388 Fault fault
= xc
->translateDataWriteReq(memReq
);
390 // do functional access
391 if (fault
== No_Fault
)
392 fault
= xc
->write(memReq
, data
);
394 if (fault
== No_Fault
&& dcacheInterface
) {
396 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
397 memReq
->completionEvent
= NULL
;
398 memReq
->time
= curTick
;
399 memReq
->flags
&= ~UNCACHEABLE
;
400 MemAccessResult result
= dcacheInterface
->access(memReq
);
402 // Ugly hack to get an event scheduled *only* if the access is
403 // a miss. We really should add first-class support for this
405 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
406 memReq
->completionEvent
= &cacheCompletionEvent
;
407 setStatus(DcacheMissStall
);
411 if (res
&& (fault
== No_Fault
))
412 *res
= memReq
->result
;
418 #ifndef DOXYGEN_SHOULD_SKIP_THIS
421 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
425 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
429 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
433 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
435 #endif //DOXYGEN_SHOULD_SKIP_THIS
439 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
441 return write(*(uint64_t*)&data
, addr
, flags
, res
);
446 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
448 return write(*(uint32_t*)&data
, addr
, flags
, res
);
454 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
456 return write((uint32_t)data
, addr
, flags
, res
);
462 SimpleCPU::dbg_vtophys(Addr addr
)
464 return vtophys(xc
, addr
);
466 #endif // FULL_SYSTEM
472 SimpleCPU::processCacheCompletion()
475 case IcacheMissStall
:
476 icacheStallCycles
+= curTick
- lastIcacheStall
;
477 setStatus(IcacheMissComplete
);
479 case DcacheMissStall
:
480 dcacheStallCycles
+= curTick
- lastDcacheStall
;
484 // If this CPU has been switched out due to sampling/warm-up,
485 // ignore any further status changes (e.g., due to cache
486 // misses outstanding at the time of the switch).
489 panic("SimpleCPU::processCacheCompletion: bad state");
496 SimpleCPU::post_interrupt(int int_num
, int index
)
498 BaseCPU::post_interrupt(int_num
, index
);
500 if (xc
->status() == ExecContext::Suspended
) {
501 DPRINTF(IPI
,"Suspended Processor awoke\n");
502 xc
->setStatus(ExecContext::Active
);
503 Annotate::Resume(xc
);
506 #endif // FULL_SYSTEM
508 /* start simulation, program loaded, processor precise state initialized */
514 Fault fault
= No_Fault
;
517 if (AlphaISA::check_interrupts
&&
518 xc
->cpu
->check_interrupts() &&
519 !PC_PAL(xc
->regs
.pc
) &&
520 status() != IcacheMissComplete
) {
523 AlphaISA::check_interrupts
= 0;
524 IntReg
*ipr
= xc
->regs
.ipr
;
526 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
527 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
528 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
529 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
530 // See table 4-19 of 21164 hardware reference
531 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
532 summary
|= (ULL(1) << i
);
537 uint64_t interrupts
= xc
->cpu
->intr_status();
538 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
539 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
540 if (interrupts
& (ULL(1) << i
)) {
541 // See table 4-19 of 21164 hardware reference
543 summary
|= (ULL(1) << i
);
547 if (ipr
[TheISA::IPR_ASTRR
])
548 panic("asynchronous traps not implemented\n");
550 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
551 ipr
[TheISA::IPR_ISR
] = summary
;
552 ipr
[TheISA::IPR_INTID
] = ipl
;
553 xc
->ev5_trap(Interrupt_Fault
);
555 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
556 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
561 // maintain $r0 semantics
562 xc
->regs
.intRegFile
[ZeroReg
] = 0;
564 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
565 #endif // TARGET_ALPHA
567 if (status() == IcacheMissComplete
) {
568 // We've already fetched an instruction and were stalled on an
569 // I-cache miss. No need to fetch it again.
574 // Try to fetch an instruction
576 // set up memory request for instruction fetch
578 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
580 #define IFETCH_FLAGS(pc) 0
584 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
585 IFETCH_FLAGS(xc
->regs
.pc
));
587 fault
= xc
->translateInstReq(memReq
);
589 if (fault
== No_Fault
)
590 fault
= xc
->mem
->read(memReq
, inst
);
592 if (icacheInterface
&& fault
== No_Fault
) {
593 memReq
->completionEvent
= NULL
;
595 memReq
->time
= curTick
;
596 memReq
->flags
&= ~UNCACHEABLE
;
597 MemAccessResult result
= icacheInterface
->access(memReq
);
599 // Ugly hack to get an event scheduled *only* if the access is
600 // a miss. We really should add first-class support for this
602 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
603 memReq
->completionEvent
= &cacheCompletionEvent
;
604 setStatus(IcacheMissStall
);
610 // If we've got a valid instruction (i.e., no fault on instruction
611 // fetch), then execute it.
612 if (fault
== No_Fault
) {
614 // keep an instruction count
617 // check for instruction-count-based events
618 comInsnEventQueue
[0]->serviceEvents(numInst
);
620 // decode the instruction
621 StaticInstPtr
<TheISA
> si(inst
);
623 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
627 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
628 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
629 #endif // FULL_SYSTEM
633 fault
= si
->execute(this, xc
, traceData
);
635 if (!(xc
->misspeculating()) && (xc
->system
->bin
)) {
636 SWContext
*ctx
= xc
->swCtx
;
637 if (ctx
&& !ctx
->callStack
.empty()) {
641 if (si
->isReturn()) {
642 if (ctx
->calls
== 0) {
643 fnCall
*top
= ctx
->callStack
.top();
644 DPRINTF(TCPIP
, "Removing %s from callstack.\n", top
->name
);
646 ctx
->callStack
.pop();
647 if (ctx
->callStack
.empty())
648 xc
->system
->nonPath
->activate();
650 ctx
->callStack
.top()->myBin
->activate();
652 xc
->system
->dumpState(xc
);
660 if (si
->isMemRef()) {
666 comLoadEventQueue
[0]->serviceEvents(numLoad
);
670 traceData
->finalize();
672 } // if (fault == No_Fault)
674 if (fault
!= No_Fault
) {
677 #else // !FULL_SYSTEM
678 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
679 #endif // FULL_SYSTEM
682 // go to the next instruction
683 xc
->regs
.pc
= xc
->regs
.npc
;
684 xc
->regs
.npc
+= sizeof(MachInst
);
691 system
->pcEventQueue
.service(xc
);
692 } while (oldpc
!= xc
->regs
.pc
);
695 assert(status() == Running
||
697 status() == DcacheMissStall
);
699 if (status() == Running
&& !tickEvent
.scheduled())
700 tickEvent
.schedule(curTick
+ 1);
704 ////////////////////////////////////////////////////////////////////////
706 // SimpleCPU Simulation Object
708 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
710 Param
<Counter
> max_insts_any_thread
;
711 Param
<Counter
> max_insts_all_threads
;
712 Param
<Counter
> max_loads_any_thread
;
713 Param
<Counter
> max_loads_all_threads
;
716 SimObjectParam
<AlphaItb
*> itb
;
717 SimObjectParam
<AlphaDtb
*> dtb
;
718 SimObjectParam
<FunctionalMemory
*> mem
;
719 SimObjectParam
<System
*> system
;
722 SimObjectParam
<Process
*> workload
;
723 #endif // FULL_SYSTEM
725 SimObjectParam
<BaseMem
*> icache
;
726 SimObjectParam
<BaseMem
*> dcache
;
728 Param
<bool> defer_registration
;
730 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
732 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
734 INIT_PARAM_DFLT(max_insts_any_thread
,
735 "terminate when any thread reaches this insn count",
737 INIT_PARAM_DFLT(max_insts_all_threads
,
738 "terminate when all threads have reached this insn count",
740 INIT_PARAM_DFLT(max_loads_any_thread
,
741 "terminate when any thread reaches this load count",
743 INIT_PARAM_DFLT(max_loads_all_threads
,
744 "terminate when all threads have reached this load count",
748 INIT_PARAM(itb
, "Instruction TLB"),
749 INIT_PARAM(dtb
, "Data TLB"),
750 INIT_PARAM(mem
, "memory"),
751 INIT_PARAM(system
, "system object"),
752 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
754 INIT_PARAM(workload
, "processes to run"),
755 #endif // FULL_SYSTEM
757 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
758 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
759 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
760 "(for sampling)", false)
762 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
765 CREATE_SIM_OBJECT(SimpleCPU
)
770 panic("processor clock multiplier must be 1\n");
772 cpu
= new SimpleCPU(getInstanceName(), system
,
773 max_insts_any_thread
, max_insts_all_threads
,
774 max_loads_any_thread
, max_loads_all_threads
,
776 (icache
) ? icache
->getInterface() : NULL
,
777 (dcache
) ? dcache
->getInterface() : NULL
,
778 ticksPerSecond
* mult
);
781 cpu
= new SimpleCPU(getInstanceName(), workload
,
782 max_insts_any_thread
, max_insts_all_threads
,
783 max_loads_any_thread
, max_loads_all_threads
,
784 (icache
) ? icache
->getInterface() : NULL
,
785 (dcache
) ? dcache
->getInterface() : NULL
);
787 #endif // FULL_SYSTEM
789 if (!defer_registration
) {
790 cpu
->registerExecContexts();
796 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)