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/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
)
331 SimpleCPU::copySrcTranslate(Addr src
)
333 memReq
->reset(src
, (dcacheInterface
) ?
334 dcacheInterface
->getBlockSize()
337 // translate to physical address
338 Fault fault
= xc
->translateDataReadReq(memReq
);
340 if (fault
== No_Fault
) {
341 xc
->copySrcAddr
= src
;
342 xc
->copySrcPhysAddr
= memReq
->paddr
;
345 xc
->copySrcPhysAddr
= 0;
351 SimpleCPU::copy(Addr dest
)
353 int blk_size
= (dcacheInterface
) ? dcacheInterface
->getBlockSize() : 64;
354 uint8_t data
[blk_size
];
355 assert(xc
->copySrcPhysAddr
);
356 memReq
->reset(dest
, blk_size
);
357 // translate to physical address
358 Fault fault
= xc
->translateDataWriteReq(memReq
);
359 if (fault
== No_Fault
) {
360 Addr dest_addr
= memReq
->paddr
;
361 // Need to read straight from memory since we have more than 8 bytes.
362 memReq
->paddr
= xc
->copySrcPhysAddr
;
363 xc
->mem
->read(memReq
, data
);
364 memReq
->paddr
= dest_addr
;
365 xc
->mem
->write(memReq
, data
);
370 // precise architected memory state accessor macros
373 SimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
375 memReq
->reset(addr
, sizeof(T
), flags
);
377 // translate to physical address
378 Fault fault
= xc
->translateDataReadReq(memReq
);
380 // do functional access
381 if (fault
== No_Fault
)
382 fault
= xc
->read(memReq
, data
);
385 traceData
->setAddr(addr
);
386 if (fault
== No_Fault
)
387 traceData
->setData(data
);
390 // if we have a cache, do cache access too
391 if (fault
== No_Fault
&& dcacheInterface
) {
393 memReq
->completionEvent
= NULL
;
394 memReq
->time
= curTick
;
395 MemAccessResult result
= dcacheInterface
->access(memReq
);
397 // Ugly hack to get an event scheduled *only* if the access is
398 // a miss. We really should add first-class support for this
400 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
401 memReq
->completionEvent
= &cacheCompletionEvent
;
402 lastDcacheStall
= curTick
;
403 unscheduleTickEvent();
404 _status
= DcacheMissStall
;
411 #ifndef DOXYGEN_SHOULD_SKIP_THIS
415 SimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
419 SimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
423 SimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
427 SimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
429 #endif //DOXYGEN_SHOULD_SKIP_THIS
433 SimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
435 return read(addr
, *(uint64_t*)&data
, flags
);
440 SimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
442 return read(addr
, *(uint32_t*)&data
, flags
);
448 SimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
450 return read(addr
, (uint32_t&)data
, flags
);
456 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
459 traceData
->setAddr(addr
);
460 traceData
->setData(data
);
463 memReq
->reset(addr
, sizeof(T
), flags
);
465 // translate to physical address
466 Fault fault
= xc
->translateDataWriteReq(memReq
);
468 // do functional access
469 if (fault
== No_Fault
)
470 fault
= xc
->write(memReq
, data
);
472 if (fault
== No_Fault
&& dcacheInterface
) {
474 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
475 memReq
->completionEvent
= NULL
;
476 memReq
->time
= curTick
;
477 MemAccessResult result
= dcacheInterface
->access(memReq
);
479 // Ugly hack to get an event scheduled *only* if the access is
480 // a miss. We really should add first-class support for this
482 if (result
!= MA_HIT
&& dcacheInterface
->doEvents()) {
483 memReq
->completionEvent
= &cacheCompletionEvent
;
484 lastDcacheStall
= curTick
;
485 unscheduleTickEvent();
486 _status
= DcacheMissStall
;
490 if (res
&& (fault
== No_Fault
))
491 *res
= memReq
->result
;
497 #ifndef DOXYGEN_SHOULD_SKIP_THIS
500 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
504 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
508 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
512 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
514 #endif //DOXYGEN_SHOULD_SKIP_THIS
518 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
520 return write(*(uint64_t*)&data
, addr
, flags
, res
);
525 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
527 return write(*(uint32_t*)&data
, addr
, flags
, res
);
533 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
535 return write((uint32_t)data
, addr
, flags
, res
);
541 SimpleCPU::dbg_vtophys(Addr addr
)
543 return vtophys(xc
, addr
);
545 #endif // FULL_SYSTEM
551 SimpleCPU::processCacheCompletion()
554 case IcacheMissStall
:
555 icacheStallCycles
+= curTick
- lastIcacheStall
;
556 _status
= IcacheMissComplete
;
557 scheduleTickEvent(1);
559 case DcacheMissStall
:
560 dcacheStallCycles
+= curTick
- lastDcacheStall
;
562 scheduleTickEvent(1);
565 // If this CPU has been switched out due to sampling/warm-up,
566 // ignore any further status changes (e.g., due to cache
567 // misses outstanding at the time of the switch).
570 panic("SimpleCPU::processCacheCompletion: bad state");
577 SimpleCPU::post_interrupt(int int_num
, int index
)
579 BaseCPU::post_interrupt(int_num
, index
);
581 if (xc
->status() == ExecContext::Suspended
) {
582 DPRINTF(IPI
,"Suspended Processor awoke\n");
584 Annotate::Resume(xc
);
587 #endif // FULL_SYSTEM
589 /* start simulation, program loaded, processor precise state initialized */
595 Fault fault
= No_Fault
;
598 if (AlphaISA::check_interrupts
&&
599 xc
->cpu
->check_interrupts() &&
600 !PC_PAL(xc
->regs
.pc
) &&
601 status() != IcacheMissComplete
) {
604 AlphaISA::check_interrupts
= 0;
605 IntReg
*ipr
= xc
->regs
.ipr
;
607 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
608 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
609 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
610 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
611 // See table 4-19 of 21164 hardware reference
612 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
613 summary
|= (ULL(1) << i
);
618 uint64_t interrupts
= xc
->cpu
->intr_status();
619 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
620 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
621 if (interrupts
& (ULL(1) << i
)) {
622 // See table 4-19 of 21164 hardware reference
624 summary
|= (ULL(1) << i
);
628 if (ipr
[TheISA::IPR_ASTRR
])
629 panic("asynchronous traps not implemented\n");
631 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
632 ipr
[TheISA::IPR_ISR
] = summary
;
633 ipr
[TheISA::IPR_INTID
] = ipl
;
634 xc
->ev5_trap(Interrupt_Fault
);
636 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
637 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
642 // maintain $r0 semantics
643 xc
->regs
.intRegFile
[ZeroReg
] = 0;
645 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
646 #endif // TARGET_ALPHA
648 if (status() == IcacheMissComplete
) {
649 // We've already fetched an instruction and were stalled on an
650 // I-cache miss. No need to fetch it again.
652 // Set status to running; tick event will get rescheduled if
653 // necessary at end of tick() function.
657 // Try to fetch an instruction
659 // set up memory request for instruction fetch
661 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
663 #define IFETCH_FLAGS(pc) 0
667 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
668 IFETCH_FLAGS(xc
->regs
.pc
));
670 fault
= xc
->translateInstReq(memReq
);
672 if (fault
== No_Fault
)
673 fault
= xc
->mem
->read(memReq
, inst
);
675 if (icacheInterface
&& fault
== No_Fault
) {
676 memReq
->completionEvent
= NULL
;
678 memReq
->time
= curTick
;
679 MemAccessResult result
= icacheInterface
->access(memReq
);
681 // Ugly hack to get an event scheduled *only* if the access is
682 // a miss. We really should add first-class support for this
684 if (result
!= MA_HIT
&& icacheInterface
->doEvents()) {
685 memReq
->completionEvent
= &cacheCompletionEvent
;
686 lastIcacheStall
= curTick
;
687 unscheduleTickEvent();
688 _status
= IcacheMissStall
;
694 // If we've got a valid instruction (i.e., no fault on instruction
695 // fetch), then execute it.
696 if (fault
== No_Fault
) {
698 // keep an instruction count
701 // check for instruction-count-based events
702 comInstEventQueue
[0]->serviceEvents(numInst
);
704 // decode the instruction
705 StaticInstPtr
<TheISA
> si(inst
);
707 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
711 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
712 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
713 #endif // FULL_SYSTEM
717 fault
= si
->execute(this, xc
, traceData
);
720 SWContext
*ctx
= xc
->swCtx
;
722 ctx
->process(xc
, si
.get());
725 if (si
->isMemRef()) {
731 comLoadEventQueue
[0]->serviceEvents(numLoad
);
735 traceData
->finalize();
737 } // if (fault == No_Fault)
739 if (fault
!= No_Fault
) {
742 #else // !FULL_SYSTEM
743 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
744 #endif // FULL_SYSTEM
747 // go to the next instruction
748 xc
->regs
.pc
= xc
->regs
.npc
;
749 xc
->regs
.npc
+= sizeof(MachInst
);
756 system
->pcEventQueue
.service(xc
);
757 } while (oldpc
!= xc
->regs
.pc
);
760 assert(status() == Running
||
762 status() == DcacheMissStall
);
764 if (status() == Running
&& !tickEvent
.scheduled())
765 tickEvent
.schedule(curTick
+ 1);
769 ////////////////////////////////////////////////////////////////////////
771 // SimpleCPU Simulation Object
773 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
775 Param
<Counter
> max_insts_any_thread
;
776 Param
<Counter
> max_insts_all_threads
;
777 Param
<Counter
> max_loads_any_thread
;
778 Param
<Counter
> max_loads_all_threads
;
781 SimObjectParam
<AlphaITB
*> itb
;
782 SimObjectParam
<AlphaDTB
*> dtb
;
783 SimObjectParam
<FunctionalMemory
*> mem
;
784 SimObjectParam
<System
*> system
;
787 SimObjectParam
<Process
*> workload
;
788 #endif // FULL_SYSTEM
790 SimObjectParam
<BaseMem
*> icache
;
791 SimObjectParam
<BaseMem
*> dcache
;
793 Param
<bool> defer_registration
;
795 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
797 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
799 INIT_PARAM_DFLT(max_insts_any_thread
,
800 "terminate when any thread reaches this inst count",
802 INIT_PARAM_DFLT(max_insts_all_threads
,
803 "terminate when all threads have reached this inst count",
805 INIT_PARAM_DFLT(max_loads_any_thread
,
806 "terminate when any thread reaches this load count",
808 INIT_PARAM_DFLT(max_loads_all_threads
,
809 "terminate when all threads have reached this load count",
813 INIT_PARAM(itb
, "Instruction TLB"),
814 INIT_PARAM(dtb
, "Data TLB"),
815 INIT_PARAM(mem
, "memory"),
816 INIT_PARAM(system
, "system object"),
817 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
819 INIT_PARAM(workload
, "processes to run"),
820 #endif // FULL_SYSTEM
822 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
823 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
),
824 INIT_PARAM_DFLT(defer_registration
, "defer registration with system "
825 "(for sampling)", false)
827 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
830 CREATE_SIM_OBJECT(SimpleCPU
)
835 panic("processor clock multiplier must be 1\n");
837 cpu
= new SimpleCPU(getInstanceName(), system
,
838 max_insts_any_thread
, max_insts_all_threads
,
839 max_loads_any_thread
, max_loads_all_threads
,
841 (icache
) ? icache
->getInterface() : NULL
,
842 (dcache
) ? dcache
->getInterface() : NULL
,
844 ticksPerSecond
* mult
);
847 cpu
= new SimpleCPU(getInstanceName(), workload
,
848 max_insts_any_thread
, max_insts_all_threads
,
849 max_loads_any_thread
, max_loads_all_threads
,
850 (icache
) ? icache
->getInterface() : NULL
,
851 (dcache
) ? dcache
->getInterface() : NULL
,
854 #endif // FULL_SYSTEM
859 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)