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
,
123 bool _def_reg
, Tick freq
)
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
,
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
), defer_registration(_def_reg
),
142 cacheCompletionEvent(this)
146 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
);
148 // initialize CPU, including PC
149 TheISA::initCPU(&xc
->regs
);
151 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
152 #endif // !FULL_SYSTEM
154 icacheInterface
= icache_interface
;
155 dcacheInterface
= dcache_interface
;
157 memReq
= new MemReq();
160 memReq
->data
= new uint8_t[64];
169 execContexts
.push_back(xc
);
172 SimpleCPU::~SimpleCPU()
176 void SimpleCPU::init()
178 if (!defer_registration
) {
179 this->registerExecContexts();
184 SimpleCPU::switchOut()
186 _status
= SwitchedOut
;
187 if (tickEvent
.scheduled())
193 SimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
195 BaseCPU::takeOverFrom(oldCPU
);
197 assert(!tickEvent
.scheduled());
199 // if any of this CPU's ExecContexts are active, mark the CPU as
200 // running and schedule its tick event.
201 for (int i
= 0; i
< execContexts
.size(); ++i
) {
202 ExecContext
*xc
= execContexts
[i
];
203 if (xc
->status() == ExecContext::Active
&& _status
!= Running
) {
205 tickEvent
.schedule(curTick
);
214 SimpleCPU::activateContext(int thread_num
, int delay
)
216 assert(thread_num
== 0);
219 assert(_status
== Idle
);
221 scheduleTickEvent(delay
);
227 SimpleCPU::suspendContext(int thread_num
)
229 assert(thread_num
== 0);
232 assert(_status
== Running
);
234 unscheduleTickEvent();
240 SimpleCPU::deallocateContext(int thread_num
)
242 // for now, these are equivalent
243 suspendContext(thread_num
);
248 SimpleCPU::haltContext(int thread_num
)
250 // for now, these are equivalent
251 suspendContext(thread_num
);
256 SimpleCPU::regStats()
258 using namespace Statistics
;
263 .name(name() + ".num_insts")
264 .desc("Number of instructions executed")
268 .name(name() + ".num_refs")
269 .desc("Number of memory references")
273 .name(name() + ".idle_fraction")
274 .desc("Percentage of idle cycles")
278 .name(name() + ".icache_stall_cycles")
279 .desc("ICache total stall cycles")
280 .prereq(icacheStallCycles
)
284 .name(name() + ".dcache_stall_cycles")
285 .desc("DCache total stall cycles")
286 .prereq(dcacheStallCycles
)
289 idleFraction
= constant(1.0) - notIdleFraction
;
290 numInsts
= Statistics::scalar(numInst
) - Statistics::scalar(startNumInst
);
291 simInsts
+= numInsts
;
295 SimpleCPU::resetStats()
297 startNumInst
= numInst
;
298 notIdleFraction
= (_status
!= Idle
);
302 SimpleCPU::serialize(ostream
&os
)
304 SERIALIZE_ENUM(_status
);
305 SERIALIZE_SCALAR(inst
);
306 nameOut(os
, csprintf("%s.xc", name()));
308 nameOut(os
, csprintf("%s.tickEvent", name()));
309 tickEvent
.serialize(os
);
310 nameOut(os
, csprintf("%s.cacheCompletionEvent", name()));
311 cacheCompletionEvent
.serialize(os
);
315 SimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
317 UNSERIALIZE_ENUM(_status
);
318 UNSERIALIZE_SCALAR(inst
);
319 xc
->unserialize(cp
, csprintf("%s.xc", section
));
320 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
322 .unserialize(cp
, csprintf("%s.cacheCompletionEvent", section
));
326 change_thread_state(int thread_number
, int activate
, int priority
)
330 // precise architected memory state accessor macros
333 SimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
335 memReq
->reset(addr
, sizeof(T
), flags
);
337 // translate to physical address
338 Fault fault
= xc
->translateDataReadReq(memReq
);
340 // do functional access
341 if (fault
== No_Fault
)
342 fault
= xc
->read(memReq
, data
);
345 traceData
->setAddr(addr
);
346 if (fault
== No_Fault
)
347 traceData
->setData(data
);
350 // if we have a cache, do cache access too
351 if (fault
== No_Fault
&& dcacheInterface
) {
353 memReq
->completionEvent
= NULL
;
354 memReq
->time
= curTick
;
355 MemAccessResult result
= dcacheInterface
->access(memReq
);
357 // Ugly hack to get an event scheduled *only* if the access is
358 // a miss. We really should add first-class support for this
360 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
361 memReq
->completionEvent
= &cacheCompletionEvent
;
362 lastDcacheStall
= curTick
;
363 unscheduleTickEvent();
364 _status
= DcacheMissStall
;
371 #ifndef DOXYGEN_SHOULD_SKIP_THIS
375 SimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
379 SimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
383 SimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
387 SimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
389 #endif //DOXYGEN_SHOULD_SKIP_THIS
393 SimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
395 return read(addr
, *(uint64_t*)&data
, flags
);
400 SimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
402 return read(addr
, *(uint32_t*)&data
, flags
);
408 SimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
410 return read(addr
, (uint32_t&)data
, flags
);
416 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
419 traceData
->setAddr(addr
);
420 traceData
->setData(data
);
423 memReq
->reset(addr
, sizeof(T
), flags
);
425 // translate to physical address
426 Fault fault
= xc
->translateDataWriteReq(memReq
);
428 // do functional access
429 if (fault
== No_Fault
)
430 fault
= xc
->write(memReq
, data
);
432 if (fault
== No_Fault
&& dcacheInterface
) {
434 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
435 memReq
->completionEvent
= NULL
;
436 memReq
->time
= curTick
;
437 MemAccessResult result
= dcacheInterface
->access(memReq
);
439 // Ugly hack to get an event scheduled *only* if the access is
440 // a miss. We really should add first-class support for this
442 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
443 memReq
->completionEvent
= &cacheCompletionEvent
;
444 lastDcacheStall
= curTick
;
445 unscheduleTickEvent();
446 _status
= DcacheMissStall
;
450 if (res
&& (fault
== No_Fault
))
451 *res
= memReq
->result
;
457 #ifndef DOXYGEN_SHOULD_SKIP_THIS
460 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
464 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
468 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
472 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
474 #endif //DOXYGEN_SHOULD_SKIP_THIS
478 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
480 return write(*(uint64_t*)&data
, addr
, flags
, res
);
485 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
487 return write(*(uint32_t*)&data
, addr
, flags
, res
);
493 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
495 return write((uint32_t)data
, addr
, flags
, res
);
501 SimpleCPU::dbg_vtophys(Addr addr
)
503 return vtophys(xc
, addr
);
505 #endif // FULL_SYSTEM
511 SimpleCPU::processCacheCompletion()
514 case IcacheMissStall
:
515 icacheStallCycles
+= curTick
- lastIcacheStall
;
516 _status
= IcacheMissComplete
;
517 scheduleTickEvent(1);
519 case DcacheMissStall
:
520 dcacheStallCycles
+= curTick
- lastDcacheStall
;
522 scheduleTickEvent(1);
525 // If this CPU has been switched out due to sampling/warm-up,
526 // ignore any further status changes (e.g., due to cache
527 // misses outstanding at the time of the switch).
530 panic("SimpleCPU::processCacheCompletion: bad state");
537 SimpleCPU::post_interrupt(int int_num
, int index
)
539 BaseCPU::post_interrupt(int_num
, index
);
541 if (xc
->status() == ExecContext::Suspended
) {
542 DPRINTF(IPI
,"Suspended Processor awoke\n");
544 Annotate::Resume(xc
);
547 #endif // FULL_SYSTEM
549 /* start simulation, program loaded, processor precise state initialized */
555 Fault fault
= No_Fault
;
558 if (AlphaISA::check_interrupts
&&
559 xc
->cpu
->check_interrupts() &&
560 !PC_PAL(xc
->regs
.pc
) &&
561 status() != IcacheMissComplete
) {
564 AlphaISA::check_interrupts
= 0;
565 IntReg
*ipr
= xc
->regs
.ipr
;
567 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
568 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
569 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
570 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
571 // See table 4-19 of 21164 hardware reference
572 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
573 summary
|= (ULL(1) << i
);
578 uint64_t interrupts
= xc
->cpu
->intr_status();
579 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
580 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
581 if (interrupts
& (ULL(1) << i
)) {
582 // See table 4-19 of 21164 hardware reference
584 summary
|= (ULL(1) << i
);
588 if (ipr
[TheISA::IPR_ASTRR
])
589 panic("asynchronous traps not implemented\n");
591 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
592 ipr
[TheISA::IPR_ISR
] = summary
;
593 ipr
[TheISA::IPR_INTID
] = ipl
;
594 xc
->ev5_trap(Interrupt_Fault
);
596 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
597 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
602 // maintain $r0 semantics
603 xc
->regs
.intRegFile
[ZeroReg
] = 0;
605 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
606 #endif // TARGET_ALPHA
608 if (status() == IcacheMissComplete
) {
609 // We've already fetched an instruction and were stalled on an
610 // I-cache miss. No need to fetch it again.
612 // Set status to running; tick event will get rescheduled if
613 // necessary at end of tick() function.
617 // Try to fetch an instruction
619 // set up memory request for instruction fetch
621 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
623 #define IFETCH_FLAGS(pc) 0
627 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
628 IFETCH_FLAGS(xc
->regs
.pc
));
630 fault
= xc
->translateInstReq(memReq
);
632 if (fault
== No_Fault
)
633 fault
= xc
->mem
->read(memReq
, inst
);
635 if (icacheInterface
&& fault
== No_Fault
) {
636 memReq
->completionEvent
= NULL
;
638 memReq
->time
= curTick
;
639 MemAccessResult result
= icacheInterface
->access(memReq
);
641 // Ugly hack to get an event scheduled *only* if the access is
642 // a miss. We really should add first-class support for this
644 if (result
!= MA_HIT
&& icacheInterface
->doEvents()) {
645 memReq
->completionEvent
= &cacheCompletionEvent
;
646 lastIcacheStall
= curTick
;
647 unscheduleTickEvent();
648 _status
= IcacheMissStall
;
654 // If we've got a valid instruction (i.e., no fault on instruction
655 // fetch), then execute it.
656 if (fault
== No_Fault
) {
658 // keep an instruction count
661 // check for instruction-count-based events
662 comInstEventQueue
[0]->serviceEvents(numInst
);
664 // decode the instruction
665 StaticInstPtr
<TheISA
> si(inst
);
667 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
671 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
672 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
673 #endif // FULL_SYSTEM
677 fault
= si
->execute(this, xc
, traceData
);
680 SWContext
*ctx
= xc
->swCtx
;
682 ctx
->process(xc
, si
.get());
685 if (si
->isMemRef()) {
691 comLoadEventQueue
[0]->serviceEvents(numLoad
);
695 traceData
->finalize();
697 } // if (fault == No_Fault)
699 if (fault
!= No_Fault
) {
702 #else // !FULL_SYSTEM
703 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
704 #endif // FULL_SYSTEM
707 // go to the next instruction
708 xc
->regs
.pc
= xc
->regs
.npc
;
709 xc
->regs
.npc
+= sizeof(MachInst
);
716 system
->pcEventQueue
.service(xc
);
717 } while (oldpc
!= xc
->regs
.pc
);
720 assert(status() == Running
||
722 status() == DcacheMissStall
);
724 if (status() == Running
&& !tickEvent
.scheduled())
725 tickEvent
.schedule(curTick
+ 1);
729 ////////////////////////////////////////////////////////////////////////
731 // SimpleCPU Simulation Object
733 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
735 Param
<Counter
> max_insts_any_thread
;
736 Param
<Counter
> max_insts_all_threads
;
737 Param
<Counter
> max_loads_any_thread
;
738 Param
<Counter
> max_loads_all_threads
;
741 SimObjectParam
<AlphaItb
*> itb
;
742 SimObjectParam
<AlphaDtb
*> dtb
;
743 SimObjectParam
<FunctionalMemory
*> mem
;
744 SimObjectParam
<System
*> system
;
747 SimObjectParam
<Process
*> workload
;
748 #endif // FULL_SYSTEM
750 SimObjectParam
<BaseMem
*> icache
;
751 SimObjectParam
<BaseMem
*> dcache
;
753 Param
<bool> defer_registration
;
755 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
757 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
759 INIT_PARAM_DFLT(max_insts_any_thread
,
760 "terminate when any thread reaches this inst count",
762 INIT_PARAM_DFLT(max_insts_all_threads
,
763 "terminate when all threads have reached this inst count",
765 INIT_PARAM_DFLT(max_loads_any_thread
,
766 "terminate when any thread reaches this load count",
768 INIT_PARAM_DFLT(max_loads_all_threads
,
769 "terminate when all threads have reached this load count",
773 INIT_PARAM(itb
, "Instruction TLB"),
774 INIT_PARAM(dtb
, "Data TLB"),
775 INIT_PARAM(mem
, "memory"),
776 INIT_PARAM(system
, "system object"),
777 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
779 INIT_PARAM(workload
, "processes to run"),
780 #endif // FULL_SYSTEM
782 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
783 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
784 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
785 "(for sampling)", false)
787 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
790 CREATE_SIM_OBJECT(SimpleCPU
)
795 panic("processor clock multiplier must be 1\n");
797 cpu
= new SimpleCPU(getInstanceName(), system
,
798 max_insts_any_thread
, max_insts_all_threads
,
799 max_loads_any_thread
, max_loads_all_threads
,
801 (icache
) ? icache
->getInterface() : NULL
,
802 (dcache
) ? dcache
->getInterface() : NULL
,
804 ticksPerSecond
* mult
);
807 cpu
= new SimpleCPU(getInstanceName(), workload
,
808 max_insts_any_thread
, max_insts_all_threads
,
809 max_loads_any_thread
, max_loads_all_threads
,
810 (icache
) ? icache
->getInterface() : NULL
,
811 (dcache
) ? dcache
->getInterface() : NULL
,
814 #endif // FULL_SYSTEM
816 if (!defer_registration
) {
817 cpu
->registerExecContexts();
823 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)