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::TickEvent::TickEvent(SimpleCPU
*c
)
79 : Event(&mainEventQueue
, 100), cpu(c
)
84 SimpleCPU::TickEvent::process()
90 SimpleCPU::TickEvent::description()
92 return "SimpleCPU tick event";
96 SimpleCPU::CacheCompletionEvent::CacheCompletionEvent(SimpleCPU
*_cpu
)
97 : Event(&mainEventQueue
),
102 void SimpleCPU::CacheCompletionEvent::process()
104 cpu
->processCacheCompletion();
108 SimpleCPU::CacheCompletionEvent::description()
110 return "SimpleCPU cache completion event";
114 SimpleCPU::SimpleCPU(const string
&_name
,
116 Counter max_insts_any_thread
,
117 Counter max_insts_all_threads
,
118 Counter max_loads_any_thread
,
119 Counter max_loads_all_threads
,
120 AlphaItb
*itb
, AlphaDtb
*dtb
,
121 FunctionalMemory
*mem
,
122 MemInterface
*icache_interface
,
123 MemInterface
*dcache_interface
,
125 : BaseCPU(_name
, /* number_of_threads */ 1,
126 max_insts_any_thread
, max_insts_all_threads
,
127 max_loads_any_thread
, max_loads_all_threads
,
130 SimpleCPU::SimpleCPU(const string
&_name
, Process
*_process
,
131 Counter max_insts_any_thread
,
132 Counter max_insts_all_threads
,
133 Counter max_loads_any_thread
,
134 Counter max_loads_all_threads
,
135 MemInterface
*icache_interface
,
136 MemInterface
*dcache_interface
)
137 : BaseCPU(_name
, /* number_of_threads */ 1,
138 max_insts_any_thread
, max_insts_all_threads
,
139 max_loads_any_thread
, max_loads_all_threads
),
141 tickEvent(this), xc(NULL
), cacheCompletionEvent(this)
145 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
);
147 // initialize CPU, including PC
148 TheISA::initCPU(&xc
->regs
);
150 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
151 #endif // !FULL_SYSTEM
153 icacheInterface
= icache_interface
;
154 dcacheInterface
= dcache_interface
;
156 memReq
= new MemReq();
159 memReq
->data
= new uint8_t[64];
166 execContexts
.push_back(xc
);
169 SimpleCPU::~SimpleCPU()
174 SimpleCPU::switchOut()
176 _status
= SwitchedOut
;
177 if (tickEvent
.scheduled())
183 SimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
185 BaseCPU::takeOverFrom(oldCPU
);
187 assert(!tickEvent
.scheduled());
189 // if any of this CPU's ExecContexts are active, mark the CPU as
190 // running and schedule its tick event.
191 for (int i
= 0; i
< execContexts
.size(); ++i
) {
192 ExecContext
*xc
= execContexts
[i
];
193 if (xc
->status() == ExecContext::Active
&& _status
!= Running
) {
195 tickEvent
.schedule(curTick
);
204 SimpleCPU::execCtxStatusChg(int thread_num
) {
205 assert(thread_num
== 0);
208 if (xc
->status() == ExecContext::Active
)
216 SimpleCPU::regStats()
221 .name(name() + ".num_insts")
222 .desc("Number of instructions executed")
226 .name(name() + ".num_refs")
227 .desc("Number of memory references")
231 .name(name() + ".idle_fraction")
232 .desc("Percentage of idle cycles")
236 .name(name() + ".icache_stall_cycles")
237 .desc("ICache total stall cycles")
238 .prereq(icacheStallCycles
)
242 .name(name() + ".dcache_stall_cycles")
243 .desc("DCache total stall cycles")
244 .prereq(dcacheStallCycles
)
247 numInsts
= Statistics::scalar(numInst
);
248 simInsts
+= numInsts
;
252 SimpleCPU::serialize(ostream
&os
)
254 SERIALIZE_ENUM(_status
);
255 SERIALIZE_SCALAR(inst
);
256 nameOut(os
, csprintf("%s.xc", name()));
258 nameOut(os
, csprintf("%s.tickEvent", name()));
259 tickEvent
.serialize(os
);
260 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
261 cacheCompletionEvent
.serialize(os
);
265 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
267 UNSERIALIZE_ENUM(_status
);
268 UNSERIALIZE_SCALAR(inst
);
269 xc
->unserialize(cp
, csprintf("%s.xc", section
));
270 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
272 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
276 change_thread_state(int thread_number
, int activate
, int priority
)
280 // precise architected memory state accessor macros
283 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
285 memReq
->reset(addr
, sizeof(T
), flags
);
287 // translate to physical address
288 Fault fault
= xc
->translateDataReadReq(memReq
);
290 // do functional access
291 if (fault
== No_Fault
)
292 fault
= xc
->read(memReq
, data
);
295 traceData
->setAddr(addr
);
296 if (fault
== No_Fault
)
297 traceData
->setData(data
);
300 // if we have a cache, do cache access too
301 if (fault
== No_Fault
&& dcacheInterface
) {
303 memReq
->completionEvent
= NULL
;
304 memReq
->time
= curTick
;
305 memReq
->flags
&= ~UNCACHEABLE
;
306 MemAccessResult result
= dcacheInterface
->access(memReq
);
308 // Ugly hack to get an event scheduled *only* if the access is
309 // a miss. We really should add first-class support for this
311 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
312 memReq
->completionEvent
= &cacheCompletionEvent
;
313 setStatus(DcacheMissStall
);
320 #ifndef DOXYGEN_SHOULD_SKIP_THIS
324 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
328 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
332 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
336 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
338 #endif //DOXYGEN_SHOULD_SKIP_THIS
342 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
344 return read(addr
, *(uint64_t*)&data
, flags
);
349 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
351 return read(addr
, *(uint32_t*)&data
, flags
);
357 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
359 return read(addr
, (uint32_t&)data
, flags
);
365 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
368 traceData
->setAddr(addr
);
369 traceData
->setData(data
);
372 memReq
->reset(addr
, sizeof(T
), flags
);
374 // translate to physical address
375 Fault fault
= xc
->translateDataWriteReq(memReq
);
377 // do functional access
378 if (fault
== No_Fault
)
379 fault
= xc
->write(memReq
, data
);
381 if (fault
== No_Fault
&& dcacheInterface
) {
383 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
384 memReq
->completionEvent
= NULL
;
385 memReq
->time
= curTick
;
386 memReq
->flags
&= ~UNCACHEABLE
;
387 MemAccessResult result
= dcacheInterface
->access(memReq
);
389 // Ugly hack to get an event scheduled *only* if the access is
390 // a miss. We really should add first-class support for this
392 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
393 memReq
->completionEvent
= &cacheCompletionEvent
;
394 setStatus(DcacheMissStall
);
398 if (res
&& (fault
== No_Fault
))
399 *res
= memReq
->result
;
405 #ifndef DOXYGEN_SHOULD_SKIP_THIS
408 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
412 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
416 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
420 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
422 #endif //DOXYGEN_SHOULD_SKIP_THIS
426 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
428 return write(*(uint64_t*)&data
, addr
, flags
, res
);
433 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
435 return write(*(uint32_t*)&data
, addr
, flags
, res
);
441 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
443 return write((uint32_t)data
, addr
, flags
, res
);
449 SimpleCPU::dbg_vtophys(Addr addr
)
451 return vtophys(xc
, addr
);
453 #endif // FULL_SYSTEM
459 SimpleCPU::processCacheCompletion()
462 case IcacheMissStall
:
463 icacheStallCycles
+= curTick
- lastIcacheStall
;
464 setStatus(IcacheMissComplete
);
466 case DcacheMissStall
:
467 dcacheStallCycles
+= curTick
- lastDcacheStall
;
471 // If this CPU has been switched out due to sampling/warm-up,
472 // ignore any further status changes (e.g., due to cache
473 // misses outstanding at the time of the switch).
476 panic("SimpleCPU::processCacheCompletion: bad state");
483 SimpleCPU::post_interrupt(int int_num
, int index
)
485 BaseCPU::post_interrupt(int_num
, index
);
487 if (xc
->status() == ExecContext::Suspended
) {
488 DPRINTF(IPI
,"Suspended Processor awoke\n");
489 xc
->setStatus(ExecContext::Active
);
490 Annotate::Resume(xc
);
493 #endif // FULL_SYSTEM
495 /* start simulation, program loaded, processor precise state initialized */
501 Fault fault
= No_Fault
;
504 if (AlphaISA::check_interrupts
&&
505 xc
->cpu
->check_interrupts() &&
506 !PC_PAL(xc
->regs
.pc
) &&
507 status() != IcacheMissComplete
) {
510 AlphaISA::check_interrupts
= 0;
511 IntReg
*ipr
= xc
->regs
.ipr
;
513 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
514 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
515 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
516 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
517 // See table 4-19 of 21164 hardware reference
518 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
519 summary
|= (ULL(1) << i
);
524 uint64_t interrupts
= xc
->cpu
->intr_status();
525 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
526 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
527 if (interrupts
& (ULL(1) << i
)) {
528 // See table 4-19 of 21164 hardware reference
530 summary
|= (ULL(1) << i
);
534 if (ipr
[TheISA::IPR_ASTRR
])
535 panic("asynchronous traps not implemented\n");
537 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
538 ipr
[TheISA::IPR_ISR
] = summary
;
539 ipr
[TheISA::IPR_INTID
] = ipl
;
540 xc
->ev5_trap(Interrupt_Fault
);
542 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
543 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
548 // maintain $r0 semantics
549 xc
->regs
.intRegFile
[ZeroReg
] = 0;
551 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
552 #endif // TARGET_ALPHA
554 if (status() == IcacheMissComplete
) {
555 // We've already fetched an instruction and were stalled on an
556 // I-cache miss. No need to fetch it again.
561 // Try to fetch an instruction
563 // set up memory request for instruction fetch
565 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
567 #define IFETCH_FLAGS(pc) 0
571 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
572 IFETCH_FLAGS(xc
->regs
.pc
));
574 fault
= xc
->translateInstReq(memReq
);
576 if (fault
== No_Fault
)
577 fault
= xc
->mem
->read(memReq
, inst
);
579 if (icacheInterface
&& fault
== No_Fault
) {
580 memReq
->completionEvent
= NULL
;
582 memReq
->time
= curTick
;
583 memReq
->flags
&= ~UNCACHEABLE
;
584 MemAccessResult result
= icacheInterface
->access(memReq
);
586 // Ugly hack to get an event scheduled *only* if the access is
587 // a miss. We really should add first-class support for this
589 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
590 memReq
->completionEvent
= &cacheCompletionEvent
;
591 setStatus(IcacheMissStall
);
597 // If we've got a valid instruction (i.e., no fault on instruction
598 // fetch), then execute it.
599 if (fault
== No_Fault
) {
601 // keep an instruction count
604 // check for instruction-count-based events
605 comInsnEventQueue
[0]->serviceEvents(numInst
);
607 // decode the instruction
608 StaticInstPtr
<TheISA
> si(inst
);
610 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
614 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
615 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
616 #endif // FULL_SYSTEM
620 fault
= si
->execute(this, xc
, traceData
);
622 if (!(xc
->misspeculating()) && (xc
->system
->bin
)) {
623 SWContext
*ctx
= xc
->swCtx
;
624 if (ctx
&& !ctx
->callStack
.empty()) {
628 if (si
->isReturn()) {
629 if (ctx
->calls
== 0) {
630 fnCall
*top
= ctx
->callStack
.top();
631 DPRINTF(TCPIP
, "Removing %s from callstack.\n", top
->name
);
633 ctx
->callStack
.pop();
634 if (ctx
->callStack
.empty())
635 xc
->system
->nonPath
->activate();
637 ctx
->callStack
.top()->myBin
->activate();
639 xc
->system
->dumpState(xc
);
647 if (si
->isMemRef()) {
653 comLoadEventQueue
[0]->serviceEvents(numLoad
);
657 traceData
->finalize();
659 } // if (fault == No_Fault)
661 if (fault
!= No_Fault
) {
664 #else // !FULL_SYSTEM
665 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
666 #endif // FULL_SYSTEM
669 // go to the next instruction
670 xc
->regs
.pc
= xc
->regs
.npc
;
671 xc
->regs
.npc
+= sizeof(MachInst
);
678 system
->pcEventQueue
.service(xc
);
679 } while (oldpc
!= xc
->regs
.pc
);
682 assert(status() == Running
||
684 status() == DcacheMissStall
);
686 if (status() == Running
&& !tickEvent
.scheduled())
687 tickEvent
.schedule(curTick
+ 1);
691 ////////////////////////////////////////////////////////////////////////
693 // SimpleCPU Simulation Object
695 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
697 Param
<Counter
> max_insts_any_thread
;
698 Param
<Counter
> max_insts_all_threads
;
699 Param
<Counter
> max_loads_any_thread
;
700 Param
<Counter
> max_loads_all_threads
;
703 SimObjectParam
<AlphaItb
*> itb
;
704 SimObjectParam
<AlphaDtb
*> dtb
;
705 SimObjectParam
<FunctionalMemory
*> mem
;
706 SimObjectParam
<System
*> system
;
709 SimObjectParam
<Process
*> workload
;
710 #endif // FULL_SYSTEM
712 SimObjectParam
<BaseMem
*> icache
;
713 SimObjectParam
<BaseMem
*> dcache
;
715 Param
<bool> defer_registration
;
717 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
719 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
721 INIT_PARAM_DFLT(max_insts_any_thread
,
722 "terminate when any thread reaches this insn count",
724 INIT_PARAM_DFLT(max_insts_all_threads
,
725 "terminate when all threads have reached this insn count",
727 INIT_PARAM_DFLT(max_loads_any_thread
,
728 "terminate when any thread reaches this load count",
730 INIT_PARAM_DFLT(max_loads_all_threads
,
731 "terminate when all threads have reached this load count",
735 INIT_PARAM(itb
, "Instruction TLB"),
736 INIT_PARAM(dtb
, "Data TLB"),
737 INIT_PARAM(mem
, "memory"),
738 INIT_PARAM(system
, "system object"),
739 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
741 INIT_PARAM(workload
, "processes to run"),
742 #endif // FULL_SYSTEM
744 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
745 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
746 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
747 "(for sampling)", false)
749 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
752 CREATE_SIM_OBJECT(SimpleCPU
)
757 panic("processor clock multiplier must be 1\n");
759 cpu
= new SimpleCPU(getInstanceName(), system
,
760 max_insts_any_thread
, max_insts_all_threads
,
761 max_loads_any_thread
, max_loads_all_threads
,
763 (icache
) ? icache
->getInterface() : NULL
,
764 (dcache
) ? dcache
->getInterface() : NULL
,
765 ticksPerSecond
* mult
);
768 cpu
= new SimpleCPU(getInstanceName(), workload
,
769 max_insts_any_thread
, max_insts_all_threads
,
770 max_loads_any_thread
, max_loads_all_threads
,
771 (icache
) ? icache
->getInterface() : NULL
,
772 (dcache
) ? dcache
->getInterface() : NULL
);
774 #endif // FULL_SYSTEM
776 if (!defer_registration
) {
777 cpu
->registerExecContexts();
783 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)