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 tickEvent
.schedule(curTick
);
205 SimpleCPU::execCtxStatusChg(int thread_num
) {
206 assert(thread_num
== 0);
209 if (xc
->status() == ExecContext::Active
)
217 SimpleCPU::regStats()
219 using namespace Statistics
;
224 .name(name() + ".num_insts")
225 .desc("Number of instructions executed")
229 .name(name() + ".num_refs")
230 .desc("Number of memory references")
234 .name(name() + ".idle_fraction")
235 .desc("Percentage of idle cycles")
239 .name(name() + ".icache_stall_cycles")
240 .desc("ICache total stall cycles")
241 .prereq(icacheStallCycles
)
245 .name(name() + ".dcache_stall_cycles")
246 .desc("DCache total stall cycles")
247 .prereq(dcacheStallCycles
)
250 numInsts
= Statistics::scalar(numInst
) - Statistics::scalar(startNumInst
);
251 simInsts
+= numInsts
;
255 SimpleCPU::resetStats()
257 startNumInst
= numInst
;
261 SimpleCPU::serialize(ostream
&os
)
263 SERIALIZE_ENUM(_status
);
264 SERIALIZE_SCALAR(inst
);
265 nameOut(os
, csprintf("%s.xc", name()));
267 nameOut(os
, csprintf("%s.tickEvent", name()));
268 tickEvent
.serialize(os
);
269 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
270 cacheCompletionEvent
.serialize(os
);
274 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
276 UNSERIALIZE_ENUM(_status
);
277 UNSERIALIZE_SCALAR(inst
);
278 xc
->unserialize(cp
, csprintf("%s.xc", section
));
279 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
281 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
285 change_thread_state(int thread_number
, int activate
, int priority
)
289 // precise architected memory state accessor macros
292 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
294 memReq
->reset(addr
, sizeof(T
), flags
);
296 // translate to physical address
297 Fault fault
= xc
->translateDataReadReq(memReq
);
299 // do functional access
300 if (fault
== No_Fault
)
301 fault
= xc
->read(memReq
, data
);
304 traceData
->setAddr(addr
);
305 if (fault
== No_Fault
)
306 traceData
->setData(data
);
309 // if we have a cache, do cache access too
310 if (fault
== No_Fault
&& dcacheInterface
) {
312 memReq
->completionEvent
= NULL
;
313 memReq
->time
= curTick
;
314 memReq
->flags
&= ~UNCACHEABLE
;
315 MemAccessResult result
= dcacheInterface
->access(memReq
);
317 // Ugly hack to get an event scheduled *only* if the access is
318 // a miss. We really should add first-class support for this
320 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
321 memReq
->completionEvent
= &cacheCompletionEvent
;
322 setStatus(DcacheMissStall
);
329 #ifndef DOXYGEN_SHOULD_SKIP_THIS
333 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
337 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
341 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
345 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
347 #endif //DOXYGEN_SHOULD_SKIP_THIS
351 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
353 return read(addr
, *(uint64_t*)&data
, flags
);
358 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
360 return read(addr
, *(uint32_t*)&data
, flags
);
366 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
368 return read(addr
, (uint32_t&)data
, flags
);
374 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
377 traceData
->setAddr(addr
);
378 traceData
->setData(data
);
381 memReq
->reset(addr
, sizeof(T
), flags
);
383 // translate to physical address
384 Fault fault
= xc
->translateDataWriteReq(memReq
);
386 // do functional access
387 if (fault
== No_Fault
)
388 fault
= xc
->write(memReq
, data
);
390 if (fault
== No_Fault
&& dcacheInterface
) {
392 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
393 memReq
->completionEvent
= NULL
;
394 memReq
->time
= curTick
;
395 memReq
->flags
&= ~UNCACHEABLE
;
396 MemAccessResult result
= dcacheInterface
->access(memReq
);
398 // Ugly hack to get an event scheduled *only* if the access is
399 // a miss. We really should add first-class support for this
401 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
402 memReq
->completionEvent
= &cacheCompletionEvent
;
403 setStatus(DcacheMissStall
);
407 if (res
&& (fault
== No_Fault
))
408 *res
= memReq
->result
;
414 #ifndef DOXYGEN_SHOULD_SKIP_THIS
417 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
421 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
425 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
429 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
431 #endif //DOXYGEN_SHOULD_SKIP_THIS
435 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
437 return write(*(uint64_t*)&data
, addr
, flags
, res
);
442 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
444 return write(*(uint32_t*)&data
, addr
, flags
, res
);
450 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
452 return write((uint32_t)data
, addr
, flags
, res
);
458 SimpleCPU::dbg_vtophys(Addr addr
)
460 return vtophys(xc
, addr
);
462 #endif // FULL_SYSTEM
468 SimpleCPU::processCacheCompletion()
471 case IcacheMissStall
:
472 icacheStallCycles
+= curTick
- lastIcacheStall
;
473 setStatus(IcacheMissComplete
);
475 case DcacheMissStall
:
476 dcacheStallCycles
+= curTick
- lastDcacheStall
;
480 // If this CPU has been switched out due to sampling/warm-up,
481 // ignore any further status changes (e.g., due to cache
482 // misses outstanding at the time of the switch).
485 panic("SimpleCPU::processCacheCompletion: bad state");
492 SimpleCPU::post_interrupt(int int_num
, int index
)
494 BaseCPU::post_interrupt(int_num
, index
);
496 if (xc
->status() == ExecContext::Suspended
) {
497 DPRINTF(IPI
,"Suspended Processor awoke\n");
498 xc
->setStatus(ExecContext::Active
);
499 Annotate::Resume(xc
);
502 #endif // FULL_SYSTEM
504 /* start simulation, program loaded, processor precise state initialized */
510 Fault fault
= No_Fault
;
513 if (AlphaISA::check_interrupts
&&
514 xc
->cpu
->check_interrupts() &&
515 !PC_PAL(xc
->regs
.pc
) &&
516 status() != IcacheMissComplete
) {
519 AlphaISA::check_interrupts
= 0;
520 IntReg
*ipr
= xc
->regs
.ipr
;
522 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
523 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
524 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
525 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
526 // See table 4-19 of 21164 hardware reference
527 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
528 summary
|= (ULL(1) << i
);
533 uint64_t interrupts
= xc
->cpu
->intr_status();
534 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
535 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
536 if (interrupts
& (ULL(1) << i
)) {
537 // See table 4-19 of 21164 hardware reference
539 summary
|= (ULL(1) << i
);
543 if (ipr
[TheISA::IPR_ASTRR
])
544 panic("asynchronous traps not implemented\n");
546 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
547 ipr
[TheISA::IPR_ISR
] = summary
;
548 ipr
[TheISA::IPR_INTID
] = ipl
;
549 xc
->ev5_trap(Interrupt_Fault
);
551 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
552 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
557 // maintain $r0 semantics
558 xc
->regs
.intRegFile
[ZeroReg
] = 0;
560 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
561 #endif // TARGET_ALPHA
563 if (status() == IcacheMissComplete
) {
564 // We've already fetched an instruction and were stalled on an
565 // I-cache miss. No need to fetch it again.
570 // Try to fetch an instruction
572 // set up memory request for instruction fetch
574 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
576 #define IFETCH_FLAGS(pc) 0
580 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
581 IFETCH_FLAGS(xc
->regs
.pc
));
583 fault
= xc
->translateInstReq(memReq
);
585 if (fault
== No_Fault
)
586 fault
= xc
->mem
->read(memReq
, inst
);
588 if (icacheInterface
&& fault
== No_Fault
) {
589 memReq
->completionEvent
= NULL
;
591 memReq
->time
= curTick
;
592 memReq
->flags
&= ~UNCACHEABLE
;
593 MemAccessResult result
= icacheInterface
->access(memReq
);
595 // Ugly hack to get an event scheduled *only* if the access is
596 // a miss. We really should add first-class support for this
598 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
599 memReq
->completionEvent
= &cacheCompletionEvent
;
600 setStatus(IcacheMissStall
);
606 // If we've got a valid instruction (i.e., no fault on instruction
607 // fetch), then execute it.
608 if (fault
== No_Fault
) {
610 // keep an instruction count
613 // check for instruction-count-based events
614 comInsnEventQueue
[0]->serviceEvents(numInst
);
616 // decode the instruction
617 StaticInstPtr
<TheISA
> si(inst
);
619 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
623 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
624 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
625 #endif // FULL_SYSTEM
629 fault
= si
->execute(this, xc
, traceData
);
631 if (!(xc
->misspeculating()) && (xc
->system
->bin
)) {
632 SWContext
*ctx
= xc
->swCtx
;
633 if (ctx
&& !ctx
->callStack
.empty()) {
637 if (si
->isReturn()) {
638 if (ctx
->calls
== 0) {
639 fnCall
*top
= ctx
->callStack
.top();
640 DPRINTF(TCPIP
, "Removing %s from callstack.\n", top
->name
);
642 ctx
->callStack
.pop();
643 if (ctx
->callStack
.empty())
644 xc
->system
->nonPath
->activate();
646 ctx
->callStack
.top()->myBin
->activate();
648 xc
->system
->dumpState(xc
);
656 if (si
->isMemRef()) {
662 comLoadEventQueue
[0]->serviceEvents(numLoad
);
666 traceData
->finalize();
668 } // if (fault == No_Fault)
670 if (fault
!= No_Fault
) {
673 #else // !FULL_SYSTEM
674 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
675 #endif // FULL_SYSTEM
678 // go to the next instruction
679 xc
->regs
.pc
= xc
->regs
.npc
;
680 xc
->regs
.npc
+= sizeof(MachInst
);
687 system
->pcEventQueue
.service(xc
);
688 } while (oldpc
!= xc
->regs
.pc
);
691 assert(status() == Running
||
693 status() == DcacheMissStall
);
695 if (status() == Running
&& !tickEvent
.scheduled())
696 tickEvent
.schedule(curTick
+ 1);
700 ////////////////////////////////////////////////////////////////////////
702 // SimpleCPU Simulation Object
704 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
706 Param
<Counter
> max_insts_any_thread
;
707 Param
<Counter
> max_insts_all_threads
;
708 Param
<Counter
> max_loads_any_thread
;
709 Param
<Counter
> max_loads_all_threads
;
712 SimObjectParam
<AlphaItb
*> itb
;
713 SimObjectParam
<AlphaDtb
*> dtb
;
714 SimObjectParam
<FunctionalMemory
*> mem
;
715 SimObjectParam
<System
*> system
;
718 SimObjectParam
<Process
*> workload
;
719 #endif // FULL_SYSTEM
721 SimObjectParam
<BaseMem
*> icache
;
722 SimObjectParam
<BaseMem
*> dcache
;
724 Param
<bool> defer_registration
;
726 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
728 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
730 INIT_PARAM_DFLT(max_insts_any_thread
,
731 "terminate when any thread reaches this insn count",
733 INIT_PARAM_DFLT(max_insts_all_threads
,
734 "terminate when all threads have reached this insn count",
736 INIT_PARAM_DFLT(max_loads_any_thread
,
737 "terminate when any thread reaches this load count",
739 INIT_PARAM_DFLT(max_loads_all_threads
,
740 "terminate when all threads have reached this load count",
744 INIT_PARAM(itb
, "Instruction TLB"),
745 INIT_PARAM(dtb
, "Data TLB"),
746 INIT_PARAM(mem
, "memory"),
747 INIT_PARAM(system
, "system object"),
748 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
750 INIT_PARAM(workload
, "processes to run"),
751 #endif // FULL_SYSTEM
753 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
754 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
755 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
756 "(for sampling)", false)
758 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
761 CREATE_SIM_OBJECT(SimpleCPU
)
766 panic("processor clock multiplier must be 1\n");
768 cpu
= new SimpleCPU(getInstanceName(), system
,
769 max_insts_any_thread
, max_insts_all_threads
,
770 max_loads_any_thread
, max_loads_all_threads
,
772 (icache
) ? icache
->getInterface() : NULL
,
773 (dcache
) ? dcache
->getInterface() : NULL
,
774 ticksPerSecond
* mult
);
777 cpu
= new SimpleCPU(getInstanceName(), workload
,
778 max_insts_any_thread
, max_insts_all_threads
,
779 max_loads_any_thread
, max_loads_all_threads
,
780 (icache
) ? icache
->getInterface() : NULL
,
781 (dcache
) ? dcache
->getInterface() : NULL
);
783 #endif // FULL_SYSTEM
785 if (!defer_registration
) {
786 cpu
->registerExecContexts();
792 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)